This commit is contained in:
gingerBill
2022-07-16 16:12:24 +01:00
+18 -12
View File
@@ -172,14 +172,14 @@ murmur32 :: proc(data: []byte, seed := u32(0)) -> u32 {
return h1 return h1
} }
// See https://github.com/aappleby/smhasher/blob/master/src/MurmurHash2.cpp#L96
@(optimization_mode="speed") @(optimization_mode="speed")
murmur64 :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 { murmur64a :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 {
when size_of(int) == 8 {
m :: 0xc6a4a7935bd1e995 m :: 0xc6a4a7935bd1e995
r :: 47 r :: 47
h: u64 = seed ~ (u64(len(data)) * m) h: u64 = seed ~ (u64(len(data)) * m)
data64 := mem.slice_ptr(cast(^u64)raw_data(data), len(data)/size_of(u64)) data64 := mem.slice_data_cast([]u64, data)
for _, i in data64 { for _, i in data64 {
k := data64[i] k := data64[i]
@@ -192,15 +192,17 @@ murmur64 :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 {
h *= m h *= m
} }
offset := len(data64) * size_of(u64)
switch len(data)&7 { switch len(data)&7 {
case 7: h ~= u64(data[6]) << 48; fallthrough case 7: h ~= u64(data[offset + 6]) << 48; fallthrough
case 6: h ~= u64(data[5]) << 40; fallthrough case 6: h ~= u64(data[offset + 5]) << 40; fallthrough
case 5: h ~= u64(data[4]) << 32; fallthrough case 5: h ~= u64(data[offset + 4]) << 32; fallthrough
case 4: h ~= u64(data[3]) << 24; fallthrough case 4: h ~= u64(data[offset + 3]) << 24; fallthrough
case 3: h ~= u64(data[2]) << 16; fallthrough case 3: h ~= u64(data[offset + 2]) << 16; fallthrough
case 2: h ~= u64(data[1]) << 8; fallthrough case 2: h ~= u64(data[offset + 1]) << 8; fallthrough
case 1: case 1:
h ~= u64(data[0]) h ~= u64(data[offset + 0])
h *= m h *= m
} }
@@ -209,12 +211,17 @@ murmur64 :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 {
h ~= h>>r h ~= h>>r
return h return h
} else { }
// See https://github.com/aappleby/smhasher/blob/master/src/MurmurHash2.cpp#L140
@(optimization_mode="speed")
murmur64b :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 {
m :: 0x5bd1e995 m :: 0x5bd1e995
r :: 24 r :: 24
h1 := u32(seed) ~ u32(len(data)) h1 := u32(seed) ~ u32(len(data))
h2 := u32(seed) >> 32 h2 := u32(seed) >> 32
data32 := mem.slice_ptr(cast(^u32)raw_data(data), len(data)/size_of(u32)) data32 := mem.slice_ptr(cast(^u32)raw_data(data), len(data)/size_of(u32))
len := len(data) len := len(data)
i := 0 i := 0
@@ -274,7 +281,6 @@ murmur64 :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 {
return u64(h1)<<32 | u64(h2) return u64(h1)<<32 | u64(h2)
} }
}
@(optimization_mode="speed") @(optimization_mode="speed")
sdbm :: proc(data: []byte, seed := u32(0)) -> u32 { sdbm :: proc(data: []byte, seed := u32(0)) -> u32 {