forked from vranhub/aeroway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrvv-inl.h
6300 lines (5349 loc) · 260 KB
/
rvv-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 2021 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.
// RISC-V V vectors (length not known at compile time).
// External include guard in highway.h - see comment there.
#include <riscv_vector.h>
#include "hwy/ops/shared-inl.h"
HWY_BEFORE_NAMESPACE();
namespace hwy {
namespace HWY_NAMESPACE {
// Support for vfloat16m*_t and PromoteTo/DemoteTo.
#ifdef __riscv_zvfhmin
#define HWY_RVV_HAVE_F16C 1
#else
#define HWY_RVV_HAVE_F16C 0
#endif
template <class V>
struct DFromV_t {}; // specialized in macros
template <class V>
using DFromV = typename DFromV_t<RemoveConst<V>>::type;
template <class V>
using TFromV = TFromD<DFromV<V>>;
template <typename T, size_t N, int kPow2>
constexpr size_t MLenFromD(Simd<T, N, kPow2> /* tag */) {
// Returns divisor = type bits / LMUL. Folding *8 into the ScaleByPower
// argument enables fractional LMUL < 1. Limit to 64 because that is the
// largest value for which vbool##_t are defined.
return HWY_MIN(64, sizeof(T) * 8 * 8 / detail::ScaleByPower(8, kPow2));
}
namespace detail {
template <class D>
class AdjustSimdTagToMinVecPow2_t {};
template <typename T, size_t N, int kPow2>
class AdjustSimdTagToMinVecPow2_t<Simd<T, N, kPow2>> {
private:
using D = Simd<T, N, kPow2>;
static constexpr int kMinVecPow2 =
-3 + static_cast<int>(FloorLog2(sizeof(T)));
static constexpr size_t kNumMaxLanes = HWY_MAX_LANES_D(D);
static constexpr int kNewPow2 = HWY_MAX(kPow2, kMinVecPow2);
static constexpr size_t kNewN = D::template NewN<kNewPow2, kNumMaxLanes>();
public:
using type = Simd<T, kNewN, kNewPow2>;
};
template <class D>
using AdjustSimdTagToMinVecPow2 =
typename AdjustSimdTagToMinVecPow2_t<RemoveConst<D>>::type;
} // namespace detail
// ================================================== MACROS
// Generate specializations and function definitions using X macros. Although
// harder to read and debug, writing everything manually is too bulky.
namespace detail { // for code folding
// For all mask sizes MLEN: (1/Nth of a register, one bit per lane)
// The first three arguments are arbitrary SEW, LMUL, SHIFT such that
// SEW >> SHIFT = MLEN.
#define HWY_RVV_FOREACH_B(X_MACRO, NAME, OP) \
X_MACRO(64, 0, 64, NAME, OP) \
X_MACRO(32, 0, 32, NAME, OP) \
X_MACRO(16, 0, 16, NAME, OP) \
X_MACRO(8, 0, 8, NAME, OP) \
X_MACRO(8, 1, 4, NAME, OP) \
X_MACRO(8, 2, 2, NAME, OP) \
X_MACRO(8, 3, 1, NAME, OP)
// For given SEW, iterate over one of LMULS: _TRUNC, _EXT, _ALL. This allows
// reusing type lists such as HWY_RVV_FOREACH_U for _ALL (the usual case) or
// _EXT (for Combine). To achieve this, we HWY_CONCAT with the LMULS suffix.
//
// Precompute SEW/LMUL => MLEN to allow token-pasting the result. For the same
// reason, also pass the double-width and half SEW and LMUL (suffixed D and H,
// respectively). "__" means there is no corresponding LMUL (e.g. LMULD for m8).
// Args: BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, SHIFT, MLEN, NAME, OP
// LMULS = _TRUNC: truncatable (not the smallest LMUL)
#define HWY_RVV_FOREACH_08_TRUNC(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, mf4, mf2, mf8, -2, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, mf2, m1, mf4, -1, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m1, m2, mf2, 0, /*MLEN=*/8, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m2, m4, m1, 1, /*MLEN=*/4, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m4, m8, m2, 2, /*MLEN=*/2, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m8, __, m4, 3, /*MLEN=*/1, NAME, OP)
#define HWY_RVV_FOREACH_16_TRUNC(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, mf2, m1, mf4, -1, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m1, m2, mf2, 0, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m2, m4, m1, 1, /*MLEN=*/8, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m4, m8, m2, 2, /*MLEN=*/4, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m8, __, m4, 3, /*MLEN=*/2, NAME, OP)
#define HWY_RVV_FOREACH_32_TRUNC(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m1, m2, mf2, 0, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m2, m4, m1, 1, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m4, m8, m2, 2, /*MLEN=*/8, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m8, __, m4, 3, /*MLEN=*/4, NAME, OP)
#define HWY_RVV_FOREACH_64_TRUNC(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m2, m4, m1, 1, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m4, m8, m2, 2, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m8, __, m4, 3, /*MLEN=*/8, NAME, OP)
// LMULS = _DEMOTE: can demote from SEW*LMUL to SEWH*LMULH.
#define HWY_RVV_FOREACH_08_DEMOTE(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, mf4, mf2, mf8, -2, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, mf2, m1, mf4, -1, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m1, m2, mf2, 0, /*MLEN=*/8, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m2, m4, m1, 1, /*MLEN=*/4, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m4, m8, m2, 2, /*MLEN=*/2, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m8, __, m4, 3, /*MLEN=*/1, NAME, OP)
#define HWY_RVV_FOREACH_16_DEMOTE(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, mf4, mf2, mf8, -2, /*MLEN=*/64, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, mf2, m1, mf4, -1, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m1, m2, mf2, 0, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m2, m4, m1, 1, /*MLEN=*/8, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m4, m8, m2, 2, /*MLEN=*/4, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m8, __, m4, 3, /*MLEN=*/2, NAME, OP)
#define HWY_RVV_FOREACH_32_DEMOTE(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, mf2, m1, mf4, -1, /*MLEN=*/64, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m1, m2, mf2, 0, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m2, m4, m1, 1, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m4, m8, m2, 2, /*MLEN=*/8, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m8, __, m4, 3, /*MLEN=*/4, NAME, OP)
#define HWY_RVV_FOREACH_64_DEMOTE(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m1, m2, mf2, 0, /*MLEN=*/64, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m2, m4, m1, 1, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m4, m8, m2, 2, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m8, __, m4, 3, /*MLEN=*/8, NAME, OP)
// LMULS = _LE2: <= 2
#define HWY_RVV_FOREACH_08_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, mf8, mf4, __, -3, /*MLEN=*/64, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, mf4, mf2, mf8, -2, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, mf2, m1, mf4, -1, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m1, m2, mf2, 0, /*MLEN=*/8, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m2, m4, m1, 1, /*MLEN=*/4, NAME, OP)
#define HWY_RVV_FOREACH_16_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, mf4, mf2, mf8, -2, /*MLEN=*/64, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, mf2, m1, mf4, -1, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m1, m2, mf2, 0, /*MLEN=*/16, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m2, m4, m1, 1, /*MLEN=*/8, NAME, OP)
#define HWY_RVV_FOREACH_32_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, mf2, m1, mf4, -1, /*MLEN=*/64, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m1, m2, mf2, 0, /*MLEN=*/32, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m2, m4, m1, 1, /*MLEN=*/16, NAME, OP)
#define HWY_RVV_FOREACH_64_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m1, m2, mf2, 0, /*MLEN=*/64, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m2, m4, m1, 1, /*MLEN=*/32, NAME, OP)
// LMULS = _EXT: not the largest LMUL
#define HWY_RVV_FOREACH_08_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m4, m8, m2, 2, /*MLEN=*/2, NAME, OP)
#define HWY_RVV_FOREACH_16_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m4, m8, m2, 2, /*MLEN=*/4, NAME, OP)
#define HWY_RVV_FOREACH_32_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m4, m8, m2, 2, /*MLEN=*/8, NAME, OP)
#define HWY_RVV_FOREACH_64_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m4, m8, m2, 2, /*MLEN=*/16, NAME, OP)
// LMULS = _ALL (2^MinPow2() <= LMUL <= 8)
#define HWY_RVV_FOREACH_08_ALL(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 8, 16, __, m8, __, m4, 3, /*MLEN=*/1, NAME, OP)
#define HWY_RVV_FOREACH_16_ALL(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, m8, __, m4, 3, /*MLEN=*/2, NAME, OP)
#define HWY_RVV_FOREACH_32_ALL(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, m8, __, m4, 3, /*MLEN=*/4, NAME, OP)
#define HWY_RVV_FOREACH_64_ALL(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m8, __, m4, 3, /*MLEN=*/8, NAME, OP)
// 'Virtual' LMUL. This upholds the Highway guarantee that vectors are at least
// 128 bit and LowerHalf is defined whenever there are at least 2 lanes, even
// though RISC-V LMUL must be at least SEW/64 (notice that this rules out
// LMUL=1/2 for SEW=64). To bridge the gap, we add overloads for kPow2 equal to
// one less than should be supported, with all other parameters (vector type
// etc.) unchanged. For D with the lowest kPow2 ('virtual LMUL'), Lanes()
// returns half of what it usually would.
//
// Notice that we can only add overloads whenever there is a D argument: those
// are unique with respect to non-virtual-LMUL overloads because their kPow2
// template argument differs. Otherwise, there is no actual vuint64mf2_t, and
// defining another overload with the same LMUL would be an error. Thus we have
// a separate _VIRT category for HWY_RVV_FOREACH*, and the common case is
// _ALL_VIRT (meaning the regular LMUL plus the VIRT overloads), used in most
// functions that take a D.
#define HWY_RVV_FOREACH_08_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_16_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 16, 32, 8, mf4, mf2, mf8, -3, /*MLEN=*/64, NAME, OP)
#define HWY_RVV_FOREACH_32_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 32, 64, 16, mf2, m1, mf4, -2, /*MLEN=*/64, NAME, OP)
#define HWY_RVV_FOREACH_64_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
X_MACRO(BASE, CHAR, 64, __, 32, m1, m2, mf2, -1, /*MLEN=*/64, NAME, OP)
// ALL + VIRT
#define HWY_RVV_FOREACH_08_ALL_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_ALL(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_16_ALL_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_ALL(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_32_ALL_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_ALL(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_64_ALL_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_ALL(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
// LE2 + VIRT
#define HWY_RVV_FOREACH_08_LE2_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_16_LE2_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_32_LE2_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_64_LE2_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_LE2(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
// EXT + VIRT
#define HWY_RVV_FOREACH_08_EXT_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_16_EXT_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_32_EXT_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_64_EXT_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_EXT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
// DEMOTE + VIRT
#define HWY_RVV_FOREACH_08_DEMOTE_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_DEMOTE(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_08_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_16_DEMOTE_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_DEMOTE(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_16_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_32_DEMOTE_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_DEMOTE(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_32_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
#define HWY_RVV_FOREACH_64_DEMOTE_VIRT(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_DEMOTE(X_MACRO, BASE, CHAR, NAME, OP) \
HWY_RVV_FOREACH_64_VIRT(X_MACRO, BASE, CHAR, NAME, OP)
// SEW for unsigned:
#define HWY_RVV_FOREACH_U08(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_08, LMULS)(X_MACRO, uint, u, NAME, OP)
#define HWY_RVV_FOREACH_U16(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_16, LMULS)(X_MACRO, uint, u, NAME, OP)
#define HWY_RVV_FOREACH_U32(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_32, LMULS)(X_MACRO, uint, u, NAME, OP)
#define HWY_RVV_FOREACH_U64(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_64, LMULS)(X_MACRO, uint, u, NAME, OP)
// SEW for signed:
#define HWY_RVV_FOREACH_I08(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_08, LMULS)(X_MACRO, int, i, NAME, OP)
#define HWY_RVV_FOREACH_I16(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_16, LMULS)(X_MACRO, int, i, NAME, OP)
#define HWY_RVV_FOREACH_I32(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_32, LMULS)(X_MACRO, int, i, NAME, OP)
#define HWY_RVV_FOREACH_I64(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_64, LMULS)(X_MACRO, int, i, NAME, OP)
// SEW for float:
// Used for conversion instructions if HWY_RVV_HAVE_F16C.
#define HWY_RVV_FOREACH_F16_UNCONDITIONAL(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_16, LMULS)(X_MACRO, float, f, NAME, OP)
#if HWY_HAVE_FLOAT16
// Full support for f16 in all ops
#define HWY_RVV_FOREACH_F16(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_F16_UNCONDITIONAL(X_MACRO, NAME, OP, LMULS)
// Only BF16 is emulated.
#define HWY_RVV_IF_EMULATED_D(D) HWY_IF_BF16_D(D)
#define HWY_GENERIC_IF_EMULATED_D(D) HWY_IF_BF16_D(D)
#define HWY_RVV_IF_NOT_EMULATED_D(D) HWY_IF_NOT_BF16_D(D)
#else
#define HWY_RVV_FOREACH_F16(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_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_RVV_IF_NOT_EMULATED_D(D) HWY_IF_NOT_SPECIAL_FLOAT_D(D)
#endif
#define HWY_RVV_FOREACH_F32(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_32, LMULS)(X_MACRO, float, f, NAME, OP)
#define HWY_RVV_FOREACH_F64(X_MACRO, NAME, OP, LMULS) \
HWY_CONCAT(HWY_RVV_FOREACH_64, LMULS)(X_MACRO, float, f, NAME, OP)
// Commonly used type/SEW groups:
#define HWY_RVV_FOREACH_UI08(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U08(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I08(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_UI16(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U16(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I16(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_UI32(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U32(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I32(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_UI64(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U64(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I64(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_UI3264(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_UI32(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_UI64(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_U163264(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U16(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U32(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U64(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_I163264(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I16(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I32(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I64(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_UI163264(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U163264(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I163264(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_F3264(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_F32(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_F64(X_MACRO, NAME, OP, LMULS)
// For all combinations of SEW:
#define HWY_RVV_FOREACH_U(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U08(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U163264(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_I(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I08(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I163264(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH_F(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_F16(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_F3264(X_MACRO, NAME, OP, LMULS)
// Commonly used type categories:
#define HWY_RVV_FOREACH_UI(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_U(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_I(X_MACRO, NAME, OP, LMULS)
#define HWY_RVV_FOREACH(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_UI(X_MACRO, NAME, OP, LMULS) \
HWY_RVV_FOREACH_F(X_MACRO, NAME, OP, LMULS)
// Assemble types for use in x-macros
#define HWY_RVV_T(BASE, SEW) BASE##SEW##_t
#define HWY_RVV_D(BASE, SEW, N, SHIFT) Simd<HWY_RVV_T(BASE, SEW), N, SHIFT>
#define HWY_RVV_V(BASE, SEW, LMUL) v##BASE##SEW##LMUL##_t
#define HWY_RVV_TUP(BASE, SEW, LMUL, TUP) v##BASE##SEW##LMUL##x##TUP##_t
#define HWY_RVV_M(MLEN) vbool##MLEN##_t
} // namespace detail
// Until we have full intrinsic support for fractional LMUL, mixed-precision
// code can use LMUL 1..8 (adequate unless they need many registers).
#define HWY_SPECIALIZE(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, SHIFT, \
MLEN, NAME, OP) \
template <> \
struct DFromV_t<HWY_RVV_V(BASE, SEW, LMUL)> { \
using Lane = HWY_RVV_T(BASE, SEW); \
using type = ScalableTag<Lane, SHIFT>; \
};
HWY_RVV_FOREACH(HWY_SPECIALIZE, _, _, _ALL)
#undef HWY_SPECIALIZE
// ------------------------------ Lanes
// WARNING: we want to query VLMAX/sizeof(T), but this may actually change VL!
#if HWY_COMPILER_GCC && !HWY_IS_DEBUG_BUILD
// HWY_RVV_CAPPED_LANES_SPECIAL_CASES provides some additional optimizations
// to CappedLanes in non-debug builds
#define HWY_RVV_CAPPED_LANES_SPECIAL_CASES(BASE, SEW, LMUL) \
if (__builtin_constant_p(cap >= kMaxLanes) && (cap >= kMaxLanes)) { \
/* If cap is known to be greater than or equal to MaxLanes(d), */ \
/* HWY_MIN(cap, Lanes(d)) will be equal to Lanes(d) */ \
return Lanes(d); \
} \
\
if ((__builtin_constant_p((cap & (cap - 1)) == 0) && \
((cap & (cap - 1)) == 0)) || \
(__builtin_constant_p(cap <= HWY_MAX(kMinLanesPerFullVec, 4)) && \
(cap <= HWY_MAX(kMinLanesPerFullVec, 4)))) { \
/* If cap is known to be a power of 2, then */ \
/* vsetvl(HWY_MIN(cap, kMaxLanes)) is guaranteed to return the same */ \
/* result as HWY_MIN(cap, Lanes(d)) as kMaxLanes is a power of 2 and */ \
/* as (cap > VLMAX && cap < 2 * VLMAX) can only be true if cap is not a */ \
/* power of 2 since VLMAX is always a power of 2 */ \
\
/* If cap is known to be less than or equal to 4, then */ \
/* vsetvl(HWY_MIN(cap, kMaxLanes)) is guaranteed to return the same */ \
/* result as HWY_MIN(cap, Lanes(d)) as HWY_MIN(cap, kMaxLanes) <= 4 is */ \
/* true if cap <= 4 and as vsetvl(HWY_MIN(cap, kMaxLanes)) is */ \
/* guaranteed to return the same result as HWY_MIN(cap, Lanes(d)) */ \
/* if HWY_MIN(cap, kMaxLanes) <= 4 is true */ \
\
/* If cap is known to be less than or equal to kMinLanesPerFullVec, */ \
/* then vsetvl(HWY_MIN(cap, kMaxLanes)) is guaranteed to return the */ \
/* same result as HWY_MIN(cap, Lanes(d)) as */ \
/* HWY_MIN(cap, kMaxLanes) <= kMinLanesPerFullVec is true if */ \
/* cap <= kMinLanesPerFullVec is true */ \
\
/* If cap <= HWY_MAX(kMinLanesPerFullVec, 4) is true, then either */ \
/* cap <= 4 or cap <= kMinLanesPerFullVec must be true */ \
\
/* If cap <= HWY_MAX(kMinLanesPerFullVec, 4) is known to be true, */ \
/* then vsetvl(HWY_MIN(cap, kMaxLanes)) is guaranteed to return the */ \
/* same result as HWY_MIN(cap, Lanes(d)) */ \
\
/* If no cap, avoid the HWY_MIN. */ \
return detail::IsFull(d) \
? __riscv_vsetvl_e##SEW##LMUL(cap) \
: __riscv_vsetvl_e##SEW##LMUL(HWY_MIN(cap, kMaxLanes)); \
}
#else
#define HWY_RVV_CAPPED_LANES_SPECIAL_CASES(BASE, SEW, LMUL)
#endif
#define HWY_RVV_LANES(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, SHIFT, \
MLEN, NAME, OP) \
template <size_t N> \
HWY_API size_t NAME(HWY_RVV_D(BASE, SEW, N, SHIFT) d) { \
constexpr size_t kFull = HWY_LANES(HWY_RVV_T(BASE, SEW)); \
constexpr size_t kCap = MaxLanes(d); \
/* If no cap, avoid generating a constant by using VLMAX. */ \
return N == kFull ? __riscv_vsetvlmax_e##SEW##LMUL() \
: __riscv_vsetvl_e##SEW##LMUL(kCap); \
} \
template <size_t N> \
HWY_API size_t Capped##NAME(HWY_RVV_D(BASE, SEW, N, SHIFT) d, size_t cap) { \
/* NOTE: Section 6.3 of the RVV specification, which can be found at */ \
/* https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc, */ \
/* allows vsetvl to return a result less than Lanes(d) but greater than */ \
/* or equal to ((cap + 1) / 2) if */ \
/* (Lanes(d) > 2 && cap > HWY_MAX(Lanes(d), 4) && cap < (2 * Lanes(d))) */ \
/* is true */ \
\
/* VLMAX is the number of lanes in a vector of type */ \
/* VFromD<decltype(d)>, which is returned by */ \
/* Lanes(DFromV<VFromD<decltype(d)>>()) */ \
\
/* VLMAX is guaranteed to be a power of 2 under Section 2 of the RVV */ \
/* specification */ \
\
/* The VLMAX of a vector of type VFromD<decltype(d)> is at least 2 as */ \
/* the HWY_RVV target requires support for the RVV Zvl128b extension, */ \
/* which guarantees that vectors with LMUL=1 are at least 16 bytes */ \
\
/* If VLMAX == 2 is true, then vsetvl(cap) is equal to HWY_MIN(cap, 2) */ \
/* as cap == 3 is the only value such that */ \
/* (cap > VLMAX && cap < 2 * VLMAX) if VLMAX == 2 and as */ \
/* ((3 + 1) / 2) is equal to 2 */ \
\
/* If cap <= 4 is true, then vsetvl(cap) must be equal to */ \
/* HWY_MIN(cap, VLMAX) as cap <= VLMAX is true if VLMAX >= 4 is true */ \
/* and as vsetvl(cap) is guaranteed to be equal to HWY_MIN(cap, VLMAX) */ \
/* if VLMAX == 2 */ \
\
/* We want CappedLanes(d, cap) to return Lanes(d) if cap > Lanes(d) as */ \
/* LoadN(d, p, cap) expects to load exactly HWY_MIN(cap, Lanes(d)) */ \
/* lanes and StoreN(v, d, p, cap) expects to store exactly */ \
/* HWY_MIN(cap, Lanes(d)) lanes, even in the case where vsetvl returns */ \
/* a result that is less than HWY_MIN(cap, Lanes(d)) */ \
\
/* kMinLanesPerFullVec is the minimum value of VLMAX for a vector of */ \
/* type VFromD<decltype(d)> */ \
constexpr size_t kMinLanesPerFullVec = \
detail::ScaleByPower(16 / (SEW / 8), SHIFT); \
/* kMaxLanes is the maximum number of lanes returned by Lanes(d) */ \
constexpr size_t kMaxLanes = MaxLanes(d); \
\
HWY_RVV_CAPPED_LANES_SPECIAL_CASES(BASE, SEW, LMUL) \
\
if (kMaxLanes <= HWY_MAX(kMinLanesPerFullVec, 4)) { \
/* If kMaxLanes <= kMinLanesPerFullVec is true, then */ \
/* vsetvl(HWY_MIN(cap, kMaxLanes)) is guaranteed to return */ \
/* HWY_MIN(cap, Lanes(d)) as */ \
/* HWY_MIN(cap, kMaxLanes) <= kMaxLanes <= VLMAX is true if */ \
/* kMaxLanes <= kMinLanesPerFullVec is true */ \
\
/* If kMaxLanes <= 4 is true, then vsetvl(HWY_MIN(cap, kMaxLanes)) is */ \
/* guaranteed to return the same result as HWY_MIN(cap, Lanes(d)) as */ \
/* HWY_MIN(cap, kMaxLanes) <= 4 is true if kMaxLanes <= 4 is true */ \
\
/* If kMaxLanes <= HWY_MAX(kMinLanesPerFullVec, 4) is true, then */ \
/* either kMaxLanes <= 4 or kMaxLanes <= kMinLanesPerFullVec must be */ \
/* true */ \
\
return __riscv_vsetvl_e##SEW##LMUL(HWY_MIN(cap, kMaxLanes)); \
} else { \
/* If kMaxLanes > HWY_MAX(kMinLanesPerFullVec, 4) is true, need to */ \
/* obtain the actual number of lanes using Lanes(d) and clamp cap to */ \
/* the result of Lanes(d) */ \
const size_t actual = Lanes(d); \
return HWY_MIN(actual, cap); \
} \
}
#define HWY_RVV_LANES_VIRT(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <size_t N> \
HWY_API size_t NAME(HWY_RVV_D(BASE, SEW, N, SHIFT) d) { \
constexpr size_t kCap = MaxLanes(d); \
/* In case of virtual LMUL (intrinsics do not provide "uint16mf8_t") */ \
/* vsetvl may or may not be correct, so do it ourselves. */ \
const size_t actual = \
detail::ScaleByPower(__riscv_vlenb() / (SEW / 8), SHIFT); \
return HWY_MIN(actual, kCap); \
} \
template <size_t N> \
HWY_API size_t Capped##NAME(HWY_RVV_D(BASE, SEW, N, SHIFT) d, size_t cap) { \
/* In case of virtual LMUL (intrinsics do not provide "uint16mf8_t") */ \
/* vsetvl may or may not be correct, so do it ourselves. */ \
const size_t actual = \
detail::ScaleByPower(__riscv_vlenb() / (SEW / 8), SHIFT); \
/* If no cap, avoid an extra HWY_MIN. */ \
return detail::IsFull(d) ? HWY_MIN(actual, cap) \
: HWY_MIN(HWY_MIN(actual, cap), MaxLanes(d)); \
}
HWY_RVV_FOREACH(HWY_RVV_LANES, Lanes, setvlmax_e, _ALL)
HWY_RVV_FOREACH(HWY_RVV_LANES_VIRT, Lanes, lenb, _VIRT)
#undef HWY_RVV_LANES
#undef HWY_RVV_LANES_VIRT
#undef HWY_RVV_CAPPED_LANES_SPECIAL_CASES
template <class D, HWY_RVV_IF_EMULATED_D(D)>
HWY_API size_t Lanes(D /* tag*/) {
return Lanes(RebindToUnsigned<D>());
}
template <class D, HWY_RVV_IF_EMULATED_D(D)>
HWY_API size_t CappedLanes(D /* tag*/, size_t cap) {
return CappedLanes(RebindToUnsigned<D>(), cap);
}
// ------------------------------ Common x-macros
// Last argument to most intrinsics. Use when the op has no d arg of its own,
// which means there is no user-specified cap.
#define HWY_RVV_AVL(SEW, SHIFT) \
Lanes(ScalableTag<HWY_RVV_T(uint, SEW), SHIFT>())
// vector = f(vector), e.g. Not
#define HWY_RVV_RETV_ARGV(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) NAME(HWY_RVV_V(BASE, SEW, LMUL) v) { \
return __riscv_v##OP##_v_##CHAR##SEW##LMUL(v, HWY_RVV_AVL(SEW, SHIFT)); \
}
// vector = f(vector, scalar), e.g. detail::AddS
#define HWY_RVV_RETV_ARGVS(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) \
NAME(HWY_RVV_V(BASE, SEW, LMUL) a, HWY_RVV_T(BASE, SEW) b) { \
return __riscv_v##OP##_##CHAR##SEW##LMUL(a, b, HWY_RVV_AVL(SEW, SHIFT)); \
}
// vector = f(vector, vector), e.g. Add
#define HWY_RVV_RETV_ARGVV(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) \
NAME(HWY_RVV_V(BASE, SEW, LMUL) a, HWY_RVV_V(BASE, SEW, LMUL) b) { \
return __riscv_v##OP##_vv_##CHAR##SEW##LMUL(a, b, \
HWY_RVV_AVL(SEW, SHIFT)); \
}
// vector = f(vector, mask, vector, vector), e.g. MaskedAddOr
#define HWY_RVV_RETV_ARGMVV(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) \
NAME(HWY_RVV_V(BASE, SEW, LMUL) no, HWY_RVV_M(MLEN) m, \
HWY_RVV_V(BASE, SEW, LMUL) a, HWY_RVV_V(BASE, SEW, LMUL) b) { \
return __riscv_v##OP##_vv_##CHAR##SEW##LMUL##_mu(m, no, a, b, \
HWY_RVV_AVL(SEW, SHIFT)); \
}
// mask = f(mask)
#define HWY_RVV_RETM_ARGM(SEW, SHIFT, MLEN, NAME, OP) \
HWY_API HWY_RVV_M(MLEN) NAME(HWY_RVV_M(MLEN) m) { \
return __riscv_vm##OP##_m_b##MLEN(m, HWY_RVV_AVL(SEW, SHIFT)); \
}
// ================================================== INIT
// ------------------------------ Set
#define HWY_RVV_SET(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, SHIFT, \
MLEN, NAME, OP) \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) \
NAME(HWY_RVV_D(BASE, SEW, N, SHIFT) d, HWY_RVV_T(BASE, SEW) arg) { \
return __riscv_v##OP##_##CHAR##SEW##LMUL(arg, Lanes(d)); \
}
HWY_RVV_FOREACH_UI(HWY_RVV_SET, Set, mv_v_x, _ALL_VIRT)
HWY_RVV_FOREACH_F(HWY_RVV_SET, Set, fmv_v_f, _ALL_VIRT)
#undef HWY_RVV_SET
// Treat bfloat16_t as int16_t (using the previously defined Set overloads);
// required for Zero and VFromD.
template <class D, HWY_IF_BF16_D(D)>
decltype(Set(RebindToSigned<D>(), 0)) Set(D d, hwy::bfloat16_t arg) {
return Set(RebindToSigned<decltype(d)>(), BitCastScalar<int16_t>(arg));
}
#if !HWY_HAVE_FLOAT16 // Otherwise already defined above.
// WARNING: returns a different type than emulated bfloat16_t so that we can
// implement PromoteTo overloads for both bfloat16_t and float16_t, and also
// provide a Neg(hwy::float16_t) overload that coexists with Neg(int16_t).
template <class D, HWY_IF_F16_D(D)>
decltype(Set(RebindToUnsigned<D>(), 0)) Set(D d, hwy::float16_t arg) {
return Set(RebindToUnsigned<decltype(d)>(), BitCastScalar<uint16_t>(arg));
}
#endif
template <class D>
using VFromD = decltype(Set(D(), TFromD<D>()));
// ------------------------------ Zero
template <class D>
HWY_API VFromD<D> Zero(D d) {
// Cast to support bfloat16_t.
const RebindToUnsigned<decltype(d)> du;
return BitCast(d, Set(du, 0));
}
// ------------------------------ Undefined
// RVV vundefined is 'poisoned' such that even XORing a _variable_ initialized
// by it gives unpredictable results. It should only be used for maskoff, so
// keep it internal. For the Highway op, just use Zero (single instruction).
namespace detail {
#define HWY_RVV_UNDEFINED(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) \
NAME(HWY_RVV_D(BASE, SEW, N, SHIFT) /* tag */) { \
return __riscv_v##OP##_##CHAR##SEW##LMUL(); /* no AVL */ \
}
HWY_RVV_FOREACH(HWY_RVV_UNDEFINED, Undefined, undefined, _ALL)
#undef HWY_RVV_UNDEFINED
} // namespace detail
template <class D>
HWY_API VFromD<D> Undefined(D d) {
return Zero(d);
}
// ------------------------------ BitCast
namespace detail {
// Halves LMUL. (Use LMUL arg for the source so we can use _TRUNC.)
#define HWY_RVV_TRUNC(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, SHIFT, \
MLEN, NAME, OP) \
HWY_API HWY_RVV_V(BASE, SEW, LMULH) NAME(HWY_RVV_V(BASE, SEW, LMUL) v) { \
return __riscv_v##OP##_v_##CHAR##SEW##LMUL##_##CHAR##SEW##LMULH( \
v); /* no AVL */ \
}
HWY_RVV_FOREACH(HWY_RVV_TRUNC, Trunc, lmul_trunc, _TRUNC)
#undef HWY_RVV_TRUNC
// Doubles LMUL to `d2` (the arg is only necessary for _VIRT).
#define HWY_RVV_EXT(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, SHIFT, \
MLEN, NAME, OP) \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMULD) \
NAME(HWY_RVV_D(BASE, SEW, N, SHIFT + 1) /* d2 */, \
HWY_RVV_V(BASE, SEW, LMUL) v) { \
return __riscv_v##OP##_v_##CHAR##SEW##LMUL##_##CHAR##SEW##LMULD( \
v); /* no AVL */ \
}
HWY_RVV_FOREACH(HWY_RVV_EXT, Ext, lmul_ext, _EXT)
#undef HWY_RVV_EXT
// For virtual LMUL e.g. 'uint32mf4_t', the return type should be mf2, which is
// the same as the actual input type.
#define HWY_RVV_EXT_VIRT(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) \
NAME(HWY_RVV_D(BASE, SEW, N, SHIFT + 1) /* d2 */, \
HWY_RVV_V(BASE, SEW, LMUL) v) { \
return v; \
}
HWY_RVV_FOREACH(HWY_RVV_EXT_VIRT, Ext, lmul_ext, _VIRT)
#undef HWY_RVV_EXT_VIRT
template <class D, HWY_RVV_IF_EMULATED_D(D)>
VFromD<D> Ext(D d, VFromD<Half<D>> v) {
const RebindToUnsigned<decltype(d)> du;
const Half<decltype(du)> duh;
return BitCast(d, Ext(du, BitCast(duh, v)));
}
// For BitCastToByte, the D arg is only to prevent duplicate definitions caused
// by _ALL_VIRT.
// There is no reinterpret from u8 <-> u8, so just return.
#define HWY_RVV_CAST_U8(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <typename T, size_t N> \
HWY_API vuint8##LMUL##_t BitCastToByte(Simd<T, N, SHIFT> /* d */, \
vuint8##LMUL##_t v) { \
return v; \
} \
template <size_t N> \
HWY_API vuint8##LMUL##_t BitCastFromByte( \
HWY_RVV_D(BASE, SEW, N, SHIFT) /* d */, vuint8##LMUL##_t v) { \
return v; \
}
// For i8, need a single reinterpret (HWY_RVV_CAST_IF does two).
#define HWY_RVV_CAST_I8(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <typename T, size_t N> \
HWY_API vuint8##LMUL##_t BitCastToByte(Simd<T, N, SHIFT> /* d */, \
vint8##LMUL##_t v) { \
return __riscv_vreinterpret_v_i8##LMUL##_u8##LMUL(v); \
} \
template <size_t N> \
HWY_API vint8##LMUL##_t BitCastFromByte( \
HWY_RVV_D(BASE, SEW, N, SHIFT) /* d */, vuint8##LMUL##_t v) { \
return __riscv_vreinterpret_v_u8##LMUL##_i8##LMUL(v); \
}
// Separate u/i because clang only provides signed <-> unsigned reinterpret for
// the same SEW.
#define HWY_RVV_CAST_U(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, SHIFT, \
MLEN, NAME, OP) \
template <typename T, size_t N> \
HWY_API vuint8##LMUL##_t BitCastToByte(Simd<T, N, SHIFT> /* d */, \
HWY_RVV_V(BASE, SEW, LMUL) v) { \
return __riscv_v##OP##_v_##CHAR##SEW##LMUL##_u8##LMUL(v); \
} \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) BitCastFromByte( \
HWY_RVV_D(BASE, SEW, N, SHIFT) /* d */, vuint8##LMUL##_t v) { \
return __riscv_v##OP##_v_u8##LMUL##_##CHAR##SEW##LMUL(v); \
}
// Signed/Float: first cast to/from unsigned
#define HWY_RVV_CAST_IF(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <typename T, size_t N> \
HWY_API vuint8##LMUL##_t BitCastToByte(Simd<T, N, SHIFT> /* d */, \
HWY_RVV_V(BASE, SEW, LMUL) v) { \
return __riscv_v##OP##_v_u##SEW##LMUL##_u8##LMUL( \
__riscv_v##OP##_v_##CHAR##SEW##LMUL##_u##SEW##LMUL(v)); \
} \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) BitCastFromByte( \
HWY_RVV_D(BASE, SEW, N, SHIFT) /* d */, vuint8##LMUL##_t v) { \
return __riscv_v##OP##_v_u##SEW##LMUL##_##CHAR##SEW##LMUL( \
__riscv_v##OP##_v_u8##LMUL##_u##SEW##LMUL(v)); \
}
// Additional versions for virtual LMUL using LMULH for byte vectors.
#define HWY_RVV_CAST_VIRT_U(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <typename T, size_t N> \
HWY_API vuint8##LMULH##_t BitCastToByte(Simd<T, N, SHIFT> /* d */, \
HWY_RVV_V(BASE, SEW, LMUL) v) { \
return detail::Trunc(__riscv_v##OP##_v_##CHAR##SEW##LMUL##_u8##LMUL(v)); \
} \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) BitCastFromByte( \
HWY_RVV_D(BASE, SEW, N, SHIFT) /* d */, vuint8##LMULH##_t v) { \
HWY_RVV_D(uint, 8, N, SHIFT + 1) d2; \
const vuint8##LMUL##_t v2 = detail::Ext(d2, v); \
return __riscv_v##OP##_v_u8##LMUL##_##CHAR##SEW##LMUL(v2); \
}
// Signed/Float: first cast to/from unsigned
#define HWY_RVV_CAST_VIRT_IF(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <typename T, size_t N> \
HWY_API vuint8##LMULH##_t BitCastToByte(Simd<T, N, SHIFT> /* d */, \
HWY_RVV_V(BASE, SEW, LMUL) v) { \
return detail::Trunc(__riscv_v##OP##_v_u##SEW##LMUL##_u8##LMUL( \
__riscv_v##OP##_v_##CHAR##SEW##LMUL##_u##SEW##LMUL(v))); \
} \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) BitCastFromByte( \
HWY_RVV_D(BASE, SEW, N, SHIFT) /* d */, vuint8##LMULH##_t v) { \
HWY_RVV_D(uint, 8, N, SHIFT + 1) d2; \
const vuint8##LMUL##_t v2 = detail::Ext(d2, v); \
return __riscv_v##OP##_v_u##SEW##LMUL##_##CHAR##SEW##LMUL( \
__riscv_v##OP##_v_u8##LMUL##_u##SEW##LMUL(v2)); \
}
HWY_RVV_FOREACH_U08(HWY_RVV_CAST_U8, _, reinterpret, _ALL)
HWY_RVV_FOREACH_I08(HWY_RVV_CAST_I8, _, reinterpret, _ALL)
HWY_RVV_FOREACH_U163264(HWY_RVV_CAST_U, _, reinterpret, _ALL)
HWY_RVV_FOREACH_I163264(HWY_RVV_CAST_IF, _, reinterpret, _ALL)
HWY_RVV_FOREACH_U163264(HWY_RVV_CAST_VIRT_U, _, reinterpret, _VIRT)
HWY_RVV_FOREACH_I163264(HWY_RVV_CAST_VIRT_IF, _, reinterpret, _VIRT)
HWY_RVV_FOREACH_F(HWY_RVV_CAST_IF, _, reinterpret, _ALL)
HWY_RVV_FOREACH_F(HWY_RVV_CAST_VIRT_IF, _, reinterpret, _VIRT)
#if HWY_HAVE_FLOAT16 // HWY_RVV_FOREACH_F already covered float16_
#elif HWY_RVV_HAVE_F16C // zvfhmin provides reinterpret* intrinsics:
HWY_RVV_FOREACH_F16_UNCONDITIONAL(HWY_RVV_CAST_IF, _, reinterpret, _ALL)
HWY_RVV_FOREACH_F16_UNCONDITIONAL(HWY_RVV_CAST_VIRT_IF, _, reinterpret, _VIRT)
#else
template <class D, HWY_IF_F16_D(D)>
HWY_INLINE VFromD<RebindToUnsigned<D>> BitCastFromByte(
D /* d */, VFromD<Repartition<uint8_t, D>> v) {
return BitCastFromByte(RebindToUnsigned<D>(), v);
}
#endif
#undef HWY_RVV_CAST_U8
#undef HWY_RVV_CAST_I8
#undef HWY_RVV_CAST_U
#undef HWY_RVV_CAST_IF
#undef HWY_RVV_CAST_VIRT_U
#undef HWY_RVV_CAST_VIRT_IF
template <class D, HWY_IF_BF16_D(D)>
HWY_INLINE VFromD<RebindToSigned<D>> BitCastFromByte(
D d, VFromD<Repartition<uint8_t, D>> v) {
return BitCastFromByte(RebindToSigned<decltype(d)>(), v);
}
} // namespace detail
template <class D, class FromV>
HWY_API VFromD<D> BitCast(D d, FromV v) {
return detail::BitCastFromByte(d, detail::BitCastToByte(d, v));
}
// ------------------------------ Iota
namespace detail {
#define HWY_RVV_IOTA(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, SHIFT, \
MLEN, NAME, OP) \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) NAME(HWY_RVV_D(BASE, SEW, N, SHIFT) d) { \
return __riscv_v##OP##_##CHAR##SEW##LMUL(Lanes(d)); \
}
// For i8 lanes, this may well wrap around. Unsigned only is less error-prone.
HWY_RVV_FOREACH_U(HWY_RVV_IOTA, Iota0, id_v, _ALL_VIRT)
#undef HWY_RVV_IOTA
// Used by Expand.
#define HWY_RVV_MASKED_IOTA(BASE, CHAR, SEW, SEWD, SEWH, LMUL, LMULD, LMULH, \
SHIFT, MLEN, NAME, OP) \
template <size_t N> \
HWY_API HWY_RVV_V(BASE, SEW, LMUL) \
NAME(HWY_RVV_D(BASE, SEW, N, SHIFT) d, HWY_RVV_M(MLEN) mask) { \
return __riscv_v##OP##_##CHAR##SEW##LMUL(mask, Lanes(d)); \
}
HWY_RVV_FOREACH_U(HWY_RVV_MASKED_IOTA, MaskedIota, iota_m, _ALL_VIRT)
#undef HWY_RVV_MASKED_IOTA
} // namespace detail
// ================================================== LOGICAL
// ------------------------------ Not
HWY_RVV_FOREACH_UI(HWY_RVV_RETV_ARGV, Not, not, _ALL)
template <class V, HWY_IF_FLOAT_V(V)>
HWY_API V Not(const V v) {
using DF = DFromV<V>;
using DU = RebindToUnsigned<DF>;
return BitCast(DF(), Not(BitCast(DU(), v)));
}
// ------------------------------ And
// Non-vector version (ideally immediate) for use with Iota0
namespace detail {
HWY_RVV_FOREACH_UI(HWY_RVV_RETV_ARGVS, AndS, and_vx, _ALL)
} // namespace detail
HWY_RVV_FOREACH_UI(HWY_RVV_RETV_ARGVV, And, and, _ALL)
template <class V, HWY_IF_FLOAT_V(V)>
HWY_API V And(const V a, const V b) {
using DF = DFromV<V>;
using DU = RebindToUnsigned<DF>;
return BitCast(DF(), And(BitCast(DU(), a), BitCast(DU(), b)));
}
// ------------------------------ Or
HWY_RVV_FOREACH_UI(HWY_RVV_RETV_ARGVV, Or, or, _ALL)
template <class V, HWY_IF_FLOAT_V(V)>
HWY_API V Or(const V a, const V b) {
using DF = DFromV<V>;
using DU = RebindToUnsigned<DF>;
return BitCast(DF(), Or(BitCast(DU(), a), BitCast(DU(), b)));
}
// ------------------------------ Xor
// Non-vector version (ideally immediate) for use with Iota0
namespace detail {
HWY_RVV_FOREACH_UI(HWY_RVV_RETV_ARGVS, XorS, xor_vx, _ALL)
} // namespace detail
HWY_RVV_FOREACH_UI(HWY_RVV_RETV_ARGVV, Xor, xor, _ALL)
template <class V, HWY_IF_FLOAT_V(V)>
HWY_API V Xor(const V a, const V b) {
using DF = DFromV<V>;
using DU = RebindToUnsigned<DF>;
return BitCast(DF(), Xor(BitCast(DU(), a), BitCast(DU(), b)));
}
// ------------------------------ AndNot
template <class V>
HWY_API V AndNot(const V not_a, const V b) {
return And(Not(not_a), b);
}
// ------------------------------ Xor3
template <class V>