Debian Codegraph @

build/mem/count_pair_asm.go

raw
1 // +build ignore
2
3 package main
4
5 import (
6 "fmt"
7 "math/bits"
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 generateCountPair(countPair1{})
22 generateCountPair(countPair2{})
23 generateCountPair(countPair4{})
24 generateCountPair(countPair8{})
25 generateCountPair(countPair16{})
26 generateCountPair(countPair32{})
27 Generate()
28 }
29
30 type countPair interface {
31 size() int
32 test(a, b Mem)
33 }
34
35 type countPairAVX2 interface {
36 countPair
37 vpcmpeq(src0, src1, dst VecVirtual)
38 vpmovmskb(tmp, src VecVirtual, dst Register)
39 }
40
41 type countPair1 struct{}
42
43 func (countPair1) size() int { return 1 }
44 func (countPair1) test(a, b Mem) { generateCountPairTest(MOVB, CMPB, GP8, a, b) }
45 func (countPair1) vpcmpeq(a, b, c VecVirtual) { VPCMPEQB(a, b, c) }
46 func (countPair1) vpmovmskb(_, a VecVirtual, b Register) { VPMOVMSKB(a, b) }
47
48 type countPair2 struct{}
49
50 func (countPair2) size() int { return 2 }
51 func (countPair2) test(a, b Mem) { generateCountPairTest(MOVW, CMPW, GP16, a, b) }
52 func (countPair2) vpcmpeq(a, b, c VecVirtual) { VPCMPEQW(a, b, c) }
53 func (countPair2) vpmovmskb(_, a VecVirtual, b Register) { VPMOVMSKB(a, b) }
54
55 type countPair4 struct{}
56
57 func (countPair4) size() int { return 4 }
58 func (countPair4) test(a, b Mem) { generateCountPairTest(MOVL, CMPL, GP32, a, b) }
59 func (countPair4) vpcmpeq(a, b, c VecVirtual) { VPCMPEQD(a, b, c) }
60 func (countPair4) vpmovmskb(_, a VecVirtual, b Register) { VPMOVMSKB(a, b) }
61
62 type countPair8 struct{}
63
64 func (countPair8) size() int { return 8 }
65 func (countPair8) test(a, b Mem) { generateCountPairTest(MOVQ, CMPQ, GP64, a, b) }
66 func (countPair8) vpcmpeq(a, b, c VecVirtual) { VPCMPEQQ(a, b, c) }
67 func (countPair8) vpmovmskb(_, a VecVirtual, b Register) { VPMOVMSKB(a, b) }
68
69 type countPair16 struct{}
70
71 func (countPair16) size() int {
72 return 16
73 }
74 func (countPair16) 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 (countPair16) vpcmpeq(a, b, c VecVirtual) {
84 VPCMPEQQ(a, b, c)
85 }
86 func (countPair16) 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 countPair32 struct{}
100
101 func (countPair32) size() int {
102 return 32
103 }
104 func (countPair32) 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 (countPair32) vpcmpeq(a, b, c VecVirtual) {
119 VPCMPEQQ(a, b, c)
120 }
121 func (countPair32) vpmovmskb(_, src VecVirtual, dst Register) {
122 VPMOVMSKB(src, dst)
123 }
124
125 func generateCountPair(code countPair) {
126 size := code.size()
127 TEXT(fmt.Sprintf("countPair%d", size), NOSPLIT, "func(b []byte) int")
128
129 p := Load(Param("b").Base(), GP64())
130 n := Load(Param("b").Len(), GP64())
131 r := GP64()
132 XORQ(r, r)
133 SUBQ(Imm(uint64(size)), n)
134
135 if _, ok := code.(countPairAVX2); ok {
136 JumpIfFeature("avx2", cpu.AVX2)
137 }
138
139 Label("tail")
140 CMPQ(n, Imm(0))
141 JLE(LabelRef("done"))
142
143 Label("generic")
144 x := GP64()
145 MOVQ(r, x)
146 INCQ(x)
147 code.test(Mem{Base: p}, (Mem{Base: p}).Offset(size))
148 CMOVQEQ(x, r)
149 ADDQ(Imm(uint64(size)), p)
150 SUBQ(Imm(uint64(size)), n)
151 CMPQ(n, Imm(0))
152 JG(LabelRef("generic"))
153
154 Label("done")
155 Store(r, ReturnIndex(0))
156 RET()
157
158 if avx, ok := code.(countPairAVX2); ok {
159 const avxChunk = 256
160 const avxLanes = avxChunk / 32
161 Label("avx2")
162 CMPQ(n, U32(avxChunk+uint64(size)))
163 JL(LabelRef(fmt.Sprintf("avx2_tail%d", avxChunk/2)))
164
165 masks := make([]GPVirtual, avxLanes)
166 for i := range masks {
167 masks[i] = GP64()
168 XORQ(masks[i], masks[i])
169 }
170
171 regA := make([]VecVirtual, avxLanes)
172 regB := make([]VecVirtual, avxLanes)
173 for i := range regA {
174 regA[i] = YMM()
175 regB[i] = YMM()
176 }
177
178 Label(fmt.Sprintf("avx2_loop%d", avxChunk))
179 generateCountPairAVX2(r, p, regA, regB, masks, avx)
180 ADDQ(U32(avxChunk), p)
181 SUBQ(U32(avxChunk), n)
182 CMPQ(n, U32(avxChunk+uint64(size)))
183 JGE(LabelRef(fmt.Sprintf("avx2_loop%d", avxChunk)))
184
185 for chunk := avxChunk / 2; chunk >= 32; chunk /= 2 {
186 Label(fmt.Sprintf("avx2_tail%d", chunk))
187 CMPQ(n, Imm(uint64(chunk+size)))
188 JL(LabelRef(fmt.Sprintf("avx2_tail%d", chunk/2)))
189 lanes := chunk / 32
190 generateCountPairAVX2(r, p, regA[:lanes], regB[:lanes], masks[:lanes], avx)
191 ADDQ(U32(uint64(chunk)), p)
192 SUBQ(U32(uint64(chunk)), n)
193 }
194
195 Label("avx2_tail16")
196 if size < 16 {
197 CMPQ(n, Imm(uint64(16+size)))
198 JL(LabelRef("avx2_tail"))
199 generateCountPairAVX2(r, p, []VecVirtual{XMM()}, []VecVirtual{XMM()}, masks[:1], avx)
200 ADDQ(Imm(16), p)
201 SUBQ(Imm(16), n)
202 }
203
204 Label("avx2_tail")
205 VZEROUPPER()
206 if size < 32 {
207 if shift := divideShift(size); shift > 0 {
208 SHRQ(Imm(uint64(shift)), r)
209 }
210 }
211 JMP(LabelRef("tail"))
212 }
213 }
214
215 func generateCountPairTest(mov func(Op, Op), cmp func(Op, Op), reg func() GPVirtual, a, b Mem) {
216 r := reg()
217 mov(a, r)
218 cmp(r, b)
219 }
220
221 func generateCountPairAVX2(r, p Register, regA, regB []VecVirtual, masks []GPVirtual, code countPairAVX2) {
222 size := code.size()
223 moves := make(map[int]VecVirtual)
224
225 for i, reg := range regA {
226 VMOVDQU((Mem{Base: p}).Offset(i*32), reg)
227 moves[i*32] = reg
228 }
229
230 for i, reg := range regB {
231 // Skip loading from memory a second time if we already loaded the
232 // offset in the previous loop. This optimization applies for items
233 // of size 32.
234 if moves[i*32+size] == nil {
235 lo := moves[i*32+(size-16)]
236 hi := moves[i*32+(size+16)]
237
238 if lo != nil && hi != nil {
239 // https://www.felixcloutier.com/x86/vperm2i128#vperm2i128
240 //
241 // The data was already loaded, but split across two registers.
242 // We recompose it using a permutation of the upper and lower
243 // halves of the registers holding the contiguous data.
244 //
245 // Note that in Go assembly the arguments are reversed;
246 // SRC1 is `lo` and SRC2 is `hi`, but we pass them in the
247 // reverse order.
248 const permutation = (1 << 0) | (2 << 4)
249 VPERM2I128(Imm(permutation), hi, lo, reg)
250 } else {
251 VMOVDQU((Mem{Base: p}).Offset(i*32+size), reg)
252 }
253 }
254 }
255
256 for i := range regA {
257 // The load may have been elided if there was offset overlaps between
258 // the two sources.
259 if mov := moves[i*32+size]; mov != nil {
260 code.vpcmpeq(regA[i], mov, regB[i])
261 } else {
262 code.vpcmpeq(regA[i], regB[i], regB[i])
263 }
264 code.vpmovmskb(regA[i], regB[i], masks[i].As32())
265 }
266
267 for _, mask := range masks {
268 POPCNTQ(mask, mask)
269 if size == 32 {
270 SHRQ(Imm(uint64(divideShift(size))), mask)
271 }
272 }
273
274 ADDQ(divideAndConquerSum(masks), r)
275 }
276
277 func divideShift(size int) int {
278 return bits.TrailingZeros(uint(size))
279 }
280
281 func divideAndConquerSum(regs []GPVirtual) GPVirtual {
282 switch len(regs) {
283 case 1:
284 return regs[0]
285
286 case 2:
287 r0, r1 := regs[0], regs[1]
288 ADDQ(r1, r0)
289 return r0
290
291 default:
292 i := len(regs) / 2
293 r0 := divideAndConquerSum(regs[:i])
294 r1 := divideAndConquerSum(regs[i:])
295 ADDQ(r1, r0)
296 return r0
297 }
298 }