Debian Codegraph @

build/mem/index_pair_asm.go

raw
1 // +build ignore
2
3 package main
4
5 import (
6 "fmt"
7 "math"
8
9 . "github.com/mmcloughlin/avo/build"
10 . "github.com/mmcloughlin/avo/operand"
11 . "github.com/mmcloughlin/avo/reg"
12 . "github.com/segmentio/asm/build/internal/x86"
13 "github.com/segmentio/asm/cpu"
14 )
15
16 func init() {
17 ConstraintExpr("!purego")
18 }
19
20 func main() {
21 generateIndexPair(indexPair1{})
22 generateIndexPair(indexPair2{})
23 generateIndexPair(indexPair4{})
24 generateIndexPair(indexPair8{})
25 generateIndexPair(indexPair16{})
26 generateIndexPair(indexPair32{})
27 Generate()
28 }
29
30 type indexPair interface {
31 size() int
32 test(a, b Mem)
33 }
34
35 type indexPairAVX2 interface {
36 indexPair
37 vpcmpeq(src0, src1, dst VecVirtual)
38 vpmovmskb(tmp, src VecVirtual, spare, dst Register)
39 }
40
41 type indexPair1 struct{}
42
43 func (indexPair1) size() int { return 1 }
44 func (indexPair1) test(a, b Mem) { generateIndexPairTest(MOVB, CMPB, GP8, a, b) }
45 func (indexPair1) vpcmpeq(a, b, c VecVirtual) { VPCMPEQB(a, b, c) }
46 func (indexPair1) vpmovmskb(_, a VecVirtual, _, b Register) { VPMOVMSKB(a, b) }
47
48 type indexPair2 struct{}
49
50 func (indexPair2) size() int { return 2 }
51 func (indexPair2) test(a, b Mem) { generateIndexPairTest(MOVW, CMPW, GP16, a, b) }
52 func (indexPair2) vpcmpeq(a, b, c VecVirtual) { VPCMPEQW(a, b, c) }
53 func (indexPair2) vpmovmskb(_, a VecVirtual, _, b Register) { VPMOVMSKB(a, b) }
54
55 type indexPair4 struct{}
56
57 func (indexPair4) size() int { return 4 }
58 func (indexPair4) test(a, b Mem) { generateIndexPairTest(MOVL, CMPL, GP32, a, b) }
59 func (indexPair4) vpcmpeq(a, b, c VecVirtual) { VPCMPEQD(a, b, c) }
60 func (indexPair4) vpmovmskb(_, a VecVirtual, _, b Register) { VPMOVMSKB(a, b) }
61
62 type indexPair8 struct{}
63
64 func (indexPair8) size() int { return 8 }
65 func (indexPair8) test(a, b Mem) { generateIndexPairTest(MOVQ, CMPQ, GP64, a, b) }
66 func (indexPair8) vpcmpeq(a, b, c VecVirtual) { VPCMPEQQ(a, b, c) }
67 func (indexPair8) vpmovmskb(_, a VecVirtual, _, b Register) { VPMOVMSKB(a, b) }
68
69 type indexPair16 struct{}
70
71 func (indexPair16) size() int {
72 return 16
73 }
74 func (indexPair16) test(a, b Mem) {
75 r0, r1 := XMM(), XMM()
76 MOVOU(a, r0)
77 MOVOU(b, r1)
78 mask := GP32()
79 PCMPEQQ(r0, r1)
80 PMOVMSKB(r1, mask)
81 CMPL(mask, U32(0xFFFF))
82 }
83 func (indexPair16) vpcmpeq(a, b, c VecVirtual) {
84 VPCMPEQQ(a, b, c)
85 }
86 func (indexPair16) vpmovmskb(tmp, src VecVirtual, _, dst Register) {
87 // https://www.felixcloutier.com/x86/vpermq#vpermq--vex-256-encoded-version-
88 //
89 // Swap each quad word in the lower and upper half of the 32 bytes register,
90 // then AND the src and tmp registers to zero each halves that were partial
91 // equality; only fully equal 128 bits need to result in setting 1 bits in
92 // the destination mask.
93 const permutation = (1 << 0) | (0 << 2) | (3 << 4) | (2 << 6)
94 VPERMQ(Imm(permutation), src, tmp)
95 VPAND(src, tmp, tmp)
96 VPMOVMSKB(tmp, dst)
97 }
98
99 type indexPair32 struct{}
100
101 func (indexPair32) size() int {
102 return 32
103 }
104 func (indexPair32) test(a, b Mem) {
105 r0, r1, r2, r3 := XMM(), XMM(), XMM(), XMM()
106 MOVOU(a, r0)
107 MOVOU(a.Offset(16), r1)
108 MOVOU(b, r2)
109 MOVOU(b.Offset(16), r3)
110 mask0, mask1 := GP32(), GP32()
111 PCMPEQQ(r0, r2)
112 PCMPEQQ(r1, r3)
113 PMOVMSKB(r2, mask0)
114 PMOVMSKB(r3, mask1)
115 ANDL(mask1, mask0)
116 CMPL(mask0, U32(0xFFFF))
117 }
118 func (indexPair32) vpcmpeq(a, b, c VecVirtual) {
119 VPCMPEQQ(a, b, c)
120 }
121 func (indexPair32) vpmovmskb(_, src VecVirtual, zero, dst Register) {
122 VPMOVMSKB(src, dst)
123 CMPL(dst, U32(0xFFFFFFFF))
124 CMOVLNE(zero, dst)
125 }
126
127 func generateIndexPair(code indexPair) {
128 size := code.size()
129 TEXT(fmt.Sprintf("indexPair%d", size), NOSPLIT, "func(b []byte) int")
130
131 p := Load(Param("b").Base(), GP64())
132 n := Load(Param("b").Len(), GP64())
133
134 base := GP64()
135 MOVQ(p, base)
136
137 CMPQ(n, Imm(0))
138 JLE(LabelRef("fail"))
139 SUBQ(Imm(uint64(size)), n)
140
141 if _, ok := code.(indexPairAVX2); ok {
142 JumpIfFeature("avx2", cpu.AVX2)
143 }
144
145 Label("tail")
146 CMPQ(n, Imm(0))
147 JE(LabelRef("fail"))
148
149 Label("generic")
150 code.test(Mem{Base: p}, (Mem{Base: p}).Offset(size))
151 JE(LabelRef("done"))
152 ADDQ(Imm(uint64(size)), p)
153 SUBQ(Imm(uint64(size)), n)
154 CMPQ(n, Imm(0))
155 JA(LabelRef("generic"))
156
157 index := p
158 Label("fail")
159 MOVQ(U64(math.MaxUint64), index)
160 Store(index, ReturnIndex(0))
161 RET()
162
163 Label("done")
164 // The delta between the base pointer and how far we advanced is the index of the pair.
165 SUBQ(base, index)
166 Store(index, ReturnIndex(0))
167 RET()
168
169 if avx, ok := code.(indexPairAVX2); ok {
170 const avxChunk = 256
171 const avxLanes = avxChunk / 32
172 Label("avx2")
173 CMPQ(n, U32(avxChunk+uint64(size)))
174 JB(LabelRef(fmt.Sprintf("avx2_tail%d", avxChunk/2)))
175
176 masks := make([]GPVirtual, avxLanes)
177 for i := range masks {
178 masks[i] = GP64()
179 XORQ(masks[i], masks[i])
180 }
181
182 regA := make([]VecVirtual, avxLanes)
183 regB := make([]VecVirtual, avxLanes)
184 for i := range regA {
185 regA[i] = YMM()
186 regB[i] = YMM()
187 }
188
189 Label(fmt.Sprintf("avx2_loop%d", avxChunk))
190 generateIndexPairAVX2(p, regA, regB, masks, avx)
191 ADDQ(U32(avxChunk), p)
192 SUBQ(U32(avxChunk), n)
193 CMPQ(n, U32(avxChunk+uint64(size)))
194 JAE(LabelRef(fmt.Sprintf("avx2_loop%d", avxChunk)))
195
196 for chunk := avxChunk / 2; chunk >= 32; chunk /= 2 {
197 Label(fmt.Sprintf("avx2_tail%d", chunk))
198 CMPQ(n, Imm(uint64(chunk+size)))
199 JB(LabelRef(fmt.Sprintf("avx2_tail%d", chunk/2)))
200 lanes := chunk / 32
201 generateIndexPairAVX2(p, regA[:lanes], regB[:lanes], masks[:lanes], avx)
202 ADDQ(U32(uint64(chunk)), p)
203 SUBQ(U32(uint64(chunk)), n)
204 }
205
206 Label("avx2_tail16")
207 if size < 16 {
208 CMPQ(n, Imm(uint64(16+size)))
209 JB(LabelRef("avx2_tail"))
210 generateIndexPairAVX2(p, []VecVirtual{XMM()}, []VecVirtual{XMM()}, masks[:1], avx)
211 ADDQ(Imm(16), p)
212 SUBQ(Imm(16), n)
213 }
214
215 Label("avx2_tail")
216 VZEROUPPER()
217 JMP(LabelRef("tail"))
218
219 Label("avx2_done")
220 VZEROUPPER()
221 for i, mask := range masks {
222 CMPQ(mask, Imm(0))
223 JNE(LabelRef(fmt.Sprintf("avx2_done%d", i)))
224 }
225
226 for i, mask := range masks {
227 Label(fmt.Sprintf("avx2_done%d", i))
228 if i > 0 {
229 ADDQ(U32(uint64(i*32)), p)
230 SUBQ(U32(uint64(i*32)), n)
231 }
232 TZCNTQ(mask, mask)
233 ADDQ(mask, p)
234 SUBQ(mask, n)
235 JMP(LabelRef("done"))
236 }
237 }
238 }
239
240 func generateIndexPairTest(mov func(Op, Op), cmp func(Op, Op), reg func() GPVirtual, a, b Mem) {
241 r := reg()
242 mov(a, r)
243 cmp(r, b)
244 }
245
246 func generateIndexPairAVX2(p Register, regA, regB []VecVirtual, masks []GPVirtual, code indexPairAVX2) {
247 size := code.size()
248 moves := make(map[int]VecVirtual)
249
250 spare := GP64()
251 if size == 32 {
252 // This is a bit of an implicit coupling to the 32 bytes specialication,
253 // but it did not seem worth the extra complexity to have more
254 // abstractions.
255 //
256 // The spare register is passed to vpmovmskb and must be initialized to
257 // zero as it may be used to clear the mask register.
258 XORQ(spare, spare)
259 }
260
261 for i, reg := range regA {
262 VMOVDQU((Mem{Base: p}).Offset(i*32), reg)
263 moves[i*32] = reg
264 }
265
266 for i, reg := range regB {
267 // Skip loading from memory a second time if we already loaded the
268 // offset in the previous loop. This optimization applies for items
269 // of size 32.
270 if moves[i*32+size] == nil {
271 lo := moves[i*32+(size-16)]
272 hi := moves[i*32+(size+16)]
273
274 if lo != nil && hi != nil {
275 // https://www.felixcloutier.com/x86/vperm2i128#vperm2i128
276 //
277 // The data was already loaded, but split across two registers.
278 // We recompose it using a permutation of the upper and lower
279 // halves of the registers holding the contiguous data.
280 //
281 // Note that in Go assembly the arguments are reversed;
282 // SRC1 is `lo` and SRC2 is `hi`, but we pass them in the
283 // reverse order.
284 const permutation = (1 << 0) | (2 << 4)
285 VPERM2I128(Imm(permutation), hi, lo, reg)
286 } else {
287 VMOVDQU((Mem{Base: p}).Offset(i*32+size), reg)
288 }
289 }
290 }
291
292 for i := range regA {
293 // The load may have been elided if there was offset overlaps between
294 // the two sources.
295 if mov := moves[i*32+size]; mov != nil {
296 code.vpcmpeq(regA[i], mov, regB[i])
297 } else {
298 code.vpcmpeq(regA[i], regB[i], regB[i])
299 }
300 }
301
302 for i := range regB {
303 code.vpmovmskb(regA[i], regB[i], spare.As32(), masks[i].As32())
304 }
305
306 combinedMask := spare
307 if len(masks) == 1 {
308 combinedMask = masks[0]
309 } else {
310 XORQ(combinedMask, combinedMask)
311 for _, mask := range masks {
312 ORQ(mask, combinedMask)
313 }
314 }
315
316 CMPQ(combinedMask, Imm(0))
317 JNE(LabelRef("avx2_done"))
318 }