|
1 | 1 | // There is a generic CRC implementation "Crc()" which can be paramterized via
|
2 |
| -// the Algorithm struct for a plethora of uses, along with two implementations |
3 |
| -// of CRC32 implemented with the following key characteristics: |
4 |
| -// |
5 |
| -// - Crc32WithPoly uses 8Kb of tables but is ~10x faster than the small method. |
6 |
| -// |
7 |
| -// - Crc32SmallWithPoly uses only 64 bytes of memory but is slower. Be aware that this is |
8 |
| -// still moderately fast just slow relative to the slicing approach. |
| 2 | +// the Algorithm struct for a plethora of uses. |
9 | 3 | //
|
10 | 4 | // The primary interface for all of the standard CRC algorithms is the
|
11 | 5 | // generated file "crc.zig", which uses the implementation code here to define
|
@@ -108,134 +102,11 @@ pub fn Crc(comptime W: type, comptime algorithm: Algorithm(W)) type {
|
108 | 102 | }
|
109 | 103 |
|
110 | 104 | pub const Polynomial = enum(u32) {
|
111 |
| - IEEE = 0xedb88320, |
112 |
| - Castagnoli = 0x82f63b78, |
113 |
| - Koopman = 0xeb31d82e, |
| 105 | + IEEE = @compileError("use Crc with algorithm .Crc32IsoHdlc"), |
| 106 | + Castagnoli = @compileError("use Crc with algorithm .Crc32Iscsi"), |
| 107 | + Koopman = @compileError("use Crc with algorithm .Crc32Koopman"), |
114 | 108 | _,
|
115 | 109 | };
|
116 | 110 |
|
117 |
| -// slicing-by-8 crc32 implementation. |
118 |
| -pub fn Crc32WithPoly(comptime poly: Polynomial) type { |
119 |
| - return struct { |
120 |
| - const Self = @This(); |
121 |
| - const lookup_tables = block: { |
122 |
| - @setEvalBranchQuota(20000); |
123 |
| - var tables: [8][256]u32 = undefined; |
124 |
| - |
125 |
| - for (&tables[0], 0..) |*e, i| { |
126 |
| - var crc = @as(u32, @intCast(i)); |
127 |
| - var j: usize = 0; |
128 |
| - while (j < 8) : (j += 1) { |
129 |
| - if (crc & 1 == 1) { |
130 |
| - crc = (crc >> 1) ^ @intFromEnum(poly); |
131 |
| - } else { |
132 |
| - crc = (crc >> 1); |
133 |
| - } |
134 |
| - } |
135 |
| - e.* = crc; |
136 |
| - } |
137 |
| - |
138 |
| - var i: usize = 0; |
139 |
| - while (i < 256) : (i += 1) { |
140 |
| - var crc = tables[0][i]; |
141 |
| - var j: usize = 1; |
142 |
| - while (j < 8) : (j += 1) { |
143 |
| - const index: u8 = @truncate(crc); |
144 |
| - crc = tables[0][index] ^ (crc >> 8); |
145 |
| - tables[j][i] = crc; |
146 |
| - } |
147 |
| - } |
148 |
| - |
149 |
| - break :block tables; |
150 |
| - }; |
151 |
| - |
152 |
| - crc: u32, |
153 |
| - |
154 |
| - pub fn init() Self { |
155 |
| - return Self{ .crc = 0xffffffff }; |
156 |
| - } |
157 |
| - |
158 |
| - pub fn update(self: *Self, input: []const u8) void { |
159 |
| - var i: usize = 0; |
160 |
| - while (i + 8 <= input.len) : (i += 8) { |
161 |
| - const p = input[i..][0..8]; |
162 |
| - |
163 |
| - // Unrolling this way gives ~50Mb/s increase |
164 |
| - self.crc ^= std.mem.readInt(u32, p[0..4], .little); |
165 |
| - |
166 |
| - self.crc = |
167 |
| - lookup_tables[0][p[7]] ^ |
168 |
| - lookup_tables[1][p[6]] ^ |
169 |
| - lookup_tables[2][p[5]] ^ |
170 |
| - lookup_tables[3][p[4]] ^ |
171 |
| - lookup_tables[4][@as(u8, @truncate(self.crc >> 24))] ^ |
172 |
| - lookup_tables[5][@as(u8, @truncate(self.crc >> 16))] ^ |
173 |
| - lookup_tables[6][@as(u8, @truncate(self.crc >> 8))] ^ |
174 |
| - lookup_tables[7][@as(u8, @truncate(self.crc >> 0))]; |
175 |
| - } |
176 |
| - |
177 |
| - while (i < input.len) : (i += 1) { |
178 |
| - const index = @as(u8, @truncate(self.crc)) ^ input[i]; |
179 |
| - self.crc = (self.crc >> 8) ^ lookup_tables[0][index]; |
180 |
| - } |
181 |
| - } |
182 |
| - |
183 |
| - pub fn final(self: *Self) u32 { |
184 |
| - return ~self.crc; |
185 |
| - } |
186 |
| - |
187 |
| - pub fn hash(input: []const u8) u32 { |
188 |
| - var c = Self.init(); |
189 |
| - c.update(input); |
190 |
| - return c.final(); |
191 |
| - } |
192 |
| - }; |
193 |
| -} |
194 |
| - |
195 |
| -// half-byte lookup table implementation. |
196 |
| -pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type { |
197 |
| - return struct { |
198 |
| - const Self = @This(); |
199 |
| - const lookup_table = block: { |
200 |
| - var table: [16]u32 = undefined; |
201 |
| - |
202 |
| - for (&table, 0..) |*e, i| { |
203 |
| - var crc = @as(u32, @intCast(i * 16)); |
204 |
| - var j: usize = 0; |
205 |
| - while (j < 8) : (j += 1) { |
206 |
| - if (crc & 1 == 1) { |
207 |
| - crc = (crc >> 1) ^ @intFromEnum(poly); |
208 |
| - } else { |
209 |
| - crc = (crc >> 1); |
210 |
| - } |
211 |
| - } |
212 |
| - e.* = crc; |
213 |
| - } |
214 |
| - |
215 |
| - break :block table; |
216 |
| - }; |
217 |
| - |
218 |
| - crc: u32, |
219 |
| - |
220 |
| - pub fn init() Self { |
221 |
| - return Self{ .crc = 0xffffffff }; |
222 |
| - } |
223 |
| - |
224 |
| - pub fn update(self: *Self, input: []const u8) void { |
225 |
| - for (input) |b| { |
226 |
| - self.crc = lookup_table[@as(u4, @truncate(self.crc ^ (b >> 0)))] ^ (self.crc >> 4); |
227 |
| - self.crc = lookup_table[@as(u4, @truncate(self.crc ^ (b >> 4)))] ^ (self.crc >> 4); |
228 |
| - } |
229 |
| - } |
230 |
| - |
231 |
| - pub fn final(self: *Self) u32 { |
232 |
| - return ~self.crc; |
233 |
| - } |
234 |
| - |
235 |
| - pub fn hash(input: []const u8) u32 { |
236 |
| - var c = Self.init(); |
237 |
| - c.update(input); |
238 |
| - return c.final(); |
239 |
| - } |
240 |
| - }; |
241 |
| -} |
| 111 | +pub const Crc32WithPoly = @compileError("use Crc instead"); |
| 112 | +pub const Crc32SmallWithPoly = @compileError("use Crc instead"); |
0 commit comments