|
2 | 2 | #include <clcmacro.h> |
3 | 3 | #include <spirv/spirv.h> |
4 | 4 |
|
| 5 | +// We can't use __builtin_popcountg because it supports only unsigned |
| 6 | +// types, and we can't use __builtin_popcount because the implicit cast |
| 7 | +// to int doesn't work due to sign extension, so we use type punning to |
| 8 | +// preserve the bit pattern and avoid sign extension. |
| 9 | + |
| 10 | +#define DEF_POPCOUNT_HELPER(TYPE, UTYPE) \ |
| 11 | +_CLC_OVERLOAD TYPE __popcount_helper(TYPE c) { \ |
| 12 | + return __builtin_popcountg(*(UTYPE*)&c); \ |
| 13 | +} |
| 14 | + |
| 15 | +DEF_POPCOUNT_HELPER(char, unsigned char) |
| 16 | +DEF_POPCOUNT_HELPER(schar, unsigned char) |
| 17 | +DEF_POPCOUNT_HELPER(short, unsigned short) |
| 18 | + |
5 | 19 | _CLC_DEFINE_UNARY_BUILTIN(int, __spirv_ocl_popcount, __builtin_popcount, int) |
6 | 20 | _CLC_DEFINE_UNARY_BUILTIN(uint, __spirv_ocl_popcount, __builtin_popcount, uint) |
7 | | -_CLC_DEFINE_UNARY_BUILTIN(short, __spirv_ocl_popcount, __builtin_popcount, short) |
8 | | -_CLC_DEFINE_UNARY_BUILTIN(ushort, __spirv_ocl_popcount, __builtin_popcount, ushort) |
9 | | -_CLC_DEFINE_UNARY_BUILTIN(long, __spirv_ocl_popcount, __builtin_popcount, long) |
10 | | -_CLC_DEFINE_UNARY_BUILTIN(ulong, __spirv_ocl_popcount, __builtin_popcount, ulong) |
11 | | -_CLC_DEFINE_UNARY_BUILTIN(char, __spirv_ocl_popcount, __builtin_popcount, char) |
12 | | -_CLC_DEFINE_UNARY_BUILTIN(uchar, __spirv_ocl_popcount, __builtin_popcount, uchar) |
13 | | -_CLC_DEFINE_UNARY_BUILTIN(schar, __spirv_ocl_popcount, __builtin_popcount, schar) |
| 21 | +_CLC_DEFINE_UNARY_BUILTIN(short, __spirv_ocl_popcount, __popcount_helper, short) |
| 22 | +_CLC_DEFINE_UNARY_BUILTIN(ushort, __spirv_ocl_popcount, __builtin_popcountg, ushort) |
| 23 | +_CLC_DEFINE_UNARY_BUILTIN(long, __spirv_ocl_popcount, __builtin_popcountl, long) |
| 24 | +_CLC_DEFINE_UNARY_BUILTIN(ulong, __spirv_ocl_popcount, __builtin_popcountl, ulong) |
| 25 | +_CLC_DEFINE_UNARY_BUILTIN(char, __spirv_ocl_popcount, __popcount_helper, char) |
| 26 | +_CLC_DEFINE_UNARY_BUILTIN(uchar, __spirv_ocl_popcount, __builtin_popcountg, uchar) |
| 27 | +_CLC_DEFINE_UNARY_BUILTIN(schar, __spirv_ocl_popcount, __popcount_helper, schar) |
0 commit comments