-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
vector_algorithms.cpp
1567 lines (1314 loc) · 70.7 KB
/
vector_algorithms.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifdef _M_CEE_PURE
#error _M_CEE_PURE should not be defined when compiling vector_algorithms.cpp.
#endif
#if defined(_M_IX86) || defined(_M_X64) // NB: includes _M_ARM64EC
#include <cstdint>
#ifndef _M_ARM64EC
#include <intrin.h>
#include <isa_availability.h>
extern "C" long __isa_enabled;
#ifndef _DEBUG
#pragma optimize("t", on) // Override /Os with /Ot for this TU
#endif // !defined(_DEBUG)
namespace {
bool _Use_avx2() noexcept {
return __isa_enabled & (1 << __ISA_AVAILABLE_AVX2);
}
bool _Use_sse42() noexcept {
return __isa_enabled & (1 << __ISA_AVAILABLE_SSE42);
}
bool _Use_sse2() noexcept {
#ifdef _M_IX86
return __isa_enabled & (1 << __ISA_AVAILABLE_SSE2);
#else
return true;
#endif
}
struct [[nodiscard]] _Zeroupper_on_exit { // TRANSITION, DevCom-10331414
_Zeroupper_on_exit() = default;
_Zeroupper_on_exit(const _Zeroupper_on_exit&) = delete;
_Zeroupper_on_exit& operator=(const _Zeroupper_on_exit&) = delete;
~_Zeroupper_on_exit() {
_mm256_zeroupper();
}
};
} // namespace
#endif // !defined(_M_ARM64EC)
namespace {
template <class _BidIt>
void _Reverse_tail(_BidIt _First, _BidIt _Last) noexcept {
for (; _First != _Last && _First != --_Last; ++_First) {
const auto _Temp = *_First;
*_First = *_Last;
*_Last = _Temp;
}
}
template <class _BidIt, class _OutIt>
void _Reverse_copy_tail(_BidIt _First, _BidIt _Last, _OutIt _Dest) noexcept {
while (_First != _Last) {
*_Dest++ = *--_Last;
}
}
size_t _Byte_length(const void* _First, const void* _Last) noexcept {
return static_cast<const unsigned char*>(_Last) - static_cast<const unsigned char*>(_First);
}
void _Rewind_bytes(void*& _Target, size_t _Offset) noexcept {
_Target = static_cast<unsigned char*>(_Target) - _Offset;
}
void _Rewind_bytes(const void*& _Target, size_t _Offset) noexcept {
_Target = static_cast<const unsigned char*>(_Target) - _Offset;
}
template <class _Integral>
void _Advance_bytes(void*& _Target, _Integral _Offset) noexcept {
_Target = static_cast<unsigned char*>(_Target) + _Offset;
}
template <class _Integral>
void _Advance_bytes(const void*& _Target, _Integral _Offset) noexcept {
_Target = static_cast<const unsigned char*>(_Target) + _Offset;
}
} // unnamed namespace
extern "C" {
// Must be in sync with _Min_max_element_t in <algorithm>
struct _Min_max_element_t {
const void* _Min;
const void* _Max;
};
__declspec(noalias) void __cdecl __std_swap_ranges_trivially_swappable_noalias(
void* _First1, void* _Last1, void* _First2) noexcept {
#ifndef _M_ARM64EC
constexpr size_t _Mask_32 = ~((static_cast<size_t>(1) << 5) - 1);
if (_Byte_length(_First1, _Last1) >= 32 && _Use_avx2()) {
const void* _Stop_at = _First1;
_Advance_bytes(_Stop_at, _Byte_length(_First1, _Last1) & _Mask_32);
do {
const __m256i _Left = _mm256_loadu_si256(static_cast<__m256i*>(_First1));
const __m256i _Right = _mm256_loadu_si256(static_cast<__m256i*>(_First2));
_mm256_storeu_si256(static_cast<__m256i*>(_First1), _Right);
_mm256_storeu_si256(static_cast<__m256i*>(_First2), _Left);
_Advance_bytes(_First1, 32);
_Advance_bytes(_First2, 32);
} while (_First1 != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
constexpr size_t _Mask_16 = ~((static_cast<size_t>(1) << 4) - 1);
if (_Byte_length(_First1, _Last1) >= 16 && _Use_sse2()) {
const void* _Stop_at = _First1;
_Advance_bytes(_Stop_at, _Byte_length(_First1, _Last1) & _Mask_16);
do {
const __m128i _Left = _mm_loadu_si128(static_cast<__m128i*>(_First1));
const __m128i _Right = _mm_loadu_si128(static_cast<__m128i*>(_First2));
_mm_storeu_si128(static_cast<__m128i*>(_First1), _Right);
_mm_storeu_si128(static_cast<__m128i*>(_First2), _Left);
_Advance_bytes(_First1, 16);
_Advance_bytes(_First2, 16);
} while (_First1 != _Stop_at);
}
#if defined(_M_X64) // NOTE: UNALIGNED MEMORY ACCESSES
constexpr size_t _Mask_8 = ~((static_cast<size_t>(1) << 3) - 1);
if (_Byte_length(_First1, _Last1) >= 8) {
const void* _Stop_at = _First1;
_Advance_bytes(_Stop_at, _Byte_length(_First1, _Last1) & _Mask_8);
do {
const unsigned long long _Left = *static_cast<unsigned long long*>(_First1);
const unsigned long long _Right = *static_cast<unsigned long long*>(_First2);
*static_cast<unsigned long long*>(_First1) = _Right;
*static_cast<unsigned long long*>(_First2) = _Left;
_Advance_bytes(_First1, 8);
_Advance_bytes(_First2, 8);
} while (_First1 != _Stop_at);
}
#elif defined(_M_IX86) // NOTE: UNALIGNED MEMORY ACCESSES
constexpr size_t _Mask_4 = ~((static_cast<size_t>(1) << 2) - 1);
if (_Byte_length(_First1, _Last1) >= 4) {
const void* _Stop_at = _First1;
_Advance_bytes(_Stop_at, _Byte_length(_First1, _Last1) & _Mask_4);
do {
const unsigned long _Left = *static_cast<unsigned long*>(_First1);
const unsigned long _Right = *static_cast<unsigned long*>(_First2);
*static_cast<unsigned long*>(_First1) = _Right;
*static_cast<unsigned long*>(_First2) = _Left;
_Advance_bytes(_First1, 4);
_Advance_bytes(_First2, 4);
} while (_First1 != _Stop_at);
}
#else
#error Unsupported architecture
#endif
#endif // !_M_ARM64EC
auto _First1c = static_cast<unsigned char*>(_First1);
auto _Last1c = static_cast<unsigned char*>(_Last1);
auto _First2c = static_cast<unsigned char*>(_First2);
for (; _First1c != _Last1c; ++_First1c, ++_First2c) {
unsigned char _Ch = *_First1c;
*_First1c = *_First2c;
*_First2c = _Ch;
}
}
// TRANSITION, ABI: __std_swap_ranges_trivially_swappable() is preserved for binary compatibility
void* __cdecl __std_swap_ranges_trivially_swappable(void* _First1, void* _Last1, void* _First2) noexcept {
__std_swap_ranges_trivially_swappable_noalias(_First1, _Last1, _First2);
return static_cast<char*>(_First2) + (static_cast<char*>(_Last1) - static_cast<char*>(_First1));
}
__declspec(noalias) void __cdecl __std_reverse_trivially_swappable_1(void* _First, void* _Last) noexcept {
#ifndef _M_ARM64EC
if (_Byte_length(_First, _Last) >= 64 && _Use_avx2()) {
const __m256i _Reverse_char_lanes_avx = _mm256_set_epi8( //
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, //
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, (_Byte_length(_First, _Last) >> 1) & ~size_t{0x1F});
do {
_Advance_bytes(_Last, -32);
// vpermq to load left and right, and transpose the lanes
const __m256i _Left = _mm256_loadu_si256(static_cast<__m256i*>(_First));
const __m256i _Right = _mm256_loadu_si256(static_cast<__m256i*>(_Last));
const __m256i _Left_perm = _mm256_permute4x64_epi64(_Left, _MM_SHUFFLE(1, 0, 3, 2));
const __m256i _Right_perm = _mm256_permute4x64_epi64(_Right, _MM_SHUFFLE(1, 0, 3, 2));
// transpose all the chars in the lanes
const __m256i _Left_reversed = _mm256_shuffle_epi8(_Left_perm, _Reverse_char_lanes_avx);
const __m256i _Right_reversed = _mm256_shuffle_epi8(_Right_perm, _Reverse_char_lanes_avx);
_mm256_storeu_si256(static_cast<__m256i*>(_First), _Right_reversed);
_mm256_storeu_si256(static_cast<__m256i*>(_Last), _Left_reversed);
_Advance_bytes(_First, 32);
} while (_First != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
if (_Byte_length(_First, _Last) >= 32 && _Use_sse42()) {
const __m128i _Reverse_char_sse = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, (_Byte_length(_First, _Last) >> 1) & ~size_t{0xF});
do {
_Advance_bytes(_Last, -16);
const __m128i _Left = _mm_loadu_si128(static_cast<__m128i*>(_First));
const __m128i _Right = _mm_loadu_si128(static_cast<__m128i*>(_Last));
const __m128i _Left_reversed = _mm_shuffle_epi8(_Left, _Reverse_char_sse); // SSSE3
const __m128i _Right_reversed = _mm_shuffle_epi8(_Right, _Reverse_char_sse);
_mm_storeu_si128(static_cast<__m128i*>(_First), _Right_reversed);
_mm_storeu_si128(static_cast<__m128i*>(_Last), _Left_reversed);
_Advance_bytes(_First, 16);
} while (_First != _Stop_at);
}
#endif // !_M_ARM64EC
_Reverse_tail(static_cast<unsigned char*>(_First), static_cast<unsigned char*>(_Last));
}
__declspec(noalias) void __cdecl __std_reverse_trivially_swappable_2(void* _First, void* _Last) noexcept {
#ifndef _M_ARM64EC
if (_Byte_length(_First, _Last) >= 64 && _Use_avx2()) {
const __m256i _Reverse_short_lanes_avx = _mm256_set_epi8( //
1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, //
1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14);
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, (_Byte_length(_First, _Last) >> 1) & ~size_t{0x1F});
do {
_Advance_bytes(_Last, -32);
const __m256i _Left = _mm256_loadu_si256(static_cast<__m256i*>(_First));
const __m256i _Right = _mm256_loadu_si256(static_cast<__m256i*>(_Last));
const __m256i _Left_perm = _mm256_permute4x64_epi64(_Left, _MM_SHUFFLE(1, 0, 3, 2));
const __m256i _Right_perm = _mm256_permute4x64_epi64(_Right, _MM_SHUFFLE(1, 0, 3, 2));
const __m256i _Left_reversed = _mm256_shuffle_epi8(_Left_perm, _Reverse_short_lanes_avx);
const __m256i _Right_reversed = _mm256_shuffle_epi8(_Right_perm, _Reverse_short_lanes_avx);
_mm256_storeu_si256(static_cast<__m256i*>(_First), _Right_reversed);
_mm256_storeu_si256(static_cast<__m256i*>(_Last), _Left_reversed);
_Advance_bytes(_First, 32);
} while (_First != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
if (_Byte_length(_First, _Last) >= 32 && _Use_sse42()) {
const __m128i _Reverse_short_sse = _mm_set_epi8(1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14);
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, (_Byte_length(_First, _Last) >> 1) & ~size_t{0xF});
do {
_Advance_bytes(_Last, -16);
const __m128i _Left = _mm_loadu_si128(static_cast<__m128i*>(_First));
const __m128i _Right = _mm_loadu_si128(static_cast<__m128i*>(_Last));
const __m128i _Left_reversed = _mm_shuffle_epi8(_Left, _Reverse_short_sse); // SSSE3
const __m128i _Right_reversed = _mm_shuffle_epi8(_Right, _Reverse_short_sse);
_mm_storeu_si128(static_cast<__m128i*>(_First), _Right_reversed);
_mm_storeu_si128(static_cast<__m128i*>(_Last), _Left_reversed);
_Advance_bytes(_First, 16);
} while (_First != _Stop_at);
}
#endif // !_M_ARM64EC
_Reverse_tail(static_cast<unsigned short*>(_First), static_cast<unsigned short*>(_Last));
}
__declspec(noalias) void __cdecl __std_reverse_trivially_swappable_4(void* _First, void* _Last) noexcept {
#ifndef _M_ARM64EC
if (_Byte_length(_First, _Last) >= 64 && _Use_avx2()) {
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, (_Byte_length(_First, _Last) >> 1) & ~size_t{0x1F});
const __m256i _Shuf = _mm256_set_epi32(0, 1, 2, 3, 4, 5, 6, 7);
do {
_Advance_bytes(_Last, -32);
const __m256i _Left = _mm256_loadu_si256(static_cast<__m256i*>(_First));
const __m256i _Right = _mm256_loadu_si256(static_cast<__m256i*>(_Last));
const __m256i _Left_reversed = _mm256_permutevar8x32_epi32(_Left, _Shuf);
const __m256i _Right_reversed = _mm256_permutevar8x32_epi32(_Right, _Shuf);
_mm256_storeu_si256(static_cast<__m256i*>(_First), _Right_reversed);
_mm256_storeu_si256(static_cast<__m256i*>(_Last), _Left_reversed);
_Advance_bytes(_First, 32);
} while (_First != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
if (_Byte_length(_First, _Last) >= 32 && _Use_sse2()) {
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, (_Byte_length(_First, _Last) >> 1) & ~size_t{0xF});
do {
_Advance_bytes(_Last, -16);
const __m128i _Left = _mm_loadu_si128(static_cast<__m128i*>(_First));
const __m128i _Right = _mm_loadu_si128(static_cast<__m128i*>(_Last));
const __m128i _Left_reversed = _mm_shuffle_epi32(_Left, _MM_SHUFFLE(0, 1, 2, 3));
const __m128i _Right_reversed = _mm_shuffle_epi32(_Right, _MM_SHUFFLE(0, 1, 2, 3));
_mm_storeu_si128(static_cast<__m128i*>(_First), _Right_reversed);
_mm_storeu_si128(static_cast<__m128i*>(_Last), _Left_reversed);
_Advance_bytes(_First, 16);
} while (_First != _Stop_at);
}
#endif // !_M_ARM64EC
_Reverse_tail(static_cast<unsigned long*>(_First), static_cast<unsigned long*>(_Last));
}
__declspec(noalias) void __cdecl __std_reverse_trivially_swappable_8(void* _First, void* _Last) noexcept {
#ifndef _M_ARM64EC
if (_Byte_length(_First, _Last) >= 64 && _Use_avx2()) {
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, (_Byte_length(_First, _Last) >> 1) & ~size_t{0x1F});
do {
_Advance_bytes(_Last, -32);
const __m256i _Left = _mm256_loadu_si256(static_cast<__m256i*>(_First));
const __m256i _Right = _mm256_loadu_si256(static_cast<__m256i*>(_Last));
const __m256i _Left_reversed = _mm256_permute4x64_epi64(_Left, _MM_SHUFFLE(0, 1, 2, 3));
const __m256i _Right_reversed = _mm256_permute4x64_epi64(_Right, _MM_SHUFFLE(0, 1, 2, 3));
_mm256_storeu_si256(static_cast<__m256i*>(_First), _Right_reversed);
_mm256_storeu_si256(static_cast<__m256i*>(_Last), _Left_reversed);
_Advance_bytes(_First, 32);
} while (_First != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
if (_Byte_length(_First, _Last) >= 32 && _Use_sse2()) {
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, (_Byte_length(_First, _Last) >> 1) & ~size_t{0xF});
do {
_Advance_bytes(_Last, -16);
const __m128i _Left = _mm_loadu_si128(static_cast<__m128i*>(_First));
const __m128i _Right = _mm_loadu_si128(static_cast<__m128i*>(_Last));
const __m128i _Left_reversed = _mm_shuffle_epi32(_Left, _MM_SHUFFLE(1, 0, 3, 2));
const __m128i _Right_reversed = _mm_shuffle_epi32(_Right, _MM_SHUFFLE(1, 0, 3, 2));
_mm_storeu_si128(static_cast<__m128i*>(_First), _Right_reversed);
_mm_storeu_si128(static_cast<__m128i*>(_Last), _Left_reversed);
_Advance_bytes(_First, 16);
} while (_First != _Stop_at);
}
#endif // !_M_ARM64EC
_Reverse_tail(static_cast<unsigned long long*>(_First), static_cast<unsigned long long*>(_Last));
}
__declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_1(
const void* _First, const void* _Last, void* _Dest) noexcept {
#ifndef _M_ARM64EC
if (_Byte_length(_First, _Last) >= 32 && _Use_avx2()) {
const __m256i _Reverse_char_lanes_avx = _mm256_set_epi8( //
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, //
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
const void* _Stop_at = _Dest;
_Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~size_t{0x1F});
do {
_Advance_bytes(_Last, -32);
const __m256i _Block = _mm256_loadu_si256(static_cast<const __m256i*>(_Last));
const __m256i _Block_permuted = _mm256_permute4x64_epi64(_Block, _MM_SHUFFLE(1, 0, 3, 2));
const __m256i _Block_reversed = _mm256_shuffle_epi8(_Block_permuted, _Reverse_char_lanes_avx);
_mm256_storeu_si256(static_cast<__m256i*>(_Dest), _Block_reversed);
_Advance_bytes(_Dest, 32);
} while (_Dest != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
if (_Byte_length(_First, _Last) >= 16 && _Use_sse42()) {
const __m128i _Reverse_char_sse = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
const void* _Stop_at = _Dest;
_Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~size_t{0xF});
do {
_Advance_bytes(_Last, -16);
const __m128i _Block = _mm_loadu_si128(static_cast<const __m128i*>(_Last));
const __m128i _Block_reversed = _mm_shuffle_epi8(_Block, _Reverse_char_sse); // SSSE3
_mm_storeu_si128(static_cast<__m128i*>(_Dest), _Block_reversed);
_Advance_bytes(_Dest, 16);
} while (_Dest != _Stop_at);
}
#endif // !_M_ARM64EC
_Reverse_copy_tail(static_cast<const unsigned char*>(_First), static_cast<const unsigned char*>(_Last),
static_cast<unsigned char*>(_Dest));
}
__declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_2(
const void* _First, const void* _Last, void* _Dest) noexcept {
#ifndef _M_ARM64EC
if (_Byte_length(_First, _Last) >= 32 && _Use_avx2()) {
const __m256i _Reverse_short_lanes_avx = _mm256_set_epi8( //
1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, //
1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14);
const void* _Stop_at = _Dest;
_Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~size_t{0x1F});
do {
_Advance_bytes(_Last, -32);
const __m256i _Block = _mm256_loadu_si256(static_cast<const __m256i*>(_Last));
const __m256i _Block_permuted = _mm256_permute4x64_epi64(_Block, _MM_SHUFFLE(1, 0, 3, 2));
const __m256i _Block_reversed = _mm256_shuffle_epi8(_Block_permuted, _Reverse_short_lanes_avx);
_mm256_storeu_si256(static_cast<__m256i*>(_Dest), _Block_reversed);
_Advance_bytes(_Dest, 32);
} while (_Dest != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
if (_Byte_length(_First, _Last) >= 16 && _Use_sse42()) {
const __m128i _Reverse_short_sse = _mm_set_epi8(1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14);
const void* _Stop_at = _Dest;
_Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~size_t{0xF});
do {
_Advance_bytes(_Last, -16);
const __m128i _Block = _mm_loadu_si128(static_cast<const __m128i*>(_Last));
const __m128i _Block_reversed = _mm_shuffle_epi8(_Block, _Reverse_short_sse); // SSSE3
_mm_storeu_si128(static_cast<__m128i*>(_Dest), _Block_reversed);
_Advance_bytes(_Dest, 16);
} while (_Dest != _Stop_at);
}
#endif // !_M_ARM64EC
_Reverse_copy_tail(static_cast<const unsigned short*>(_First), static_cast<const unsigned short*>(_Last),
static_cast<unsigned short*>(_Dest));
}
__declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_4(
const void* _First, const void* _Last, void* _Dest) noexcept {
#ifndef _M_ARM64EC
if (_Byte_length(_First, _Last) >= 32 && _Use_avx2()) {
const void* _Stop_at = _Dest;
_Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~size_t{0x1F});
const __m256i _Shuf = _mm256_set_epi32(0, 1, 2, 3, 4, 5, 6, 7);
do {
_Advance_bytes(_Last, -32);
const __m256i _Block = _mm256_loadu_si256(static_cast<const __m256i*>(_Last));
const __m256i _Block_reversed = _mm256_permutevar8x32_epi32(_Block, _Shuf);
_mm256_storeu_si256(static_cast<__m256i*>(_Dest), _Block_reversed);
_Advance_bytes(_Dest, 32);
} while (_Dest != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
if (_Byte_length(_First, _Last) >= 16 && _Use_sse2()) {
const void* _Stop_at = _Dest;
_Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~size_t{0xF});
do {
_Advance_bytes(_Last, -16);
const __m128i _Block = _mm_loadu_si128(static_cast<const __m128i*>(_Last));
const __m128i _Block_reversed = _mm_shuffle_epi32(_Block, _MM_SHUFFLE(0, 1, 2, 3));
_mm_storeu_si128(static_cast<__m128i*>(_Dest), _Block_reversed);
_Advance_bytes(_Dest, 16);
} while (_Dest != _Stop_at);
}
#endif // !_M_ARM64EC
_Reverse_copy_tail(static_cast<const unsigned long*>(_First), static_cast<const unsigned long*>(_Last),
static_cast<unsigned long*>(_Dest));
}
__declspec(noalias) void __cdecl __std_reverse_copy_trivially_copyable_8(
const void* _First, const void* _Last, void* _Dest) noexcept {
#ifndef _M_ARM64EC
if (_Byte_length(_First, _Last) >= 32 && _Use_avx2()) {
const void* _Stop_at = _Dest;
_Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~size_t{0x1F});
do {
_Advance_bytes(_Last, -32);
const __m256i _Block = _mm256_loadu_si256(static_cast<const __m256i*>(_Last));
const __m256i _Block_reversed = _mm256_permute4x64_epi64(_Block, _MM_SHUFFLE(0, 1, 2, 3));
_mm256_storeu_si256(static_cast<__m256i*>(_Dest), _Block_reversed);
_Advance_bytes(_Dest, 32);
} while (_Dest != _Stop_at);
_mm256_zeroupper(); // TRANSITION, DevCom-10331414
}
if (_Byte_length(_First, _Last) >= 16 && _Use_sse2()) {
const void* _Stop_at = _Dest;
_Advance_bytes(_Stop_at, _Byte_length(_First, _Last) & ~size_t{0xF});
do {
_Advance_bytes(_Last, -16);
const __m128i _Block = _mm_loadu_si128(static_cast<const __m128i*>(_Last));
const __m128i _Block_reversed = _mm_shuffle_epi32(_Block, _MM_SHUFFLE(1, 0, 3, 2));
_mm_storeu_si128(static_cast<__m128i*>(_Dest), _Block_reversed);
_Advance_bytes(_Dest, 16);
} while (_Dest != _Stop_at);
}
#endif // !_M_ARM64EC
_Reverse_copy_tail(static_cast<const unsigned long long*>(_First), static_cast<const unsigned long long*>(_Last),
static_cast<unsigned long long*>(_Dest));
}
} // extern "C"
namespace {
template <class _Ty>
const void* _Min_tail(const void* const _First, const void* const _Last, const void* _Res, _Ty _Cur) noexcept {
for (auto _Ptr = static_cast<const _Ty*>(_First); _Ptr != _Last; ++_Ptr) {
if (*_Ptr < _Cur) {
_Res = _Ptr;
_Cur = *_Ptr;
}
}
return _Res;
}
template <class _Ty>
const void* _Max_tail(const void* const _First, const void* const _Last, const void* _Res, _Ty _Cur) noexcept {
for (auto _Ptr = static_cast<const _Ty*>(_First); _Ptr != _Last; ++_Ptr) {
if (_Cur < *_Ptr) {
_Res = _Ptr;
_Cur = *_Ptr;
}
}
return _Res;
}
template <class _Ty>
_Min_max_element_t _Both_tail(const void* const _First, const void* const _Last, _Min_max_element_t& _Res,
_Ty _Cur_min, _Ty _Cur_max) noexcept {
for (auto _Ptr = static_cast<const _Ty*>(_First); _Ptr != _Last; ++_Ptr) {
if (*_Ptr < _Cur_min) {
_Res._Min = _Ptr;
_Cur_min = *_Ptr;
}
// Not else!
// * Needed for correctness if start with maximum, as we don't handle specially the first element.
// * Promote branchless code generation.
if (_Cur_max <= *_Ptr) {
_Res._Max = _Ptr;
_Cur_max = *_Ptr;
}
}
return _Res;
}
enum _Min_max_mode {
_Mode_min = 1 << 0,
_Mode_max = 1 << 1,
_Mode_both = _Mode_min | _Mode_max,
};
struct _Minmax_traits_1 {
using _Signed_t = int8_t;
using _Unsigned_t = uint8_t;
static constexpr _Signed_t _Init_min_val = static_cast<_Signed_t>(0x7F);
static constexpr _Signed_t _Init_max_val = static_cast<_Signed_t>(0x80);
#ifndef _M_ARM64EC
static constexpr bool _Has_portion_max = true;
static constexpr size_t _Portion_max = 256;
static __m128i _Sign_correction(const __m128i _Val, const bool _Sign) noexcept {
alignas(16) static constexpr _Unsigned_t _Sign_corrections[2][16] = {
{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}, {}};
return _mm_sub_epi8(_Val, _mm_load_si128(reinterpret_cast<const __m128i*>(_Sign_corrections[_Sign])));
}
static __m128i _Inc(__m128i _Idx) noexcept {
return _mm_add_epi8(_Idx, _mm_set1_epi8(1));
}
template <class _Fn>
static __m128i _H_func(const __m128i _Cur, _Fn _Funct) noexcept {
const __m128i _Shuf_bytes = _mm_set_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1);
const __m128i _Shuf_words = _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2);
__m128i _H_min_val = _Cur;
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi32(_H_min_val, _MM_SHUFFLE(1, 0, 3, 2)));
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi32(_H_min_val, _MM_SHUFFLE(2, 3, 0, 1)));
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi8(_H_min_val, _Shuf_words));
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi8(_H_min_val, _Shuf_bytes));
return _H_min_val;
}
static __m128i _H_min(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_min_epi8(_First, _Second); });
}
static __m128i _H_max(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_max_epi8(_First, _Second); });
}
static __m128i _H_min_u(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_min_epu8(_First, _Second); });
}
static __m128i _H_max_u(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_max_epu8(_First, _Second); });
}
static _Signed_t _Get_any(const __m128i _Cur) noexcept {
return static_cast<_Signed_t>(_mm_cvtsi128_si32(_Cur));
}
static _Unsigned_t _Get_v_pos(const __m128i _Idx, const unsigned long _H_pos) noexcept {
return static_cast<_Unsigned_t>(_mm_cvtsi128_si32(_mm_shuffle_epi8(_Idx, _mm_cvtsi32_si128(_H_pos))));
}
static __m128i _Cmp_eq(const __m128i _First, const __m128i _Second) noexcept {
return _mm_cmpeq_epi8(_First, _Second);
}
static __m128i _Cmp_gt(const __m128i _First, const __m128i _Second) noexcept {
return _mm_cmpgt_epi8(_First, _Second);
}
static __m128i _Min(const __m128i _First, const __m128i _Second, __m128i) noexcept {
return _mm_min_epi8(_First, _Second);
}
static __m128i _Max(const __m128i _First, const __m128i _Second, __m128i) noexcept {
return _mm_max_epi8(_First, _Second);
}
#endif // !_M_ARM64EC
};
struct _Minmax_traits_2 {
using _Signed_t = int16_t;
using _Unsigned_t = uint16_t;
static constexpr _Signed_t _Init_min_val = static_cast<_Signed_t>(0x7FFF);
static constexpr _Signed_t _Init_max_val = static_cast<_Signed_t>(0x8000);
#ifndef _M_ARM64EC
static constexpr bool _Has_portion_max = true;
static constexpr size_t _Portion_max = 65536;
static __m128i _Sign_correction(const __m128i _Val, const bool _Sign) noexcept {
alignas(16) static constexpr _Unsigned_t _Sign_corrections[2][8] = {
0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000, {}};
return _mm_sub_epi16(_Val, _mm_load_si128(reinterpret_cast<const __m128i*>(_Sign_corrections[_Sign])));
}
static __m128i _Inc(__m128i _Idx) noexcept {
return _mm_add_epi16(_Idx, _mm_set1_epi16(1));
}
template <class _Fn>
static __m128i _H_func(const __m128i _Cur, _Fn _Funct) noexcept {
const __m128i _Shuf_words = _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2);
__m128i _H_min_val = _Cur;
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi32(_H_min_val, _MM_SHUFFLE(1, 0, 3, 2)));
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi32(_H_min_val, _MM_SHUFFLE(2, 3, 0, 1)));
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi8(_H_min_val, _Shuf_words));
return _H_min_val;
}
static __m128i _H_min(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_min_epi16(_First, _Second); });
}
static __m128i _H_max(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_max_epi16(_First, _Second); });
}
static __m128i _H_min_u(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_min_epu16(_First, _Second); });
}
static __m128i _H_max_u(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_max_epu16(_First, _Second); });
}
static _Signed_t _Get_any(const __m128i _Cur) noexcept {
return static_cast<_Signed_t>(_mm_cvtsi128_si32(_Cur));
}
static _Unsigned_t _Get_v_pos(const __m128i _Idx, const unsigned long _H_pos) noexcept {
static constexpr _Unsigned_t _Shuf[] = {0x0100, 0x0302, 0x0504, 0x0706, 0x0908, 0x0B0A, 0x0D0C, 0x0F0E};
return static_cast<_Unsigned_t>(
_mm_cvtsi128_si32(_mm_shuffle_epi8(_Idx, _mm_cvtsi32_si128(_Shuf[_H_pos >> 1]))));
}
static __m128i _Cmp_eq(const __m128i _First, const __m128i _Second) noexcept {
return _mm_cmpeq_epi16(_First, _Second);
}
static __m128i _Cmp_gt(const __m128i _First, const __m128i _Second) noexcept {
return _mm_cmpgt_epi16(_First, _Second);
}
static __m128i _Min(const __m128i _First, const __m128i _Second, __m128i) noexcept {
return _mm_min_epi16(_First, _Second);
}
static __m128i _Max(const __m128i _First, const __m128i _Second, __m128i) noexcept {
return _mm_max_epi16(_First, _Second);
}
#endif // !_M_ARM64EC
};
struct _Minmax_traits_4 {
using _Signed_t = int32_t;
using _Unsigned_t = uint32_t;
static constexpr _Signed_t _Init_min_val = static_cast<_Signed_t>(0x7FFF'FFFFUL);
static constexpr _Signed_t _Init_max_val = static_cast<_Signed_t>(0x8000'0000UL);
#ifndef _M_ARM64EC
#ifdef _M_IX86
static constexpr bool _Has_portion_max = false;
#else // ^^^ 32-bit / 64-bit vvv
static constexpr bool _Has_portion_max = true;
static constexpr size_t _Portion_max = 0x1'0000'0000ULL;
#endif // ^^^ 64-bit ^^^
static __m128i _Sign_correction(const __m128i _Val, const bool _Sign) noexcept {
alignas(16) static constexpr _Unsigned_t _Sign_corrections[2][4] = {
0x8000'0000UL, 0x8000'0000UL, 0x8000'0000UL, 0x8000'0000UL, {}};
return _mm_sub_epi32(_Val, _mm_load_si128(reinterpret_cast<const __m128i*>(_Sign_corrections[_Sign])));
}
static __m128i _Inc(__m128i _Idx) noexcept {
return _mm_add_epi32(_Idx, _mm_set1_epi32(1));
}
template <class _Fn>
static __m128i _H_func(const __m128i _Cur, _Fn _Funct) noexcept {
__m128i _H_min_val = _Cur;
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi32(_H_min_val, _MM_SHUFFLE(1, 0, 3, 2)));
_H_min_val = _Funct(_H_min_val, _mm_shuffle_epi32(_H_min_val, _MM_SHUFFLE(2, 3, 0, 1)));
return _H_min_val;
}
static __m128i _H_min(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_min_epi32(_First, _Second); });
}
static __m128i _H_max(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_max_epi32(_First, _Second); });
}
static __m128i _H_min_u(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_min_epu32(_First, _Second); });
}
static __m128i _H_max_u(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](__m128i _First, __m128i _Second) { return _mm_max_epu32(_First, _Second); });
}
static _Signed_t _Get_any(const __m128i _Cur) noexcept {
return static_cast<_Signed_t>(_mm_cvtsi128_si32(_Cur));
}
static _Unsigned_t _Get_v_pos(const __m128i _Idx, const unsigned long _H_pos) noexcept {
_Unsigned_t _Array[4];
_mm_storeu_si128(reinterpret_cast<__m128i*>(&_Array), _Idx);
return _Array[_H_pos >> 2];
}
static __m128i _Cmp_eq(const __m128i _First, const __m128i _Second) noexcept {
return _mm_cmpeq_epi32(_First, _Second);
}
static __m128i _Cmp_gt(const __m128i _First, const __m128i _Second) noexcept {
return _mm_cmpgt_epi32(_First, _Second);
}
static __m128i _Min(const __m128i _First, const __m128i _Second, __m128i) noexcept {
return _mm_min_epi32(_First, _Second);
}
static __m128i _Max(const __m128i _First, const __m128i _Second, __m128i) noexcept {
return _mm_max_epi32(_First, _Second);
}
#endif // !_M_ARM64EC
};
struct _Minmax_traits_8 {
using _Signed_t = int64_t;
using _Unsigned_t = uint64_t;
static constexpr _Signed_t _Init_min_val = static_cast<_Signed_t>(0x7FFF'FFFF'FFFF'FFFFULL);
static constexpr _Signed_t _Init_max_val = static_cast<_Signed_t>(0x8000'0000'0000'0000ULL);
#ifndef _M_ARM64EC
static constexpr bool _Has_portion_max = false;
static __m128i _Sign_correction(const __m128i _Val, const bool _Sign) noexcept {
alignas(16) static constexpr _Unsigned_t _Sign_corrections[2][2] = {
0x8000'0000'0000'0000ULL, 0x8000'0000'0000'0000ULL, {}};
return _mm_sub_epi64(_Val, _mm_load_si128(reinterpret_cast<const __m128i*>(_Sign_corrections[_Sign])));
}
static __m128i _Inc(__m128i _Idx) noexcept {
return _mm_add_epi64(_Idx, _mm_set1_epi64x(1));
}
template <class _Fn>
static __m128i _H_func(const __m128i _Cur, _Fn _Funct) noexcept {
_Signed_t _H_min_a = _Get_any(_Cur);
_Signed_t _H_min_b = _Get_any(_mm_bsrli_si128(_Cur, 8));
if (_Funct(_H_min_b, _H_min_a)) {
_H_min_a = _H_min_b;
}
return _mm_set1_epi64x(_H_min_a);
}
static __m128i _H_min(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](_Signed_t _Lhs, _Signed_t _Rhs) { return _Lhs < _Rhs; });
}
static __m128i _H_max(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](_Signed_t _Lhs, _Signed_t _Rhs) { return _Lhs > _Rhs; });
}
static __m128i _H_min_u(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](_Unsigned_t _Lhs, _Unsigned_t _Rhs) { return _Lhs < _Rhs; });
}
static __m128i _H_max_u(const __m128i _Cur) noexcept {
return _H_func(_Cur, [](_Unsigned_t _Lhs, _Unsigned_t _Rhs) { return _Lhs > _Rhs; });
}
static _Signed_t _Get_any(const __m128i _Cur) noexcept {
#ifdef _M_IX86
return static_cast<_Signed_t>(
(static_cast<_Unsigned_t>(static_cast<uint32_t>(_mm_extract_epi32(_Cur, 1))) << 32)
| static_cast<_Unsigned_t>(static_cast<uint32_t>(_mm_cvtsi128_si32(_Cur))));
#else // ^^^ x86 / x64 vvv
return static_cast<_Signed_t>(_mm_cvtsi128_si64(_Cur));
#endif // ^^^ x64 ^^^
}
static _Unsigned_t _Get_v_pos(const __m128i _Idx, const unsigned long _H_pos) noexcept {
_Unsigned_t _Array[2];
_mm_storeu_si128(reinterpret_cast<__m128i*>(&_Array), _Idx);
return _Array[_H_pos >> 3];
}
static __m128i _Cmp_eq(const __m128i _First, const __m128i _Second) noexcept {
return _mm_cmpeq_epi64(_First, _Second);
}
static __m128i _Cmp_gt(const __m128i _First, const __m128i _Second) noexcept {
return _mm_cmpgt_epi64(_First, _Second);
}
static __m128i _Min(const __m128i _First, const __m128i _Second, const __m128i _Mask) noexcept {
return _mm_blendv_epi8(_First, _Second, _Mask);
}
static __m128i _Max(const __m128i _First, const __m128i _Second, const __m128i _Mask) noexcept {
return _mm_blendv_epi8(_First, _Second, _Mask);
}
#endif // !_M_ARM64EC
};
// _Minmax_element has exactly the same signature as the extern "C" functions
// (__std_min_element_N, __std_max_element_N, __std_minmax_element_N), up to calling convention.
// This makes sure the template specialization is fused with the extern "C" function.
// In optimized builds it avoids an extra call, as this function is too large to inline.
template <_Min_max_mode _Mode, class _Traits>
auto __stdcall _Minmax_element(const void* _First, const void* const _Last, const bool _Sign) noexcept {
_Min_max_element_t _Res = {_First, _First};
auto _Cur_min_val = _Traits::_Init_min_val;
auto _Cur_max_val = _Traits::_Init_max_val;
#ifndef _M_ARM64EC
auto _Base = static_cast<const char*>(_First);
if (_Byte_length(_First, _Last) >= 16 && _Use_sse42()) {
size_t _Portion_byte_size = _Byte_length(_First, _Last) & ~size_t{0xF};
if constexpr (_Traits::_Has_portion_max) {
// vector of indices will wrap around at exactly this size
constexpr size_t _Max_portion_byte_size = _Traits::_Portion_max * 16;
if (_Portion_byte_size > _Max_portion_byte_size) {
_Portion_byte_size = _Max_portion_byte_size;
}
}
const void* _Stop_at = _First;
_Advance_bytes(_Stop_at, _Portion_byte_size);
// Load values and if unsigned adjust them to be signed (for signed vector comparisons)
__m128i _Cur_vals =
_Traits::_Sign_correction(_mm_loadu_si128(reinterpret_cast<const __m128i*>(_First)), _Sign);
__m128i _Cur_vals_min = _Cur_vals; // vector of vertical minimum values
__m128i _Cur_idx_min = _mm_setzero_si128(); // vector of vertical minimum indices
__m128i _Cur_vals_max = _Cur_vals; // vector of vertical maximum values
__m128i _Cur_idx_max = _mm_setzero_si128(); // vector of vertical maximum indices
__m128i _Cur_idx = _mm_setzero_si128(); // current vector of indices
for (;;) {
_Advance_bytes(_First, 16);
// Increment vertical indices. Will stop at exactly wrap around, if not reach the end before
_Cur_idx = _Traits::_Inc(_Cur_idx);
if (_First == _Stop_at) {
// Reached end or indices wrap around point.
// Compute horizontal min and/or max. Determine horizontal and vertical position of it.
if constexpr ((_Mode & _Mode_min) != 0) {
const __m128i _H_min =
_Traits::_H_min(_Cur_vals_min); // Vector populated by the smallest element
const auto _H_min_val = _Traits::_Get_any(_H_min); // Get any element of it
if (_H_min_val < _Cur_min_val) { // Current horizontal min is less than the old
_Cur_min_val = _H_min_val; // update min
const __m128i _Eq_mask =
_Traits::_Cmp_eq(_H_min, _Cur_vals_min); // Mask of all elems eq to min
int _Mask = _mm_movemask_epi8(_Eq_mask);
// Indices of minimum elements or the greatest index if none
const __m128i _All_max = _mm_set1_epi8(static_cast<char>(0xFF));
const __m128i _Idx_min_val = _mm_blendv_epi8(_All_max, _Cur_idx_min, _Eq_mask);
__m128i _Idx_min = _Traits::_H_min_u(_Idx_min_val); // The smallest indices
// Select the smallest vertical indices from the smallest element mask
_Mask &= _mm_movemask_epi8(_Traits::_Cmp_eq(_Idx_min, _Idx_min_val));
unsigned long _H_pos;
// Find the smallest horizontal index
_BitScanForward(&_H_pos, _Mask); // lgtm [cpp/conditionallyuninitializedvariable]
const auto _V_pos = _Traits::_Get_v_pos(_Cur_idx_min, _H_pos); // Extract its vertical index
_Res._Min =
_Base + static_cast<size_t>(_V_pos) * 16 + _H_pos; // Finally, compute the pointer
}
}
if constexpr ((_Mode & _Mode_max) != 0) {
const __m128i _H_max =
_Traits::_H_max(_Cur_vals_max); // Vector populated by the largest element
const auto _H_max_val = _Traits::_Get_any(_H_max); // Get any element of it
if (_Mode == _Mode_both && _Cur_max_val <= _H_max_val
|| _Mode == _Mode_max && _Cur_max_val < _H_max_val) {
// max_element: current horizontal max is greater than the old, update max
// minmax_element: current horizontal max is not less than the old, update max
_Cur_max_val = _H_max_val;
const __m128i _Eq_mask =
_Traits::_Cmp_eq(_H_max, _Cur_vals_max); // Mask of all elems eq to max
int _Mask = _mm_movemask_epi8(_Eq_mask);
unsigned long _H_pos;
if constexpr (_Mode == _Mode_both) {
// Looking for the last occurrence of maximum
// Indices of maximum elements or zero if none
const __m128i _Idx_max_val =
_mm_blendv_epi8(_mm_setzero_si128(), _Cur_idx_max, _Eq_mask);
const __m128i _Idx_max = _Traits::_H_max_u(_Idx_max_val); // The greatest indices
// Select the greatest vertical indices from the largest element mask
_Mask &= _mm_movemask_epi8(_Traits::_Cmp_eq(_Idx_max, _Idx_max_val));
// Find the largest horizontal index
_BitScanReverse(&_H_pos, _Mask); // lgtm [cpp/conditionallyuninitializedvariable]
_H_pos -= sizeof(_Cur_max_val) - 1; // Correct from highest val bit to lowest
} else {
// Looking for the first occurrence of maximum
// Indices of maximum elements or the greatest index if none
const __m128i _All_max = _mm_set1_epi8(static_cast<char>(0xFF));
const __m128i _Idx_max_val = _mm_blendv_epi8(_All_max, _Cur_idx_max, _Eq_mask);
const __m128i _Idx_max = _Traits::_H_min_u(_Idx_max_val); // The smallest indices
// Select the smallest vertical indices from the largest element mask
_Mask &= _mm_movemask_epi8(_Traits::_Cmp_eq(_Idx_max, _Idx_max_val));
// Find the smallest horizontal index
_BitScanForward(&_H_pos, _Mask); // lgtm [cpp/conditionallyuninitializedvariable]
}
const auto _V_pos = _Traits::_Get_v_pos(_Cur_idx_max, _H_pos); // Extract its vertical index
_Res._Max =
_Base + static_cast<size_t>(_V_pos) * 16 + _H_pos; // Finally, compute the pointer
}
}
// Horizontal part done, results are saved, now need to see if there is another portion to process
if constexpr (_Traits::_Has_portion_max) {
// Either the last portion or wrapping point reached, need to determine
_Portion_byte_size = _Byte_length(_First, _Last) & ~size_t{0xF};
if (_Portion_byte_size == 0) {
break; // That was the last portion
}
// Start next portion to handle the wrapping indices. Assume _Cur_idx is zero
constexpr size_t _Max_portion_byte_size = _Traits::_Portion_max * 16;
if (_Portion_byte_size > _Max_portion_byte_size) {
_Portion_byte_size = _Max_portion_byte_size;
}
_Advance_bytes(_Stop_at, _Portion_byte_size);
// Indices will be relative to the new base
_Base = static_cast<const char*>(_First);
// Load values and if unsigned adjust them to be signed (for signed vector comparisons)
_Cur_vals =
_Traits::_Sign_correction(_mm_loadu_si128(reinterpret_cast<const __m128i*>(_First)), _Sign);
if constexpr ((_Mode & _Mode_min) != 0) {
_Cur_vals_min = _Cur_vals;
_Cur_idx_min = _mm_setzero_si128();
}