Skip to content

std/hash: Revert crc32 api change #3118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions std/hash/benchmark.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ const hashes = [_]Hash{
.name = "adler32",
},
Hash{
.ty = hash.crc.Crc32WithPoly(.IEEE),
.ty = hash.crc.Crc32WithPoly(hash.crc.Polynomial.IEEE),
.name = "crc32-slicing-by-8",
},
Hash{
.ty = hash.crc.Crc32SmallWithPoly(.IEEE),
.ty = hash.crc.Crc32SmallWithPoly(hash.crc.Polynomial.IEEE),
.name = "crc32-half-byte-lookup",
},
Hash{
Expand Down
26 changes: 13 additions & 13 deletions std/hash/crc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const std = @import("../std.zig");
const debug = std.debug;
const testing = std.testing;

pub const Polynomial = enum(u32) {
IEEE = 0xedb88320,
Castagnoli = 0x82f63b78,
Koopman = 0xeb31d82e,
pub const Polynomial = struct {
pub const IEEE = 0xedb88320;
pub const Castagnoli = 0x82f63b78;
pub const Koopman = 0xeb31d82e;
};

// IEEE is by far the most common CRC and so is aliased by default.
pub const Crc32 = Crc32WithPoly(.IEEE);
pub const Crc32 = Crc32WithPoly(Polynomial.IEEE);

// slicing-by-8 crc32 implementation.
pub fn Crc32WithPoly(comptime poly: Polynomial) type {
pub fn Crc32WithPoly(comptime poly: u32) type {
return struct {
const Self = @This();
const lookup_tables = comptime block: {
Expand All @@ -31,7 +31,7 @@ pub fn Crc32WithPoly(comptime poly: Polynomial) type {
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
crc = (crc >> 1) ^ @enumToInt(poly);
crc = (crc >> 1) ^ poly;
} else {
crc = (crc >> 1);
}
Expand Down Expand Up @@ -100,23 +100,23 @@ pub fn Crc32WithPoly(comptime poly: Polynomial) type {
}

test "crc32 ieee" {
const Crc32Ieee = Crc32WithPoly(.IEEE);
const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE);

testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
testing.expect(Crc32Ieee.hash("abc") == 0x352441c2);
}

test "crc32 castagnoli" {
const Crc32Castagnoli = Crc32WithPoly(.Castagnoli);
const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli);

testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
testing.expect(Crc32Castagnoli.hash("abc") == 0x364b3fb7);
}

// half-byte lookup table implementation.
pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
pub fn Crc32SmallWithPoly(comptime poly: u32) type {
return struct {
const Self = @This();
const lookup_table = comptime block: {
Expand All @@ -127,7 +127,7 @@ pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
crc = (crc >> 1) ^ @enumToInt(poly);
crc = (crc >> 1) ^ poly;
} else {
crc = (crc >> 1);
}
Expand Down Expand Up @@ -164,15 +164,15 @@ pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
}

test "small crc32 ieee" {
const Crc32Ieee = Crc32SmallWithPoly(.IEEE);
const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE);

testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
testing.expect(Crc32Ieee.hash("abc") == 0x352441c2);
}

test "small crc32 castagnoli" {
const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli);
const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli);

testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
Expand Down