| ... | @@ -41,17 +41,6 @@ fn @"i like cheese"(arena: std.mem.Allocator, io: Io, args: []const []const u8) | ... | @@ -41,17 +41,6 @@ fn @"i like cheese"(arena: std.mem.Allocator, io: Io, args: []const []const u8) |
| 41 | \\ | 41 | \\ |
| 42 | \\const builtin = @import("builtin"); | 42 | \\const builtin = @import("builtin"); |
| 43 | \\ | 43 | \\ |
| 44 | \\pub const Crc32Iscsi = if (builtin.cpu.hasAll(.x86, &.{ .@"64bit", .crc32 })) | | |
| 45 | \\ @import("crc/Crc32c.zig") | | |
| 46 | \\else | | |
| 47 | \\ Generic(u32, .{ | | |
| 48 | \\ .polynomial = 0x1edc6f41, | | |
| 49 | \\ .initial = 0xffffffff, | | |
| 50 | \\ .reflect_input = true, | | |
| 51 | \\ .reflect_output = true, | | |
| 52 | \\ .xor_output = 0xffffffff, | | |
| 53 | \\ }); | | |
| 54 | \\ | | |
| 55 | ); | 44 | ); |
| 56 | | 45 | |
| 57 | var zig_test_file = try crc_target_dir.createFile(io, "test.zig", .{}); | 46 | var zig_test_file = try crc_target_dir.createFile(io, "test.zig", .{}); |
| ... | @@ -70,35 +59,24 @@ fn @"i like cheese"(arena: std.mem.Allocator, io: Io, args: []const []const u8) | ... | @@ -70,35 +59,24 @@ fn @"i like cheese"(arena: std.mem.Allocator, io: Io, args: []const []const u8) |
| 70 | \\const crc = @import("../crc.zig"); | 59 | \\const crc = @import("../crc.zig"); |
| 71 | \\ | 60 | \\ |
| 72 | \\test "crc32 ieee regression" { | 61 | \\test "crc32 ieee regression" { |
| 73 | \\ const crc32 = crc.Crc32IsoHdlc; | 62 | \\ const Crc = crc.@"CRC-32/ISO-HDLC"; |
| 74 | \\ try testing.expectEqual(crc32.hash(""), 0x00000000); | 63 | \\ try testing.expectEqual(Crc.hash(""), 0x00000000); |
| 75 | \\ try testing.expectEqual(crc32.hash("a"), 0xe8b7be43); | 64 | \\ try testing.expectEqual(Crc.hash("a"), 0xe8b7be43); |
| 76 | \\ try testing.expectEqual(crc32.hash("abc"), 0x352441c2); | 65 | \\ try testing.expectEqual(Crc.hash("abc"), 0x352441c2); |
| 77 | \\} | 66 | \\} |
| 78 | \\ | 67 | \\ |
| 79 | \\test "crc32 castagnoli regression" { | 68 | \\test "crc32 castagnoli regression" { |
| 80 | \\ const crc32 = crc.Crc32Iscsi; | 69 | \\ const Crc = crc.@"CRC-32/ISCSI"; |
| 81 | \\ try testing.expectEqual(crc32.hash(""), 0x00000000); | 70 | \\ try testing.expectEqual(Crc.hash(""), 0x00000000); |
| 82 | \\ try testing.expectEqual(crc32.hash("a"), 0xc1d04330); | 71 | \\ try testing.expectEqual(Crc.hash("a"), 0xc1d04330); |
| 83 | \\ try testing.expectEqual(crc32.hash("abc"), 0x364b3fb7); | 72 | \\ try testing.expectEqual(Crc.hash("abc"), 0x364b3fb7); |
| 84 | \\} | 73 | \\} |
| 85 | \\ | 74 | \\ |
| 86 | \\test "crc32 koopman regression" { | 75 | \\test "crc32 koopman regression" { |
| 87 | \\ const crc32 = crc.Crc32Koopman; | 76 | \\ const Crc = crc.@"CRC-32/KOOPMAN"; |
| 88 | \\ try testing.expectEqual(crc32.hash(""), 0x00000000); | 77 | \\ try testing.expectEqual(Crc.hash(""), 0x00000000); |
| 89 | \\ try testing.expectEqual(crc32.hash("a"), 0x0da2aa8a); | 78 | \\ try testing.expectEqual(Crc.hash("a"), 0x0da2aa8a); |
| 90 | \\ try testing.expectEqual(crc32.hash("abc"), 0xba2322ac); | 79 | \\ try testing.expectEqual(Crc.hash("abc"), 0xba2322ac); |
| 91 | \\} | | |
| 92 | \\ | | |
| 93 | \\test "CRC-32/ISCSI" { | | |
| 94 | \\ const Crc32Iscsi = crc.Crc32Iscsi; | | |
| 95 | \\ | | |
| 96 | \\ try testing.expectEqual(@as(u32, 0xe3069283), Crc32Iscsi.hash("123456789")); | | |
| 97 | \\ | | |
| 98 | \\ var c = Crc32Iscsi.init(); | | |
| 99 | \\ c.update("1234"); | | |
| 100 | \\ c.update("56789"); | | |
| 101 | \\ try testing.expectEqual(@as(u32, 0xe3069283), c.final()); | | |
| 102 | \\} | 80 | \\} |
| 103 | \\ | 81 | \\ |
| 104 | ); | 82 | ); |
| ... | @@ -147,17 +125,34 @@ fn @"i like cheese"(arena: std.mem.Allocator, io: Io, args: []const []const u8) | ... | @@ -147,17 +125,34 @@ fn @"i like cheese"(arena: std.mem.Allocator, io: Io, args: []const []const u8) |
| 147 | } | 125 | } |
| 148 | } | 126 | } |
| 149 | | 127 | |
| 150 | try code_writer.print( | 128 | if (mem.eql(u8, name, "CRC-32/ISCSI")) { |
| 151 | \\ | 129 | try code_writer.print( |
| 152 | \\pub const {f} = Generic(u{s}, .{{ | 130 | \\ |
| 153 | \\ .polynomial = {s}, | 131 | \\pub const {f} = if (builtin.cpu.hasAll(.x86, &.{{ .@"64bit", .crc32 }})) |
| 154 | \\ .initial = {s}, | 132 | \\ @import("crc/Crc32c.zig") |
| 155 | \\ .reflect_input = {s}, | 133 | \\else |
| 156 | \\ .reflect_output = {s}, | 134 | \\ Generic(u{s}, .{{ |
| 157 | \\ .xor_output = {s}, | 135 | \\ .polynomial = {s}, |
| 158 | \\}}); | 136 | \\ .initial = {s}, |
| 159 | \\ | 137 | \\ .reflect_input = {s}, |
| 160 | , .{ std.zig.fmtId(name), width, poly, init, refin, refout, xorout }); | 138 | \\ .reflect_output = {s}, |
| | 139 | \\ .xor_output = {s}, |
| | 140 | \\ }}); |
| | 141 | \\ |
| | 142 | , .{ std.zig.fmtId(name), width, poly, init, refin, refout, xorout }); |
| | 143 | } else { |
| | 144 | try code_writer.print( |
| | 145 | \\ |
| | 146 | \\pub const {f} = Generic(u{s}, .{{ |
| | 147 | \\ .polynomial = {s}, |
| | 148 | \\ .initial = {s}, |
| | 149 | \\ .reflect_input = {s}, |
| | 150 | \\ .reflect_output = {s}, |
| | 151 | \\ .xor_output = {s}, |
| | 152 | \\}}); |
| | 153 | \\ |
| | 154 | , .{ std.zig.fmtId(name), width, poly, init, refin, refout, xorout }); |
| | 155 | } |
| 161 | | 156 | |
| 162 | try test_writer.print( | 157 | try test_writer.print( |
| 163 | \\ | 158 | \\ |