mirror of
https://github.com/SabreTools/SabreTools.Hashing.git
synced 2026-07-08 18:06:40 +00:00
Move helper to main utility class
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user