-
Notifications
You must be signed in to change notification settings - Fork 5
/
qsort_AVX2_HSW_256bit_double.s
executable file
·540 lines (434 loc) · 10.6 KB
/
qsort_AVX2_HSW_256bit_double.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
.set A0, %ymm0
.set A1, %ymm1
.set A2, %ymm2
.set A3, %ymm3
.set A4, %ymm4
.set A5, %ymm5
.set A6, %ymm6
.set M0, %ymm7
.set M1, %ymm8
.set M2, %ymm9
.set M3, %ymm10
.set M4, %ymm11
.set M5, %ymm12
.set M6, %ymm13
.set T, %ymm14
.set PIVOT, %ymm15
.set r0, %r8
.set r1, %r9
.set r2, %r10
.set r3, %r11
.set r4, %r15
.set r5, %r12
.set r6, %rax
# This one sorts in descending order, sort in ascending order, requires swapping the cmp operands.
.align 16
.globl qsort_AVX2
qsort_AVX2:
.set array, %rdi
.set temp_space, %rsi
.set n, %rdx
.set bottom, %r13
.set top, %r14
.Ldo_qsort:
push %r12
push %r13
push %r14
push %r15
push %rbp
.set el1, %xmm0
.set el2, %xmm1
.set el3, %xmm2
# Median of three
mov n, r1
shr $1, r1
vmovsd (array), el1
vmovsd (array, r1, 8), el3
vmovsd -8(array, n, 8), el2
# if el1 > el2
vucomisd el2, el1
jbe .Lm2
# swap el1, el2
vmovapd el1, %xmm3
vmovapd el2, el1
vmovapd %xmm3, el2
.Lm2:
vucomisd el3, el2
jbe .Lm3
# swap el2, el3
vmovapd el3, %xmm3
vmovapd el2, el3
vmovapd %xmm3, el2
# if el1 > el2
vucomisd el2, el1
jbe .Lm3
# swap el1, el2
vmovapd el1, %xmm3
vmovapd el2, el1
vmovapd %xmm3, el2
.Lm3:
vmovsd el1, (array)
vmovsd el3, (array, r1, 8)
vmovsd el2, -8(array, n, 8)
vpermpd $0, %ymm1, PIVOT
#vbroadcastsd -8(array, n, 8), PIVOT
lea (array), bottom
lea (temp_space), top
dec n
.Lx7_loop:
cmp $28, n
jl .Lx7_loop_exit
# Load the next 28 elements
vmovupd 32*0(array), A0
vmovupd 32*1(array), A1
vmovupd 32*2(array), A2
vmovupd 32*3(array), A3
vmovupd 32*4(array), A4
vmovupd 32*5(array), A5
vmovupd 32*6(array), A6
# Find elements lesser-than-equal to PIVOT
vcmppd $2, PIVOT, A0, M0
vcmppd $2, PIVOT, A1, M1
vcmppd $2, PIVOT, A2, M2
vcmppd $2, PIVOT, A3, M3
vcmppd $2, PIVOT, A4, M4
vcmppd $2, PIVOT, A5, M5
vcmppd $2, PIVOT, A6, M6
vmovmskpd M0, r0
vmovmskpd M1, r1
vmovmskpd M2, r2
vmovmskpd M3, r3
vmovmskpd M4, r4
vmovmskpd M5, r5
vmovmskpd M6, r6
shl $5, r0
shl $5, r1
shl $5, r2
shl $5, r3
shl $5, r4
shl $5, r5
shl $5, r6
vmovapd .LPermTableLeft(,r0), T
vpermps A0, T, M0
vmovapd .LPermTableLeft(,r1), T
vpermps A1, T, M1
vmovapd .LPermTableLeft(,r2), T
vpermps A2, T, M2
vmovapd .LPermTableLeft(,r3), T
vpermps A3, T, M3
vmovapd .LPermTableLeft(,r4), T
vpermps A4, T, M4
vmovapd .LPermTableLeft(,r5), T
vpermps A5, T, M5
vmovapd .LPermTableLeft(,r6), T
vpermps A6, T, M6
vmovapd .LPermTableRight(,r0), T
vpermps A0, T, A0
vmovapd .LPermTableRight(,r1), T
vpermps A1, T, A1
vmovapd .LPermTableRight(,r2), T
vpermps A2, T, A2
vmovapd .LPermTableRight(,r3), T
vpermps A3, T, A3
vmovapd .LPermTableRight(,r4), T
vpermps A4, T, A4
vmovapd .LPermTableRight(,r5), T
vpermps A5, T, A5
vmovapd .LPermTableRight(,r6), T
vpermps A6, T, A6
popcnt r0, r0
popcnt r1, r1
popcnt r2, r2
popcnt r3, r3
popcnt r4, r4
popcnt r5, r5
popcnt r6, r6
vmovupd M0, (bottom)
lea (bottom, r0, 8), bottom
vmovupd M1, (bottom)
lea (bottom, r1, 8), bottom
vmovupd M2, (bottom)
lea (bottom, r2, 8), bottom
vmovupd M3, (bottom)
lea (bottom, r3, 8), bottom
vmovupd M4, (bottom)
lea (bottom, r4, 8), bottom
vmovupd M5, (bottom)
lea (bottom, r5, 8), bottom
vmovupd M6, (bottom)
lea (bottom, r6, 8), bottom
neg r0
neg r1
neg r2
neg r3
neg r4
neg r5
neg r6
vmovupd A0, (top)
lea 32(top, r0, 8), top
vmovupd A1, (top)
lea 32(top, r1, 8), top
vmovupd A2, (top)
lea 32(top, r2, 8), top
vmovupd A3, (top)
lea 32(top, r3, 8), top
vmovupd A4, (top)
lea 32(top, r4, 8), top
vmovupd A5, (top)
lea 32(top, r5, 8), top
vmovupd A6, (top)
lea 32(top, r6, 8), top
sub $28, n
add $28*8, array
jmp .Lx7_loop
.Lx7_loop_exit:
.Lx5_loop:
cmp $20, n
jl .Lx5_loop_exit
# Load the next 28 elements
vmovupd 32*0(array), A0
vmovupd 32*1(array), A1
vmovupd 32*2(array), A2
vmovupd 32*3(array), A3
vmovupd 32*4(array), A4
# Find elements lesser-than-equal to PIVOT
vcmppd $2, PIVOT, A0, M0
vcmppd $2, PIVOT, A1, M1
vcmppd $2, PIVOT, A2, M2
vcmppd $2, PIVOT, A3, M3
vcmppd $2, PIVOT, A4, M4
vmovmskpd M0, r0
vmovmskpd M1, r1
vmovmskpd M2, r2
vmovmskpd M3, r3
vmovmskpd M4, r4
shl $5, r0
shl $5, r1
shl $5, r2
shl $5, r3
shl $5, r4
vmovapd .LPermTableLeft(,r0), T
vpermps A0, T, M0
vmovapd .LPermTableLeft(,r1), T
vpermps A1, T, M1
vmovapd .LPermTableLeft(,r2), T
vpermps A2, T, M2
vmovapd .LPermTableLeft(,r3), T
vpermps A3, T, M3
vmovapd .LPermTableLeft(,r4), T
vpermps A4, T, M4
vmovapd .LPermTableRight(,r0), T
vpermps A0, T, A0
vmovapd .LPermTableRight(,r1), T
vpermps A1, T, A1
vmovapd .LPermTableRight(,r2), T
vpermps A2, T, A2
vmovapd .LPermTableRight(,r3), T
vpermps A3, T, A3
vmovapd .LPermTableRight(,r4), T
vpermps A4, T, A4
popcnt r0, r0
popcnt r1, r1
popcnt r2, r2
popcnt r3, r3
popcnt r4, r4
vmovupd M0, (bottom)
lea (bottom, r0, 8), bottom
vmovupd M1, (bottom)
lea (bottom, r1, 8), bottom
vmovupd M2, (bottom)
lea (bottom, r2, 8), bottom
vmovupd M3, (bottom)
lea (bottom, r3, 8), bottom
vmovupd M4, (bottom)
lea (bottom, r4, 8), bottom
neg r0
neg r1
neg r2
neg r3
neg r4
vmovupd A0, (top)
lea 32(top, r0, 8), top
vmovupd A1, (top)
lea 32(top, r1, 8), top
vmovupd A2, (top)
lea 32(top, r2, 8), top
vmovupd A3, (top)
lea 32(top, r3, 8), top
vmovupd A4, (top)
lea 32(top, r4, 8), top
sub $20, n
add $20*8, array
jmp .Lx5_loop
.Lx5_loop_exit:
.Lx3_loop:
cmp $8, n
jl .Lx3_loop_exit
# Load the next 28 elements
vmovupd 32*0(array), A0
vmovupd 32*1(array), A1
# Find elements lesser-than-equal to PIVOT
vcmppd $2, PIVOT, A0, M0
vcmppd $2, PIVOT, A1, M1
vmovmskpd M0, r0
vmovmskpd M1, r1
shl $5, r0
shl $5, r1
vmovapd .LPermTableLeft(,r0), T
vpermps A0, T, M0
vmovapd .LPermTableLeft(,r1), T
vpermps A1, T, M1
vmovapd .LPermTableRight(,r0), T
vpermps A0, T, A0
vmovapd .LPermTableRight(,r1), T
vpermps A1, T, A1
popcnt r0, r0
popcnt r1, r1
vmovupd M0, (bottom)
lea (bottom, r0, 8), bottom
vmovupd M1, (bottom)
lea (bottom, r1, 8), bottom
neg r0
neg r1
vmovupd A0, (top)
lea 32(top, r0, 8), top
vmovupd A1, (top)
lea 32(top, r1, 8), top
sub $8, n
add $8*8, array
jmp .Lx3_loop
.Lx3_loop_exit:
.Lx1_loop:
cmp $2, n
jl .Lx1_loop_exit
# Load the next 28 elements
vmovupd 32*0(array), %xmm0
# Find elements lesser-than-equal to PIVOT
vcmppd $2, %xmm15, %xmm0, %xmm1
vmovmskpd %xmm1, r0
shl $4, r0
vpermilpd .LPermTableLeftS(,r0), %xmm0, %xmm1
vpermilpd .LPermTableRightS(,r0), %xmm0, %xmm0
popcnt r0, r0
shl $3, r0
vmovupd %xmm1, (bottom)
add r0, bottom
sub $16, r0
vmovupd %xmm0, (top)
sub r0, top
sub $2, n
add $2*8, array
jmp .Lx1_loop
.Lx1_loop_exit:
.Ls_loop:
cmp $0, n
je .Ls_loop_exit
vmovsd (array), %xmm0
vucomisd %xmm15, %xmm0
ja .Lgreater
vmovsd %xmm0, (bottom)
add $8, bottom
add $8, array
dec n
jmp .Ls_loop
.Lgreater:
vmovsd %xmm0, (top)
add $8, top
add $8, array
dec n
jmp .Ls_loop
.Ls_loop_exit:
vmovsd %xmm15, (bottom)
add $8, bottom
sub temp_space, top
shr $3, top
mov top, %rax
.Lx4_copy_loop:
cmp $16, top
jl .Lx4_copy_end
vmovdqu 32*0(temp_space), %ymm0
vmovdqu 32*1(temp_space), %ymm1
vmovdqu 32*2(temp_space), %ymm2
vmovdqu 32*3(temp_space), %ymm3
vmovdqu %ymm0, 32*0(bottom)
vmovdqu %ymm1, 32*1(bottom)
vmovdqu %ymm2, 32*2(bottom)
vmovdqu %ymm3, 32*3(bottom)
lea 32*4(temp_space), temp_space
lea 32*4(bottom), bottom
sub $16, top
jmp .Lx4_copy_loop
.Lx4_copy_end:
.Lx1_copy_loop:
cmp $4, top
jl .Lx1_copy_end
vmovdqu 32*0(temp_space), %ymm0
vmovdqu %ymm0, 32*0(bottom)
lea 8*4(temp_space), temp_space
lea 8*4(bottom), bottom
sub $4, top
jmp .Lx1_copy_loop
.Lx1_copy_end:
cmp $0, top
je .Lcopy_end
mov (temp_space), %r8
mov %r8, (bottom)
lea 8(temp_space), temp_space
lea 8(bottom), bottom
sub $1, top
jmp .Lx1_copy_end
.Lcopy_end:
pop %rbp
pop %r15
pop %r14
pop %r13
pop %r12
ret
.align 32
.LPermTableLeft:
.long 0,0,0,0,0,0,0,0 #0
.long 0,1,0,0,0,0,0,0 #1
.long 2,3,0,0,0,0,0,0 #2
.long 0,1,2,3,0,0,0,0 #3
.long 4,5,0,0,0,0,0,0 #4
.long 0,1,4,5,0,0,0,0 #5
.long 2,3,4,5,0,0,0,0 #6
.long 0,1,2,3,4,5,0,0 #7
.long 6,7,0,0,0,0,0,0 #8
.long 0,1,6,7,0,0,0,0 #9
.long 2,3,6,7,0,0,0,0 #10
.long 0,1,2,3,6,7,0,0 #11
.long 4,5,6,7,0,0,0,0 #12
.long 0,1,4,5,6,7,0,0 #13
.long 2,3,4,5,6,7,0,0 #14
.long 0,1,2,3,4,5,6,7 #15
.align 32
.LPermTableRight:
.long 0,1,2,3,4,5,6,7 #0
.long 2,3,4,5,6,7,0,0 #1
.long 0,1,4,5,6,7,0,0 #2
.long 4,5,6,7,0,0,0,0 #3
.long 0,1,2,3,6,7,0,0 #4
.long 2,3,6,7,0,0,0,0 #5
.long 0,1,6,7,0,0,0,0 #6
.long 6,7,0,0,0,0,0,0 #7
.long 0,1,2,3,4,5,0,0 #8
.long 2,3,4,5,0,0,0,0 #9
.long 0,1,4,5,0,0,0,0 #10
.long 4,5,0,0,0,0,0,0 #11
.long 0,1,2,3,0,0,0,0 #12
.long 2,3,0,0,0,0,0,0 #13
.long 0,1,0,0,0,0,0,0 #14
.long 0,0,0,0,0,0,0,0 #15
.align 16
.LPermTableLeftS:
.quad 0,0 #0
.quad 0,0 #1
.quad 2,0 #2
.quad 0,2 #3
.align 16
.LPermTableRightS:
.quad 0,2 #0
.quad 2,0 #1
.quad 0,0 #2
.quad 0,0 #3