Skip to content

Commit a851d51

Browse files
committed
Remove unsafe code from Log2SoftwareFallback
1 parent cf2786c commit a851d51

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

src/libraries/System.Private.CoreLib/src/System/Numerics/BitOperations.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,8 @@ private static int Log2SoftwareFallback(uint value)
389389
value |= value >> 08;
390390
value |= value >> 16;
391391

392-
// uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check
393-
return Unsafe.AddByteOffset(
394-
// Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_1100_0100_1010_1100_1101_1101u
395-
ref MemoryMarshal.GetReference(Log2DeBruijn),
396-
// uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here
397-
(IntPtr)(int)((value * 0x07C4ACDDu) >> 27));
392+
// Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_1100_0100_1010_1100_1101_1101u
393+
return Log2DeBruijn[(int)((value * 0x07C4ACDDu) >> 27)];
398394
}
399395

400396
/// <summary>Returns the integer (ceiling) log of the specified value, base 2.</summary>

0 commit comments

Comments
 (0)