-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCrcConfig32.cs
290 lines (266 loc) · 11.6 KB
/
CrcConfig32.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
namespace Roydl.Crypto.Checksum
{
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics.X86;
using Internal;
using Resources;
#if NET5_0_OR_GREATER
using Arm = System.Runtime.Intrinsics.Arm.Crc32;
using Arm64 = System.Runtime.Intrinsics.Arm.Crc32.Arm64;
#endif
/// <summary>Represents a 32-bit CRC configuration structure.</summary>
public readonly struct CrcConfig32 : ICrcConfig<uint>
{
private const int Columns = 1 << 8;
private const int Rows = 1 << 4;
private readonly int _mode;
/// <inheritdoc/>
public int BitWidth { get; }
/// <inheritdoc/>
public uint Check { get; }
/// <inheritdoc/>
public uint Mask { get; }
/// <inheritdoc/>
public uint Poly { get; }
/// <inheritdoc/>
public uint Init { get; }
/// <inheritdoc/>
public bool RefIn { get; }
/// <inheritdoc/>
public bool RefOut { get; }
/// <inheritdoc/>
public uint XorOut { get; }
/// <inheritdoc/>
public ReadOnlyMemory<uint> Table { get; }
/// <summary>Creates a new configuration of the <see cref="CrcConfig32"/> struct.</summary>
/// <inheritdoc cref="CrcConfig(int, byte, byte, byte, bool, bool, byte, byte, bool)"/>
public CrcConfig32(int bitWidth, uint check, uint poly, uint init = default, bool refIn = false, bool refOut = false, uint xorOut = default, uint mask = default, bool skipValidation = false)
{
if (bitWidth < 8)
throw new ArgumentOutOfRangeException(nameof(bitWidth), bitWidth, null);
if (sizeof(uint) < (int)MathF.Floor(bitWidth / 8f))
throw new ArgumentException(ExceptionMessages.ArgumentBitsTypeRatioInvalid);
if (mask == default)
mask = NumericHelper.CreateBitMask<uint>(bitWidth);
BitWidth = bitWidth;
Check = check;
Poly = poly;
Init = init;
RefIn = refIn;
RefOut = refOut;
XorOut = xorOut;
Mask = mask;
_mode = bitWidth switch
{
32 => check switch
{
0xe3069283u => 1, // `iSCSI` config, which is available for hardware mode on `ARM` and `SSE4.2 CPU`
0xcbf43926u => 2, // `PKZip` config, which is only available for hardware mode on `ARM`
_ => 0 // Default
},
_ => 0 // Software mode by default
};
switch (_mode)
{
#if NET5_0_OR_GREATER
case > 0 when Arm.IsSupported || Arm64.IsSupported:
#endif
case 1 when Sse42.IsSupported || Sse42.X64.IsSupported:
Table = null;
break;
default:
Table = CreateTable(bitWidth, poly, mask, refIn);
break;
}
if (!skipValidation)
CrcConfig.ThrowIfInvalid(this);
}
/// <inheritdoc/>
public void ComputeHash(Stream stream, out uint hash)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead)
throw new NotSupportedException(ExceptionMessages.NotSupportedStreamRead);
var sum = Init;
Span<byte> bytes = stackalloc byte[stream.GetBufferSize()];
int len;
while ((len = stream.Read(bytes)) > 0)
AppendData(bytes, len, ref sum);
FinalizeHash(ref sum);
hash = sum;
}
/// <inheritdoc/>
public void ComputeHash(ReadOnlySpan<byte> bytes, out uint hash)
{
if (bytes.IsEmpty)
throw new ArgumentException(ExceptionMessages.ArgumentEmpty, nameof(bytes));
var sum = Init;
AppendData(bytes, bytes.Length, ref sum);
FinalizeHash(ref sum);
hash = sum;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void AppendData(ReadOnlySpan<byte> bytes, int len, ref uint hash)
{
if (bytes.IsEmpty)
throw new ArgumentException(ExceptionMessages.ArgumentEmpty, nameof(bytes));
var sum = hash;
var i = 0;
fixed (byte* input = bytes)
{
const int size32 = sizeof(uint);
const int size64 = sizeof(ulong);
switch (_mode)
{
/*
adding `if` or `switch` inside `while` or `for` results in
significant performance degradation, so we cannot simply
merge the loops below
*/
#if NET5_0_OR_GREATER
case > 0 when Arm64.IsSupported:
{
if (_mode > 1)
for (; len >= size64; i += size64, len -= size64)
sum = Arm64.ComputeCrc32(sum, Unsafe.Read<ulong>(input + i));
else
for (; len >= size64; i += size64, len -= size64)
sum = Arm64.ComputeCrc32C(sum, Unsafe.Read<ulong>(input + i));
while (--len >= 0)
AppendData(input[i++], ref sum);
hash = sum;
return;
}
#endif
case 1 when Sse42.IsSupported:
{
if (Sse42.X64.IsSupported)
{
ulong sum64 = sum;
for (; len >= size64; i += size64, len -= size64)
sum64 = Sse42.X64.Crc32(sum64, Unsafe.Read<ulong>(input + i));
if (sum != sum64)
sum = (uint)(sum64 & Mask);
}
for (; len >= size32; i += size32, len -= size32)
sum = Sse42.Crc32(sum, Unsafe.Read<uint>(input + i));
while (--len >= 0)
AppendData(input[i++], ref sum);
hash = sum;
return;
}
}
fixed (uint* table = Table.Span)
{
/*
replacing `i + pos++` with `i++` or replacing `--row` with
constants, both lead to a significant drop in performance
*/
while (RefIn && len >= Rows)
{
var row = Rows;
var pos = 0;
sum = (Unsafe.Read<uint>(table + --row * Columns + (((sum >> 00) & 0xff) ^ Unsafe.Read<byte>(input + i + pos++))) ^
Unsafe.Read<uint>(table + --row * Columns + (((sum >> 08) & 0xff) ^ Unsafe.Read<byte>(input + i + pos++))) ^
Unsafe.Read<uint>(table + --row * Columns + (((sum >> 16) & 0xff) ^ Unsafe.Read<byte>(input + i + pos++))) ^
Unsafe.Read<uint>(table + --row * Columns + (((sum >> 24) & 0xff) ^ Unsafe.Read<byte>(input + i + pos++))) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos++)) ^
Unsafe.Read<uint>(table + --row * Columns + Unsafe.Read<byte>(input + i + pos))) & Mask;
i += Rows;
len -= Rows;
}
while (--len >= 0)
AppendData(input[i++], table, ref sum);
}
}
hash = sum;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void AppendData(byte value, ref uint hash)
{
switch (_mode)
{
#if NET5_0_OR_GREATER
case 2 when Arm.IsSupported:
hash = Arm.ComputeCrc32(hash, value);
return;
case 1 when Arm.IsSupported:
hash = Arm.ComputeCrc32C(hash, value);
return;
#endif
case 1 when Sse42.IsSupported:
hash = Sse42.Crc32(hash, value);
return;
}
fixed (uint* table = Table.Span)
AppendData(value, table, ref hash);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void FinalizeHash(ref uint hash)
{
if (!RefIn && RefOut)
hash = hash.ReverseBits();
else if (RefIn ^ RefOut)
hash = ~hash;
hash ^= XorOut;
}
/// <inheritdoc/>
public bool IsValid(out uint current)
{
ComputeHash(CrcConfig.ValidationBytes, out current);
return current == Check;
}
/// <inheritdoc/>
public bool IsValid() =>
IsValid(out _);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void AppendData(byte value, uint* table, ref uint hash)
{
if (RefIn)
hash = ((hash >> 8) ^ Unsafe.Read<uint>(table + (value ^ (hash & 0xff)))) & Mask;
else
hash = (Unsafe.Read<uint>(table + (((hash >> (BitWidth - 8)) ^ value) & 0xff)) ^ (hash << 8)) & Mask;
}
private static ReadOnlyMemory<uint> CreateTable(int bitWidth, uint poly, uint mask, bool refIn)
{
var top = 1u << (bitWidth - 1);
var rows = refIn ? Rows : 1;
var mem = new uint[rows * Columns].AsMemory();
var span = mem.Span;
for (var i = 0; i < Columns; i++)
{
var x = (uint)i;
for (var j = 0; j < rows; j++)
{
if (refIn)
for (var k = 0; k < 8; k++)
x = (x & 1) == 1 ? (x >> 1) ^ poly : x >> 1;
else
{
x <<= bitWidth - 8;
for (var k = 0; k < 8; k++)
x = (x & top) != 0 ? (x << 1) ^ poly : x << 1;
}
span[j * Columns + i] = x & mask;
}
}
return mem;
}
}
}