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