1
2
3
4
5 package maps
6
7 import (
8 "internal/byteorder"
9 "internal/cpu"
10 "internal/goarch"
11 "unsafe"
12 )
13
14
15
16
17 var UseAeshash bool
18
19 const hashRandomBytes = goarch.PtrSize / 4 * 64
20
21
22 var aeskeysched [hashRandomBytes]byte
23
24
25 var hashkey [4]uintptr
26
27 func AlgInit() {
28
29
30
31 for i := range hashkey {
32 hashkey[i] = uintptr(bootstrapRand())
33 }
34
35
36 if (goarch.GOARCH == "386" || goarch.GOARCH == "amd64") &&
37 cpu.X86.HasAES &&
38 cpu.X86.HasSSSE3 &&
39 cpu.X86.HasSSE41 {
40
41
42
43
44
45 if !checkMasksAndShiftsAlignment() {
46 fatal("maps: global variables for AES hashing are not properly aligned!")
47 }
48 initAlgAES()
49
50 if memHashUsesVAES {
51
52
53
54
55 UseAeshash = cpu.X86.HasAVX
56 }
57 return
58 }
59 if goarch.GOARCH == "arm64" && cpu.ARM64.HasAES {
60 initAlgAES()
61 return
62 }
63 }
64
65 func initAlgAES() {
66 UseAeshash = true
67
68 key := (*[hashRandomBytes / 8]uint64)(unsafe.Pointer(&aeskeysched))
69 for i := range key {
70 key[i] = bootstrapRand()
71 }
72 }
73
74 func strHashFallback(a unsafe.Pointer, h uintptr) uintptr {
75 type stringStruct struct {
76 str unsafe.Pointer
77 len int
78 }
79 x := (*stringStruct)(a)
80 return memHashFallback(x.str, h, uintptr(x.len))
81 }
82
83
84 func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
85 return unsafe.Pointer(uintptr(p) + x)
86 }
87
88
89 func readUnaligned32(p unsafe.Pointer) uint32 {
90 q := (*[4]byte)(p)
91 if goarch.BigEndian {
92 return byteorder.BEUint32(q[:])
93 }
94 return byteorder.LEUint32(q[:])
95 }
96
97 func readUnaligned64(p unsafe.Pointer) uint64 {
98 q := (*[8]byte)(p)
99 if goarch.BigEndian {
100 return byteorder.BEUint64(q[:])
101 }
102 return byteorder.LEUint64(q[:])
103 }
104
View as plain text