Debian Codegraph @

utils.go

raw
1 package xxh3
2
3 import (
4 "encoding/binary"
5 "math/bits"
6 "unsafe"
7 )
8
9 // Uint128 is a 128 bit value.
10 // The actual value can be thought of as u.Hi<<64 | u.Lo.
11 type Uint128 struct {
12 Hi, Lo uint64
13 }
14
15 // Bytes returns the uint128 as an array of bytes in canonical form (big-endian encoded).
16 func (u Uint128) Bytes() [16]byte {
17 return [16]byte{
18 byte(u.Hi >> 0x38), byte(u.Hi >> 0x30), byte(u.Hi >> 0x28), byte(u.Hi >> 0x20),
19 byte(u.Hi >> 0x18), byte(u.Hi >> 0x10), byte(u.Hi >> 0x08), byte(u.Hi),
20 byte(u.Lo >> 0x38), byte(u.Lo >> 0x30), byte(u.Lo >> 0x28), byte(u.Lo >> 0x20),
21 byte(u.Lo >> 0x18), byte(u.Lo >> 0x10), byte(u.Lo >> 0x08), byte(u.Lo),
22 }
23 }
24
25 type (
26 ptr = unsafe.Pointer
27 ui = uintptr
28
29 u8 = uint8
30 u32 = uint32
31 u64 = uint64
32 u128 = Uint128
33 )
34
35 type str struct {
36 p ptr
37 l uint
38 }
39
40 func readU8(p ptr, o ui) uint8 {
41 return *(*uint8)(ptr(ui(p) + o))
42 }
43
44 func readU16(p ptr, o ui) uint16 {
45 b := (*[2]byte)(ptr(ui(p) + o))
46 return uint16(b[0]) | uint16(b[1])<<8
47 }
48
49 func readU32(p ptr, o ui) uint32 {
50 b := (*[4]byte)(ptr(ui(p) + o))
51 return binary.LittleEndian.Uint32(b[:])
52 }
53
54 func readU64(p ptr, o ui) uint64 {
55 b := (*[8]byte)(ptr(ui(p) + o))
56 return binary.LittleEndian.Uint64(b[:])
57 }
58
59 func writeU64(p ptr, o ui, v u64) {
60 b := (*[8]byte)(ptr(ui(p) + o))
61 b[0] = byte(v)
62 b[1] = byte(v >> 8)
63 b[2] = byte(v >> 16)
64 b[3] = byte(v >> 24)
65 b[4] = byte(v >> 32)
66 b[5] = byte(v >> 40)
67 b[6] = byte(v >> 48)
68 b[7] = byte(v >> 56)
69 }
70
71 const secretSize = 192
72
73 func initSecret(secret ptr, seed u64) {
74 for i := ui(0); i < secretSize/16; i++ {
75 lo := readU64(key, 16*i) + seed
76 hi := readU64(key, 16*i+8) - seed
77 writeU64(secret, 16*i, lo)
78 writeU64(secret, 16*i+8, hi)
79 }
80 }
81
82 func xxh64AvalancheSmall(x u64) u64 {
83 // x ^= x >> 33 // x must be < 32 bits
84 // x ^= u64(key32_000 ^ key32_004) // caller must do this
85 x *= prime64_2
86 x ^= x >> 29
87 x *= prime64_3
88 x ^= x >> 32
89 return x
90 }
91
92 func xxhAvalancheSmall(x u64) u64 {
93 x ^= x >> 33
94 x *= prime64_2
95 x ^= x >> 29
96 x *= prime64_3
97 x ^= x >> 32
98 return x
99 }
100
101 func xxh64AvalancheFull(x u64) u64 {
102 x ^= x >> 33
103 x *= prime64_2
104 x ^= x >> 29
105 x *= prime64_3
106 x ^= x >> 32
107 return x
108 }
109
110 func xxh3Avalanche(x u64) u64 {
111 x ^= x >> 37
112 x *= 0x165667919e3779f9
113 x ^= x >> 32
114 return x
115 }
116
117 func rrmxmx(h64 u64, len u64) u64 {
118 h64 ^= bits.RotateLeft64(h64, 49) ^ bits.RotateLeft64(h64, 24)
119 h64 *= 0x9fb21c651e98df25
120 h64 ^= (h64 >> 35) + len
121 h64 *= 0x9fb21c651e98df25
122 h64 ^= (h64 >> 28)
123 return h64
124 }
125
126 func mulFold64(x, y u64) u64 {
127 hi, lo := bits.Mul64(x, y)
128 return hi ^ lo
129 }