| ... | ... | @@ -0,0 +1,387 @@ |
| 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 3 | |
| 4 | pub const CityHash32 = struct { |
| 5 | const Self = @This(); |
| 6 | |
| 7 | // Magic numbers for 32-bit hashing. Copied from Murmur3. |
| 8 | const c1: u32 = 0xcc9e2d51; |
| 9 | const c2: u32 = 0x1b873593; |
| 10 | |
| 11 | fn fetch32(ptr: [*]const u8) u32 { |
| 12 | var v: u32 = undefined; |
| 13 | @memcpy(@ptrCast([*]u8, &v), ptr, 4); |
| 14 | if (builtin.endian == builtin.Endian.Big) |
| 15 | return @byteSwap(u32, v); |
| 16 | return v; |
| 17 | } |
| 18 | |
| 19 | // A 32-bit to 32-bit integer hash copied from Murmur3. |
| 20 | fn fmix(h: u32) u32 { |
| 21 | var h1: u32 = h; |
| 22 | h1 ^= h1 >> 16; |
| 23 | h1 *%= 0x85ebca6b; |
| 24 | h1 ^= h1 >> 13; |
| 25 | h1 *%= 0xc2b2ae35; |
| 26 | h1 ^= h1 >> 16; |
| 27 | return h1; |
| 28 | } |
| 29 | |
| 30 | // Rotate right helper |
| 31 | fn rotr32(x: u32, comptime r: u32) u32 { |
| 32 | return (x >> r) | (x << (32 - r)); |
| 33 | } |
| 34 | |
| 35 | // Helper from Murmur3 for combining two 32-bit values. |
| 36 | fn mur(a: u32, h: u32) u32 { |
| 37 | var a1: u32 = a; |
| 38 | var h1: u32 = h; |
| 39 | a1 *%= c1; |
| 40 | a1 = rotr32(a1, 17); |
| 41 | a1 *%= c2; |
| 42 | h1 ^= a1; |
| 43 | h1 = rotr32(h1, 19); |
| 44 | return h1 *% 5 +% 0xe6546b64; |
| 45 | } |
| 46 | |
| 47 | fn hash32Len0To4(str: []const u8) u32 { |
| 48 | const len: u32 = @intCast(u32, str.len); |
| 49 | var b: u32 = 0; |
| 50 | var c: u32 = 9; |
| 51 | for (str) |v| { |
| 52 | b = b *% c1 +% @bitCast(u32, @intCast(i32, @bitCast(i8, v))); |
| 53 | c ^= b; |
| 54 | } |
| 55 | return fmix(mur(b, mur(len, c))); |
| 56 | } |
| 57 | |
| 58 | fn hash32Len5To12(str: []const u8) u32 { |
| 59 | var a: u32 = @intCast(u32, str.len); |
| 60 | var b: u32 = a *% 5; |
| 61 | var c: u32 = 9; |
| 62 | const d: u32 = b; |
| 63 | |
| 64 | a +%= fetch32(str.ptr); |
| 65 | b +%= fetch32(str.ptr + str.len - 4); |
| 66 | c +%= fetch32(str.ptr + ((str.len >> 1) & 4)); |
| 67 | |
| 68 | return fmix(mur(c, mur(b, mur(a, d)))); |
| 69 | } |
| 70 | |
| 71 | fn hash32Len13To24(str: []const u8) u32 { |
| 72 | const len: u32 = @intCast(u32, str.len); |
| 73 | const a: u32 = fetch32(str.ptr + (str.len >> 1) - 4); |
| 74 | const b: u32 = fetch32(str.ptr + 4); |
| 75 | const c: u32 = fetch32(str.ptr + str.len - 8); |
| 76 | const d: u32 = fetch32(str.ptr + (str.len >> 1)); |
| 77 | const e: u32 = fetch32(str.ptr); |
| 78 | const f: u32 = fetch32(str.ptr + str.len - 4); |
| 79 | |
| 80 | return fmix(mur(f, mur(e, mur(d, mur(c, mur(b, mur(a, len))))))); |
| 81 | } |
| 82 | |
| 83 | pub fn hash(str: []const u8) u32 { |
| 84 | if (str.len <= 24) { |
| 85 | if (str.len <= 4) { |
| 86 | return hash32Len0To4(str); |
| 87 | } else { |
| 88 | if (str.len <= 12) |
| 89 | return hash32Len5To12(str); |
| 90 | return hash32Len13To24(str); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | const len: u32 = @intCast(u32, str.len); |
| 95 | var h: u32 = len; |
| 96 | var g: u32 = c1 *% len; |
| 97 | var f: u32 = g; |
| 98 | |
| 99 | const a0: u32 = rotr32(fetch32(str.ptr + str.len - 4) *% c1, 17) *% c2; |
| 100 | const a1: u32 = rotr32(fetch32(str.ptr + str.len - 8) *% c1, 17) *% c2; |
| 101 | const a2: u32 = rotr32(fetch32(str.ptr + str.len - 16) *% c1, 17) *% c2; |
| 102 | const a3: u32 = rotr32(fetch32(str.ptr + str.len - 12) *% c1, 17) *% c2; |
| 103 | const a4: u32 = rotr32(fetch32(str.ptr + str.len - 20) *% c1, 17) *% c2; |
| 104 | |
| 105 | h ^= a0; |
| 106 | h = rotr32(h, 19); |
| 107 | h = h *% 5 +% 0xe6546b64; |
| 108 | h ^= a2; |
| 109 | h = rotr32(h, 19); |
| 110 | h = h *% 5 +% 0xe6546b64; |
| 111 | g ^= a1; |
| 112 | g = rotr32(g, 19); |
| 113 | g = g *% 5 +% 0xe6546b64; |
| 114 | g ^= a3; |
| 115 | g = rotr32(g, 19); |
| 116 | g = g *% 5 +% 0xe6546b64; |
| 117 | f +%= a4; |
| 118 | f = rotr32(f, 19); |
| 119 | f = f *% 5 +% 0xe6546b64; |
| 120 | var iters = (str.len - 1) / 20; |
| 121 | var ptr = str.ptr; |
| 122 | while (iters != 0) : (iters -= 1) { |
| 123 | const b0: u32 = rotr32(fetch32(ptr) *% c1, 17) *% c2; |
| 124 | const b1: u32 = fetch32(ptr + 4); |
| 125 | const b2: u32 = rotr32(fetch32(ptr + 8) *% c1, 17) *% c2; |
| 126 | const b3: u32 = rotr32(fetch32(ptr + 12) *% c1, 17) *% c2; |
| 127 | const b4: u32 = fetch32(ptr + 16); |
| 128 | |
| 129 | h ^= b0; |
| 130 | h = rotr32(h, 18); |
| 131 | h = h *% 5 +% 0xe6546b64; |
| 132 | f +%= b1; |
| 133 | f = rotr32(f, 19); |
| 134 | f = f *% c1; |
| 135 | g +%= b2; |
| 136 | g = rotr32(g, 18); |
| 137 | g = g *% 5 +% 0xe6546b64; |
| 138 | h ^= b3 +% b1; |
| 139 | h = rotr32(h, 19); |
| 140 | h = h *% 5 +% 0xe6546b64; |
| 141 | g ^= b4; |
| 142 | g = @byteSwap(u32, g) *% 5; |
| 143 | h +%= b4 *% 5; |
| 144 | h = @byteSwap(u32, h); |
| 145 | f +%= b0; |
| 146 | const t: u32 = h; |
| 147 | h = f; |
| 148 | f = g; |
| 149 | g = t; |
| 150 | ptr += 20; |
| 151 | } |
| 152 | g = rotr32(g, 11) *% c1; |
| 153 | g = rotr32(g, 17) *% c1; |
| 154 | f = rotr32(f, 11) *% c1; |
| 155 | f = rotr32(f, 17) *% c1; |
| 156 | h = rotr32(h +% g, 19); |
| 157 | h = h *% 5 +% 0xe6546b64; |
| 158 | h = rotr32(h, 17) *% c1; |
| 159 | h = rotr32(h +% f, 19); |
| 160 | h = h *% 5 +% 0xe6546b64; |
| 161 | h = rotr32(h, 17) *% c1; |
| 162 | return h; |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | pub const CityHash64 = struct { |
| 167 | const Self = @This(); |
| 168 | |
| 169 | // Some primes between 2^63 and 2^64 for various uses. |
| 170 | const k0: u64 = 0xc3a5c85c97cb3127; |
| 171 | const k1: u64 = 0xb492b66fbe98f273; |
| 172 | const k2: u64 = 0x9ae16a3b2f90404f; |
| 173 | |
| 174 | fn fetch32(ptr: [*]const u8) u32 { |
| 175 | var v: u32 = undefined; |
| 176 | @memcpy(@ptrCast([*]u8, &v), ptr, 4); |
| 177 | if (builtin.endian == builtin.Endian.Big) |
| 178 | return @byteSwap(u32, v); |
| 179 | return v; |
| 180 | } |
| 181 | |
| 182 | fn fetch64(ptr: [*]const u8) u64 { |
| 183 | var v: u64 = undefined; |
| 184 | @memcpy(@ptrCast([*]u8, &v), ptr, 8); |
| 185 | if (builtin.endian == builtin.Endian.Big) |
| 186 | return @byteSwap(u64, v); |
| 187 | return v; |
| 188 | } |
| 189 | |
| 190 | // Rotate right helper |
| 191 | fn rotr64(x: u64, comptime r: u64) u64 { |
| 192 | return (x >> r) | (x << (64 - r)); |
| 193 | } |
| 194 | |
| 195 | fn shiftmix(v: u64) u64 { |
| 196 | return v ^ (v >> 47); |
| 197 | } |
| 198 | |
| 199 | fn hashLen16(u: u64, v: u64) u64 { |
| 200 | return @inlineCall(hash128To64, u, v); |
| 201 | } |
| 202 | |
| 203 | fn hashLen16Mul(low: u64, high: u64, mul: u64) u64 { |
| 204 | var a: u64 = (low ^ high) *% mul; |
| 205 | a ^= (a >> 47); |
| 206 | var b: u64 = (high ^ a) *% mul; |
| 207 | b ^= (b >> 47); |
| 208 | b *%= mul; |
| 209 | return b; |
| 210 | } |
| 211 | |
| 212 | fn hash128To64(low: u64, high: u64) u64 { |
| 213 | return @inlineCall(hashLen16Mul, low, high, 0x9ddfea08eb382d69); |
| 214 | } |
| 215 | |
| 216 | fn hashLen0To16(str: []const u8) u64 { |
| 217 | const len: u64 = @intCast(u64, str.len); |
| 218 | if (len >= 8) { |
| 219 | const mul: u64 = k2 +% len *% 2; |
| 220 | const a: u64 = fetch64(str.ptr) + k2; |
| 221 | const b: u64 = fetch64(str.ptr + str.len - 8); |
| 222 | const c: u64 = rotr64(b, 37) *% mul +% a; |
| 223 | const d: u64 = (rotr64(a, 25) +% b) *% mul; |
| 224 | return hashLen16Mul(c, d, mul); |
| 225 | } |
| 226 | if (len >= 4) { |
| 227 | const mul: u64 = k2 +% len *% 2; |
| 228 | const a: u64 = fetch32(str.ptr); |
| 229 | return hashLen16Mul(len +% (a << 3), fetch32(str.ptr + str.len - 4), mul); |
| 230 | } |
| 231 | if (len > 0) { |
| 232 | const a: u8 = str[0]; |
| 233 | const b: u8 = str[str.len >> 1]; |
| 234 | const c: u8 = str[str.len - 1]; |
| 235 | const y: u32 = @intCast(u32, a) +% (@intCast(u32, b) << 8); |
| 236 | const z: u32 = @intCast(u32, str.len) +% (@intCast(u32, c) << 2); |
| 237 | return shiftmix(@intCast(u64, y) *% k2 ^ @intCast(u64, z) *% k0) *% k2; |
| 238 | } |
| 239 | return k2; |
| 240 | } |
| 241 | |
| 242 | fn hashLen17To32(str: []const u8) u64 { |
| 243 | const len: u64 = @intCast(u64, str.len); |
| 244 | const mul: u64 = k2 +% len *% 2; |
| 245 | const a: u64 = fetch64(str.ptr) *% k1; |
| 246 | const b: u64 = fetch64(str.ptr + 8); |
| 247 | const c: u64 = fetch64(str.ptr + str.len - 8) *% mul; |
| 248 | const d: u64 = fetch64(str.ptr + str.len - 16) *% k2; |
| 249 | |
| 250 | return hashLen16Mul(rotr64(a +% b, 43) +% rotr64(c, 30) +% d, a +% rotr64(b +% k2, 18) +% c, mul); |
| 251 | } |
| 252 | |
| 253 | fn hashLen33To64(str: []const u8) u64 { |
| 254 | const len: u64 = @intCast(u64, str.len); |
| 255 | const mul: u64 = k2 +% len *% 2; |
| 256 | const a: u64 = fetch64(str.ptr) *% k2; |
| 257 | const b: u64 = fetch64(str.ptr + 8); |
| 258 | const c: u64 = fetch64(str.ptr + str.len - 24); |
| 259 | const d: u64 = fetch64(str.ptr + str.len - 32); |
| 260 | const e: u64 = fetch64(str.ptr + 16) *% k2; |
| 261 | const f: u64 = fetch64(str.ptr + 24) *% 9; |
| 262 | const g: u64 = fetch64(str.ptr + str.len - 8); |
| 263 | const h: u64 = fetch64(str.ptr + str.len - 16) *% mul; |
| 264 | |
| 265 | const u: u64 = rotr64(a +% g, 43) +% (rotr64(b, 30) +% c) *% 9; |
| 266 | const v: u64 = ((a +% g) ^ d) +% f +% 1; |
| 267 | const w: u64 = @byteSwap(u64, (u +% v) *% mul) +% h; |
| 268 | const x: u64 = rotr64(e +% f, 42) +% c; |
| 269 | const y: u64 = (@byteSwap(u64, (v +% w) *% mul) +% g) *% mul; |
| 270 | const z: u64 = e +% f +% c; |
| 271 | const a1: u64 = @byteSwap(u64, (x +% z) *% mul +% y) +% b; |
| 272 | const b1: u64 = shiftmix((z +% a1) *% mul +% d +% h) *% mul; |
| 273 | return b1 +% x; |
| 274 | } |
| 275 | |
| 276 | const WeakPair = struct { |
| 277 | first: u64, |
| 278 | second: u64, |
| 279 | }; |
| 280 | |
| 281 | fn weakHashLen32WithSeedsHelper(w: u64, x: u64, y: u64, z: u64, a: u64, b: u64) WeakPair { |
| 282 | var a1: u64 = a; |
| 283 | var b1: u64 = b; |
| 284 | a1 +%= w; |
| 285 | b1 = rotr64(b1 +% a1 +% z, 21); |
| 286 | var c: u64 = a1; |
| 287 | a1 +%= x; |
| 288 | a1 +%= y; |
| 289 | b1 +%= rotr64(a1, 44); |
| 290 | return WeakPair{ .first = a1 +% z, .second = b1 +% c }; |
| 291 | } |
| 292 | |
| 293 | fn weakHashLen32WithSeeds(ptr: [*]const u8, a: u64, b: u64) WeakPair { |
| 294 | return @inlineCall(weakHashLen32WithSeedsHelper, fetch64(ptr), fetch64(ptr + 8), fetch64(ptr + 16), fetch64(ptr + 24), a, b); |
| 295 | } |
| 296 | |
| 297 | pub fn hash(str: []const u8) u64 { |
| 298 | if (str.len <= 32) { |
| 299 | if (str.len <= 16) { |
| 300 | return hashLen0To16(str); |
| 301 | } else { |
| 302 | return hashLen17To32(str); |
| 303 | } |
| 304 | } else if (str.len <= 64) { |
| 305 | return hashLen33To64(str); |
| 306 | } |
| 307 | |
| 308 | var len: u64 = @intCast(u64, str.len); |
| 309 | |
| 310 | var x: u64 = fetch64(str.ptr + str.len - 40); |
| 311 | var y: u64 = fetch64(str.ptr + str.len - 16) +% fetch64(str.ptr + str.len - 56); |
| 312 | var z: u64 = hashLen16(fetch64(str.ptr + str.len - 48) +% len, fetch64(str.ptr + str.len - 24)); |
| 313 | var v: WeakPair = weakHashLen32WithSeeds(str.ptr + str.len - 64, len, z); |
| 314 | var w: WeakPair = weakHashLen32WithSeeds(str.ptr + str.len - 32, y +% k1, x); |
| 315 | |
| 316 | x = x *% k1 +% fetch64(str.ptr); |
| 317 | len = (len - 1) & ~@intCast(u64, 63); |
| 318 | |
| 319 | var ptr: [*]const u8 = str.ptr; |
| 320 | while (true) { |
| 321 | x = rotr64(x +% y +% v.first +% fetch64(ptr + 8), 37) *% k1; |
| 322 | y = rotr64(y +% v.second +% fetch64(ptr + 48), 42) *% k1; |
| 323 | x ^= w.second; |
| 324 | y +%= v.first +% fetch64(ptr + 40); |
| 325 | z = rotr64(z +% w.first, 33) *% k1; |
| 326 | v = weakHashLen32WithSeeds(ptr, v.second *% k1, x +% w.first); |
| 327 | w = weakHashLen32WithSeeds(ptr + 32, z +% w.second, y +% fetch64(ptr + 16)); |
| 328 | const t: u64 = z; |
| 329 | z = x; |
| 330 | x = t; |
| 331 | |
| 332 | ptr += 64; |
| 333 | len -= 64; |
| 334 | if (len == 0) |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | return hashLen16(hashLen16(v.first, w.first) +% shiftmix(y) *% k1 +% z, hashLen16(v.second, w.second) +% x); |
| 339 | } |
| 340 | |
| 341 | pub fn hashWithSeed(str: []const u8, seed: u64) u64 { |
| 342 | return @inlineCall(Self.hashWithSeeds, str, k2, seed); |
| 343 | } |
| 344 | |
| 345 | pub fn hashWithSeeds(str: []const u8, seed0: u64, seed1: u64) u64 { |
| 346 | return hashLen16(hash(str) -% seed0, seed1); |
| 347 | } |
| 348 | }; |
| 349 | |
| 350 | fn SMHasherTest(comptime hash_fn: var, comptime hashbits: u32) u32 { |
| 351 | const hashbytes = hashbits / 8; |
| 352 | var key: [256]u8 = undefined; |
| 353 | var hashes: [hashbytes * 256]u8 = undefined; |
| 354 | var final: [hashbytes]u8 = undefined; |
| 355 | |
| 356 | @memset(@ptrCast([*]u8, &key[0]), 0, @sizeOf(@typeOf(key))); |
| 357 | @memset(@ptrCast([*]u8, &hashes[0]), 0, @sizeOf(@typeOf(hashes))); |
| 358 | @memset(@ptrCast([*]u8, &final[0]), 0, @sizeOf(@typeOf(final))); |
| 359 | |
| 360 | var i: u32 = 0; |
| 361 | while (i < 256) : (i += 1) { |
| 362 | key[i] = @intCast(u8, i); |
| 363 | |
| 364 | var h = hash_fn(key[0..i], 256 - i); |
| 365 | if (builtin.endian == builtin.Endian.Big) |
| 366 | h = @byteSwap(@typeOf(h), h); |
| 367 | @memcpy(@ptrCast([*]u8, &hashes[i * hashbytes]), @ptrCast([*]u8, &h), hashbytes); |
| 368 | } |
| 369 | |
| 370 | return @intCast(u32, hash_fn(hashes, 0) & 0xffffffff); |
| 371 | } |
| 372 | |
| 373 | fn CityHash32hashIgnoreSeed(str: []const u8, seed: u32) u32 { |
| 374 | return CityHash32.hash(str); |
| 375 | } |
| 376 | |
| 377 | test "cityhash32" { |
| 378 | // Note: SMHasher doesn't provide a 32bit version of the algorithm. |
| 379 | // Note: The implementation was verified against the Google Abseil version. |
| 380 | std.testing.expectEqual(SMHasherTest(CityHash32hashIgnoreSeed, 32), 0x68254F81); |
| 381 | } |
| 382 | |
| 383 | test "cityhash64" { |
| 384 | // Note: This is not compliant with the SMHasher implementation of CityHash64! |
| 385 | // Note: The implementation was verified against the Google Abseil version. |
| 386 | std.testing.expectEqual(SMHasherTest(CityHash64.hashWithSeed, 64), 0x5FABC5C5); |
| 387 | } |