forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/cespare/xxhash/v2/xxhash.go

384 lines
5.2 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Package xxhash implements the 64-bit variant of xxHash (XXH64) as described
2024-02-18 10:42:21 +00:00
// at http://cyan4973.github.io/xxHash/.
2024-02-18 10:42:21 +00:00
package xxhash
import (
"encoding/binary"
"errors"
"math/bits"
)
const (
prime1 uint64 = 11400714785074694791
2024-02-18 10:42:21 +00:00
prime2 uint64 = 14029467366897019727
2024-02-18 10:42:21 +00:00
prime3 uint64 = 1609587929392839161
2024-02-18 10:42:21 +00:00
prime4 uint64 = 9650029242287828579
2024-02-18 10:42:21 +00:00
prime5 uint64 = 2870177450012600261
)
// Store the primes in an array as well.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The consts are used when possible in Go code to avoid MOVs but we need a
2024-02-18 10:42:21 +00:00
// contiguous array of the assembly code.
2024-02-18 10:42:21 +00:00
var primes = [...]uint64{prime1, prime2, prime3, prime4, prime5}
// Digest implements hash.Hash64.
2024-02-18 10:42:21 +00:00
type Digest struct {
v1 uint64
v2 uint64
v3 uint64
v4 uint64
2024-02-18 10:42:21 +00:00
total uint64
mem [32]byte
n int // how much of mem is used
2024-02-18 10:42:21 +00:00
}
// New creates a new Digest that computes the 64-bit xxHash algorithm.
2024-02-18 10:42:21 +00:00
func New() *Digest {
2024-02-18 10:42:21 +00:00
var d Digest
2024-02-18 10:42:21 +00:00
d.Reset()
2024-02-18 10:42:21 +00:00
return &d
2024-02-18 10:42:21 +00:00
}
// Reset clears the Digest's state so that it can be reused.
2024-02-18 10:42:21 +00:00
func (d *Digest) Reset() {
2024-02-18 10:42:21 +00:00
d.v1 = primes[0] + prime2
2024-02-18 10:42:21 +00:00
d.v2 = prime2
2024-02-18 10:42:21 +00:00
d.v3 = 0
2024-02-18 10:42:21 +00:00
d.v4 = -primes[0]
2024-02-18 10:42:21 +00:00
d.total = 0
2024-02-18 10:42:21 +00:00
d.n = 0
2024-02-18 10:42:21 +00:00
}
// Size always returns 8 bytes.
2024-02-18 10:42:21 +00:00
func (d *Digest) Size() int { return 8 }
// BlockSize always returns 32 bytes.
2024-02-18 10:42:21 +00:00
func (d *Digest) BlockSize() int { return 32 }
// Write adds more data to d. It always returns len(b), nil.
2024-02-18 10:42:21 +00:00
func (d *Digest) Write(b []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
n = len(b)
2024-02-18 10:42:21 +00:00
d.total += uint64(n)
memleft := d.mem[d.n&(len(d.mem)-1):]
if d.n+n < 32 {
2024-02-18 10:42:21 +00:00
// This new data doesn't even fill the current block.
2024-02-18 10:42:21 +00:00
copy(memleft, b)
2024-02-18 10:42:21 +00:00
d.n += n
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
if d.n > 0 {
2024-02-18 10:42:21 +00:00
// Finish off the partial block.
2024-02-18 10:42:21 +00:00
c := copy(memleft, b)
2024-02-18 10:42:21 +00:00
d.v1 = round(d.v1, u64(d.mem[0:8]))
2024-02-18 10:42:21 +00:00
d.v2 = round(d.v2, u64(d.mem[8:16]))
2024-02-18 10:42:21 +00:00
d.v3 = round(d.v3, u64(d.mem[16:24]))
2024-02-18 10:42:21 +00:00
d.v4 = round(d.v4, u64(d.mem[24:32]))
2024-02-18 10:42:21 +00:00
b = b[c:]
2024-02-18 10:42:21 +00:00
d.n = 0
2024-02-18 10:42:21 +00:00
}
if len(b) >= 32 {
2024-02-18 10:42:21 +00:00
// One or more full blocks left.
2024-02-18 10:42:21 +00:00
nw := writeBlocks(d, b)
2024-02-18 10:42:21 +00:00
b = b[nw:]
2024-02-18 10:42:21 +00:00
}
// Store any remaining partial block.
2024-02-18 10:42:21 +00:00
copy(d.mem[:], b)
2024-02-18 10:42:21 +00:00
d.n = len(b)
return
2024-02-18 10:42:21 +00:00
}
// Sum appends the current hash to b and returns the resulting slice.
2024-02-18 10:42:21 +00:00
func (d *Digest) Sum(b []byte) []byte {
2024-02-18 10:42:21 +00:00
s := d.Sum64()
2024-02-18 10:42:21 +00:00
return append(
2024-02-18 10:42:21 +00:00
b,
2024-02-18 10:42:21 +00:00
byte(s>>56),
2024-02-18 10:42:21 +00:00
byte(s>>48),
2024-02-18 10:42:21 +00:00
byte(s>>40),
2024-02-18 10:42:21 +00:00
byte(s>>32),
2024-02-18 10:42:21 +00:00
byte(s>>24),
2024-02-18 10:42:21 +00:00
byte(s>>16),
2024-02-18 10:42:21 +00:00
byte(s>>8),
2024-02-18 10:42:21 +00:00
byte(s),
)
2024-02-18 10:42:21 +00:00
}
// Sum64 returns the current hash.
2024-02-18 10:42:21 +00:00
func (d *Digest) Sum64() uint64 {
2024-02-18 10:42:21 +00:00
var h uint64
if d.total >= 32 {
2024-02-18 10:42:21 +00:00
v1, v2, v3, v4 := d.v1, d.v2, d.v3, d.v4
2024-02-18 10:42:21 +00:00
h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)
2024-02-18 10:42:21 +00:00
h = mergeRound(h, v1)
2024-02-18 10:42:21 +00:00
h = mergeRound(h, v2)
2024-02-18 10:42:21 +00:00
h = mergeRound(h, v3)
2024-02-18 10:42:21 +00:00
h = mergeRound(h, v4)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
h = d.v3 + prime5
2024-02-18 10:42:21 +00:00
}
h += d.total
b := d.mem[:d.n&(len(d.mem)-1)]
2024-02-18 10:42:21 +00:00
for ; len(b) >= 8; b = b[8:] {
2024-02-18 10:42:21 +00:00
k1 := round(0, u64(b[:8]))
2024-02-18 10:42:21 +00:00
h ^= k1
2024-02-18 10:42:21 +00:00
h = rol27(h)*prime1 + prime4
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(b) >= 4 {
2024-02-18 10:42:21 +00:00
h ^= uint64(u32(b[:4])) * prime1
2024-02-18 10:42:21 +00:00
h = rol23(h)*prime2 + prime3
2024-02-18 10:42:21 +00:00
b = b[4:]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for ; len(b) > 0; b = b[1:] {
2024-02-18 10:42:21 +00:00
h ^= uint64(b[0]) * prime5
2024-02-18 10:42:21 +00:00
h = rol11(h) * prime1
2024-02-18 10:42:21 +00:00
}
h ^= h >> 33
2024-02-18 10:42:21 +00:00
h *= prime2
2024-02-18 10:42:21 +00:00
h ^= h >> 29
2024-02-18 10:42:21 +00:00
h *= prime3
2024-02-18 10:42:21 +00:00
h ^= h >> 32
return h
2024-02-18 10:42:21 +00:00
}
const (
magic = "xxh\x06"
2024-02-18 10:42:21 +00:00
marshaledSize = len(magic) + 8*5 + 32
)
// MarshalBinary implements the encoding.BinaryMarshaler interface.
2024-02-18 10:42:21 +00:00
func (d *Digest) MarshalBinary() ([]byte, error) {
2024-02-18 10:42:21 +00:00
b := make([]byte, 0, marshaledSize)
2024-02-18 10:42:21 +00:00
b = append(b, magic...)
2024-02-18 10:42:21 +00:00
b = appendUint64(b, d.v1)
2024-02-18 10:42:21 +00:00
b = appendUint64(b, d.v2)
2024-02-18 10:42:21 +00:00
b = appendUint64(b, d.v3)
2024-02-18 10:42:21 +00:00
b = appendUint64(b, d.v4)
2024-02-18 10:42:21 +00:00
b = appendUint64(b, d.total)
2024-02-18 10:42:21 +00:00
b = append(b, d.mem[:d.n]...)
2024-02-18 10:42:21 +00:00
b = b[:len(b)+len(d.mem)-d.n]
2024-02-18 10:42:21 +00:00
return b, nil
2024-02-18 10:42:21 +00:00
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
2024-02-18 10:42:21 +00:00
func (d *Digest) UnmarshalBinary(b []byte) error {
2024-02-18 10:42:21 +00:00
if len(b) < len(magic) || string(b[:len(magic)]) != magic {
2024-02-18 10:42:21 +00:00
return errors.New("xxhash: invalid hash state identifier")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(b) != marshaledSize {
2024-02-18 10:42:21 +00:00
return errors.New("xxhash: invalid hash state size")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
b = b[len(magic):]
2024-02-18 10:42:21 +00:00
b, d.v1 = consumeUint64(b)
2024-02-18 10:42:21 +00:00
b, d.v2 = consumeUint64(b)
2024-02-18 10:42:21 +00:00
b, d.v3 = consumeUint64(b)
2024-02-18 10:42:21 +00:00
b, d.v4 = consumeUint64(b)
2024-02-18 10:42:21 +00:00
b, d.total = consumeUint64(b)
2024-02-18 10:42:21 +00:00
copy(d.mem[:], b)
2024-02-18 10:42:21 +00:00
d.n = int(d.total % uint64(len(d.mem)))
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func appendUint64(b []byte, x uint64) []byte {
2024-02-18 10:42:21 +00:00
var a [8]byte
2024-02-18 10:42:21 +00:00
binary.LittleEndian.PutUint64(a[:], x)
2024-02-18 10:42:21 +00:00
return append(b, a[:]...)
2024-02-18 10:42:21 +00:00
}
func consumeUint64(b []byte) ([]byte, uint64) {
2024-02-18 10:42:21 +00:00
x := u64(b)
2024-02-18 10:42:21 +00:00
return b[8:], x
2024-02-18 10:42:21 +00:00
}
func u64(b []byte) uint64 { return binary.LittleEndian.Uint64(b) }
2024-02-18 10:42:21 +00:00
func u32(b []byte) uint32 { return binary.LittleEndian.Uint32(b) }
func round(acc, input uint64) uint64 {
2024-02-18 10:42:21 +00:00
acc += input * prime2
2024-02-18 10:42:21 +00:00
acc = rol31(acc)
2024-02-18 10:42:21 +00:00
acc *= prime1
2024-02-18 10:42:21 +00:00
return acc
2024-02-18 10:42:21 +00:00
}
func mergeRound(acc, val uint64) uint64 {
2024-02-18 10:42:21 +00:00
val = round(0, val)
2024-02-18 10:42:21 +00:00
acc ^= val
2024-02-18 10:42:21 +00:00
acc = acc*prime1 + prime4
2024-02-18 10:42:21 +00:00
return acc
2024-02-18 10:42:21 +00:00
}
func rol1(x uint64) uint64 { return bits.RotateLeft64(x, 1) }
func rol7(x uint64) uint64 { return bits.RotateLeft64(x, 7) }
2024-02-18 10:42:21 +00:00
func rol11(x uint64) uint64 { return bits.RotateLeft64(x, 11) }
2024-02-18 10:42:21 +00:00
func rol12(x uint64) uint64 { return bits.RotateLeft64(x, 12) }
2024-02-18 10:42:21 +00:00
func rol18(x uint64) uint64 { return bits.RotateLeft64(x, 18) }
2024-02-18 10:42:21 +00:00
func rol23(x uint64) uint64 { return bits.RotateLeft64(x, 23) }
2024-02-18 10:42:21 +00:00
func rol27(x uint64) uint64 { return bits.RotateLeft64(x, 27) }
2024-02-18 10:42:21 +00:00
func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) }