cubefs
1// Package xxhash implements the 64-bit variant of xxHash (XXH64) as described
2// at http://cyan4973.github.io/xxHash/.
3package xxhash4
5import (6"encoding/binary"7"errors"8"math/bits"9)
10
11const (12prime1 uint64 = 1140071478507469479113prime2 uint64 = 1402946736689701972714prime3 uint64 = 160958792939283916115prime4 uint64 = 965002924228782857916prime5 uint64 = 287017745001260026117)
18
19// NOTE(caleb): I'm using both consts and vars of the primes. Using consts where
20// possible in the Go code is worth a small (but measurable) performance boost
21// by avoiding some MOVQs. Vars are needed for the asm and also are useful for
22// convenience in the Go code in a few places where we need to intentionally
23// avoid constant arithmetic (e.g., v1 := prime1 + prime2 fails because the
24// result overflows a uint64).
25var (26prime1v = prime127prime2v = prime228prime3v = prime329prime4v = prime430prime5v = prime531)
32
33// Digest implements hash.Hash64.
34type Digest struct {35v1 uint6436v2 uint6437v3 uint6438v4 uint6439total uint6440mem [32]byte41n int // how much of mem is used42}
43
44// New creates a new Digest that computes the 64-bit xxHash algorithm.
45func New() *Digest {46var d Digest47d.Reset()48return &d49}
50
51// Reset clears the Digest's state so that it can be reused.
52func (d *Digest) Reset() {53d.v1 = prime1v + prime254d.v2 = prime255d.v3 = 056d.v4 = -prime1v57d.total = 058d.n = 059}
60
61// Size always returns 8 bytes.
62func (d *Digest) Size() int { return 8 }63
64// BlockSize always returns 32 bytes.
65func (d *Digest) BlockSize() int { return 32 }66
67// Write adds more data to d. It always returns len(b), nil.
68func (d *Digest) Write(b []byte) (n int, err error) {69n = len(b)70d.total += uint64(n)71
72if d.n+n < 32 {73// This new data doesn't even fill the current block.74copy(d.mem[d.n:], b)75d.n += n76return77}78
79if d.n > 0 {80// Finish off the partial block.81copy(d.mem[d.n:], b)82d.v1 = round(d.v1, u64(d.mem[0:8]))83d.v2 = round(d.v2, u64(d.mem[8:16]))84d.v3 = round(d.v3, u64(d.mem[16:24]))85d.v4 = round(d.v4, u64(d.mem[24:32]))86b = b[32-d.n:]87d.n = 088}89
90if len(b) >= 32 {91// One or more full blocks left.92nw := writeBlocks(d, b)93b = b[nw:]94}95
96// Store any remaining partial block.97copy(d.mem[:], b)98d.n = len(b)99
100return101}
102
103// Sum appends the current hash to b and returns the resulting slice.
104func (d *Digest) Sum(b []byte) []byte {105s := d.Sum64()106return append(107b,108byte(s>>56),109byte(s>>48),110byte(s>>40),111byte(s>>32),112byte(s>>24),113byte(s>>16),114byte(s>>8),115byte(s),116)117}
118
119// Sum64 returns the current hash.
120func (d *Digest) Sum64() uint64 {121var h uint64122
123if d.total >= 32 {124v1, v2, v3, v4 := d.v1, d.v2, d.v3, d.v4125h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4)126h = mergeRound(h, v1)127h = mergeRound(h, v2)128h = mergeRound(h, v3)129h = mergeRound(h, v4)130} else {131h = d.v3 + prime5132}133
134h += d.total135
136i, end := 0, d.n137for ; i+8 <= end; i += 8 {138k1 := round(0, u64(d.mem[i:i+8]))139h ^= k1140h = rol27(h)*prime1 + prime4141}142if i+4 <= end {143h ^= uint64(u32(d.mem[i:i+4])) * prime1144h = rol23(h)*prime2 + prime3145i += 4146}147for i < end {148h ^= uint64(d.mem[i]) * prime5149h = rol11(h) * prime1150i++151}152
153h ^= h >> 33154h *= prime2155h ^= h >> 29156h *= prime3157h ^= h >> 32158
159return h160}
161
162const (163magic = "xxh\x06"164marshaledSize = len(magic) + 8*5 + 32165)
166
167// MarshalBinary implements the encoding.BinaryMarshaler interface.
168func (d *Digest) MarshalBinary() ([]byte, error) {169b := make([]byte, 0, marshaledSize)170b = append(b, magic...)171b = appendUint64(b, d.v1)172b = appendUint64(b, d.v2)173b = appendUint64(b, d.v3)174b = appendUint64(b, d.v4)175b = appendUint64(b, d.total)176b = append(b, d.mem[:d.n]...)177b = b[:len(b)+len(d.mem)-d.n]178return b, nil179}
180
181// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
182func (d *Digest) UnmarshalBinary(b []byte) error {183if len(b) < len(magic) || string(b[:len(magic)]) != magic {184return errors.New("xxhash: invalid hash state identifier")185}186if len(b) != marshaledSize {187return errors.New("xxhash: invalid hash state size")188}189b = b[len(magic):]190b, d.v1 = consumeUint64(b)191b, d.v2 = consumeUint64(b)192b, d.v3 = consumeUint64(b)193b, d.v4 = consumeUint64(b)194b, d.total = consumeUint64(b)195copy(d.mem[:], b)196d.n = int(d.total % uint64(len(d.mem)))197return nil198}
199
200func appendUint64(b []byte, x uint64) []byte {201var a [8]byte202binary.LittleEndian.PutUint64(a[:], x)203return append(b, a[:]...)204}
205
206func consumeUint64(b []byte) ([]byte, uint64) {207x := u64(b)208return b[8:], x209}
210
211func u64(b []byte) uint64 { return binary.LittleEndian.Uint64(b) }212func u32(b []byte) uint32 { return binary.LittleEndian.Uint32(b) }213
214func round(acc, input uint64) uint64 {215acc += input * prime2216acc = rol31(acc)217acc *= prime1218return acc219}
220
221func mergeRound(acc, val uint64) uint64 {222val = round(0, val)223acc ^= val224acc = acc*prime1 + prime4225return acc226}
227
228func rol1(x uint64) uint64 { return bits.RotateLeft64(x, 1) }229func rol7(x uint64) uint64 { return bits.RotateLeft64(x, 7) }230func rol11(x uint64) uint64 { return bits.RotateLeft64(x, 11) }231func rol12(x uint64) uint64 { return bits.RotateLeft64(x, 12) }232func rol18(x uint64) uint64 { return bits.RotateLeft64(x, 18) }233func rol23(x uint64) uint64 { return bits.RotateLeft64(x, 23) }234func rol27(x uint64) uint64 { return bits.RotateLeft64(x, 27) }235func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) }236