authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-26 15:25:42-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-26 16:07:53-07:00
log3a11c86837562dd6caf143e21ca8562a8374b08a
tree06db7adfba7cc3878636b2b3ab9ab4b3b6dc833c
parent32b44497e228c676de17d6c2a2ce78cf145a97c9

std.hash.crc: fix namespace issues

in accordance with the style guide in the langref

5 files changed, 220 insertions(+), 237 deletions(-)

lib/std/debug/ElfFile.zig+1-1
......@@ -455,7 +455,7 @@ fn loadInner(
455455 };
456456
457457 if (opt_crc) |crc| {
458 if (std.hash.crc.Crc32.hash(mapped_mem) != crc) {
458 if (std.hash.Crc32.hash(mapped_mem) != crc) {
459459 return error.CrcMismatch;
460460 }
461461 }
lib/std/hash.zig+1-2
......@@ -5,9 +5,8 @@ pub const autoHash = auto_hash.autoHash;
55pub const autoHashStrat = auto_hash.hash;
66pub const Strategy = auto_hash.HashStrategy;
77
8// pub for polynomials + generic crc32 construction
98pub const crc = @import("hash/crc.zig");
10pub const Crc32 = crc.Crc32;
9pub const Crc32 = crc.Crc32IsoHdlc;
1110
1211const fnv = @import("hash/fnv.zig");
1312pub const Fnv1a_32 = fnv.Fnv1a_32;
lib/std/hash/benchmark.zig+1-1
......@@ -61,7 +61,7 @@ const hashes = [_]Hash{
6161 .init_default = true,
6262 },
6363 Hash{
64 .ty = hash.crc.Crc32,
64 .ty = hash.Crc32,
6565 .name = "crc32",
6666 },
6767 Hash{
lib/std/hash/crc.zig+217-130
......@@ -1,31 +1,19 @@
1//! This file is auto-generated by tools/update_crc_catalog.zig.
1//! This file is maintained with the help of tools/update_crc_catalog.zig.
22
33const builtin = @import("builtin");
4const impl = @import("crc/impl.zig");
54
6pub const Crc = impl.Crc;
7pub const Polynomial = impl.Polynomial;
8pub const Crc32WithPoly = impl.Crc32WithPoly;
9pub const Crc32SmallWithPoly = impl.Crc32SmallWithPoly;
10
11pub const Crc32 = Crc32IsoHdlc;
12
13test {
14 _ = @import("crc/test.zig");
15}
16
17pub const Crc32Iscsi = switch (builtin.cpu.hasAll(.x86, &.{ .@"64bit", .crc32 })) {
18 true => @import("crc/Crc32c.zig"),
19 else => Crc(u32, .{
5pub const Crc32Iscsi = if (builtin.cpu.hasAll(.x86, &.{ .@"64bit", .crc32 }))
6 @import("crc/Crc32c.zig")
7else
8 Generic(u32, .{
209 .polynomial = 0x1edc6f41,
2110 .initial = 0xffffffff,
2211 .reflect_input = true,
2312 .reflect_output = true,
2413 .xor_output = 0xffffffff,
25 }),
26};
14 });
2715
28pub const Crc3Gsm = Crc(u3, .{
16pub const Crc3Gsm = Generic(u3, .{
2917 .polynomial = 0x3,
3018 .initial = 0x0,
3119 .reflect_input = false,
......@@ -33,7 +21,7 @@ pub const Crc3Gsm = Crc(u3, .{
3321 .xor_output = 0x7,
3422});
3523
36pub const Crc3Rohc = Crc(u3, .{
24pub const Crc3Rohc = Generic(u3, .{
3725 .polynomial = 0x3,
3826 .initial = 0x7,
3927 .reflect_input = true,
......@@ -41,7 +29,7 @@ pub const Crc3Rohc = Crc(u3, .{
4129 .xor_output = 0x0,
4230});
4331
44pub const Crc4G704 = Crc(u4, .{
32pub const Crc4G704 = Generic(u4, .{
4533 .polynomial = 0x3,
4634 .initial = 0x0,
4735 .reflect_input = true,
......@@ -49,7 +37,7 @@ pub const Crc4G704 = Crc(u4, .{
4937 .xor_output = 0x0,
5038});
5139
52pub const Crc4Interlaken = Crc(u4, .{
40pub const Crc4Interlaken = Generic(u4, .{
5341 .polynomial = 0x3,
5442 .initial = 0xf,
5543 .reflect_input = false,
......@@ -57,7 +45,7 @@ pub const Crc4Interlaken = Crc(u4, .{
5745 .xor_output = 0xf,
5846});
5947
60pub const Crc5EpcC1g2 = Crc(u5, .{
48pub const Crc5EpcC1g2 = Generic(u5, .{
6149 .polynomial = 0x09,
6250 .initial = 0x09,
6351 .reflect_input = false,
......@@ -65,7 +53,7 @@ pub const Crc5EpcC1g2 = Crc(u5, .{
6553 .xor_output = 0x00,
6654});
6755
68pub const Crc5G704 = Crc(u5, .{
56pub const Crc5G704 = Generic(u5, .{
6957 .polynomial = 0x15,
7058 .initial = 0x00,
7159 .reflect_input = true,
......@@ -73,7 +61,7 @@ pub const Crc5G704 = Crc(u5, .{
7361 .xor_output = 0x00,
7462});
7563
76pub const Crc5Usb = Crc(u5, .{
64pub const Crc5Usb = Generic(u5, .{
7765 .polynomial = 0x05,
7866 .initial = 0x1f,
7967 .reflect_input = true,
......@@ -81,7 +69,7 @@ pub const Crc5Usb = Crc(u5, .{
8169 .xor_output = 0x1f,
8270});
8371
84pub const Crc6Cdma2000A = Crc(u6, .{
72pub const Crc6Cdma2000A = Generic(u6, .{
8573 .polynomial = 0x27,
8674 .initial = 0x3f,
8775 .reflect_input = false,
......@@ -89,7 +77,7 @@ pub const Crc6Cdma2000A = Crc(u6, .{
8977 .xor_output = 0x00,
9078});
9179
92pub const Crc6Cdma2000B = Crc(u6, .{
80pub const Crc6Cdma2000B = Generic(u6, .{
9381 .polynomial = 0x07,
9482 .initial = 0x3f,
9583 .reflect_input = false,
......@@ -97,7 +85,7 @@ pub const Crc6Cdma2000B = Crc(u6, .{
9785 .xor_output = 0x00,
9886});
9987
100pub const Crc6Darc = Crc(u6, .{
88pub const Crc6Darc = Generic(u6, .{
10189 .polynomial = 0x19,
10290 .initial = 0x00,
10391 .reflect_input = true,
......@@ -105,7 +93,7 @@ pub const Crc6Darc = Crc(u6, .{
10593 .xor_output = 0x00,
10694});
10795
108pub const Crc6G704 = Crc(u6, .{
96pub const Crc6G704 = Generic(u6, .{
10997 .polynomial = 0x03,
11098 .initial = 0x00,
11199 .reflect_input = true,
......@@ -113,7 +101,7 @@ pub const Crc6G704 = Crc(u6, .{
113101 .xor_output = 0x00,
114102});
115103
116pub const Crc6Gsm = Crc(u6, .{
104pub const Crc6Gsm = Generic(u6, .{
117105 .polynomial = 0x2f,
118106 .initial = 0x00,
119107 .reflect_input = false,
......@@ -121,7 +109,7 @@ pub const Crc6Gsm = Crc(u6, .{
121109 .xor_output = 0x3f,
122110});
123111
124pub const Crc7Mmc = Crc(u7, .{
112pub const Crc7Mmc = Generic(u7, .{
125113 .polynomial = 0x09,
126114 .initial = 0x00,
127115 .reflect_input = false,
......@@ -129,7 +117,7 @@ pub const Crc7Mmc = Crc(u7, .{
129117 .xor_output = 0x00,
130118});
131119
132pub const Crc7Rohc = Crc(u7, .{
120pub const Crc7Rohc = Generic(u7, .{
133121 .polynomial = 0x4f,
134122 .initial = 0x7f,
135123 .reflect_input = true,
......@@ -137,7 +125,7 @@ pub const Crc7Rohc = Crc(u7, .{
137125 .xor_output = 0x00,
138126});
139127
140pub const Crc7Umts = Crc(u7, .{
128pub const Crc7Umts = Generic(u7, .{
141129 .polynomial = 0x45,
142130 .initial = 0x00,
143131 .reflect_input = false,
......@@ -145,7 +133,7 @@ pub const Crc7Umts = Crc(u7, .{
145133 .xor_output = 0x00,
146134});
147135
148pub const Crc8Autosar = Crc(u8, .{
136pub const Crc8Autosar = Generic(u8, .{
149137 .polynomial = 0x2f,
150138 .initial = 0xff,
151139 .reflect_input = false,
......@@ -153,7 +141,7 @@ pub const Crc8Autosar = Crc(u8, .{
153141 .xor_output = 0xff,
154142});
155143
156pub const Crc8Bluetooth = Crc(u8, .{
144pub const Crc8Bluetooth = Generic(u8, .{
157145 .polynomial = 0xa7,
158146 .initial = 0x00,
159147 .reflect_input = true,
......@@ -161,7 +149,7 @@ pub const Crc8Bluetooth = Crc(u8, .{
161149 .xor_output = 0x00,
162150});
163151
164pub const Crc8Cdma2000 = Crc(u8, .{
152pub const Crc8Cdma2000 = Generic(u8, .{
165153 .polynomial = 0x9b,
166154 .initial = 0xff,
167155 .reflect_input = false,
......@@ -169,7 +157,7 @@ pub const Crc8Cdma2000 = Crc(u8, .{
169157 .xor_output = 0x00,
170158});
171159
172pub const Crc8Darc = Crc(u8, .{
160pub const Crc8Darc = Generic(u8, .{
173161 .polynomial = 0x39,
174162 .initial = 0x00,
175163 .reflect_input = true,
......@@ -177,7 +165,7 @@ pub const Crc8Darc = Crc(u8, .{
177165 .xor_output = 0x00,
178166});
179167
180pub const Crc8DvbS2 = Crc(u8, .{
168pub const Crc8DvbS2 = Generic(u8, .{
181169 .polynomial = 0xd5,
182170 .initial = 0x00,
183171 .reflect_input = false,
......@@ -185,7 +173,7 @@ pub const Crc8DvbS2 = Crc(u8, .{
185173 .xor_output = 0x00,
186174});
187175
188pub const Crc8GsmA = Crc(u8, .{
176pub const Crc8GsmA = Generic(u8, .{
189177 .polynomial = 0x1d,
190178 .initial = 0x00,
191179 .reflect_input = false,
......@@ -193,7 +181,7 @@ pub const Crc8GsmA = Crc(u8, .{
193181 .xor_output = 0x00,
194182});
195183
196pub const Crc8GsmB = Crc(u8, .{
184pub const Crc8GsmB = Generic(u8, .{
197185 .polynomial = 0x49,
198186 .initial = 0x00,
199187 .reflect_input = false,
......@@ -201,7 +189,7 @@ pub const Crc8GsmB = Crc(u8, .{
201189 .xor_output = 0xff,
202190});
203191
204pub const Crc8Hitag = Crc(u8, .{
192pub const Crc8Hitag = Generic(u8, .{
205193 .polynomial = 0x1d,
206194 .initial = 0xff,
207195 .reflect_input = false,
......@@ -209,7 +197,7 @@ pub const Crc8Hitag = Crc(u8, .{
209197 .xor_output = 0x00,
210198});
211199
212pub const Crc8I4321 = Crc(u8, .{
200pub const Crc8I4321 = Generic(u8, .{
213201 .polynomial = 0x07,
214202 .initial = 0x00,
215203 .reflect_input = false,
......@@ -217,7 +205,7 @@ pub const Crc8I4321 = Crc(u8, .{
217205 .xor_output = 0x55,
218206});
219207
220pub const Crc8ICode = Crc(u8, .{
208pub const Crc8ICode = Generic(u8, .{
221209 .polynomial = 0x1d,
222210 .initial = 0xfd,
223211 .reflect_input = false,
......@@ -225,7 +213,7 @@ pub const Crc8ICode = Crc(u8, .{
225213 .xor_output = 0x00,
226214});
227215
228pub const Crc8Lte = Crc(u8, .{
216pub const Crc8Lte = Generic(u8, .{
229217 .polynomial = 0x9b,
230218 .initial = 0x00,
231219 .reflect_input = false,
......@@ -233,7 +221,7 @@ pub const Crc8Lte = Crc(u8, .{
233221 .xor_output = 0x00,
234222});
235223
236pub const Crc8MaximDow = Crc(u8, .{
224pub const Crc8MaximDow = Generic(u8, .{
237225 .polynomial = 0x31,
238226 .initial = 0x00,
239227 .reflect_input = true,
......@@ -241,7 +229,7 @@ pub const Crc8MaximDow = Crc(u8, .{
241229 .xor_output = 0x00,
242230});
243231
244pub const Crc8MifareMad = Crc(u8, .{
232pub const Crc8MifareMad = Generic(u8, .{
245233 .polynomial = 0x1d,
246234 .initial = 0xc7,
247235 .reflect_input = false,
......@@ -249,7 +237,7 @@ pub const Crc8MifareMad = Crc(u8, .{
249237 .xor_output = 0x00,
250238});
251239
252pub const Crc8Nrsc5 = Crc(u8, .{
240pub const Crc8Nrsc5 = Generic(u8, .{
253241 .polynomial = 0x31,
254242 .initial = 0xff,
255243 .reflect_input = false,
......@@ -257,7 +245,7 @@ pub const Crc8Nrsc5 = Crc(u8, .{
257245 .xor_output = 0x00,
258246});
259247
260pub const Crc8Opensafety = Crc(u8, .{
248pub const Crc8Opensafety = Generic(u8, .{
261249 .polynomial = 0x2f,
262250 .initial = 0x00,
263251 .reflect_input = false,
......@@ -265,7 +253,7 @@ pub const Crc8Opensafety = Crc(u8, .{
265253 .xor_output = 0x00,
266254});
267255
268pub const Crc8Rohc = Crc(u8, .{
256pub const Crc8Rohc = Generic(u8, .{
269257 .polynomial = 0x07,
270258 .initial = 0xff,
271259 .reflect_input = true,
......@@ -273,7 +261,7 @@ pub const Crc8Rohc = Crc(u8, .{
273261 .xor_output = 0x00,
274262});
275263
276pub const Crc8SaeJ1850 = Crc(u8, .{
264pub const Crc8SaeJ1850 = Generic(u8, .{
277265 .polynomial = 0x1d,
278266 .initial = 0xff,
279267 .reflect_input = false,
......@@ -281,7 +269,7 @@ pub const Crc8SaeJ1850 = Crc(u8, .{
281269 .xor_output = 0xff,
282270});
283271
284pub const Crc8Smbus = Crc(u8, .{
272pub const Crc8Smbus = Generic(u8, .{
285273 .polynomial = 0x07,
286274 .initial = 0x00,
287275 .reflect_input = false,
......@@ -289,7 +277,7 @@ pub const Crc8Smbus = Crc(u8, .{
289277 .xor_output = 0x00,
290278});
291279
292pub const Crc8Tech3250 = Crc(u8, .{
280pub const Crc8Tech3250 = Generic(u8, .{
293281 .polynomial = 0x1d,
294282 .initial = 0xff,
295283 .reflect_input = true,
......@@ -297,7 +285,7 @@ pub const Crc8Tech3250 = Crc(u8, .{
297285 .xor_output = 0x00,
298286});
299287
300pub const Crc8Wcdma = Crc(u8, .{
288pub const Crc8Wcdma = Generic(u8, .{
301289 .polynomial = 0x9b,
302290 .initial = 0x00,
303291 .reflect_input = true,
......@@ -305,7 +293,7 @@ pub const Crc8Wcdma = Crc(u8, .{
305293 .xor_output = 0x00,
306294});
307295
308pub const Crc10Atm = Crc(u10, .{
296pub const Crc10Atm = Generic(u10, .{
309297 .polynomial = 0x233,
310298 .initial = 0x000,
311299 .reflect_input = false,
......@@ -313,7 +301,7 @@ pub const Crc10Atm = Crc(u10, .{
313301 .xor_output = 0x000,
314302});
315303
316pub const Crc10Cdma2000 = Crc(u10, .{
304pub const Crc10Cdma2000 = Generic(u10, .{
317305 .polynomial = 0x3d9,
318306 .initial = 0x3ff,
319307 .reflect_input = false,
......@@ -321,7 +309,7 @@ pub const Crc10Cdma2000 = Crc(u10, .{
321309 .xor_output = 0x000,
322310});
323311
324pub const Crc10Gsm = Crc(u10, .{
312pub const Crc10Gsm = Generic(u10, .{
325313 .polynomial = 0x175,
326314 .initial = 0x000,
327315 .reflect_input = false,
......@@ -329,7 +317,7 @@ pub const Crc10Gsm = Crc(u10, .{
329317 .xor_output = 0x3ff,
330318});
331319
332pub const Crc11Flexray = Crc(u11, .{
320pub const Crc11Flexray = Generic(u11, .{
333321 .polynomial = 0x385,
334322 .initial = 0x01a,
335323 .reflect_input = false,
......@@ -337,7 +325,7 @@ pub const Crc11Flexray = Crc(u11, .{
337325 .xor_output = 0x000,
338326});
339327
340pub const Crc11Umts = Crc(u11, .{
328pub const Crc11Umts = Generic(u11, .{
341329 .polynomial = 0x307,
342330 .initial = 0x000,
343331 .reflect_input = false,
......@@ -345,7 +333,7 @@ pub const Crc11Umts = Crc(u11, .{
345333 .xor_output = 0x000,
346334});
347335
348pub const Crc12Cdma2000 = Crc(u12, .{
336pub const Crc12Cdma2000 = Generic(u12, .{
349337 .polynomial = 0xf13,
350338 .initial = 0xfff,
351339 .reflect_input = false,
......@@ -353,7 +341,7 @@ pub const Crc12Cdma2000 = Crc(u12, .{
353341 .xor_output = 0x000,
354342});
355343
356pub const Crc12Dect = Crc(u12, .{
344pub const Crc12Dect = Generic(u12, .{
357345 .polynomial = 0x80f,
358346 .initial = 0x000,
359347 .reflect_input = false,
......@@ -361,7 +349,7 @@ pub const Crc12Dect = Crc(u12, .{
361349 .xor_output = 0x000,
362350});
363351
364pub const Crc12Gsm = Crc(u12, .{
352pub const Crc12Gsm = Generic(u12, .{
365353 .polynomial = 0xd31,
366354 .initial = 0x000,
367355 .reflect_input = false,
......@@ -369,7 +357,7 @@ pub const Crc12Gsm = Crc(u12, .{
369357 .xor_output = 0xfff,
370358});
371359
372pub const Crc12Umts = Crc(u12, .{
360pub const Crc12Umts = Generic(u12, .{
373361 .polynomial = 0x80f,
374362 .initial = 0x000,
375363 .reflect_input = false,
......@@ -377,7 +365,7 @@ pub const Crc12Umts = Crc(u12, .{
377365 .xor_output = 0x000,
378366});
379367
380pub const Crc13Bbc = Crc(u13, .{
368pub const Crc13Bbc = Generic(u13, .{
381369 .polynomial = 0x1cf5,
382370 .initial = 0x0000,
383371 .reflect_input = false,
......@@ -385,7 +373,7 @@ pub const Crc13Bbc = Crc(u13, .{
385373 .xor_output = 0x0000,
386374});
387375
388pub const Crc14Darc = Crc(u14, .{
376pub const Crc14Darc = Generic(u14, .{
389377 .polynomial = 0x0805,
390378 .initial = 0x0000,
391379 .reflect_input = true,
......@@ -393,7 +381,7 @@ pub const Crc14Darc = Crc(u14, .{
393381 .xor_output = 0x0000,
394382});
395383
396pub const Crc14Gsm = Crc(u14, .{
384pub const Crc14Gsm = Generic(u14, .{
397385 .polynomial = 0x202d,
398386 .initial = 0x0000,
399387 .reflect_input = false,
......@@ -401,7 +389,7 @@ pub const Crc14Gsm = Crc(u14, .{
401389 .xor_output = 0x3fff,
402390});
403391
404pub const Crc15Can = Crc(u15, .{
392pub const Crc15Can = Generic(u15, .{
405393 .polynomial = 0x4599,
406394 .initial = 0x0000,
407395 .reflect_input = false,
......@@ -409,7 +397,7 @@ pub const Crc15Can = Crc(u15, .{
409397 .xor_output = 0x0000,
410398});
411399
412pub const Crc15Mpt1327 = Crc(u15, .{
400pub const Crc15Mpt1327 = Generic(u15, .{
413401 .polynomial = 0x6815,
414402 .initial = 0x0000,
415403 .reflect_input = false,
......@@ -417,7 +405,7 @@ pub const Crc15Mpt1327 = Crc(u15, .{
417405 .xor_output = 0x0001,
418406});
419407
420pub const Crc16Arc = Crc(u16, .{
408pub const Crc16Arc = Generic(u16, .{
421409 .polynomial = 0x8005,
422410 .initial = 0x0000,
423411 .reflect_input = true,
......@@ -425,7 +413,7 @@ pub const Crc16Arc = Crc(u16, .{
425413 .xor_output = 0x0000,
426414});
427415
428pub const Crc16Cdma2000 = Crc(u16, .{
416pub const Crc16Cdma2000 = Generic(u16, .{
429417 .polynomial = 0xc867,
430418 .initial = 0xffff,
431419 .reflect_input = false,
......@@ -433,7 +421,7 @@ pub const Crc16Cdma2000 = Crc(u16, .{
433421 .xor_output = 0x0000,
434422});
435423
436pub const Crc16Cms = Crc(u16, .{
424pub const Crc16Cms = Generic(u16, .{
437425 .polynomial = 0x8005,
438426 .initial = 0xffff,
439427 .reflect_input = false,
......@@ -441,7 +429,7 @@ pub const Crc16Cms = Crc(u16, .{
441429 .xor_output = 0x0000,
442430});
443431
444pub const Crc16Dds110 = Crc(u16, .{
432pub const Crc16Dds110 = Generic(u16, .{
445433 .polynomial = 0x8005,
446434 .initial = 0x800d,
447435 .reflect_input = false,
......@@ -449,7 +437,7 @@ pub const Crc16Dds110 = Crc(u16, .{
449437 .xor_output = 0x0000,
450438});
451439
452pub const Crc16DectR = Crc(u16, .{
440pub const Crc16DectR = Generic(u16, .{
453441 .polynomial = 0x0589,
454442 .initial = 0x0000,
455443 .reflect_input = false,
......@@ -457,7 +445,7 @@ pub const Crc16DectR = Crc(u16, .{
457445 .xor_output = 0x0001,
458446});
459447
460pub const Crc16DectX = Crc(u16, .{
448pub const Crc16DectX = Generic(u16, .{
461449 .polynomial = 0x0589,
462450 .initial = 0x0000,
463451 .reflect_input = false,
......@@ -465,7 +453,7 @@ pub const Crc16DectX = Crc(u16, .{
465453 .xor_output = 0x0000,
466454});
467455
468pub const Crc16Dnp = Crc(u16, .{
456pub const Crc16Dnp = Generic(u16, .{
469457 .polynomial = 0x3d65,
470458 .initial = 0x0000,
471459 .reflect_input = true,
......@@ -473,7 +461,7 @@ pub const Crc16Dnp = Crc(u16, .{
473461 .xor_output = 0xffff,
474462});
475463
476pub const Crc16En13757 = Crc(u16, .{
464pub const Crc16En13757 = Generic(u16, .{
477465 .polynomial = 0x3d65,
478466 .initial = 0x0000,
479467 .reflect_input = false,
......@@ -481,7 +469,7 @@ pub const Crc16En13757 = Crc(u16, .{
481469 .xor_output = 0xffff,
482470});
483471
484pub const Crc16Genibus = Crc(u16, .{
472pub const Crc16Genibus = Generic(u16, .{
485473 .polynomial = 0x1021,
486474 .initial = 0xffff,
487475 .reflect_input = false,
......@@ -489,7 +477,7 @@ pub const Crc16Genibus = Crc(u16, .{
489477 .xor_output = 0xffff,
490478});
491479
492pub const Crc16Gsm = Crc(u16, .{
480pub const Crc16Gsm = Generic(u16, .{
493481 .polynomial = 0x1021,
494482 .initial = 0x0000,
495483 .reflect_input = false,
......@@ -497,7 +485,7 @@ pub const Crc16Gsm = Crc(u16, .{
497485 .xor_output = 0xffff,
498486});
499487
500pub const Crc16Ibm3740 = Crc(u16, .{
488pub const Crc16Ibm3740 = Generic(u16, .{
501489 .polynomial = 0x1021,
502490 .initial = 0xffff,
503491 .reflect_input = false,
......@@ -505,7 +493,7 @@ pub const Crc16Ibm3740 = Crc(u16, .{
505493 .xor_output = 0x0000,
506494});
507495
508pub const Crc16IbmSdlc = Crc(u16, .{
496pub const Crc16IbmSdlc = Generic(u16, .{
509497 .polynomial = 0x1021,
510498 .initial = 0xffff,
511499 .reflect_input = true,
......@@ -513,7 +501,7 @@ pub const Crc16IbmSdlc = Crc(u16, .{
513501 .xor_output = 0xffff,
514502});
515503
516pub const Crc16IsoIec144433A = Crc(u16, .{
504pub const Crc16IsoIec144433A = Generic(u16, .{
517505 .polynomial = 0x1021,
518506 .initial = 0xc6c6,
519507 .reflect_input = true,
......@@ -521,7 +509,7 @@ pub const Crc16IsoIec144433A = Crc(u16, .{
521509 .xor_output = 0x0000,
522510});
523511
524pub const Crc16Kermit = Crc(u16, .{
512pub const Crc16Kermit = Generic(u16, .{
525513 .polynomial = 0x1021,
526514 .initial = 0x0000,
527515 .reflect_input = true,
......@@ -529,7 +517,7 @@ pub const Crc16Kermit = Crc(u16, .{
529517 .xor_output = 0x0000,
530518});
531519
532pub const Crc16Lj1200 = Crc(u16, .{
520pub const Crc16Lj1200 = Generic(u16, .{
533521 .polynomial = 0x6f63,
534522 .initial = 0x0000,
535523 .reflect_input = false,
......@@ -537,7 +525,7 @@ pub const Crc16Lj1200 = Crc(u16, .{
537525 .xor_output = 0x0000,
538526});
539527
540pub const Crc16M17 = Crc(u16, .{
528pub const Crc16M17 = Generic(u16, .{
541529 .polynomial = 0x5935,
542530 .initial = 0xffff,
543531 .reflect_input = false,
......@@ -545,7 +533,7 @@ pub const Crc16M17 = Crc(u16, .{
545533 .xor_output = 0x0000,
546534});
547535
548pub const Crc16MaximDow = Crc(u16, .{
536pub const Crc16MaximDow = Generic(u16, .{
549537 .polynomial = 0x8005,
550538 .initial = 0x0000,
551539 .reflect_input = true,
......@@ -553,7 +541,7 @@ pub const Crc16MaximDow = Crc(u16, .{
553541 .xor_output = 0xffff,
554542});
555543
556pub const Crc16Mcrf4xx = Crc(u16, .{
544pub const Crc16Mcrf4xx = Generic(u16, .{
557545 .polynomial = 0x1021,
558546 .initial = 0xffff,
559547 .reflect_input = true,
......@@ -561,7 +549,7 @@ pub const Crc16Mcrf4xx = Crc(u16, .{
561549 .xor_output = 0x0000,
562550});
563551
564pub const Crc16Modbus = Crc(u16, .{
552pub const Crc16Modbus = Generic(u16, .{
565553 .polynomial = 0x8005,
566554 .initial = 0xffff,
567555 .reflect_input = true,
......@@ -569,7 +557,7 @@ pub const Crc16Modbus = Crc(u16, .{
569557 .xor_output = 0x0000,
570558});
571559
572pub const Crc16Nrsc5 = Crc(u16, .{
560pub const Crc16Nrsc5 = Generic(u16, .{
573561 .polynomial = 0x080b,
574562 .initial = 0xffff,
575563 .reflect_input = true,
......@@ -577,7 +565,7 @@ pub const Crc16Nrsc5 = Crc(u16, .{
577565 .xor_output = 0x0000,
578566});
579567
580pub const Crc16OpensafetyA = Crc(u16, .{
568pub const Crc16OpensafetyA = Generic(u16, .{
581569 .polynomial = 0x5935,
582570 .initial = 0x0000,
583571 .reflect_input = false,
......@@ -585,7 +573,7 @@ pub const Crc16OpensafetyA = Crc(u16, .{
585573 .xor_output = 0x0000,
586574});
587575
588pub const Crc16OpensafetyB = Crc(u16, .{
576pub const Crc16OpensafetyB = Generic(u16, .{
589577 .polynomial = 0x755b,
590578 .initial = 0x0000,
591579 .reflect_input = false,
......@@ -593,7 +581,7 @@ pub const Crc16OpensafetyB = Crc(u16, .{
593581 .xor_output = 0x0000,
594582});
595583
596pub const Crc16Profibus = Crc(u16, .{
584pub const Crc16Profibus = Generic(u16, .{
597585 .polynomial = 0x1dcf,
598586 .initial = 0xffff,
599587 .reflect_input = false,
......@@ -601,7 +589,7 @@ pub const Crc16Profibus = Crc(u16, .{
601589 .xor_output = 0xffff,
602590});
603591
604pub const Crc16Riello = Crc(u16, .{
592pub const Crc16Riello = Generic(u16, .{
605593 .polynomial = 0x1021,
606594 .initial = 0xb2aa,
607595 .reflect_input = true,
......@@ -609,7 +597,7 @@ pub const Crc16Riello = Crc(u16, .{
609597 .xor_output = 0x0000,
610598});
611599
612pub const Crc16SpiFujitsu = Crc(u16, .{
600pub const Crc16SpiFujitsu = Generic(u16, .{
613601 .polynomial = 0x1021,
614602 .initial = 0x1d0f,
615603 .reflect_input = false,
......@@ -617,7 +605,7 @@ pub const Crc16SpiFujitsu = Crc(u16, .{
617605 .xor_output = 0x0000,
618606});
619607
620pub const Crc16T10Dif = Crc(u16, .{
608pub const Crc16T10Dif = Generic(u16, .{
621609 .polynomial = 0x8bb7,
622610 .initial = 0x0000,
623611 .reflect_input = false,
......@@ -625,7 +613,7 @@ pub const Crc16T10Dif = Crc(u16, .{
625613 .xor_output = 0x0000,
626614});
627615
628pub const Crc16Teledisk = Crc(u16, .{
616pub const Crc16Teledisk = Generic(u16, .{
629617 .polynomial = 0xa097,
630618 .initial = 0x0000,
631619 .reflect_input = false,
......@@ -633,7 +621,7 @@ pub const Crc16Teledisk = Crc(u16, .{
633621 .xor_output = 0x0000,
634622});
635623
636pub const Crc16Tms37157 = Crc(u16, .{
624pub const Crc16Tms37157 = Generic(u16, .{
637625 .polynomial = 0x1021,
638626 .initial = 0x89ec,
639627 .reflect_input = true,
......@@ -641,7 +629,7 @@ pub const Crc16Tms37157 = Crc(u16, .{
641629 .xor_output = 0x0000,
642630});
643631
644pub const Crc16Umts = Crc(u16, .{
632pub const Crc16Umts = Generic(u16, .{
645633 .polynomial = 0x8005,
646634 .initial = 0x0000,
647635 .reflect_input = false,
......@@ -649,7 +637,7 @@ pub const Crc16Umts = Crc(u16, .{
649637 .xor_output = 0x0000,
650638});
651639
652pub const Crc16Usb = Crc(u16, .{
640pub const Crc16Usb = Generic(u16, .{
653641 .polynomial = 0x8005,
654642 .initial = 0xffff,
655643 .reflect_input = true,
......@@ -657,7 +645,7 @@ pub const Crc16Usb = Crc(u16, .{
657645 .xor_output = 0xffff,
658646});
659647
660pub const Crc16Xmodem = Crc(u16, .{
648pub const Crc16Xmodem = Generic(u16, .{
661649 .polynomial = 0x1021,
662650 .initial = 0x0000,
663651 .reflect_input = false,
......@@ -665,7 +653,7 @@ pub const Crc16Xmodem = Crc(u16, .{
665653 .xor_output = 0x0000,
666654});
667655
668pub const Crc17CanFd = Crc(u17, .{
656pub const Crc17CanFd = Generic(u17, .{
669657 .polynomial = 0x1685b,
670658 .initial = 0x00000,
671659 .reflect_input = false,
......@@ -673,7 +661,7 @@ pub const Crc17CanFd = Crc(u17, .{
673661 .xor_output = 0x00000,
674662});
675663
676pub const Crc21CanFd = Crc(u21, .{
664pub const Crc21CanFd = Generic(u21, .{
677665 .polynomial = 0x102899,
678666 .initial = 0x000000,
679667 .reflect_input = false,
......@@ -681,7 +669,7 @@ pub const Crc21CanFd = Crc(u21, .{
681669 .xor_output = 0x000000,
682670});
683671
684pub const Crc24Ble = Crc(u24, .{
672pub const Crc24Ble = Generic(u24, .{
685673 .polynomial = 0x00065b,
686674 .initial = 0x555555,
687675 .reflect_input = true,
......@@ -689,7 +677,7 @@ pub const Crc24Ble = Crc(u24, .{
689677 .xor_output = 0x000000,
690678});
691679
692pub const Crc24FlexrayA = Crc(u24, .{
680pub const Crc24FlexrayA = Generic(u24, .{
693681 .polynomial = 0x5d6dcb,
694682 .initial = 0xfedcba,
695683 .reflect_input = false,
......@@ -697,7 +685,7 @@ pub const Crc24FlexrayA = Crc(u24, .{
697685 .xor_output = 0x000000,
698686});
699687
700pub const Crc24FlexrayB = Crc(u24, .{
688pub const Crc24FlexrayB = Generic(u24, .{
701689 .polynomial = 0x5d6dcb,
702690 .initial = 0xabcdef,
703691 .reflect_input = false,
......@@ -705,7 +693,7 @@ pub const Crc24FlexrayB = Crc(u24, .{
705693 .xor_output = 0x000000,
706694});
707695
708pub const Crc24Interlaken = Crc(u24, .{
696pub const Crc24Interlaken = Generic(u24, .{
709697 .polynomial = 0x328b63,
710698 .initial = 0xffffff,
711699 .reflect_input = false,
......@@ -713,7 +701,7 @@ pub const Crc24Interlaken = Crc(u24, .{
713701 .xor_output = 0xffffff,
714702});
715703
716pub const Crc24LteA = Crc(u24, .{
704pub const Crc24LteA = Generic(u24, .{
717705 .polynomial = 0x864cfb,
718706 .initial = 0x000000,
719707 .reflect_input = false,
......@@ -721,7 +709,7 @@ pub const Crc24LteA = Crc(u24, .{
721709 .xor_output = 0x000000,
722710});
723711
724pub const Crc24LteB = Crc(u24, .{
712pub const Crc24LteB = Generic(u24, .{
725713 .polynomial = 0x800063,
726714 .initial = 0x000000,
727715 .reflect_input = false,
......@@ -729,7 +717,7 @@ pub const Crc24LteB = Crc(u24, .{
729717 .xor_output = 0x000000,
730718});
731719
732pub const Crc24Openpgp = Crc(u24, .{
720pub const Crc24Openpgp = Generic(u24, .{
733721 .polynomial = 0x864cfb,
734722 .initial = 0xb704ce,
735723 .reflect_input = false,
......@@ -737,7 +725,7 @@ pub const Crc24Openpgp = Crc(u24, .{
737725 .xor_output = 0x000000,
738726});
739727
740pub const Crc24Os9 = Crc(u24, .{
728pub const Crc24Os9 = Generic(u24, .{
741729 .polynomial = 0x800063,
742730 .initial = 0xffffff,
743731 .reflect_input = false,
......@@ -745,7 +733,7 @@ pub const Crc24Os9 = Crc(u24, .{
745733 .xor_output = 0xffffff,
746734});
747735
748pub const Crc30Cdma = Crc(u30, .{
736pub const Crc30Cdma = Generic(u30, .{
749737 .polynomial = 0x2030b9c7,
750738 .initial = 0x3fffffff,
751739 .reflect_input = false,
......@@ -753,7 +741,7 @@ pub const Crc30Cdma = Crc(u30, .{
753741 .xor_output = 0x3fffffff,
754742});
755743
756pub const Crc31Philips = Crc(u31, .{
744pub const Crc31Philips = Generic(u31, .{
757745 .polynomial = 0x04c11db7,
758746 .initial = 0x7fffffff,
759747 .reflect_input = false,
......@@ -761,7 +749,7 @@ pub const Crc31Philips = Crc(u31, .{
761749 .xor_output = 0x7fffffff,
762750});
763751
764pub const Crc32Aixm = Crc(u32, .{
752pub const Crc32Aixm = Generic(u32, .{
765753 .polynomial = 0x814141ab,
766754 .initial = 0x00000000,
767755 .reflect_input = false,
......@@ -769,7 +757,7 @@ pub const Crc32Aixm = Crc(u32, .{
769757 .xor_output = 0x00000000,
770758});
771759
772pub const Crc32Autosar = Crc(u32, .{
760pub const Crc32Autosar = Generic(u32, .{
773761 .polynomial = 0xf4acfb13,
774762 .initial = 0xffffffff,
775763 .reflect_input = true,
......@@ -777,7 +765,7 @@ pub const Crc32Autosar = Crc(u32, .{
777765 .xor_output = 0xffffffff,
778766});
779767
780pub const Crc32Base91D = Crc(u32, .{
768pub const Crc32Base91D = Generic(u32, .{
781769 .polynomial = 0xa833982b,
782770 .initial = 0xffffffff,
783771 .reflect_input = true,
......@@ -785,7 +773,7 @@ pub const Crc32Base91D = Crc(u32, .{
785773 .xor_output = 0xffffffff,
786774});
787775
788pub const Crc32Bzip2 = Crc(u32, .{
776pub const Crc32Bzip2 = Generic(u32, .{
789777 .polynomial = 0x04c11db7,
790778 .initial = 0xffffffff,
791779 .reflect_input = false,
......@@ -793,7 +781,7 @@ pub const Crc32Bzip2 = Crc(u32, .{
793781 .xor_output = 0xffffffff,
794782});
795783
796pub const Crc32CdRomEdc = Crc(u32, .{
784pub const Crc32CdRomEdc = Generic(u32, .{
797785 .polynomial = 0x8001801b,
798786 .initial = 0x00000000,
799787 .reflect_input = true,
......@@ -801,7 +789,7 @@ pub const Crc32CdRomEdc = Crc(u32, .{
801789 .xor_output = 0x00000000,
802790});
803791
804pub const Crc32Cksum = Crc(u32, .{
792pub const Crc32Cksum = Generic(u32, .{
805793 .polynomial = 0x04c11db7,
806794 .initial = 0x00000000,
807795 .reflect_input = false,
......@@ -809,7 +797,7 @@ pub const Crc32Cksum = Crc(u32, .{
809797 .xor_output = 0xffffffff,
810798});
811799
812pub const Crc32IsoHdlc = Crc(u32, .{
800pub const Crc32IsoHdlc = Generic(u32, .{
813801 .polynomial = 0x04c11db7,
814802 .initial = 0xffffffff,
815803 .reflect_input = true,
......@@ -817,7 +805,7 @@ pub const Crc32IsoHdlc = Crc(u32, .{
817805 .xor_output = 0xffffffff,
818806});
819807
820pub const Crc32Jamcrc = Crc(u32, .{
808pub const Crc32Jamcrc = Generic(u32, .{
821809 .polynomial = 0x04c11db7,
822810 .initial = 0xffffffff,
823811 .reflect_input = true,
......@@ -825,7 +813,7 @@ pub const Crc32Jamcrc = Crc(u32, .{
825813 .xor_output = 0x00000000,
826814});
827815
828pub const Crc32Koopman = Crc(u32, .{
816pub const Crc32Koopman = Generic(u32, .{
829817 .polynomial = 0x741b8cd7,
830818 .initial = 0xffffffff,
831819 .reflect_input = true,
......@@ -833,7 +821,7 @@ pub const Crc32Koopman = Crc(u32, .{
833821 .xor_output = 0xffffffff,
834822});
835823
836pub const Crc32Mef = Crc(u32, .{
824pub const Crc32Mef = Generic(u32, .{
837825 .polynomial = 0x741b8cd7,
838826 .initial = 0xffffffff,
839827 .reflect_input = true,
......@@ -841,7 +829,7 @@ pub const Crc32Mef = Crc(u32, .{
841829 .xor_output = 0x00000000,
842830});
843831
844pub const Crc32Mpeg2 = Crc(u32, .{
832pub const Crc32Mpeg2 = Generic(u32, .{
845833 .polynomial = 0x04c11db7,
846834 .initial = 0xffffffff,
847835 .reflect_input = false,
......@@ -849,7 +837,7 @@ pub const Crc32Mpeg2 = Crc(u32, .{
849837 .xor_output = 0x00000000,
850838});
851839
852pub const Crc32Xfer = Crc(u32, .{
840pub const Crc32Xfer = Generic(u32, .{
853841 .polynomial = 0x000000af,
854842 .initial = 0x00000000,
855843 .reflect_input = false,
......@@ -857,7 +845,7 @@ pub const Crc32Xfer = Crc(u32, .{
857845 .xor_output = 0x00000000,
858846});
859847
860pub const Crc40Gsm = Crc(u40, .{
848pub const Crc40Gsm = Generic(u40, .{
861849 .polynomial = 0x0004820009,
862850 .initial = 0x0000000000,
863851 .reflect_input = false,
......@@ -865,7 +853,7 @@ pub const Crc40Gsm = Crc(u40, .{
865853 .xor_output = 0xffffffffff,
866854});
867855
868pub const Crc64Ecma182 = Crc(u64, .{
856pub const Crc64Ecma182 = Generic(u64, .{
869857 .polynomial = 0x42f0e1eba9ea3693,
870858 .initial = 0x0000000000000000,
871859 .reflect_input = false,
......@@ -873,7 +861,7 @@ pub const Crc64Ecma182 = Crc(u64, .{
873861 .xor_output = 0x0000000000000000,
874862});
875863
876pub const Crc64GoIso = Crc(u64, .{
864pub const Crc64GoIso = Generic(u64, .{
877865 .polynomial = 0x000000000000001b,
878866 .initial = 0xffffffffffffffff,
879867 .reflect_input = true,
......@@ -881,7 +869,7 @@ pub const Crc64GoIso = Crc(u64, .{
881869 .xor_output = 0xffffffffffffffff,
882870});
883871
884pub const Crc64Ms = Crc(u64, .{
872pub const Crc64Ms = Generic(u64, .{
885873 .polynomial = 0x259c84cba6426349,
886874 .initial = 0xffffffffffffffff,
887875 .reflect_input = true,
......@@ -889,7 +877,7 @@ pub const Crc64Ms = Crc(u64, .{
889877 .xor_output = 0x0000000000000000,
890878});
891879
892pub const Crc64Redis = Crc(u64, .{
880pub const Crc64Redis = Generic(u64, .{
893881 .polynomial = 0xad93d23594c935a9,
894882 .initial = 0x0000000000000000,
895883 .reflect_input = true,
......@@ -897,7 +885,7 @@ pub const Crc64Redis = Crc(u64, .{
897885 .xor_output = 0x0000000000000000,
898886});
899887
900pub const Crc64We = Crc(u64, .{
888pub const Crc64We = Generic(u64, .{
901889 .polynomial = 0x42f0e1eba9ea3693,
902890 .initial = 0xffffffffffffffff,
903891 .reflect_input = false,
......@@ -905,7 +893,7 @@ pub const Crc64We = Crc(u64, .{
905893 .xor_output = 0xffffffffffffffff,
906894});
907895
908pub const Crc64Xz = Crc(u64, .{
896pub const Crc64Xz = Generic(u64, .{
909897 .polynomial = 0x42f0e1eba9ea3693,
910898 .initial = 0xffffffffffffffff,
911899 .reflect_input = true,
......@@ -913,10 +901,109 @@ pub const Crc64Xz = Crc(u64, .{
913901 .xor_output = 0xffffffffffffffff,
914902});
915903
916pub const Crc82Darc = Crc(u82, .{
904pub const Crc82Darc = Generic(u82, .{
917905 .polynomial = 0x0308c0111011401440411,
918906 .initial = 0x000000000000000000000,
919907 .reflect_input = true,
920908 .reflect_output = true,
921909 .xor_output = 0x000000000000000000000,
922910});
911
912pub fn Algorithm(comptime W: type) type {
913 return struct {
914 polynomial: W,
915 initial: W,
916 reflect_input: bool,
917 reflect_output: bool,
918 xor_output: W,
919 };
920}
921
922pub fn Generic(comptime W: type, comptime algorithm: Algorithm(W)) type {
923 return struct {
924 const Self = @This();
925 const I = if (@bitSizeOf(W) < 8) u8 else W;
926 const lookup_table = blk: {
927 @setEvalBranchQuota(2500);
928 const poly = reflect(algorithm.polynomial);
929 var table: [256]I = undefined;
930 for (&table, 0..) |*e, i| {
931 var crc: I = i;
932 if (algorithm.reflect_input) {
933 var j: usize = 0;
934 while (j < 8) : (j += 1) {
935 crc = (crc >> 1) ^ ((crc & 1) * poly);
936 }
937 } else {
938 crc <<= @bitSizeOf(I) - 8;
939 var j: usize = 0;
940 while (j < 8) : (j += 1) {
941 crc = (crc << 1) ^ (((crc >> (@bitSizeOf(I) - 1)) & 1) * poly);
942 }
943 }
944 e.* = crc;
945 }
946 break :blk table;
947 };
948
949 crc: I,
950
951 pub fn init() Self {
952 const initial = reflect(algorithm.initial);
953 return .{ .crc = initial };
954 }
955
956 inline fn tableEntry(index: I) I {
957 const short: u8 = @truncate(index);
958 return lookup_table[short];
959 }
960
961 pub fn update(self: *Self, bytes: []const u8) void {
962 var i: usize = 0;
963 if (@bitSizeOf(I) <= 8) {
964 while (i < bytes.len) : (i += 1) {
965 self.crc = tableEntry(self.crc ^ bytes[i]);
966 }
967 } else if (algorithm.reflect_input) {
968 while (i < bytes.len) : (i += 1) {
969 const table_index = self.crc ^ bytes[i];
970 self.crc = tableEntry(table_index) ^ (self.crc >> 8);
971 }
972 } else {
973 while (i < bytes.len) : (i += 1) {
974 const table_index = (self.crc >> (@bitSizeOf(I) - 8)) ^ bytes[i];
975 self.crc = tableEntry(table_index) ^ (self.crc << 8);
976 }
977 }
978 }
979
980 pub fn final(self: Self) W {
981 var c = self.crc;
982 if (algorithm.reflect_input != algorithm.reflect_output) {
983 c = @bitReverse(c);
984 }
985 if (!algorithm.reflect_output) {
986 c >>= @bitSizeOf(I) - @bitSizeOf(W);
987 }
988 return @intCast(c ^ algorithm.xor_output);
989 }
990
991 pub fn hash(bytes: []const u8) W {
992 var c = Self.init();
993 c.update(bytes);
994 return c.final();
995 }
996
997 fn reflect(x: I) I {
998 const offset = @bitSizeOf(I) - @bitSizeOf(W);
999 if (algorithm.reflect_input)
1000 return @bitReverse(x) >> offset
1001 else
1002 return x << offset;
1003 }
1004 };
1005}
1006
1007test {
1008 _ = @import("crc/test.zig");
1009}
lib/std/hash/crc/impl.zig deleted-103
......@@ -1,103 +0,0 @@
1// There is a generic CRC implementation "Crc()" which can be parameterized via
2// the Algorithm struct for a plethora of uses.
3//
4// The primary interface for all of the standard CRC algorithms is the
5// generated file "crc.zig", which uses the implementation code here to define
6// many standard CRCs.
7
8const std = @import("std");
9
10pub fn Algorithm(comptime W: type) type {
11 return struct {
12 polynomial: W,
13 initial: W,
14 reflect_input: bool,
15 reflect_output: bool,
16 xor_output: W,
17 };
18}
19
20pub fn Crc(comptime W: type, comptime algorithm: Algorithm(W)) type {
21 return struct {
22 const Self = @This();
23 const I = if (@bitSizeOf(W) < 8) u8 else W;
24 const lookup_table = blk: {
25 @setEvalBranchQuota(2500);
26 const poly = reflect(algorithm.polynomial);
27 var table: [256]I = undefined;
28 for (&table, 0..) |*e, i| {
29 var crc: I = i;
30 if (algorithm.reflect_input) {
31 var j: usize = 0;
32 while (j < 8) : (j += 1) {
33 crc = (crc >> 1) ^ ((crc & 1) * poly);
34 }
35 } else {
36 crc <<= @bitSizeOf(I) - 8;
37 var j: usize = 0;
38 while (j < 8) : (j += 1) {
39 crc = (crc << 1) ^ (((crc >> (@bitSizeOf(I) - 1)) & 1) * poly);
40 }
41 }
42 e.* = crc;
43 }
44 break :blk table;
45 };
46
47 crc: I,
48
49 pub fn init() Self {
50 const initial = reflect(algorithm.initial);
51 return .{ .crc = initial };
52 }
53
54 inline fn tableEntry(index: I) I {
55 const short: u8 = @truncate(index);
56 return lookup_table[short];
57 }
58
59 pub fn update(self: *Self, bytes: []const u8) void {
60 var i: usize = 0;
61 if (@bitSizeOf(I) <= 8) {
62 while (i < bytes.len) : (i += 1) {
63 self.crc = tableEntry(self.crc ^ bytes[i]);
64 }
65 } else if (algorithm.reflect_input) {
66 while (i < bytes.len) : (i += 1) {
67 const table_index = self.crc ^ bytes[i];
68 self.crc = tableEntry(table_index) ^ (self.crc >> 8);
69 }
70 } else {
71 while (i < bytes.len) : (i += 1) {
72 const table_index = (self.crc >> (@bitSizeOf(I) - 8)) ^ bytes[i];
73 self.crc = tableEntry(table_index) ^ (self.crc << 8);
74 }
75 }
76 }
77
78 pub fn final(self: Self) W {
79 var c = self.crc;
80 if (algorithm.reflect_input != algorithm.reflect_output) {
81 c = @bitReverse(c);
82 }
83 if (!algorithm.reflect_output) {
84 c >>= @bitSizeOf(I) - @bitSizeOf(W);
85 }
86 return @intCast(c ^ algorithm.xor_output);
87 }
88
89 pub fn hash(bytes: []const u8) W {
90 var c = Self.init();
91 c.update(bytes);
92 return c.final();
93 }
94
95 fn reflect(x: I) I {
96 const offset = @bitSizeOf(I) - @bitSizeOf(W);
97 if (algorithm.reflect_input)
98 return @bitReverse(x) >> offset
99 else
100 return x << offset;
101 }
102 };
103}