-
Notifications
You must be signed in to change notification settings - Fork 322
/
arm_neon-inl.h
10459 lines (9225 loc) · 381 KB
/
arm_neon-inl.h
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 2019 Google LLC
// Copyright 2024 Arm Limited and/or its affiliates <[email protected]>
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: BSD-3-Clause
//
// 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.
// 128-bit Arm NEON vectors and operations.
// External include guard in highway.h - see comment there.
// Arm NEON intrinsics are documented at:
// https://developer.arm.com/architectures/instruction-sets/intrinsics/#f:@navigationhierarchiessimdisa=[Neon]
#include "hwy/ops/shared-inl.h"
HWY_DIAGNOSTICS(push)
HWY_DIAGNOSTICS_OFF(disable : 4701, ignored "-Wuninitialized")
#include <arm_neon.h> // NOLINT(build/include_order)
HWY_DIAGNOSTICS(pop)
HWY_BEFORE_NAMESPACE();
namespace hwy {
namespace HWY_NAMESPACE {
namespace detail { // for code folding and Raw128
// Macros used to define single and double function calls for multiple types
// for full and half vectors. These macros are undefined at the end of the file.
// HWY_NEON_BUILD_TPL_* is the template<...> prefix to the function.
#define HWY_NEON_BUILD_TPL_1
#define HWY_NEON_BUILD_TPL_2
#define HWY_NEON_BUILD_TPL_3
// HWY_NEON_BUILD_RET_* is return type; type arg is without _t suffix so we can
// extend it to int32x4x2_t packs.
#define HWY_NEON_BUILD_RET_1(type, size) Vec128<type##_t, size>
#define HWY_NEON_BUILD_RET_2(type, size) Vec128<type##_t, size>
#define HWY_NEON_BUILD_RET_3(type, size) Vec128<type##_t, size>
// HWY_NEON_BUILD_PARAM_* is the list of parameters the function receives.
#define HWY_NEON_BUILD_PARAM_1(type, size) const Vec128<type##_t, size> a
#define HWY_NEON_BUILD_PARAM_2(type, size) \
const Vec128<type##_t, size> a, const Vec128<type##_t, size> b
#define HWY_NEON_BUILD_PARAM_3(type, size) \
const Vec128<type##_t, size> a, const Vec128<type##_t, size> b, \
const Vec128<type##_t, size> c
// HWY_NEON_BUILD_ARG_* is the list of arguments passed to the underlying
// function.
#define HWY_NEON_BUILD_ARG_1 a.raw
#define HWY_NEON_BUILD_ARG_2 a.raw, b.raw
#define HWY_NEON_BUILD_ARG_3 a.raw, b.raw, c.raw
// We use HWY_NEON_EVAL(func, ...) to delay the evaluation of func until after
// the __VA_ARGS__ have been expanded. This allows "func" to be a macro on
// itself like with some of the library "functions" such as vshlq_u8. For
// example, HWY_NEON_EVAL(vshlq_u8, MY_PARAMS) where MY_PARAMS is defined as
// "a, b" (without the quotes) will end up expanding "vshlq_u8(a, b)" if needed.
// Directly writing vshlq_u8(MY_PARAMS) would fail since vshlq_u8() macro
// expects two arguments.
#define HWY_NEON_EVAL(func, ...) func(__VA_ARGS__)
// Main macro definition that defines a single function for the given type and
// size of vector, using the underlying (prefix##infix##suffix) function and
// the template, return type, parameters and arguments defined by the "args"
// parameters passed here (see HWY_NEON_BUILD_* macros defined before).
#define HWY_NEON_DEF_FUNCTION(type, size, name, prefix, infix, suffix, args) \
HWY_CONCAT(HWY_NEON_BUILD_TPL_, args) \
HWY_API HWY_CONCAT(HWY_NEON_BUILD_RET_, args)(type, size) \
name(HWY_CONCAT(HWY_NEON_BUILD_PARAM_, args)(type, size)) { \
return HWY_CONCAT(HWY_NEON_BUILD_RET_, args)(type, size)( \
HWY_NEON_EVAL(prefix##infix##suffix, HWY_NEON_BUILD_ARG_##args)); \
}
// The HWY_NEON_DEF_FUNCTION_* macros define all the variants of a function
// called "name" using the set of neon functions starting with the given
// "prefix" for all the variants of certain types, as specified next to each
// macro. For example, the prefix "vsub" can be used to define the operator-
// using args=2.
// uint8_t
#define HWY_NEON_DEF_FUNCTION_UINT_8(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(uint8, 16, name, prefix##q, infix, u8, args) \
HWY_NEON_DEF_FUNCTION(uint8, 8, name, prefix, infix, u8, args) \
HWY_NEON_DEF_FUNCTION(uint8, 4, name, prefix, infix, u8, args) \
HWY_NEON_DEF_FUNCTION(uint8, 2, name, prefix, infix, u8, args) \
HWY_NEON_DEF_FUNCTION(uint8, 1, name, prefix, infix, u8, args)
// int8_t
#define HWY_NEON_DEF_FUNCTION_INT_8(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(int8, 16, name, prefix##q, infix, s8, args) \
HWY_NEON_DEF_FUNCTION(int8, 8, name, prefix, infix, s8, args) \
HWY_NEON_DEF_FUNCTION(int8, 4, name, prefix, infix, s8, args) \
HWY_NEON_DEF_FUNCTION(int8, 2, name, prefix, infix, s8, args) \
HWY_NEON_DEF_FUNCTION(int8, 1, name, prefix, infix, s8, args)
// uint16_t
#define HWY_NEON_DEF_FUNCTION_UINT_16(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(uint16, 8, name, prefix##q, infix, u16, args) \
HWY_NEON_DEF_FUNCTION(uint16, 4, name, prefix, infix, u16, args) \
HWY_NEON_DEF_FUNCTION(uint16, 2, name, prefix, infix, u16, args) \
HWY_NEON_DEF_FUNCTION(uint16, 1, name, prefix, infix, u16, args)
// int16_t
#define HWY_NEON_DEF_FUNCTION_INT_16(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(int16, 8, name, prefix##q, infix, s16, args) \
HWY_NEON_DEF_FUNCTION(int16, 4, name, prefix, infix, s16, args) \
HWY_NEON_DEF_FUNCTION(int16, 2, name, prefix, infix, s16, args) \
HWY_NEON_DEF_FUNCTION(int16, 1, name, prefix, infix, s16, args)
// uint32_t
#define HWY_NEON_DEF_FUNCTION_UINT_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(uint32, 4, name, prefix##q, infix, u32, args) \
HWY_NEON_DEF_FUNCTION(uint32, 2, name, prefix, infix, u32, args) \
HWY_NEON_DEF_FUNCTION(uint32, 1, name, prefix, infix, u32, args)
// int32_t
#define HWY_NEON_DEF_FUNCTION_INT_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(int32, 4, name, prefix##q, infix, s32, args) \
HWY_NEON_DEF_FUNCTION(int32, 2, name, prefix, infix, s32, args) \
HWY_NEON_DEF_FUNCTION(int32, 1, name, prefix, infix, s32, args)
// uint64_t
#define HWY_NEON_DEF_FUNCTION_UINT_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(uint64, 2, name, prefix##q, infix, u64, args) \
HWY_NEON_DEF_FUNCTION(uint64, 1, name, prefix, infix, u64, args)
// int64_t
#define HWY_NEON_DEF_FUNCTION_INT_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(int64, 2, name, prefix##q, infix, s64, args) \
HWY_NEON_DEF_FUNCTION(int64, 1, name, prefix, infix, s64, args)
// Clang 17 crashes with bf16, see github.com/llvm/llvm-project/issues/64179.
#undef HWY_NEON_HAVE_BFLOAT16
#if HWY_HAVE_SCALAR_BF16_TYPE && \
((HWY_TARGET == HWY_NEON_BF16 && \
(!HWY_COMPILER_CLANG || HWY_COMPILER_CLANG >= 1800)) || \
defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC))
#define HWY_NEON_HAVE_BFLOAT16 1
#else
#define HWY_NEON_HAVE_BFLOAT16 0
#endif
// HWY_NEON_HAVE_F32_TO_BF16C is defined if NEON vcvt_bf16_f32 and
// vbfdot_f32 are available, even if the __bf16 type is disabled due to
// GCC/Clang bugs.
#undef HWY_NEON_HAVE_F32_TO_BF16C
#if HWY_NEON_HAVE_BFLOAT16 || HWY_TARGET == HWY_NEON_BF16 || \
(defined(__ARM_FEATURE_BF16_VECTOR_ARITHMETIC) && \
(HWY_COMPILER_GCC_ACTUAL >= 1000 || HWY_COMPILER_CLANG >= 1100))
#define HWY_NEON_HAVE_F32_TO_BF16C 1
#else
#define HWY_NEON_HAVE_F32_TO_BF16C 0
#endif
// bfloat16_t
#if HWY_NEON_HAVE_BFLOAT16
#define HWY_NEON_DEF_FUNCTION_BFLOAT_16(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(bfloat16, 8, name, prefix##q, infix, bf16, args) \
HWY_NEON_DEF_FUNCTION(bfloat16, 4, name, prefix, infix, bf16, args) \
HWY_NEON_DEF_FUNCTION(bfloat16, 2, name, prefix, infix, bf16, args) \
HWY_NEON_DEF_FUNCTION(bfloat16, 1, name, prefix, infix, bf16, args)
#else
#define HWY_NEON_DEF_FUNCTION_BFLOAT_16(name, prefix, infix, args)
#endif
// Used for conversion instructions if HWY_NEON_HAVE_F16C.
#define HWY_NEON_DEF_FUNCTION_FLOAT_16_UNCONDITIONAL(name, prefix, infix, \
args) \
HWY_NEON_DEF_FUNCTION(float16, 8, name, prefix##q, infix, f16, args) \
HWY_NEON_DEF_FUNCTION(float16, 4, name, prefix, infix, f16, args) \
HWY_NEON_DEF_FUNCTION(float16, 2, name, prefix, infix, f16, args) \
HWY_NEON_DEF_FUNCTION(float16, 1, name, prefix, infix, f16, args)
// float16_t
#if HWY_HAVE_FLOAT16
#define HWY_NEON_DEF_FUNCTION_FLOAT_16(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_FLOAT_16_UNCONDITIONAL(name, prefix, infix, args)
#else
#define HWY_NEON_DEF_FUNCTION_FLOAT_16(name, prefix, infix, args)
#endif
// Enable generic functions for whichever of (f16, bf16) are not supported.
#if !HWY_HAVE_FLOAT16 && !HWY_NEON_HAVE_BFLOAT16
#define HWY_NEON_IF_EMULATED_D(D) HWY_IF_SPECIAL_FLOAT_D(D)
#define HWY_GENERIC_IF_EMULATED_D(D) HWY_IF_SPECIAL_FLOAT_D(D)
#define HWY_NEON_IF_NOT_EMULATED_D(D) HWY_IF_NOT_SPECIAL_FLOAT_D(D)
#elif !HWY_HAVE_FLOAT16 && HWY_NEON_HAVE_BFLOAT16
#define HWY_NEON_IF_EMULATED_D(D) HWY_IF_F16_D(D)
#define HWY_GENERIC_IF_EMULATED_D(D) HWY_IF_F16_D(D)
#define HWY_NEON_IF_NOT_EMULATED_D(D) HWY_IF_NOT_F16_D(D)
#elif HWY_HAVE_FLOAT16 && !HWY_NEON_HAVE_BFLOAT16
#define HWY_NEON_IF_EMULATED_D(D) HWY_IF_BF16_D(D)
#define HWY_GENERIC_IF_EMULATED_D(D) HWY_IF_BF16_D(D)
#define HWY_NEON_IF_NOT_EMULATED_D(D) HWY_IF_NOT_BF16_D(D)
#elif HWY_HAVE_FLOAT16 && HWY_NEON_HAVE_BFLOAT16
// NOTE: hwy::EnableIf<!hwy::IsSame<D, D>()>* = nullptr is used instead of
// hwy::EnableIf<false>* = nullptr to avoid compiler errors since
// !hwy::IsSame<D, D>() is always false and as !hwy::IsSame<D, D>() will cause
// SFINAE to occur instead of a hard error due to a dependency on the D template
// argument
#define HWY_NEON_IF_EMULATED_D(D) hwy::EnableIf<!hwy::IsSame<D, D>()>* = nullptr
#define HWY_GENERIC_IF_EMULATED_D(D) \
hwy::EnableIf<!hwy::IsSame<D, D>()>* = nullptr
#define HWY_NEON_IF_NOT_EMULATED_D(D) hwy::EnableIf<true>* = nullptr
#else
#error "Logic error, handled all four cases"
#endif
// float
#define HWY_NEON_DEF_FUNCTION_FLOAT_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(float32, 4, name, prefix##q, infix, f32, args) \
HWY_NEON_DEF_FUNCTION(float32, 2, name, prefix, infix, f32, args) \
HWY_NEON_DEF_FUNCTION(float32, 1, name, prefix, infix, f32, args)
// double
#if HWY_HAVE_FLOAT64
#define HWY_NEON_DEF_FUNCTION_FLOAT_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(float64, 2, name, prefix##q, infix, f64, args) \
HWY_NEON_DEF_FUNCTION(float64, 1, name, prefix, infix, f64, args)
#else
#define HWY_NEON_DEF_FUNCTION_FLOAT_64(name, prefix, infix, args)
#endif
// Helper macros to define for more than one type.
// uint8_t, uint16_t and uint32_t
#define HWY_NEON_DEF_FUNCTION_UINT_8_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UINT_8(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UINT_16(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UINT_32(name, prefix, infix, args)
// int8_t, int16_t and int32_t
#define HWY_NEON_DEF_FUNCTION_INT_8_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INT_8(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INT_16(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INT_32(name, prefix, infix, args)
// uint8_t, uint16_t, uint32_t and uint64_t
#define HWY_NEON_DEF_FUNCTION_UINTS(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UINT_8_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UINT_64(name, prefix, infix, args)
// int8_t, int16_t, int32_t and int64_t
#define HWY_NEON_DEF_FUNCTION_INTS(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INT_8_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INT_64(name, prefix, infix, args)
// All int*_t and uint*_t up to 64
#define HWY_NEON_DEF_FUNCTION_INTS_UINTS(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INTS(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UINTS(name, prefix, infix, args)
#define HWY_NEON_DEF_FUNCTION_FLOAT_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_FLOAT_16(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_FLOAT_32(name, prefix, infix, args)
#define HWY_NEON_DEF_FUNCTION_ALL_FLOATS(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_FLOAT_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_FLOAT_64(name, prefix, infix, args)
// All previous types.
#define HWY_NEON_DEF_FUNCTION_ALL_TYPES(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INTS_UINTS(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_ALL_FLOATS(name, prefix, infix, args)
#define HWY_NEON_DEF_FUNCTION_UI_8_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UINT_8_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INT_8_16_32(name, prefix, infix, args)
#define HWY_NEON_DEF_FUNCTION_UIF_8_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UI_8_16_32(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_FLOAT_16_32(name, prefix, infix, args)
#define HWY_NEON_DEF_FUNCTION_UIF_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_UINT_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_INT_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_FLOAT_64(name, prefix, infix, args)
// For vzip1/2
#define HWY_NEON_DEF_FUNCTION_FULL_UI_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(uint64, 2, name, prefix##q, infix, u64, args) \
HWY_NEON_DEF_FUNCTION(int64, 2, name, prefix##q, infix, s64, args)
#define HWY_NEON_DEF_FUNCTION_FULL_UIF_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION_FULL_UI_64(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(float64, 2, name, prefix##q, infix, f64, args)
// For eor3q, which is only defined for full vectors.
#define HWY_NEON_DEF_FUNCTION_FULL_UI(name, prefix, infix, args) \
HWY_NEON_DEF_FUNCTION(uint8, 16, name, prefix##q, infix, u8, args) \
HWY_NEON_DEF_FUNCTION(uint16, 8, name, prefix##q, infix, u16, args) \
HWY_NEON_DEF_FUNCTION(uint32, 4, name, prefix##q, infix, u32, args) \
HWY_NEON_DEF_FUNCTION(int8, 16, name, prefix##q, infix, s8, args) \
HWY_NEON_DEF_FUNCTION(int16, 8, name, prefix##q, infix, s16, args) \
HWY_NEON_DEF_FUNCTION(int32, 4, name, prefix##q, infix, s32, args) \
HWY_NEON_DEF_FUNCTION_FULL_UI_64(name, prefix, infix, args)
// Emulation of some intrinsics on armv7.
#if HWY_ARCH_ARM_V7
#define vuzp1_s8(x, y) vuzp_s8(x, y).val[0]
#define vuzp1_u8(x, y) vuzp_u8(x, y).val[0]
#define vuzp1_s16(x, y) vuzp_s16(x, y).val[0]
#define vuzp1_u16(x, y) vuzp_u16(x, y).val[0]
#define vuzp1_s32(x, y) vuzp_s32(x, y).val[0]
#define vuzp1_u32(x, y) vuzp_u32(x, y).val[0]
#define vuzp1_f32(x, y) vuzp_f32(x, y).val[0]
#define vuzp1q_s8(x, y) vuzpq_s8(x, y).val[0]
#define vuzp1q_u8(x, y) vuzpq_u8(x, y).val[0]
#define vuzp1q_s16(x, y) vuzpq_s16(x, y).val[0]
#define vuzp1q_u16(x, y) vuzpq_u16(x, y).val[0]
#define vuzp1q_s32(x, y) vuzpq_s32(x, y).val[0]
#define vuzp1q_u32(x, y) vuzpq_u32(x, y).val[0]
#define vuzp1q_f32(x, y) vuzpq_f32(x, y).val[0]
#define vuzp2_s8(x, y) vuzp_s8(x, y).val[1]
#define vuzp2_u8(x, y) vuzp_u8(x, y).val[1]
#define vuzp2_s16(x, y) vuzp_s16(x, y).val[1]
#define vuzp2_u16(x, y) vuzp_u16(x, y).val[1]
#define vuzp2_s32(x, y) vuzp_s32(x, y).val[1]
#define vuzp2_u32(x, y) vuzp_u32(x, y).val[1]
#define vuzp2_f32(x, y) vuzp_f32(x, y).val[1]
#define vuzp2q_s8(x, y) vuzpq_s8(x, y).val[1]
#define vuzp2q_u8(x, y) vuzpq_u8(x, y).val[1]
#define vuzp2q_s16(x, y) vuzpq_s16(x, y).val[1]
#define vuzp2q_u16(x, y) vuzpq_u16(x, y).val[1]
#define vuzp2q_s32(x, y) vuzpq_s32(x, y).val[1]
#define vuzp2q_u32(x, y) vuzpq_u32(x, y).val[1]
#define vuzp2q_f32(x, y) vuzpq_f32(x, y).val[1]
#define vzip1_s8(x, y) vzip_s8(x, y).val[0]
#define vzip1_u8(x, y) vzip_u8(x, y).val[0]
#define vzip1_s16(x, y) vzip_s16(x, y).val[0]
#define vzip1_u16(x, y) vzip_u16(x, y).val[0]
#define vzip1_f32(x, y) vzip_f32(x, y).val[0]
#define vzip1_u32(x, y) vzip_u32(x, y).val[0]
#define vzip1_s32(x, y) vzip_s32(x, y).val[0]
#define vzip1q_s8(x, y) vzipq_s8(x, y).val[0]
#define vzip1q_u8(x, y) vzipq_u8(x, y).val[0]
#define vzip1q_s16(x, y) vzipq_s16(x, y).val[0]
#define vzip1q_u16(x, y) vzipq_u16(x, y).val[0]
#define vzip1q_s32(x, y) vzipq_s32(x, y).val[0]
#define vzip1q_u32(x, y) vzipq_u32(x, y).val[0]
#define vzip1q_f32(x, y) vzipq_f32(x, y).val[0]
#define vzip2_s8(x, y) vzip_s8(x, y).val[1]
#define vzip2_u8(x, y) vzip_u8(x, y).val[1]
#define vzip2_s16(x, y) vzip_s16(x, y).val[1]
#define vzip2_u16(x, y) vzip_u16(x, y).val[1]
#define vzip2_s32(x, y) vzip_s32(x, y).val[1]
#define vzip2_u32(x, y) vzip_u32(x, y).val[1]
#define vzip2_f32(x, y) vzip_f32(x, y).val[1]
#define vzip2q_s8(x, y) vzipq_s8(x, y).val[1]
#define vzip2q_u8(x, y) vzipq_u8(x, y).val[1]
#define vzip2q_s16(x, y) vzipq_s16(x, y).val[1]
#define vzip2q_u16(x, y) vzipq_u16(x, y).val[1]
#define vzip2q_s32(x, y) vzipq_s32(x, y).val[1]
#define vzip2q_u32(x, y) vzipq_u32(x, y).val[1]
#define vzip2q_f32(x, y) vzipq_f32(x, y).val[1]
#endif
// Wrappers over uint8x16x2_t etc. so we can define StoreInterleaved2
// overloads for all vector types, even those (bfloat16_t) where the
// underlying vector is the same as others (uint16_t).
template <typename T, size_t N>
struct Tuple2;
template <typename T, size_t N>
struct Tuple3;
template <typename T, size_t N>
struct Tuple4;
template <>
struct Tuple2<uint8_t, 16> {
uint8x16x2_t raw;
};
template <size_t N>
struct Tuple2<uint8_t, N> {
uint8x8x2_t raw;
};
template <>
struct Tuple2<int8_t, 16> {
int8x16x2_t raw;
};
template <size_t N>
struct Tuple2<int8_t, N> {
int8x8x2_t raw;
};
template <>
struct Tuple2<uint16_t, 8> {
uint16x8x2_t raw;
};
template <size_t N>
struct Tuple2<uint16_t, N> {
uint16x4x2_t raw;
};
template <>
struct Tuple2<int16_t, 8> {
int16x8x2_t raw;
};
template <size_t N>
struct Tuple2<int16_t, N> {
int16x4x2_t raw;
};
template <>
struct Tuple2<uint32_t, 4> {
uint32x4x2_t raw;
};
template <size_t N>
struct Tuple2<uint32_t, N> {
uint32x2x2_t raw;
};
template <>
struct Tuple2<int32_t, 4> {
int32x4x2_t raw;
};
template <size_t N>
struct Tuple2<int32_t, N> {
int32x2x2_t raw;
};
template <>
struct Tuple2<uint64_t, 2> {
uint64x2x2_t raw;
};
template <size_t N>
struct Tuple2<uint64_t, N> {
uint64x1x2_t raw;
};
template <>
struct Tuple2<int64_t, 2> {
int64x2x2_t raw;
};
template <size_t N>
struct Tuple2<int64_t, N> {
int64x1x2_t raw;
};
template <>
struct Tuple2<float32_t, 4> {
float32x4x2_t raw;
};
template <size_t N>
struct Tuple2<float32_t, N> {
float32x2x2_t raw;
};
#if HWY_HAVE_FLOAT64
template <>
struct Tuple2<float64_t, 2> {
float64x2x2_t raw;
};
template <size_t N>
struct Tuple2<float64_t, N> {
float64x1x2_t raw;
};
#endif // HWY_HAVE_FLOAT64
template <>
struct Tuple3<uint8_t, 16> {
uint8x16x3_t raw;
};
template <size_t N>
struct Tuple3<uint8_t, N> {
uint8x8x3_t raw;
};
template <>
struct Tuple3<int8_t, 16> {
int8x16x3_t raw;
};
template <size_t N>
struct Tuple3<int8_t, N> {
int8x8x3_t raw;
};
template <>
struct Tuple3<uint16_t, 8> {
uint16x8x3_t raw;
};
template <size_t N>
struct Tuple3<uint16_t, N> {
uint16x4x3_t raw;
};
template <>
struct Tuple3<int16_t, 8> {
int16x8x3_t raw;
};
template <size_t N>
struct Tuple3<int16_t, N> {
int16x4x3_t raw;
};
template <>
struct Tuple3<uint32_t, 4> {
uint32x4x3_t raw;
};
template <size_t N>
struct Tuple3<uint32_t, N> {
uint32x2x3_t raw;
};
template <>
struct Tuple3<int32_t, 4> {
int32x4x3_t raw;
};
template <size_t N>
struct Tuple3<int32_t, N> {
int32x2x3_t raw;
};
template <>
struct Tuple3<uint64_t, 2> {
uint64x2x3_t raw;
};
template <size_t N>
struct Tuple3<uint64_t, N> {
uint64x1x3_t raw;
};
template <>
struct Tuple3<int64_t, 2> {
int64x2x3_t raw;
};
template <size_t N>
struct Tuple3<int64_t, N> {
int64x1x3_t raw;
};
template <>
struct Tuple3<float32_t, 4> {
float32x4x3_t raw;
};
template <size_t N>
struct Tuple3<float32_t, N> {
float32x2x3_t raw;
};
#if HWY_HAVE_FLOAT64
template <>
struct Tuple3<float64_t, 2> {
float64x2x3_t raw;
};
template <size_t N>
struct Tuple3<float64_t, N> {
float64x1x3_t raw;
};
#endif // HWY_HAVE_FLOAT64
template <>
struct Tuple4<uint8_t, 16> {
uint8x16x4_t raw;
};
template <size_t N>
struct Tuple4<uint8_t, N> {
uint8x8x4_t raw;
};
template <>
struct Tuple4<int8_t, 16> {
int8x16x4_t raw;
};
template <size_t N>
struct Tuple4<int8_t, N> {
int8x8x4_t raw;
};
template <>
struct Tuple4<uint16_t, 8> {
uint16x8x4_t raw;
};
template <size_t N>
struct Tuple4<uint16_t, N> {
uint16x4x4_t raw;
};
template <>
struct Tuple4<int16_t, 8> {
int16x8x4_t raw;
};
template <size_t N>
struct Tuple4<int16_t, N> {
int16x4x4_t raw;
};
template <>
struct Tuple4<uint32_t, 4> {
uint32x4x4_t raw;
};
template <size_t N>
struct Tuple4<uint32_t, N> {
uint32x2x4_t raw;
};
template <>
struct Tuple4<int32_t, 4> {
int32x4x4_t raw;
};
template <size_t N>
struct Tuple4<int32_t, N> {
int32x2x4_t raw;
};
template <>
struct Tuple4<uint64_t, 2> {
uint64x2x4_t raw;
};
template <size_t N>
struct Tuple4<uint64_t, N> {
uint64x1x4_t raw;
};
template <>
struct Tuple4<int64_t, 2> {
int64x2x4_t raw;
};
template <size_t N>
struct Tuple4<int64_t, N> {
int64x1x4_t raw;
};
template <>
struct Tuple4<float32_t, 4> {
float32x4x4_t raw;
};
template <size_t N>
struct Tuple4<float32_t, N> {
float32x2x4_t raw;
};
#if HWY_HAVE_FLOAT64
template <>
struct Tuple4<float64_t, 2> {
float64x2x4_t raw;
};
template <size_t N>
struct Tuple4<float64_t, N> {
float64x1x4_t raw;
};
#endif // HWY_HAVE_FLOAT64
template <typename T, size_t N>
struct Raw128;
template <>
struct Raw128<uint8_t, 16> {
using type = uint8x16_t;
};
template <size_t N>
struct Raw128<uint8_t, N> {
using type = uint8x8_t;
};
template <>
struct Raw128<uint16_t, 8> {
using type = uint16x8_t;
};
template <size_t N>
struct Raw128<uint16_t, N> {
using type = uint16x4_t;
};
template <>
struct Raw128<uint32_t, 4> {
using type = uint32x4_t;
};
template <size_t N>
struct Raw128<uint32_t, N> {
using type = uint32x2_t;
};
template <>
struct Raw128<uint64_t, 2> {
using type = uint64x2_t;
};
template <>
struct Raw128<uint64_t, 1> {
using type = uint64x1_t;
};
template <>
struct Raw128<int8_t, 16> {
using type = int8x16_t;
};
template <size_t N>
struct Raw128<int8_t, N> {
using type = int8x8_t;
};
template <>
struct Raw128<int16_t, 8> {
using type = int16x8_t;
};
template <size_t N>
struct Raw128<int16_t, N> {
using type = int16x4_t;
};
template <>
struct Raw128<int32_t, 4> {
using type = int32x4_t;
};
template <size_t N>
struct Raw128<int32_t, N> {
using type = int32x2_t;
};
template <>
struct Raw128<int64_t, 2> {
using type = int64x2_t;
};
template <>
struct Raw128<int64_t, 1> {
using type = int64x1_t;
};
template <>
struct Raw128<float, 4> {
using type = float32x4_t;
};
template <size_t N>
struct Raw128<float, N> {
using type = float32x2_t;
};
#if HWY_HAVE_FLOAT64
template <>
struct Raw128<double, 2> {
using type = float64x2_t;
};
template <>
struct Raw128<double, 1> {
using type = float64x1_t;
};
#endif // HWY_HAVE_FLOAT64
#if HWY_NEON_HAVE_F16C
template <>
struct Tuple2<float16_t, 8> {
float16x8x2_t raw;
};
template <size_t N>
struct Tuple2<float16_t, N> {
float16x4x2_t raw;
};
template <>
struct Tuple3<float16_t, 8> {
float16x8x3_t raw;
};
template <size_t N>
struct Tuple3<float16_t, N> {
float16x4x3_t raw;
};
template <>
struct Tuple4<float16_t, 8> {
float16x8x4_t raw;
};
template <size_t N>
struct Tuple4<float16_t, N> {
float16x4x4_t raw;
};
template <>
struct Raw128<float16_t, 8> {
using type = float16x8_t;
};
template <size_t N>
struct Raw128<float16_t, N> {
using type = float16x4_t;
};
#else // !HWY_NEON_HAVE_F16C
template <size_t N>
struct Tuple2<float16_t, N> : public Tuple2<uint16_t, N> {};
template <size_t N>
struct Tuple3<float16_t, N> : public Tuple3<uint16_t, N> {};
template <size_t N>
struct Tuple4<float16_t, N> : public Tuple4<uint16_t, N> {};
template <size_t N>
struct Raw128<float16_t, N> : public Raw128<uint16_t, N> {};
#endif // HWY_NEON_HAVE_F16C
#if HWY_NEON_HAVE_BFLOAT16
template <>
struct Tuple2<bfloat16_t, 8> {
bfloat16x8x2_t raw;
};
template <size_t N>
struct Tuple2<bfloat16_t, N> {
bfloat16x4x2_t raw;
};
template <>
struct Tuple3<bfloat16_t, 8> {
bfloat16x8x3_t raw;
};
template <size_t N>
struct Tuple3<bfloat16_t, N> {
bfloat16x4x3_t raw;
};
template <>
struct Tuple4<bfloat16_t, 8> {
bfloat16x8x4_t raw;
};
template <size_t N>
struct Tuple4<bfloat16_t, N> {
bfloat16x4x4_t raw;
};
template <>
struct Raw128<bfloat16_t, 8> {
using type = bfloat16x8_t;
};
template <size_t N>
struct Raw128<bfloat16_t, N> {
using type = bfloat16x4_t;
};
#else // !HWY_NEON_HAVE_BFLOAT16
template <size_t N>
struct Tuple2<bfloat16_t, N> : public Tuple2<uint16_t, N> {};
template <size_t N>
struct Tuple3<bfloat16_t, N> : public Tuple3<uint16_t, N> {};
template <size_t N>
struct Tuple4<bfloat16_t, N> : public Tuple4<uint16_t, N> {};
template <size_t N>
struct Raw128<bfloat16_t, N> : public Raw128<uint16_t, N> {};
#endif // HWY_NEON_HAVE_BFLOAT16
} // namespace detail
template <typename T, size_t N = 16 / sizeof(T)>
class Vec128 {
public:
using Raw = typename detail::Raw128<T, N>::type;
using PrivateT = T; // only for DFromV
static constexpr size_t kPrivateN = N; // only for DFromV
HWY_INLINE Vec128() {}
Vec128(const Vec128&) = default;
Vec128& operator=(const Vec128&) = default;
HWY_INLINE explicit Vec128(const Raw raw) : raw(raw) {}
// Compound assignment. Only usable if there is a corresponding non-member
// binary operator overload. For example, only f32 and f64 support division.
HWY_INLINE Vec128& operator*=(const Vec128 other) {
return *this = (*this * other);
}
HWY_INLINE Vec128& operator/=(const Vec128 other) {
return *this = (*this / other);
}
HWY_INLINE Vec128& operator+=(const Vec128 other) {
return *this = (*this + other);
}
HWY_INLINE Vec128& operator-=(const Vec128 other) {
return *this = (*this - other);
}
HWY_INLINE Vec128& operator%=(const Vec128 other) {
return *this = (*this % other);
}
HWY_INLINE Vec128& operator&=(const Vec128 other) {
return *this = (*this & other);
}
HWY_INLINE Vec128& operator|=(const Vec128 other) {
return *this = (*this | other);
}
HWY_INLINE Vec128& operator^=(const Vec128 other) {
return *this = (*this ^ other);
}
Raw raw;
};
template <typename T>
using Vec64 = Vec128<T, 8 / sizeof(T)>;
template <typename T>
using Vec32 = Vec128<T, 4 / sizeof(T)>;
template <typename T>
using Vec16 = Vec128<T, 2 / sizeof(T)>;
// FF..FF or 0.
template <typename T, size_t N = 16 / sizeof(T)>
class Mask128 {
public:
// Arm C Language Extensions return and expect unsigned type.
using Raw = typename detail::Raw128<MakeUnsigned<T>, N>::type;
using PrivateT = T; // only for DFromM
static constexpr size_t kPrivateN = N; // only for DFromM
HWY_INLINE Mask128() {}
Mask128(const Mask128&) = default;
Mask128& operator=(const Mask128&) = default;
HWY_INLINE explicit Mask128(const Raw raw) : raw(raw) {}
Raw raw;
};
template <typename T>
using Mask64 = Mask128<T, 8 / sizeof(T)>;
template <class V>
using DFromV = Simd<typename V::PrivateT, V::kPrivateN, 0>;
template <class M>
using DFromM = Simd<typename M::PrivateT, M::kPrivateN, 0>;
template <class V>
using TFromV = typename V::PrivateT;
// ------------------------------ Set
namespace detail {
// We want to route any combination of N/kPow2 to the intrinsics depending on
// whether the requested size is <= 64 bits or 128. HWY_NEON_BUILD_TPL is
// unconditional and currently does not accept inputs (such as whether the
// vector is 64 or 128-bit). Thus we are not able to use HWY_IF_V_SIZE_D for
// SFINAE. We instead define a private NativeSet which receives a Simd<> whose
// kPow2 has already been folded into its N.
#define HWY_NEON_BUILD_TPL_HWY_SET
#define HWY_NEON_BUILD_RET_HWY_SET(type, size) Vec128<type##_t, size>
#define HWY_NEON_BUILD_PARAM_HWY_SET(type, size) \
Simd<type##_t, size, 0> /* tag */, type##_t t
#define HWY_NEON_BUILD_ARG_HWY_SET t
HWY_NEON_DEF_FUNCTION_ALL_TYPES(NativeSet, vdup, _n_, HWY_SET)
#if !HWY_HAVE_FLOAT16 && HWY_NEON_HAVE_F16C
HWY_NEON_DEF_FUNCTION_FLOAT_16_UNCONDITIONAL(NativeSet, vdup, _n_, HWY_SET)
#endif
HWY_NEON_DEF_FUNCTION_BFLOAT_16(NativeSet, vdup, _n_, HWY_SET)
template <class D, HWY_NEON_IF_EMULATED_D(D)>
HWY_API Vec128<TFromD<D>, MaxLanes(D())> NativeSet(D d, TFromD<D> t) {
const uint16_t tu = BitCastScalar<uint16_t>(t);
return Vec128<TFromD<D>, d.MaxLanes()>(Set(RebindToUnsigned<D>(), tu).raw);
}
#undef HWY_NEON_BUILD_TPL_HWY_SET
#undef HWY_NEON_BUILD_RET_HWY_SET
#undef HWY_NEON_BUILD_PARAM_HWY_SET
#undef HWY_NEON_BUILD_ARG_HWY_SET
} // namespace detail
// Full vector. Cannot yet use VFromD because that is defined in terms of Set.
// Do not use a typename T = TFromD<D> argument because T will be deduced from
// the actual argument type, which can differ from TFromD<D>.
template <class D, HWY_IF_V_SIZE_D(D, 16), typename T>
HWY_INLINE Vec128<TFromD<D>> Set(D /* tag */, T t) {
return detail::NativeSet(Full128<TFromD<D>>(), static_cast<TFromD<D>>(t));
}
// Partial vector: create 64-bit and return wrapper.
template <class D, HWY_IF_V_SIZE_LE_D(D, 8), typename T>
HWY_API Vec128<TFromD<D>, MaxLanes(D())> Set(D /* tag */, T t) {
const Full64<TFromD<D>> dfull;
return Vec128<TFromD<D>, MaxLanes(D())>(
detail::NativeSet(dfull, static_cast<TFromD<D>>(t)).raw);
}
template <class D>
using VFromD = decltype(Set(D(), TFromD<D>()));
template <class D>
HWY_API VFromD<D> Zero(D d) {
// Default ctor also works for bfloat16_t and float16_t.
return Set(d, TFromD<D>{});
}
HWY_DIAGNOSTICS(push)
HWY_DIAGNOSTICS_OFF(disable : 4700, ignored "-Wuninitialized")
#if HWY_COMPILER_GCC_ACTUAL
HWY_DIAGNOSTICS_OFF(disable : 4701, ignored "-Wmaybe-uninitialized")
#endif
template <class D>
HWY_API VFromD<D> Undefined(D /*tag*/) {
#if HWY_HAS_BUILTIN(__builtin_nondeterministic_value)
return VFromD<D>{__builtin_nondeterministic_value(Zero(D()).raw)};
#else
VFromD<D> v;
return v;
#endif
}
HWY_DIAGNOSTICS(pop)
#if !HWY_COMPILER_GCC && !HWY_COMPILER_CLANGCL
namespace detail {
#pragma pack(push, 1)
template <class T>
struct alignas(8) Vec64ValsWrapper {
static_assert(sizeof(T) >= 1, "sizeof(T) >= 1 must be true");
static_assert(sizeof(T) <= 8, "sizeof(T) <= 8 must be true");
T vals[8 / sizeof(T)];
};
#pragma pack(pop)
} // namespace detail