authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2023-06-17 20:22:16+12:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-17 12:06:40-07:00
log5baa05664e6dac0f473c8411f6e9d8e0f62555a9
tree1e28c1373579c158938bf52e76fe246d986434fd
parent6f766fbf008160150a6a164c2dae5a6ee2a5543c

wyhash: support comptime usage

Closes #16070.

1 files changed, 23 insertions(+), 11 deletions(-)

lib/std/hash/wyhash.zig+23-11
......@@ -68,18 +68,20 @@ pub const Wyhash = struct {
6868 if (self.total_len <= 16) {
6969 newSelf.smallKey(input);
7070 } else {
71 var offset: usize = 0;
7172 if (self.buf_len < 16) {
7273 var scratch: [16]u8 = undefined;
7374 const rem = 16 - self.buf_len;
7475 @memcpy(scratch[0..rem], self.buf[self.buf.len - rem ..][0..rem]);
7576 @memcpy(scratch[rem..][0..self.buf_len], self.buf[0..self.buf_len]);
7677
77 // Same as input with lookbehind to pad to 16-bytes
78 input = scratch[rem..];
78 // Same as input but with additional bytes preceeding start in case of a short buffer
79 input = &scratch;
80 offset = rem;
7981 }
8082
8183 newSelf.final0();
82 newSelf.final1(input);
84 newSelf.final1(input, offset);
8385 }
8486
8587 return newSelf.final2();
......@@ -145,19 +147,21 @@ pub const Wyhash = struct {
145147 self.state[0] ^= self.state[1] ^ self.state[2];
146148 }
147149
148 // Input must reside in a 16-byte buffer. The input slice passed be offset into it in which
149 // case this function will index in front of the slice.
150 inline fn final1(self: *Wyhash, input: []const u8) void {
151 std.debug.assert(input.len <= 48);
150 // input_lb must be at least 16-bytes long (in shorter key cases the smallKey function will be
151 // used instead). We use an index into a slice to for comptime processing as opposed to if we
152 // used pointers.
153 inline fn final1(self: *Wyhash, input_lb: []const u8, start_pos: usize) void {
154 std.debug.assert(input_lb.len >= 16);
155 std.debug.assert(input_lb.len - start_pos <= 48);
156 const input = input_lb[start_pos..];
152157
153158 var i: usize = 0;
154159 while (i + 16 < input.len) : (i += 16) {
155160 self.state[0] = mix(read(8, input[i..]) ^ secret[1], read(8, input[i + 8 ..]) ^ self.state[0]);
156161 }
157162
158 // Possible lookbehind past pointer start.
159 self.a = read(8, (input.ptr + input.len - 16)[0..8]);
160 self.b = read(8, (input.ptr + input.len - 8)[0..8]);
163 self.a = read(8, input_lb[input_lb.len - 16 ..][0..8]);
164 self.b = read(8, input_lb[input_lb.len - 8 ..][0..8]);
161165 }
162166
163167 inline fn final2(self: *Wyhash) u64 {
......@@ -180,7 +184,7 @@ pub const Wyhash = struct {
180184 }
181185 self.final0();
182186 }
183 self.final1(input[i..]);
187 self.final1(input, i);
184188 }
185189
186190 self.total_len = input.len;
......@@ -213,6 +217,14 @@ test "test vectors" {
213217 }
214218}
215219
220test "test vectors at comptime" {
221 comptime {
222 inline for (vectors) |e| {
223 try expectEqual(e.expected, Wyhash.hash(e.seed, e.input));
224 }
225 }
226}
227
216228test "test vectors streaming" {
217229 const step = 5;
218230