-
Notifications
You must be signed in to change notification settings - Fork 148
/
vector_convert.h
828 lines (692 loc) · 27.1 KB
/
vector_convert.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
/************************** vector_convert.h *******************************
* Author: Agner Fog
* Date created: 2014-07-23
* Last modified: 2022-07-20
* Version: 2.02.00
* Project: vector class library
* Description:
* Header file for conversion between different vector classes with different
* sizes. Also includes verious generic template functions.
*
* (c) Copyright 2012-2022 Agner Fog.
* Apache License version 2.0 or later.
*****************************************************************************/
#ifndef VECTOR_CONVERT_H
#define VECTOR_CONVERT_H
#ifndef VECTORCLASS_H
#include "vectorclass.h"
#endif
#if VECTORCLASS_H < 20200
#error Incompatible versions of vector class library mixed
#endif
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
#if MAX_VECTOR_SIZE >= 256
/*****************************************************************************
*
* Extend from 128 to 256 bit vectors
*
*****************************************************************************/
#if INSTRSET >= 8 // AVX2. 256 bit integer vectors
// sign extend
static inline Vec16s extend (Vec16c const a) {
return _mm256_cvtepi8_epi16(a);
}
// zero extend
static inline Vec16us extend (Vec16uc const a) {
return _mm256_cvtepu8_epi16(a);
}
// sign extend
static inline Vec8i extend (Vec8s const a) {
return _mm256_cvtepi16_epi32(a);
}
// zero extend
static inline Vec8ui extend (Vec8us const a) {
return _mm256_cvtepu16_epi32(a);
}
// sign extend
static inline Vec4q extend (Vec4i const a) {
return _mm256_cvtepi32_epi64(a);
}
// zero extend
static inline Vec4uq extend (Vec4ui const a) {
return _mm256_cvtepu32_epi64(a);
}
#else // no AVX2. 256 bit integer vectors are emulated
// sign extend and zero extend functions:
static inline Vec16s extend (Vec16c const a) {
return Vec16s(extend_low(a), extend_high(a));
}
static inline Vec16us extend (Vec16uc const a) {
return Vec16us(extend_low(a), extend_high(a));
}
static inline Vec8i extend (Vec8s const a) {
return Vec8i(extend_low(a), extend_high(a));
}
static inline Vec8ui extend (Vec8us const a) {
return Vec8ui(extend_low(a), extend_high(a));
}
static inline Vec4q extend (Vec4i const a) {
return Vec4q(extend_low(a), extend_high(a));
}
static inline Vec4uq extend (Vec4ui const a) {
return Vec4uq(extend_low(a), extend_high(a));
}
#endif // AVX2
/*****************************************************************************
*
* Conversions between float and double
*
*****************************************************************************/
#if INSTRSET >= 7 // AVX. 256 bit float vectors
// float to double
static inline Vec4d to_double (Vec4f const a) {
return _mm256_cvtps_pd(a);
}
// double to float
static inline Vec4f to_float (Vec4d const a) {
return _mm256_cvtpd_ps(a);
}
#else // no AVX2. 256 bit float vectors are emulated
// float to double
static inline Vec4d to_double (Vec4f const a) {
Vec2d lo = _mm_cvtps_pd(a);
Vec2d hi = _mm_cvtps_pd(_mm_movehl_ps(a, a));
return Vec4d(lo,hi);
}
// double to float
static inline Vec4f to_float (Vec4d const a) {
Vec4f lo = _mm_cvtpd_ps(a.get_low());
Vec4f hi = _mm_cvtpd_ps(a.get_high());
return _mm_movelh_ps(lo, hi);
}
#endif
/*****************************************************************************
*
* Reduce from 256 to 128 bit vectors
*
*****************************************************************************/
#if INSTRSET >= 10 // AVX512VL
// compress functions. overflow wraps around
static inline Vec16c compress (Vec16s const a) {
return _mm256_cvtepi16_epi8(a);
}
static inline Vec16uc compress (Vec16us const a) {
return _mm256_cvtepi16_epi8(a);
}
static inline Vec8s compress (Vec8i const a) {
return _mm256_cvtepi32_epi16(a);
}
static inline Vec8us compress (Vec8ui const a) {
return _mm256_cvtepi32_epi16(a);
}
static inline Vec4i compress (Vec4q const a) {
return _mm256_cvtepi64_epi32(a);
}
static inline Vec4ui compress (Vec4uq const a) {
return _mm256_cvtepi64_epi32(a);
}
// compress_saturated functions. overflow saturates
static inline Vec16c compress_saturated (Vec16s const a) {
return _mm256_cvtsepi16_epi8(a);
}
static inline Vec16uc compress_saturated (Vec16us const a) {
return _mm256_cvtusepi16_epi8(a);
}
static inline Vec8s compress_saturated (Vec8i const a) {
return _mm256_cvtsepi32_epi16(a);
}
static inline Vec8us compress_saturated (Vec8ui const a) {
return _mm256_cvtusepi32_epi16(a);
}
static inline Vec4i compress_saturated (Vec4q const a) {
return _mm256_cvtsepi64_epi32(a);
}
static inline Vec4ui compress_saturated (Vec4uq const a) {
return _mm256_cvtusepi64_epi32(a);
}
#else // no AVX512
// compress functions. overflow wraps around
static inline Vec16c compress (Vec16s const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec16uc compress (Vec16us const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec8s compress (Vec8i const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec8us compress (Vec8ui const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec4i compress (Vec4q const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec4ui compress (Vec4uq const a) {
return compress(a.get_low(), a.get_high());
}
// compress_saturated functions. overflow saturates
static inline Vec16c compress_saturated (Vec16s const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec16uc compress_saturated (Vec16us const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec8s compress_saturated (Vec8i const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec8us compress_saturated (Vec8ui const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec4i compress_saturated (Vec4q const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec4ui compress_saturated (Vec4uq const a) {
return compress_saturated(a.get_low(), a.get_high());
}
#endif // AVX512
#endif // MAX_VECTOR_SIZE >= 256
#if MAX_VECTOR_SIZE >= 512
/*****************************************************************************
*
* Reduce from 512 to 256 bit vectors
*
*****************************************************************************/
#if INSTRSET >= 10 // AVX512VL
// compress_saturated functions. overflow saturates
static inline Vec32c compress_saturated (Vec32s const a) {
return _mm512_cvtsepi16_epi8(a);
}
static inline Vec32uc compress_saturated (Vec32us const a) {
return _mm512_cvtusepi16_epi8(a);
}
static inline Vec16s compress_saturated (Vec16i const a) {
return _mm512_cvtsepi32_epi16(a);
}
static inline Vec16us compress_saturated (Vec16ui const a) {
return _mm512_cvtusepi32_epi16(a);
}
static inline Vec8i compress_saturated (Vec8q const a) {
return _mm512_cvtsepi64_epi32(a);
}
static inline Vec8ui compress_saturated (Vec8uq const a) {
return _mm512_cvtusepi64_epi32(a);
}
#else // no AVX512
// compress_saturated functions. overflow saturates
static inline Vec32c compress_saturated (Vec32s const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec32uc compress_saturated (Vec32us const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec16s compress_saturated (Vec16i const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec16us compress_saturated (Vec16ui const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec8i compress_saturated (Vec8q const a) {
return compress_saturated(a.get_low(), a.get_high());
}
static inline Vec8ui compress_saturated (Vec8uq const a) {
return compress_saturated(a.get_low(), a.get_high());
}
#endif // AVX512
/*****************************************************************************
*
* Extend from 256 to 512 bit vectors
*
*****************************************************************************/
#if INSTRSET >= 9 // AVX512. 512 bit integer vectors
// sign extend
static inline Vec32s extend (Vec32c const a) {
#if INSTRSET >= 10
return _mm512_cvtepi8_epi16(a);
#else
return Vec32s(extend_low(a), extend_high(a));
#endif
}
// zero extend
static inline Vec32us extend (Vec32uc const a) {
#if INSTRSET >= 10
return _mm512_cvtepu8_epi16(a);
#else
return Vec32us(extend_low(a), extend_high(a));
#endif
}
// sign extend
static inline Vec16i extend (Vec16s const a) {
return _mm512_cvtepi16_epi32(a);
}
// zero extend
static inline Vec16ui extend (Vec16us const a) {
return _mm512_cvtepu16_epi32(a);
}
// sign extend
static inline Vec8q extend (Vec8i const a) {
return _mm512_cvtepi32_epi64(a);
}
// zero extend
static inline Vec8uq extend (Vec8ui const a) {
return _mm512_cvtepu32_epi64(a);
}
#else // no AVX512. 512 bit vectors are emulated
// sign extend
static inline Vec32s extend (Vec32c const a) {
return Vec32s(extend_low(a), extend_high(a));
}
// zero extend
static inline Vec32us extend (Vec32uc const a) {
return Vec32us(extend_low(a), extend_high(a));
}
// sign extend
static inline Vec16i extend (Vec16s const a) {
return Vec16i(extend_low(a), extend_high(a));
}
// zero extend
static inline Vec16ui extend (Vec16us const a) {
return Vec16ui(extend_low(a), extend_high(a));
}
// sign extend
static inline Vec8q extend (Vec8i const a) {
return Vec8q(extend_low(a), extend_high(a));
}
// zero extend
static inline Vec8uq extend (Vec8ui const a) {
return Vec8uq(extend_low(a), extend_high(a));
}
#endif // AVX512
/*****************************************************************************
*
* Reduce from 512 to 256 bit vectors
*
*****************************************************************************/
#if INSTRSET >= 9 // AVX512F
// compress functions. overflow wraps around
static inline Vec32c compress (Vec32s const a) {
#if INSTRSET >= 10 // AVVX512BW
return _mm512_cvtepi16_epi8(a);
#else
return compress(a.get_low(), a.get_high());
#endif
}
static inline Vec32uc compress (Vec32us const a) {
return Vec32uc(compress(Vec32s(a)));
}
static inline Vec16s compress (Vec16i const a) {
return _mm512_cvtepi32_epi16(a);
}
static inline Vec16us compress (Vec16ui const a) {
return _mm512_cvtepi32_epi16(a);
}
static inline Vec8i compress (Vec8q const a) {
return _mm512_cvtepi64_epi32(a);
}
static inline Vec8ui compress (Vec8uq const a) {
return _mm512_cvtepi64_epi32(a);
}
#else // no AVX512
// compress functions. overflow wraps around
static inline Vec32c compress (Vec32s const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec32uc compress (Vec32us const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec16s compress (Vec16i const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec16us compress (Vec16ui const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec8i compress (Vec8q const a) {
return compress(a.get_low(), a.get_high());
}
static inline Vec8ui compress (Vec8uq const a) {
return compress(a.get_low(), a.get_high());
}
#endif // AVX512
/*****************************************************************************
*
* Conversions between float and double
*
*****************************************************************************/
#if INSTRSET >= 9 // AVX512. 512 bit float vectors
// float to double
static inline Vec8d to_double (Vec8f const a) {
return _mm512_cvtps_pd(a);
}
// double to float
static inline Vec8f to_float (Vec8d const a) {
return _mm512_cvtpd_ps(a);
}
#else // no AVX512. 512 bit float vectors are emulated
// float to double
static inline Vec8d to_double (Vec8f const a) {
Vec4d lo = to_double(a.get_low());
Vec4d hi = to_double(a.get_high());
return Vec8d(lo,hi);
}
// double to float
static inline Vec8f to_float (Vec8d const a) {
Vec4f lo = to_float(a.get_low());
Vec4f hi = to_float(a.get_high());
return Vec8f(lo, hi);
}
#endif
#endif // MAX_VECTOR_SIZE >= 512
// double to float
static inline Vec4f to_float (Vec2d const a) {
return _mm_cvtpd_ps(a);
}
/*****************************************************************************
*
* Generic template functions
*
* These templates define functions for multiple vector types in one template
*
*****************************************************************************/
// concatenate two vectors into one vector of double size
template <typename T> auto concatenate2(T const a, T const b) {
static_assert(sizeof(T) * 8 < MAX_VECTOR_SIZE, "Maximum vector size exceeded");
return decltype(extend_z(a))(a, b); // call constructor for double size vector type
}
// horizontal min/max of vector elements
// implemented with universal template, works for all vector types:
template <typename T> auto horizontal_min(T const x) {
if constexpr (T::elementtype() >= 15) {
// T is a float or double vector
if (horizontal_or(is_nan(x))) {
// check for NAN because min does not guarantee NAN propagation
return x[horizontal_find_first(is_nan(x))];
}
}
return horizontal_min1(x);
}
template <typename T> auto horizontal_min1(T const x) {
if constexpr (T::elementtype() <= 3) { // boolean vector type
return horizontal_and(x);
}
else if constexpr (sizeof(T) >= 32) {
// split recursively into smaller vectors
return horizontal_min1(min(x.get_low(), x.get_high()));
}
else if constexpr (T::size() == 2) {
T a = permute2 <1, V_DC>(x); // high half
T b = min(a, x);
return b[0];
}
else if constexpr (T::size() == 4) {
T a = permute4<2, 3, V_DC, V_DC>(x); // high half
T b = min(a, x);
a = permute4<1, V_DC, V_DC, V_DC>(b);
b = min(a, b);
return b[0];
}
else if constexpr (T::size() == 8) {
T a = permute8<4, 5, 6, 7, V_DC, V_DC, V_DC, V_DC>(x); // high half
T b = min(a, x);
a = permute8<2, 3, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = min(a, b);
a = permute8<1, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = min(a, b);
return b[0];
}
else {
static_assert(T::size() == 16); // no other size is allowed
T a = permute16<8, 9, 10, 11, 12, 13, 14, 15, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC >(x); // high half
T b = min(a, x);
a = permute16<4, 5, 6, 7, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = min(a, b);
a = permute16<2, 3, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = min(a, b);
a = permute16<1, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = min(a, b);
return b[0];
}
}
template <typename T> auto horizontal_max(T const x) {
if constexpr (T::elementtype() >= 15) {
// T is a float or double vector
if (horizontal_or(is_nan(x))) {
// check for NAN because max does not guarantee NAN propagation
return x[horizontal_find_first(is_nan(x))];
}
}
return horizontal_max1(x);
}
template <typename T> auto horizontal_max1(T const x) {
if constexpr (T::elementtype() <= 3) { // boolean vector type
return horizontal_or(x);
}
else if constexpr (sizeof(T) >= 32) {
// split recursively into smaller vectors
return horizontal_max1(max(x.get_low(), x.get_high()));
}
else if constexpr (T::size() == 2) {
T a = permute2 <1, V_DC>(x); // high half
T b = max(a, x);
return b[0];
}
else if constexpr (T::size() == 4) {
T a = permute4<2, 3, V_DC, V_DC>(x); // high half
T b = max(a, x);
a = permute4<1, V_DC, V_DC, V_DC>(b);
b = max(a, b);
return b[0];
}
else if constexpr (T::size() == 8) {
T a = permute8<4, 5, 6, 7, V_DC, V_DC, V_DC, V_DC>(x); // high half
T b = max(a, x);
a = permute8<2, 3, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = max(a, b);
a = permute8<1, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = max(a, b);
return b[0];
}
else {
static_assert(T::size() == 16); // no other size is allowed
T a = permute16<8, 9, 10, 11, 12, 13, 14, 15, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC >(x); // high half
T b = max(a, x);
a = permute16<4, 5, 6, 7, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = max(a, b);
a = permute16<2, 3, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = max(a, b);
a = permute16<1, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
b = max(a, b);
return b[0];
}
}
// Find first element that is true in a boolean vector
template <typename V>
static inline int horizontal_find_first(V const x) {
static_assert(V::elementtype() == 2 || V::elementtype() == 3, "Boolean vector expected");
auto bits = to_bits(x); // convert to bits
if (bits == 0) return -1;
if constexpr (V::size() < 32) {
return bit_scan_forward((uint32_t)bits);
}
else {
return bit_scan_forward(bits);
}
}
// Count the number of elements that are true in a boolean vector
template <typename V>
static inline int horizontal_count(V const x) {
static_assert(V::elementtype() == 2 || V::elementtype() == 3, "Boolean vector expected");
auto bits = to_bits(x); // convert to bits
if constexpr (V::size() < 32) {
return vml_popcnt((uint32_t)bits);
}
else {
return (int)vml_popcnt(bits);
}
}
// maximum and minimum functions. This version is sure to propagate NANs,
// conforming to the new IEEE-754 2019 standard
template <typename V>
static inline V maximum(V const a, V const b) {
if constexpr (V::elementtype() < 15) {
return max(a, b); // integer type
}
else { // float or double vector
V y = select(is_nan(a), a, max(a, b));
#ifdef SIGNED_ZERO // pedantic about signed zero
y = select(a == b, a & b, y); // maximum(+0, -0) = +0
#endif
return y;
}
}
template <typename V>
static inline V minimum(V const a, V const b) {
if constexpr (V::elementtype() < 15) {
return min(a, b); // integer type
}
else { // float or double vector
V y = select(is_nan(a), a, min(a, b));
#ifdef SIGNED_ZERO // pedantic about signed zero
y = select(a == b, a | b, y); // minimum(+0, -0) = -0
#endif
return y;
}
}
// floating point remainder
// -denominator/2 <= result < denominator/2
template <typename V>
static inline V fremainder(V const numerator, double const denominator) {
// (Optimization notice: Calculation of 1/denominator and constants for extended precision reduction
// may be optimized by a compiler moving loop-invariant code. This is intended)
static_assert(V::elementtype() == 16 || V::elementtype() == 17, "wrong vector type"); // supports only float and double
if (denominator > 0.) { // denominator must be positive
if constexpr (V::elementtype() == 16) { // float
#ifdef __FMA__
float recipd = float(1.0 / denominator); // reciprocal denominator
float fd = float(denominator); // denominator rounded to single precision
float d2 = float(denominator - fd); // remaining bits for double precision
V q = round(numerator * recipd); // divide and round
V m = nmul_add(q, d2, nmul_add(q, fd, numerator));// double precision reduction
#else // no FMA. Use extended precision reduction
union {
float f;
uint32_t i;
} u;
u.f = float(denominator);
u.i &= 0xFFFFF000; // remove 12 least significant bits for extended precision reduction
float d2 = denominator - u.f; // remaining bits
float recipd = float(1.0 / denominator); // reciprocal
V q = round(numerator * recipd); // divide and round
V m = nmul_add(q, d2, nmul_add(q, u.f, numerator));// extended precision reduction
#endif // FMA
if (true) { // Check that result is within desired interval. This may be omitted if not essential:
// This check may be needed in extreme cases of numerator > 1.E5 * denominator
auto too_high = m >= float( denominator * 0.5);
auto too_low = m < float(-denominator * 0.5);
m = if_sub(too_high, m, float(denominator));
m = if_add(too_low, m, float(denominator));
}
return m;
}
else if constexpr (V::elementtype() == 17) { // double precision
#ifdef __FMA__
double recipd = 1.0 / denominator; // reciprocal
V q = round(numerator * recipd); // divide and round
V m = nmul_add(q, denominator, numerator); // nmul_add has extended precision
#else // no FMA. Use extended precision reduction
union {
double f;
uint64_t i;
} u;
u.f = denominator;
u.i &= 0xFFFFFFFFFF000000; // remove 24 least significant bits for extended precision reduction
double d2 = denominator - u.f; // remaining bits
double recipd = 1.0 / denominator; // reciprocal
V q = round(numerator * recipd); // divide and round
V m = nmul_add(q, d2, nmul_add(q, u.f, numerator));// extended precision reduction
#endif // FMA
if (true) { // Check that result is within desired interval. This may be omitted if not essential:
// This check is rarely needed except in extreme cases of numerator > 1.E14 * denominator
auto too_high = m >= denominator * 0.5;
auto too_low = m < -denominator * 0.5;
m = if_sub(too_high, m, denominator);
m = if_add(too_low, m, denominator);
}
return m;
}
}
else {
return nan_vec<V>(1); // denominator is not positive
}
}
// floating point modulo
// 0 <= result < denominator
template <typename V>
static inline V fmodulo(V const numerator, double const denominator) {
// (Optimization notice: Calculation of 1/denominator and constants for extended precision reduction
// may be optimized by a compiler moving loop-invariant code. This is intended)
static_assert(V::elementtype() == 16 || V::elementtype() == 17, "wrong vector type"); // supports only float and double
if (denominator > 0.) { // denominator must be positive
if constexpr (V::elementtype() == 16) { // float
#ifdef __FMA__
float recipd = float(1.0 / denominator); // reciprocal denominator
float fd = float(denominator); // denominator rounded to single precision
float d2 = float(denominator - fd); // remaining bits for double precision
V q = floor(numerator * recipd); // divide and floor
V m = nmul_add(q, d2, nmul_add(q, fd, numerator));// double precision reduction
#else // no FMA. Use extended precision reduction
union {
float f;
uint32_t i;
} u;
u.f = float(denominator);
u.i &= 0xFFFFF000; // remove 12 least significant bits for extended precision reduction
float d2 = denominator - u.f; // remaining bits
float recipd = float(1.0 / denominator); // reciprocal
V q = floor(numerator * recipd); // divide and floor
V m = nmul_add(q, d2, nmul_add(q, u.f, numerator));// extended precision reduction
#endif // FMA
if (true) { // Check that result is within desired interval. This may be omitted if not essential:
// This check may be needed in extreme cases of numerator > 1.E5 * denominator
auto too_high = m >= float(denominator);
auto too_low = m < 0.f;
m = if_sub(too_high, m, float(denominator));
m = if_add(too_low, m, float(denominator));
}
return m;
}
else if constexpr (V::elementtype() == 17) { // double precision
#ifdef __FMA__
double recipd = 1.0 / denominator; // reciprocal
V q = floor(numerator * recipd); // divide and floor
V m = nmul_add(q, denominator, numerator); // nmul_add has extended precision
#else // no FMA. Use extended precision reduction
union {
double f;
uint64_t i;
} u;
u.f = denominator;
u.i &= 0xFFFFFFFFFF000000; // remove 24 least significant bits for extended precision reduction
double d2 = denominator - u.f; // remaining bits
double recipd = 1.0 / denominator; // reciprocal
V q = floor(numerator * recipd); // divide and floor
V m = nmul_add(q, d2, nmul_add(q, u.f, numerator));// extended precision reduction
#endif // FMA
if (true) { // Check that result is within desired interval. This may be omitted if not essential:
// This check is rarely needed except in extreme cases of numerator > 1.E14 * denominator
auto too_high = m >= denominator;
auto too_low = m < 0.;
m = if_sub(too_high, m, denominator);
m = if_add(too_low, m, denominator);
}
return m;
}
}
else {
return nan_vec<V>(1); // denominator is not positive
}
}
#ifdef VCL_NAMESPACE
}
#endif
#endif // VECTOR_CONVERT_H