Browse Source

Fix BitOperations

pull/1632/head
James Jackson-South 5 years ago
parent
commit
5ea8da6c97
  1. 33
      src/ImageSharp/Common/Helpers/Numerics.cs

33
src/ImageSharp/Common/Helpers/Numerics.cs

@ -23,6 +23,28 @@ namespace SixLabors.ImageSharp
private const int ShuffleAlphaControl = 0b_11_11_11_11; private const int ShuffleAlphaControl = 0b_11_11_11_11;
#endif #endif
#if !SUPPORTS_BITOPERATIONS
/// <summary>
/// Gets the counts the number of bits needed to hold an integer.
/// </summary>
private static ReadOnlySpan<byte> BitCountLut => new byte[]
{
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8,
};
#endif
/// <summary> /// <summary>
/// Determine the Greatest CommonDivisor (GCD) of two numbers. /// Determine the Greatest CommonDivisor (GCD) of two numbers.
/// </summary> /// </summary>
@ -756,7 +778,7 @@ namespace SixLabors.ImageSharp
/// widening them to 32-bit integers and performing four additions. /// widening them to 32-bit integers and performing four additions.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <code>byte(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)</code> /// <c>byte(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)</c>
/// is widened and added onto <paramref name="accumulator"/> as such: /// is widened and added onto <paramref name="accumulator"/> as such:
/// <code> /// <code>
/// accumulator += i32(1, 2, 3, 4); /// accumulator += i32(1, 2, 3, 4);
@ -834,8 +856,17 @@ namespace SixLabors.ImageSharp
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int MinimumBitsToStore(uint number) public static int MinimumBitsToStore(uint number)
{ {
#if !SUPPORTS_BITOPERATIONS
if (number < 0x100)
{
return BitCountLut[(int)number];
}
return 8 + BitCountLut[(int)number >> 8];
#else
const int bitInUnsignedInteger = sizeof(uint) * 8; const int bitInUnsignedInteger = sizeof(uint) * 8;
return bitInUnsignedInteger - BitOperations.LeadingZeroCount(number); return bitInUnsignedInteger - BitOperations.LeadingZeroCount(number);
#endif
} }
} }
} }

Loading…
Cancel
Save