Move helper to main utility class

This commit is contained in:
Matt Nadareski
2024-11-09 22:06:46 -05:00
parent 89582a56ac
commit 1b06751c68
4 changed files with 27 additions and 20 deletions

View File

@@ -2,22 +2,6 @@ namespace SabreTools.Hashing.Checksum
{
internal static class BitOperations
{
/// <summary>
/// Reverse the endianness of a value
/// </summary>
public static ulong ReverseBits(ulong value, int bitWidth)
{
ulong reverse = 0;
for (int i = 0; i < bitWidth; i++)
{
reverse <<= 1;
reverse |= value & 1;
value >>= 1;
}
return reverse;
}
/// <summary>
/// Clamp a value to a certain bit width and convert to a byte array
/// </summary>

View File

@@ -1,4 +1,5 @@
using System;
using static SabreTools.Hashing.HashOperations;
namespace SabreTools.Hashing.Checksum
{
@@ -27,7 +28,7 @@ namespace SabreTools.Hashing.Checksum
Def = def;
_table = new CrcTable(def);
_hash = def.ReflectIn ? BitOperations.ReverseBits(def.Init, def.Width) : def.Init;
_hash = def.ReflectIn ? ReverseBits(def.Init, def.Width) : def.Init;
}
/// <summary>
@@ -57,7 +58,7 @@ namespace SabreTools.Hashing.Checksum
// Handle mutual reflection
if (Def.ReflectIn ^ Def.ReflectOut)
localHash = BitOperations.ReverseBits(localHash, Def.Width);
localHash = ReverseBits(localHash, Def.Width);
// Handle XOR
localHash ^= Def.XorOut;

View File

@@ -1,3 +1,5 @@
using static SabreTools.Hashing.HashOperations;
namespace SabreTools.Hashing.Checksum
{
internal class CrcTable
@@ -55,7 +57,7 @@ namespace SabreTools.Hashing.Checksum
// Get the starting value for this index
ulong point = i;
if (!_processBitwise && def.ReflectIn)
point = BitOperations.ReverseBits(point, _processBits);
point = ReverseBits(point, _processBits);
// Shift to account for storage
point <<= _definition.Width - _processBits;
@@ -71,7 +73,7 @@ namespace SabreTools.Hashing.Checksum
// Reflect if necessary
if (def.ReflectIn)
point = BitOperations.ReverseBits(point, def.Width);
point = ReverseBits(point, def.Width);
// Shift back to account for storage
point &= ulong.MaxValue >> (64 - def.Width);

View File

@@ -62,6 +62,26 @@ namespace SabreTools.Hashing
#endregion
#region Reverse
/// <summary>
/// Reverse the endianness of a value
/// </summary>
public static ulong ReverseBits(ulong value, int bitWidth)
{
ulong reverse = 0;
for (int i = 0; i < bitWidth; i++)
{
reverse <<= 1;
reverse |= value & 1;
value >>= 1;
}
return reverse;
}
#endregion
#region Rotate
/// <summary>