forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_max_amd64.s
598 lines (522 loc) · 13.4 KB
/
page_max_amd64.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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//go:build !purego
#include "textflag.h"
// func maxInt32(data []int32) int32
TEXT ·maxInt32(SB), NOSPLIT, $-28
MOVQ data_base+0(FP), AX
MOVQ data_len+8(FP), CX
XORQ BX, BX
CMPQ CX, $0
JE done
XORQ SI, SI
MOVLQZX (AX), BX
CMPB ·hasAVX512VL(SB), $0
JE loop
CMPQ CX, $32
JB loop
MOVQ CX, DI
SHRQ $5, DI
SHLQ $5, DI
VPBROADCASTD (AX), Z0
loop32:
VMOVDQU32 (AX)(SI*4), Z1
VMOVDQU32 64(AX)(SI*4), Z2
VPMAXSD Z1, Z0, Z0
VPMAXSD Z2, Z0, Z0
ADDQ $32, SI
CMPQ SI, DI
JNE loop32
VMOVDQU32 swap32+0(SB), Z1
VPERMI2D Z0, Z0, Z1
VPMAXSD Y1, Y0, Y0
VMOVDQU32 swap32+32(SB), Y1
VPERMI2D Y0, Y0, Y1
VPMAXSD X1, X0, X0
VMOVDQU32 swap32+48(SB), X1
VPERMI2D X0, X0, X1
VPMAXSD X1, X0, X0
VZEROUPPER
MOVQ X0, DX
MOVL DX, BX
SHRQ $32, DX
CMPL DX, BX
CMOVLGT DX, BX
CMPQ SI, CX
JE done
loop:
MOVLQZX (AX)(SI*4), DX
CMPL DX, BX
CMOVLGT DX, BX
INCQ SI
CMPQ SI, CX
JNE loop
done:
MOVL BX, ret+24(FP)
RET
// func maxInt64(data []int64) int64
TEXT ·maxInt64(SB), NOSPLIT, $-32
MOVQ data_base+0(FP), AX
MOVQ data_len+8(FP), CX
XORQ BX, BX
CMPQ CX, $0
JE done
XORQ SI, SI
MOVQ (AX), BX
CMPB ·hasAVX512VL(SB), $0
JE loop
CMPQ CX, $32
JB loop
MOVQ CX, DI
SHRQ $5, DI
SHLQ $5, DI
VPBROADCASTQ (AX), Z0
loop32:
VMOVDQU64 (AX)(SI*8), Z1
VMOVDQU64 64(AX)(SI*8), Z2
VMOVDQU64 128(AX)(SI*8), Z3
VMOVDQU64 192(AX)(SI*8), Z4
VPMAXSQ Z1, Z2, Z5
VPMAXSQ Z3, Z4, Z6
VPMAXSQ Z5, Z6, Z1
VPMAXSQ Z1, Z0, Z0
ADDQ $32, SI
CMPQ SI, DI
JNE loop32
VMOVDQU32 swap32+0(SB), Z1
VPERMI2D Z0, Z0, Z1
VPMAXSQ Y1, Y0, Y0
VMOVDQU32 swap32+32(SB), Y1
VPERMI2D Y0, Y0, Y1
VPMAXSQ X1, X0, X0
VMOVDQU32 swap32+48(SB), X1
VPERMI2D X0, X0, X1
VPMAXSQ X1, X0, X0
VZEROUPPER
MOVQ X0, BX
CMPQ SI, CX
JE done
loop:
MOVQ (AX)(SI*8), DX
CMPQ DX, BX
CMOVQGT DX, BX
INCQ SI
CMPQ SI, CX
JNE loop
done:
MOVQ BX, ret+24(FP)
RET
// func maxUint32(data []int32) int32
TEXT ·maxUint32(SB), NOSPLIT, $-28
MOVQ data_base+0(FP), AX
MOVQ data_len+8(FP), CX
XORQ BX, BX
CMPQ CX, $0
JE done
XORQ SI, SI
MOVLQZX (AX), BX
CMPB ·hasAVX512VL(SB), $0
JE loop
CMPQ CX, $32
JB loop
MOVQ CX, DI
SHRQ $5, DI
SHLQ $5, DI
VPBROADCASTD (AX), Z0
loop32:
VMOVDQU32 (AX)(SI*4), Z1
VMOVDQU32 64(AX)(SI*4), Z2
VPMAXUD Z1, Z0, Z0
VPMAXUD Z2, Z0, Z0
ADDQ $32, SI
CMPQ SI, DI
JNE loop32
VMOVDQU32 swap32+0(SB), Z1
VPERMI2D Z0, Z0, Z1
VPMAXUD Y1, Y0, Y0
VMOVDQU32 swap32+32(SB), Y1
VPERMI2D Y0, Y0, Y1
VPMAXUD X1, X0, X0
VMOVDQU32 swap32+48(SB), X1
VPERMI2D X0, X0, X1
VPMAXUD X1, X0, X0
VZEROUPPER
MOVQ X0, DX
MOVL DX, BX
SHRQ $32, DX
CMPL DX, BX
CMOVLHI DX, BX
CMPQ SI, CX
JE done
loop:
MOVLQZX (AX)(SI*4), DX
CMPL DX, BX
CMOVLHI DX, BX
INCQ SI
CMPQ SI, CX
JNE loop
done:
MOVL BX, ret+24(FP)
RET
// func maxUint64(data []uint64) uint64
TEXT ·maxUint64(SB), NOSPLIT, $-32
MOVQ data_base+0(FP), AX
MOVQ data_len+8(FP), CX
XORQ BX, BX
CMPQ CX, $0
JE done
XORQ SI, SI
MOVQ (AX), BX
CMPB ·hasAVX512VL(SB), $0
JE loop
CMPQ CX, $32
JB loop
MOVQ CX, DI
SHRQ $5, DI
SHLQ $5, DI
VPBROADCASTQ (AX), Z0
loop32:
VMOVDQU64 (AX)(SI*8), Z1
VMOVDQU64 64(AX)(SI*8), Z2
VMOVDQU64 128(AX)(SI*8), Z3
VMOVDQU64 192(AX)(SI*8), Z4
VPMAXUQ Z1, Z2, Z5
VPMAXUQ Z3, Z4, Z6
VPMAXUQ Z5, Z6, Z1
VPMAXUQ Z1, Z0, Z0
ADDQ $32, SI
CMPQ SI, DI
JNE loop32
VMOVDQU32 swap32+0(SB), Z1
VPERMI2D Z0, Z0, Z1
VPMAXUQ Y1, Y0, Y0
VMOVDQU32 swap32+32(SB), Y1
VPERMI2D Y0, Y0, Y1
VPMAXUQ X1, X0, X0
VMOVDQU32 swap32+48(SB), X1
VPERMI2D X0, X0, X1
VPMAXUQ X1, X0, X0
VZEROUPPER
MOVQ X0, BX
CMPQ SI, CX
JE done
loop:
MOVQ (AX)(SI*8), DX
CMPQ DX, BX
CMOVQHI DX, BX
INCQ SI
CMPQ SI, CX
JNE loop
done:
MOVQ BX, ret+24(FP)
RET
// func maxFloat32(data []float32) float32
TEXT ·maxFloat32(SB), NOSPLIT, $-28
MOVQ data_base+0(FP), AX
MOVQ data_len+8(FP), CX
XORQ BX, BX
CMPQ CX, $0
JE done
XORPS X0, X0
XORPS X1, X1
XORQ SI, SI
MOVLQZX (AX), BX
MOVQ BX, X0
CMPB ·hasAVX512VL(SB), $0
JE loop
CMPQ CX, $64
JB loop
MOVQ CX, DI
SHRQ $6, DI
SHLQ $6, DI
VPBROADCASTD (AX), Z0
loop64:
VMOVDQU32 (AX)(SI*4), Z1
VMOVDQU32 64(AX)(SI*4), Z2
VMOVDQU32 128(AX)(SI*4), Z3
VMOVDQU32 192(AX)(SI*4), Z4
VMAXPS Z1, Z2, Z5
VMAXPS Z3, Z4, Z6
VMAXPS Z5, Z6, Z1
VMAXPS Z1, Z0, Z0
ADDQ $64, SI
CMPQ SI, DI
JNE loop64
VMOVDQU32 swap32+0(SB), Z1
VPERMI2D Z0, Z0, Z1
VMAXPS Y1, Y0, Y0
VMOVDQU32 swap32+32(SB), Y1
VPERMI2D Y0, Y0, Y1
VMAXPS X1, X0, X0
VMOVDQU32 swap32+48(SB), X1
VPERMI2D X0, X0, X1
VMAXPS X1, X0, X0
VZEROUPPER
MOVAPS X0, X1
PSRLQ $32, X1
MOVQ X0, BX
MOVQ X1, DX
UCOMISS X0, X1
CMOVLHI DX, BX
CMPQ SI, CX
JE done
MOVQ BX, X0
loop:
MOVLQZX (AX)(SI*4), DX
MOVQ DX, X1
UCOMISS X0, X1
CMOVLHI DX, BX
MOVQ BX, X0
INCQ SI
CMPQ SI, CX
JNE loop
done:
MOVL BX, ret+24(FP)
RET
// func maxFloat64(data []float64) float64
TEXT ·maxFloat64(SB), NOSPLIT, $-32
MOVQ data_base+0(FP), AX
MOVQ data_len+8(FP), CX
XORQ BX, BX
CMPQ CX, $0
JE done
XORPD X0, X0
XORPD X1, X1
XORQ SI, SI
MOVQ (AX), BX
MOVQ BX, X0
CMPB ·hasAVX512VL(SB), $0
JE loop
CMPQ CX, $32
JB loop
MOVQ CX, DI
SHRQ $5, DI
SHLQ $5, DI
VPBROADCASTQ (AX), Z0
loop32:
VMOVDQU64 (AX)(SI*8), Z1
VMOVDQU64 64(AX)(SI*8), Z2
VMOVDQU64 128(AX)(SI*8), Z3
VMOVDQU64 192(AX)(SI*8), Z4
VMAXPD Z1, Z2, Z5
VMAXPD Z3, Z4, Z6
VMAXPD Z5, Z6, Z1
VMAXPD Z1, Z0, Z0
ADDQ $32, SI
CMPQ SI, DI
JNE loop32
VMOVDQU64 swap32+0(SB), Z1
VPERMI2D Z0, Z0, Z1
VMAXPD Y1, Y0, Y0
VMOVDQU64 swap32+32(SB), Y1
VPERMI2D Y0, Y0, Y1
VMAXPD X1, X0, X0
VMOVDQU64 swap32+48(SB), X1
VPERMI2D X0, X0, X1
VMAXPD X1, X0, X0
VZEROUPPER
MOVQ X0, BX
CMPQ SI, CX
JE done
loop:
MOVQ (AX)(SI*8), DX
MOVQ DX, X1
UCOMISD X0, X1
CMOVQHI DX, BX
MOVQ BX, X0
INCQ SI
CMPQ SI, CX
JNE loop
done:
MOVQ BX, ret+24(FP)
RET
// vpmaxu128 is a macro comparing unsigned 128 bits values held in the
// `srcValues` and `maxValues` vectors. The `srcIndexes` and `maxIndexes`
// vectors contain the indexes of elements in the value vectors. Remaining
// K and R arguments are mask and general purpose registers needed to hold
// temporary values during the computation. The last M argument is a mask
// generated by vpmaxu128mask.
//
// The routine uses AVX-512 instructions (VPCMPUQ, VPBLENDMQ) to implement
// the comparison of 128 bits values. The values are expected to be stored
// in the vectors as a little-endian pair of two consecutive quad words.
//
// The results are written to the `maxValues` and `maxIndexes` vectors,
// overwriting the inputs. `srcValues` and `srcIndexes` are read-only
// parameters.
//
// At a high level, for two pairs of quad words formaxg two 128 bits values
// A and B, the test implemented by this macro is:
//
// A[1] > B[1] || (A[1] == B[1] && A[0] > B[0])
//
// Values in the source vector that evaluate to true on this expression are
// written to the vector of maximum values, and their indexes are written to
// the vector of indexes.
#define vpmaxu128(srcValues, srcIndexes, maxValues, maxIndexes, K1, K2, R1, R2, R3, M) \
VPCMPUQ $0, maxValues, srcValues, K1 \
VPCMPUQ $6, maxValues, srcValues, K2 \
KMOVB K1, R1 \
KMOVB K2, R2 \
MOVB R2, R3 \
SHLB $1, R3 \
ANDB R3, R1 \
ORB R2, R1 \
ANDB M, R1 \
MOVB R1, R2 \
SHRB $1, R2 \
ORB R2, R1 \
KMOVB R1, K1 \
VPBLENDMQ srcValues, maxValues, K1, maxValues \
VPBLENDMQ srcIndexes, maxIndexes, K1, maxIndexes
// vpmaxu128mask is a macro used to initialize the mask passed as last argument
// to vpmaxu128. The argument M is intended to be a general purpose register.
//
// The bit mask is used to merge the results of the "greater than" and "equal"
// comparison that are performed on each lane of maximum vectors. The upper bits
// are used to compute results of the operation to determine which of the pairs
// of quad words representing the 128 bits elements are the maximums.
#define vpmaxu128mask(M) MOVB $0b10101010, M
// func maxBE128(data [][16]byte) []byte
TEXT ·maxBE128(SB), NOSPLIT, $-48
MOVQ data_base+0(FP), AX
MOVQ data_len+8(FP), CX
CMPQ CX, $0
JE null
SHLQ $4, CX
MOVQ CX, DX // len
MOVQ AX, BX // max
ADDQ AX, CX // end
CMPQ DX, $256
JB loop
CMPB ·hasAVX512MinMaxBE128(SB), $0
JE loop
// Z19 holds a vector of the count by which we increment the vectors of
// swap at each loop iteration.
MOVQ $16, DI
VPBROADCASTQ DI, Z19
// Z31 holds the shuffle mask used to convert 128 bits elements from big to
// little endian so we can apply vectorized comparison instructions.
VMOVDQU64 bswap128(SB), Z31
// These vectors hold four lanes of maximum values found in the input.
VBROADCASTI64X2 (AX), Z0
VPSHUFB Z31, Z0, Z0
VMOVDQU64 Z0, Z5
VMOVDQU64 Z0, Z10
VMOVDQU64 Z0, Z15
// These vectors hold four lanes of swap of maximum values.
//
// We initialize them at zero because we broadcast the first value of the
// input in the vectors that track the maximums of each lane; in other
// words, we assume the maximum value is at the first offset and work our
// way up from there.
VPXORQ Z2, Z2, Z2
VPXORQ Z7, Z7, Z7
VPXORQ Z12, Z12, Z12
VPXORQ Z17, Z17, Z17
// These vectors are used to compute the swap of maximum values held
// in [Z1, Z5, Z10, Z15]. Each vector holds a contiguous sequence of
// swap; for example, Z3 is initialized with [0, 1, 2, 3]. At each
// loop iteration, the swap are incremented by the number of elements
// consumed from the input (4x4=16).
VMOVDQU64 indexes128(SB), Z3
VPXORQ Z8, Z8, Z8
VPXORQ Z13, Z13, Z13
VPXORQ Z18, Z18, Z18
MOVQ $4, DI
VPBROADCASTQ DI, Z1
VPADDQ Z1, Z3, Z8
VPADDQ Z1, Z8, Z13
VPADDQ Z1, Z13, Z18
// This bit mask is used to merge the results of the "less than" and "equal"
// comparison that we perform on each lane of maximum vectors. We use the
// upper bits to compute four results of the operation which determines
// which of the pair of quad words representing the 128 bits elements is the
// maximum.
vpmaxu128mask(DI)
SHRQ $8, DX
SHLQ $8, DX
ADDQ AX, DX
loop16:
// Compute 4x4 maximum values in vector registers, along with their swap
// in the input array.
VMOVDQU64 (AX), Z1
VMOVDQU64 64(AX), Z6
VMOVDQU64 128(AX), Z11
VMOVDQU64 192(AX), Z16
VPSHUFB Z31, Z1, Z1
VPSHUFB Z31, Z6, Z6
VPSHUFB Z31, Z11, Z11
VPSHUFB Z31, Z16, Z16
vpmaxu128(Z1, Z3, Z0, Z2, K1, K2, R8, R9, R10, DI)
vpmaxu128(Z6, Z8, Z5, Z7, K3, K4, R11, R12, R13, DI)
vpmaxu128(Z11, Z13, Z10, Z12, K1, K2, R8, R9, R10, DI)
vpmaxu128(Z16, Z18, Z15, Z17, K3, K4, R11, R12, R13, DI)
VPADDQ Z19, Z3, Z3
VPADDQ Z19, Z8, Z8
VPADDQ Z19, Z13, Z13
VPADDQ Z19, Z18, Z18
ADDQ $256, AX
CMPQ AX, DX
JB loop16
// After the loop completed, we need to merge the lanes that each contain
// 4 maximum values (so 16 total candidate at this stage). The results are
// reduced into 4 candidates in Z0, with their swap in Z2.
vpmaxu128(Z10, Z12, Z0, Z2, K1, K2, R8, R9, R10, DI)
vpmaxu128(Z15, Z17, Z5, Z7, K3, K4, R11, R12, R13, DI)
vpmaxu128(Z5, Z7, Z0, Z2, K1, K2, R8, R9, R10, DI)
// Further reduce the results by swapping the upper and lower parts of the
// vector registers, and comparing them to determaxe which values are the
// smallest. We compare 2x2 values at this step, then 2x1 values at the next
// to find the index of the maximum.
VMOVDQU64 swap64+0(SB), Z1
VMOVDQU64 swap64+0(SB), Z3
VPERMI2Q Z0, Z0, Z1
VPERMI2Q Z2, Z2, Z3
vpmaxu128(Y1, Y3, Y0, Y2, K1, K2, R8, R9, R10, DI)
VMOVDQU64 swap64+32(SB), Y1
VMOVDQU64 swap64+32(SB), Y3
VPERMI2Q Y0, Y0, Y1
VPERMI2Q Y2, Y2, Y3
vpmaxu128(X1, X3, X0, X2, K1, K2, R8, R9, R10, DI)
VZEROUPPER
// Extract the index of the maximum value computed in the lower 64 bits of
// X2 and position the BX pointer at the index of the maximum value.
MOVQ X2, DX
SHLQ $4, DX
ADDQ DX, BX
CMPQ AX, CX
JE done
// Unless the input was aligned on 256 bytes, we need to perform a few more
// iterations on the remaining elements.
//
// This loop is also taken if the CPU has no support for AVX-512.
loop:
MOVQ (AX), R8
MOVQ (BX), R9
BSWAPQ R8
BSWAPQ R9
CMPQ R8, R9
JA more
JB next
MOVQ 8(AX), R8
MOVQ 8(BX), R9
BSWAPQ R8
BSWAPQ R9
CMPQ R8, R9
JAE next
more:
MOVQ AX, BX
next:
ADDQ $16, AX
CMPQ AX, CX
JB loop
done:
MOVQ BX, ret_base+24(FP)
MOVQ $16, ret_len+32(FP)
MOVQ $16, ret_cap+40(FP)
RET
null:
XORQ BX, BX
MOVQ BX, ret_base+24(FP)
MOVQ BX, ret_len+32(FP)
MOVQ BX, ret_cap+40(FP)
RET