Skip to content

Commit dde1b38

Browse files
authored
Refactor FNV1Base class with Endianness, renaming to primitives and enhanced comment
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent 54405bc commit dde1b38

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

HashifyNet/Algorithms/FNV/FNV1Base.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ protected abstract class BlockTransformer_32BitBase<TSelf>
7979
: BlockTransformerBase<TSelf>
8080
where TSelf : BlockTransformer_32BitBase<TSelf>, new()
8181
{
82-
protected UInt32 _prime;
83-
protected UInt32 _hashValue;
82+
protected uint _prime;
83+
protected uint _hashValue;
8484

8585
public BlockTransformer_32BitBase()
8686
{
@@ -99,22 +99,23 @@ protected override void CopyStateTo(TSelf other)
9999
other._hashValue = _hashValue;
100100
}
101101
protected override IHashValue FinalizeHashValueInternal(CancellationToken cancellationToken) =>
102-
new HashValue(BitConverter.GetBytes(_hashValue), 32);
102+
new HashValue(Endianness.GetBytesLittleEndian(_hashValue), 32);
103103
}
104+
104105
protected abstract class BlockTransformer_64BitBase<TSelf>
105106
: BlockTransformerBase<TSelf>
106107
where TSelf : BlockTransformer_64BitBase<TSelf>, new()
107108
{
108-
protected UInt64 _prime;
109-
protected UInt64 _hashValue;
109+
protected ulong _prime;
110+
protected ulong _hashValue;
110111
public BlockTransformer_64BitBase()
111112
{
112113
}
113114
public BlockTransformer_64BitBase(FNVPrimeOffset fnvPrimeOffset)
114115
: this()
115116
{
116-
_prime = ((UInt64)fnvPrimeOffset.Prime[1] << 32) | fnvPrimeOffset.Prime[0];
117-
_hashValue = ((UInt64)fnvPrimeOffset.Offset[1] << 32) | fnvPrimeOffset.Offset[0];
117+
_prime = ((ulong)fnvPrimeOffset.Prime[1] << 32) | fnvPrimeOffset.Prime[0];
118+
_hashValue = ((ulong)fnvPrimeOffset.Offset[1] << 32) | fnvPrimeOffset.Offset[0];
118119
}
119120

120121
protected override void CopyStateTo(TSelf other)
@@ -125,15 +126,15 @@ protected override void CopyStateTo(TSelf other)
125126
}
126127

127128
protected override IHashValue FinalizeHashValueInternal(CancellationToken cancellationToken) =>
128-
new HashValue(BitConverter.GetBytes(_hashValue), 64);
129+
new HashValue(Endianness.GetBytesLittleEndian(_hashValue), 64);
129130
}
130131

131132
protected abstract class BlockTransformer_ExtendedBase<TSelf>
132133
: BlockTransformerBase<TSelf>
133134
where TSelf : BlockTransformer_ExtendedBase<TSelf>, new()
134135
{
135-
protected UInt32[] _prime;
136-
protected UInt32[] _hashValue;
136+
protected uint[] _prime;
137+
protected uint[] _hashValue;
137138
protected int _hashSizeInBytes;
138139

139140
public BlockTransformer_ExtendedBase()
@@ -164,16 +165,16 @@ protected override IHashValue FinalizeHashValueInternal(CancellationToken cancel
164165
/// <summary>
165166
/// Multiplies operand1 by operand2 as if both operand1 and operand2 were single large integers.
166167
/// </summary>
167-
/// <param name="operand1">Array of UInt32 values to be multiplied.</param>
168-
/// <param name="operand2">Array of UInt32 values to multiply by.</param>
168+
/// <param name="operand1">Array of <see cref="uint"/> values to be multiplied.</param>
169+
/// <param name="operand2">Array of <see cref="uint"/> values to multiply by.</param>
169170
/// <param name="hashSizeInBytes">Hash size, in bytes, to truncate products at.</param>
170171
/// <returns></returns>
171-
protected static UInt32[] ExtendedMultiply(IReadOnlyList<UInt32> operand1, IReadOnlyList<UInt32> operand2, int hashSizeInBytes)
172+
protected static uint[] ExtendedMultiply(IReadOnlyList<uint> operand1, IReadOnlyList<uint> operand2, int hashSizeInBytes)
172173
{
173174
Debug.Assert(hashSizeInBytes % 4 == 0);
174175

175176
// Temporary array to hold the results of 32-bit multiplication.
176-
var product = new UInt32[hashSizeInBytes / 4];
177+
var product = new uint[hashSizeInBytes / 4];
177178

178179
// Bottom of equation
179180
for (int y = 0; y < operand2.Count; ++y)
@@ -184,7 +185,7 @@ protected static UInt32[] ExtendedMultiply(IReadOnlyList<UInt32> operand1, IRead
184185
continue;
185186
}
186187

187-
UInt32 carryOver = 0;
188+
uint carryOver = 0;
188189

189190
// Top of equation
190191
for (int x = 0; x < operand1.Count; ++x)
@@ -194,19 +195,19 @@ protected static UInt32[] ExtendedMultiply(IReadOnlyList<UInt32> operand1, IRead
194195
break;
195196
}
196197

197-
var productResult = product[x + y] + (((UInt64)operand2[y]) * operand1[x]) + carryOver;
198-
product[x + y] = (UInt32)productResult;
198+
var productResult = product[x + y] + (((ulong)operand2[y]) * operand1[x]) + carryOver;
199+
product[x + y] = (uint)productResult;
199200

200-
carryOver = (UInt32)(productResult >> 32);
201+
carryOver = (uint)(productResult >> 32);
201202
}
202203
}
203204

204205
return product;
205206
}
206207

207-
private static IEnumerable<byte> UInt32ArrayToBytes(IEnumerable<UInt32> values)
208+
private static IEnumerable<byte> UInt32ArrayToBytes(IEnumerable<uint> values)
208209
{
209-
return values.SelectMany(v => BitConverter.GetBytes(v));
210+
return values.SelectMany(v => Endianness.GetBytesLittleEndian(v));
210211
}
211212
}
212-
}
213+
}

0 commit comments

Comments
 (0)