Skip to content

Commit eeb97e2

Browse files
committed
Use type punning
1 parent 202241c commit eeb97e2

File tree

1 file changed

+4
-3
lines changed
  • libclc/native_cpu-unknown-linux/libspirv/integer

1 file changed

+4
-3
lines changed

libclc/native_cpu-unknown-linux/libspirv/integer/popcount.cl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
// We can't use __builtin_popcountg because it supports only unsigned
66
// types, and we can't use __builtin_popcount because the implicit cast
7-
// to int doesn't work due to sign extension, so we explictly cast to
8-
// the unsigned type.
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+
910
#define DEF_POPCOUNT_HELPER(TYPE, UTYPE) \
1011
_CLC_OVERLOAD TYPE __popcount_helper(TYPE c) { \
11-
return __builtin_popcountg((UTYPE)c); \
12+
return __builtin_popcountg(*(UTYPE*)&c); \
1213
}
1314

1415
DEF_POPCOUNT_HELPER(char, unsigned char)

0 commit comments

Comments
 (0)