-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathsimd.h
More file actions
4941 lines (4536 loc) · 149 KB
/
simd.h
File metadata and controls
4941 lines (4536 loc) · 149 KB
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
// Definition of the public simd interfaces -*- C++ -*-
// Copyright © 2015-2020 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
// Matthias Kretz <m.kretz@gsi.de>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the names of contributing organizations nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef _GLIBCXX_EXPERIMENTAL_SIMD_H
#define _GLIBCXX_EXPERIMENTAL_SIMD_H
#if __cplusplus >= 201703L
#include "simd_detail.h"
#include <bitset>
#include <climits>
#include <cstring>
#include <functional>
#include <iosfwd>
#include <limits>
#include <utility>
#if _GLIBCXX_SIMD_X86INTRIN
#include <x86intrin.h>
#elif _GLIBCXX_SIMD_HAVE_NEON
#include <arm_neon.h>
#endif
_GLIBCXX_SIMD_BEGIN_NAMESPACE
#if !_GLIBCXX_SIMD_X86INTRIN
using __m128 [[__gnu__::__vector_size__(16)]] = float;
using __m128d [[__gnu__::__vector_size__(16)]] = double;
using __m128i [[__gnu__::__vector_size__(16)]] = long long;
using __m256 [[__gnu__::__vector_size__(32)]] = float;
using __m256d [[__gnu__::__vector_size__(32)]] = double;
using __m256i [[__gnu__::__vector_size__(32)]] = long long;
using __m512 [[__gnu__::__vector_size__(64)]] = float;
using __m512d [[__gnu__::__vector_size__(64)]] = double;
using __m512i [[__gnu__::__vector_size__(64)]] = long long;
#endif
#if __clang__
template<typename T> auto __builtin_ia32_ps256_ps (T x) { return __builtin_shufflevector(x, _mm_setzero_ps() , 0, 1, 2, 3, 4, 4, 4, 4); }
template<typename T> auto __builtin_ia32_ps512_ps (T x) { return __builtin_shufflevector(x, _mm_setzero_ps() , 0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4); }
template<typename T> auto __builtin_ia32_ps512_256ps(T x) { return __builtin_shufflevector(x, _mm256_setzero_ps(), 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8); }
#endif
// __next_power_of_2{{{
/**
* \internal
* Returns the next power of 2 larger than or equal to \p __x.
*/
constexpr std::size_t
__next_power_of_2(std::size_t __x)
{
return (__x & (__x - 1)) == 0 ? __x
: __next_power_of_2((__x | (__x >> 1)) + 1);
}
// }}}
namespace simd_abi {
// {{{
// implementation details:
struct _Scalar;
template <int _Np> struct _Fixed;
// There are two major ABIs that appear on different architectures.
// Both have non-boolean values packed into an N Byte register
// -> #elements = N / sizeof(T)
// Masks differ:
// 1. Use value vector registers for masks (all 0 or all 1)
// 2. Use bitmasks (mask registers) with one bit per value in the corresponding
// value vector
//
// Both can be partially used, masking off the rest when doing horizontal
// operations or operations that can trap (e.g. FP_INVALID or integer division
// by 0). This is encoded as the number of used bytes.
template <int _UsedBytes> struct _VecBuiltin;
template <int _UsedBytes> struct _VecBltnBtmsk;
template <typename _Tp, int _Np> using _VecN = _VecBuiltin<sizeof(_Tp) * _Np>;
template <int _UsedBytes = 16> using _Sse = _VecBuiltin<_UsedBytes>;
template <int _UsedBytes = 32> using _Avx = _VecBuiltin<_UsedBytes>;
template <int _UsedBytes = 64> using _Avx512 = _VecBltnBtmsk<_UsedBytes>;
template <int _UsedBytes = 16> using _Neon = _VecBuiltin<_UsedBytes>;
// implementation-defined:
using __sse = _Sse<>;
using __avx = _Avx<>;
using __avx512 = _Avx512<>;
using __neon = _Neon<>;
using __neon128 = _Neon<16>;
using __neon64 = _Neon<8>;
// standard:
template <typename _Tp, size_t _Np, typename...> struct deduce;
template <int _Np> using fixed_size = _Fixed<_Np>;
using scalar = _Scalar;
// }}}
} // namespace simd_abi
// forward declarations is_simd(_mask), simd(_mask), simd_size {{{
template <typename _Tp> struct is_simd;
template <typename _Tp> struct is_simd_mask;
template <typename _Tp, typename _Abi> class simd;
template <typename _Tp, typename _Abi> class simd_mask;
template <typename _Tp, typename _Abi> struct simd_size;
// }}}
// load/store flags {{{
struct element_aligned_tag
{
};
struct vector_aligned_tag
{
};
template <size_t _Np> struct overaligned_tag
{
static constexpr size_t _S_alignment = _Np;
};
inline constexpr element_aligned_tag element_aligned = {};
inline constexpr vector_aligned_tag vector_aligned = {};
template <size_t _Np> inline constexpr overaligned_tag<_Np> overaligned = {};
// }}}
// vvv ---- type traits ---- vvv
// integer type aliases{{{
using _UChar = unsigned char;
using _SChar = signed char;
using _UShort = unsigned short;
using _UInt = unsigned int;
using _ULong = unsigned long;
using _ULLong = unsigned long long;
using _LLong = long long;
//}}}
// __identity/__id{{{
template <typename _Tp> struct __identity
{
using type = _Tp;
};
template <typename _Tp> using __id = typename __identity<_Tp>::type;
// }}}
// __first_of_pack{{{
template <typename _T0, typename...> struct __first_of_pack
{
using type = _T0;
};
template <typename... _Ts>
using __first_of_pack_t = typename __first_of_pack<_Ts...>::type;
//}}}
// __value_type_or_identity_t {{{
template <typename _Tp>
typename _Tp::value_type
__value_type_or_identity_impl(int);
template <typename _Tp>
_Tp
__value_type_or_identity_impl(float);
template <typename _Tp>
using __value_type_or_identity_t
= decltype(__value_type_or_identity_impl<_Tp>(int()));
// }}}
// __is_vectorizable {{{
template <typename _Tp>
struct __is_vectorizable : public std::is_arithmetic<std::remove_reference_t<_Tp>>
{
};
template <> struct __is_vectorizable<bool> : public false_type
{
};
template <typename _Tp>
inline constexpr bool __is_vectorizable_v = __is_vectorizable<_Tp>::value;
// Deduces to a vectorizable type
template <typename _Tp, typename = enable_if_t<__is_vectorizable_v<_Tp>>>
using _Vectorizable = _Tp;
// }}}
// _LoadStorePtr / __is_possible_loadstore_conversion {{{
template <typename _Ptr, typename _ValueType>
struct __is_possible_loadstore_conversion
: conjunction<__is_vectorizable<_Ptr>, __is_vectorizable<_ValueType>>
{
};
template <> struct __is_possible_loadstore_conversion<bool, bool> : true_type
{
};
// Deduces to a type allowed for load/store with the given value type.
template <typename _Ptr, typename _ValueType,
typename = enable_if_t<
__is_possible_loadstore_conversion<_Ptr, _ValueType>::value>>
using _LoadStorePtr = _Ptr;
// }}}
// _SizeConstant{{{
template <size_t _X> using _SizeConstant = integral_constant<size_t, _X>;
// }}}
// __is_bitmask{{{
template <typename _Tp, typename = std::void_t<>>
struct __is_bitmask : false_type
{
};
template <typename _Tp>
inline constexpr bool __is_bitmask_v = __is_bitmask<_Tp>::value;
// the __mmaskXX case:
template <typename _Tp>
struct __is_bitmask<_Tp, std::void_t<decltype(std::declval<unsigned&>()
= std::declval<_Tp>() & 1u)>>
: true_type
{
};
// }}}
// __int_for_sizeof{{{
template <size_t> struct __int_for_sizeof;
template <> struct __int_for_sizeof<1>
{
using type = signed char;
};
template <> struct __int_for_sizeof<2>
{
using type = signed short;
};
template <> struct __int_for_sizeof<4>
{
using type = signed int;
};
template <> struct __int_for_sizeof<8>
{
using type = signed long long;
};
#ifdef __SIZEOF_INT128__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
template <> struct __int_for_sizeof<16>
{
using type = __int128;
};
#pragma GCC diagnostic pop
#endif // __SIZEOF_INT128__
template <typename _Tp>
using __int_for_sizeof_t = typename __int_for_sizeof<sizeof(_Tp)>::type;
template <size_t _Np>
using __int_with_sizeof_t = typename __int_for_sizeof<_Np>::type;
// }}}
// __is_fixed_size_abi{{{
template <typename _Tp> struct __is_fixed_size_abi : false_type
{
};
template <int _Np>
struct __is_fixed_size_abi<simd_abi::fixed_size<_Np>> : true_type
{
};
template <typename _Tp>
inline constexpr bool __is_fixed_size_abi_v = __is_fixed_size_abi<_Tp>::value;
// }}}
// constexpr feature detection{{{
constexpr inline bool __have_mmx = _GLIBCXX_SIMD_HAVE_MMX;
constexpr inline bool __have_sse = _GLIBCXX_SIMD_HAVE_SSE;
constexpr inline bool __have_sse2 = _GLIBCXX_SIMD_HAVE_SSE2;
constexpr inline bool __have_sse3 = _GLIBCXX_SIMD_HAVE_SSE3;
constexpr inline bool __have_ssse3 = _GLIBCXX_SIMD_HAVE_SSSE3;
constexpr inline bool __have_sse4_1 = _GLIBCXX_SIMD_HAVE_SSE4_1;
constexpr inline bool __have_sse4_2 = _GLIBCXX_SIMD_HAVE_SSE4_2;
constexpr inline bool __have_xop = _GLIBCXX_SIMD_HAVE_XOP;
constexpr inline bool __have_avx = _GLIBCXX_SIMD_HAVE_AVX;
constexpr inline bool __have_avx2 = _GLIBCXX_SIMD_HAVE_AVX2;
constexpr inline bool __have_bmi = _GLIBCXX_SIMD_HAVE_BMI1;
constexpr inline bool __have_bmi2 = _GLIBCXX_SIMD_HAVE_BMI2;
constexpr inline bool __have_lzcnt = _GLIBCXX_SIMD_HAVE_LZCNT;
constexpr inline bool __have_sse4a = _GLIBCXX_SIMD_HAVE_SSE4A;
constexpr inline bool __have_fma = _GLIBCXX_SIMD_HAVE_FMA;
constexpr inline bool __have_fma4 = _GLIBCXX_SIMD_HAVE_FMA4;
constexpr inline bool __have_f16c = _GLIBCXX_SIMD_HAVE_F16C;
constexpr inline bool __have_popcnt = _GLIBCXX_SIMD_HAVE_POPCNT;
constexpr inline bool __have_avx512f = _GLIBCXX_SIMD_HAVE_AVX512F;
constexpr inline bool __have_avx512dq = _GLIBCXX_SIMD_HAVE_AVX512DQ;
constexpr inline bool __have_avx512vl = _GLIBCXX_SIMD_HAVE_AVX512VL;
constexpr inline bool __have_avx512bw = _GLIBCXX_SIMD_HAVE_AVX512BW;
constexpr inline bool __have_avx512dq_vl = __have_avx512dq && __have_avx512vl;
constexpr inline bool __have_avx512bw_vl = __have_avx512bw && __have_avx512vl;
constexpr inline bool __have_neon = _GLIBCXX_SIMD_HAVE_NEON;
constexpr inline bool __have_neon_a32 = _GLIBCXX_SIMD_HAVE_NEON_A32;
constexpr inline bool __have_neon_a64 = _GLIBCXX_SIMD_HAVE_NEON_A64;
// }}}
// __is_scalar_abi {{{
template <typename _Abi>
constexpr bool
__is_scalar_abi()
{
return std::is_same_v<simd_abi::scalar, _Abi>;
}
// }}}
// __abi_bytes_v {{{
template <template <int> class _Abi, int _Bytes>
constexpr int
__abi_bytes_impl(_Abi<_Bytes>*)
{
return _Bytes;
}
template <typename _Tp>
constexpr int
__abi_bytes_impl(_Tp*)
{
return -1;
}
template <typename _Abi>
inline constexpr int __abi_bytes_v
= __abi_bytes_impl(static_cast<_Abi*>(nullptr));
// }}}
// __is_builtin_bitmask_abi {{{
template <typename _Abi>
constexpr bool
__is_builtin_bitmask_abi()
{
return std::is_same_v<simd_abi::_VecBltnBtmsk<__abi_bytes_v<_Abi>>, _Abi>;
}
// }}}
// __is_sse_abi {{{
template <typename _Abi>
constexpr bool
__is_sse_abi()
{
constexpr auto _Bytes = __abi_bytes_v<_Abi>;
return _Bytes <= 16 && std::is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
}
// }}}
// __is_avx_abi {{{
template <typename _Abi>
constexpr bool
__is_avx_abi()
{
constexpr auto _Bytes = __abi_bytes_v<_Abi>;
return _Bytes > 16 && _Bytes <= 32
&& std::is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
}
// }}}
// __is_avx512_abi {{{
template <typename _Abi>
constexpr bool
__is_avx512_abi()
{
constexpr auto _Bytes = __abi_bytes_v<_Abi>;
return _Bytes <= 64 && std::is_same_v<simd_abi::_Avx512<_Bytes>, _Abi>;
}
// }}}
// __is_neon_abi {{{
template <typename _Abi>
constexpr bool
__is_neon_abi()
{
constexpr auto _Bytes = __abi_bytes_v<_Abi>;
return _Bytes <= 16 && std::is_same_v<simd_abi::_VecBuiltin<_Bytes>, _Abi>;
}
// }}}
// __make_dependent_t {{{
template <typename, typename _Up> struct __make_dependent
{
using type = _Up;
};
template <typename _Tp, typename _Up>
using __make_dependent_t = typename __make_dependent<_Tp, _Up>::type;
// }}}
// ^^^ ---- type traits ---- ^^^
// __assert_unreachable{{{
template <typename _Tp> struct __assert_unreachable
{
static_assert(!std::is_same_v<_Tp, _Tp>, "this should be unreachable");
};
// }}}
// __size_or_zero_v {{{
template <typename _Tp, typename _Ap, size_t _Np = simd_size<_Tp, _Ap>::value>
constexpr size_t
__size_or_zero_dispatch(int)
{
return _Np;
}
template <typename _Tp, typename _Ap>
constexpr size_t
__size_or_zero_dispatch(float)
{
return 0;
}
template <typename _Tp, typename _Ap>
inline constexpr size_t __size_or_zero_v = __size_or_zero_dispatch<_Tp, _Ap>(0);
// }}}
// __bit_cast {{{
template <typename _To, typename _From>
_GLIBCXX_SIMD_INTRINSIC _To
__bit_cast(const _From __x)
{
static_assert(sizeof(_To) == sizeof(_From));
_To __r;
__builtin_memcpy(reinterpret_cast<char*>(&__r),
reinterpret_cast<const char*>(&__x), sizeof(_To));
return __r;
}
// }}}
// __div_roundup {{{
inline constexpr std::size_t
__div_roundup(std::size_t __a, std::size_t __b)
{
return (__a + __b - 1) / __b;
}
// }}}
// _ExactBool{{{
class _ExactBool
{
const bool _M_data;
public:
_GLIBCXX_SIMD_INTRINSIC constexpr _ExactBool(bool __b) : _M_data(__b) {}
_ExactBool(int) = delete;
_GLIBCXX_SIMD_INTRINSIC constexpr operator bool() const { return _M_data; }
};
// }}}
// __execute_n_times{{{
template <typename _Fp, size_t... _I>
_GLIBCXX_SIMD_INTRINSIC constexpr void
__execute_on_index_sequence(_Fp&& __f, std::index_sequence<_I...>)
{
[[maybe_unused]] auto&& __x = {(__f(_SizeConstant<_I>()), 0)...};
}
template <typename _Fp>
_GLIBCXX_SIMD_INTRINSIC constexpr void
__execute_on_index_sequence(_Fp&&, std::index_sequence<>)
{}
template <size_t _Np, typename _Fp>
_GLIBCXX_SIMD_INTRINSIC constexpr void
__execute_n_times(_Fp&& __f)
{
__execute_on_index_sequence(static_cast<_Fp&&>(__f),
std::make_index_sequence<_Np>{});
}
// }}}
// __generate_from_n_evaluations{{{
template <typename _R, typename _Fp, size_t... _I>
_GLIBCXX_SIMD_INTRINSIC constexpr _R
__execute_on_index_sequence_with_return(_Fp&& __f, std::index_sequence<_I...>)
{
return _R{__f(_SizeConstant<_I>())...};
}
template <size_t _Np, typename _R, typename _Fp>
_GLIBCXX_SIMD_INTRINSIC constexpr _R
__generate_from_n_evaluations(_Fp&& __f)
{
return __execute_on_index_sequence_with_return<_R>(
static_cast<_Fp&&>(__f), std::make_index_sequence<_Np>{});
}
// }}}
// __call_with_n_evaluations{{{
template <size_t... _I, typename _F0, typename _FArgs>
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__call_with_n_evaluations(std::index_sequence<_I...>, _F0&& __f0,
_FArgs&& __fargs)
{
return __f0(__fargs(_SizeConstant<_I>())...);
}
template <size_t _Np, typename _F0, typename _FArgs>
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__call_with_n_evaluations(_F0&& __f0, _FArgs&& __fargs)
{
return __call_with_n_evaluations(std::make_index_sequence<_Np>{},
static_cast<_F0&&>(__f0),
static_cast<_FArgs&&>(__fargs));
}
// }}}
// __call_with_subscripts{{{
template <size_t _First = 0, size_t... _It, typename _Tp, typename _Fp>
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__call_with_subscripts(_Tp&& __x, index_sequence<_It...>, _Fp&& __fun)
{
return __fun(__x[_First + _It]...);
}
template <size_t _Np, size_t _First = 0, typename _Tp, typename _Fp>
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__call_with_subscripts(_Tp&& __x, _Fp&& __fun)
{
return __call_with_subscripts<_First>(static_cast<_Tp&&>(__x),
std::make_index_sequence<_Np>(),
static_cast<_Fp&&>(__fun));
}
// }}}
// __may_alias{{{
/**\internal
* Helper __may_alias<_Tp> that turns _Tp into the type to be used for an
* aliasing pointer. This adds the __may_alias attribute to _Tp (with compilers
* that support it).
*/
template <typename _Tp> using __may_alias [[__gnu__::__may_alias__]] = _Tp;
// }}}
// _UnsupportedBase {{{
// simd and simd_mask base for unsupported <_Tp, _Abi>
struct _UnsupportedBase
{
_UnsupportedBase() = delete;
_UnsupportedBase(const _UnsupportedBase&) = delete;
_UnsupportedBase& operator=(const _UnsupportedBase&) = delete;
~_UnsupportedBase() = delete;
};
// }}}
// _InvalidTraits {{{
/**
* \internal
* Defines the implementation of __a given <_Tp, _Abi>.
*
* Implementations must ensure that only valid <_Tp, _Abi> instantiations are
* possible. Static assertions in the type definition do not suffice. It is
* important that SFINAE works.
*/
struct _InvalidTraits
{
using _IsValid = false_type;
using _SimdBase = _UnsupportedBase;
using _MaskBase = _UnsupportedBase;
static constexpr size_t _S_simd_align = 1;
struct _SimdImpl;
struct _SimdMember
{
};
struct _SimdCastType;
static constexpr size_t _S_mask_align = 1;
struct _MaskImpl;
struct _MaskMember
{
};
struct _MaskCastType;
};
// }}}
// _SimdTraits {{{
template <typename _Tp, typename _Abi, typename = std::void_t<>>
struct _SimdTraits : _InvalidTraits
{
};
// }}}
// __private_init, __bitset_init{{{
/**
* \internal
* Tag used for private init constructor of simd and simd_mask
*/
inline constexpr struct _PrivateInit
{
} __private_init = {};
inline constexpr struct _BitsetInit
{
} __bitset_init = {};
// }}}
// __is_narrowing_conversion<_From, _To>{{{
template <typename _From, typename _To, bool = std::is_arithmetic<_From>::value,
bool = std::is_arithmetic<_To>::value>
struct __is_narrowing_conversion;
// ignore "warning C4018: '<': signed/unsigned mismatch" in the following trait.
// The implicit conversions will do the right thing here.
template <typename _From, typename _To>
struct __is_narrowing_conversion<_From, _To, true, true>
: public __bool_constant<(
std::numeric_limits<_From>::digits > std::numeric_limits<_To>::digits
|| std::numeric_limits<_From>::max() > std::numeric_limits<_To>::max()
|| std::numeric_limits<_From>::lowest()
< std::numeric_limits<_To>::lowest()
|| (std::is_signed<_From>::value && std::is_unsigned<_To>::value))>
{
};
template <typename _Tp>
struct __is_narrowing_conversion<bool, _Tp, true, true> : public true_type
{
};
template <>
struct __is_narrowing_conversion<bool, bool, true, true> : public false_type
{
};
template <typename _Tp>
struct __is_narrowing_conversion<_Tp, _Tp, true, true> : public false_type
{
};
template <typename _From, typename _To>
struct __is_narrowing_conversion<_From, _To, false, true>
: public negation<std::is_convertible<_From, _To>>
{
};
// }}}
// __converts_to_higher_integer_rank{{{
template <typename _From, typename _To, bool = (sizeof(_From) < sizeof(_To))>
struct __converts_to_higher_integer_rank : public true_type
{
};
// this may fail for char -> short if sizeof(char) == sizeof(short)
template <typename _From, typename _To>
struct __converts_to_higher_integer_rank<_From, _To, false>
: public std::is_same<decltype(std::declval<_From>() + std::declval<_To>()),
_To>
{
};
// }}}
// __is_aligned(_v){{{
template <typename _Flag, size_t _Alignment> struct __is_aligned;
template <size_t _Alignment>
struct __is_aligned<vector_aligned_tag, _Alignment> : public true_type
{
};
template <size_t _Alignment>
struct __is_aligned<element_aligned_tag, _Alignment> : public false_type
{
};
template <size_t _GivenAlignment, size_t _Alignment>
struct __is_aligned<overaligned_tag<_GivenAlignment>, _Alignment>
: public std::integral_constant<bool, (_GivenAlignment % _Alignment == 0)>
{
};
template <typename _Flag, size_t _Alignment>
inline constexpr bool __is_aligned_v = __is_aligned<_Flag, _Alignment>::value;
// }}}
// __data(simd/simd_mask) {{{
template <typename _Tp, typename _Ap>
_GLIBCXX_SIMD_INTRINSIC constexpr const auto&
__data(const simd<_Tp, _Ap>& __x);
template <typename _Tp, typename _Ap>
_GLIBCXX_SIMD_INTRINSIC constexpr auto&
__data(simd<_Tp, _Ap>& __x);
template <typename _Tp, typename _Ap>
_GLIBCXX_SIMD_INTRINSIC constexpr const auto&
__data(const simd_mask<_Tp, _Ap>& __x);
template <typename _Tp, typename _Ap>
_GLIBCXX_SIMD_INTRINSIC constexpr auto&
__data(simd_mask<_Tp, _Ap>& __x);
// }}}
// _SimdConverter {{{
template <typename _FromT, typename _FromA, typename _ToT, typename _ToA,
typename = void>
struct _SimdConverter;
template <typename _Tp, typename _Ap>
struct _SimdConverter<_Tp, _Ap, _Tp, _Ap, void>
{
template <typename _Up>
_GLIBCXX_SIMD_INTRINSIC const _Up& operator()(const _Up& __x)
{
return __x;
}
};
// }}}
// __to_value_type_or_member_type {{{
template <typename _V>
_GLIBCXX_SIMD_INTRINSIC constexpr auto
__to_value_type_or_member_type(const _V& __x) -> decltype(__data(__x))
{
return __data(__x);
}
template <typename _V>
_GLIBCXX_SIMD_INTRINSIC constexpr const typename _V::value_type&
__to_value_type_or_member_type(const typename _V::value_type& __x)
{
return __x;
}
// }}}
// __bool_storage_member_type{{{
template <size_t _Size> struct __bool_storage_member_type;
template <size_t _Size>
using __bool_storage_member_type_t =
typename __bool_storage_member_type<_Size>::type;
// }}}
// _SimdTuple {{{
// why not std::tuple?
// 1. std::tuple gives no guarantee about the storage order, but I require
// storage
// equivalent to std::array<_Tp, _Np>
// 2. direct access to the element type (first template argument)
// 3. enforces equal element type, only different _Abi types are allowed
template <typename _Tp, typename... _Abis> struct _SimdTuple;
//}}}
// __fixed_size_storage_t {{{
template <typename _Tp, int _Np> struct __fixed_size_storage;
template <typename _Tp, int _Np>
using __fixed_size_storage_t = typename __fixed_size_storage<_Tp, _Np>::type;
// }}}
// _SimdWrapper fwd decl{{{
template <typename _Tp, size_t _Size, typename = std::void_t<>>
struct _SimdWrapper;
template <typename _Tp>
using _SimdWrapper8 = _SimdWrapper<_Tp, 8 / sizeof(_Tp)>;
template <typename _Tp>
using _SimdWrapper16 = _SimdWrapper<_Tp, 16 / sizeof(_Tp)>;
template <typename _Tp>
using _SimdWrapper32 = _SimdWrapper<_Tp, 32 / sizeof(_Tp)>;
template <typename _Tp>
using _SimdWrapper64 = _SimdWrapper<_Tp, 64 / sizeof(_Tp)>;
// }}}
// __is_simd_wrapper {{{
template <typename _Tp> struct __is_simd_wrapper : false_type
{
};
template <typename _Tp, size_t _Np>
struct __is_simd_wrapper<_SimdWrapper<_Tp, _Np>> : true_type
{
};
template <typename _Tp>
inline constexpr bool __is_simd_wrapper_v = __is_simd_wrapper<_Tp>::value;
// }}}
// _BitOps {{{
struct _BitOps
{
// __popcount {{{
static constexpr _UInt __popcount(_UInt __x)
{
return __builtin_popcount(__x);
}
static constexpr _ULong __popcount(_ULong __x)
{
return __builtin_popcountl(__x);
}
static constexpr _ULLong __popcount(_ULLong __x)
{
return __builtin_popcountll(__x);
}
// }}}
// __ctz/__clz {{{
static constexpr _UInt __ctz(_UInt __x) { return __builtin_ctz(__x); }
static constexpr _ULong __ctz(_ULong __x) { return __builtin_ctzl(__x); }
static constexpr _ULLong __ctz(_ULLong __x) { return __builtin_ctzll(__x); }
static constexpr _UInt __clz(_UInt __x) { return __builtin_clz(__x); }
static constexpr _ULong __clz(_ULong __x) { return __builtin_clzl(__x); }
static constexpr _ULLong __clz(_ULLong __x) { return __builtin_clzll(__x); }
// }}}
// __bit_iteration {{{
template <typename _Tp, typename _Fp>
static void __bit_iteration(_Tp __mask, _Fp&& __f)
{
static_assert(sizeof(_ULLong) >= sizeof(_Tp));
std::conditional_t<sizeof(_Tp) <= sizeof(_UInt), _UInt, _ULLong> __k;
if constexpr (std::is_convertible_v<_Tp, decltype(__k)>)
__k = __mask;
else
__k = __mask.to_ullong();
switch (__popcount(__k))
{
default:
do
{
__f(__ctz(__k));
__k &= (__k - 1);
}
while (__k);
break;
/*case 3:
__f(__ctz(__k));
__k &= (__k - 1);
[[fallthrough]];*/
case 2:
__f(__ctz(__k));
[[fallthrough]];
case 1:
__f(__popcount(~decltype(__k)()) - 1 - __clz(__k));
[[fallthrough]];
case 0:
break;
}
}
//}}}
// __firstbit{{{
template <typename _Tp>
_GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONST static auto __firstbit(_Tp __bits)
{
static_assert(std::is_integral_v<_Tp>,
"__firstbit requires an integral argument");
if constexpr (sizeof(_Tp) <= sizeof(int))
return __builtin_ctz(__bits);
else if constexpr (alignof(_ULLong) == 8)
return __builtin_ctzll(__bits);
else
{
_UInt __lo = __bits;
return __lo == 0 ? 32 + __builtin_ctz(__bits >> 32)
: __builtin_ctz(__lo);
}
}
// }}}
// __lastbit{{{
template <typename _Tp>
_GLIBCXX_SIMD_INTRINSIC _GLIBCXX_SIMD_CONST static auto __lastbit(_Tp __bits)
{
static_assert(std::is_integral_v<_Tp>,
"__lastbit requires an integral argument");
if constexpr (sizeof(_Tp) <= sizeof(int))
return 31 - __builtin_clz(__bits);
else if constexpr (alignof(_ULLong) == 8)
return 63 - __builtin_clzll(__bits);
else
{
_UInt __lo = __bits;
_UInt __hi = __bits >> 32u;
return __hi == 0 ? 31 - __builtin_clz(__lo) : 63 - __builtin_clz(__hi);
}
}
// }}}
};
//}}}
// __increment, __decrement {{{
template <typename _Tp = void> struct __increment
{
constexpr _Tp operator()(_Tp __a) const { return ++__a; }
};
template <> struct __increment<void>
{
template <typename _Tp> constexpr _Tp operator()(_Tp __a) const
{
return ++__a;
}
};
template <typename _Tp = void> struct __decrement
{
constexpr _Tp operator()(_Tp __a) const { return --__a; }
};
template <> struct __decrement<void>
{
template <typename _Tp> constexpr _Tp operator()(_Tp __a) const
{
return --__a;
}
};
// }}}
// _ValuePreserving(OrInt) {{{
template <typename _From, typename _To,
typename = enable_if_t<negation<
__is_narrowing_conversion<__remove_cvref_t<_From>, _To>>::value>>
using _ValuePreserving = _From;
template <typename _From, typename _To,
typename _DecayedFrom = __remove_cvref_t<_From>,
typename = enable_if_t<conjunction<
is_convertible<_From, _To>,
disjunction<
is_same<_DecayedFrom, _To>, is_same<_DecayedFrom, int>,
conjunction<is_same<_DecayedFrom, _UInt>, is_unsigned<_To>>,
negation<__is_narrowing_conversion<_DecayedFrom, _To>>>>::value>>
using _ValuePreservingOrInt = _From;
// }}}
// __intrinsic_type {{{
template <typename _Tp, size_t _Bytes, typename = std::void_t<>>
struct __intrinsic_type;
template <typename _Tp, size_t _Size>
using __intrinsic_type_t =
typename __intrinsic_type<_Tp, _Size * sizeof(_Tp)>::type;
template <typename _Tp>
using __intrinsic_type2_t = typename __intrinsic_type<_Tp, 2>::type;
template <typename _Tp>
using __intrinsic_type4_t = typename __intrinsic_type<_Tp, 4>::type;
template <typename _Tp>
using __intrinsic_type8_t = typename __intrinsic_type<_Tp, 8>::type;
template <typename _Tp>
using __intrinsic_type16_t = typename __intrinsic_type<_Tp, 16>::type;
template <typename _Tp>
using __intrinsic_type32_t = typename __intrinsic_type<_Tp, 32>::type;
template <typename _Tp>
using __intrinsic_type64_t = typename __intrinsic_type<_Tp, 64>::type;
template <typename _Tp>
using __intrinsic_type128_t = typename __intrinsic_type<_Tp, 128>::type;
// }}}
// _BitMask {{{
template <size_t _Np, bool _Sanitized = false> struct _BitMask;
template <size_t _Np, bool _Sanitized>
struct __is_bitmask<_BitMask<_Np, _Sanitized>, void> : true_type
{
};
template <size_t _Np> using _SanitizedBitMask = _BitMask<_Np, true>;
template <size_t _Np, bool _Sanitized> struct _BitMask
{
static_assert(_Np > 0);
static constexpr size_t _NBytes = __div_roundup(_Np, CHAR_BIT);
using _Tp = conditional_t<_Np == 1, bool,
make_unsigned_t<__int_with_sizeof_t<std::min(
sizeof(_ULLong), __next_power_of_2(_NBytes))>>>;
static constexpr int _S_array_size = __div_roundup(_NBytes, sizeof(_Tp));
_Tp _M_bits[_S_array_size];
static constexpr int _S_unused_bits
= _Np == 1 ? 0 : _S_array_size * sizeof(_Tp) * CHAR_BIT - _Np;
static constexpr _Tp _S_bitmask = +_Tp(~_Tp()) >> _S_unused_bits;
constexpr _BitMask() noexcept = default;
constexpr _BitMask(unsigned long long __x) noexcept
: _M_bits{static_cast<_Tp>(__x)}
{}
_BitMask(std::bitset<_Np> __x) noexcept : _BitMask(__x.to_ullong()) {}
constexpr _BitMask(const _BitMask&) noexcept = default;
template <bool _RhsSanitized, typename = enable_if_t<_RhsSanitized == false
&& _Sanitized == true>>
constexpr _BitMask(const _BitMask<_Np, _RhsSanitized>& __rhs) noexcept
: _BitMask(__rhs._M_sanitized())
{}
constexpr operator _SimdWrapper<bool, _Np>() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
// precondition: is sanitized
constexpr _Tp _M_to_bits() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
// precondition: is sanitized
constexpr unsigned long long to_ullong() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];
}
// precondition: is sanitized
constexpr unsigned long to_ulong() const noexcept
{
static_assert(_S_array_size == 1);
return _M_bits[0];