summaryrefslogtreecommitdiff
path: root/lib/std/hashfuncs.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/hashfuncs.myr')
-rw-r--r--lib/std/hashfuncs.myr60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/std/hashfuncs.myr b/lib/std/hashfuncs.myr
index 3046377..1fb949c 100644
--- a/lib/std/hashfuncs.myr
+++ b/lib/std/hashfuncs.myr
@@ -1,6 +1,7 @@
use "alloc"
use "chartype"
use "die"
+use "getint"
use "sleq"
use "slpush"
use "types"
@@ -20,6 +21,7 @@ pkg std =
generic inteq : (a : @a::(integral,numeric), b : @a::(integral,numeric) -> bool)
const murmurhash2 : (data : byte[:], seed : uint32 -> uint32)
+ const siphash24 : (data : byte[:], seed : byte[16] -> uint64)
generic slhash : (sl : @a[:] -> uint32)
;;
@@ -141,3 +143,61 @@ const murmurhash2 = {data, seed
-> h
}
+
+const rotl = {x, n -> uint64; -> (x << n) | (x >> (64 - n))}
+
+const sipround = {v0, v1, v2, v3 -> (uint64, uint64, uint64, uint64)
+ v0 += v1
+ v1 = rotl(v1, 13)
+ v1 ^= v0
+ v0 = rotl(v0, 32)
+ v2 += v3
+ v3 = rotl(v3, 16)
+ v3 ^= v2
+
+ v2 += v1
+ v1 = rotl(v1, 17)
+ v1 ^= v2
+ v2 = rotl(v2, 32)
+ v0 += v3
+ v3 = rotl(v3, 21)
+ v3 ^= v0
+
+ -> (v0, v1, v2, v3)
+}
+
+const siphash24 = {data, seed
+ var k0, k1, m, v0, v1, v2, v3, w
+ var tail : byte[8] = [0, 0, 0, 0, 0, 0, 0, 0]
+
+ k0 = std.getle64(seed[0:8])
+ k1 = std.getle64(seed[8:16])
+ v0 = k0 ^ 0x736f6d6570736575
+ v1 = k1 ^ 0x646f72616e646f6d
+ v2 = k0 ^ 0x6c7967656e657261
+ v3 = k1 ^ 0x7465646279746573
+ w = (data.len + 8) / 8 - 1
+ for var i = 0; i < w; i++
+ m = std.getle64(data[8 * i:8 * (i + 1)])
+ v3 ^= m
+ (v0, v1, v2, v3) = sipround(v0, v1, v2, v3)
+ (v0, v1, v2, v3) = sipround(v0, v1, v2, v3)
+ v0 ^= m
+ ;;
+ for var i = 0; i < data.len % 8; i++
+ tail[i] = data[8 * w + i]
+ ;;
+ tail[7] = (data.len % 256 : byte)
+ m = std.getle64(tail[:])
+ v3 ^= m
+ (v0, v1, v2, v3) = sipround(v0, v1, v2, v3)
+ (v0, v1, v2, v3) = sipround(v0, v1, v2, v3)
+ v0 ^= m
+
+ v2 ^= 0xff
+ (v0, v1, v2, v3) = sipround(v0, v1, v2, v3)
+ (v0, v1, v2, v3) = sipround(v0, v1, v2, v3)
+ (v0, v1, v2, v3) = sipround(v0, v1, v2, v3)
+ (v0, v1, v2, v3) = sipround(v0, v1, v2, v3)
+ -> v0 ^ v1 ^ v2 ^ v3
+}