Debian Codegraph @

build/qsort/sort_asm.go

raw
1 // +build !amd64
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 )
13
14 func init() {
15 ConstraintExpr("!purego")
16 }
17
18 func main() {
19 distributeForward(&SortableScalar{reg: GP64, size: 8, mov: MOVQ, cmp: CMPQ})
20 distributeBackward(&SortableScalar{reg: GP64, size: 8, mov: MOVQ, cmp: CMPQ})
21
22 insertionsort(&SortableVector{reg: XMM, size: 16})
23 distributeForward(&SortableVector{reg: XMM, size: 16})
24 distributeBackward(&SortableVector{reg: XMM, size: 16})
25
26 insertionsort(&SortableVector{reg: YMM, size: 32})
27 distributeForward(&SortableVector{reg: YMM, size: 32})
28 distributeBackward(&SortableVector{reg: YMM, size: 32})
29
30 Generate()
31 }
32
33 type Sortable interface {
34 Register() Register
35 Size() uint64
36 Init()
37 Move(Op, Op)
38 Compare(Register, Register)
39 }
40
41 type SortableScalar struct {
42 reg func() GPVirtual
43 size uint64
44 mov func(Op, Op)
45 cmp func(Op, Op)
46 }
47
48 func (s *SortableScalar) Register() Register {
49 return s.reg()
50 }
51
52 func (s *SortableScalar) Size() uint64 {
53 return s.size
54 }
55
56 func (s *SortableScalar) Init() {}
57
58 func (s *SortableScalar) Move(a, b Op) {
59 s.mov(a, b)
60 }
61
62 func (s *SortableScalar) Compare(a, b Register) {
63 s.cmp(a, b)
64 }
65
66 type SortableVector struct {
67 reg func() VecVirtual
68 size uint64
69 ones Register
70 msb Register
71 }
72
73 func (s *SortableVector) Register() Register {
74 return s.reg()
75 }
76
77 func (s *SortableVector) Size() uint64 {
78 return s.size
79 }
80
81 func (s *SortableVector) Init() {
82 s.ones = s.reg()
83 VPCMPEQB(s.ones, s.ones, s.ones)
84 s.msb = s.reg()
85 VPSLLQ(Imm(63), s.ones, s.msb)
86 }
87
88 func (s *SortableVector) Move(a, b Op) {
89 VMOVDQU(a, b)
90 }
91
92 func (s *SortableVector) Compare(a, b Register) {
93 // The following is a routine for vectors that yields the same ZF/CF
94 // result as a CMP instruction.
95
96 // First compare each packed qword for equality.
97 eq := s.Register()
98 VPCMPEQQ(a, b, eq)
99
100 // SSE4.2 and AVX2 have a CMPGTQ to compare packed qwords, but
101 // unfortunately it's a signed comparison. We know that u64 has
102 // range [0,2^64-1] and signed (two's complement) i64 has range
103 // [-2^63,2^63-1]. We can add (or subtract) 2^63 to each packed
104 // unsigned qword and reinterpret each as a signed qword. Doing so
105 // allows us to utilize a signed comparison, and yields the same
106 // result as if we were doing an unsigned comparison with the input.
107 // As usual, AVX-512 fixes the problem with its VPCMPUQ.
108 lt := s.Register()
109 aSigned := s.Register()
110 bSigned := s.Register()
111 VPADDQ(a, s.msb, aSigned)
112 VPADDQ(b, s.msb, bSigned)
113 VPCMPGTQ(aSigned, bSigned, lt)
114
115 // Extract bit masks.
116 eqMask := GP32()
117 ltMask := GP32()
118 VMOVMSKPD(eq, eqMask)
119 VMOVMSKPD(lt, ltMask)
120
121 // Invert the equality mask to find qwords that weren't equal.
122 // Bit-scan forward to find the first unequal byte, then test
123 // that bit in the less-than mask.
124 NOTL(eqMask)
125 unequalByteIndex := GP32()
126 BSFL(eqMask, unequalByteIndex) // set ZF
127 BTSL(unequalByteIndex, ltMask) // set CF
128 }
129
130 func insertionsort(s Sortable) {
131 size := s.Size()
132 TEXT(fmt.Sprintf("insertionsort%dNoSwap", size*8), NOSPLIT, fmt.Sprintf("func(data []%s, base int, swap func(int, int))", typeFor(size)))
133 Pragma("noescape")
134
135 data := Load(Param("data").Base(), GP64())
136 end := Load(Param("data").Len(), GP64())
137 shift := log2(size)
138 SHLQ(Imm(shift), end)
139 ADDQ(data, end)
140 TESTQ(data, end)
141 JE(LabelRef("done"))
142
143 s.Init()
144
145 i := GP64()
146 MOVQ(data, i)
147
148 Label("outer")
149 ADDQ(Imm(size), i)
150 CMPQ(i, end)
151 JAE(LabelRef("done"))
152 item := s.Register()
153 s.Move(Mem{Base: i}, item)
154 j := GP64()
155 MOVQ(i, j)
156
157 Label("inner")
158 prev := s.Register()
159 s.Move(Mem{Base: j, Disp: -int(size)}, prev)
160 s.Compare(item, prev)
161 JAE(LabelRef("outer"))
162
163 s.Move(prev, Mem{Base: j})
164 s.Move(item, Mem{Base: j, Disp: -int(size)})
165 SUBQ(Imm(size), j)
166 CMPQ(j, data)
167 JA(LabelRef("inner"))
168 JMP(LabelRef("outer"))
169
170 Label("done")
171 if size > 16 {
172 VZEROUPPER()
173 }
174 RET()
175 }
176
177 func distributeForward(s Sortable) {
178 size := s.Size()
179 TEXT(fmt.Sprintf("distributeForward%d", size*8), NOSPLIT, fmt.Sprintf("func(data, scratch []%s, limit, lo, hi int) int", typeFor(size)))
180 Pragma("noescape")
181
182 // Load inputs.
183 data := Load(Param("data").Base(), GP64())
184 scratch := Load(Param("scratch").Base(), GP64())
185 limit := Load(Param("limit"), GP64())
186 loIndex := Load(Param("lo"), GP64())
187 hiIndex := Load(Param("hi"), GP64())
188
189 // Convert indices to byte offsets if necessary. We can use indices
190 // only if the size is a valid scale (1/2/4/8).
191 shift := log2(size)
192 var scale uint8
193 if size <= 8 {
194 scale = uint8(size)
195 } else {
196 scale = 1
197 SHLQ(Imm(shift), limit)
198 SHLQ(Imm(shift), loIndex)
199 SHLQ(Imm(shift), hiIndex)
200 }
201
202 // Prepare read/cmp pointers.
203 lo := GP64()
204 hi := GP64()
205 tail := GP64()
206 LEAQ(Mem{Base: data, Index: loIndex, Scale: scale}, lo)
207 LEAQ(Mem{Base: data, Index: hiIndex, Scale: scale}, hi)
208 LEAQ(Mem{Base: scratch, Index: limit, Scale: scale, Disp: -int(size)}, tail)
209
210 s.Init()
211
212 // Load the pivot item.
213 pivot := s.Register()
214 s.Move(Mem{Base: data}, pivot)
215
216 offset := GP64()
217 zero := GP64()
218 XORQ(offset, offset)
219 XORQ(zero, zero)
220 isGreaterOrEqual := zero
221
222 // We'll be keeping a negative offset. Negate the limit so we can
223 // compare the two in the loop.
224 NEGQ(limit)
225
226 Label("loop")
227
228 // Load the next item.
229 next := s.Register()
230 s.Move(Mem{Base: lo}, next)
231
232 // Compare the item with the pivot.
233 s.Compare(next, pivot)
234
235 // Conditionally write to either the beginning of the data slice, or
236 // end of the scratch slice.
237 dst := GP64()
238 MOVQ(lo, dst)
239 CMOVQCC(tail, dst)
240 s.Move(next, Mem{Base: dst, Index: offset, Scale: scale})
241 if size <= 8 {
242 // If we're only subtracting one from the index, we can invert CF and use
243 // subtract with carry.
244 CMC()
245 SBBQ(zero, offset)
246 } else {
247 // Otherwise we need to extract an inverted CF and shift to get a byte
248 // amount to advance by.
249 SETCC(isGreaterOrEqual.As8())
250 SHLQ(Imm(shift), isGreaterOrEqual)
251 SUBQ(isGreaterOrEqual, offset)
252 }
253 ADDQ(Imm(size), lo)
254
255 // Loop while we have more input, and enough room in the scratch slice.
256 CMPQ(lo, hi)
257 JA(LabelRef("done"))
258 CMPQ(offset, limit)
259 JNE(LabelRef("loop"))
260
261 // Return the number of items written to the data slice.
262 Label("done")
263 SUBQ(data, lo)
264 LEAQ(Mem{Base: lo, Index: offset, Scale: scale}, lo)
265 SHRQ(Imm(shift), lo)
266 DECQ(lo)
267 Store(lo, ReturnIndex(0))
268 if size > 16 {
269 VZEROUPPER()
270 }
271 RET()
272 }
273
274 func distributeBackward(s Sortable) {
275 size := s.Size()
276 TEXT(fmt.Sprintf("distributeBackward%d", size*8), NOSPLIT, fmt.Sprintf("func(data, scratch []%s, limit, lo, hi int) int", typeFor(size)))
277 Pragma("noescape")
278
279 // Load inputs.
280 data := Load(Param("data").Base(), GP64())
281 scratch := Load(Param("scratch").Base(), GP64())
282 limit := Load(Param("limit"), GP64())
283 loIndex := Load(Param("lo"), GP64())
284 hiIndex := Load(Param("hi"), GP64())
285
286 // Convert indices to byte offsets if necessary. We can use indices
287 // only if the size is a valid scale (1/2/4/8).
288 shift := log2(size)
289 var scale uint8
290 if size <= 8 {
291 scale = uint8(size)
292 } else {
293 scale = 1
294 SHLQ(Imm(shift), limit)
295 SHLQ(Imm(shift), loIndex)
296 SHLQ(Imm(shift), hiIndex)
297 }
298
299 // Prepare read/cmp pointers.
300 lo := GP64()
301 hi := GP64()
302 LEAQ(Mem{Base: data, Index: loIndex, Scale: scale}, lo)
303 LEAQ(Mem{Base: data, Index: hiIndex, Scale: scale}, hi)
304
305 s.Init()
306
307 // Load the pivot item.
308 pivot := s.Register()
309 s.Move(Mem{Base: data}, pivot)
310
311 offset := GP64()
312 zero := GP64()
313 XORQ(offset, offset)
314 XORQ(zero, zero)
315 isLess := zero
316
317 CMPQ(hi, lo)
318 JBE(LabelRef("done"))
319
320 Label("loop")
321
322 // Load the next item.
323 next := s.Register()
324 s.Move(Mem{Base: hi}, next)
325
326 // Compare the item with the pivot.
327 s.Compare(next, pivot)
328
329 // Conditionally write to either the end of the data slice, or
330 // beginning of the scratch slice.
331 dst := GP64()
332 MOVQ(scratch, dst)
333 CMOVQCC(hi, dst)
334 s.Move(next, Mem{Base: dst, Index: offset, Scale: scale})
335 if size <= 8 {
336 ADCQ(zero, offset)
337 } else {
338 SETCS(isLess.As8())
339 SHLQ(Imm(shift), isLess)
340 ADDQ(isLess, offset)
341 }
342 SUBQ(Imm(size), hi)
343
344 // Loop while we have more input, and enough room in the scratch slice.
345 CMPQ(hi, lo)
346 JBE(LabelRef("done"))
347 CMPQ(offset, limit)
348 JNE(LabelRef("loop"))
349
350 // Return the number of items written to the data slice.
351 Label("done")
352 SUBQ(data, hi)
353 LEAQ(Mem{Base: hi, Index: offset, Scale: scale}, hi)
354 SHRQ(Imm(shift), hi)
355 Store(hi, ReturnIndex(0))
356 if size > 16 {
357 VZEROUPPER()
358 }
359 RET()
360 }
361
362 func log2(size uint64) uint64 {
363 return uint64(math.Log2(float64(size)))
364 }
365
366 func typeFor(size uint64) string {
367 switch size {
368 case 32:
369 return "struct { a, b, c, d uint64 }"
370 case 16:
371 return "struct { hi, lo uint64 }"
372 default:
373 return fmt.Sprintf("uint%d", size*8)
374 }
375 }