diff options
| author | fn ⌃ ⌥ <70830482+FnControlOption@users.noreply.github.com> | 2023-01-22 07:16:49 -0800 |
|---|---|---|
| committer | fn ⌃ ⌥ <70830482+FnControlOption@users.noreply.github.com> | 2023-01-22 07:16:49 -0800 |
| commit | 33682a29c6feadaf14cd8ad37f6a9480eda2d49a (patch) | |
| tree | 05688e7537b6151eebea2977029d74148ece8e7e /lib/std/hash/crc.zig | |
| parent | 6089ed9ee77ed034a39c2b557f4608cd8d779d3f (diff) | |
| download | zig-33682a29c6feadaf14cd8ad37f6a9480eda2d49a.tar.gz zig-33682a29c6feadaf14cd8ad37f6a9480eda2d49a.zip | |
Rewrite update_crc_catalog in zig and move tests to separate file
Diffstat (limited to 'lib/std/hash/crc.zig')
| -rw-r--r-- | lib/std/hash/crc.zig | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/lib/std/hash/crc.zig b/lib/std/hash/crc.zig index 288ce2c8ca..1c69978f80 100644 --- a/lib/std/hash/crc.zig +++ b/lib/std/hash/crc.zig @@ -14,13 +14,11 @@ pub usingnamespace @import("crc/catalog.zig"); pub fn Algorithm(comptime W: type) type { return struct { - poly: W, - init: W, - refin: bool, - refout: bool, - xorout: W, - check: W, - residue: W, + polynomial: W, + initial: W, + reflect_input: bool, + reflect_output: bool, + xor_output: W, }; } @@ -31,15 +29,15 @@ pub fn Crc(comptime W: type, comptime algorithm: Algorithm(W)) type { const lookup_table = blk: { @setEvalBranchQuota(2500); - const poly = if (algorithm.refin) - @bitReverse(@as(I, algorithm.poly)) >> (@bitSizeOf(I) - @bitSizeOf(W)) + const poly = if (algorithm.reflect_input) + @bitReverse(@as(I, algorithm.polynomial)) >> (@bitSizeOf(I) - @bitSizeOf(W)) else - @as(I, algorithm.poly) << (@bitSizeOf(I) - @bitSizeOf(W)); + @as(I, algorithm.polynomial) << (@bitSizeOf(I) - @bitSizeOf(W)); var table: [256]I = undefined; for (table) |*e, i| { var crc: I = i; - if (algorithm.refin) { + if (algorithm.reflect_input) { var j: usize = 0; while (j < 8) : (j += 1) { crc = (crc >> 1) ^ ((crc & 1) * poly); @@ -59,10 +57,10 @@ pub fn Crc(comptime W: type, comptime algorithm: Algorithm(W)) type { crc: I, pub fn init() Self { - const initial = if (algorithm.refin) - @bitReverse(@as(I, algorithm.init)) >> (@bitSizeOf(I) - @bitSizeOf(W)) + const initial = if (algorithm.reflect_input) + @bitReverse(@as(I, algorithm.initial)) >> (@bitSizeOf(I) - @bitSizeOf(W)) else - @as(I, algorithm.init) << (@bitSizeOf(I) - @bitSizeOf(W)); + @as(I, algorithm.initial) << (@bitSizeOf(I) - @bitSizeOf(W)); return Self{ .crc = initial }; } @@ -76,7 +74,7 @@ pub fn Crc(comptime W: type, comptime algorithm: Algorithm(W)) type { while (i < bytes.len) : (i += 1) { self.crc = tableEntry(self.crc ^ bytes[i]); } - } else if (algorithm.refin) { + } else if (algorithm.reflect_input) { while (i < bytes.len) : (i += 1) { const table_index = self.crc ^ bytes[i]; self.crc = tableEntry(table_index) ^ (self.crc >> 8); @@ -91,13 +89,13 @@ pub fn Crc(comptime W: type, comptime algorithm: Algorithm(W)) type { pub fn final(self: Self) W { var c = self.crc; - if (algorithm.refin != algorithm.refout) { + if (algorithm.reflect_input != algorithm.reflect_output) { c = @bitReverse(c); } - if (!algorithm.refout) { + if (!algorithm.reflect_output) { c >>= @bitSizeOf(I) - @bitSizeOf(W); } - return @intCast(W, c ^ algorithm.xorout); + return @intCast(W, c ^ algorithm.xor_output); } pub fn hash(bytes: []const u8) W { |
