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