Skip to content

Commit fa9f94d

Browse files
authored
Update PAL and GC BitScanForward to use __builtin_clz (#89350)
The existing PAL code was using `__builtin_clzl` which is intended for platforms where `long` is 64 bits. Instead use `__builtin_clz`. The GC version had a similar issue so I've changed that too. The JIT version was already using `__builtin_clz`. Fixes #89340.
1 parent a2ce877 commit fa9f94d

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/coreclr/gc/env/gcenv.base.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,12 @@ inline uint8_t BitScanReverse(uint32_t *bitIndex, uint32_t mask)
300300
#ifdef _MSC_VER
301301
return _BitScanReverse((unsigned long*)bitIndex, mask);
302302
#else // _MSC_VER
303-
// The result of __builtin_clzl is undefined when mask is zero,
303+
// The result of __builtin_clz is undefined when mask is zero,
304304
// but it's still OK to call the intrinsic in that case (just don't use the output).
305305
// Unconditionally calling the intrinsic in this way allows the compiler to
306306
// emit branchless code for this function when possible (depending on how the
307307
// intrinsic is implemented for the target platform).
308-
int lzcount = __builtin_clzl(mask);
308+
int lzcount = __builtin_clz(mask);
309309
*bitIndex = static_cast<uint32_t>(31 - lzcount);
310310
return mask != 0 ? TRUE : FALSE;
311311
#endif // _MSC_VER

src/coreclr/pal/inc/pal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,7 +3458,7 @@ BitScanForward64(
34583458
// intrinsic, which returns the number of leading 0-bits in x starting at the most significant
34593459
// bit position (the result is undefined when x = 0).
34603460
//
3461-
// The same is true for BitScanReverse, except that the GCC function is __builtin_clzl.
3461+
// The same is true for BitScanReverse, except that the GCC function is __builtin_clz.
34623462

34633463
EXTERN_C
34643464
PALIMPORT
@@ -3469,12 +3469,12 @@ BitScanReverse(
34693469
IN OUT PDWORD Index,
34703470
IN UINT qwMask)
34713471
{
3472-
// The result of __builtin_clzl is undefined when qwMask is zero,
3472+
// The result of __builtin_clz is undefined when qwMask is zero,
34733473
// but it's still OK to call the intrinsic in that case (just don't use the output).
34743474
// Unconditionally calling the intrinsic in this way allows the compiler to
34753475
// emit branchless code for this function when possible (depending on how the
34763476
// intrinsic is implemented for the target platform).
3477-
int lzcount = __builtin_clzl(qwMask);
3477+
int lzcount = __builtin_clz(qwMask);
34783478
*Index = (DWORD)(31 - lzcount);
34793479
return qwMask != 0;
34803480
}

0 commit comments

Comments
 (0)