authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-21 11:30:59+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-21 12:09:27+11:00
loga34c2de7bcdce8e563ee23bcf0f508e556efd763
tree9067b51aa1998a7168967689051e829c9f1c2583
parenta74f800dd79fec48a50152394c9fb3c2d6f080d0

std.hash: use std.math.rotl in Xxhash64 and Xxhash32


1 files changed, 12 insertions(+), 12 deletions(-)

lib/std/hash/xxhash.zig+12-12
...@@ -2,9 +2,7 @@ const std = @import("std");...@@ -2,9 +2,7 @@ const std = @import("std");
2const mem = std.mem;2const mem = std.mem;
3const expectEqual = std.testing.expectEqual;3const expectEqual = std.testing.expectEqual;
44
5inline fn rotl(comptime count: comptime_int, value: anytype) @TypeOf(value) {5const rotl = std.math.rotl;
6 return (value << count) | (value >> (@bitSizeOf(@TypeOf(value)) - count));
7}
86
9pub const XxHash64 = struct {7pub const XxHash64 = struct {
10 acc1: u64,8 acc1: u64,
...@@ -71,7 +69,7 @@ pub const XxHash64 = struct {...@@ -71,7 +69,7 @@ pub const XxHash64 = struct {
7169
72 inline fn round(acc: u64, lane: u64) u64 {70 inline fn round(acc: u64, lane: u64) u64 {
73 const a = acc +% (lane *% prime_2);71 const a = acc +% (lane *% prime_2);
74 const b = rotl(31, a);72 const b = rotl(u64, a, 31);
75 return b *% prime_1;73 return b *% prime_1;
76 }74 }
7775
...@@ -81,7 +79,8 @@ pub const XxHash64 = struct {...@@ -81,7 +79,8 @@ pub const XxHash64 = struct {
81 if (self.byte_count < 32) {79 if (self.byte_count < 32) {
82 acc = self.seed +% prime_5;80 acc = self.seed +% prime_5;
83 } else {81 } else {
84 acc = rotl(1, self.acc1) +% rotl(7, self.acc2) +% rotl(12, self.acc3) +% rotl(18, self.acc4);82 acc = rotl(u64, self.acc1, 1) +% rotl(u64, self.acc2, 7) +%
83 rotl(u64, self.acc3, 12) +% rotl(u64, self.acc4, 18);
85 acc = mergeAccumulator(acc, self.acc1);84 acc = mergeAccumulator(acc, self.acc1);
86 acc = mergeAccumulator(acc, self.acc2);85 acc = mergeAccumulator(acc, self.acc2);
87 acc = mergeAccumulator(acc, self.acc3);86 acc = mergeAccumulator(acc, self.acc3);
...@@ -94,14 +93,14 @@ pub const XxHash64 = struct {...@@ -94,14 +93,14 @@ pub const XxHash64 = struct {
94 while (pos + 8 <= self.buf_len) : (pos += 8) {93 while (pos + 8 <= self.buf_len) : (pos += 8) {
95 const lane = mem.readIntLittle(u64, self.buf[pos..][0..8]);94 const lane = mem.readIntLittle(u64, self.buf[pos..][0..8]);
96 acc ^= round(0, lane);95 acc ^= round(0, lane);
97 acc = rotl(27, acc) *% prime_1;96 acc = rotl(u64, acc, 27) *% prime_1;
98 acc +%= prime_4;97 acc +%= prime_4;
99 }98 }
10099
101 if (pos + 4 <= self.buf_len) {100 if (pos + 4 <= self.buf_len) {
102 const lane = @as(u64, mem.readIntLittle(u32, self.buf[pos..][0..4]));101 const lane = @as(u64, mem.readIntLittle(u32, self.buf[pos..][0..4]));
103 acc ^= lane *% prime_1;102 acc ^= lane *% prime_1;
104 acc = rotl(23, acc) *% prime_2;103 acc = rotl(u64, acc, 23) *% prime_2;
105 acc +%= prime_3;104 acc +%= prime_3;
106 pos += 4;105 pos += 4;
107 }106 }
...@@ -109,7 +108,7 @@ pub const XxHash64 = struct {...@@ -109,7 +108,7 @@ pub const XxHash64 = struct {
109 while (pos < self.buf_len) : (pos += 1) {108 while (pos < self.buf_len) : (pos += 1) {
110 const lane = @as(u64, self.buf[pos]);109 const lane = @as(u64, self.buf[pos]);
111 acc ^= lane *% prime_5;110 acc ^= lane *% prime_5;
112 acc = rotl(11, acc) *% prime_1;111 acc = rotl(u64, acc, 11) *% prime_1;
113 }112 }
114113
115 acc ^= acc >> 33;114 acc ^= acc >> 33;
...@@ -199,7 +198,7 @@ pub const XxHash32 = struct {...@@ -199,7 +198,7 @@ pub const XxHash32 = struct {
199198
200 inline fn round(acc: u32, lane: u32) u32 {199 inline fn round(acc: u32, lane: u32) u32 {
201 const a = acc +% (lane *% prime_2);200 const a = acc +% (lane *% prime_2);
202 const b = rotl(13, a);201 const b = rotl(u32, a, 13);
203 return b *% prime_1;202 return b *% prime_1;
204 }203 }
205204
...@@ -209,7 +208,8 @@ pub const XxHash32 = struct {...@@ -209,7 +208,8 @@ pub const XxHash32 = struct {
209 if (self.byte_count < 16) {208 if (self.byte_count < 16) {
210 acc = self.seed +% prime_5;209 acc = self.seed +% prime_5;
211 } else {210 } else {
212 acc = rotl(1, self.acc1) +% rotl(7, self.acc2) +% rotl(12, self.acc3) +% rotl(18, self.acc4);211 acc = rotl(u32, self.acc1, 1) +% rotl(u32, self.acc2, 7) +%
212 rotl(u32, self.acc3, 12) +% rotl(u32, self.acc4, 18);
213 }213 }
214214
215 acc = acc +% @intCast(u32, self.byte_count) +% @intCast(u32, self.buf_len);215 acc = acc +% @intCast(u32, self.byte_count) +% @intCast(u32, self.buf_len);
...@@ -218,13 +218,13 @@ pub const XxHash32 = struct {...@@ -218,13 +218,13 @@ pub const XxHash32 = struct {
218 while (pos + 4 <= self.buf_len) : (pos += 4) {218 while (pos + 4 <= self.buf_len) : (pos += 4) {
219 const lane = mem.readIntLittle(u32, self.buf[pos..][0..4]);219 const lane = mem.readIntLittle(u32, self.buf[pos..][0..4]);
220 acc +%= lane *% prime_3;220 acc +%= lane *% prime_3;
221 acc = rotl(17, acc) *% prime_4;221 acc = rotl(u32, acc, 17) *% prime_4;
222 }222 }
223223
224 while (pos < self.buf_len) : (pos += 1) {224 while (pos < self.buf_len) : (pos += 1) {
225 const lane = @as(u32, self.buf[pos]);225 const lane = @as(u32, self.buf[pos]);
226 acc +%= lane *% prime_5;226 acc +%= lane *% prime_5;
227 acc = rotl(11, acc) *% prime_1;227 acc = rotl(u32, acc, 11) *% prime_1;
228 }228 }
229229
230 acc ^= acc >> 15;230 acc ^= acc >> 15;