1pub const Adler32 = @import("hash/Adler32.zig");
2
3const auto_hash = @import("hash/auto_hash.zig");
4pub const autoHash = auto_hash.autoHash;
5pub const autoHashStrat = auto_hash.hash;
6pub const Strategy = auto_hash.HashStrategy;
7
8pub const crc = @import("hash/crc.zig");
9pub const Crc32 = crc.@"CRC-32/ISO-HDLC";
10
11const fnv = @import("hash/fnv.zig");
12pub const Fnv1a_32 = fnv.Fnv1a_32;
13pub const Fnv1a_64 = fnv.Fnv1a_64;
14pub const Fnv1a_128 = fnv.Fnv1a_128;
15
16const siphash = @import("crypto/siphash.zig");
17pub const SipHash64 = siphash.SipHash64;
18pub const SipHash128 = siphash.SipHash128;
19
20pub const murmur = @import("hash/murmur.zig");
21pub const Murmur2_32 = murmur.Murmur2_32;
22
23pub const Murmur2_64 = murmur.Murmur2_64;
24pub const Murmur3_32 = murmur.Murmur3_32;
25
26pub const cityhash = @import("hash/cityhash.zig");
27pub const CityHash32 = cityhash.CityHash32;
28pub const CityHash64 = cityhash.CityHash64;
29
30const wyhash = @import("hash/wyhash.zig");
31pub const Wyhash = wyhash.Wyhash;
32
33const xxhash = @import("hash/xxhash.zig");
34pub const XxHash3 = xxhash.XxHash3;
35pub const XxHash64 = xxhash.XxHash64;
36pub const XxHash32 = xxhash.XxHash32;
37
38/// Integer-to-integer hashing for bit widths <= 256.
39pub fn int(input: anytype) @TypeOf(input) {
40 // This function is only intended for integer types
41 const info = @typeInfo(@TypeOf(input)).int;
42 const bits = info.bits;
43 // Convert input to unsigned integer (easier to deal with)
44 const Uint = @Int(.unsigned, bits);
45 const u_input: Uint = @bitCast(input);
46 if (bits > 256) @compileError("bit widths > 256 are unsupported, use std.hash.autoHash functionality.");
47 // For bit widths that don't have a dedicated function, use a heuristic
48 // construction with a multiplier suited to diffusion -
49 // a mod 2^bits where a^2 - 46 * a + 1 = 0 mod 2^(bits + 4),
50 // on Mathematica: bits = 256; BaseForm[Solve[1 - 46 a + a^2 == 0, a, Modulus -> 2^(bits + 4)][[-1]][[1]][[2]], 16]
51 const mult: Uint = @truncate(0xfac2e27ed2036860a062b5f264d80a512b00aa459b448bf1eca24d41c96f59e5b);
52 // The bit width of the input integer determines how to hash it
53 const output = switch (bits) {
54 0...2 => u_input *% mult,
55 16 => uint16(u_input),
56 32 => uint32(u_input),
57 64 => uint64(u_input),
58 else => blk: {
59 var x: Uint = u_input;
60 inline for (0..4) |_| {
61 x ^= x >> (bits / 2);
62 x *%= mult;
63 }
64 break :blk x;
65 },
66 };
67 return @bitCast(output);
68}
69
70/// Source: https://github.com/skeeto/hash-prospector
71fn uint16(input: u16) u16 {
72 var x: u16 = input;
73 x = (x ^ (x >> 7)) *% 0x2993;
74 x = (x ^ (x >> 5)) *% 0xe877;
75 x = (x ^ (x >> 9)) *% 0x0235;
76 x = x ^ (x >> 10);
77 return x;
78}
79
80/// Source: https://github.com/skeeto/hash-prospector
81fn uint32(input: u32) u32 {
82 var x: u32 = input;
83 x = (x ^ (x >> 17)) *% 0xed5ad4bb;
84 x = (x ^ (x >> 11)) *% 0xac4c1b51;
85 x = (x ^ (x >> 15)) *% 0x31848bab;
86 x = x ^ (x >> 14);
87 return x;
88}
89
90/// Source: https://github.com/jonmaiga/mx3
91fn uint64(input: u64) u64 {
92 var x: u64 = input;
93 const c = 0xbea225f9eb34556d;
94 x = (x ^ (x >> 32)) *% c;
95 x = (x ^ (x >> 29)) *% c;
96 x = (x ^ (x >> 32)) *% c;
97 x = x ^ (x >> 29);
98 return x;
99}
100
101test int {
102 const expectEqual = @import("std").testing.expectEqual;
103 try expectEqual(0x1, int(@as(u1, 1)));
104 try expectEqual(0x3, int(@as(u2, 1)));
105 try expectEqual(0x4, int(@as(u3, 1)));
106 try expectEqual(0xD6, int(@as(u8, 1)));
107 try expectEqual(0x2880, int(@as(u16, 1)));
108 try expectEqual(0x2880, int(@as(i16, 1)));
109 try expectEqual(0x838380, int(@as(u24, 1)));
110 try expectEqual(0x42741D6, int(@as(u32, 1)));
111 try expectEqual(0x42741D6, int(@as(i32, 1)));
112 try expectEqual(0x71894DE00D9981F, int(@as(u64, 1)));
113 try expectEqual(0x71894DE00D9981F, int(@as(i64, 1)));
114}
115
116test {
117 _ = Adler32;
118 _ = auto_hash;
119 _ = crc;
120 _ = fnv;
121 _ = murmur;
122 _ = cityhash;
123 _ = wyhash;
124 _ = xxhash;
125}