File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change @@ -47,28 +47,31 @@ __forceinline size_t SAFE_SHIFT_RIGHT(size_t x, size_t count)
47
47
48
48
inline UINT32 CeilOfLog2 (size_t x )
49
49
{
50
- // we want lzcnt, but bsr is ok too
50
+ // it is ok to use bsr or clz unconditionally
51
51
_ASSERTE (x > 0 );
52
52
53
53
x = (x << 1 ) - 1 ;
54
54
55
55
#ifdef TARGET_64BIT
56
56
#ifdef _MSC_VER
57
- DWORD lzcountCeil ;
58
- _BitScanReverse64 (& lzcountCeil , (unsigned long )x );
57
+ DWORD result ;
58
+ _BitScanReverse64 (& result , (unsigned long )x );
59
+ return (UINT32 )result ;
59
60
#else // _MSC_VER
60
- UINT32 lzcountCeil = (UINT32 )__builtin_clzl ((unsigned long )x );
61
+ // LZCNT returns index starting from MSB, whereas BSR gives the index from LSB.
62
+ // 63 ^ BSR here is equivalent to 63 - BSR since the BSR result is always between 0 and 63.
63
+ // This saves an instruction, as subtraction from constant requires either MOV/SUB or NEG/ADD.
64
+ return (UINT32 )63 ^ (UINT32 )__builtin_clzl ((unsigned long )x );
61
65
#endif // _MSC_VER
62
66
#else // TARGET_64BIT
63
67
#ifdef _MSC_VER
64
- DWORD lzcountCeil ;
65
- _BitScanReverse (& lzcountCeil , (unsigned long )x );
68
+ DWORD result ;
69
+ _BitScanReverse (& result , (unsigned int )x );
70
+ return (UINT32 )result ;
66
71
#else // _MSC_VER
67
- UINT32 lzcountCeil = (UINT32 )__builtin_clz ((unsigned int )x );
72
+ return ( UINT32 ) 31 ^ (UINT32 )__builtin_clz ((unsigned int )x );
68
73
#endif // _MSC_VER
69
74
#endif
70
-
71
- return BITS_PER_SIZE_T - lzcountCeil ;
72
75
}
73
76
74
77
enum GcSlotFlags
You can’t perform that action at this time.
0 commit comments