-
Notifications
You must be signed in to change notification settings - Fork 148
/
vectormath_exp.h
2173 lines (1822 loc) · 75.9 KB
/
vectormath_exp.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
/**************************** vectormath_exp.h ******************************
* Author: Agner Fog
* Date created: 2014-04-18
* Last modified: 2022-07-20
* Version: 2.02.00
* Project: vector class library
* Description:
* Header file containing inline vector functions of logarithms, exponential
* and power functions:
* exp exponential function
* exp2 exponential function base 2
* exp10 exponential function base 10
* exmp1 exponential function minus 1
* log natural logarithm
* log2 logarithm base 2
* log10 logarithm base 10
* log1p natural logarithm of 1+x
* cbrt cube root
* pow raise vector elements to power
* pow_ratio raise vector elements to rational power
*
* Theory, methods and inspiration based partially on these sources:
* > Moshier, Stephen Lloyd Baluk: Methods and programs for mathematical functions.
* Ellis Horwood, 1989.
* > VDT library developed on CERN by Danilo Piparo, Thomas Hauth and Vincenzo Innocente,
* 2012, https://root.cern.ch/doc/v606_/md_math_vdt_ReadMe.html
* > Cephes math library by Stephen L. Moshier 1992,
* http://www.netlib.org/cephes/
*
* For detailed instructions see vcl_manual.pdf
*
* (c) Copyright 2014-2022 Agner Fog.
* Apache License version 2.0 or later.
******************************************************************************/
#ifndef VECTORMATH_EXP_H
#define VECTORMATH_EXP_H 202
#include "vectormath_common.h"
#ifdef VCL_NAMESPACE
namespace VCL_NAMESPACE {
#endif
/******************************************************************************
* Exponential functions
******************************************************************************/
// Helper functions, used internally:
// This function calculates pow(2,n) where n must be an integer. Does not check for overflow or underflow
static inline Vec2d vm_pow2n (Vec2d const n) {
const double pow2_52 = 4503599627370496.0; // 2^52
const double bias = 1023.0; // bias in exponent
Vec2d a = n + (bias + pow2_52); // put n + bias in least significant bits
Vec2q b = reinterpret_i(a); // bit-cast to integer
Vec2q c = b << 52; // shift left 52 places to get into exponent field
Vec2d d = reinterpret_d(c); // bit-cast back to double
return d;
}
static inline Vec4f vm_pow2n (Vec4f const n) {
const float pow2_23 = 8388608.0; // 2^23
const float bias = 127.0; // bias in exponent
Vec4f a = n + (bias + pow2_23); // put n + bias in least significant bits
Vec4i b = reinterpret_i(a); // bit-cast to integer
Vec4i c = b << 23; // shift left 23 places to get into exponent field
Vec4f d = reinterpret_f(c); // bit-cast back to float
return d;
}
#if MAX_VECTOR_SIZE >= 256
static inline Vec4d vm_pow2n (Vec4d const n) {
const double pow2_52 = 4503599627370496.0; // 2^52
const double bias = 1023.0; // bias in exponent
Vec4d a = n + (bias + pow2_52); // put n + bias in least significant bits
Vec4q b = reinterpret_i(a); // bit-cast to integer
Vec4q c = b << 52; // shift left 52 places to get value into exponent field
Vec4d d = reinterpret_d(c); // bit-cast back to double
return d;
}
static inline Vec8f vm_pow2n (Vec8f const n) {
const float pow2_23 = 8388608.0; // 2^23
const float bias = 127.0; // bias in exponent
Vec8f a = n + (bias + pow2_23); // put n + bias in least significant bits
Vec8i b = reinterpret_i(a); // bit-cast to integer
Vec8i c = b << 23; // shift left 23 places to get into exponent field
Vec8f d = reinterpret_f(c); // bit-cast back to float
return d;
}
#endif // MAX_VECTOR_SIZE >= 256
#if MAX_VECTOR_SIZE >= 512
static inline Vec8d vm_pow2n (Vec8d const n) {
#ifdef __AVX512ER__
return _mm512_exp2a23_round_pd(n, _MM_FROUND_NO_EXC); // this is exact only for integral n
#else
const double pow2_52 = 4503599627370496.0; // 2^52
const double bias = 1023.0; // bias in exponent
Vec8d a = n + (bias + pow2_52); // put n + bias in least significant bits
Vec8q b = Vec8q(reinterpret_i(a)); // bit-cast to integer
Vec8q c = b << 52; // shift left 52 places to get value into exponent field
Vec8d d = Vec8d(reinterpret_d(c)); // bit-cast back to double
return d;
#endif
}
static inline Vec16f vm_pow2n (Vec16f const n) {
#ifdef __AVX512ER__
return _mm512_exp2a23_round_ps(n, _MM_FROUND_NO_EXC);
#else
const float pow2_23 = 8388608.0; // 2^23
const float bias = 127.0; // bias in exponent
Vec16f a = n + (bias + pow2_23); // put n + bias in least significant bits
Vec16i b = Vec16i(reinterpret_i(a)); // bit-cast to integer
Vec16i c = b << 23; // shift left 23 places to get into exponent field
Vec16f d = Vec16f(reinterpret_f(c)); // bit-cast back to float
return d;
#endif
}
#endif // MAX_VECTOR_SIZE >= 512
// Template for exp function, double precision
// The limit of abs(x) is defined by max_x below
// This function does not produce denormals
// Template parameters:
// VTYPE: double vector type
// M1: 0 for exp, 1 for expm1
// BA: 0 for exp, 1 for 0.5*exp, 2 for pow(2,x), 10 for pow(10,x)
#if true // choose method
// Taylor expansion
template<typename VTYPE, int M1, int BA>
static inline VTYPE exp_d(VTYPE const initial_x) {
// Taylor coefficients, 1/n!
// Not using minimax approximation because we prioritize precision close to x = 0
const double p2 = 1./2.;
const double p3 = 1./6.;
const double p4 = 1./24.;
const double p5 = 1./120.;
const double p6 = 1./720.;
const double p7 = 1./5040.;
const double p8 = 1./40320.;
const double p9 = 1./362880.;
const double p10 = 1./3628800.;
const double p11 = 1./39916800.;
const double p12 = 1./479001600.;
const double p13 = 1./6227020800.;
// maximum abs(x), value depends on BA, defined below
// The lower limit of x is slightly more restrictive than the upper limit.
// We are specifying the lower limit, except for BA = 1 because it is not used for negative x
double max_x;
// data vectors
VTYPE x, r, z, n2;
if constexpr (BA <= 1) { // exp(x)
max_x = BA == 0 ? 708.39 : 709.7; // lower limit for 0.5*exp(x) is -707.6, but we are using 0.5*exp(x) only for positive x in hyperbolic functions
const double ln2d_hi = 0.693145751953125;
const double ln2d_lo = 1.42860682030941723212E-6;
x = initial_x;
r = round(initial_x*VM_LOG2E);
// subtraction in two steps for higher precision
x = nmul_add(r, ln2d_hi, x); // x -= r * ln2d_hi;
x = nmul_add(r, ln2d_lo, x); // x -= r * ln2d_lo;
}
else if constexpr (BA == 2) { // pow(2,x)
max_x = 1022.0;
r = round(initial_x);
x = initial_x - r;
x *= VM_LN2;
}
else if constexpr (BA == 10) { // pow(10,x)
max_x = 307.65;
const double log10_2_hi = 0.30102999554947019; // log10(2) in two parts
const double log10_2_lo = 1.1451100899212592E-10;
x = initial_x;
r = round(initial_x*(VM_LOG2E*VM_LN10));
// subtraction in two steps for higher precision
x = nmul_add(r, log10_2_hi, x); // x -= r * log10_2_hi;
x = nmul_add(r, log10_2_lo, x); // x -= r * log10_2_lo;
x *= VM_LN10;
}
else { // undefined value of BA
return 0.;
}
z = polynomial_13m(x, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13);
if constexpr (BA == 1) r--; // 0.5 * exp(x)
// multiply by power of 2
n2 = vm_pow2n(r);
if constexpr (M1 == 0) {
// exp
z = (z + 1.0) * n2;
}
else {
// expm1
z = mul_add(z, n2, n2 - 1.0); // z = z * n2 + (n2 - 1.0);
#ifdef SIGNED_ZERO // pedantic preservation of signed zero
z = select(initial_x == 0., initial_x, z);
#endif
}
// check for overflow
auto inrange = abs(initial_x) < max_x; // boolean vector
// check for INF and NAN
inrange &= is_finite(initial_x);
if (horizontal_and(inrange)) {
// fast normal path
return z;
}
else {
// overflow, underflow and NAN
r = select(sign_bit(initial_x), 0.-(M1&1), infinite_vec<VTYPE>()); // value in case of +/- overflow or INF
z = select(inrange, z, r); // +/- underflow
z = select(is_nan(initial_x), initial_x, z); // NAN goes through
return z;
}
}
#else
// Pade expansion uses less code and fewer registers, but is slower
template<typename VTYPE, int M1, int BA>
static inline VTYPE exp_d(VTYPE const initial_x) {
// define constants
const double ln2p1 = 0.693145751953125;
const double ln2p2 = 1.42860682030941723212E-6;
const double log2e = VM_LOG2E;
const double max_exp = 708.39;
// coefficients of pade polynomials
const double P0exp = 9.99999999999999999910E-1;
const double P1exp = 3.02994407707441961300E-2;
const double P2exp = 1.26177193074810590878E-4;
const double Q0exp = 2.00000000000000000009E0;
const double Q1exp = 2.27265548208155028766E-1;
const double Q2exp = 2.52448340349684104192E-3;
const double Q3exp = 3.00198505138664455042E-6;
VTYPE x, r, xx, px, qx, y, n2; // data vectors
x = initial_x;
r = round(initial_x*log2e);
// subtraction in one step would gives loss of precision
x -= r * ln2p1;
x -= r * ln2p2;
xx = x * x;
// px = x * P(x^2).
px = polynomial_2(xx, P0exp, P1exp, P2exp) * x;
// Evaluate Q(x^2).
qx = polynomial_3(xx, Q0exp, Q1exp, Q2exp, Q3exp);
// e^x = 1 + 2*P(x^2)/( Q(x^2) - P(x^2) )
y = (2.0 * px) / (qx - px);
// Get 2^n in double.
// n = round_to_int64_limited(r);
// n2 = exp2(n);
n2 = vm_pow2n(r); // this is faster
if constexpr (M1 == 0) {
// exp
y = (y + 1.0) * n2;
}
else {
// expm1
y = y * n2 + (n2 - 1.0);
}
// overflow
auto inrange = abs(initial_x) < max_exp;
// check for INF and NAN
inrange &= is_finite(initial_x);
if (horizontal_and(inrange)) {
// fast normal path
return y;
}
else {
// overflow, underflow and NAN
r = select(sign_bit(initial_x), 0.-M1, infinite_vec<VTYPE>()); // value in case of overflow or INF
y = select(inrange, y, r); // +/- overflow
y = select(is_nan(initial_x), initial_x, y); // NAN goes through
return y;
}
}
#endif
// instances of exp_d template
static inline Vec2d exp(Vec2d const x) {
return exp_d<Vec2d, 0, 0>(x);
}
static inline Vec2d expm1(Vec2d const x) {
return exp_d<Vec2d, 3, 0>(x);
}
static inline Vec2d exp2(Vec2d const x) {
return exp_d<Vec2d, 0, 2>(x);
}
static inline Vec2d exp10(Vec2d const x) {
return exp_d<Vec2d, 0, 10>(x);
}
#if MAX_VECTOR_SIZE >= 256
static inline Vec4d exp(Vec4d const x) {
return exp_d<Vec4d, 0, 0>(x);
}
static inline Vec4d expm1(Vec4d const x) {
return exp_d<Vec4d, 3, 0>(x);
}
static inline Vec4d exp2(Vec4d const x) {
return exp_d<Vec4d, 0, 2>(x);
}
static inline Vec4d exp10(Vec4d const x) {
return exp_d<Vec4d, 0, 10>(x);
}
#endif // MAX_VECTOR_SIZE >= 256
#if MAX_VECTOR_SIZE >= 512
static inline Vec8d exp(Vec8d const x) {
return exp_d<Vec8d, 0, 0>(x);
}
static inline Vec8d expm1(Vec8d const x) {
return exp_d<Vec8d, 3, 0>(x);
}
static inline Vec8d exp2(Vec8d const x) {
return exp_d<Vec8d, 0, 2>(x);
}
static inline Vec8d exp10(Vec8d const x) {
return exp_d<Vec8d, 0, 10>(x);
}
#endif // MAX_VECTOR_SIZE >= 512
// Template for exp function, single precision
// The limit of abs(x) is defined by max_x below
// This function does not produce denormals
// Template parameters:
// VTYPE: float vector type
// M1: 0 for exp, 1 for expm1
// BA: 0 for exp, 1 for 0.5*exp, 2 for pow(2,x), 10 for pow(10,x)
template<typename VTYPE, int M1, int BA>
static inline VTYPE exp_f(VTYPE const initial_x) {
// Taylor coefficients
const float P0expf = 1.f/2.f;
const float P1expf = 1.f/6.f;
const float P2expf = 1.f/24.f;
const float P3expf = 1.f/120.f;
const float P4expf = 1.f/720.f;
const float P5expf = 1.f/5040.f;
VTYPE x, r, x2, z, n2; // data vectors
// maximum abs(x), value depends on BA, defined below
// The lower limit of x is slightly more restrictive than the upper limit.
// We are specifying the lower limit, except for BA = 1 because it is not used for negative x
float max_x;
if constexpr (BA <= 1) { // exp(x)
const float ln2f_hi = 0.693359375f;
const float ln2f_lo = -2.12194440e-4f;
max_x = (BA == 0) ? 87.3f : 89.0f;
x = initial_x;
r = round(initial_x*float(VM_LOG2E));
x = nmul_add(r, VTYPE(ln2f_hi), x); // x -= r * ln2f_hi;
x = nmul_add(r, VTYPE(ln2f_lo), x); // x -= r * ln2f_lo;
}
else if constexpr (BA == 2) { // pow(2,x)
max_x = 126.f;
r = round(initial_x);
x = initial_x - r;
x = x * (float)VM_LN2;
}
else if constexpr (BA == 10) { // pow(10,x)
max_x = 37.9f;
const float log10_2_hi = 0.301025391f; // log10(2) in two parts
const float log10_2_lo = 4.60503907E-6f;
x = initial_x;
r = round(initial_x*float(VM_LOG2E*VM_LN10));
x = nmul_add(r, VTYPE(log10_2_hi), x); // x -= r * log10_2_hi;
x = nmul_add(r, VTYPE(log10_2_lo), x); // x -= r * log10_2_lo;
x = x * (float)VM_LN10;
}
else { // undefined value of BA
return 0.;
}
x2 = x * x;
z = polynomial_5(x,P0expf,P1expf,P2expf,P3expf,P4expf,P5expf);
z = mul_add(z, x2, x); // z *= x2; z += x;
if constexpr (BA == 1) r--; // 0.5 * exp(x)
// multiply by power of 2
n2 = vm_pow2n(r);
if constexpr (M1 == 0) {
// exp
z = (z + 1.0f) * n2;
}
else {
// expm1
z = mul_add(z, n2, n2 - 1.0f); // z = z * n2 + (n2 - 1.0f);
#ifdef SIGNED_ZERO // pedantic preservation of signed zero
z = select(initial_x == 0.f, initial_x, z);
#endif
}
// check for overflow
auto inrange = abs(initial_x) < max_x; // boolean vector
// check for INF and NAN
inrange &= is_finite(initial_x);
if (horizontal_and(inrange)) {
// fast normal path
return z;
}
else {
// overflow, underflow and NAN
r = select(sign_bit(initial_x), 0.f-(M1&1), infinite_vec<VTYPE>()); // value in case of +/- overflow or INF
z = select(inrange, z, r); // +/- underflow
z = select(is_nan(initial_x), initial_x, z); // NAN goes through
return z;
}
}
#if defined(__AVX512ER__) && MAX_VECTOR_SIZE >= 512
// forward declarations of fast 512 bit versions
static Vec16f exp(Vec16f const x);
static Vec16f exp2(Vec16f const x);
static Vec16f exp10(Vec16f const x);
#endif
// instances of exp_f template
static inline Vec4f exp(Vec4f const x) {
#if defined(__AVX512ER__) && MAX_VECTOR_SIZE >= 512 // use faster 512 bit version
return _mm512_castps512_ps128(exp(Vec16f(_mm512_castps128_ps512(x))));
#else
return exp_f<Vec4f, 0, 0>(x);
#endif
}
static inline Vec4f expm1(Vec4f const x) {
return exp_f<Vec4f, 3, 0>(x);
}
static inline Vec4f exp2(Vec4f const x) {
#if defined(__AVX512ER__) && MAX_VECTOR_SIZE >= 512 // use faster 512 bit version
return _mm512_castps512_ps128(exp2(Vec16f(_mm512_castps128_ps512(x))));
#else
return exp_f<Vec4f, 0, 2>(x);
#endif
}
static inline Vec4f exp10(Vec4f const x) {
#if defined(__AVX512ER__) && MAX_VECTOR_SIZE >= 512 // use faster 512 bit version
return _mm512_castps512_ps128(exp10(Vec16f(_mm512_castps128_ps512(x))));
#else
return exp_f<Vec4f, 0, 10>(x);
#endif
}
#if MAX_VECTOR_SIZE >= 256
static inline Vec8f exp(Vec8f const x) {
#if defined(__AVX512ER__) && MAX_VECTOR_SIZE >= 512 // use faster 512 bit version
return _mm512_castps512_ps256(exp(Vec16f(_mm512_castps256_ps512(x))));
#else
return exp_f<Vec8f, 0, 0>(x);
#endif
}
static inline Vec8f expm1(Vec8f const x) {
return exp_f<Vec8f, 3, 0>(x);
}
static inline Vec8f exp2(Vec8f const x) {
#if defined(__AVX512ER__) && MAX_VECTOR_SIZE >= 512 // use faster 512 bit version
return _mm512_castps512_ps256(exp2(Vec16f(_mm512_castps256_ps512(x))));
#else
return exp_f<Vec8f, 0, 2>(x);
#endif
}
static inline Vec8f exp10(Vec8f const x) {
#if defined(__AVX512ER__) && MAX_VECTOR_SIZE >= 512 // use faster 512 bit version
return _mm512_castps512_ps256(exp10(Vec16f(_mm512_castps256_ps512(x))));
#else
return exp_f<Vec8f, 0, 10>(x);
#endif
}
#endif // MAX_VECTOR_SIZE >= 256
#if MAX_VECTOR_SIZE >= 512
static inline Vec16f exp(Vec16f const x) {
#ifdef __AVX512ER__ // AVX512ER instruction set includes fast exponential function
#ifdef VCL_FASTEXP
// very fast, but less precise for large x:
return _mm512_exp2a23_round_ps(x*float(VM_LOG2E), _MM_FROUND_NO_EXC);
#else
// best precision, also for large x:
const Vec16f log2e = float(VM_LOG2E);
const float ln2f_hi = 0.693359375f;
const float ln2f_lo = -2.12194440e-4f;
Vec16f x1 = x, r, y;
r = round(x1*log2e);
x1 = nmul_add(r, Vec16f(ln2f_hi), x1); // x -= r * ln2f_hi;
x1 = nmul_add(r, Vec16f(ln2f_lo), x1); // x -= r * ln2f_lo;
x1 = x1 * log2e;
y = _mm512_exp2a23_round_ps(r, _MM_FROUND_NO_EXC);
// y = vm_pow2n(r);
return y * _mm512_exp2a23_round_ps(x1, _MM_FROUND_NO_EXC);
#endif // VCL_FASTEXP
#else // no AVX512ER, use above template
return exp_f<Vec16f, 0, 0>(x);
#endif
}
static inline Vec16f expm1(Vec16f const x) {
return exp_f<Vec16f, 3, 0>(x);
}
static inline Vec16f exp2(Vec16f const x) {
#ifdef __AVX512ER__
return Vec16f(_mm512_exp2a23_round_ps(x, _MM_FROUND_NO_EXC));
#else
return exp_f<Vec16f, 0, 2>(x);
#endif
}
static inline Vec16f exp10(Vec16f const x) {
#ifdef __AVX512ER__ // AVX512ER instruction set includes fast exponential function
#ifdef VCL_FASTEXP
// very fast, but less precise for large x:
return _mm512_exp2a23_round_ps(x*float(VM_LOG210), _MM_FROUND_NO_EXC);
#else
// best precision, also for large x:
const float log10_2_hi = 0.301025391f; // log10(2) in two parts
const float log10_2_lo = 4.60503907E-6f;
Vec16f x1 = x, r, y;
Vec16f log210 = float(VM_LOG210);
r = round(x1*log210);
x1 = nmul_add(r, Vec16f(log10_2_hi), x1); // x -= r * log10_2_hi
x1 = nmul_add(r, Vec16f(log10_2_lo), x1); // x -= r * log10_2_lo
x1 = x1 * log210;
// y = vm_pow2n(r);
y = _mm512_exp2a23_round_ps(r, _MM_FROUND_NO_EXC);
return y * _mm512_exp2a23_round_ps(x1, _MM_FROUND_NO_EXC);
#endif // VCL_FASTEXP
#else // no AVX512ER, use above template
return exp_f<Vec16f, 0, 10>(x);
#endif
}
#endif // MAX_VECTOR_SIZE >= 512
/******************************************************************************
* Logarithm functions
******************************************************************************/
// Helper function: fraction_2(x) = fraction(x)*0.5
// Modified fraction function:
// Extract the fraction part of a floating point number, and divide by 2
// The fraction function is defined in vectorf128.h etc.
// fraction_2(x) = fraction(x)*0.5
// This version gives half the fraction without extra delay
// Does not work for x = 0
static inline Vec4f fraction_2(Vec4f const a) {
Vec4ui t1 = _mm_castps_si128(a); // reinterpret as 32-bit integer
Vec4ui t2 = Vec4ui((t1 & 0x007FFFFF) | 0x3F000000); // set exponent to 0 + bias
return _mm_castsi128_ps(t2);
}
static inline Vec2d fraction_2(Vec2d const a) {
Vec2uq t1 = _mm_castpd_si128(a); // reinterpret as 64-bit integer
Vec2uq t2 = Vec2uq((t1 & 0x000FFFFFFFFFFFFFll) | 0x3FE0000000000000ll); // set exponent to 0 + bias
return _mm_castsi128_pd(t2);
}
#if MAX_VECTOR_SIZE >= 256
static inline Vec8f fraction_2(Vec8f const a) {
#if defined (VECTORI256_H) && VECTORI256_H > 2 // 256 bit integer vectors are available, AVX2
Vec8ui t1 = _mm256_castps_si256(a); // reinterpret as 32-bit integer
Vec8ui t2 = (t1 & 0x007FFFFF) | 0x3F000000; // set exponent to 0 + bias
return _mm256_castsi256_ps(t2);
#else
return Vec8f(fraction_2(a.get_low()), fraction_2(a.get_high()));
#endif
}
static inline Vec4d fraction_2(Vec4d const a) {
#if VECTORI256_H > 1 // AVX2
Vec4uq t1 = _mm256_castpd_si256(a); // reinterpret as 64-bit integer
Vec4uq t2 = Vec4uq((t1 & 0x000FFFFFFFFFFFFFll) | 0x3FE0000000000000ll); // set exponent to 0 + bias
return _mm256_castsi256_pd(t2);
#else
return Vec4d(fraction_2(a.get_low()), fraction_2(a.get_high()));
#endif
}
#endif // MAX_VECTOR_SIZE >= 256
#if MAX_VECTOR_SIZE >= 512
static inline Vec16f fraction_2(Vec16f const a) {
#if INSTRSET >= 9 // 512 bit integer vectors are available, AVX512
return _mm512_getmant_ps(a, _MM_MANT_NORM_p5_1, _MM_MANT_SIGN_zero);
//return Vec16f(_mm512_getmant_ps(a, _MM_MANT_NORM_1_2, _MM_MANT_SIGN_zero)) * 0.5f;
#else
return Vec16f(fraction_2(a.get_low()), fraction_2(a.get_high()));
#endif
}
static inline Vec8d fraction_2(Vec8d const a) {
#if INSTRSET >= 9 // 512 bit integer vectors are available, AVX512
return _mm512_getmant_pd(a, _MM_MANT_NORM_p5_1, _MM_MANT_SIGN_zero);
//return Vec8d(_mm512_getmant_pd(a, _MM_MANT_NORM_1_2, _MM_MANT_SIGN_zero)) * 0.5;
#else
return Vec8d(fraction_2(a.get_low()), fraction_2(a.get_high()));
#endif
}
#endif // MAX_VECTOR_SIZE >= 512
// Helper function: exponent_f(x) = exponent(x) as floating point number
union vm_ufi {
float f;
uint32_t i;
};
union vm_udi {
double d;
uint64_t i;
};
// extract exponent of a positive number x as a floating point number
static inline Vec4f exponent_f(Vec4f const x) {
#if INSTRSET >= 10 // AVX512VL
// prevent returning -inf for x=0
return _mm_maskz_getexp_ps(_mm_cmp_ps_mask(x,Vec4f(0.f),4), x);
#else
const float pow2_23 = 8388608.0f; // 2^23
const float bias = 127.f; // bias in exponent
const vm_ufi upow2_23 = {pow2_23};
Vec4ui a = reinterpret_i(x); // bit-cast x to integer
Vec4ui b = a >> 23; // shift down exponent to low bits
Vec4ui c = b | Vec4ui(upow2_23.i); // insert new exponent
Vec4f d = reinterpret_f(c); // bit-cast back to double
Vec4f e = d - (pow2_23 + bias); // subtract magic number and bias
return e;
#endif
}
static inline Vec2d exponent_f(Vec2d const x) {
#if INSTRSET >= 10 // AVX512VL
// prevent returning -inf for x=0
//return _mm_maskz_getexp_pd(x != 0., x);
return _mm_maskz_getexp_pd(_mm_cmp_pd_mask(x,Vec2d(0.),4), x);
#else
const double pow2_52 = 4503599627370496.0; // 2^52
const double bias = 1023.0; // bias in exponent
const vm_udi upow2_52 = {pow2_52};
Vec2uq a = reinterpret_i(x); // bit-cast x to integer
Vec2uq b = a >> 52; // shift down exponent to low bits
Vec2uq c = b | Vec2uq(upow2_52.i); // insert new exponent
Vec2d d = reinterpret_d(c); // bit-cast back to double
Vec2d e = d - (pow2_52 + bias); // subtract magic number and bias
return e;
#endif
}
#if MAX_VECTOR_SIZE >= 256
static inline Vec8f exponent_f(Vec8f const x) {
#if INSTRSET >= 10
// prevent returning -inf for x=0
//return _mm256_maskz_getexp_ps(x != 0.f, x);
return _mm256_maskz_getexp_ps(_mm256_cmp_ps_mask(x,Vec8f(0.f),4), x);
#else
const float pow2_23 = 8388608.0f; // 2^23
const float bias = 127.f; // bias in exponent
const vm_ufi upow2_23 = {pow2_23};
Vec8ui a = reinterpret_i(x); // bit-cast x to integer
Vec8ui b = a >> 23; // shift down exponent to low bits
Vec8ui c = b | Vec8ui(upow2_23.i); // insert new exponent
Vec8f d = reinterpret_f(c); // bit-cast back to double
Vec8f e = d - (pow2_23 + bias); // subtract magic number and bias
return e;
#endif
}
// extract exponent of a positive number x as a floating point number
static inline Vec4d exponent_f(Vec4d const x) {
#if INSTRSET >= 10
// prevent returning -inf for x=0
//return _mm256_maskz_getexp_pd(x != 0., x);
return _mm256_maskz_getexp_pd(_mm256_cmp_pd_mask(x,Vec4d(0.),4), x);
#else
const double pow2_52 = 4503599627370496.0; // 2^52
const double bias = 1023.0; // bias in exponent
const vm_udi upow2_52 = {pow2_52};
Vec4uq a = reinterpret_i(x); // bit-cast x to integer
Vec4uq b = a >> 52; // shift down exponent to low bits
Vec4uq c = b | Vec4uq(upow2_52.i); // insert new exponent
Vec4d d = reinterpret_d(c); // bit-cast back to double
Vec4d e = d - (pow2_52 + bias); // subtract magic number and bias
return e;
#endif
}
#endif // MAX_VECTOR_SIZE >= 256
#if MAX_VECTOR_SIZE >= 512
static inline Vec16f exponent_f(Vec16f const x) {
#if INSTRSET >= 9 // AVX512
// prevent returning -inf for x=0
return _mm512_maskz_getexp_ps(x != 0.f, x);
#else
return Vec16f(exponent_f(x.get_low()), exponent_f(x.get_high()));
#endif
}
// extract exponent of a positive number x as a floating point number
static inline Vec8d exponent_f(Vec8d const x) {
#if INSTRSET >= 9 // AVX512
// prevent returning -inf for x=0
return _mm512_maskz_getexp_pd(uint8_t(x != 0.), x);
#else
return Vec8d(exponent_f(x.get_low()), exponent_f(x.get_high()));
#endif
}
#endif // MAX_VECTOR_SIZE >= 512
// Helper function: log_special_cases(x,r). Handle special cases for log function
#if MAX_VECTOR_SIZE >= 512
static inline Vec8d log_special_cases(Vec8d const x1, Vec8d const r) {
Vec8d res = r;
#if INSTRSET >= 10 // AVX512DQ
Vec8db specialcases = _mm512_fpclass_pd_mask(x1, 0x7E);// zero, subnormal, negative, +-inf
if (!horizontal_or(specialcases)) {
return res; // normal path
}
res = _mm512_fixupimm_pd(res, x1, Vec8q(0x03530411),0);// handle most cases
res = select(Vec8db(_mm512_fpclass_pd_mask(x1, 0x26)),-infinite_vec<Vec8d>(),res); // subnormal -> -INF
res = select(Vec8db(_mm512_fpclass_pd_mask(x1, 0x50)),nan_vec<Vec8d>(NAN_LOG),res); // negative -> specific NAN
return res;
#else
Vec8db overflow = !is_finite(x1);
Vec8db underflow = x1 < VM_SMALLEST_NORMAL; // denormals not supported by this functions
if (!horizontal_or(overflow | underflow)) {
return res; // normal path
}
// overflow and underflow
res = select(underflow, nan_vec<Vec8d>(NAN_LOG), res); // x1 < 0 gives NAN
res = select(is_zero_or_subnormal(x1), -infinite_vec<Vec8d>(), res); // x1 == 0 gives -INF
res = select(overflow, x1, res); // INF or NAN goes through
res = select(is_inf(x1) & sign_bit(x1), nan_vec<Vec8d>(NAN_LOG), res);// -INF gives NAN
return res;
#endif // INSTRSET
}
static inline Vec16f log_special_cases(Vec16f const x1, Vec16f const r) {
Vec16f res = r;
#if INSTRSET >= 10 // AVX512DQ
Vec16fb specialcases = _mm512_fpclass_ps_mask(x1, 0x7E); // zero, subnormal, negative, +-inf
if (!horizontal_or(specialcases)) {
return res; // normal path
}
res = _mm512_fixupimm_ps(res, x1, Vec16i(0x03530411), 0); // handle most cases
res = select(Vec16fb(_mm512_fpclass_ps_mask(x1, 0x26)),-infinite_vec<Vec16f>(),res); // subnormal -> -INF
res = select(Vec16fb(_mm512_fpclass_ps_mask(x1, 0x50)),nan_vec<Vec16f>(NAN_LOG),res); // negative -> specific NAN
return res;
#else
Vec16fb overflow = !is_finite(x1);
Vec16fb underflow = x1 < VM_SMALLEST_NORMALF;// denormals not supported by this functions
if (!horizontal_or(overflow | underflow)) {
return res; // normal path
}
// overflow and underflow
res = select(underflow, nan_vec<Vec16f>(NAN_LOG), res); // x1 < 0 gives NAN
res = select(is_zero_or_subnormal(x1), -infinite_vec<Vec16f>(), res); // x1 == 0 gives -INF
res = select(overflow, x1, res); // INF or NAN goes through
res = select(is_inf(x1) & sign_bit(x1), nan_vec<Vec16f>(NAN_LOG), res);// -INF gives NAN
return res;
#endif // INSTRSET
}
#endif // MAX_VECTOR_SIZE >= 512
#if MAX_VECTOR_SIZE >= 256
static inline Vec4d log_special_cases(Vec4d const x1, Vec4d const r) {
Vec4d res = r;
#if INSTRSET >= 10 // AVX512DQ AVX512VL
__mmask8 specialcases = _mm256_fpclass_pd_mask(x1, 0x7E); // zero, subnormal, negative, +-inf
if (specialcases == 0) {
return res; // normal path
}
res = _mm256_fixupimm_pd(res, x1, Vec4q(0x03530411), 0); // handle most cases
res = _mm256_mask_mov_pd(res, _mm256_fpclass_pd_mask(x1, 0x26), -infinite_vec<Vec4d>()); // subnormal -> -INF
res = _mm256_mask_mov_pd(res, _mm256_fpclass_pd_mask(x1, 0x50), nan_vec<Vec4d>(NAN_LOG)); // negative -> specific NAN
return res;
#else
Vec4db overflow = !is_finite(x1);
Vec4db underflow = x1 < VM_SMALLEST_NORMAL; // denormals not supported by this functions
if (!horizontal_or(overflow | underflow)) {
return res; // normal path
}
// overflow and underflow
res = select(underflow, nan_vec<Vec4d>(NAN_LOG), res); // x1 < 0 gives NAN
res = select(is_zero_or_subnormal(x1), -infinite_vec<Vec4d>(), res); // x1 == 0 gives -INF
res = select(overflow, x1, res); // INF or NAN goes through
res = select(is_inf(x1) & sign_bit(x1), nan_vec<Vec4d>(NAN_LOG), res);// -INF gives NAN
return res;
#endif // INSTRSET
}
static inline Vec8f log_special_cases(Vec8f const x1, Vec8f const r) {
Vec8f res = r;
#if INSTRSET >= 10 // AVX512DQ AVX512VL
__mmask8 specialcases = _mm256_fpclass_ps_mask(x1, 0x7E); // zero, subnormal, negative, +-inf
if (specialcases == 0) {
return res; // normal path
}
res = _mm256_fixupimm_ps(res, x1, Vec8i(0x03530411), 0); // handle most cases
res = _mm256_mask_mov_ps(res, _mm256_fpclass_ps_mask(x1, 0x26), -infinite_vec<Vec8f>()); // subnormal -> -INF
res = _mm256_mask_mov_ps(res, _mm256_fpclass_ps_mask(x1, 0x50), nan_vec<Vec8f>(NAN_LOG)); // negative -> specific NAN
return res;
#else
Vec8fb overflow = !is_finite(x1);
Vec8fb underflow = x1 < VM_SMALLEST_NORMALF; // denormals not supported by this functions
if (!horizontal_or(overflow | underflow)) {
return res; // normal path
}
// overflow and underflow
res = select(underflow, nan_vec<Vec8f>(NAN_LOG), res); // x1 < 0 gives NAN
res = select(is_zero_or_subnormal(x1), -infinite_vec<Vec8f>(), res); // x1 == 0 gives -INF
res = select(overflow, x1, res); // INF or NAN goes through
res = select(is_inf(x1) & sign_bit(x1), nan_vec<Vec8f>(NAN_LOG), res);// -INF gives NAN
return res;
#endif // INSTRSET
}
#endif // MAX_VECTOR_SIZE >= 256
static inline Vec2d log_special_cases(Vec2d const x1, Vec2d const r) {
Vec2d res = r;
#if INSTRSET >= 10 // AVX512DQ AVX512VL
__mmask8 specialcases = _mm_fpclass_pd_mask(x1, 0x7E); // zero, subnormal, negative, +-inf
if (specialcases == 0) {
return res; // normal path
}
res = _mm_fixupimm_pd(res, x1, Vec2q(0x03530411), 0); // handle most cases
res = _mm_mask_mov_pd(res, _mm_fpclass_pd_mask(x1, 0x26), -infinite_vec<Vec2d>()); // subnormal -> -INF
res = _mm_mask_mov_pd(res, _mm_fpclass_pd_mask(x1, 0x50), nan_vec<Vec2d>(NAN_LOG)); // negative -> specific NAN
return res;
#else
Vec2db overflow = !is_finite(x1);
Vec2db underflow = x1 < VM_SMALLEST_NORMAL; // denormals not supported by this functions
if (!horizontal_or(overflow | underflow)) {
return res; // normal path
}
// overflow and underflow
res = select(underflow, nan_vec<Vec2d>(NAN_LOG), res); // x1 < 0 gives NAN
res = select(is_zero_or_subnormal(x1), -infinite_vec<Vec2d>(), res); // x1 == 0 gives -INF
res = select(overflow, x1, res); // INF or NAN goes through
res = select(is_inf(x1) & sign_bit(x1), nan_vec<Vec2d>(NAN_LOG), res);// -INF gives NAN
return res;
#endif // INSTRSET
}
static inline Vec4f log_special_cases(Vec4f const x1, Vec4f const r) {
Vec4f res = r;
#if INSTRSET >= 10 // AVX512DQ AVX512VL
__mmask8 specialcases = _mm_fpclass_ps_mask(x1, 0x7E); // zero, subnormal, negative, +-inf
if (specialcases == 0) {
return res; // normal path
}
res = _mm_fixupimm_ps(res, x1, Vec4i(0x03530411), 0); // handle most cases
res = _mm_mask_mov_ps(res, _mm_fpclass_ps_mask(x1, 0x26), -infinite_vec<Vec4f>()); // subnormal -> -INF
res = _mm_mask_mov_ps(res, _mm_fpclass_ps_mask(x1, 0x50), nan_vec<Vec4f>(NAN_LOG)); // negative -> specific NAN
return res;
#else
Vec4fb overflow = !is_finite(x1);
Vec4fb underflow = x1 < VM_SMALLEST_NORMALF; // denormals not supported by this functions
if (!horizontal_or(overflow | underflow)) {
return res; // normal path
}
// overflow and underflow
res = select(underflow, nan_vec<Vec4f>(NAN_LOG), res); // x1 < 0 gives NAN
res = select(is_zero_or_subnormal(x1), -infinite_vec<Vec4f>(), res); // x1 == 0 gives -INF
res = select(overflow, x1, res); // INF or NAN goes through
res = select(is_inf(x1) & sign_bit(x1), nan_vec<Vec4f>(NAN_LOG), res);// -INF gives NAN
return res;
#endif // INSTRSET
}
// log function, double precision
// template parameters:
// VTYPE: f.p. vector type
// M1: 0 for log, 1 for log1p
template<typename VTYPE, int M1>
static inline VTYPE log_d(VTYPE const initial_x) {
// define constants
const double ln2_hi = 0.693359375;
const double ln2_lo = -2.121944400546905827679E-4;
const double P0log = 7.70838733755885391666E0;
const double P1log = 1.79368678507819816313E1;
const double P2log = 1.44989225341610930846E1;
const double P3log = 4.70579119878881725854E0;
const double P4log = 4.97494994976747001425E-1;
const double P5log = 1.01875663804580931796E-4;
const double Q0log = 2.31251620126765340583E1;
const double Q1log = 7.11544750618563894466E1;
const double Q2log = 8.29875266912776603211E1;
const double Q3log = 4.52279145837532221105E1;
const double Q4log = 1.12873587189167450590E1;
VTYPE x1, x, x2, px, qx, res, fe; // data vectors
if constexpr (M1 == 0) {
x1 = initial_x; // log(x)
}
else {
x1 = initial_x + 1.0; // log(x+1)
}
// separate mantissa from exponent
// VTYPE x = fraction(x1) * 0.5;
x = fraction_2(x1);
fe = exponent_f(x1);
auto blend = x > VM_SQRT2*0.5; // boolean vector
x = if_add(!blend, x, x); // conditional add
fe = if_add(blend, fe, 1.); // conditional add
if constexpr (M1 == 0) {
// log(x). Expand around 1.0
x -= 1.0;
}
else {
// log(x+1). Avoid loss of precision when adding 1 and later subtracting 1 if exponent = 0
x = select(fe==0., initial_x, x - 1.0);
}
// rational form
px = polynomial_5 (x, P0log, P1log, P2log, P3log, P4log, P5log);
x2 = x * x;
px *= x * x2;
qx = polynomial_5n(x, Q0log, Q1log, Q2log, Q3log, Q4log);
res = px / qx ;
// add exponent
res = mul_add(fe, ln2_lo, res); // res += fe * ln2_lo;
res += nmul_add(x2, 0.5, x); // res += x - 0.5 * x2;
res = mul_add(fe, ln2_hi, res); // res += fe * ln2_hi;