| ... | @@ -37,20 +37,85 @@ pub const XxHash3 = xxhash.XxHash3; | ... | @@ -37,20 +37,85 @@ pub const XxHash3 = xxhash.XxHash3; |
| 37 | pub const XxHash64 = xxhash.XxHash64; | 37 | pub const XxHash64 = xxhash.XxHash64; |
| 38 | pub const XxHash32 = xxhash.XxHash32; | 38 | pub const XxHash32 = xxhash.XxHash32; |
| 39 | | 39 | |
| 40 | /// This is handy if you have a u32 and want a u32 and don't want to take a | 40 | /// Integer-to-integer hashing for bit widths <= 256. |
| 41 | /// detour through many layers of abstraction elsewhere in the std.hash | 41 | pub 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 |
| | 73 | fn 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 |
| 44 | pub fn uint32(input: u32) u32 { | 84 | pub 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 | } |
| 53 | | 92 | |
| | 93 | /// Source: https://github.com/jonmaiga/mx3 |
| | 94 | fn 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 | |
| | 104 | test 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 | |
| 54 | test { | 119 | test { |
| 55 | _ = adler; | 120 | _ = adler; |
| 56 | _ = auto_hash; | 121 | _ = auto_hash; |