struct CityHash64 [src]

Alias for std.hash.cityhash.CityHash64

Members

Source

pub const CityHash64 = struct { const Self = @This(); // Some primes between 2^63 and 2^64 for various uses. const k0: u64 = 0xc3a5c85c97cb3127; const k1: u64 = 0xb492b66fbe98f273; const k2: u64 = 0x9ae16a3b2f90404f; // Rotate right helper fn rotr64(x: u64, comptime r: u64) u64 { return (x >> r) | (x << (64 - r)); } fn shiftmix(v: u64) u64 { return v ^ (v >> 47); } fn hashLen16(u: u64, v: u64) u64 { return @call(.always_inline, hash128To64, .{ u, v }); } fn hashLen16Mul(low: u64, high: u64, mul: u64) u64 { var a: u64 = (low ^ high) *% mul; a ^= (a >> 47); var b: u64 = (high ^ a) *% mul; b ^= (b >> 47); b *%= mul; return b; } fn hash128To64(low: u64, high: u64) u64 { return @call(.always_inline, hashLen16Mul, .{ low, high, 0x9ddfea08eb382d69 }); } fn hashLen0To16(str: []const u8) u64 { const len: u64 = @as(u64, str.len); if (len >= 8) { const mul: u64 = k2 +% len *% 2; const a: u64 = fetch64(str.ptr, 0) +% k2; const b: u64 = fetch64(str.ptr, str.len - 8); const c: u64 = rotr64(b, 37) *% mul +% a; const d: u64 = (rotr64(a, 25) +% b) *% mul; return hashLen16Mul(c, d, mul); } if (len >= 4) { const mul: u64 = k2 +% len *% 2; const a: u64 = fetch32(str.ptr, 0); return hashLen16Mul(len +% (a << 3), fetch32(str.ptr, str.len - 4), mul); } if (len > 0) { const a: u8 = str[0]; const b: u8 = str[str.len >> 1]; const c: u8 = str[str.len - 1]; const y: u32 = @as(u32, @intCast(a)) +% (@as(u32, @intCast(b)) << 8); const z: u32 = @as(u32, @truncate(str.len)) +% (@as(u32, @intCast(c)) << 2); return shiftmix(@as(u64, @intCast(y)) *% k2 ^ @as(u64, @intCast(z)) *% k0) *% k2; } return k2; } fn hashLen17To32(str: []const u8) u64 { const len: u64 = @as(u64, str.len); const mul: u64 = k2 +% len *% 2; const a: u64 = fetch64(str.ptr, 0) *% k1; const b: u64 = fetch64(str.ptr, 8); const c: u64 = fetch64(str.ptr, str.len - 8) *% mul; const d: u64 = fetch64(str.ptr, str.len - 16) *% k2; return hashLen16Mul(rotr64(a +% b, 43) +% rotr64(c, 30) +% d, a +% rotr64(b +% k2, 18) +% c, mul); } fn hashLen33To64(str: []const u8) u64 { const len: u64 = @as(u64, str.len); const mul: u64 = k2 +% len *% 2; const a: u64 = fetch64(str.ptr, 0) *% k2; const b: u64 = fetch64(str.ptr, 8); const c: u64 = fetch64(str.ptr, str.len - 24); const d: u64 = fetch64(str.ptr, str.len - 32); const e: u64 = fetch64(str.ptr, 16) *% k2; const f: u64 = fetch64(str.ptr, 24) *% 9; const g: u64 = fetch64(str.ptr, str.len - 8); const h: u64 = fetch64(str.ptr, str.len - 16) *% mul; const u: u64 = rotr64(a +% g, 43) +% (rotr64(b, 30) +% c) *% 9; const v: u64 = ((a +% g) ^ d) +% f +% 1; const w: u64 = @byteSwap((u +% v) *% mul) +% h; const x: u64 = rotr64(e +% f, 42) +% c; const y: u64 = (@byteSwap((v +% w) *% mul) +% g) *% mul; const z: u64 = e +% f +% c; const a1: u64 = @byteSwap((x +% z) *% mul +% y) +% b; const b1: u64 = shiftmix((z +% a1) *% mul +% d +% h) *% mul; return b1 +% x; } const WeakPair = struct { first: u64, second: u64, }; fn weakHashLen32WithSeedsHelper(w: u64, x: u64, y: u64, z: u64, a: u64, b: u64) WeakPair { var a1: u64 = a; var b1: u64 = b; a1 +%= w; b1 = rotr64(b1 +% a1 +% z, 21); const c: u64 = a1; a1 +%= x; a1 +%= y; b1 +%= rotr64(a1, 44); return WeakPair{ .first = a1 +% z, .second = b1 +% c }; } fn weakHashLen32WithSeeds(ptr: [*]const u8, a: u64, b: u64) WeakPair { return @call(.always_inline, weakHashLen32WithSeedsHelper, .{ fetch64(ptr, 0), fetch64(ptr, 8), fetch64(ptr, 16), fetch64(ptr, 24), a, b, }); } pub fn hash(str: []const u8) u64 { if (str.len <= 32) { if (str.len <= 16) { return hashLen0To16(str); } else { return hashLen17To32(str); } } else if (str.len <= 64) { return hashLen33To64(str); } var len: u64 = @as(u64, str.len); var x: u64 = fetch64(str.ptr, str.len - 40); var y: u64 = fetch64(str.ptr, str.len - 16) +% fetch64(str.ptr, str.len - 56); var z: u64 = hashLen16(fetch64(str.ptr, str.len - 48) +% len, fetch64(str.ptr, str.len - 24)); var v: WeakPair = weakHashLen32WithSeeds(offsetPtr(str.ptr, str.len - 64), len, z); var w: WeakPair = weakHashLen32WithSeeds(offsetPtr(str.ptr, str.len - 32), y +% k1, x); x = x *% k1 +% fetch64(str.ptr, 0); len = (len - 1) & ~@as(u64, @intCast(63)); var ptr: [*]const u8 = str.ptr; while (true) { x = rotr64(x +% y +% v.first +% fetch64(ptr, 8), 37) *% k1; y = rotr64(y +% v.second +% fetch64(ptr, 48), 42) *% k1; x ^= w.second; y +%= v.first +% fetch64(ptr, 40); z = rotr64(z +% w.first, 33) *% k1; v = weakHashLen32WithSeeds(ptr, v.second *% k1, x +% w.first); w = weakHashLen32WithSeeds(offsetPtr(ptr, 32), z +% w.second, y +% fetch64(ptr, 16)); const t: u64 = z; z = x; x = t; ptr = offsetPtr(ptr, 64); len -= 64; if (len == 0) break; } return hashLen16(hashLen16(v.first, w.first) +% shiftmix(y) *% k1 +% z, hashLen16(v.second, w.second) +% x); } pub fn hashWithSeed(str: []const u8, seed: u64) u64 { return @call(.always_inline, Self.hashWithSeeds, .{ str, k2, seed }); } pub fn hashWithSeeds(str: []const u8, seed0: u64, seed1: u64) u64 { return hashLen16(hash(str) -% seed0, seed1); } }