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;...@@ -37,25 +37,31 @@ 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/// Deprecated in favor of `int`.
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.
44pub fn uint32(input: u32) u32 {41pub fn uint32(input: u32) u32 {
45 return int(u32, input);42 return int(input);
46}43}
4744
48/// Applies a bit-mangling transformation to an unsigned integer type `T`.45/// Applies a bit-mangling transformation to an unsigned integer type `T`.
49/// Optimized per type: for `u16` and `u32`, Skeeto's xorshift-multiply; for `u64`, Maiga's mx3.46/// 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.47/// Falls back on an avalanche pattern for other integer types, ensuring high entropy.
51/// Only unsigned types are accepted; signed types will raise a compile-time error.48pub fn int(input: anytype) @TypeOf(input) {
52pub fn int(comptime T: type, input: T) T {49 const info = @typeInfo(@TypeOf(input)).int;
53 const tInfo = @typeInfo(T).int;50 if (info.signedness == .signed) {
54 if (tInfo.signedness != .unsigned) @compileError("type has to be unsigned integer");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 }
55 var x = input;61 var x = input;
56 switch (T) {62 switch (info.bits) {
57 u16 => {63 16 => {
58 //https://github.com/skeeto/hash-prospector64 // https://github.com/skeeto/hash-prospector
59 // 3-round xorshift-multiply (-Xn3)65 // 3-round xorshift-multiply (-Xn3)
60 // bias = 0.004597670901882060266 // bias = 0.0045976709018820602
61 x = (x ^ (x >> 7)) *% 0x2993;67 x = (x ^ (x >> 7)) *% 0x2993;
...@@ -63,36 +69,34 @@ pub fn int(comptime T: type, input: T) T {...@@ -63,36 +69,34 @@ pub fn int(comptime T: type, input: T) T {
63 x = (x ^ (x >> 9)) *% 0x0235;69 x = (x ^ (x >> 9)) *% 0x0235;
64 x = x ^ (x >> 10);70 x = x ^ (x >> 10);
65 },71 },
66 u32 => {72 32 => {
67 // https://github.com/skeeto/hash-prospector73 // https://github.com/skeeto/hash-prospector
68 x = (x ^ (x >> 17)) *% 0xed5ad4bb;74 x = (x ^ (x >> 17)) *% 0xed5ad4bb;
69 x = (x ^ (x >> 11)) *% 0xac4c1b51;75 x = (x ^ (x >> 11)) *% 0xac4c1b51;
70 x = (x ^ (x >> 15)) *% 0x31848bab;76 x = (x ^ (x >> 15)) *% 0x31848bab;
71 x = x ^ (x >> 14);77 x = x ^ (x >> 14);
72 },78 },
73 u64 => {79 64 => {
74 // https://github.com/jonmaiga/mx380 // https://github.com/jonmaiga/mx3
75 // https://github.com/jonmaiga/mx3/blob/48924ee743d724aea2cafd2b4249ef8df57fa8b9/mx3.h#L1781 // https://github.com/jonmaiga/mx3/blob/48924ee743d724aea2cafd2b4249ef8df57fa8b9/mx3.h#L17
76 const C = 0xbea225f9eb34556d;82 const c = 0xbea225f9eb34556d;
77 x = (x ^ (x >> 32)) *% C;83 x = (x ^ (x >> 32)) *% c;
78 x = (x ^ (x >> 29)) *% C;84 x = (x ^ (x >> 29)) *% c;
79 x = (x ^ (x >> 32)) *% C;85 x = (x ^ (x >> 32)) *% c;
80 x = x ^ (x >> 29);86 x = x ^ (x >> 29);
81 },87 },
82 else => {88 else => {
83 // this construction provides robust avalanche properties, but it is not optimal for any given size.89 // This construction provides robust avalanche properties, but it is not optimal for any given size.
84 const Tsize = @bitSizeOf(T);90 const hsize = info.bits >> 1;
85 if (Tsize < 4) @compileError("not implemented.");91 const c = comptime blk: {
86 const hsize = Tsize >> 1;92 const max = (1 << info.bits) - 1;
87 const C = comptime blk: {
88 const max = (1 << Tsize) - 1;
89 var mul = 1;93 var mul = 1;
90 while (mul * 3 < max) mul *= 3;94 while (mul * 3 < max) mul *= 3;
91 break :blk ((mul ^ (mul >> hsize)) | 1);95 break :blk ((mul ^ (mul >> hsize)) | 1);
92 };96 };
93 inline for (0..2) |_| {97 inline for (0..2) |_| {
94 x = (x ^ (x >> hsize + 1)) *% C;98 x = (x ^ (x >> hsize + 1)) *% c;
95 x = (x ^ (x >> hsize - 1)) *% C;99 x = (x ^ (x >> hsize - 1)) *% c;
96 }100 }
97 x ^= (x >> hsize);101 x ^= (x >> hsize);
98 },102 },
...@@ -100,14 +104,15 @@ pub fn int(comptime T: type, input: T) T {...@@ -100,14 +104,15 @@ pub fn int(comptime T: type, input: T) T {
100 return x;104 return x;
101}105}
102106
103test "bit manglers" {107test int {
104 const expect = @import("std").testing.expect;108 const expectEqual = @import("std").testing.expectEqual;
105 try expect(int(u4, 1) == 0xC);109 try expectEqual(0xC, int(@as(u4, 1)));
106 try expect(int(u8, 1) == 0x4F);110 try expectEqual(0x4F, int(@as(u8, 1)));
107 try expect(int(u16, 1) == 0x2880);111 try expectEqual(0x4F, int(@as(i8, 1)));
108 try expect(int(u32, 1) == 0x42741D6);112 try expectEqual(0x2880, int(@as(u16, 1)));
109 try expect(int(u64, 1) == 0x71894DE00D9981F);113 try expectEqual(0x42741D6, int(@as(u32, 1)));
110 try expect(int(u128, 1) == 0x50BC2BB18910C3DE0BAA2CE0D0C5B83E);114 try expectEqual(0x71894DE00D9981F, int(@as(u64, 1)));
115 try expectEqual(0x50BC2BB18910C3DE0BAA2CE0D0C5B83E, int(@as(u128, 1)));
111}116}
112117
113test {118test {