diff options
| author | LDj3SNuD <35856442+LDj3SNuD@users.noreply.github.com> | 2019-12-07 13:45:32 +0100 |
|---|---|---|
| committer | Ac_K <Acoustik666@gmail.com> | 2019-12-07 13:45:32 +0100 |
| commit | 8c85bdf2edf5ebd7965fbbd08106f2e8d877d73e (patch) | |
| tree | 1f112a5d4a6dc0b4c8882b8151623e006172bb6c /ARMeilleure/Common/BitUtils.cs | |
| parent | d562ba37a0bc603e9719bb36dc9e7e9bf4406687 (diff) | |
Implemented fast paths for: (#841)
* cpu-misc_opt
* B = ~b
* ;
Diffstat (limited to 'ARMeilleure/Common/BitUtils.cs')
| -rw-r--r-- | ARMeilleure/Common/BitUtils.cs | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/ARMeilleure/Common/BitUtils.cs b/ARMeilleure/Common/BitUtils.cs index 55344608..7a29dcff 100644 --- a/ARMeilleure/Common/BitUtils.cs +++ b/ARMeilleure/Common/BitUtils.cs @@ -1,12 +1,12 @@ -using System.Runtime.CompilerServices; - namespace ARMeilleure.Common { static class BitUtils { private const int DeBrujinSequence = 0x77cb531; - private static int[] DeBrujinLbsLut; + private static readonly int[] DeBrujinLbsLut; + + private static readonly sbyte[] HbsNibbleLut; static BitUtils() { @@ -18,19 +18,27 @@ namespace ARMeilleure.Common DeBrujinLbsLut[lutIndex] = index; } + + HbsNibbleLut = new sbyte[] { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 }; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int LowestBitSet(int value) + public static int CountBits(int value) { - if (value == 0) + int count = 0; + + while (value != 0) { - return -1; + value &= ~(value & -value); + + count++; } - int lsb = value & -value; + return count; + } - return DeBrujinLbsLut[(uint)(DeBrujinSequence * lsb) >> 27]; + public static long FillWithOnes(int bits) + { + return bits == 64 ? -1L : (1L << bits) - 1; } public static int HighestBitSet(int value) @@ -51,39 +59,33 @@ namespace ARMeilleure.Common return -1; } - private static readonly sbyte[] HbsNibbleLut = { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 }; - - public static int HighestBitSetNibble(int value) => HbsNibbleLut[value & 0b1111]; - - public static long Replicate(long bits, int size) + public static int HighestBitSetNibble(int value) { - long output = 0; + return HbsNibbleLut[value]; + } - for (int bit = 0; bit < 64; bit += size) + public static int LowestBitSet(int value) + { + if (value == 0) { - output |= bits << bit; + return -1; } - return output; + int lsb = value & -value; + + return DeBrujinLbsLut[(uint)(DeBrujinSequence * lsb) >> 27]; } - public static int CountBits(int value) + public static long Replicate(long bits, int size) { - int count = 0; + long output = 0; - while (value != 0) + for (int bit = 0; bit < 64; bit += size) { - value &= ~(value & -value); - - count++; + output |= bits << bit; } - return count; - } - - public static long FillWithOnes(int bits) - { - return bits == 64 ? -1L : (1L << bits) - 1; + return output; } public static int RotateRight(int bits, int shift, int size) |
