-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.cpp
638 lines (577 loc) · 21.1 KB
/
QuickSort.cpp
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#include "QuickSort.h"
#include "iostream"
#include <immintrin.h>
#include <chrono>
#include <cassert>
#include <random>
#include <intrin.h>
using namespace std;
// pointer to the helper array
int* _temp;
// start pointer position of the helper array
int* _TEMPSTART;
// end pointer position of the helper array
int* _TEMPEND;
// N_OFFSET is the amount of how many registers can do calculations at the same time in AVX
// Since we are working anyway with AVX2, we always have 256 bit registers. We also work here with int, that's why devide it here by 32 bit!
//
// In C#: "var N = Vector256<T>.Count"
// Vector256<T>.Count is dependent on the provided CPU architecture.
// Let's say we use Vector256<float>.Count. Float is 64 bit, and we have a CPU that has AVX2 with 256 bit. '.Count()' function will
// now give 4, since 64 fits 4 times in 256(!). In older architecture there might be possibilty that it only fits for example 2 times.
// With this "N" offset, we can always jump to the next array entry by this offset value!
const int N_OFFSET = 256 / 32;
// Constructor for initialzing the default values
QuickSort::QuickSort()
{
// 96 bytes = 3 x 8 integers!
// 3 times => 2 x 8 integers free space for each side of the data array,
// 1 x 8 integers for handling remaining values in data array
_temp = new int[3 * N_OFFSET];
_TEMPSTART = _temp;
_TEMPEND = _temp + 3 * N_OFFSET;
}
// swap function
void QuickSort::qsSwap(std::vector<int>& data, uint64_t i, uint64_t j)
{
uint64_t temp = data[i];
data[i] = data[j];
data[j] = temp;
}
// insertion function for the CUT_OFF / THRESHOLD part, same code as in algd1
void QuickSort::insertionSort(std::vector<int>& data, uint64_t beg, uint64_t end)
{
for (size_t i = beg + 1; i <= end; i++)
{
size_t j = i;
while (j > beg && data[j] < data[j - 1])
{
qsSwap(data, j, j - 1);
j--;
}
}
}
// Partition function, same code as in algd1
void QuickSort::qsPartition(std::vector<int>& data, uint64_t beg, uint64_t end)
{
uint64_t i = beg;
uint64_t j = end;
uint64_t pivot = (i + j) >> 1;
while (i <= j)
{
while (data[i] < data[pivot])
{
i++;
}
while (data[pivot] < data[j])
{
j--;
}
if (i <= j)
{
if (i == pivot)
{
pivot = j;
}
else if (j == pivot)
{
pivot = i;
}
qsSwap(data, i, j);
i++;
j--;
}
}
qsSort(data, beg, j);
qsSort(data, i, end);
}
// QuickSort recursive function with CUT_OFF / THRESHOLD
void QuickSort::qsSort(std::vector<int>& data, uint64_t beg, uint64_t end)
{
if (end - beg >= CUT_OFF)
{
qsPartition(data, beg, end);
}
else
{
insertionSort(data, beg, end);
}
}
// Pre-Generated Permutation table
// 8 * 4 byte = 32 bytes per element
static const int PermTablePtr[] = {
0, 1, 2, 3, 4, 5, 6, 7, // 0b00000000 (0)
1, 2, 3, 4, 5, 6, 7, 0, // 0b00000001 (1)
0, 2, 3, 4, 5, 6, 7, 1, // 0b00000010 (2)
2, 3, 4, 5, 6, 7, 0, 1, // 0b00000011 (3)
0, 1, 3, 4, 5, 6, 7, 2, // 0b00000100 (4)
1, 3, 4, 5, 6, 7, 0, 2, // 0b00000101 (5)
0, 3, 4, 5, 6, 7, 1, 2, // 0b00000110 (6)
3, 4, 5, 6, 7, 0, 1, 2, // 0b00000111 (7)
0, 1, 2, 4, 5, 6, 7, 3, // 0b00001000 (8)
1, 2, 4, 5, 6, 7, 0, 3, // 0b00001001 (9)
0, 2, 4, 5, 6, 7, 1, 3, // 0b00001010 (10)
2, 4, 5, 6, 7, 0, 1, 3, // 0b00001011 (11)
0, 1, 4, 5, 6, 7, 2, 3, // 0b00001100 (12)
1, 4, 5, 6, 7, 0, 2, 3, // 0b00001101 (13)
0, 4, 5, 6, 7, 1, 2, 3, // 0b00001110 (14)
4, 5, 6, 7, 0, 1, 2, 3, // 0b00001111 (15)
0, 1, 2, 3, 5, 6, 7, 4, // 0b00010000 (16)
1, 2, 3, 5, 6, 7, 0, 4, // 0b00010001 (17)
0, 2, 3, 5, 6, 7, 1, 4, // 0b00010010 (18)
2, 3, 5, 6, 7, 0, 1, 4, // 0b00010011 (19)
0, 1, 3, 5, 6, 7, 2, 4, // 0b00010100 (20)
1, 3, 5, 6, 7, 0, 2, 4, // 0b00010101 (21)
0, 3, 5, 6, 7, 1, 2, 4, // 0b00010110 (22)
3, 5, 6, 7, 0, 1, 2, 4, // 0b00010111 (23)
0, 1, 2, 5, 6, 7, 3, 4, // 0b00011000 (24)
1, 2, 5, 6, 7, 0, 3, 4, // 0b00011001 (25)
0, 2, 5, 6, 7, 1, 3, 4, // 0b00011010 (26)
2, 5, 6, 7, 0, 1, 3, 4, // 0b00011011 (27)
0, 1, 5, 6, 7, 2, 3, 4, // 0b00011100 (28)
1, 5, 6, 7, 0, 2, 3, 4, // 0b00011101 (29)
0, 5, 6, 7, 1, 2, 3, 4, // 0b00011110 (30)
5, 6, 7, 0, 1, 2, 3, 4, // 0b00011111 (31)
0, 1, 2, 3, 4, 6, 7, 5, // 0b00100000 (32)
1, 2, 3, 4, 6, 7, 0, 5, // 0b00100001 (33)
0, 2, 3, 4, 6, 7, 1, 5, // 0b00100010 (34)
2, 3, 4, 6, 7, 0, 1, 5, // 0b00100011 (35)
0, 1, 3, 4, 6, 7, 2, 5, // 0b00100100 (36)
1, 3, 4, 6, 7, 0, 2, 5, // 0b00100101 (37)
0, 3, 4, 6, 7, 1, 2, 5, // 0b00100110 (38)
3, 4, 6, 7, 0, 1, 2, 5, // 0b00100111 (39)
0, 1, 2, 4, 6, 7, 3, 5, // 0b00101000 (40)
1, 2, 4, 6, 7, 0, 3, 5, // 0b00101001 (41)
0, 2, 4, 6, 7, 1, 3, 5, // 0b00101010 (42)
2, 4, 6, 7, 0, 1, 3, 5, // 0b00101011 (43)
0, 1, 4, 6, 7, 2, 3, 5, // 0b00101100 (44)
1, 4, 6, 7, 0, 2, 3, 5, // 0b00101101 (45)
0, 4, 6, 7, 1, 2, 3, 5, // 0b00101110 (46)
4, 6, 7, 0, 1, 2, 3, 5, // 0b00101111 (47)
0, 1, 2, 3, 6, 7, 4, 5, // 0b00110000 (48)
1, 2, 3, 6, 7, 0, 4, 5, // 0b00110001 (49)
0, 2, 3, 6, 7, 1, 4, 5, // 0b00110010 (50)
2, 3, 6, 7, 0, 1, 4, 5, // 0b00110011 (51)
0, 1, 3, 6, 7, 2, 4, 5, // 0b00110100 (52)
1, 3, 6, 7, 0, 2, 4, 5, // 0b00110101 (53)
0, 3, 6, 7, 1, 2, 4, 5, // 0b00110110 (54)
3, 6, 7, 0, 1, 2, 4, 5, // 0b00110111 (55)
0, 1, 2, 6, 7, 3, 4, 5, // 0b00111000 (56)
1, 2, 6, 7, 0, 3, 4, 5, // 0b00111001 (57)
0, 2, 6, 7, 1, 3, 4, 5, // 0b00111010 (58)
2, 6, 7, 0, 1, 3, 4, 5, // 0b00111011 (59)
0, 1, 6, 7, 2, 3, 4, 5, // 0b00111100 (60)
1, 6, 7, 0, 2, 3, 4, 5, // 0b00111101 (61)
0, 6, 7, 1, 2, 3, 4, 5, // 0b00111110 (62)
6, 7, 0, 1, 2, 3, 4, 5, // 0b00111111 (63)
0, 1, 2, 3, 4, 5, 7, 6, // 0b01000000 (64)
1, 2, 3, 4, 5, 7, 0, 6, // 0b01000001 (65)
0, 2, 3, 4, 5, 7, 1, 6, // 0b01000010 (66)
2, 3, 4, 5, 7, 0, 1, 6, // 0b01000011 (67)
0, 1, 3, 4, 5, 7, 2, 6, // 0b01000100 (68)
1, 3, 4, 5, 7, 0, 2, 6, // 0b01000101 (69)
0, 3, 4, 5, 7, 1, 2, 6, // 0b01000110 (70)
3, 4, 5, 7, 0, 1, 2, 6, // 0b01000111 (71)
0, 1, 2, 4, 5, 7, 3, 6, // 0b01001000 (72)
1, 2, 4, 5, 7, 0, 3, 6, // 0b01001001 (73)
0, 2, 4, 5, 7, 1, 3, 6, // 0b01001010 (74)
2, 4, 5, 7, 0, 1, 3, 6, // 0b01001011 (75)
0, 1, 4, 5, 7, 2, 3, 6, // 0b01001100 (76)
1, 4, 5, 7, 0, 2, 3, 6, // 0b01001101 (77)
0, 4, 5, 7, 1, 2, 3, 6, // 0b01001110 (78)
4, 5, 7, 0, 1, 2, 3, 6, // 0b01001111 (79)
0, 1, 2, 3, 5, 7, 4, 6, // 0b01010000 (80)
1, 2, 3, 5, 7, 0, 4, 6, // 0b01010001 (81)
0, 2, 3, 5, 7, 1, 4, 6, // 0b01010010 (82)
2, 3, 5, 7, 0, 1, 4, 6, // 0b01010011 (83)
0, 1, 3, 5, 7, 2, 4, 6, // 0b01010100 (84)
1, 3, 5, 7, 0, 2, 4, 6, // 0b01010101 (85)
0, 3, 5, 7, 1, 2, 4, 6, // 0b01010110 (86)
3, 5, 7, 0, 1, 2, 4, 6, // 0b01010111 (87)
0, 1, 2, 5, 7, 3, 4, 6, // 0b01011000 (88)
1, 2, 5, 7, 0, 3, 4, 6, // 0b01011001 (89)
0, 2, 5, 7, 1, 3, 4, 6, // 0b01011010 (90)
2, 5, 7, 0, 1, 3, 4, 6, // 0b01011011 (91)
0, 1, 5, 7, 2, 3, 4, 6, // 0b01011100 (92)
1, 5, 7, 0, 2, 3, 4, 6, // 0b01011101 (93)
0, 5, 7, 1, 2, 3, 4, 6, // 0b01011110 (94)
5, 7, 0, 1, 2, 3, 4, 6, // 0b01011111 (95)
0, 1, 2, 3, 4, 7, 5, 6, // 0b01100000 (96)
1, 2, 3, 4, 7, 0, 5, 6, // 0b01100001 (97)
0, 2, 3, 4, 7, 1, 5, 6, // 0b01100010 (98)
2, 3, 4, 7, 0, 1, 5, 6, // 0b01100011 (99)
0, 1, 3, 4, 7, 2, 5, 6, // 0b01100100 (100)
1, 3, 4, 7, 0, 2, 5, 6, // 0b01100101 (101)
0, 3, 4, 7, 1, 2, 5, 6, // 0b01100110 (102)
3, 4, 7, 0, 1, 2, 5, 6, // 0b01100111 (103)
0, 1, 2, 4, 7, 3, 5, 6, // 0b01101000 (104)
1, 2, 4, 7, 0, 3, 5, 6, // 0b01101001 (105)
0, 2, 4, 7, 1, 3, 5, 6, // 0b01101010 (106)
2, 4, 7, 0, 1, 3, 5, 6, // 0b01101011 (107)
0, 1, 4, 7, 2, 3, 5, 6, // 0b01101100 (108)
1, 4, 7, 0, 2, 3, 5, 6, // 0b01101101 (109)
0, 4, 7, 1, 2, 3, 5, 6, // 0b01101110 (110)
4, 7, 0, 1, 2, 3, 5, 6, // 0b01101111 (111)
0, 1, 2, 3, 7, 4, 5, 6, // 0b01110000 (112)
1, 2, 3, 7, 0, 4, 5, 6, // 0b01110001 (113)
0, 2, 3, 7, 1, 4, 5, 6, // 0b01110010 (114)
2, 3, 7, 0, 1, 4, 5, 6, // 0b01110011 (115)
0, 1, 3, 7, 2, 4, 5, 6, // 0b01110100 (116)
1, 3, 7, 0, 2, 4, 5, 6, // 0b01110101 (117)
0, 3, 7, 1, 2, 4, 5, 6, // 0b01110110 (118)
3, 7, 0, 1, 2, 4, 5, 6, // 0b01110111 (119)
0, 1, 2, 7, 3, 4, 5, 6, // 0b01111000 (120)
1, 2, 7, 0, 3, 4, 5, 6, // 0b01111001 (121)
0, 2, 7, 1, 3, 4, 5, 6, // 0b01111010 (122)
2, 7, 0, 1, 3, 4, 5, 6, // 0b01111011 (123)
0, 1, 7, 2, 3, 4, 5, 6, // 0b01111100 (124)
1, 7, 0, 2, 3, 4, 5, 6, // 0b01111101 (125)
0, 7, 1, 2, 3, 4, 5, 6, // 0b01111110 (126)
7, 0, 1, 2, 3, 4, 5, 6, // 0b01111111 (127)
0, 1, 2, 3, 4, 5, 6, 7, // 0b10000000 (128)
1, 2, 3, 4, 5, 6, 0, 7, // 0b10000001 (129)
0, 2, 3, 4, 5, 6, 1, 7, // 0b10000010 (130)
2, 3, 4, 5, 6, 0, 1, 7, // 0b10000011 (131)
0, 1, 3, 4, 5, 6, 2, 7, // 0b10000100 (132)
1, 3, 4, 5, 6, 0, 2, 7, // 0b10000101 (133)
0, 3, 4, 5, 6, 1, 2, 7, // 0b10000110 (134)
3, 4, 5, 6, 0, 1, 2, 7, // 0b10000111 (135)
0, 1, 2, 4, 5, 6, 3, 7, // 0b10001000 (136)
1, 2, 4, 5, 6, 0, 3, 7, // 0b10001001 (137)
0, 2, 4, 5, 6, 1, 3, 7, // 0b10001010 (138)
2, 4, 5, 6, 0, 1, 3, 7, // 0b10001011 (139)
0, 1, 4, 5, 6, 2, 3, 7, // 0b10001100 (140)
1, 4, 5, 6, 0, 2, 3, 7, // 0b10001101 (141)
0, 4, 5, 6, 1, 2, 3, 7, // 0b10001110 (142)
4, 5, 6, 0, 1, 2, 3, 7, // 0b10001111 (143)
0, 1, 2, 3, 5, 6, 4, 7, // 0b10010000 (144)
1, 2, 3, 5, 6, 0, 4, 7, // 0b10010001 (145)
0, 2, 3, 5, 6, 1, 4, 7, // 0b10010010 (146)
2, 3, 5, 6, 0, 1, 4, 7, // 0b10010011 (147)
0, 1, 3, 5, 6, 2, 4, 7, // 0b10010100 (148)
1, 3, 5, 6, 0, 2, 4, 7, // 0b10010101 (149)
0, 3, 5, 6, 1, 2, 4, 7, // 0b10010110 (150)
3, 5, 6, 0, 1, 2, 4, 7, // 0b10010111 (151)
0, 1, 2, 5, 6, 3, 4, 7, // 0b10011000 (152)
1, 2, 5, 6, 0, 3, 4, 7, // 0b10011001 (153)
0, 2, 5, 6, 1, 3, 4, 7, // 0b10011010 (154)
2, 5, 6, 0, 1, 3, 4, 7, // 0b10011011 (155)
0, 1, 5, 6, 2, 3, 4, 7, // 0b10011100 (156)
1, 5, 6, 0, 2, 3, 4, 7, // 0b10011101 (157)
0, 5, 6, 1, 2, 3, 4, 7, // 0b10011110 (158)
5, 6, 0, 1, 2, 3, 4, 7, // 0b10011111 (159)
0, 1, 2, 3, 4, 6, 5, 7, // 0b10100000 (160)
1, 2, 3, 4, 6, 0, 5, 7, // 0b10100001 (161)
0, 2, 3, 4, 6, 1, 5, 7, // 0b10100010 (162)
2, 3, 4, 6, 0, 1, 5, 7, // 0b10100011 (163)
0, 1, 3, 4, 6, 2, 5, 7, // 0b10100100 (164)
1, 3, 4, 6, 0, 2, 5, 7, // 0b10100101 (165)
0, 3, 4, 6, 1, 2, 5, 7, // 0b10100110 (166)
3, 4, 6, 0, 1, 2, 5, 7, // 0b10100111 (167)
0, 1, 2, 4, 6, 3, 5, 7, // 0b10101000 (168)
1, 2, 4, 6, 0, 3, 5, 7, // 0b10101001 (169)
0, 2, 4, 6, 1, 3, 5, 7, // 0b10101010 (170)
2, 4, 6, 0, 1, 3, 5, 7, // 0b10101011 (171)
0, 1, 4, 6, 2, 3, 5, 7, // 0b10101100 (172)
1, 4, 6, 0, 2, 3, 5, 7, // 0b10101101 (173)
0, 4, 6, 1, 2, 3, 5, 7, // 0b10101110 (174)
4, 6, 0, 1, 2, 3, 5, 7, // 0b10101111 (175)
0, 1, 2, 3, 6, 4, 5, 7, // 0b10110000 (176)
1, 2, 3, 6, 0, 4, 5, 7, // 0b10110001 (177)
0, 2, 3, 6, 1, 4, 5, 7, // 0b10110010 (178)
2, 3, 6, 0, 1, 4, 5, 7, // 0b10110011 (179)
0, 1, 3, 6, 2, 4, 5, 7, // 0b10110100 (180)
1, 3, 6, 0, 2, 4, 5, 7, // 0b10110101 (181)
0, 3, 6, 1, 2, 4, 5, 7, // 0b10110110 (182)
3, 6, 0, 1, 2, 4, 5, 7, // 0b10110111 (183)
0, 1, 2, 6, 3, 4, 5, 7, // 0b10111000 (184)
1, 2, 6, 0, 3, 4, 5, 7, // 0b10111001 (185)
0, 2, 6, 1, 3, 4, 5, 7, // 0b10111010 (186)
2, 6, 0, 1, 3, 4, 5, 7, // 0b10111011 (187)
0, 1, 6, 2, 3, 4, 5, 7, // 0b10111100 (188)
1, 6, 0, 2, 3, 4, 5, 7, // 0b10111101 (189)
0, 6, 1, 2, 3, 4, 5, 7, // 0b10111110 (190)
6, 0, 1, 2, 3, 4, 5, 7, // 0b10111111 (191)
0, 1, 2, 3, 4, 5, 6, 7, // 0b11000000 (192)
1, 2, 3, 4, 5, 0, 6, 7, // 0b11000001 (193)
0, 2, 3, 4, 5, 1, 6, 7, // 0b11000010 (194)
2, 3, 4, 5, 0, 1, 6, 7, // 0b11000011 (195)
0, 1, 3, 4, 5, 2, 6, 7, // 0b11000100 (196)
1, 3, 4, 5, 0, 2, 6, 7, // 0b11000101 (197)
0, 3, 4, 5, 1, 2, 6, 7, // 0b11000110 (198)
3, 4, 5, 0, 1, 2, 6, 7, // 0b11000111 (199)
0, 1, 2, 4, 5, 3, 6, 7, // 0b11001000 (200)
1, 2, 4, 5, 0, 3, 6, 7, // 0b11001001 (201)
0, 2, 4, 5, 1, 3, 6, 7, // 0b11001010 (202)
2, 4, 5, 0, 1, 3, 6, 7, // 0b11001011 (203)
0, 1, 4, 5, 2, 3, 6, 7, // 0b11001100 (204)
1, 4, 5, 0, 2, 3, 6, 7, // 0b11001101 (205)
0, 4, 5, 1, 2, 3, 6, 7, // 0b11001110 (206)
4, 5, 0, 1, 2, 3, 6, 7, // 0b11001111 (207)
0, 1, 2, 3, 5, 4, 6, 7, // 0b11010000 (208)
1, 2, 3, 5, 0, 4, 6, 7, // 0b11010001 (209)
0, 2, 3, 5, 1, 4, 6, 7, // 0b11010010 (210)
2, 3, 5, 0, 1, 4, 6, 7, // 0b11010011 (211)
0, 1, 3, 5, 2, 4, 6, 7, // 0b11010100 (212)
1, 3, 5, 0, 2, 4, 6, 7, // 0b11010101 (213)
0, 3, 5, 1, 2, 4, 6, 7, // 0b11010110 (214)
3, 5, 0, 1, 2, 4, 6, 7, // 0b11010111 (215)
0, 1, 2, 5, 3, 4, 6, 7, // 0b11011000 (216)
1, 2, 5, 0, 3, 4, 6, 7, // 0b11011001 (217)
0, 2, 5, 1, 3, 4, 6, 7, // 0b11011010 (218)
2, 5, 0, 1, 3, 4, 6, 7, // 0b11011011 (219)
0, 1, 5, 2, 3, 4, 6, 7, // 0b11011100 (220)
1, 5, 0, 2, 3, 4, 6, 7, // 0b11011101 (221)
0, 5, 1, 2, 3, 4, 6, 7, // 0b11011110 (222)
5, 0, 1, 2, 3, 4, 6, 7, // 0b11011111 (223)
0, 1, 2, 3, 4, 5, 6, 7, // 0b11100000 (224)
1, 2, 3, 4, 0, 5, 6, 7, // 0b11100001 (225)
0, 2, 3, 4, 1, 5, 6, 7, // 0b11100010 (226)
2, 3, 4, 0, 1, 5, 6, 7, // 0b11100011 (227)
0, 1, 3, 4, 2, 5, 6, 7, // 0b11100100 (228)
1, 3, 4, 0, 2, 5, 6, 7, // 0b11100101 (229)
0, 3, 4, 1, 2, 5, 6, 7, // 0b11100110 (230)
3, 4, 0, 1, 2, 5, 6, 7, // 0b11100111 (231)
0, 1, 2, 4, 3, 5, 6, 7, // 0b11101000 (232)
1, 2, 4, 0, 3, 5, 6, 7, // 0b11101001 (233)
0, 2, 4, 1, 3, 5, 6, 7, // 0b11101010 (234)
2, 4, 0, 1, 3, 5, 6, 7, // 0b11101011 (235)
0, 1, 4, 2, 3, 5, 6, 7, // 0b11101100 (236)
1, 4, 0, 2, 3, 5, 6, 7, // 0b11101101 (237)
0, 4, 1, 2, 3, 5, 6, 7, // 0b11101110 (238)
4, 0, 1, 2, 3, 5, 6, 7, // 0b11101111 (239)
0, 1, 2, 3, 4, 5, 6, 7, // 0b11110000 (240)
1, 2, 3, 0, 4, 5, 6, 7, // 0b11110001 (241)
0, 2, 3, 1, 4, 5, 6, 7, // 0b11110010 (242)
2, 3, 0, 1, 4, 5, 6, 7, // 0b11110011 (243)
0, 1, 3, 2, 4, 5, 6, 7, // 0b11110100 (244)
1, 3, 0, 2, 4, 5, 6, 7, // 0b11110101 (245)
0, 3, 1, 2, 4, 5, 6, 7, // 0b11110110 (246)
3, 0, 1, 2, 4, 5, 6, 7, // 0b11110111 (247)
0, 1, 2, 3, 4, 5, 6, 7, // 0b11111000 (248)
1, 2, 0, 3, 4, 5, 6, 7, // 0b11111001 (249)
0, 2, 1, 3, 4, 5, 6, 7, // 0b11111010 (250)
2, 0, 1, 3, 4, 5, 6, 7, // 0b11111011 (251)
0, 1, 2, 3, 4, 5, 6, 7, // 0b11111100 (252)
1, 0, 2, 3, 4, 5, 6, 7, // 0b11111101 (253)
0, 1, 2, 3, 4, 5, 6, 7, // 0b11111110 (254)
0, 1, 2, 3, 4, 5, 6, 7, // 0b11111111 (255)
};
// Full 8-element partitioning block!
void QuickSort::avxPartitionBlock(int* dataPtr, __m256i pivots, int*& writeLeft, int*& writeRight)
{
// cast writeLeft and writeRight into __m256i pointers
__m256i* writeLeftPtr = (__m256i*) writeLeft;
__m256i* writeRightPtr = (__m256i*) writeRight;
// read first 8-element from the data starting at the dataPtr
__m256i data = _mm256_lddqu_si256((__m256i*) dataPtr);
// compare data with the pivots values
__m256i cmpData = _mm256_cmpgt_epi32(data, pivots);
// create mask based on the cmpData
int mask = _mm256_movemask_ps(*(__m256*) & cmpData);
// get the corresponding permutation values depending on the mask.
// 8 times, since we are working with int ( 8 * 4 byte = 32 bytes)
__m256i permData = _mm256_lddqu_si256((__m256i*) (PermTablePtr + (mask * 8)));
// Permute data based on the permutation table
data = _mm256_permutevar8x32_epi32(data, permData);
// store data at the pointers postion from writeLeft and writeRight
_mm256_storeu_si256(writeLeftPtr, data);
_mm256_storeu_si256(writeRightPtr, data);
// pop count
int pc = _mm_popcnt_u32(mask);
writeRight -= pc;
writeLeft += 8 - pc;
}
// AVX QuickSort function, partition in-place
int* QuickSort::avxVectorizedPartitionInPlace(int* left, int* right)
{
// define pointers positions
auto writeLeft = left;
auto writeRight = right - N_OFFSET;
auto tmpLeft = _TEMPSTART;
auto tmpRight = _TEMPEND - N_OFFSET;
// get the pivot element which is always at the right side of the handled partition chunk
auto pivot = *right;
// broadcast pivot into a 8-elements of the same value
__m256i p = _mm256_broadcastd_epi32(*(__m128i*) & pivot);
// Make some room in the data array by doing partition and copy these values into the helperarray "_temp"
avxPartitionBlock(left, p, tmpLeft, tmpRight);
avxPartitionBlock(right - N_OFFSET, p, tmpLeft, tmpRight);
// move pointers to the correct position where it should read next
auto readLeft = left + N_OFFSET;
auto readRight = right - 2 * N_OFFSET;
// call partition function within the provided data
while (readRight >= readLeft)
{
int* nextPtr;
// check which side of the 8-element chunks is smaller and do partitioning on that 8-element chunk
if ((readLeft - writeLeft) <= (writeRight - readRight))
{
nextPtr = readLeft;
readLeft += N_OFFSET;
}
else {
nextPtr = readRight;
readRight -= N_OFFSET;
}
// do partition of the array and save result directly on the same data array in-place
avxPartitionBlock(nextPtr, p, writeLeft, writeRight);
}
readRight += N_OFFSET;
tmpRight += N_OFFSET;
// handle remainder values inside the data array (which are in the middle of the array)
while (readLeft < readRight) {
auto v = *readLeft++;
// copy values to the temp helper array
if (v <= pivot)
{
*tmpLeft++ = v;
}
else
{
*--tmpRight = v;
}
}
// copy values from the left side of the temp helper array back to the array
auto leftTmpSize = (tmpLeft - _TEMPSTART);
memcpy(writeLeft, _TEMPSTART, leftTmpSize * sizeof(int));
writeLeft += leftTmpSize;
// copy values from the right side of the temp helper array back to the array
auto rightTmpSize = (_TEMPEND - tmpRight);
memcpy(writeLeft, tmpRight, rightTmpSize * sizeof(int));
// Since we swapped the pivot to the right side of the array, we move it back to the position of the "writeLeft" so that we can handle this
// as a boundary!
avxSwap(writeLeft, right);
return writeLeft;
}
// swap function for pointers
void QuickSort::avxSwap(int* left, int* right)
{
auto tmp = *left;
*left = *right;
*right = tmp;
}
// swap if left value is greater than the right value
void QuickSort::avxSwapIfGreater(int* left, int* right)
{
if (*left <= *right)
{
// in this case the data is already in order.
return;
}
// swaps the values
avxSwap(left, right);
}
// insertion function for the CUT_OFF / THRESHOLD part, same code as in algd1 but with pointers!
void QuickSort::avxInsertionSort(int* left, int* right)
{
for (int* i = left + 1; i <= right; i++)
{
int* j = i;
while (j > left && *j < *(j - 1))
{
avxSwap(j, j - 1);
j--;
}
}
}
// QuickSort with AVX recursive function
void QuickSort::avxSort(int* left, int* right)
{
int length = right - left + 1;
int* mid;
// special cases for lengths of 0-3
switch (length)
{
case 0:
case 1:
return;
case 2:
avxSwapIfGreater(left, right);
return;
case 3:
mid = right - 1;
avxSwapIfGreater(left, mid);
avxSwapIfGreater(left, right);
avxSwapIfGreater(mid, right);
return;
}
// Do insertionSort if length is below the CUT_OFF / THRESHOLD
if (length <= CUT_OFF)
{
avxInsertionSort(left, right);
return;
}
// Compute median-of-three, of:
// the first, mid and one before last elements
mid = left + ((right - left) >> 1);
avxSwapIfGreater(left, mid);
avxSwapIfGreater(left, right - 1);
avxSwapIfGreater(mid, right - 1);
// Pivot is mid, place it in the right hand site
// This prevents that the pivot in the middle might be the biggest value of the whole array, so that we cannot fulfill the QuickSort condition, that
// in the next iteration we split the array into two segements for the "Divide and Conquer" approach.
// By doing this we are making sure that we can split the array into two segments!
avxSwap(mid, right);
// do in-place partitioning
int* boundary = avxVectorizedPartitionInPlace(left, right);
// recursive avxSort call
avxSort(left, boundary - 1);
avxSort(boundary + 1, right);
}
// Serial QuickSort function, same code as in algd1
void QuickSort::serialQSort(std::vector<int>& data)
{
qsSort(data, 0, data.size() - 1);
}
// AVX QuickSort function, partition in-place
void QuickSort::avxQSort(std::vector<int>& data)
{
// refrence data on the first entry, since std::vector is not a pointer to its first element
int* left = &data[0];
avxSort(left, left + data.size() - 1);
}
// public facade function for measuring and running the serial QuickSort function
double QuickSort::serialQuickSort(std::vector<int>& data)
{
return measuredSort(data, &QuickSort::serialQSort);
}
// public facade function for measuring and running the AVX QuickSort function
double QuickSort::avxQuickSort(std::vector<int>& data)
{
return measuredSort(data, &QuickSort::avxQSort);
}
// Measure Sort function
double QuickSort::measuredSort(std::vector<int>& data, void(QuickSort::* sortFunc)(std::vector<int>& data))
{
auto start = chrono::high_resolution_clock::now();
(this->*sortFunc) (data);
auto stop = chrono::high_resolution_clock::now();
return (double)(stop - start).count();
}
// Check if array is sorted
bool QuickSort::proove(std::vector<int>& data)
{
for (uint64_t i = 0; i < data.size() - 1; ++i)
{
if (data[i] > data[i + 1])
{
return false;
}
}
return true;
}
// Generate random data
std::vector<int> QuickSort::createRandomData(uint64_t size)
{
vector<int> data(size, 0);
// random gen
default_random_engine generator;
uniform_int_distribution<int> distribution(0, (int)size);
generator.seed(chrono::system_clock::now().time_since_epoch().count());
for (int i = 0; i < size; ++i)
{
data[i] = distribution(generator);
}
return data;
}