-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathbase.h
More file actions
2076 lines (1822 loc) · 64 KB
/
Copy pathbase.h
File metadata and controls
2076 lines (1822 loc) · 64 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
// Copyright 2020 Google LLC
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef HIGHWAY_HWY_BASE_H_
#define HIGHWAY_HWY_BASE_H_
// For SIMD module implementations and their callers, target-independent.
// IWYU pragma: begin_exports
#include <stddef.h>
#include <stdint.h>
#include "hwy/detect_compiler_arch.h"
#include "hwy/highway_export.h"
#if HWY_COMPILER_MSVC && defined(_MSVC_LANG) && _MSVC_LANG > __cplusplus
#define HWY_CXX_LANG _MSVC_LANG
#else
#define HWY_CXX_LANG __cplusplus
#endif
// Wrapping this into a HWY_HAS_INCLUDE causes clang-format to fail.
#if HWY_CXX_LANG >= 202100L && defined(__has_include)
#if __has_include(<stdfloat>)
#include <stdfloat> // std::float16_t
#endif
#endif
// "IWYU pragma: keep" does not work for these includes, so hide from the IDE.
#if !HWY_IDE
#if !defined(HWY_NO_LIBCXX)
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS // before inttypes.h
#endif
#include <inttypes.h>
#endif
#if (HWY_ARCH_X86 && !defined(HWY_NO_LIBCXX)) || HWY_COMPILER_MSVC
#include <atomic>
#endif
#endif // !HWY_IDE
#if !defined(HWY_NO_LIBCXX) && HWY_CXX_LANG > 201703L && \
__cpp_impl_three_way_comparison >= 201907L && defined(__has_include) && \
!defined(HWY_DISABLE_CXX20_THREE_WAY_COMPARE)
#if __has_include(<compare>)
#include <compare>
#define HWY_HAVE_CXX20_THREE_WAY_COMPARE 1
#endif
#endif
// IWYU pragma: end_exports
#if HWY_COMPILER_MSVC
#include <string.h> // memcpy
#endif
//------------------------------------------------------------------------------
// Compiler-specific definitions
#define HWY_STR_IMPL(macro) #macro
#define HWY_STR(macro) HWY_STR_IMPL(macro)
#if HWY_COMPILER_MSVC
#include <intrin.h>
#define HWY_RESTRICT __restrict
#define HWY_INLINE __forceinline
#define HWY_NOINLINE __declspec(noinline)
#define HWY_FLATTEN
#define HWY_NORETURN __declspec(noreturn)
#define HWY_LIKELY(expr) (expr)
#define HWY_UNLIKELY(expr) (expr)
#define HWY_PRAGMA(tokens) __pragma(tokens)
#define HWY_DIAGNOSTICS(tokens) HWY_PRAGMA(warning(tokens))
#define HWY_DIAGNOSTICS_OFF(msc, gcc) HWY_DIAGNOSTICS(msc)
#define HWY_MAYBE_UNUSED
#define HWY_HAS_ASSUME_ALIGNED 0
#if (_MSC_VER >= 1700)
#define HWY_MUST_USE_RESULT _Check_return_
#else
#define HWY_MUST_USE_RESULT
#endif
#else
#define HWY_RESTRICT __restrict__
// force inlining without optimization enabled creates very inefficient code
// that can cause compiler timeout
#ifdef __OPTIMIZE__
#define HWY_INLINE inline __attribute__((always_inline))
#else
#define HWY_INLINE inline
#endif
#define HWY_NOINLINE __attribute__((noinline))
#define HWY_FLATTEN __attribute__((flatten))
#define HWY_NORETURN __attribute__((noreturn))
#define HWY_LIKELY(expr) __builtin_expect(!!(expr), 1)
#define HWY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
#define HWY_PRAGMA(tokens) _Pragma(#tokens)
#define HWY_DIAGNOSTICS(tokens) HWY_PRAGMA(GCC diagnostic tokens)
#define HWY_DIAGNOSTICS_OFF(msc, gcc) HWY_DIAGNOSTICS(gcc)
// Encountered "attribute list cannot appear here" when using the C++17
// [[maybe_unused]], so only use the old style attribute for now.
#define HWY_MAYBE_UNUSED __attribute__((unused))
#define HWY_MUST_USE_RESULT __attribute__((warn_unused_result))
#endif // !HWY_COMPILER_MSVC
//------------------------------------------------------------------------------
// Builtin/attributes (no more #include after this point due to namespace!)
namespace hwy {
// Enables error-checking of format strings.
#if HWY_HAS_ATTRIBUTE(__format__)
#define HWY_FORMAT(idx_fmt, idx_arg) \
__attribute__((__format__(__printf__, idx_fmt, idx_arg)))
#else
#define HWY_FORMAT(idx_fmt, idx_arg)
#endif
// Returns a void* pointer which the compiler then assumes is N-byte aligned.
// Example: float* HWY_RESTRICT aligned = (float*)HWY_ASSUME_ALIGNED(in, 32);
//
// The assignment semantics are required by GCC/Clang. ICC provides an in-place
// __assume_aligned, whereas MSVC's __assume appears unsuitable.
#if HWY_HAS_BUILTIN(__builtin_assume_aligned)
#define HWY_ASSUME_ALIGNED(ptr, align) __builtin_assume_aligned((ptr), (align))
#else
#define HWY_ASSUME_ALIGNED(ptr, align) (ptr) /* not supported */
#endif
// Special case to increases required alignment
#define HWY_RCAST_ALIGNED(type, ptr) \
reinterpret_cast<type>(HWY_ASSUME_ALIGNED((ptr), alignof(type)))
// Clang and GCC require attributes on each function into which SIMD intrinsics
// are inlined. Support both per-function annotation (HWY_ATTR) for lambdas and
// automatic annotation via pragmas.
#if HWY_COMPILER_ICC
// As of ICC 2021.{1-9} the pragma is neither implemented nor required.
#define HWY_PUSH_ATTRIBUTES(targets_str)
#define HWY_POP_ATTRIBUTES
#elif HWY_COMPILER_CLANG
#define HWY_PUSH_ATTRIBUTES(targets_str) \
HWY_PRAGMA(clang attribute push(__attribute__((target(targets_str))), \
apply_to = function))
#define HWY_POP_ATTRIBUTES HWY_PRAGMA(clang attribute pop)
#elif HWY_COMPILER_GCC_ACTUAL
#define HWY_PUSH_ATTRIBUTES(targets_str) \
HWY_PRAGMA(GCC push_options) HWY_PRAGMA(GCC target targets_str)
#define HWY_POP_ATTRIBUTES HWY_PRAGMA(GCC pop_options)
#else
#define HWY_PUSH_ATTRIBUTES(targets_str)
#define HWY_POP_ATTRIBUTES
#endif
//------------------------------------------------------------------------------
// Macros
#define HWY_API static HWY_INLINE HWY_FLATTEN HWY_MAYBE_UNUSED
#define HWY_CONCAT_IMPL(a, b) a##b
#define HWY_CONCAT(a, b) HWY_CONCAT_IMPL(a, b)
#define HWY_MIN(a, b) ((a) < (b) ? (a) : (b))
#define HWY_MAX(a, b) ((a) > (b) ? (a) : (b))
#if HWY_COMPILER_GCC_ACTUAL
// nielskm: GCC does not support '#pragma GCC unroll' without the factor.
#define HWY_UNROLL(factor) HWY_PRAGMA(GCC unroll factor)
#define HWY_DEFAULT_UNROLL HWY_UNROLL(4)
#elif HWY_COMPILER_CLANG || HWY_COMPILER_ICC || HWY_COMPILER_ICX
#define HWY_UNROLL(factor) HWY_PRAGMA(unroll factor)
#define HWY_DEFAULT_UNROLL HWY_UNROLL()
#else
#define HWY_UNROLL(factor)
#define HWY_DEFAULT_UNROLL
#endif
// Tell a compiler that the expression always evaluates to true.
// The expression should be free from any side effects.
// Some older compilers may have trouble with complex expressions, therefore
// it is advisable to split multiple conditions into separate assume statements,
// and manually check the generated code.
// OK but could fail:
// HWY_ASSUME(x == 2 && y == 3);
// Better:
// HWY_ASSUME(x == 2);
// HWY_ASSUME(y == 3);
#if HWY_HAS_CPP_ATTRIBUTE(assume)
#define HWY_ASSUME(expr) [[assume(expr)]]
#elif HWY_COMPILER_MSVC || HWY_COMPILER_ICC
#define HWY_ASSUME(expr) __assume(expr)
// __builtin_assume() was added in clang 3.6.
#elif HWY_COMPILER_CLANG && HWY_HAS_BUILTIN(__builtin_assume)
#define HWY_ASSUME(expr) __builtin_assume(expr)
// __builtin_unreachable() was added in GCC 4.5, but __has_builtin() was added
// later, so check for the compiler version directly.
#elif HWY_COMPILER_GCC_ACTUAL >= 405
#define HWY_ASSUME(expr) \
((expr) ? static_cast<void>(0) : __builtin_unreachable())
#else
#define HWY_ASSUME(expr) static_cast<void>(0)
#endif
// Compile-time fence to prevent undesirable code reordering. On Clang x86, the
// typical asm volatile("" : : : "memory") has no effect, whereas atomic fence
// does, without generating code.
#if HWY_ARCH_X86 && !defined(HWY_NO_LIBCXX)
#define HWY_FENCE std::atomic_thread_fence(std::memory_order_acq_rel)
#else
// TODO(janwas): investigate alternatives. On Arm, the above generates barriers.
#define HWY_FENCE
#endif
// 4 instances of a given literal value, useful as input to LoadDup128.
#define HWY_REP4(literal) literal, literal, literal, literal
HWY_DLLEXPORT HWY_NORETURN void HWY_FORMAT(3, 4)
Abort(const char* file, int line, const char* format, ...);
#define HWY_ABORT(format, ...) \
::hwy::Abort(__FILE__, __LINE__, format, ##__VA_ARGS__)
// Always enabled.
#define HWY_ASSERT(condition) \
do { \
if (!(condition)) { \
HWY_ABORT("Assert %s", #condition); \
} \
} while (0)
#if HWY_HAS_FEATURE(memory_sanitizer) || defined(MEMORY_SANITIZER)
#define HWY_IS_MSAN 1
#else
#define HWY_IS_MSAN 0
#endif
#if HWY_HAS_FEATURE(address_sanitizer) || defined(ADDRESS_SANITIZER)
#define HWY_IS_ASAN 1
#else
#define HWY_IS_ASAN 0
#endif
#if HWY_HAS_FEATURE(thread_sanitizer) || defined(THREAD_SANITIZER)
#define HWY_IS_TSAN 1
#else
#define HWY_IS_TSAN 0
#endif
// MSAN may cause lengthy build times or false positives e.g. in AVX3 DemoteTo.
// You can disable MSAN by adding this attribute to the function that fails.
#if HWY_IS_MSAN
#define HWY_ATTR_NO_MSAN __attribute__((no_sanitize_memory))
#else
#define HWY_ATTR_NO_MSAN
#endif
// For enabling HWY_DASSERT and shortening tests in slower debug builds
#if !defined(HWY_IS_DEBUG_BUILD)
// Clang does not define NDEBUG, but it and GCC define __OPTIMIZE__, and recent
// MSVC defines NDEBUG (if not, could instead check _DEBUG).
#if (!defined(__OPTIMIZE__) && !defined(NDEBUG)) || HWY_IS_ASAN || \
HWY_IS_MSAN || HWY_IS_TSAN || defined(__clang_analyzer__)
#define HWY_IS_DEBUG_BUILD 1
#else
#define HWY_IS_DEBUG_BUILD 0
#endif
#endif // HWY_IS_DEBUG_BUILD
#if HWY_IS_DEBUG_BUILD
#define HWY_DASSERT(condition) HWY_ASSERT(condition)
#else
#define HWY_DASSERT(condition) \
do { \
} while (0)
#endif
#define HWY_DASSERT_ALIGNED(d, addr) \
HWY_DASSERT(reinterpret_cast<uintptr_t>(addr) % \
(Lanes(d) * sizeof(TFromD<decltype(d)>)) == \
0)
#if __cpp_constexpr >= 201304L
#define HWY_CXX14_CONSTEXPR constexpr
#else
#define HWY_CXX14_CONSTEXPR
#endif
#ifndef HWY_HAVE_CXX20_THREE_WAY_COMPARE
#define HWY_HAVE_CXX20_THREE_WAY_COMPARE 0
#endif
//------------------------------------------------------------------------------
// CopyBytes / ZeroBytes
#if HWY_COMPILER_MSVC
#pragma intrinsic(memcpy)
#pragma intrinsic(memset)
#endif
// The source/destination must not overlap/alias.
template <size_t kBytes, typename From, typename To>
HWY_API void CopyBytes(const From* from, To* to) {
#if HWY_COMPILER_MSVC
memcpy(to, from, kBytes);
#else
__builtin_memcpy(to, from, kBytes);
#endif
}
HWY_API void CopyBytes(const void* HWY_RESTRICT from, void* HWY_RESTRICT to,
size_t num_of_bytes_to_copy) {
#if HWY_COMPILER_MSVC
memcpy(to, from, num_of_bytes_to_copy);
#else
__builtin_memcpy(to, from, num_of_bytes_to_copy);
#endif
}
// Same as CopyBytes, but for same-sized objects; avoids a size argument.
template <typename From, typename To>
HWY_API void CopySameSize(const From* HWY_RESTRICT from, To* HWY_RESTRICT to) {
static_assert(sizeof(From) == sizeof(To), "");
CopyBytes<sizeof(From)>(from, to);
}
template <size_t kBytes, typename To>
HWY_API void ZeroBytes(To* to) {
#if HWY_COMPILER_MSVC
memset(to, 0, kBytes);
#else
__builtin_memset(to, 0, kBytes);
#endif
}
HWY_API void ZeroBytes(void* to, size_t num_bytes) {
#if HWY_COMPILER_MSVC
memset(to, 0, num_bytes);
#else
__builtin_memset(to, 0, num_bytes);
#endif
}
// -----------------------------------------------------------------------------
// BitCastScalar
#if HWY_HAS_BUILTIN(__builtin_bit_cast) || HWY_COMPILER_MSVC >= 1926
#define HWY_BITCASTSCALAR_CONSTEXPR constexpr
#else
#define HWY_BITCASTSCALAR_CONSTEXPR
#endif
#if __cpp_constexpr >= 201304L
#define HWY_BITCASTSCALAR_CXX14_CONSTEXPR HWY_BITCASTSCALAR_CONSTEXPR
#else
#define HWY_BITCASTSCALAR_CXX14_CONSTEXPR
#endif
template <class To, class From>
HWY_API HWY_BITCASTSCALAR_CONSTEXPR To BitCastScalar(const From& val) {
#if HWY_HAS_BUILTIN(__builtin_bit_cast) || HWY_COMPILER_MSVC >= 1926
return __builtin_bit_cast(To, val);
#else
To result;
CopySameSize(&val, &result);
return result;
#endif
}
//------------------------------------------------------------------------------
// kMaxVectorSize (undocumented, pending removal)
#if HWY_ARCH_X86
static constexpr HWY_MAYBE_UNUSED size_t kMaxVectorSize = 64; // AVX-512
#elif HWY_ARCH_RVV && defined(__riscv_v_intrinsic) && \
__riscv_v_intrinsic >= 11000
// Not actually an upper bound on the size.
static constexpr HWY_MAYBE_UNUSED size_t kMaxVectorSize = 4096;
#else
static constexpr HWY_MAYBE_UNUSED size_t kMaxVectorSize = 16;
#endif
//------------------------------------------------------------------------------
// Alignment
// Potentially useful for LoadDup128 and capped vectors. In other cases, arrays
// should be allocated dynamically via aligned_allocator.h because Lanes() may
// exceed the stack size.
#if HWY_ARCH_X86
#define HWY_ALIGN_MAX alignas(64)
#elif HWY_ARCH_RVV && defined(__riscv_v_intrinsic) && \
__riscv_v_intrinsic >= 11000
#define HWY_ALIGN_MAX alignas(8) // only elements need be aligned
#else
#define HWY_ALIGN_MAX alignas(16)
#endif
//------------------------------------------------------------------------------
// Lane types
using float32_t = float;
using float64_t = double;
#pragma pack(push, 1)
// Aligned 128-bit type. Cannot use __int128 because clang doesn't yet align it:
// https://reviews.llvm.org/D86310
struct alignas(16) uint128_t {
uint64_t lo; // little-endian layout
uint64_t hi;
};
// 64 bit key plus 64 bit value. Faster than using uint128_t when only the key
// field is to be compared (Lt128Upper instead of Lt128).
struct alignas(16) K64V64 {
uint64_t value; // little-endian layout
uint64_t key;
};
// 32 bit key plus 32 bit value. Allows vqsort recursions to terminate earlier
// than when considering both to be a 64-bit key.
struct alignas(8) K32V32 {
uint32_t value; // little-endian layout
uint32_t key;
};
#pragma pack(pop)
static inline HWY_MAYBE_UNUSED bool operator<(const uint128_t& a,
const uint128_t& b) {
return (a.hi == b.hi) ? a.lo < b.lo : a.hi < b.hi;
}
// Required for std::greater.
static inline HWY_MAYBE_UNUSED bool operator>(const uint128_t& a,
const uint128_t& b) {
return b < a;
}
static inline HWY_MAYBE_UNUSED bool operator==(const uint128_t& a,
const uint128_t& b) {
return a.lo == b.lo && a.hi == b.hi;
}
static inline HWY_MAYBE_UNUSED bool operator<(const K64V64& a,
const K64V64& b) {
return a.key < b.key;
}
// Required for std::greater.
static inline HWY_MAYBE_UNUSED bool operator>(const K64V64& a,
const K64V64& b) {
return b < a;
}
static inline HWY_MAYBE_UNUSED bool operator==(const K64V64& a,
const K64V64& b) {
return a.key == b.key;
}
static inline HWY_MAYBE_UNUSED bool operator<(const K32V32& a,
const K32V32& b) {
return a.key < b.key;
}
// Required for std::greater.
static inline HWY_MAYBE_UNUSED bool operator>(const K32V32& a,
const K32V32& b) {
return b < a;
}
static inline HWY_MAYBE_UNUSED bool operator==(const K32V32& a,
const K32V32& b) {
return a.key == b.key;
}
//------------------------------------------------------------------------------
// Controlling overload resolution (SFINAE)
template <bool Condition>
struct EnableIfT {};
template <>
struct EnableIfT<true> {
using type = void;
};
template <bool Condition>
using EnableIf = typename EnableIfT<Condition>::type;
template <typename T, typename U>
struct IsSameT {
enum { value = 0 };
};
template <typename T>
struct IsSameT<T, T> {
enum { value = 1 };
};
template <typename T, typename U>
HWY_API constexpr bool IsSame() {
return IsSameT<T, U>::value;
}
template <bool Condition, typename Then, typename Else>
struct IfT {
using type = Then;
};
template <class Then, class Else>
struct IfT<false, Then, Else> {
using type = Else;
};
template <bool Condition, typename Then, typename Else>
using If = typename IfT<Condition, Then, Else>::type;
// Insert into template/function arguments to enable this overload only for
// vectors of exactly, at most (LE), or more than (GT) this many bytes.
//
// As an example, checking for a total size of 16 bytes will match both
// Simd<uint8_t, 16, 0> and Simd<uint8_t, 8, 1>.
#define HWY_IF_V_SIZE(T, kN, bytes) \
hwy::EnableIf<kN * sizeof(T) == bytes>* = nullptr
#define HWY_IF_V_SIZE_LE(T, kN, bytes) \
hwy::EnableIf<kN * sizeof(T) <= bytes>* = nullptr
#define HWY_IF_V_SIZE_GT(T, kN, bytes) \
hwy::EnableIf<(kN * sizeof(T) > bytes)>* = nullptr
#define HWY_IF_LANES(kN, lanes) hwy::EnableIf<(kN == lanes)>* = nullptr
#define HWY_IF_LANES_LE(kN, lanes) hwy::EnableIf<(kN <= lanes)>* = nullptr
#define HWY_IF_LANES_GT(kN, lanes) hwy::EnableIf<(kN > lanes)>* = nullptr
#define HWY_IF_UNSIGNED(T) hwy::EnableIf<!IsSigned<T>()>* = nullptr
#define HWY_IF_SIGNED(T) \
hwy::EnableIf<IsSigned<T>() && !IsFloat<T>() && !IsSpecialFloat<T>()>* = \
nullptr
#define HWY_IF_FLOAT(T) hwy::EnableIf<hwy::IsFloat<T>()>* = nullptr
#define HWY_IF_NOT_FLOAT(T) hwy::EnableIf<!hwy::IsFloat<T>()>* = nullptr
#define HWY_IF_FLOAT3264(T) hwy::EnableIf<hwy::IsFloat3264<T>()>* = nullptr
#define HWY_IF_NOT_FLOAT3264(T) hwy::EnableIf<!hwy::IsFloat3264<T>()>* = nullptr
#define HWY_IF_SPECIAL_FLOAT(T) \
hwy::EnableIf<hwy::IsSpecialFloat<T>()>* = nullptr
#define HWY_IF_NOT_SPECIAL_FLOAT(T) \
hwy::EnableIf<!hwy::IsSpecialFloat<T>()>* = nullptr
#define HWY_IF_FLOAT_OR_SPECIAL(T) \
hwy::EnableIf<hwy::IsFloat<T>() || hwy::IsSpecialFloat<T>()>* = nullptr
#define HWY_IF_NOT_FLOAT_NOR_SPECIAL(T) \
hwy::EnableIf<!hwy::IsFloat<T>() && !hwy::IsSpecialFloat<T>()>* = nullptr
#define HWY_IF_INTEGER(T) hwy::EnableIf<hwy::IsInteger<T>()>* = nullptr
#define HWY_IF_T_SIZE(T, bytes) hwy::EnableIf<sizeof(T) == (bytes)>* = nullptr
#define HWY_IF_NOT_T_SIZE(T, bytes) \
hwy::EnableIf<sizeof(T) != (bytes)>* = nullptr
// bit_array = 0x102 means 1 or 8 bytes. There is no NONE_OF because it sounds
// too similar. If you want the opposite of this (2 or 4 bytes), ask for those
// bits explicitly (0x14) instead of attempting to 'negate' 0x102.
#define HWY_IF_T_SIZE_ONE_OF(T, bit_array) \
hwy::EnableIf<((size_t{1} << sizeof(T)) & (bit_array)) != 0>* = nullptr
#define HWY_IF_T_SIZE_LE(T, bytes) \
hwy::EnableIf<(sizeof(T) <= (bytes))>* = nullptr
#define HWY_IF_T_SIZE_GT(T, bytes) \
hwy::EnableIf<(sizeof(T) > (bytes))>* = nullptr
#define HWY_IF_U8(T) hwy::EnableIf<IsSame<T, uint8_t>()>* = nullptr
#define HWY_IF_U16(T) hwy::EnableIf<IsSame<T, uint16_t>()>* = nullptr
#define HWY_IF_U32(T) hwy::EnableIf<IsSame<T, uint32_t>()>* = nullptr
#define HWY_IF_U64(T) hwy::EnableIf<IsSame<T, uint64_t>()>* = nullptr
#define HWY_IF_I8(T) hwy::EnableIf<IsSame<T, int8_t>()>* = nullptr
#define HWY_IF_I16(T) hwy::EnableIf<IsSame<T, int16_t>()>* = nullptr
#define HWY_IF_I32(T) hwy::EnableIf<IsSame<T, int32_t>()>* = nullptr
#define HWY_IF_I64(T) hwy::EnableIf<IsSame<T, int64_t>()>* = nullptr
#define HWY_IF_BF16(T) hwy::EnableIf<IsSame<T, hwy::bfloat16_t>()>* = nullptr
#define HWY_IF_NOT_BF16(T) \
hwy::EnableIf<!IsSame<T, hwy::bfloat16_t>()>* = nullptr
#define HWY_IF_F16(T) hwy::EnableIf<IsSame<T, hwy::float16_t>()>* = nullptr
#define HWY_IF_F32(T) hwy::EnableIf<IsSame<T, float>()>* = nullptr
#define HWY_IF_F64(T) hwy::EnableIf<IsSame<T, double>()>* = nullptr
// Use instead of HWY_IF_T_SIZE to avoid ambiguity with float16_t/float/double
// overloads.
#define HWY_IF_UI8(T) \
hwy::EnableIf<IsSame<T, uint8_t>() || IsSame<T, int8_t>()>* = nullptr
#define HWY_IF_UI16(T) \
hwy::EnableIf<IsSame<T, uint16_t>() || IsSame<T, int16_t>()>* = nullptr
#define HWY_IF_UI32(T) \
hwy::EnableIf<IsSame<T, uint32_t>() || IsSame<T, int32_t>()>* = nullptr
#define HWY_IF_UI64(T) \
hwy::EnableIf<IsSame<T, uint64_t>() || IsSame<T, int64_t>()>* = nullptr
#define HWY_IF_LANES_PER_BLOCK(T, N, LANES) \
hwy::EnableIf<HWY_MIN(sizeof(T) * N, 16) / sizeof(T) == (LANES)>* = nullptr
// Empty struct used as a size tag type.
template <size_t N>
struct SizeTag {};
template <class T>
struct RemoveConstT {
using type = T;
};
template <class T>
struct RemoveConstT<const T> {
using type = T;
};
template <class T>
using RemoveConst = typename RemoveConstT<T>::type;
template <class T>
struct RemoveVolatileT {
using type = T;
};
template <class T>
struct RemoveVolatileT<volatile T> {
using type = T;
};
template <class T>
using RemoveVolatile = typename RemoveVolatileT<T>::type;
template <class T>
struct RemoveRefT {
using type = T;
};
template <class T>
struct RemoveRefT<T&> {
using type = T;
};
template <class T>
struct RemoveRefT<T&&> {
using type = T;
};
template <class T>
using RemoveRef = typename RemoveRefT<T>::type;
template <class T>
using RemoveCvRef = RemoveConst<RemoveVolatile<RemoveRef<T>>>;
template <class T>
class DeclValT {
private:
template <class U, class URef = U&&>
static URef TryAddRValRef(int);
template <class U, class Arg>
static U TryAddRValRef(Arg);
public:
using type = decltype(TryAddRValRef<T>(0));
enum { kDisableDeclValEvaluation = 1 };
};
// hwy::DeclVal<T>() can only be used in unevaluated contexts such as within an
// expression of a decltype specifier.
// hwy::DeclVal<T>() does not require that T have a public default constructor
template <class T>
HWY_API typename DeclValT<T>::type DeclVal() noexcept {
static_assert(!DeclValT<T>::kDisableDeclValEvaluation,
"DeclVal() cannot be used in an evaluated context");
}
template <class T>
struct IsArrayT {
enum { value = 0 };
};
template <class T>
struct IsArrayT<T[]> {
enum { value = 1 };
};
template <class T, size_t N>
struct IsArrayT<T[N]> {
enum { value = 1 };
};
template <class T>
static constexpr bool IsArray() {
return IsArrayT<T>::value;
}
#if HWY_COMPILER_MSVC
HWY_DIAGNOSTICS(push)
HWY_DIAGNOSTICS_OFF(disable : 4180, ignored "-Wignored-qualifiers")
#endif
template <class From, class To>
class IsConvertibleT {
private:
template <class T>
static hwy::SizeTag<1> TestFuncWithToArg(T);
template <class T, class U>
static decltype(IsConvertibleT<T, U>::template TestFuncWithToArg<U>(
DeclVal<T>()))
TryConvTest(int);
template <class T, class U, class Arg>
static hwy::SizeTag<0> TryConvTest(Arg);
public:
enum {
value = (IsSame<RemoveConst<RemoveVolatile<From>>, void>() &&
IsSame<RemoveConst<RemoveVolatile<To>>, void>()) ||
(!IsArray<To>() &&
(IsSame<To, decltype(DeclVal<To>())>() ||
!IsSame<const RemoveConst<To>, RemoveConst<To>>()) &&
IsSame<decltype(TryConvTest<From, To>(0)), hwy::SizeTag<1>>())
};
};
#if HWY_COMPILER_MSVC
HWY_DIAGNOSTICS(pop)
#endif
template <class From, class To>
HWY_API constexpr bool IsConvertible() {
return IsConvertibleT<From, To>::value;
}
template <class From, class To>
class IsStaticCastableT {
private:
template <class T, class U, class = decltype(static_cast<U>(DeclVal<T>()))>
static hwy::SizeTag<1> TryStaticCastTest(int);
template <class T, class U, class Arg>
static hwy::SizeTag<0> TryStaticCastTest(Arg);
public:
enum {
value = IsSame<decltype(TryStaticCastTest<From, To>(0)), hwy::SizeTag<1>>()
};
};
template <class From, class To>
static constexpr bool IsStaticCastable() {
return IsStaticCastableT<From, To>::value;
}
template <class T, class From>
class IsAssignableT {
private:
template <class T1, class T2, class = decltype(DeclVal<T1>() = DeclVal<T2>())>
static hwy::SizeTag<1> TryAssignTest(int);
template <class T1, class T2, class Arg>
static hwy::SizeTag<0> TryAssignTest(Arg);
public:
enum {
value = IsSame<decltype(TryAssignTest<T, From>(0)), hwy::SizeTag<1>>()
};
};
template <class T, class From>
static constexpr bool IsAssignable() {
return IsAssignableT<T, From>::value;
}
//------------------------------------------------------------------------------
// F16/BF16 lane types
#pragma pack(push, 1)
// float16_t load/store/conversion intrinsics are always supported on Armv8 and
// VFPv4 (except with MSVC). On Armv7 Clang requires __ARM_FP & 2; GCC requires
// -mfp16-format=ieee.
#if (HWY_ARCH_ARM_A64 && !HWY_COMPILER_MSVC) || \
(HWY_COMPILER_CLANG && defined(__ARM_FP) && (__ARM_FP & 2)) || \
(HWY_COMPILER_GCC_ACTUAL && defined(__ARM_FP16_FORMAT_IEEE))
#define HWY_NEON_HAVE_FLOAT16C 1
#else
#define HWY_NEON_HAVE_FLOAT16C 0
#endif
// C11 extension ISO/IEC TS 18661-3:2015 but not supported on all targets.
// Required if HWY_HAVE_FLOAT16, i.e. RVV with zvfh or AVX3_SPR (with
// sufficiently new compiler supporting avx512fp16). Do not use on clang-cl,
// which is missing __extendhfsf2.
#if ((HWY_ARCH_RVV && defined(__riscv_zvfh) && HWY_COMPILER_CLANG) || \
(HWY_ARCH_X86 && defined(__SSE2__) && \
((HWY_COMPILER_CLANG >= 1600 && !HWY_COMPILER_CLANGCL) || \
HWY_COMPILER_GCC_ACTUAL >= 1200)))
#define HWY_HAVE_C11_FLOAT16 1
#else
#define HWY_HAVE_C11_FLOAT16 0
#endif
// If 1, both __bf16 and a limited set of *_bf16 SVE intrinsics are available:
// create/get/set/dup, ld/st, sel, rev, trn, uzp, zip.
#if HWY_ARCH_ARM_A64 && !(HWY_COMPILER_CLANG && HWY_COMPILER_CLANG < 1700) && \
(defined(__ARM_FEATURE_SVE_BF16) || HWY_COMPILER_GCC_ACTUAL >= 1100)
#define HWY_ARM_HAVE_BFLOAT16 1
#else
#define HWY_ARM_HAVE_BFLOAT16 0
#endif
#if HWY_ARM_HAVE_BFLOAT16 && \
(defined(__ARM_FEATURE_SVE_BF16) || defined(__ARM_FEATURE_SVE))
#define HWY_SVE_HAVE_BFLOAT16 1
#else
#define HWY_SVE_HAVE_BFLOAT16 0
#endif
#if HWY_ARCH_X86 && defined(__SSE2__) && \
(HWY_COMPILER_CLANG >= 1700 || HWY_COMPILER_GCC_ACTUAL >= 1300)
#define HWY_SSE2_HAVE_BFLOAT16 1
#else
#define HWY_SSE2_HAVE_BFLOAT16 0
#endif
#if HWY_ARM_HAVE_BFLOAT16 || HWY_SSE2_HAVE_BFLOAT16
#define HWY_HAVE_GCC_OR_ARM_BFLOAT16 1
#else
#define HWY_HAVE_GCC_OR_ARM_BFLOAT16 0
#endif
// Match [u]int##_t naming scheme so rvv-inl.h macros can obtain the type name
// by concatenating base type and bits. We use a wrapper class instead of a
// typedef to the native type to ensure that the same symbols, e.g. for VQSort,
// are generated regardless of F16 support; see #1684.
struct alignas(2) float16_t {
#if HWY_NEON_HAVE_FLOAT16C // ACLE's __fp16
using Raw = __fp16;
#elif HWY_HAVE_C11_FLOAT16 // C11 _Float16
using Raw = _Float16;
#elif HWY_CXX_LANG > 202002L && defined(__STDCPP_FLOAT16_T__) // C++23
using Raw = std::float16_t;
#else
#define HWY_EMULATE_FLOAT16
using Raw = uint16_t;
Raw bits;
#endif // float16_t
// When backed by a native type, ensure the wrapper behaves like the native
// type by forwarding all operators. Unfortunately it seems difficult to reuse
// this code in a base class, so we repeat it in bfloat16_t.
#ifndef HWY_EMULATE_FLOAT16
Raw raw;
float16_t() noexcept = default;
constexpr float16_t(const float16_t&) noexcept = default;
constexpr float16_t(float16_t&&) noexcept = default;
template <typename T, hwy::EnableIf<!IsSame<RemoveCvRef<T>, float16_t>() &&
IsConvertible<T, Raw>()>* = nullptr>
constexpr float16_t(T&& arg) noexcept(
noexcept(static_cast<Raw>(DeclVal<T>())))
: raw(static_cast<Raw>(static_cast<T&&>(arg))) {}
template <typename T, hwy::EnableIf<!IsSame<RemoveCvRef<T>, float16_t>() &&
!IsConvertible<T, Raw>() &&
IsStaticCastable<T, Raw>()>* = nullptr>
explicit constexpr float16_t(T&& arg) noexcept(
noexcept(static_cast<Raw>(DeclVal<T>())))
: raw(static_cast<Raw>(static_cast<T&&>(arg))) {}
float16_t& operator=(const float16_t&) noexcept = default;
float16_t& operator=(float16_t&&) noexcept = default;
HWY_CXX14_CONSTEXPR float16_t& operator=(Raw arg) noexcept {
raw = arg;
return *this;
}
constexpr operator Raw() const noexcept { return raw; }
template <
typename T,
hwy::EnableIf<IsAssignable<float16_t, T>() &&
IsStaticCastable<decltype(DeclVal<Raw>() + DeclVal<T>()),
Raw>()>* = nullptr>
HWY_CXX14_CONSTEXPR float16_t& operator+=(T&& rhs) noexcept(
noexcept(DeclVal<Raw>() + DeclVal<T>())) {
raw = static_cast<Raw>(raw + static_cast<T&&>(rhs));
return *this;
}
template <
typename T,
hwy::EnableIf<IsAssignable<float16_t, T>() &&
IsStaticCastable<decltype(DeclVal<Raw>() - DeclVal<T>()),
Raw>()>* = nullptr>
HWY_CXX14_CONSTEXPR float16_t& operator-=(T&& rhs) noexcept(
noexcept(DeclVal<Raw>() - DeclVal<T>())) {
raw = static_cast<Raw>(raw - static_cast<T&&>(rhs));
return *this;
}
template <
typename T,
hwy::EnableIf<IsAssignable<float16_t, T>() &&
IsStaticCastable<decltype(DeclVal<Raw>() * DeclVal<T>()),
Raw>()>* = nullptr>
HWY_CXX14_CONSTEXPR float16_t& operator*=(T&& rhs) noexcept(
noexcept(DeclVal<Raw>() * DeclVal<T>())) {
raw = static_cast<Raw>(raw * static_cast<T&&>(rhs));
return *this;
}
template <
typename T,
hwy::EnableIf<IsAssignable<float16_t, T>() &&
IsStaticCastable<decltype(DeclVal<Raw>() / DeclVal<T>()),
Raw>()>* = nullptr>
HWY_CXX14_CONSTEXPR float16_t& operator/=(T&& rhs) noexcept(
noexcept(DeclVal<Raw>() / DeclVal<T>())) {
raw = static_cast<Raw>(raw / static_cast<T&&>(rhs));
return *this;
}
// pre-decrement operator (--x)
HWY_CXX14_CONSTEXPR float16_t& operator--() noexcept {
raw = static_cast<Raw>(raw - Raw{1});
return *this;
}
// post-decrement operator (x--)
HWY_CXX14_CONSTEXPR float16_t operator--(int) noexcept {
float16_t result = *this;
raw = static_cast<Raw>(raw - Raw{1});
return result;
}
// pre-increment operator (++x)
HWY_CXX14_CONSTEXPR float16_t& operator++() noexcept {
raw = static_cast<Raw>(raw + Raw{1});
return *this;
}
// post-increment operator (x++)
HWY_CXX14_CONSTEXPR float16_t operator++(int) noexcept {
float16_t result = *this;
raw = static_cast<Raw>(raw + Raw{1});
return result;
}
constexpr float16_t operator-() const noexcept {
return float16_t(static_cast<Raw>(-raw));
}
constexpr float16_t operator+() const noexcept { return *this; }
#endif // HWY_EMULATE_FLOAT16
};
#ifndef HWY_EMULATE_FLOAT16
constexpr inline bool operator==(float16_t lhs, float16_t rhs) noexcept {
return lhs.raw == rhs.raw;
}
constexpr inline bool operator!=(float16_t lhs, float16_t rhs) noexcept {
return lhs.raw != rhs.raw;
}
constexpr inline bool operator<(float16_t lhs, float16_t rhs) noexcept {
return lhs.raw < rhs.raw;
}
constexpr inline bool operator<=(float16_t lhs, float16_t rhs) noexcept {
return lhs.raw <= rhs.raw;
}
constexpr inline bool operator>(float16_t lhs, float16_t rhs) noexcept {
return lhs.raw > rhs.raw;
}
constexpr inline bool operator>=(float16_t lhs, float16_t rhs) noexcept {
return lhs.raw >= rhs.raw;
}
#if HWY_HAVE_CXX20_THREE_WAY_COMPARE
constexpr inline std::partial_ordering operator<=>(float16_t lhs,
float16_t rhs) noexcept {
return lhs.raw <=> rhs.raw;
}
#endif
#endif // HWY_EMULATE_FLOAT16
#if (HWY_CXX_LANG >= 202100L && defined(__STDCPP_BFLOAT16_T__)) || \
(HWY_HAVE_GCC_OR_ARM_BFLOAT16 && \
(HWY_COMPILER_CLANG >= 1700 || HWY_COMPILER_GCC_ACTUAL >= 1300))
#define HWY_HAVE_BF16_ARITHMETIC_OPS 1
#else
#define HWY_HAVE_BF16_ARITHMETIC_OPS 0
#endif
#if HWY_HAVE_BF16_ARITHMETIC_OPS
#define HWY_BF16_CONSTEXPR constexpr
#else
#define HWY_BF16_CONSTEXPR HWY_BITCASTSCALAR_CONSTEXPR