-
Notifications
You must be signed in to change notification settings - Fork 5
/
qsort_AVX2_HSW_256bit_doublequad.s
executable file
·338 lines (260 loc) · 6.31 KB
/
qsort_AVX2_HSW_256bit_doublequad.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
.set pivotALU, %rcx
.set A0, %ymm0
.set A1, %ymm1
.set A2, %ymm2
.set A3, %ymm3
.set G0, %ymm4
.set G1, %ymm5
.set G2, %ymm6
.set G3, %ymm7
.set G0_xmm, %xmm4
.set G1_xmm, %xmm5
.set G2_xmm, %xmm6
.set G3_xmm, %xmm7
.set E0, %ymm8
.set E1, %ymm9
.set E2, %ymm10
.set E3, %ymm11
.set E0_xmm, %xmm8
.set E1_xmm, %xmm9
.set E2_xmm, %xmm10
.set E3_xmm, %xmm11
.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 ascending order
.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 n
push %r12
push %r13
push %r14
push %r15
push %rbp
#mov -8(array, n, 8), pivotALU
#vpbroadcastq -8(array, n, 8), PIVOT
mov n, r0
shl $4, r0
VBROADCASTI128 -16(array, r0), PIVOT
lea (array), bottom
lea (temp_space), top
dec n
.Lx4_loop:
cmp $8, n
jl .Lx4_loop_exit
# Load the next 8 elements
vmovdqu 32*0(array), A0
vmovdqu 32*1(array), A1
vmovdqu 32*2(array), A2
vmovdqu 32*3(array), A3
# Find elements greater-than PIVOT
vpcmpgtq A0, PIVOT, G0 # P > A
vpcmpgtq A1, PIVOT, G1
vpcmpgtq A2, PIVOT, G2
vpcmpgtq A3, PIVOT, G3
vpermq $0xd, G0, E0 # Top QWORDS of the key are lesser than equal
vpermq $0xd, G1, E1
vpermq $0xd, G2, E2
vpermq $0xd, G3, E3
vpermq $0x8, G0, G0 # Low QWORDS of the key are lesser than equal
vpermq $0x8, G1, G1
vpermq $0x8, G2, G2
vpermq $0x8, G3, G3
# Find elements equal to PIVOT
vpcmpeqq A0, PIVOT, T # A == P
vpunpckhqdq T, T, T
vpand T, T, G0
vpcmpeqq A1, PIVOT, T # A == P
vpunpckhqdq T, T, T
vpand T, T, G1
vpcmpeqq A2, PIVOT, T # A == P
vpunpckhqdq T, T, T
vpand T, T, G2
vpcmpeqq A3, PIVOT, T # A == P
vpunpckhqdq T, T, T
vpand T, T, G3
vpor E0, G0, G0
vpor E1, G1, G1
vpor E2, G2, G2
vpor E3, G3, G3
# Element is lesser-than-equal to pivot, if the top qword is lesser than pivot and
# or the top word is equal, and the bottom is lesser-than-equal
vmovmskpd G0_xmm, r0
vmovmskpd G1_xmm, r1
vmovmskpd G2_xmm, r2
vmovmskpd G3_xmm, r3
shl $5, r0
shl $5, r1
shl $5, r2
shl $5, r3
vmovdqa .LPermTableLeft(,r0), T
VPERMD A0, T, G0
vmovdqa .LPermTableLeft(,r1), T
VPERMD A1, T, G1
vmovdqa .LPermTableLeft(,r2), T
VPERMD A2, T, G2
vmovdqa .LPermTableLeft(,r3), T
VPERMD A3, T, G3
vmovdqa .LPermTableRight(,r0), T
VPERMD A0, T, A0
vmovdqa .LPermTableRight(,r1), T
VPERMD A1, T, A1
vmovdqa .LPermTableRight(,r2), T
VPERMD A2, T, A2
vmovdqa .LPermTableRight(,r2), T
VPERMD A3, T, A3
popcnt r0, r0
popcnt r1, r1
popcnt r2, r2
popcnt r3, r3
shl $4, r0
shl $4, r1
shl $4, r2
shl $4, r3
vmovdqu G0, (bottom)
add r0, bottom
vmovdqu G1, (bottom)
add r1, bottom
vmovdqu G2, (bottom)
add r2, bottom
vmovdqu G3, (bottom)
add r3, bottom
sub $32, r0
sub $32, r1
sub $32, r2
sub $32, r3
vmovdqu A0, (top)
sub r0, top
vmovdqu A1, (top)
sub r1, top
vmovdqu A2, (top)
sub r2, top
vmovdqu A3, (top)
sub r3, top
sub $8, n
add $8*16, array
jmp .Lx4_loop
.Lx4_loop_exit:
.Lx1_loop:
cmp $2, n
jl .Lx1_loop_exit
# Load the next 2 elements
vmovdqu 32*0(array), A0
# Find elements greater-than PIVOT
vpcmpgtq A0, PIVOT, G0 # P > A
# Find elements equal to PIVOT
# vpcmpeqq A0, PIVOT, E0 # A == P
# vpand E0, G0, G0
# Element is lesser-than-equal to pivot, if the top qword is lesser than pivot and
# or the top word is equal, and the bottom is lesser-than-equal
vmovmskpd G0_xmm, r0
shl $5, r0
vmovdqa .LPermTableLeft(,r0), T
VPERMD A0, T, G0
vmovdqa .LPermTableRight(,r0), T
VPERMD A0, T, A0
popcnt r0, r0
shl $4, r0
vmovdqu G0, (bottom)
add r0, bottom
sub $32, r0
vmovdqu A0, (top)
sub r0, top
sub $2, n
add $2*16, array
jmp .Lx1_loop
.Lx1_loop_exit:
.Ls_loop:
cmp $0, n
je .Ls_loop_exit
mov (array), %r8
cmp pivotALU, %r8
jg .Lgreater
mov %r8, (bottom)
add $16, bottom
add $16, array
dec n
jmp .Ls_loop
.Lgreater:
mov %r8, (top)
add $16, top
add $16, array
dec n
jmp .Ls_loop
.Ls_loop_exit:
mov pivotALU, (bottom)
add $16, bottom
sub temp_space, top
shr $4, top
mov top, %rax
.Lx4_copy_loop:
cmp $8, 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 $8, top
jmp .Lx4_copy_loop
.Lx4_copy_end:
.Lx1_copy_loop:
cmp $2, 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 $2, top
jmp .Lx1_copy_loop
.Lx1_copy_end:
cmp $0, top
je .Lcopy_end
mov (temp_space), %r8
mov 8(temp_space), %r9
mov %r8, (bottom)
mov %r9, 8(bottom)
lea 16(temp_space), temp_space
lea 16(bottom), bottom
sub $1, top
jmp .Lx1_copy_end
.Lcopy_end:
pop %rbp
pop %r15
pop %r14
pop %r13
pop %r12
pop n
mov n, %rax
shr $1, %rax
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
.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