-
Notifications
You must be signed in to change notification settings - Fork 12
/
CorradeCpu.hpp
1716 lines (1498 loc) · 65.6 KB
/
CorradeCpu.hpp
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
/*
Corrade::Cpu
— compile-time and runtime CPU feature detection and dispatch
https://doc.magnum.graphics/corrade/namespaceCorrade_1_1Cpu.html
This is a single-header library generated from the Corrade project. With
the goal being easy integration, it's deliberately free of all comments
to keep the file size small. More info, detailed changelogs and docs here:
- Project homepage — https://magnum.graphics/corrade/
- Documentation — https://doc.magnum.graphics/corrade/
- GitHub project page — https://github.com/mosra/corrade
- GitHub Singles repository — https://github.com/mosra/magnum-singles
The library has a separate non-inline implementation part, enable it *just
once* like this:
#define CORRADE_CPU_IMPLEMENTATION
#include <CorradeCpu.hpp>
If you need the deinlined symbols to be exported from a shared library,
`#define CORRADE_UTILITY_EXPORT` as appropriate. To enable the IFUNC
functionality, `#define CORRADE_CPU_USE_IFUNC` before including the file.
v2020.06-1687-g6b5f (2024-06-29)
- FreeBSD and Emscripten compatibility fixes
v2020.06-1454-gfc3b7 (2023-08-27)
- Added BMI2 detection on x86
- Fixed an issue on GCC 12+ and Clang, where only one of multiple
CORRADE_ENABLE_ macro annotations would get used
- Fixed a potential build issue on x86 if none of the extra instruction
sets are enabled at compile time
- Compatibility with C++20 which removes the <ciso646> header
v2020.06-1040-g30cd2 (2022-09-05)
- Fixed a build issue on platforms that are neither x86, ARM nor WASM
- Renamed to CorradeCpu.hpp to imply the separate implementation part
consistently with other header-only libraries
v2020.06-1018-gef42a6 (2022-08-13)
- Properly checking XSAVE prerequisites for AVX-512
v2020.06-1015-g8cbd6 (2022-08-02)
- Initial release
Generated from Corrade v2020.06-1687-g6b5f (2024-06-29), 1716 / 1995 LoC
*/
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022, 2023
Vladimír Vondruš <[email protected]>
Copyright © 2023 Robert Clausecker <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <type_traits>
#ifdef __ANDROID__
#define CORRADE_TARGET_ANDROID
#endif
#ifdef __EMSCRIPTEN__
#define CORRADE_TARGET_EMSCRIPTEN
#endif
#ifdef __APPLE__
#define CORRADE_TARGET_APPLE
#endif
#ifndef Corrade_configure_h
#define Corrade_configure_h
#if defined(__i386) || defined(__x86_64) || defined(_M_IX86) || defined(_M_X64)
#define CORRADE_TARGET_X86
#elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
#define CORRADE_TARGET_ARM
#elif defined(__powerpc__) || defined(__powerpc64__) || defined(_M_PPC)
#define CORRADE_TARGET_POWERPC
#elif defined(__wasm__)
#define CORRADE_TARGET_WASM
#endif
#if defined(CORRADE_TARGET_EMSCRIPTEN) && (defined(CORRADE_TARGET_X86) || defined(CORRADE_TARGET_ARM) || defined(CORRADE_TARGET_POWERPC))
#error CORRADE_TARGET_X86 / _ARM / _POWERPC defined on Emscripten
#endif
#if !defined(__x86_64) && !defined(_M_X64) && !defined(__aarch64__) && !defined(_M_ARM64) && !defined(__powerpc64__) && !defined(__wasm64__)
#define CORRADE_TARGET_32BIT
#endif
#ifdef _MSC_VER
#ifdef _MSVC_LANG
#define CORRADE_CXX_STANDARD _MSVC_LANG
#else
#define CORRADE_CXX_STANDARD 201103L
#endif
#else
#define CORRADE_CXX_STANDARD __cplusplus
#endif
#if CORRADE_CXX_STANDARD >= 202002
#include <version>
#else
#include <ciso646>
#endif
#ifdef _LIBCPP_VERSION
#define CORRADE_TARGET_LIBCXX
#elif defined(_CPPLIB_VER)
#define CORRADE_TARGET_DINKUMWARE
#elif defined(__GLIBCXX__)
#define CORRADE_TARGET_LIBSTDCXX
#elif defined(__has_include)
#if __has_include(<bits/c++config.h>)
#include <bits/c++config.h>
#ifdef __GLIBCXX__
#define CORRADE_TARGET_LIBSTDCXX
#endif
#endif
#elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
#define CORRADE_TARGET_LIBSTDCXX
#else
#endif
#ifdef __GNUC__
#define CORRADE_TARGET_GCC
#endif
#ifdef __clang__
#define CORRADE_TARGET_CLANG
#endif
#if defined(__clang__) && defined(_MSC_VER)
#define CORRADE_TARGET_CLANG_CL
#endif
#if defined(__clang__) && defined(__apple_build_version__)
#define CORRADE_TARGET_APPLE_CLANG
#endif
#ifdef _MSC_VER
#define CORRADE_TARGET_MSVC
#endif
#ifdef __MINGW32__
#define CORRADE_TARGET_MINGW
#endif
#ifdef __BYTE_ORDER__
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define CORRADE_TARGET_BIG_ENDIAN
#elif __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error what kind of endianness is this?
#endif
#elif defined(__hppa__) || \
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
(defined(__MIPS__) && defined(__MIPSEB__)) || \
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
defined(__sparc__)
#define CORRADE_TARGET_BIG_ENDIAN
#endif
#ifdef CORRADE_TARGET_X86
#ifdef CORRADE_TARGET_GCC
#ifdef __SSE2__
#define CORRADE_TARGET_SSE2
#endif
#ifdef __SSE3__
#define CORRADE_TARGET_SSE3
#endif
#ifdef __SSSE3__
#define CORRADE_TARGET_SSSE3
#endif
#ifdef __SSE4_1__
#define CORRADE_TARGET_SSE41
#endif
#ifdef __SSE4_2__
#define CORRADE_TARGET_SSE42
#endif
#elif defined(CORRADE_TARGET_MSVC)
#if (defined(_M_IX86_FP) && _M_IX86_FP == 2) || defined(_M_AMD64) || defined(_M_X64)
#define CORRADE_TARGET_SSE2
#endif
#ifdef __AVX__
#define CORRADE_TARGET_SSE3
#define CORRADE_TARGET_SSSE3
#define CORRADE_TARGET_SSE41
#define CORRADE_TARGET_SSE42
#endif
#endif
#if defined(CORRADE_TARGET_GCC) || defined(CORRADE_TARGET_MSVC)
#ifdef __AVX__
#define CORRADE_TARGET_AVX
#endif
#ifdef __AVX2__
#define CORRADE_TARGET_AVX2
#endif
#ifdef __AVX512F__
#define CORRADE_TARGET_AVX512F
#endif
#endif
#ifdef CORRADE_TARGET_GCC
#ifdef __POPCNT__
#define CORRADE_TARGET_POPCNT
#endif
#ifdef __LZCNT__
#define CORRADE_TARGET_LZCNT
#endif
#ifdef __BMI__
#define CORRADE_TARGET_BMI1
#endif
#ifdef __BMI2__
#define CORRADE_TARGET_BMI2
#endif
#elif defined(CORRADE_TARGET_MSVC)
#ifdef __AVX__
#if !defined(CORRADE_TARGET_CLANG_CL) || defined(__POPCNT__)
#define CORRADE_TARGET_POPCNT
#endif
#endif
#ifdef __AVX2__
#if !defined(CORRADE_TARGET_CLANG_CL) || defined(__LZCNT__)
#define CORRADE_TARGET_LZCNT
#endif
#if !defined(CORRADE_TARGET_CLANG_CL) || defined(__BMI__)
#define CORRADE_TARGET_BMI1
#endif
#if !defined(CORRADE_TARGET_CLANG_CL) || defined(__BMI2__)
#define CORRADE_TARGET_BMI2
#endif
#endif
#endif
#ifdef CORRADE_TARGET_GCC
#ifdef __F16C__
#define CORRADE_TARGET_AVX_F16C
#endif
#ifdef __FMA__
#define CORRADE_TARGET_AVX_FMA
#endif
#elif defined(CORRADE_TARGET_MSVC) && defined(__AVX2__)
#if !defined(CORRADE_TARGET_CLANG_CL) || defined(__F16C__)
#define CORRADE_TARGET_AVX_F16C
#endif
#if !defined(CORRADE_TARGET_CLANG_CL) || defined(__FMA__)
#define CORRADE_TARGET_AVX_FMA
#endif
#endif
#elif defined(CORRADE_TARGET_ARM)
#ifdef __ARM_NEON
#define CORRADE_TARGET_NEON
#if defined(__ARM_FEATURE_FMA) && (__ARM_NEON_FP || defined(__aarch64__))
#define CORRADE_TARGET_NEON_FMA
#endif
#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
#define CORRADE_TARGET_NEON_FP16
#endif
#endif
#elif defined(CORRADE_TARGET_WASM)
#if defined(CORRADE_TARGET_EMSCRIPTEN) && !defined(__EMSCRIPTEN_major__)
#include <emscripten/version.h>
#endif
#if defined(__wasm_simd128__) && __clang_major__ >= 13 && __EMSCRIPTEN_major__*10000 + __EMSCRIPTEN_minor__*100 + __EMSCRIPTEN_tiny__ >= 20018
#define CORRADE_TARGET_SIMD128
#endif
#endif
#ifdef CORRADE_CPU_USE_IFUNC
#ifdef __has_feature
#if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || __has_feature(memory_sanitizer) || __has_feature(undefined_behavior_sanitizer)
#define _CORRADE_SANITIZER_IFUNC_DETECTED
#endif
#elif defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
#define _CORRADE_SANITIZER_IFUNC_DETECTED
#endif
#ifdef _CORRADE_SANITIZER_IFUNC_DETECTED
#error Corrade was built with CORRADE_CPU_USE_IFUNC, which is incompatible with sanitizers. Rebuild without this option or disable sanitizers.
#endif
#endif
#endif // kate: hl c++
#ifndef CORRADE_UTILITY_EXPORT
#define CORRADE_UTILITY_EXPORT
#endif
#define _CORRADE_HELPER_PASTE2(a, b) a ## b
#ifdef CORRADE_TARGET_GCC
#define CORRADE_ALWAYS_INLINE __attribute__((always_inline)) inline
#elif defined(CORRADE_TARGET_MSVC)
#define CORRADE_ALWAYS_INLINE __forceinline
#else
#define CORRADE_ALWAYS_INLINE inline
#endif
#ifdef CORRADE_TARGET_GCC
#define CORRADE_NEVER_INLINE __attribute__((noinline))
#elif defined(CORRADE_TARGET_MSVC)
#define CORRADE_NEVER_INLINE __declspec(noinline)
#else
#define CORRADE_NEVER_INLINE
#endif
#ifndef Corrade_Cpu_h
#define Corrade_Cpu_h
#if defined(CORRADE_TARGET_MSVC) && !defined(CORRADE_TARGET_CLANG_CL) && defined(CORRADE_TARGET_X86)
extern "C" {
void __cpuidex(int[4], int, int);
unsigned __int64 __cdecl _xgetbv(unsigned int);
}
#endif
namespace Corrade {
namespace Cpu {
template<class> struct TypeTraits;
namespace Implementation {
struct InitT {};
constexpr InitT Init{};
enum: unsigned int { ExtraTagBitOffset = 16 };
}
struct ScalarT {
constexpr explicit ScalarT(Implementation::InitT) {}
};
template<> struct TypeTraits<ScalarT> {
enum: unsigned int { Index = 0 };
static const char* name() { return "Scalar"; }
};
#ifdef CORRADE_TARGET_X86
struct Sse2T: ScalarT {
constexpr explicit Sse2T(Implementation::InitT): ScalarT{Implementation::Init} {}
};
struct Sse3T: Sse2T {
constexpr explicit Sse3T(Implementation::InitT): Sse2T{Implementation::Init} {}
};
struct Ssse3T: Sse3T {
constexpr explicit Ssse3T(Implementation::InitT): Sse3T{Implementation::Init} {}
};
struct Sse41T: Ssse3T {
constexpr explicit Sse41T(Implementation::InitT): Ssse3T{Implementation::Init} {}
};
struct Sse42T: Sse41T {
constexpr explicit Sse42T(Implementation::InitT): Sse41T{Implementation::Init} {}
};
struct PopcntT {
constexpr explicit PopcntT(Implementation::InitT) {}
};
struct LzcntT {
constexpr explicit LzcntT(Implementation::InitT) {}
};
struct Bmi1T {
constexpr explicit Bmi1T(Implementation::InitT) {}
};
struct Bmi2T {
constexpr explicit Bmi2T(Implementation::InitT) {}
};
struct AvxT: Sse42T {
constexpr explicit AvxT(Implementation::InitT): Sse42T{Implementation::Init} {}
};
struct AvxF16cT {
constexpr explicit AvxF16cT(Implementation::InitT) {}
};
struct AvxFmaT {
constexpr explicit AvxFmaT(Implementation::InitT) {}
};
struct Avx2T: AvxT {
constexpr explicit Avx2T(Implementation::InitT): AvxT{Implementation::Init} {}
};
struct Avx512fT: Avx2T {
constexpr explicit Avx512fT(Implementation::InitT): Avx2T{Implementation::Init} {}
};
template<> struct TypeTraits<Sse2T> {
enum: unsigned int { Index = 1 << 0 };
static const char* name() { return "Sse2"; }
};
template<> struct TypeTraits<Sse3T> {
enum: unsigned int { Index = 1 << 1 };
static const char* name() { return "Sse3"; }
};
template<> struct TypeTraits<Ssse3T> {
enum: unsigned int { Index = 1 << 2 };
static const char* name() { return "Ssse3"; }
};
template<> struct TypeTraits<Sse41T> {
enum: unsigned int { Index = 1 << 3 };
static const char* name() { return "Sse41"; }
};
template<> struct TypeTraits<Sse42T> {
enum: unsigned int { Index = 1 << 4 };
static const char* name() { return "Sse42"; }
};
template<> struct TypeTraits<AvxT> {
enum: unsigned int { Index = 1 << 5 };
static const char* name() { return "Avx"; }
};
template<> struct TypeTraits<Avx2T> {
enum: unsigned int { Index = 1 << 6 };
static const char* name() { return "Avx2"; }
};
template<> struct TypeTraits<Avx512fT> {
enum: unsigned int { Index = 1 << 7 };
static const char* name() { return "Avx512f"; }
};
template<> struct TypeTraits<PopcntT> {
enum: unsigned int { Index = 1 << (0 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "Popcnt"; }
};
template<> struct TypeTraits<LzcntT> {
enum: unsigned int { Index = 1 << (1 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "Lzcnt"; }
};
template<> struct TypeTraits<Bmi1T> {
enum: unsigned int { Index = 1 << (2 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "Bmi1"; }
};
template<> struct TypeTraits<Bmi2T> {
enum: unsigned int { Index = 1 << (3 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "Bmi2"; }
};
template<> struct TypeTraits<AvxF16cT> {
enum: unsigned int { Index = 1 << (4 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "AvxF16c"; }
};
template<> struct TypeTraits<AvxFmaT> {
enum: unsigned int { Index = 1 << (5 + Implementation::ExtraTagBitOffset) };
static const char* name() { return "AvxFma"; }
};
#endif
#ifdef CORRADE_TARGET_ARM
struct NeonT: ScalarT {
constexpr explicit NeonT(Implementation::InitT): ScalarT{Implementation::Init} {}
};
struct NeonFmaT: NeonT {
constexpr explicit NeonFmaT(Implementation::InitT): NeonT{Implementation::Init} {}
};
struct NeonFp16T: NeonFmaT {
constexpr explicit NeonFp16T(Implementation::InitT): NeonFmaT{Implementation::Init} {}
};
template<> struct TypeTraits<NeonT> {
enum: unsigned int { Index = 1 << 0 };
static const char* name() { return "Neon"; }
};
template<> struct TypeTraits<NeonFmaT> {
enum: unsigned int { Index = 1 << 1 };
static const char* name() { return "NeonFma"; }
};
template<> struct TypeTraits<NeonFp16T> {
enum: unsigned int { Index = 1 << 2 };
static const char* name() { return "NeonFp16"; }
};
#endif
#ifdef CORRADE_TARGET_WASM
struct Simd128T: ScalarT {
constexpr explicit Simd128T(Implementation::InitT): ScalarT{Implementation::Init} {}
};
template<> struct TypeTraits<Simd128T> {
enum: unsigned int { Index = 1 << 0 };
static const char* name() { return "Simd128"; }
};
#endif
constexpr ScalarT Scalar{Implementation::Init};
#ifdef CORRADE_TARGET_X86
constexpr Sse2T Sse2{Implementation::Init};
constexpr Sse3T Sse3{Implementation::Init};
constexpr Ssse3T Ssse3{Implementation::Init};
constexpr Sse41T Sse41{Implementation::Init};
constexpr Sse42T Sse42{Implementation::Init};
constexpr PopcntT Popcnt{Implementation::Init};
constexpr LzcntT Lzcnt{Implementation::Init};
constexpr Bmi1T Bmi1{Implementation::Init};
constexpr Bmi2T Bmi2{Implementation::Init};
constexpr AvxT Avx{Implementation::Init};
constexpr AvxF16cT AvxF16c{Implementation::Init};
constexpr AvxFmaT AvxFma{Implementation::Init};
constexpr Avx2T Avx2{Implementation::Init};
constexpr Avx512fT Avx512f{Implementation::Init};
#endif
#ifdef CORRADE_TARGET_ARM
constexpr NeonT Neon{Implementation::Init};
constexpr NeonFmaT NeonFma{Implementation::Init};
constexpr NeonFp16T NeonFp16{Implementation::Init};
#endif
#ifdef CORRADE_TARGET_WASM
constexpr Simd128T Simd128{Implementation::Init};
#endif
namespace Implementation {
template<unsigned i> struct Priority: Priority<i - 1> {};
template<> struct Priority<0> {};
enum: unsigned int {
BaseTagMask = (1 << ExtraTagBitOffset) - 1,
ExtraTagMask = 0xffffffffu & ~BaseTagMask,
#ifdef CORRADE_TARGET_X86
ExtraTagCount = 6,
#else
ExtraTagCount = 0,
#endif
};
template<unsigned int value, unsigned int otherValue> struct IsTagConversionAllowed {
enum: bool { Value =
!((value & BaseTagMask) & ((value & BaseTagMask) - 1)) &&
!((otherValue & BaseTagMask) & ((otherValue & BaseTagMask) - 1)) &&
(otherValue & BaseTagMask) >= (value & BaseTagMask) &&
((otherValue & value) & ExtraTagMask) == (value & ExtraTagMask)
};
};
template<unsigned int value, unsigned int otherIndex> struct IsSingleTagConversionAllowed {
enum: bool { Value =
!((value & BaseTagMask) & ((value & BaseTagMask) - 1)) &&
(otherIndex & BaseTagMask) >= (value & BaseTagMask) &&
((otherIndex & value) & ExtraTagMask) == (value & ExtraTagMask)
};
};
template<unsigned int value> struct Tags {
enum: unsigned int { Value = value };
constexpr explicit Tags(InitT) {}
template<unsigned int otherValue> constexpr Tags(Tags<otherValue>, typename std::enable_if<IsTagConversionAllowed<value, otherValue>::Value>::type* = {}) {}
template<class T> constexpr Tags(T, typename std::enable_if<IsSingleTagConversionAllowed<Value, TypeTraits<T>::Index>::Value>::type* = {}) {}
template<unsigned int otherValue> constexpr Tags<value | otherValue> operator|(Tags<otherValue>) const {
return Tags<value | otherValue>{Init};
}
template<class U> constexpr Tags<value | TypeTraits<U>::Index> operator|(U) const {
return Tags<value | TypeTraits<U>::Index>{Init};
}
template<unsigned int otherValue> constexpr Tags<value & otherValue> operator&(Tags<otherValue>) const {
return Tags<value & otherValue>{Init};
}
template<class U> constexpr Tags<value & TypeTraits<U>::Index> operator&(U) const {
return Tags<value & TypeTraits<U>::Index>{Init};
}
template<unsigned int otherValue> constexpr Tags<value ^ otherValue> operator^(Tags<otherValue>) const {
return Tags<value ^ otherValue>{Init};
}
template<class U> constexpr Tags<value ^ TypeTraits<U>::Index> operator^(U) const {
return Tags<value ^ TypeTraits<U>::Index>{Init};
}
constexpr Tags<~value> operator~() const {
return Tags<~value>{Init};
}
constexpr explicit operator bool() const {
return bool(value);
}
constexpr operator unsigned int() const { return value; }
};
template<class T> constexpr Tags<TypeTraits<T>::Index> tags(T) {
return Tags<TypeTraits<T>::Index>{Init};
}
template<unsigned int value> constexpr Tags<value> tags(Tags<value> tags) {
return tags;
}
template<unsigned short A> struct BitIndex {
enum: unsigned short { Value = 1 + BitIndex<(A >> 1)>::Value };
};
template<> struct BitIndex<0> {
enum: unsigned short { Value = 0 };
};
template<unsigned short A> struct BitCount {
enum: unsigned short {
Bits1 = 0x5555,
Bits2 = 0x3333,
Bits4 = 0x0f0f,
Bits8 = 0x00ff,
B0 = (A >> 0) & Bits1,
B1 = (A >> 1) & Bits1,
C = B0 + B1,
D0 = (C >> 0) & Bits2,
D2 = (C >> 2) & Bits2,
E = D0 + D2,
F0 = (E >> 0) & Bits4,
F4 = (E >> 4) & Bits4,
G = F0 + F4,
H0 = (G >> 0) & Bits8,
H8 = (G >> 8) & Bits8,
Value = H0 + H8
};
};
template<class T> Priority<(static_cast<unsigned int>(TypeTraits<T>::Index) & ExtraTagMask ? 1 : BitIndex<static_cast<unsigned int>(TypeTraits<T>::Index) & BaseTagMask>::Value*(ExtraTagCount + 1))> constexpr priority(T) {
return {};
}
template<unsigned int value> Priority<(BitIndex<value & BaseTagMask>::Value*(ExtraTagCount + 1) + BitCount<((value & ExtraTagMask) >> ExtraTagBitOffset)>::Value)> constexpr priority(Tags<value>) {
static_assert(!((value & BaseTagMask) & ((value & BaseTagMask) - 1)), "more than one base tag used");
static_assert(((value & ExtraTagMask) >> ExtraTagBitOffset) < (1 << static_cast<unsigned int>(ExtraTagCount)), "extra tag out of expected bounds");
return {};
}
}
typedef
#ifdef CORRADE_TARGET_X86
#ifdef CORRADE_TARGET_AVX512F
Avx512fT
#elif defined(CORRADE_TARGET_AVX2)
Avx2T
#elif defined(CORRADE_TARGET_AVX)
AvxT
#elif defined(CORRADE_TARGET_SSE42)
Sse42T
#elif defined(CORRADE_TARGET_SSE41)
Sse41T
#elif defined(CORRADE_TARGET_SSSE3)
Ssse3T
#elif defined(CORRADE_TARGET_SSE3)
Sse3T
#elif defined(CORRADE_TARGET_SSE2)
Sse2T
#else
ScalarT
#endif
#elif defined(CORRADE_TARGET_ARM)
#ifdef CORRADE_TARGET_NEON_FP16
NeonFp16T
#elif defined(CORRADE_TARGET_NEON_FMA)
NeonFmaT
#elif defined(CORRADE_TARGET_NEON)
NeonT
#else
ScalarT
#endif
#elif defined(CORRADE_TARGET_WASM)
#ifdef CORRADE_TARGET_SIMD128
Simd128T
#else
ScalarT
#endif
#else
ScalarT
#endif
DefaultBaseT;
typedef Implementation::Tags<0
#ifdef CORRADE_TARGET_X86
#ifdef CORRADE_TARGET_POPCNT
|TypeTraits<PopcntT>::Index
#endif
#ifdef CORRADE_TARGET_LZCNT
|TypeTraits<LzcntT>::Index
#endif
#ifdef CORRADE_TARGET_BMI1
|TypeTraits<Bmi1T>::Index
#endif
#ifdef CORRADE_TARGET_BMI2
|TypeTraits<Bmi2T>::Index
#endif
#ifdef CORRADE_TARGET_AVX_FMA
|TypeTraits<AvxFmaT>::Index
#endif
#ifdef CORRADE_TARGET_AVX_F16C
|TypeTraits<AvxF16cT>::Index
#endif
#endif
> DefaultExtraT;
typedef Implementation::Tags<static_cast<unsigned int>(TypeTraits<DefaultBaseT>::Index)|DefaultExtraT::Value> DefaultT;
constexpr DefaultBaseT DefaultBase{Implementation::Init};
constexpr DefaultExtraT DefaultExtra{Implementation::Init};
constexpr DefaultT Default{Implementation::Init};
template<class T> constexpr T tag() { return T{Implementation::Init}; }
#if defined(CORRADE_TARGET_ARM) && ((defined(__linux__) && !(defined(CORRADE_TARGET_ANDROID) && __ANDROID_API__ < 18)) || defined(__FreeBSD__))
namespace Implementation {
Features runtimeFeatures(unsigned long caps);
}
#endif
class Features {
public:
constexpr explicit Features() noexcept: _data{} {}
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr /*implicit*/ Features(T) noexcept: _data{TypeTraits<T>::Index} {
static_assert(((static_cast<unsigned int>(TypeTraits<T>::Index) & Implementation::ExtraTagMask) >> Implementation::ExtraTagBitOffset) < (1 << static_cast<unsigned int>(Implementation::ExtraTagCount)),
"extra tag out of expected bounds");
}
template<unsigned int value> constexpr /*implicit*/ Features(Implementation::Tags<value>) noexcept: _data{value} {}
constexpr bool operator==(Features other) const {
return _data == other._data;
}
constexpr bool operator!=(Features other) const {
return _data != other._data;
}
constexpr bool operator>=(Features other) const {
return (_data & other._data) == other._data;
}
constexpr bool operator<=(Features other) const {
return (_data & other._data) == _data;
}
constexpr Features operator|(Features other) const {
return Features{_data | other._data};
}
Features& operator|=(Features other) {
_data |= other._data;
return *this;
}
constexpr Features operator&(Features other) const {
return Features{_data & other._data};
}
Features& operator&=(Features other) {
_data &= other._data;
return *this;
}
constexpr Features operator^(Features other) const {
return Features{_data ^ other._data};
}
Features& operator^=(Features other) {
_data ^= other._data;
return *this;
}
constexpr Features operator~() const {
return Features{~_data};
}
constexpr explicit operator bool() const { return _data; }
constexpr explicit operator unsigned int() const { return _data; }
private:
template<class> friend constexpr Features features();
friend constexpr Features compiledFeatures();
#if (defined(CORRADE_TARGET_X86) && (defined(CORRADE_TARGET_MSVC) || defined(CORRADE_TARGET_GCC))) || (defined(CORRADE_TARGET_ARM) && defined(CORRADE_TARGET_APPLE))
friend
#ifdef CORRADE_TARGET_ARM
CORRADE_UTILITY_EXPORT
#endif
Features runtimeFeatures();
#endif
#if defined(CORRADE_TARGET_ARM) && ((defined(__linux__) && !(defined(CORRADE_TARGET_ANDROID) && __ANDROID_API__ < 18)) || defined(__FreeBSD__))
friend Features Implementation::runtimeFeatures(unsigned long);
#endif
constexpr explicit Features(unsigned int data) noexcept: _data{data} {}
unsigned int _data;
};
template<class T> constexpr Features features() {
return Features{TypeTraits<T>::Index};
}
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr bool operator==(T a, Features b) {
return Features(a) == b;
}
template<class T, class U, class = decltype(TypeTraits<T>::Index), class = decltype(TypeTraits<U>::Index)> constexpr bool operator==(T, U) {
return static_cast<unsigned int>(TypeTraits<T>::Index) == TypeTraits<U>::Index;
}
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr bool operator!=(T a, Features b) {
return Features(a) != b;
}
template<class T, class U, class = decltype(TypeTraits<T>::Index), class = decltype(TypeTraits<U>::Index)> constexpr bool operator!=(T, U) {
return static_cast<unsigned int>(TypeTraits<T>::Index) != TypeTraits<U>::Index;
}
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr bool operator>=(T a, Features b) {
return Features(a) >= b;
}
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr bool operator<=(T a, Features b) {
return Features(a) <= b;
}
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr Features operator|(T a, Features b) {
return b | a;
}
template<class T, class U> constexpr Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) | TypeTraits<U>::Index> operator|(T, U) {
return Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) | TypeTraits<U>::Index>{Implementation::Init};
}
template<class T, unsigned int value> constexpr Implementation::Tags<TypeTraits<T>::Index | value> operator|(T, Implementation::Tags<value>) {
return Implementation::Tags<TypeTraits<T>::Index | value>{Implementation::Init};
}
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr Features operator&(T a, Features b) {
return b & a;
}
template<class T, class U> constexpr Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) & TypeTraits<U>::Index> operator&(T, U) {
return Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) & TypeTraits<U>::Index>{Implementation::Init};
}
template<class T, unsigned int value> constexpr Implementation::Tags<TypeTraits<T>::Index & value> operator&(T, Implementation::Tags<value>) {
return Implementation::Tags<TypeTraits<T>::Index & value>{Implementation::Init};
}
template<class T, class = decltype(TypeTraits<T>::Index)> constexpr Features operator^(T a, Features b) {
return b ^ a;
}
template<class T, class U> constexpr Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) ^ TypeTraits<U>::Index> operator^(T, U) {
return Implementation::Tags<static_cast<unsigned int>(TypeTraits<T>::Index) ^ TypeTraits<U>::Index>{Implementation::Init};
}
template<class T, unsigned int value> constexpr Implementation::Tags<TypeTraits<T>::Index ^ value> operator^(T, Implementation::Tags<value>) {
return Implementation::Tags<TypeTraits<T>::Index ^ value>{Implementation::Init};
}
template<class T> constexpr Implementation::Tags<~TypeTraits<T>::Index> operator~(T) {
return Implementation::Tags<~TypeTraits<T>::Index>{Implementation::Init};
}
constexpr Features compiledFeatures() {
return Features{0
#ifdef CORRADE_TARGET_X86
#ifdef CORRADE_TARGET_SSE2
|TypeTraits<Sse2T>::Index
#endif
#ifdef CORRADE_TARGET_SSE3
|TypeTraits<Sse3T>::Index
#endif
#ifdef CORRADE_TARGET_SSSE3
|TypeTraits<Ssse3T>::Index
#endif
#ifdef CORRADE_TARGET_SSE41
|TypeTraits<Sse41T>::Index
#endif
#ifdef CORRADE_TARGET_SSE42
|TypeTraits<Sse42T>::Index
#endif
#ifdef CORRADE_TARGET_POPCNT
|TypeTraits<PopcntT>::Index
#endif
#ifdef CORRADE_TARGET_LZCNT
|TypeTraits<LzcntT>::Index
#endif
#ifdef CORRADE_TARGET_BMI1
|TypeTraits<Bmi1T>::Index
#endif
#ifdef CORRADE_TARGET_BMI2
|TypeTraits<Bmi2T>::Index
#endif
#ifdef CORRADE_TARGET_AVX
|TypeTraits<AvxT>::Index
#endif
#ifdef CORRADE_TARGET_AVX_FMA
|TypeTraits<AvxFmaT>::Index
#endif
#ifdef CORRADE_TARGET_AVX_F16C
|TypeTraits<AvxF16cT>::Index
#endif
#ifdef CORRADE_TARGET_AVX2
|TypeTraits<Avx2T>::Index
#endif
#ifdef CORRADE_TARGET_AVX512F
|TypeTraits<Avx512fT>::Index
#endif
#elif defined(CORRADE_TARGET_ARM)
#ifdef CORRADE_TARGET_NEON
|TypeTraits<NeonT>::Index
#endif
#ifdef CORRADE_TARGET_NEON_FMA
|TypeTraits<NeonFmaT>::Index
#endif
#ifdef CORRADE_TARGET_NEON_FP16
|TypeTraits<NeonFp16T>::Index
#endif
#elif defined(CORRADE_TARGET_WASM)
#ifdef CORRADE_TARGET_SIMD128
|TypeTraits<Simd128T>::Index
#endif
#endif
};
}
#if (defined(CORRADE_TARGET_X86) && (defined(CORRADE_TARGET_MSVC) || defined(CORRADE_TARGET_GCC))) || (defined(CORRADE_TARGET_ARM) && ((defined(__linux__) && !(defined(CORRADE_TARGET_ANDROID) && __ANDROID_API__ < 18)) || defined(CORRADE_TARGET_APPLE) || defined(__FreeBSD__)))
#ifdef CORRADE_TARGET_ARM
CORRADE_UTILITY_EXPORT
#endif
Features runtimeFeatures();
#else
constexpr Features runtimeFeatures() { return compiledFeatures(); }
#endif
#define CORRADE_CPU_DECLARE(tag) decltype(Corrade::Cpu::Implementation::tags(tag)), decltype(Corrade::Cpu::Implementation::priority(tag))
#define CORRADE_CPU_SELECT(tag) tag, Corrade::Cpu::Implementation::priority(tag)
#define _CORRADE_HELPER_PICK(_0, _1, _2, _3, _4, _5, _6, _7, macroName, ...) macroName
#ifdef CORRADE_TARGET_X86
#define CORRADE_CPU_DISPATCHER_BASE(function) \
decltype(function(Corrade::Cpu::Scalar)) function(Corrade::Cpu::Features features) { \
if(features & Corrade::Cpu::Avx512f) \
return function(Corrade::Cpu::Avx512f); \
if(features & Corrade::Cpu::Avx2) \
return function(Corrade::Cpu::Avx2); \
if(features & Corrade::Cpu::Avx) \
return function(Corrade::Cpu::Avx); \
if(features & Corrade::Cpu::Sse42) \
return function(Corrade::Cpu::Sse42); \
if(features & Corrade::Cpu::Sse41) \
return function(Corrade::Cpu::Sse41); \
if(features & Corrade::Cpu::Ssse3) \
return function(Corrade::Cpu::Ssse3); \
if(features & Corrade::Cpu::Sse3) \
return function(Corrade::Cpu::Sse3); \
if(features & Corrade::Cpu::Sse2) \