authorgravatar for 50984334+francescoalemanno@users.noreply.github.comFrancesco Alemanno <50984334+francescoalemanno@users.noreply.github.com> 2024-10-30 14:14:12+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-24 15:27:03-08:00
logaee6f7d7eed535597e4b506132e211b9dce311dd
treedafd5dd769f1f0bf3bce341f79b63c596654dceb
parente2f24a2d7096e4a28ba74513ed9473da0b7fb372

std.hash: improve simple hashing of unsigned integers

Before, the default bit mixer was very biased, and after a lot of searching it turns out that selecting a better solution is hard. I wrote a custom statistical analysis taylored for bit mixers in order to select the best one at each size (u64/u32/u16), compared a lot of mixers, and packaged the best ones in this commit.

1 files changed, 66 insertions(+), 7 deletions(-)

lib/std/hash.zig+66-7
...@@ -37,20 +37,79 @@ pub const XxHash3 = xxhash.XxHash3;...@@ -37,20 +37,79 @@ pub const XxHash3 = xxhash.XxHash3;
37pub const XxHash64 = xxhash.XxHash64;37pub const XxHash64 = xxhash.XxHash64;
38pub const XxHash32 = xxhash.XxHash32;38pub const XxHash32 = xxhash.XxHash32;
3939
40/// Deprecated: use std.hash.int(comptime T, input T) T where T is an unsigned integer type.
40/// This is handy if you have a u32 and want a u32 and don't want to take a41/// This is handy if you have a u32 and want a u32 and don't want to take a
41/// detour through many layers of abstraction elsewhere in the std.hash42/// detour through many layers of abstraction elsewhere in the std.hash
42/// namespace.43/// namespace.
43/// Copied from https://nullprogram.com/blog/2018/07/31/
44pub fn uint32(input: u32) u32 {44pub fn uint32(input: u32) u32 {
45 var x: u32 = input;45 return int(u32, input);
46 x ^= x >> 16;46}
47 x *%= 0x7feb352d;47
48 x ^= x >> 15;48/// Applies a bit-mangling transformation to an unsigned integer type `T`.
49 x *%= 0x846ca68b;49/// Optimized per type: for `u16` and `u32`, Skeeto's xorshift-multiply; for `u64`, Maiga's mx3.
50 x ^= x >> 16;50/// Falls back on an avalanche pattern for other unsigned types, ensuring high entropy.
51/// Only unsigned types are accepted; signed types will raise a compile-time error.
52pub fn int(comptime T: type, input: T) T {
53 const tInfo = @typeInfo(T).int;
54 if (tInfo.signedness != .unsigned) @compileError("type has to be unsigned integer");
55 var x = input;
56 switch (T) {
57 u16 => {
58 //https://github.com/skeeto/hash-prospector
59 // 3-round xorshift-multiply (-Xn3)
60 // bias = 0.0045976709018820602
61 x = (x ^ (x >> 7)) *% 0x2993;
62 x = (x ^ (x >> 5)) *% 0xe877;
63 x = (x ^ (x >> 9)) *% 0x0235;
64 x = x ^ (x >> 10);
65 },
66 u32 => {
67 // https://github.com/skeeto/hash-prospector
68 x = (x ^ (x >> 17)) *% 0xed5ad4bb;
69 x = (x ^ (x >> 11)) *% 0xac4c1b51;
70 x = (x ^ (x >> 15)) *% 0x31848bab;
71 x = x ^ (x >> 14);
72 },
73 u64 => {
74 // https://github.com/jonmaiga/mx3
75 // https://github.com/jonmaiga/mx3/blob/48924ee743d724aea2cafd2b4249ef8df57fa8b9/mx3.h#L17
76 const C = 0xbea225f9eb34556d;
77 x = (x ^ (x >> 32)) *% C;
78 x = (x ^ (x >> 29)) *% C;
79 x = (x ^ (x >> 32)) *% C;
80 x = x ^ (x >> 29);
81 },
82 else => {
83 // this construction provides robust avalanche properties, but it is not optimal for any given size.
84 const Tsize = @bitSizeOf(T);
85 if (Tsize < 4) @compileError("not implemented.");
86 const hsize = Tsize >> 1;
87 const C = comptime blk: {
88 const max = (1 << Tsize) - 1;
89 var mul = 1;
90 while (mul * 3 < max) mul *= 3;
91 break :blk ((mul ^ (mul >> hsize)) | 1);
92 };
93 inline for (0..2) |_| {
94 x = (x ^ (x >> hsize + 1)) *% C;
95 x = (x ^ (x >> hsize - 1)) *% C;
96 }
97 x ^= (x >> hsize);
98 },
99 }
51 return x;100 return x;
52}101}
53102
103test "bit manglers" {
104 const expect = @import("std").testing.expect;
105 try expect(int(u4, 1) == 0xC);
106 try expect(int(u8, 1) == 0x4F);
107 try expect(int(u16, 1) == 0x2880);
108 try expect(int(u32, 1) == 0x42741D6);
109 try expect(int(u64, 1) == 0x71894DE00D9981F);
110 try expect(int(u128, 1) == 0x50BC2BB18910C3DE0BAA2CE0D0C5B83E);
111}
112
54test {113test {
55 _ = adler;114 _ = adler;
56 _ = auto_hash;115 _ = auto_hash;