-
Notifications
You must be signed in to change notification settings - Fork 0
/
cx_math.hpp
6329 lines (5412 loc) · 239 KB
/
cx_math.hpp
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
#pragma once
//=================================================================================================
// cx_math.hpp
// #include "cx_math.hpp"
// Remotion (C) 2022 - All Rights Reserved
//=================================================================================================
#include <cstdint>
#include <cfloat>
#include <cmath>
#include <type_traits> // std::is_same_v, std::declval, std::enable_if
#include <limits> // std::numeric_limits
#include <bit> // std::bit_cast
/// fast constexpr math functions
#if defined(__clang__) && __clang_major__ <= 9
namespace std {
template <class To, class From,
enable_if_t<conjunction_v<bool_constant<sizeof(To) == sizeof(From)>,
is_trivially_copyable<To>, is_trivially_copyable<From>>, int> = 0>
[[nodiscard]] constexpr To bit_cast(const From& value) noexcept { return __builtin_bit_cast(To, value); }
[[nodiscard]] constexpr bool is_constant_evaluated() noexcept { return __builtin_is_constant_evaluated(); }
} // namespace std
#endif
#if defined(__clang__)
# define CX_CLANG_PRAGMA(UnQuotedPragma) _Pragma(#UnQuotedPragma)
#else
# define CX_CLANG_PRAGMA(UnQuotedPragma)
#endif
#if defined(_MSC_VER)
# define CX_MSVC_PRAGMA(UnQuotedPragma) _Pragma(#UnQuotedPragma)
#else
# define CX_MSVC_PRAGMA(UnQuotedPragma)
#endif
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
// https://docs.microsoft.com/en-us/cpp/preprocessor/fp-contract?view=msvc-170
// https://godbolt.org/z/GM4qs7PPM
#pragma fp_contract(on)
#endif
#if defined(__clang__) || defined(__GNUC__)
# define CX_FORCEINLINE inline __attribute__((always_inline))
# ifndef __forceinline
# define __forceinline inline __attribute__((always_inline))
# endif
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
# define CX_FORCEINLINE __forceinline
#else
# define CX_FORCEINLINE
#endif
/// math constants
namespace cx {
template<typename T> constexpr T E = T(2.718281828459045235360287471352662497757247093699959574966967627724076630353555L); // e (Euler's Number)
template<typename T> constexpr T ONE_OVER_E = T(0.3678794411714423215955237701614608674458111310317678345078368016974614957448981L); // 1/e
// Omega, Ω = LambertW(1), Ω = womega(0), Ω*exp(Ω) = 1
template<typename T> constexpr T OMEGA = T(0.5671432904097838729999686622103555497538157871865125081351310792230457930866);
template<typename T> constexpr T LOG2E = T(1.442695040888963407359924681001892137426645954152985934135449406931109219181187L); // 1/log(2) = log2(e)
template<typename T> constexpr T LOG3E = T(0.9102392266268373936142401657361070006126360572552117447263020632952810831937972L); // 1/log(3)
template<typename T> constexpr T LOG4E = T(0.7213475204444817036799623405009460687133229770764929670677247034655546095905937L); // 1/log(4)
template<typename T> constexpr T LOG5E = T(0.6213349345596118107071993881805725841234130911147952958653233293551794504916422L); // 1/log(5)
template<typename T> constexpr T LOG6E = T(0.5581106265512472537174034379326177037763393583967291328410073575505919580629214L); // 1/log(6)
template<typename T> constexpr T LOG7E = T(0.5138983423697506930446493893701893866513391649321614254055023795777233681611104L); // 1/log(7)
template<typename T> constexpr T LOG8E = T(0.4808983469629878024533082270006307124755486513843286447118164689770364063937291L); // 1/log(8)
template<typename T> constexpr T LOG9E = T(0.4551196133134186968071200828680535003063180286276058723631510316476405415968986L); // 1/log(9)
template<typename T> constexpr T LOG10E = T(0.4342944819032518276511289189166050822943970058036665661144537831658646492088688L); // 1/log(10) = log10(e)
template<typename T> constexpr T LN2 = T(0.6931471805599453094172321214581765680755001343602552541206800094933936219696955L); // log(2)
template<typename T> constexpr T LN10 = T(2.302585092994045684017991454684364207601101488628772976033327900967572609677367L); // log(10)
template<typename T> constexpr T PHI = T(1.6180339887498948482045868343656381177203091798057628621354486227052604628189L); // Golden Ratio (phi = φ) = (1.0+sqrt(5.0))/2.0
template<typename T> constexpr T ONE_OVER_PHI = T(0.6180339887498948482045868343656381177203091798057628621354486227052604628189L); // 1 / φ
template<typename T> constexpr T PI = T(3.141592653589793238462643383279502884197169399375105820974944592307816406286198L); // pi, π, a bit more digits as 355/113.
template<typename T> constexpr T TWO_PI = T(6.283185307179586476925286766559005768394338798750211641949889184615632812572396L); // tau, τ = 2 * pi 'PI2'
template<typename T> constexpr T ONE_OVER_TWO_PI = T(0.15915494309189533576888376337251436203445964574046L); // 1 / (2*pi) 'invtwopi', 'PI2_INV'
template<typename T> constexpr T PI_2 = T(1.57079632679489661923132169163975144209858469968755L); // pi/2 'halfpi', 'PI05'
template<typename T> constexpr T PI_4 = T(0.78539816339744830961566084581987572104929234984378L); // pi/4 'quarterpi'
template<typename T> constexpr T ONE_OVER_PI = T(0.31830988618379067153776752674502872406891929148091L); // 1 / pi 'PI_INV'
template<typename T> constexpr T TWO_OVER_PI = T(0.63661977236758134307553505349005744813783858296183L); // 2 / pi
template<typename T> constexpr T TWO_OVER_SQRTPI = T(1.12837916709551257389615890312154517168810125865800L); // 2 / sqrt(pi)
template<typename T> constexpr T SQRT2 = T(1.414213562373095048801688724209698078569671875376948073176679737990732478462102L); // sqrt(2)
template<typename T> constexpr T ONE_OVER_SQRT2 = T(0.7071067811865475244008443621048490392848359376884740365883398689953662392310596L); // 1 / sqrt(2)
template<typename T> constexpr T SQRT3 = T(1.732050807568877293527446341505872366942805253810380628055806979451933016908798L); // sqrt(3)
template<typename T> constexpr T ONE_OVER_SQRT3 = T(0.5773502691896257645091487805019574556476017512701268760186023264839776723029325L); // 1 / sqrt(3)
template<typename T> constexpr T SQRT5 = T(2.23606797749978969640917366873127623544061835961152572427089724541052092563782); // sqrt(5)
template<typename T> constexpr T ONE_OVER_SQRT5 = T(0.4472135954999579392818347337462552470881236719223051448541794490821041851275596); // 1 / sqrt(5)
} // namespace cx
/// Function prototypes
namespace cx {
constexpr float floor(float x) noexcept;
constexpr double floor(double x) noexcept;
constexpr float round(float d) noexcept;
constexpr double round(double d) noexcept;
/// Trigonometric functions.
constexpr float cos(float x) noexcept;
constexpr double cos(double x) noexcept;
constexpr float sin(float x) noexcept;
constexpr double sin(double x) noexcept;
constexpr float tan(float x) noexcept;
constexpr double tan(double x) noexcept;
/// Inverse trigonometric functions.
constexpr float acos(float x) noexcept;
constexpr double acos(double x) noexcept;
constexpr float asin(float x) noexcept;
constexpr double asin(double x) noexcept;
constexpr float atan(float x) noexcept;
constexpr double atan(double x) noexcept;
/// Logarithms, exponential and power functions
constexpr float sqrt(float x) noexcept;
constexpr double sqrt(double x) noexcept;
constexpr float cbrt(float x) noexcept;
constexpr double cbrt(double x) noexcept;
constexpr float pow(float x, float y) noexcept;
constexpr double pow(double x, double y) noexcept;
constexpr float log(float x) noexcept;
constexpr double log(double x) noexcept;
constexpr float exp(float x) noexcept;
constexpr double exp(double x) noexcept;
} // namespace cx
/// Fast constexpr math functions
namespace cx {
/// select(m, t, f) between two values based on a boolean condition.
template<typename T> constexpr __forceinline
T select(bool m, T a_true, T b_false) noexcept { return m ? a_true : b_false; }
// Conditional add: For all vector elements i: result[i] = m[i] ? (a[i] + b[i]) : a[i]
template<typename T> constexpr __forceinline
T if_add(bool m, T a, T b) noexcept { return select(m, a + b, a); }
// Conditional sub: For all vector elements i: result[i] = m[i] ? (a[i] - b[i]) : a[i]
template<typename T> constexpr __forceinline
T if_sub(bool m, T a, T b) noexcept { return select(m, a - b, a); }
// Conditional mul: For all vector elements i: result[i] = m[i] ? (a[i] * b[i]) : a[i]
template<typename T> constexpr __forceinline
T if_mul(bool m, T a, T b) noexcept { return select(m, a * b, a); }
constexpr bool same_value(double x, double y) noexcept { return std::bit_cast<int64_t>(x) == std::bit_cast<int64_t>(y); }
constexpr bool same_value(float x, float y) noexcept { return std::bit_cast<int32_t>(x) == std::bit_cast<int32_t>(y); }
/// abs(x) computes absolute value of an integral value |x|
constexpr float fabsf(float x) { return std::bit_cast<float>(0x7fffffffU & std::bit_cast<uint32_t>(x)); }
/// abs(x) computes absolute value of an integral value |x|
constexpr float fabs(float x) { return std::bit_cast<float>(0x7fffffffU & std::bit_cast<uint32_t>(x)); }
/// abs(x) computes absolute value of an integral value |x|
constexpr double fabs(double x) { return std::bit_cast<double>(INT64_C(0x7fffffffffffffff) & std::bit_cast<uint64_t>(x)); }
/// abs(x) |x|
template<typename T> __forceinline constexpr
T abs(T d) {
if constexpr (std::is_unsigned_v<T>) { return d; }
else { return (d >= T(0)) ? d : -d; }
}
/// Number of zeros leading the binary representation of `x`.
/// leading_zeros(1) == 31
template<typename U, typename = std::enable_if_t<std::is_integral_v<U>>>
constexpr U leading_zeros(U x) noexcept { return std::countl_zero(x); }
/// Number of zeros trailing the binary representation of `x`.
/// trailing_zeros(2) == 1
template<typename U, typename = std::enable_if_t<std::is_integral_v<U>>>
constexpr U trailing_zeros(U x) noexcept { return std::countr_zero(x); }
/// Number of ones leading the binary representation of `x`.
/// leading_ones(uint32_t(ipow(2,32) - 2)) == 31
template<typename U, typename = std::enable_if_t<std::is_integral_v<U>>>
constexpr U leading_ones(U x) noexcept { return std::countl_one(x); }
/// Number of ones trailing the binary representation of `x`.
/// trailing_ones(3u) == 2
template<typename U, typename = std::enable_if_t<std::is_integral_v<U>>>
constexpr U trailing_ones(U x) noexcept { return std::countr_one(x); }
/// Number of ones in the binary representation of `x`.
/// count_ones(111) == 6
template<typename U, typename = std::enable_if_t<std::is_integral_v<U>>>
constexpr U count_ones(U x) noexcept { return std::popcount(x); }
/// Checks if two 32-bit floats are equal by computing their ULPs difference.
constexpr bool almost_equal_ulps(float x, float y, int32_t max_ulps_diff = 2) noexcept {
const int32_t ix = std::bit_cast<int32_t>(x);
const int32_t iy = std::bit_cast<int32_t>(y);
if ((ix < 0) != (iy < 0)) { // In case the sign is different we still need to check if the floats were equal to make sure -0 is equal to +0.
return (x == y);
} else { return cx::abs(ix - iy) < max_ulps_diff; }
}
/// Checks if two 64-bit floats are equal by computing their ULPs difference.
constexpr bool almost_equal_ulps(double x, double y, int64_t max_ulps_diff = 2) noexcept {
const int64_t ix = std::bit_cast<int64_t>(x);
const int64_t iy = std::bit_cast<int64_t>(y);
if ((ix < 0) != (iy < 0)) { // In case the sign is different we still need to check if the floats were equal to make sure -0 is equal to +0.
return (x == y);
} else { return cx::abs(ix - iy) < max_ulps_diff; }
}
/// eps(x) == max(x-prevfloat(x), nextfloat(x)-x)
__forceinline constexpr float eps(float x) noexcept { //TODO: do not work for std::numeric_limits<float>::max(), look to ulp(float)
return cx::abs(std::bit_cast<float>(std::bit_cast<int32_t>(x) + 1) - x);
}
/// eps(x) == max(x-prevfloat(x), nextfloat(x)-x)
__forceinline constexpr double eps(double x) noexcept { //TODO: do not work for std::numeric_limits<double>::max(), look to ulp(float)
return cx::abs(std::bit_cast<double>(std::bit_cast<int64_t>(x) + 1) - x);
}
/// sign(x) returns -1.0 if x is less than 0.0, 0.0 if x is equal to 0.0, and +1.0 if x is greater than 0.0.
__forceinline constexpr float sign(float x) noexcept { return x < 0.0f ? -1.0f : (x == 0.0f ? 0.0f : 1.0f); }
/// sign(x) returns -1.0 if x is less than 0.0, 0.0 if x is equal to 0.0, and +1.0 if x is greater than 0.0.
__forceinline constexpr double sign(double x) noexcept { return x < 0.0 ? -1.0 : (x == 0.0 ? 0.0 : 1.0); }
/// sign(x) returns -1.0 if x is less than 0.0, 0.0 if x is equal to 0.0, and +1.0 if x is greater than 0.0.
template<typename T> __forceinline constexpr
T sign(T x) noexcept { return x < T(0) ? T(-1) : (x == T(0) ? T(0) : T(1)); }
/// Composes a floating point value with the magnitude of mag and the sign of sgn.
__forceinline constexpr float copysign(float mag, float sgn) noexcept {
return std::bit_cast<float>((std::bit_cast<int32_t>(mag) & ~(1 << 31)) ^ (std::bit_cast<int32_t>(sgn) & (1 << 31)));
}
/// Create value with given magnitude, copying sign of second value.
__forceinline constexpr float copysignf(float mag, float sign) { return cx::copysign(mag, sign); }
/// Composes a floating point value with the magnitude of mag and the sign of sgn.
__forceinline constexpr double copysign(double mag, double sgn) noexcept {
return std::bit_cast<double>((std::bit_cast<int64_t>(mag) & ~(INT64_C(1) << 63)) ^ (std::bit_cast<int64_t>(sgn) & (INT64_C(1) << 63)));
}
/// min(a,b)
template<typename T> __forceinline constexpr
T min(const T& a, const T& b) noexcept { return a < b ? a : b; }
/// min(a,b,...)
template<typename T, typename... Ts> __forceinline constexpr
T min(const T& a, const T& b, const Ts &... ts) noexcept { return min(min(a, b), ts...); }
/// max(a,b)
template<typename T> __forceinline constexpr
T max(const T& a, const T& b) noexcept { return a > b ? a : b; }
/// max(a,b,...)
template<typename T, typename... Ts> __forceinline constexpr
T max(const T& a, const T& b, const Ts &... ts) noexcept { return max(max(a, b), ts...); }
/// a && b && ...
template <typename... Ts> __forceinline constexpr
auto and_all(const Ts& ... n) noexcept { return (n && ... ); }
/// a || b || ...
template <typename... Ts> __forceinline constexpr
auto or_all(const Ts& ... n) noexcept { return (n || ... ); }
/// Sum Σ
template <typename... Ts> __forceinline constexpr
auto sum(const Ts& ... n) noexcept { return (n + ... ); }
/// Product Π
template <typename... Ts> __forceinline constexpr
auto product(const Ts& ... n) noexcept { return (n * ... ); }
template<typename... Ts>
constexpr auto mean(Ts... args) noexcept {
return sum(args...) / sizeof...(args);
}
/// Greatest common (positive) divisor (or zero if all arguments are zero).
// https://en.wikipedia.org/wiki/Binary_GCD_algorithm
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>> >
constexpr T gcd(T a, T b) noexcept {
using U = std::make_unsigned_t<T>;
if (a == 0) return cx::abs(b);
if (b == 0) return cx::abs(a);
const U za = trailing_zeros(U(a));
const U zb = trailing_zeros(U(b));
const U k = cx::min(za, zb);
U u = U(cx::abs(a >> za));
U v = U(cx::abs(b >> zb));
while (u != v) {
if (u > v) { std::swap(u, v); }
v -= u;
v >>= trailing_zeros(v);
}
return u << k;
}
/// Greatest common (positive) divisor (or zero if all arguments are zero).
template<typename T, typename... Ts> constexpr
T gcd(const T& a, const T& b, const Ts &... ts) noexcept { return gcd(gcd(a, b), ts...); }
/// Least common (positive) multiple (or zero if any argument is zero).
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>> >
constexpr T lcm(T a, T b) noexcept {
if ((a == 0) || (b == 0)) return 0;
const T pa = abs(a);
const T pb = abs(b);
return (pa / gcd(pa, pb)) * pb;
}
/// Least common (positive) multiple (or zero if any argument is zero).
template<typename T, typename... Ts> constexpr
T lcm(const T& a, const T& b, const Ts &... ts) noexcept { return lcm(lcm(a, b), ts...); }
/// Arithmetic geometric mean
// https://mathworld.wolfram.com/Arithmetic-GeometricMean.html
template<typename T/*, typename = std::enable_if_t<std::is_integral_v<T>>*/ >
constexpr T agm(T a, T b) noexcept {
const T epsilon = eps(a);
const T one_over_two = T(1) / T(2);
T a1, b1;
while (abs(a-b) > epsilon) {
a1 = (a + b) * one_over_two;
b1 = sqrt(a * b);
a = a1;
b = b1;
}
return (a + b) * one_over_two;
}
/// approximation of nth fibonacci number phi^n / sqrt(5)
constexpr float fibonacci(float n) noexcept {
return cx::round(cx::pow(cx::PHI<float>, n) * cx::ONE_OVER_SQRT5<float>);
}
/// approximation of nth fibonacci number phi^n / sqrt(5)
constexpr double fibonacci(double n) noexcept {
return cx::round(cx::pow(cx::PHI<double>, n) * cx::ONE_OVER_SQRT5<float>);
}
namespace detail {
// https://www.nayuki.io/page/fast-fibonacci-algorithms
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>> >
constexpr auto _fibonacci(T n) noexcept {
struct fib_res { T fn; T fnp1; };
if (n == 0) {
return fib_res{ 0, 1 };
}
else {
auto [a, b] = _fibonacci(n / 2);
const T c = a * (b * 2 - a);
const T d = a * a + b * b;
if (n % 2 == 0) { // is_even(n)
return fib_res{ c, d };
}
else {
return fib_res{ d, c + d };
}
}
}
} // namespace detail
/// nth fibonacci number for integers !
///NOTE: work correctly only up to fibonacci(93) if T is utin64_t!
template<typename T = uint64_t, typename = std::enable_if_t<std::is_integral_v<T>> >
constexpr T fibonacci(T n) noexcept { return detail::_fibonacci(n).fn; }
/// factorial of a non-negative integer n, denoted by n!
// n! = n * (n-1) * (n-2) * ... * 1 (https://en.wikipedia.org/wiki/Factorial)
// maximal n is 170
constexpr double factorial(const int n) noexcept {
// assert(n <= 170)
double answer = 1;
for (int i = 1; i <= n; i++) { answer *= i; }
return answer;
}
/// double factorial or semifactorial of a number n, denoted by n!!
// n!! = n * (n-2) * (n-4) ... (https://en.wikipedia.org/wiki/Double_factorial)
// maximal n is 300
constexpr double double_factorial(const int n) noexcept {
// assert(n <= 300)
double answer = 1;
for (int i = n; i > 0; i -= 2) { answer *= i; }
return answer;
}
namespace detail {
constexpr auto _bit_width(uint64_t n) noexcept { return n == 0 ? 1 : 64 - std::countl_zero(n); }
// http://projecteuclid.org/euclid.rmjm/1181070157
constexpr uint64_t estimate_num_primes(uint64_t lo, uint64_t hi) noexcept {
return 5 + uint64_t(cx::floor( hi / (cx::log(double(hi)) - 1.12) - lo / (cx::log(double(lo)) - 1.12 * (lo > 7))));
}
//TODO: use _umul128 and _udiv128 here >>>
// Computes (a + b) % m, assumes a < m, b < m.
constexpr uint64_t addmod64(uint64_t a, uint64_t b, uint64_t m) noexcept {
if (b >= m - a) return a - m + b;
return a + b;
}
// Computes (a*b) % m safely, considering overflow. Requires b < m;
constexpr uint64_t mulmod64(uint64_t a, uint64_t b, uint64_t m) noexcept {
if (a == 0) return b;
if (b <= std::numeric_limits<uint64_t>::max() / a) return (a * b) % m;
uint64_t res = 0;
while (a != 0) {
if (a & 1) res = addmod64(res, b, m);
a >>= 1;
b = addmod64(b, b, m);
}
return res;
}
// Compute x^p mod m
constexpr uint64_t powermod64(uint64_t b, uint64_t e, uint64_t m) noexcept {
uint64_t r = 1;
b %= m;
while (e) {
if (e % 2 == 1) r = mulmod64(r, b, m); // r = r*b % m
b = mulmod64(b, b, m); // b = b*b % m
e >>= 1;
}
return r;
}
// Compute x^p mod m
constexpr uint32_t powermod32(uint32_t a, uint32_t b, uint32_t n) noexcept {
uint64_t d = 1, A = a;
do {
if (b & 1) d = (d * A) % n;
A = (A * A) % n;
} while (b >>= 1);
return (uint32_t)d;
}
// a*a % n
constexpr uint32_t square_modulo32(uint32_t a, uint32_t n) noexcept {
return (uint32_t)(((uint64_t)a * a) % n);
}
} // namespace detail
/// is_prime_slow(n) Returns `true` if `n` is prime, and `false` otherwise. /// O(sqrt(n))
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
constexpr bool is_prime_slow(T num) {
bool result = true;
if (num <= 1) { return false; }
else if (num == 2 || num == 3) { return true; }
else if ((num % 2) == 0 || num % 3 == 0) { return false; }
else {
for (T i = 5; (i * i) <= (num); i = (i + 6)) {
if ((num % i) == 0 || (num % (i + 2) == 0)) {
result = false;
break;
}
}
}
return (result);
}
/// is_prime(n) Returns `true` if `n` is prime, and `false` otherwise.
// https://cp-algorithms.com/algebra/primality_tests.html
// It's also possible to do the check with only 7 bases: 2, 325, 9375, 28178, 450775, 9780504 and 1795265022.
// However, since these numbers (except 2) are not prime, you need to check additionally if the number you are checking is equal
// to any prime divisor of those bases: 2, 3, 5, 13, 19, 73, 193, 407521, 299210837.
template<typename T = uint64_t, typename = std::enable_if_t<std::is_integral_v<T>> >
constexpr T is_prime(T n) noexcept {
constexpr T small_primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 73, 193, 407521, 299210837 }; // 16 primes
for (auto sp : small_primes) {
if (n % sp == 0) { return n == sp; }
}
if (n < 37 * 37) { return n > 1; }
const auto s = std::countr_zero(uint64_t(n - 1)); // trailing_zeros(uint64_t(n - 1))
const auto d = (n - 1) >> s;
if constexpr (sizeof(T) == 8) { // 64 bit
constexpr T witnesses[] = { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 }; // 7 witnesses from http://miller-rabin.appspot.com/
for (const auto& a : witnesses) {
T x = detail::powermod64(a, d, n); // x = (a^d) % n;
if (x == 1) continue;
auto t = s;
while (x != n - 1) {
if ((t -= 1) <= 0) return false;
x = detail::mulmod64(x, x, n); // x = (x*x) % n;
if (x == 1) return false;
}
}
}
else {
// if n < 4,759,123,141, it is enough to test a = 2, 7, and 61;
constexpr T witnesses[] = { 2, 7, 61 }; // 3 witnesses
for (const auto& a : witnesses) {
T x = detail::powermod32(a, d, n); // x = (a^d) % n;
if (x == 1) continue;
auto t = s;
while (x != n - 1) {
if ((t -= 1) <= 0) return false;
x = detail::square_modulo32(x, n); // x = (x*x) % n;
if (x == 1) return false;
}
}
}
return true;
}
template <typename... Ts> constexpr
auto all_primes(const Ts& ... n) noexcept { return (is_prime(n) && ... ); }
/// clamp(value, low, high) Returns v clamped to the range [low,high]
template <typename T> __forceinline constexpr
T clamp(const T& v, const T& low, const T& high) noexcept { return max(min(v, high), low); }
/// x^2, x²
template<typename T> __forceinline constexpr
T sqr(T val) noexcept { return val * val; }
/// x^3 x³
template<typename T> __forceinline constexpr
T cube(T val) noexcept { return val * val * val; }
/// √x, isqrt(x)
template<typename U, typename = std::enable_if_t<std::is_integral_v<U>> >
constexpr U isqrt(U n) noexcept {
auto shift{ detail::_bit_width(n) }; // auto shift{ n == 0 ? 1 : (sizeof(U) * 8) - std::countl_zero(n) };
shift += shift & 1; // round up to next multiple of 2
U result = 0;
do { // less that 32 iterations for 64 bit int
shift -= 2;
result <<= 1; // left-shift the result to make the next guess
result |= 1; // guess that the next bit is 1
result ^= result * result > (n >> shift); // revert if guess too high
} while (shift != 0);
return result;
}
/// xʸ, x^y, x to the power of y
//TODO: prevent integral overflow !
template <class T0, class T1, typename = std::enable_if_t<std::is_integral_v<T1>>>
__forceinline constexpr T0 ipow(const T0& t0, const T1& t1) {
static_assert(std::is_integral<T1>::value, "second argument must be an integer");
T0 a = t0;
T1 b = t1;
bool const recip = b < 0;
T0 r{ static_cast<T0>(1) };
while (1) {
if (b & 1) { r *= a; }
b /= 2;
if (b == 0) { break; }
a *= a;
}
return recip ? 1 / r : r;
}
/// Quick test for whether an integer is a power of 2.
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>> >
__forceinline constexpr bool is_pow2(T x) noexcept {
// The principle is that x is a power of 2 <=> x == 1<<b <=> x-1 is all 1 bits for bits < b.
return (x & (x - 1)) == 0 && (x >= 0);
}
/// Quick test for whether an integer is a power of 2.
template <typename... Ts> constexpr
bool all_pow2(const Ts& ... n) noexcept { return (is_pow2(n) && ...); }
/// 1,3,5... are odd
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>> >
__forceinline constexpr bool is_odd(T x) noexcept { return x % 2; }
/// 1,3,5... are odd
template <typename... Ts> constexpr
bool all_odd(const Ts& ... n) noexcept { return (is_odd(n) && ...); }
/// 0,2,4... are even
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>> >
__forceinline constexpr bool is_even(T x) noexcept { return !(x % 2); }
/// 0,2,4... are even
template <typename... Ts> constexpr
bool all_even(const Ts& ... n) noexcept { return (is_even(n) && ...); }
/// saturate(x) Returns x saturated to the range [0,1] as follows:
__forceinline constexpr float saturate(float x) noexcept { return max(0.0f, min(1.0f, x)); }
/// saturate(x) Returns x saturated to the range [0,1] as follows:
__forceinline constexpr double saturate(double x) noexcept { return max(0.0, min(1.0, x)); }
/// saturate(x) Returns x saturated to the range [0,1] as follows:
template <typename T> __forceinline constexpr
T saturate(T x) noexcept { return max(T(0), min(T(1), x)); }
constexpr float radians(float a) noexcept { return a * float(PI<double> / 180.0); }
constexpr double radians(double a) noexcept { return a * PI<double> / 180.0; }
constexpr float degrees(float a) noexcept { return a * float(180.0 / PI<double>); }
constexpr double degrees(double a) noexcept { return a * 180.0 / PI<double>; }
/// step
__forceinline constexpr float step(float edge, float x) noexcept { return (x >= edge) ? 1.0f : 0.0f; }
/// step
__forceinline constexpr double step(double edge, double x) noexcept { return (x >= edge) ? 1.0 : 0.0; }
/// perform Hermite interpolation between a and b.
template <typename T> __forceinline constexpr
T smoothstep(T a, T b, T u) {
auto t = clamp((u - a) / (b - a), T(0.0), T(1.0));
return t * t * (T(3.0) - T(2.0) * t);
}
/// https://www.iquilezles.org/www/articles/ismoothstep/ismoothstep.htm
template <typename T> __forceinline constexpr
T inverse_smoothstep(T y ) {
return T(0.5) - sin(asin(T(1.0)-T(2.0)*y)/T(3.0)); //TODO: !
}
template <typename T> __forceinline constexpr
T bias(T a, T bias) {
return a / ((1 / bias - 2) * (1 - a) + 1); //TODO: !
}
template <typename T> __forceinline constexpr
T gain(T a, T gain) {
return (a < T(0.5)) ? bias(a * 2, gain) / 2
: bias(a * 2 - 1, 1 - gain) / 2 + T(0.5); //TODO: !
}
/// mix(a,b, w) performs a linear interpolation between a and b using w to weight between them.
template <typename T> __forceinline constexpr
T mix(T a, T b, T w) noexcept { return a + w * (b - a); }
/// Computes the linear interpolation between a and b, if the parameter t is inside [0, 1].
__forceinline constexpr float lerp(float a, float b, float t) noexcept { return (1.0f - t) * a + t * b; }
/// Computes the linear interpolation between a and b, if the parameter t is inside [0, 1].
__forceinline constexpr double lerp(double a, double b, double t) noexcept { return (1.0 - t) * a + t * b; }
/// inverse lerp returns a fraction t, based on a value between a and b.
__forceinline constexpr float inverse_lerp(float a, float b, float value) noexcept { return (value - a) / (b - a); }
/// inverse lerp returns a fraction t, based on a value between a and b.
__forceinline constexpr double inverse_lerp(double a, double b, double value) noexcept { return (value - a) / (b - a); }
// TODO: optimize!
__forceinline constexpr float eerp(float a, float b, float t) noexcept { return cx::pow( a, 1.0f - t ) * cx::pow( b, t ); }
__forceinline constexpr double eerp(double a, double b, double t) noexcept { return cx::pow( a, 1.0 - t ) * cx::pow( b, t ); }
// TODO: optimize !
__forceinline constexpr float inverse_eerp(float a, float b, float value) noexcept { return cx::log( a / value ) / cx::log( a / b ); }
__forceinline constexpr double inverse_eerp(double a, double b, double value) noexcept { return cx::log( a / value ) / cx::log( a / b ); }
///
__forceinline constexpr float remap(float iMin, float iMax, float oMin, float oMax, float value) noexcept {
const float t = inverse_lerp(iMin, iMax, value);
return lerp(oMin, oMax, t);
}
///
__forceinline constexpr double remap(double iMin, double iMax, double oMin, double oMax, double value) noexcept {
const double t = inverse_lerp(iMin, iMax, value);
return lerp(oMin, oMax, t);
}
/// x==0.0f
constexpr bool is_zero(float x) noexcept { return (std::bit_cast<int32_t>(x) & 0x7FFFFFFF) == 0; }
/// x==0.0
constexpr bool is_zero(double x) noexcept { return (std::bit_cast<int64_t>(x) & 0x7FFFFFFFFFFFFFFF) == 0; }
/// all x==0.0
template <typename... Ts> constexpr
bool all_zero(const Ts& ... n) noexcept { return (is_zero(n) && ...); }
/// Determine whether argument is a NaN.
__forceinline constexpr bool isnan(float a) noexcept {
auto l = std::bit_cast<uint32_t>(a);
l &= 0x7FFFFFFF;
return l > 0x7F800000;
}
/// Determine whether argument is a NaN.
__forceinline constexpr bool isnan(double a) noexcept {
const auto l = std::bit_cast<uint64_t>(a);
return (l << 1) > 0xffe0000000000000ull;
}
__forceinline constexpr bool isinf(float x) noexcept { // duplicate
const auto l = std::bit_cast<uint32_t>(x);
return (l << 1) == uint32_t(0xFF000000);
}
/// Determine whether argument is infinite.
__forceinline constexpr bool isinf(double x) noexcept { // duplicate
const auto l = std::bit_cast<uint64_t>(x);
return (l << 1) == 0xFFE0000000000000ull;
}
__forceinline constexpr float pow2if(int q) { return std::bit_cast<float>(((int32_t)(q + 0x7f)) << 23); }
__forceinline constexpr double pow2i(int q) { return std::bit_cast<double>(((int64_t)(q + 0x3ff)) << 52); }
/// x * 2ᵉˣᵖ On binary systems (where FLT_RADIX is 2), std::scalbn is equivalent to std::ldexp.
__forceinline constexpr float scalbnf(float x, int32_t n) noexcept {
constexpr auto x1p127 = std::bit_cast<float>(0x7f000000); // 0x1p127f === 2 ^ 127
constexpr auto x1p_126 = std::bit_cast<float>(0x800000); // 0x1p-126f === 2 ^ -126
constexpr auto x1p24 = std::bit_cast<float>(0x4b800000); // 0x1p24f === 2 ^ 24
if (n > 127) {
x *= x1p127;
n -= 127;
if (n > 127) {
x *= x1p127;
n -= 127;
if (n > 127) { n = 127; }
}
}
else if (n < -126) {
x *= x1p_126 * x1p24;
n += 126 - 24;
if (n < -126) {
x *= x1p_126 * x1p24;
n += 126 - 24;
if (n < -126) { n = -126; }
}
}
return x * std::bit_cast<float>((uint32_t(0x7f + n)) << 23);
}
/// x * 2ᵉˣᵖ On binary systems (where FLT_RADIX is 2), std::scalbn is equivalent to std::ldexp.
__forceinline constexpr double scalbn(double x, int32_t n) noexcept {
constexpr auto x1p1023 = std::bit_cast<double>(0x7fe0000000000000); // 0x1p1023 === 2 ^ 1023
constexpr auto x1p53 = std::bit_cast<double>(0x4340000000000000); // 0x1p53 === 2 ^ 53
constexpr auto x1p_1022 = std::bit_cast<double>(0x0010000000000000); // 0x1p-1022 === 2 ^ (-1022)
auto y = x;
if (n > 1023) {
y *= x1p1023;
n -= 1023;
if (n > 1023) {
y *= x1p1023;
n -= 1023;
if (n > 1023) {
n = 1023;
}
}
}
else if (n < -1022) {
/* make sure final n < -53 to avoid double rounding in the subnormal range */
y *= x1p_1022 * x1p53;
n += 1022 - 53;
if (n < -1022) {
y *= x1p_1022 * x1p53;
n += 1022 - 53;
if (n < -1022) {
n = -1022;
}
}
}
return y * std::bit_cast<double>((uint64_t(0x3ff + n)) << 52);
}
#if 1
/// Multiplies a floating point value x by the number 2 raised to the n power.
__forceinline constexpr float ldexp(float x, int32_t n) {
uint32_t ex = 0x7F800000u;
uint32_t ix = std::bit_cast<uint32_t>(x);
ex &= ix; // extract old exponent;
ix = ix & ~0x7F800000u; // clear exponent
n = (n << 23) + ex;
ix |= n; // insert new exponent
return std::bit_cast<float>(ix);
}
/// Multiplies a floating point value x by the number 2 raised to the n power.
__forceinline constexpr double ldexp(double x, int32_t n) {
uint64_t ex = 0x7ff0000000000000;
uint64_t ix = std::bit_cast<uint64_t>(x);
ex &= ix;
ix = ix & ~0x7ff0000000000000; // clear exponent
const int64_t n64 = (int64_t(n) << 52) + ex;
ix |= n64; // insert new exponent
return std::bit_cast<double>(ix);
}
#else
/// ldexp
constexpr float ldexpf(float x, int exp) {
if (exp > 300) exp = 300;
if (exp < -300) exp = -300;
int e0 = exp >> 2;
if (exp < 0) e0++;
if (-50 < exp && exp < 50) e0 = 0;
int e1 = exp - (e0 << 2);
float p = pow2if(e0);
float ret = x * pow2if(e1) * p * p * p * p;
return ret;
}
/// ldexp
constexpr double ldexp(double x, int exp) {
if (exp > 2100) exp = 2100;
if (exp < -2100) exp = -2100;
int e0 = exp >> 2;
if (exp < 0) e0++;
if (-100 < exp && exp < 100) e0 = 0;
int e1 = exp - (e0 << 2);
double p = pow2i(e0);
double ret = x * pow2i(e1) * p * p * p * p;
return ret;
}
#endif
/// Return next representable double-precision floating-point value after argument x in the direction of y.
__forceinline constexpr float nextafterf(float a, float b) {
uint32_t ia = std::bit_cast<uint32_t>(a); // memcpy(&ia, &a, sizeof(float));
const uint32_t ib = std::bit_cast<uint32_t>(b); // memcpy(&ib, &b, sizeof(float));
if (isnan(a) || isnan(b)) { return a + b; } // NaN
if (((ia | ib) << 1) == 0) { return b; }
if (a == 0.0f) { return copysignf(1.401298464e-045f, b); } // crossover
if ((a < b) && (a < 0.0f)) ia--;
if ((a < b) && (a > 0.0f)) ia++;
if ((a > b) && (a < 0.0f)) ia++;
if ((a > b) && (a > 0.0f)) ia--;
a = std::bit_cast<float>(ia); // memcpy(&a, &ia, sizeof(float));
return a;
}
/// Return next representable double-precision floating-point value after argument x in the direction of y.
__forceinline constexpr double nextafter(double a, double b) {
uint64_t ia = std::bit_cast<uint64_t>(a); // memcpy(&ia, &a, sizeof(double));
const uint64_t ib = std::bit_cast<uint64_t>(b); // memcpy(&ib, &b, sizeof(double));
if (isnan(a) || isnan(b)) { return a + b; } // NaN
if (((ia | ib) << 1) == 0ULL) { return b; }
if (a == 0.0) { return copysign(4.9406564584124654e-324, b); } // crossover
if ((a < b) && (a < 0.0)) ia--;
if ((a < b) && (a > 0.0)) ia++;
if ((a > b) && (a < 0.0)) ia++;
if ((a > b) && (a > 0.0)) ia--;
a = std::bit_cast<double>(ia); // memcpy(&a, &ia, sizeof(double));
return a;
}
/// Decomposes given floating point value x into a normalized fraction and an integral power of two.
/// x = significand * 2^exponent, returns significand, pw2 is exponent.
__forceinline constexpr float frexp(float x, int32_t* pw2) {
uint32_t ex = 0x7F800000u; // exponent mask
uint32_t ix = std::bit_cast<uint32_t>(x);
ex &= ix;
ix &= ~0x7F800000u; // clear exponent
*pw2 = int32_t(ex >> 23) - 126; // compute exponent
ix |= 0x3F000000u; // insert exponent +1 in x
return std::bit_cast<float>(ix);
}
/// Decomposes given floating point value x into a normalized fraction and an integral power of two.
__forceinline constexpr double frexp(double x, int32_t* pw2) {
uint64_t ex = 0x7ff0000000000000; // exponent mask
uint64_t ix = std::bit_cast<uint64_t>(x);
ex &= ix;
ix &= ~0x7ff0000000000000; // clear exponent
*pw2 = int32_t(ex >> 52) - 1022; // compute exponent
ix |= 0x3fe0000000000000; // insert exponent +1 in x
return std::bit_cast<double>(ix);
}
#if 0
/// frexp, Get significand and exponent
constexpr float frexp(float x, int* pw2) noexcept {
uint32_t ex = 0x7F800000u; // exponent mask
uint32_t ix = std::bit_cast<uint32_t>(x);
ex &= ix;
ix &= ~0x7F800000u; // clear exponent
*pw2 = (int)(ex >> 23) - 126; // compute exponent
ix |= 0x3F000000u; // insert exponent +1 in x
return std::bit_cast<float>(ix);
}
/// frexp, Get significand and exponent
constexpr double frexp(double x, int* pw2) noexcept {
uint64_t ex = 0x7ff0000000000000; // exponent mask
uint64_t ix = std::bit_cast<uint64_t>(x);
ex &= ix;
ix &= ~0x7ff0000000000000; // clear exponent
*pw2 = (int)(ex >> 52) - 1022; // compute exponent
ix |= 0x3fe0000000000000; // insert exponent +1 in x
return std::bit_cast<double>(ix);
}
#endif
/// Rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x.
__forceinline constexpr float trunc(float x) noexcept {
float fr = x - (int32_t)x; //TODO: float int conversion !
return (isinf(x) || fabs(x) >= (float)(INT64_C(1) << 23)) ? x : copysign(x - fr, x);
}
/// Rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x.
__forceinline constexpr double trunc(double x) noexcept {
double fr = x - (double)(INT64_C(1) << 31) * (int32_t)(x * (1.0 / (INT64_C(1) << 31)));
fr = fr - (int32_t)fr;
return (isinf(x) || fabs(x) >= (double)(INT64_C(1) << 52)) ? x : copysign(x - fr, x);
}
///
__forceinline constexpr float floor(float x) noexcept {
float fr = x - (int32_t)x;
fr = fr < 0.0 ? fr + 1.0f : fr;
return (isinf(x) || fabs(x) >= (float)(INT64_C(1) << 23)) ? x : copysign(x - fr, x);
}
///
__forceinline constexpr double floor(double x) noexcept {
double fr = x - (double)(INT64_C(1) << 31) * (int32_t)(x * (1.0 / (INT64_C(1) << 31)));
fr = fr - (int32_t)fr;
fr = fr < 0.0 ? fr + 1.0 : fr;
return (isinf(x) || fabs(x) >= (double)(INT64_C(1) << 52)) ? x : copysign(x - fr, x);
}
///
__forceinline constexpr float ceil(float x) noexcept {
float fr = x - (int32_t)x;
fr = fr <= 0.0 ? fr : fr - 1.0f;
return (isinf(x) || fabs(x) >= float(INT64_C(1) << 23)) ? x : copysign(x - fr, x);
}
///
__forceinline constexpr double ceil(double x) noexcept {
double fr = x - double(INT64_C(1) << 31) * (int32_t)(x * (1.0 / (INT64_C(1) << 31)));
fr = fr - (int32_t)fr;
fr = fr <= 0.0 ? fr : fr - 1.0;
return (isinf(x) || fabs(x) >= double(INT64_C(1) << 52)) ? x : copysign(x - fr, x);
}
/// Returns the integral value that is nearest to x, with halfway cases rounded away from zero.
__forceinline constexpr double round(double d) noexcept {
double x = d + 0.5;
double fr = x - double(INT64_C(1) << 31) * (int32_t)(x * (1.0 / (INT64_C(1) << 31)));
fr = fr - (int32_t)fr;
if (fr == 0.0 && x <= 0.0) { x -= 1.0; } // x--;
fr = fr < 0.0 ? fr + 1.0 : fr;
x = d == 0.49999999999999994449 ? 0.0 : x; // nextafter(0.5, 0)
return (isinf(d) || fabs(d) >= double(INT64_C(1) << 52)) ? d : copysign(x - fr, d);
}
/// Returns the integral value that is nearest to x, with halfway cases rounded away from zero.
__forceinline constexpr float round(float d) noexcept {
float x = d + 0.5f;
float fr = x - (int32_t)x;
if (fr == 0.0f && x <= 0.0f) { x -= 1.0f; } //TODO: x--; causes compile error here !
fr = fr < 0.0f ? fr + 1.0f : fr;
x = (d == 0.4999999701976776123f) ? 0.0f : x; // nextafterf(0.5, 0)
return (isinf(d) || fabs(d) >= float(INT64_C(1) << 23)) ? d : copysign(x - fr, d);
}
/// a * b + c in one go
__forceinline constexpr float fma(float a, float b, float c) noexcept {
if (std::is_constant_evaluated()) {
CX_CLANG_PRAGMA(clang fp contract(fast))
return a * b + c;
} else {
return std::fma(a,b,c);
}
}
/// a * b + c in one go
__forceinline constexpr double fma(double a, double b, double c) noexcept {
if (std::is_constant_evaluated()) {
CX_CLANG_PRAGMA(clang fp contract(fast))
return a * b + c;
}
else {
return std::fma(a, b, c);