diff options
| author | 2026-01-29 17:41:23 +0100 | |
|---|---|---|
| committer | 2026-01-29 17:43:52 +0100 | |
| commit | 17c9aee0e781026353ead4ac749a3ae89c83d007 (patch) | |
| tree | fc681ebc99fdcd21339265a7e0fcfd4fe7a17d67 /internal | |
| parent | README: Various updates (diff) | |
| signature | No signature | |
packed: Write packs with deltas
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/murmurhash2/murmurhash2.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/internal/murmurhash2/murmurhash2.go b/internal/murmurhash2/murmurhash2.go new file mode 100644 index 00000000..c728f932 --- /dev/null +++ b/internal/murmurhash2/murmurhash2.go @@ -0,0 +1,42 @@ +// Package murmurhash2 provides a non-cryptographic hash. +package murmurhash2 + +import "encoding/binary" + +// Sum32 computes the MurmurHash2 value for key with the provided seed. +func Sum32(key []byte, seed uint32) uint32 { + const ( + m uint32 = 0x5bd1e995 + r uint32 = 24 + ) + + h := seed ^ uint32(len(key)) + i := 0 + for len(key)-i >= 4 { + k := binary.LittleEndian.Uint32(key[i:]) + k *= m + k ^= k >> r + k *= m + + h *= m + h ^= k + i += 4 + } + + switch len(key) - i { + case 3: + h ^= uint32(key[i+2]) << 16 + fallthrough + case 2: + h ^= uint32(key[i+1]) << 8 + fallthrough + case 1: + h ^= uint32(key[i]) + h *= m + } + + h ^= h >> 13 + h *= m + h ^= h >> 15 + return h +} |
