authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-01 11:57:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-24 15:27:03-08:00
log5ad44c14b0c1f8e55856a8f048a570dad564c231
tree550e7f773da1d4d31d9e97105f1adf4a5ba695df
parentaee6f7d7eed535597e4b506132e211b9dce311dd

std.hash.int: use anytype instead of explicit type parameter

also * allow signed ints, simply bitcast them to unsigned * handle odd bit sizes by upcasting and then truncating * naming conventions * remove redundant code * better use of testing API

1 files changed, 40 insertions(+), 35 deletions(-)

lib/std/hash.zig+40-35
......@@ -37,25 +37,31 @@ pub const XxHash3 = xxhash.XxHash3;
3737pub const XxHash64 = xxhash.XxHash64;
3838pub const XxHash32 = xxhash.XxHash32;
3939
40/// Deprecated: use std.hash.int(comptime T, input T) T where T is an unsigned integer type.
41/// This is handy if you have a u32 and want a u32 and don't want to take a
42/// detour through many layers of abstraction elsewhere in the std.hash
43/// namespace.
40/// Deprecated in favor of `int`.
4441pub fn uint32(input: u32) u32 {
45 return int(u32, input);
42 return int(input);
4643}
4744
4845/// Applies a bit-mangling transformation to an unsigned integer type `T`.
4946/// Optimized per type: for `u16` and `u32`, Skeeto's xorshift-multiply; for `u64`, Maiga's mx3.
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");
47/// Falls back on an avalanche pattern for other integer types, ensuring high entropy.
48pub fn int(input: anytype) @TypeOf(input) {
49 const info = @typeInfo(@TypeOf(input)).int;
50 if (info.signedness == .signed) {
51 const Unsigned = @Type(.{ .int = .{ .signedness = .unsigned, .bits = info.bits } });
52 const casted: Unsigned = @bitCast(input);
53 return @bitCast(int(casted));
54 } else if (info.bits < 16) {
55 return @truncate(int(@as(u16, input)));
56 } else if (info.bits < 32) {
57 return @truncate(int(@as(u32, input)));
58 } else if (info.bits < 64) {
59 return @truncate(int(@as(u64, input)));
60 }
5561 var x = input;
56 switch (T) {
57 u16 => {
58 //https://github.com/skeeto/hash-prospector
62 switch (info.bits) {
63 16 => {
64 // https://github.com/skeeto/hash-prospector
5965 // 3-round xorshift-multiply (-Xn3)
6066 // bias = 0.0045976709018820602
6167 x = (x ^ (x >> 7)) *% 0x2993;
......@@ -63,36 +69,34 @@ pub fn int(comptime T: type, input: T) T {
6369 x = (x ^ (x >> 9)) *% 0x0235;
6470 x = x ^ (x >> 10);
6571 },
66 u32 => {
72 32 => {
6773 // https://github.com/skeeto/hash-prospector
6874 x = (x ^ (x >> 17)) *% 0xed5ad4bb;
6975 x = (x ^ (x >> 11)) *% 0xac4c1b51;
7076 x = (x ^ (x >> 15)) *% 0x31848bab;
7177 x = x ^ (x >> 14);
7278 },
73 u64 => {
79 64 => {
7480 // https://github.com/jonmaiga/mx3
7581 // 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;
82 const c = 0xbea225f9eb34556d;
83 x = (x ^ (x >> 32)) *% c;
84 x = (x ^ (x >> 29)) *% c;
85 x = (x ^ (x >> 32)) *% c;
8086 x = x ^ (x >> 29);
8187 },
8288 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 // This construction provides robust avalanche properties, but it is not optimal for any given size.
90 const hsize = info.bits >> 1;
91 const c = comptime blk: {
92 const max = (1 << info.bits) - 1;
8993 var mul = 1;
9094 while (mul * 3 < max) mul *= 3;
9195 break :blk ((mul ^ (mul >> hsize)) | 1);
9296 };
9397 inline for (0..2) |_| {
94 x = (x ^ (x >> hsize + 1)) *% C;
95 x = (x ^ (x >> hsize - 1)) *% C;
98 x = (x ^ (x >> hsize + 1)) *% c;
99 x = (x ^ (x >> hsize - 1)) *% c;
96100 }
97101 x ^= (x >> hsize);
98102 },
......@@ -100,14 +104,15 @@ pub fn int(comptime T: type, input: T) T {
100104 return x;
101105}
102106
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);
107test int {
108 const expectEqual = @import("std").testing.expectEqual;
109 try expectEqual(0xC, int(@as(u4, 1)));
110 try expectEqual(0x4F, int(@as(u8, 1)));
111 try expectEqual(0x4F, int(@as(i8, 1)));
112 try expectEqual(0x2880, int(@as(u16, 1)));
113 try expectEqual(0x42741D6, int(@as(u32, 1)));
114 try expectEqual(0x71894DE00D9981F, int(@as(u64, 1)));
115 try expectEqual(0x50BC2BB18910C3DE0BAA2CE0D0C5B83E, int(@as(u128, 1)));
111116}
112117
113118test {