| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Runtime.Intrinsics.X86;
- using Internal.Runtime.CompilerServices;
- // Some routines inspired by the Stanford Bit Twiddling Hacks by Sean Eron Anderson:
- // http://graphics.stanford.edu/~seander/bithacks.html
- namespace System
- {
- /// <summary>
- /// Utility methods for intrinsic bit-twiddling operations.
- /// The methods use hardware intrinsics when available on the underlying platform,
- /// otherwise they use optimized software fallbacks.
- /// </summary>
- internal static class BitOps
- {
- // C# no-alloc optimization that directly wraps the data section of the dll (similar to string constants)
- // https://github.com/dotnet/roslyn/pull/24621
- private static ReadOnlySpan<byte> s_TrailingZeroCountDeBruijn => new byte[32]
- {
- 00, 01, 28, 02, 29, 14, 24, 03,
- 30, 22, 20, 15, 25, 17, 04, 08,
- 31, 27, 13, 23, 21, 19, 16, 07,
- 26, 12, 18, 06, 11, 05, 10, 09
- };
- private static ReadOnlySpan<byte> s_Log2DeBruijn => new byte[32]
- {
- 00, 09, 01, 10, 13, 21, 02, 29,
- 11, 14, 16, 18, 22, 25, 03, 30,
- 08, 12, 20, 28, 15, 17, 24, 07,
- 19, 27, 23, 06, 26, 05, 04, 31
- };
- /// <summary>
- /// Count the number of trailing zero bits in an integer value.
- /// Similar in behavior to the x86 instruction TZCNT.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int TrailingZeroCount(int value)
- => TrailingZeroCount((uint)value);
- /// <summary>
- /// Count the number of trailing zero bits in an integer value.
- /// Similar in behavior to the x86 instruction TZCNT.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int TrailingZeroCount(uint value)
- {
- if (Bmi1.IsSupported)
- {
- // TZCNT contract is 0->32
- return (int)Bmi1.TrailingZeroCount(value);
- }
- // Unguarded fallback contract is 0->0
- if (value == 0)
- {
- return 32;
- }
- // uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check
- return Unsafe.AddByteOffset(
- // Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_0111_1100_1011_0101_0011_0001u
- ref MemoryMarshal.GetReference(s_TrailingZeroCountDeBruijn),
- // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here
- (IntPtr)(int)(((value & (uint)-(int)value) * 0x077CB531u) >> 27)); // Multi-cast mitigates redundant conv.u8
- }
- /// <summary>
- /// Count the number of trailing zero bits in a mask.
- /// Similar in behavior to the x86 instruction TZCNT.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int TrailingZeroCount(long value)
- => TrailingZeroCount((ulong)value);
- /// <summary>
- /// Count the number of trailing zero bits in a mask.
- /// Similar in behavior to the x86 instruction TZCNT.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int TrailingZeroCount(ulong value)
- {
- if (Bmi1.X64.IsSupported)
- {
- // TZCNT contract is 0->64
- return (int)Bmi1.X64.TrailingZeroCount(value);
- }
- uint lo = (uint)value;
- if (lo == 0)
- {
- return 32 + TrailingZeroCount((uint)(value >> 32));
- }
- return TrailingZeroCount(lo);
- }
- /// <summary>
- /// Count the number of leading zero bits in a mask.
- /// Similar in behavior to the x86 instruction LZCNT.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int LeadingZeroCount(uint value)
- {
- if (Lzcnt.IsSupported)
- {
- // LZCNT contract is 0->32
- return (int)Lzcnt.LeadingZeroCount(value);
- }
- // Unguarded fallback contract is 0->31
- if (value == 0)
- {
- return 32;
- }
- return 31 - Log2SoftwareFallback(value);
- }
- /// <summary>
- /// Count the number of leading zero bits in a mask.
- /// Similar in behavior to the x86 instruction LZCNT.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int LeadingZeroCount(ulong value)
- {
- if (Lzcnt.X64.IsSupported)
- {
- // LZCNT contract is 0->64
- return (int)Lzcnt.X64.LeadingZeroCount(value);
- }
- uint hi = (uint)(value >> 32);
- if (hi == 0)
- {
- return 32 + LeadingZeroCount((uint)value);
- }
- return LeadingZeroCount(hi);
- }
- /// <summary>
- /// Returns the integer (floor) log of the specified value, base 2.
- /// Note that by convention, input value 0 returns 0 since Log(0) is undefined.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int Log2(uint value)
- {
- // value lzcnt actual expected
- // ..0000 32 0 0 (by convention, guard clause)
- // ..0001 31 31-31 0
- // ..0010 30 31-30 1
- // 0010.. 2 31-2 29
- // 0100.. 1 31-1 30
- // 1000.. 0 31-0 31
- if (Lzcnt.IsSupported)
- {
- // Enforce conventional contract 0->0 (Log(0) is undefined)
- if (value == 0)
- {
- return 0;
- }
- // LZCNT contract is 0->32
- return 31 - (int)Lzcnt.LeadingZeroCount(value);
- }
- // Fallback contract is 0->0
- return Log2SoftwareFallback(value);
- }
- /// <summary>
- /// Returns the integer (floor) log of the specified value, base 2.
- /// Note that by convention, input value 0 returns 0 since Log(0) is undefined.
- /// Does not directly use any hardware intrinsics, nor does it incur branching.
- /// </summary>
- /// <param name="value">The value.</param>
- private static int Log2SoftwareFallback(uint value)
- {
- // No AggressiveInlining due to large method size
- // Has conventional contract 0->0 (Log(0) is undefined)
- // Fill trailing zeros with ones, eg 00010010 becomes 00011111
- value |= value >> 01;
- value |= value >> 02;
- value |= value >> 04;
- value |= value >> 08;
- value |= value >> 16;
- // uint.MaxValue >> 27 is always in range [0 - 31] so we use Unsafe.AddByteOffset to avoid bounds check
- return Unsafe.AddByteOffset(
- // Using deBruijn sequence, k=2, n=5 (2^5=32) : 0b_0000_0111_1100_0100_1010_1100_1101_1101u
- ref MemoryMarshal.GetReference(s_Log2DeBruijn),
- // uint|long -> IntPtr cast on 32-bit platforms does expensive overflow checks not needed here
- (IntPtr)(int)((value * 0x07C4ACDDu) >> 27));
- }
- /// <summary>
- /// Returns the integer (floor) log of the specified value, base 2.
- /// Note that by convention, input value 0 returns 0 since Log(0) is undefined.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int Log2(ulong value)
- {
- if (Lzcnt.X64.IsSupported)
- {
- // Enforce conventional contract 0->0 (Log(0) is undefined)
- if (value == 0)
- {
- return 0;
- }
- // LZCNT contract is 0->64
- return 63 - (int)Lzcnt.X64.LeadingZeroCount(value);
- }
- uint hi = (uint)(value >> 32);
- if (hi == 0)
- {
- return Log2((uint)value);
- }
- return 32 + Log2(hi);
- }
- /// <summary>
- /// Rotates the specified value left by the specified number of bits.
- /// Similar in behavior to the x86 instruction ROL.
- /// </summary>
- /// <param name="value">The value to rotate.</param>
- /// <param name="offset">The number of bits to rotate by.
- /// Any value outside the range [0..31] is treated as congruent mod 32.</param>
- /// <returns>The rotated value.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static uint RotateLeft(uint value, int offset)
- => (value << offset) | (value >> (32 - offset));
- /// <summary>
- /// Rotates the specified value left by the specified number of bits.
- /// Similar in behavior to the x86 instruction ROL.
- /// </summary>
- /// <param name="value">The value to rotate.</param>
- /// <param name="offset">The number of bits to rotate by.
- /// Any value outside the range [0..63] is treated as congruent mod 64.</param>
- /// <returns>The rotated value.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ulong RotateLeft(ulong value, int offset)
- => (value << offset) | (value >> (64 - offset));
- /// <summary>
- /// Rotates the specified value right by the specified number of bits.
- /// Similar in behavior to the x86 instruction ROR.
- /// </summary>
- /// <param name="value">The value to rotate.</param>
- /// <param name="offset">The number of bits to rotate by.
- /// Any value outside the range [0..31] is treated as congruent mod 32.</param>
- /// <returns>The rotated value.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static uint RotateRight(uint value, int offset)
- => (value >> offset) | (value << (32 - offset));
- /// <summary>
- /// Rotates the specified value right by the specified number of bits.
- /// Similar in behavior to the x86 instruction ROR.
- /// </summary>
- /// <param name="value">The value to rotate.</param>
- /// <param name="offset">The number of bits to rotate by.
- /// Any value outside the range [0..63] is treated as congruent mod 64.</param>
- /// <returns>The rotated value.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ulong RotateRight(ulong value, int offset)
- => (value >> offset) | (value << (64 - offset));
- /// <summary>
- /// Returns the population count (number of bits set) of a mask.
- /// Similar in behavior to the x86 instruction POPCNT.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int PopCount(uint value)
- {
- if (Popcnt.IsSupported)
- {
- return (int)Popcnt.PopCount(value);
- }
- return SoftwareFallback(value);
- static int SoftwareFallback(uint value)
- {
- const uint c1 = 0x_55555555u;
- const uint c2 = 0x_33333333u;
- const uint c3 = 0x_0F0F0F0Fu;
- const uint c4 = 0x_01010101u;
- value = value - ((value >> 1) & c1);
- value = (value & c2) + ((value >> 2) & c2);
- value = (((value + (value >> 4)) & c3) * c4) >> 24;
- return (int)value;
- }
- }
- /// <summary>
- /// Returns the population count (number of bits set) of a mask.
- /// Similar in behavior to the x86 instruction POPCNT.
- /// </summary>
- /// <param name="value">The value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int PopCount(ulong value)
- {
- if (Popcnt.X64.IsSupported)
- {
- return (int)Popcnt.X64.PopCount(value);
- }
- #if BIT32
- return PopCount((uint)value) // lo
- + PopCount((uint)(value >> 32)); // hi
- #else
- return SoftwareFallback(value);
- static int SoftwareFallback(ulong value)
- {
- const ulong c1 = 0x_55555555_55555555ul;
- const ulong c2 = 0x_33333333_33333333ul;
- const ulong c3 = 0x_0F0F0F0F_0F0F0F0Ful;
- const ulong c4 = 0x_01010101_01010101ul;
- value = value - ((value >> 1) & c1);
- value = (value & c2) + ((value >> 2) & c2);
- value = (((value + (value >> 4)) & c3) * c4) >> 56;
- return (int)value;
- }
- #endif
- }
- }
- }
|