authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-01-18 11:58:29+10:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-18 17:42:45-05:00
log9e6e1e58bb163868db51832e54c323a9ab893329
tree2b5e9a6437107128a7412acc4a7de23f9bb65dff
parentb72f858194a3f6391de06d83ffa49596cfce21a4

std: use non-exhaustive enums from crc module

Un-reverts PR #3118

2 files changed, 16 insertions(+), 15 deletions(-)

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