@@ -9,17 +9,18 @@ const std = @import("../std.zig");
9
9
const debug = std .debug ;
10
10
const testing = std .testing ;
11
11
12
- pub const Polynomial = struct {
13
- pub const IEEE = 0xedb88320 ;
14
- pub const Castagnoli = 0x82f63b78 ;
15
- pub const Koopman = 0xeb31d82e ;
12
+ pub const Polynomial = enum (u32 ) {
13
+ IEEE = 0xedb88320 ,
14
+ Castagnoli = 0x82f63b78 ,
15
+ Koopman = 0xeb31d82e ,
16
+ _ ,
16
17
};
17
18
18
19
// IEEE is by far the most common CRC and so is aliased by default.
19
- pub const Crc32 = Crc32WithPoly (Polynomial .IEEE );
20
+ pub const Crc32 = Crc32WithPoly (.IEEE );
20
21
21
22
// slicing-by-8 crc32 implementation.
22
- pub fn Crc32WithPoly (comptime poly : u32 ) type {
23
+ pub fn Crc32WithPoly (comptime poly : Polynomial ) type {
23
24
return struct {
24
25
const Self = @This ();
25
26
const lookup_tables = comptime block : {
@@ -31,7 +32,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
31
32
var j : usize = 0 ;
32
33
while (j < 8 ) : (j += 1 ) {
33
34
if (crc & 1 == 1 ) {
34
- crc = (crc >> 1 ) ^ poly ;
35
+ crc = (crc >> 1 ) ^ @enumToInt ( poly ) ;
35
36
} else {
36
37
crc = (crc >> 1 );
37
38
}
@@ -100,23 +101,23 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
100
101
}
101
102
102
103
test "crc32 ieee" {
103
- const Crc32Ieee = Crc32WithPoly (Polynomial .IEEE );
104
+ const Crc32Ieee = Crc32WithPoly (.IEEE );
104
105
105
106
testing .expect (Crc32Ieee .hash ("" ) == 0x00000000 );
106
107
testing .expect (Crc32Ieee .hash ("a" ) == 0xe8b7be43 );
107
108
testing .expect (Crc32Ieee .hash ("abc" ) == 0x352441c2 );
108
109
}
109
110
110
111
test "crc32 castagnoli" {
111
- const Crc32Castagnoli = Crc32WithPoly (Polynomial .Castagnoli );
112
+ const Crc32Castagnoli = Crc32WithPoly (.Castagnoli );
112
113
113
114
testing .expect (Crc32Castagnoli .hash ("" ) == 0x00000000 );
114
115
testing .expect (Crc32Castagnoli .hash ("a" ) == 0xc1d04330 );
115
116
testing .expect (Crc32Castagnoli .hash ("abc" ) == 0x364b3fb7 );
116
117
}
117
118
118
119
// half-byte lookup table implementation.
119
- pub fn Crc32SmallWithPoly (comptime poly : u32 ) type {
120
+ pub fn Crc32SmallWithPoly (comptime poly : Polynomial ) type {
120
121
return struct {
121
122
const Self = @This ();
122
123
const lookup_table = comptime block : {
@@ -127,7 +128,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
127
128
var j : usize = 0 ;
128
129
while (j < 8 ) : (j += 1 ) {
129
130
if (crc & 1 == 1 ) {
130
- crc = (crc >> 1 ) ^ poly ;
131
+ crc = (crc >> 1 ) ^ @enumToInt ( poly ) ;
131
132
} else {
132
133
crc = (crc >> 1 );
133
134
}
@@ -164,15 +165,15 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
164
165
}
165
166
166
167
test "small crc32 ieee" {
167
- const Crc32Ieee = Crc32SmallWithPoly (Polynomial .IEEE );
168
+ const Crc32Ieee = Crc32SmallWithPoly (.IEEE );
168
169
169
170
testing .expect (Crc32Ieee .hash ("" ) == 0x00000000 );
170
171
testing .expect (Crc32Ieee .hash ("a" ) == 0xe8b7be43 );
171
172
testing .expect (Crc32Ieee .hash ("abc" ) == 0x352441c2 );
172
173
}
173
174
174
175
test "small crc32 castagnoli" {
175
- const Crc32Castagnoli = Crc32SmallWithPoly (Polynomial .Castagnoli );
176
+ const Crc32Castagnoli = Crc32SmallWithPoly (.Castagnoli );
176
177
177
178
testing .expect (Crc32Castagnoli .hash ("" ) == 0x00000000 );
178
179
testing .expect (Crc32Castagnoli .hash ("a" ) == 0xc1d04330 );
0 commit comments