Debian Codegraph @

avo/avx512.go

raw
1 package main
2
3 import (
4 . "github.com/mmcloughlin/avo/build"
5 . "github.com/mmcloughlin/avo/operand"
6 . "github.com/mmcloughlin/avo/reg"
7 )
8
9 // AVX512 can be used for very long blocks.
10 func AVX512() {
11 // Lay out the prime constant in memory, copy it so no unpack is needed.
12 primeData := GLOBL("prime_avx512", RODATA|NOPTR)
13 for i := 0; i < 64; i += 8 {
14 DATA(i, U64(2654435761))
15 }
16
17 {
18 TEXT("accumAVX512", NOSPLIT, "func(acc *[8]uint64, data, key *byte, len uint64)")
19
20 acc := Mem{Base: Load(Param("acc"), GP64())}
21 data := Mem{Base: Load(Param("data"), GP64())}
22 key := Mem{Base: Load(Param("key"), GP64())}
23 plen := Load(Param("len"), GP64())
24 prime := ZMM()
25 // a[0] contains merged values
26 // a[1-3] is a temporary accumulators used for the accum_large loop.
27 a := [4]VecVirtual{ZMM(), ZMM(), ZMM(), ZMM()}
28
29 Label("load")
30 {
31 VMOVDQU64(acc.Offset(0x00), a[0])
32 VMOVDQU64(primeData, prime)
33 }
34 // Load key at 8 byte offsets in the order we use it.
35 var keyReg [17]VecVirtual
36 for i := range keyReg {
37 keyReg[i] = ZMM()
38 VMOVDQU64(key.Offset(i*8), keyReg[i])
39 }
40 advance := func(n int) {
41 ADDQ(U32(n*64), data.Base)
42 SUBQ(U32(n*64), plen)
43 }
44
45 // Key at offset 121.
46 key121 := ZMM()
47 VMOVDQU64(key.Offset(121), key121)
48
49 Label("accum_large")
50 {
51 CMPQ(plen, U32(1024))
52 JLE(LabelRef("accum"))
53
54 for i := 0; i < 16; i += 2 {
55 avx512accumSplitTwo(data, a, 64*i, keyReg[i], keyReg[i+1], true, i == 0)
56 }
57 // Sum into a[0]
58 VPADDQ(a[0], a[1], a[0]) // a0 = a0 + a1
59 VPADDQ(a[2], a[3], a[2]) // a2 = a2 + a3
60 VPADDQ(a[0], a[2], a[0]) // a0 = a0 + a2
61 advance(16)
62 avx512scramble(prime, keyReg[16], a[0])
63
64 JMP(LabelRef("accum_large"))
65 }
66
67 Label("accum")
68 {
69 // Unroll loop so we can use already loaded keys.
70 for i := 0; i < 1024/64; i++ {
71 CMPQ(plen, Imm(64))
72 JLE(LabelRef("finalize"))
73
74 avx512accum(data, a[0], 0, keyReg[i], false)
75 advance(1)
76 }
77 }
78
79 Label("finalize")
80 {
81 CMPQ(plen, Imm(0))
82 JE(LabelRef("return"))
83
84 SUBQ(Imm(64), data.Base)
85 ADDQ(plen, data.Base)
86
87 avx512accum(data, a[0], 0, key121, false)
88 }
89
90 Label("return")
91 {
92 VMOVDQU64(a[0], acc.Offset(0x00))
93 VZEROUPPER()
94 RET()
95 }
96 }
97
98 {
99 TEXT("accumBlockAVX512", NOSPLIT, "func(acc *[8]uint64, data, key *byte)")
100
101 acc := Mem{Base: Load(Param("acc"), GP64())}
102 data := Mem{Base: Load(Param("data"), GP64())}
103 key := Mem{Base: Load(Param("key"), GP64())}
104 prime := ZMM()
105 // a[0] contains merged values
106 // a[1-3] is a temporary accumulators used for the accum_large loop.
107 a := [4]VecVirtual{ZMM(), ZMM(), ZMM(), ZMM()}
108
109 Label("load")
110 {
111 VMOVDQU64(acc.Offset(0x00), a[0])
112 VMOVDQU64(primeData, prime)
113 }
114
115 // Accumulate block of 1024 bytes.
116 {
117 for i := 0; i < 16; i += 2 {
118 // Load keys just in time.
119 key1, key2 := ZMM(), ZMM()
120 VMOVDQU64(key.Offset(i*8), key1)
121 VMOVDQU64(key.Offset(i*8+8), key2)
122
123 avx512accumSplitTwo(data, a, 64*i, key1, key2, false, i == 0)
124 }
125 // Sum into a[0]
126 key128 := ZMM()
127 VMOVDQU64(key.Offset(16*8), key128)
128 VPADDQ(a[0], a[1], a[0]) // a0 = a0 + a1
129 VPADDQ(a[2], a[3], a[2]) // a2 = a2 + a3
130 VPADDQ(a[0], a[2], a[0]) // a0 = a0 + a2
131 avx512scramble(prime, key128, a[0])
132 }
133
134 {
135 VMOVDQU64(a[0], acc.Offset(0x00))
136 VZEROUPPER()
137 RET()
138 }
139 }
140
141 Generate()
142 }
143
144 func avx512scramble(prime, key, a VecVirtual) {
145 y0, y1 := ZMM(), ZMM()
146
147 VPSRLQ(Imm(0x2f), a, y0)
148 // 3 way Xor, replacing VPXOR(a, y0, y0), VPXOR(key[koff], y0, y0)
149 VPTERNLOGD(U8(0x96), a, key, y0)
150 VPMULUDQ(prime, y0, y1)
151 VPSHUFD(Imm(0xf5), y0, y0)
152 VPMULUDQ(prime, y0, y0)
153 VPSLLQ(Imm(0x20), y0, y0)
154
155 VPADDQ(y1, y0, a)
156 }
157
158 func avx512accum(data Mem, a VecVirtual, doff int, key VecVirtual, prefetch bool) {
159 y0, y1, y2 := ZMM(), ZMM(), ZMM()
160 VMOVDQU64(data.Offset(doff+0x00), y0)
161 if prefetch {
162 // Prefetch once per cacheline (64 bytes), 8 iterations ahead.
163 PREFETCHT0(data.Offset(doff + 1024))
164 }
165 VPXORD(key, y0, y1)
166 VPSHUFD(Imm(49), y1, y2)
167 VPMULUDQ(y1, y2, y1)
168 VPSHUFD(Imm(78), y0, y0)
169 VPADDQ(a, y1, a)
170 VPADDQ(a, y0, a)
171 }
172
173 func avx512accumSplitTwo(data Mem, a [4]VecVirtual, doff int, key, key2 VecVirtual, prefetch, initA1 bool) {
174 y0, y1, y2 := ZMM(), ZMM(), ZMM()
175 z0, z1, z2 := ZMM(), ZMM(), ZMM()
176 if initA1 {
177 // Replace destination registers with the accumulator to avoid zeroing and adding.
178 y1 = a[1]
179 z1 = a[2]
180 z0 = a[3]
181 }
182 VMOVDQU64(data.Offset(doff+0x00), y0)
183 VMOVDQU64(data.Offset(doff+0x40), z0)
184 if prefetch {
185 // Prefetch once per cacheline (64 bytes), 8 iterations ahead.
186 PREFETCHT0(data.Offset(doff + 1024))
187 PREFETCHT0(data.Offset(doff + 1024 + 64))
188 }
189 VPXORD(key, y0, y1)
190 VPXORD(key2, z0, z1)
191 VPSHUFD(Imm(49), y1, y2)
192 VPSHUFD(Imm(49), z1, z2)
193 VPMULUDQ(y1, y2, y1)
194 VPMULUDQ(z1, z2, z1)
195 VPSHUFD(Imm(78), y0, y0)
196 VPSHUFD(Imm(78), z0, z0)
197
198 if !initA1 {
199 VPADDQ(a[1], y1, a[1])
200 VPADDQ(a[2], z1, a[2])
201 VPADDQ(a[3], z0, a[3])
202 }
203 VPADDQ(a[0], y0, a[0])
204 }