authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-25 17:00:17-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-11-25 17:00:17-05:00
logf4e042a4c3e8e299d273f00b757ec9fce840c3a6
tree6b3d19d3c713f4d10894af354353d34bb6f2838e
parente2f24a2d7096e4a28ba74513ed9473da0b7fb372
parentca67f80b6e9599246441e0f3b016fabc5c3315aa
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21858 from francescoalemanno/patch-1

add improved std.hash.int - deprecate std.hash.uint32

1 files changed, 74 insertions(+), 9 deletions(-)

lib/std/hash.zig+74-9
...@@ -37,20 +37,85 @@ pub const XxHash3 = xxhash.XxHash3;...@@ -37,20 +37,85 @@ 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/// This is handy if you have a u32 and want a u32 and don't want to take a40/// Integer-to-integer hashing for bit widths <= 256.
41/// detour through many layers of abstraction elsewhere in the std.hash41pub fn int(input: anytype) @TypeOf(input) {
42/// namespace.42 // This function is only intended for integer types
43/// Copied from https://nullprogram.com/blog/2018/07/31/43 const info = @typeInfo(@TypeOf(input)).int;
44 const bits = info.bits;
45 // Convert input to unsigned integer (easier to deal with)
46 const Uint = @Type(.{ .int = .{ .bits = bits, .signedness = .unsigned } });
47 const u_input: Uint = @bitCast(input);
48 if (bits > 256) @compileError("bit widths > 256 are unsupported, use std.hash.autoHash functionality.");
49 // For bit widths that don't have a dedicated function, use a heuristic
50 // construction with a multiplier suited to diffusion -
51 // a mod 2^bits where a^2 - 46 * a + 1 = 0 mod 2^(bits + 4),
52 // on Mathematica: bits = 256; BaseForm[Solve[1 - 46 a + a^2 == 0, a, Modulus -> 2^(bits + 4)][[-1]][[1]][[2]], 16]
53 const mult: Uint = @truncate(0xfac2e27ed2036860a062b5f264d80a512b00aa459b448bf1eca24d41c96f59e5b);
54 // The bit width of the input integer determines how to hash it
55 const output = switch (bits) {
56 0...2 => u_input *% mult,
57 16 => uint16(u_input),
58 32 => uint32(u_input),
59 64 => uint64(u_input),
60 else => blk: {
61 var x: Uint = u_input;
62 inline for (0..4) |_| {
63 x ^= x >> (bits / 2);
64 x *%= mult;
65 }
66 break :blk x;
67 },
68 };
69 return @bitCast(output);
70}
71
72/// Source: https://github.com/skeeto/hash-prospector
73fn uint16(input: u16) u16 {
74 var x: u16 = input;
75 x = (x ^ (x >> 7)) *% 0x2993;
76 x = (x ^ (x >> 5)) *% 0xe877;
77 x = (x ^ (x >> 9)) *% 0x0235;
78 x = x ^ (x >> 10);
79 return x;
80}
81
82/// DEPRECATED: use std.hash.int()
83/// Source: https://github.com/skeeto/hash-prospector
44pub fn uint32(input: u32) u32 {84pub fn uint32(input: u32) u32 {
45 var x: u32 = input;85 var x: u32 = input;
46 x ^= x >> 16;86 x = (x ^ (x >> 17)) *% 0xed5ad4bb;
47 x *%= 0x7feb352d;87 x = (x ^ (x >> 11)) *% 0xac4c1b51;
48 x ^= x >> 15;88 x = (x ^ (x >> 15)) *% 0x31848bab;
49 x *%= 0x846ca68b;89 x = x ^ (x >> 14);
50 x ^= x >> 16;
51 return x;90 return x;
52}91}
5392
93/// Source: https://github.com/jonmaiga/mx3
94fn uint64(input: u64) u64 {
95 var x: u64 = input;
96 const c = 0xbea225f9eb34556d;
97 x = (x ^ (x >> 32)) *% c;
98 x = (x ^ (x >> 29)) *% c;
99 x = (x ^ (x >> 32)) *% c;
100 x = x ^ (x >> 29);
101 return x;
102}
103
104test int {
105 const expectEqual = @import("std").testing.expectEqual;
106 try expectEqual(0x1, int(@as(u1, 1)));
107 try expectEqual(0x3, int(@as(u2, 1)));
108 try expectEqual(0x4, int(@as(u3, 1)));
109 try expectEqual(0xD6, int(@as(u8, 1)));
110 try expectEqual(0x2880, int(@as(u16, 1)));
111 try expectEqual(0x2880, int(@as(i16, 1)));
112 try expectEqual(0x838380, int(@as(u24, 1)));
113 try expectEqual(0x42741D6, int(@as(u32, 1)));
114 try expectEqual(0x42741D6, int(@as(i32, 1)));
115 try expectEqual(0x71894DE00D9981F, int(@as(u64, 1)));
116 try expectEqual(0x71894DE00D9981F, int(@as(i64, 1)));
117}
118
54test {119test {
55 _ = adler;120 _ = adler;
56 _ = auto_hash;121 _ = auto_hash;