| ... | ... | @@ -37,20 +37,79 @@ pub const XxHash3 = xxhash.XxHash3; |
| 37 | 37 | pub const XxHash64 = xxhash.XxHash64; |
| 38 | 38 | pub const XxHash32 = xxhash.XxHash32; |
| 39 | 39 | |
| 40 | /// Deprecated: use std.hash.int(comptime T, input T) T where T is an unsigned integer type. |
| 40 | 41 | /// This is handy if you have a u32 and want a u32 and don't want to take a |
| 41 | 42 | /// detour through many layers of abstraction elsewhere in the std.hash |
| 42 | 43 | /// namespace. |
| 43 | | /// Copied from https://nullprogram.com/blog/2018/07/31/ |
| 44 | 44 | pub fn uint32(input: u32) u32 { |
| 45 | | var x: u32 = input; |
| 46 | | x ^= x >> 16; |
| 47 | | x *%= 0x7feb352d; |
| 48 | | x ^= x >> 15; |
| 49 | | x *%= 0x846ca68b; |
| 50 | | x ^= x >> 16; |
| 45 | return int(u32, input); |
| 46 | } |
| 47 | |
| 48 | /// 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. |
| 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. |
| 52 | pub 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 | 100 | return x; |
| 52 | 101 | } |
| 53 | 102 | |
| 103 | test "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 | |
| 54 | 113 | test { |
| 55 | 114 | _ = adler; |
| 56 | 115 | _ = auto_hash; |