authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-08-24 19:05:05+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-08-24 19:05:05+12:00
loga6103522714784681d6a08d1dae4a61e189be044
treecc47d9b1b75593ab9e40c8dc2b354c7b9737c0ae
parentec2f9ef4e8be5995ab652dde59b12ee340a9e28d

std/hash: Revert crc32 api change

This is user specified and the user doesn't necessarily have to use one of the provided polynomials declared hence we can't use an enum. Thanks @daurnimator for catching this.

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

std/hash/benchmark.zig+2-2
......@@ -47,11 +47,11 @@ const hashes = [_]Hash{
4747 .name = "adler32",
4848 },
4949 Hash{
50 .ty = hash.crc.Crc32WithPoly(.IEEE),
50 .ty = hash.crc.Crc32WithPoly(hash.crc.Polynomial.IEEE),
5151 .name = "crc32-slicing-by-8",
5252 },
5353 Hash{
54 .ty = hash.crc.Crc32SmallWithPoly(.IEEE),
54 .ty = hash.crc.Crc32SmallWithPoly(hash.crc.Polynomial.IEEE),
5555 .name = "crc32-half-byte-lookup",
5656 },
5757 Hash{
std/hash/crc.zig+13-13
......@@ -9,17 +9,17 @@ const std = @import("../std.zig");
99const debug = std.debug;
1010const testing = std.testing;
1111
12pub const Polynomial = enum(u32) {
13 IEEE = 0xedb88320,
14 Castagnoli = 0x82f63b78,
15 Koopman = 0xeb31d82e,
12pub const Polynomial = struct {
13 pub const IEEE = 0xedb88320;
14 pub const Castagnoli = 0x82f63b78;
15 pub const Koopman = 0xeb31d82e;
1616};
1717
1818// IEEE is by far the most common CRC and so is aliased by default.
19pub const Crc32 = Crc32WithPoly(.IEEE);
19pub const Crc32 = Crc32WithPoly(Polynomial.IEEE);
2020
2121// slicing-by-8 crc32 implementation.
22pub fn Crc32WithPoly(comptime poly: Polynomial) type {
22pub fn Crc32WithPoly(comptime poly: u32) type {
2323 return struct {
2424 const Self = @This();
2525 const lookup_tables = comptime block: {
......@@ -31,7 +31,7 @@ pub fn Crc32WithPoly(comptime poly: Polynomial) type {
3131 var j: usize = 0;
3232 while (j < 8) : (j += 1) {
3333 if (crc & 1 == 1) {
34 crc = (crc >> 1) ^ @enumToInt(poly);
34 crc = (crc >> 1) ^ poly;
3535 } else {
3636 crc = (crc >> 1);
3737 }
......@@ -100,7 +100,7 @@ pub fn Crc32WithPoly(comptime poly: Polynomial) type {
100100}
101101
102102test "crc32 ieee" {
103 const Crc32Ieee = Crc32WithPoly(.IEEE);
103 const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE);
104104
105105 testing.expect(Crc32Ieee.hash("") == 0x00000000);
106106 testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
......@@ -108,7 +108,7 @@ test "crc32 ieee" {
108108}
109109
110110test "crc32 castagnoli" {
111 const Crc32Castagnoli = Crc32WithPoly(.Castagnoli);
111 const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli);
112112
113113 testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
114114 testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
......@@ -116,7 +116,7 @@ test "crc32 castagnoli" {
116116}
117117
118118// half-byte lookup table implementation.
119pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
119pub fn Crc32SmallWithPoly(comptime poly: u32) type {
120120 return struct {
121121 const Self = @This();
122122 const lookup_table = comptime block: {
......@@ -127,7 +127,7 @@ pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
127127 var j: usize = 0;
128128 while (j < 8) : (j += 1) {
129129 if (crc & 1 == 1) {
130 crc = (crc >> 1) ^ @enumToInt(poly);
130 crc = (crc >> 1) ^ poly;
131131 } else {
132132 crc = (crc >> 1);
133133 }
......@@ -164,7 +164,7 @@ pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
164164}
165165
166166test "small crc32 ieee" {
167 const Crc32Ieee = Crc32SmallWithPoly(.IEEE);
167 const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE);
168168
169169 testing.expect(Crc32Ieee.hash("") == 0x00000000);
170170 testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
......@@ -172,7 +172,7 @@ test "small crc32 ieee" {
172172}
173173
174174test "small crc32 castagnoli" {
175 const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli);
175 const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli);
176176
177177 testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
178178 testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);