cubefs
1// Package xxhash implements the 64-bit variant of xxHash (XXH64) as described
2// at http://cyan4973.github.io/xxHash/.
3// THIS IS VENDORED: Go to github.com/cespare/xxhash for original package.
4
5package xxhash6
7import (8"encoding/binary"9"errors"10"math/bits"11)
12
13const (14prime1 uint64 = 1140071478507469479115prime2 uint64 = 1402946736689701972716prime3 uint64 = 160958792939283916117prime4 uint64 = 965002924228782857918prime5 uint64 = 287017745001260026119)
20
21// NOTE(caleb): I'm using both consts and vars of the primes. Using consts where
22// possible in the Go code is worth a small (but measurable) performance boost
23// by avoiding some MOVQs. Vars are needed for the asm and also are useful for
24// convenience in the Go code in a few places where we need to intentionally
25// avoid constant arithmetic (e.g., v1 := prime1 + prime2 fails because the
26// result overflows a uint64).
27var (28prime1v = prime129prime2v = prime230prime3v = prime331prime4v = prime432prime5v = prime533)
34
35// Digest implements hash.Hash64.
36type Digest struct {37v1 uint6438v2 uint6439v3 uint6440v4 uint6441total uint6442mem [32]byte43n int // how much of mem is used44}
45
46// New creates a new Digest that computes the 64-bit xxHash algorithm.
47func New() *Digest {48var d Digest49d.Reset()50return &d51}
52
53// Reset clears the Digest's state so that it can be reused.
54func (d *Digest) Reset() {55d.v1 = prime1v + prime256d.v2 = prime257d.v3 = 058d.v4 = -prime1v59d.total = 060d.n = 061}
62
63// Size always returns 8 bytes.
64func (d *Digest) Size() int { return 8 }65
66// BlockSize always returns 32 bytes.
67func (d *Digest) BlockSize() int { return 32 }68
69// Write adds more data to d. It always returns len(b), nil.
70func (d *Digest) Write(b []byte) (n int, err error) {71n = len(b)72d.total += uint64(n)73
74if d.n+n < 32 {75// This new data doesn't even fill the current block.76copy(d.mem[d.n:], b)77d.n += n78return79}80
81if d.n > 0 {82// Finish off the partial block.83copy(d.mem[d.n:], b)84d.v1 = round(d.v1, u64(d.mem[0:8]))85d.v2 = round(d.v2, u64(d.mem[8:16]))86d.v3 = round(d.v3, u64(d.mem[16:24]))87d.v4 = round(d.v4, u64(d.mem[24:32]))88b = b[32-d.n:]89d.n = 090}91
92if len(b) >= 32 {93// One or more full blocks left.94nw := writeBlocks(d, b)95b = b[nw:]96}97
98// Store any remaining partial block.99copy(d.mem[:], b)100d.n = len(b)101
102return103}
104
105// Sum appends the current hash to b and returns the resulting slice.
106func (d *Digest) Sum(b []byte) []byte {107s := d.Sum64()108return append(109b,110byte(s>>56),111byte(s>>48),112byte(s>>40),113byte(s>>32),114byte(s>>24),115byte(s>>16),116byte(s>>8),117byte(s),118)119}
120
121// Sum64 returns the current hash.
122func (d *Digest) Sum64() uint64 {123var h uint64124
125if d.total >= 32 {126v1, v2, v3, v4 := d.v1, d.v2, d.v3, d.v4127h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)128h = mergeRound(h, v1)129h = mergeRound(h, v2)130h = mergeRound(h, v3)131h = mergeRound(h, v4)132} else {133h = d.v3 + prime5134}135
136h += d.total137
138i, end := 0, d.n139for ; i+8 <= end; i += 8 {140k1 := round(0, u64(d.mem[i:i+8]))141h ^= k1142h = rol27(h)*prime1 + prime4143}144if i+4 <= end {145h ^= uint64(u32(d.mem[i:i+4])) * prime1146h = rol23(h)*prime2 + prime3147i += 4148}149for i < end {150h ^= uint64(d.mem[i]) * prime5151h = rol11(h) * prime1152i++153}154
155h ^= h >> 33156h *= prime2157h ^= h >> 29158h *= prime3159h ^= h >> 32160
161return h162}
163
164const (165magic = "xxh\x06"166marshaledSize = len(magic) + 8*5 + 32167)
168
169// MarshalBinary implements the encoding.BinaryMarshaler interface.
170func (d *Digest) MarshalBinary() ([]byte, error) {171b := make([]byte, 0, marshaledSize)172b = append(b, magic...)173b = appendUint64(b, d.v1)174b = appendUint64(b, d.v2)175b = appendUint64(b, d.v3)176b = appendUint64(b, d.v4)177b = appendUint64(b, d.total)178b = append(b, d.mem[:d.n]...)179b = b[:len(b)+len(d.mem)-d.n]180return b, nil181}
182
183// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
184func (d *Digest) UnmarshalBinary(b []byte) error {185if len(b) < len(magic) || string(b[:len(magic)]) != magic {186return errors.New("xxhash: invalid hash state identifier")187}188if len(b) != marshaledSize {189return errors.New("xxhash: invalid hash state size")190}191b = b[len(magic):]192b, d.v1 = consumeUint64(b)193b, d.v2 = consumeUint64(b)194b, d.v3 = consumeUint64(b)195b, d.v4 = consumeUint64(b)196b, d.total = consumeUint64(b)197copy(d.mem[:], b)198d.n = int(d.total % uint64(len(d.mem)))199return nil200}
201
202func appendUint64(b []byte, x uint64) []byte {203var a [8]byte204binary.LittleEndian.PutUint64(a[:], x)205return append(b, a[:]...)206}
207
208func consumeUint64(b []byte) ([]byte, uint64) {209x := u64(b)210return b[8:], x211}
212
213func u64(b []byte) uint64 { return binary.LittleEndian.Uint64(b) }214func u32(b []byte) uint32 { return binary.LittleEndian.Uint32(b) }215
216func round(acc, input uint64) uint64 {217acc += input * prime2218acc = rol31(acc)219acc *= prime1220return acc221}
222
223func mergeRound(acc, val uint64) uint64 {224val = round(0, val)225acc ^= val226acc = acc*prime1 + prime4227return acc228}
229
230func rol1(x uint64) uint64 { return bits.RotateLeft64(x, 1) }231func rol7(x uint64) uint64 { return bits.RotateLeft64(x, 7) }232func rol11(x uint64) uint64 { return bits.RotateLeft64(x, 11) }233func rol12(x uint64) uint64 { return bits.RotateLeft64(x, 12) }234func rol18(x uint64) uint64 { return bits.RotateLeft64(x, 18) }235func rol23(x uint64) uint64 { return bits.RotateLeft64(x, 23) }236func rol27(x uint64) uint64 { return bits.RotateLeft64(x, 27) }237func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) }238