-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsga.hxx
7471 lines (6261 loc) · 251 KB
/
dsga.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright David Browne 2020-2025.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// https://github.com/davidbrowne/dsga
// opening include guard
#if !defined(DSGA_DSGA_HXX)
#define DSGA_DSGA_HXX
#include <limits>
#include <type_traits> // requirements
#include <concepts> // requirements
#include <cmath>
#include <bit> // bit_cast
#include <stdexcept>
#include <array> // underlying storage
#include <tuple> // tuple interface for structured bindings, variadic constructors
#include <algorithm> // min()
#include <numbers> // pi_v<>, inv_pi_v<>
#include <numeric>
//
// Data Structures for Geometric Algebra (dsga)
//
namespace dsga
{
// Copyright David Browne 2020-2025.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// version info
constexpr inline int DSGA_MAJOR_VERSION = 2;
constexpr inline int DSGA_MINOR_VERSION = 2;
constexpr inline int DSGA_PATCH_VERSION = 5;
namespace cxcm
{
// Copyright David Browne 2020-2025.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// https://github.com/davidbrowne/cxcm - cxcm
// version info
constexpr int CXCM_MAJOR_VERSION = 1;
constexpr int CXCM_MINOR_VERSION = 2;
constexpr int CXCM_PATCH_VERSION = 0;
namespace dd_real
{
// https://www.davidhbailey.com/dhbsoftware/ - QD
/*
Modified BSD 3-Clause License
This work was supported by the Director, Office of Science, Division
of Mathematical, Information, and Computational Sciences of the
U.S. Department of Energy under contract number DE-AC03-76SF00098.
Copyright (c) 2000-2007
1. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
(1) Redistributions of source code must retain the copyright notice, this list of conditions and the following disclaimer.
(2) Redistributions in binary form must reproduce the copyright notice, this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
(3) Neither the name of the University of California, Lawrence Berkeley National Laboratory, U.S. Dept. of Energy nor the names of its contributors
may be used to endorse or promote products derived from this software without specific prior written permission.
2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
3. You are under no obligation whatsoever to provide any bug fixes, patches, or upgrades to the features, functionality or performance of the
source code ("Enhancements") to anyone; however, if you choose to make your Enhancements available either publicly, or directly to Lawrence
Berkeley National Laboratory, without imposing a separate written license agreement for such Enhancements, then you hereby grant the following
license: a non-exclusive, royalty-free perpetual license to install, use, modify, prepare derivative works, incorporate into other computer
software, distribute, and sublicense such enhancements or derivative works thereof, in binary and source code form.
*/
//
// heavily modified dd_real type and support
//
// The following code computes s = fl(a+b) and error(a + b), assuming |a| >= |b|.
constexpr double quick_two_sum(double a, double b, double &error) noexcept
{
double s = a + b;
error = b - (s - a);
return s;
}
// The following code computes s = fl(a+b) and error(a + b).
constexpr double two_sum(double a, double b, double &error) noexcept
{
double s = a + b;
double v = s - a;
error = (a - (s - v)) + (b - v);
return s;
}
// The following code splits a 53-bit IEEE double precision floating number a into a high word and a low word, each with 26
// bits of significand, such that a is the sum of the high word with the low word. The high word will contain the first 26 bits,
// while the low word will contain the lower 26 bits.
constexpr void split(double a, double &high, double &low) noexcept
{
double temp = 134217729.0 * a; // 134217729.0 = 2^27 + 1
high = temp - (temp - a);
low = a - high;
}
// The following code computes fl(a x b) and error(a x b).
constexpr double two_prod(double a, double b, double &error) noexcept
{
double a_high = 0.0;
double a_low = 0.0;
double b_high = 0.0;
double b_low = 0.0;
double p = a * b;
split(a, a_high, a_low);
split(b, b_high, b_low);
error = ((a_high * b_high - p) + a_high * b_low + a_low * b_high) + a_low * b_low;
return p;
}
// higher precision double-double
struct dd_real
{
double x[2];
constexpr dd_real() noexcept : x{}
{
}
constexpr dd_real(double hi, double lo) noexcept : x{hi, lo}
{
}
explicit constexpr dd_real(double h) noexcept : x{h, 0.}
{
}
constexpr dd_real(const dd_real &) noexcept = default;
constexpr dd_real(dd_real &&) noexcept = default;
constexpr dd_real &operator =(const dd_real &) noexcept = default;
constexpr dd_real &operator =(dd_real &&) noexcept = default;
constexpr double operator [](unsigned int index) const noexcept
{
return x[index];
}
constexpr double &operator [](unsigned int index) noexcept
{
return x[index];
}
explicit constexpr operator double() const noexcept
{
return x[0];
}
explicit constexpr operator float() const noexcept
{
return static_cast<float>(x[0]);
}
};
// double-double + double-double
constexpr dd_real ieee_add(const dd_real &a, const dd_real &b) noexcept
{
// This one satisfies IEEE style error bound, due to K. Briggs and W. Kahan.
double s1 = 0.0;
double s2 = 0.0;
double t1 = 0.0;
double t2 = 0.0;
s1 = two_sum(a.x[0], b.x[0], s2);
t1 = two_sum(a.x[1], b.x[1], t2);
s2 += t1;
s1 = quick_two_sum(s1, s2, s2);
s2 += t2;
s1 = quick_two_sum(s1, s2, s2);
return dd_real(s1, s2);
}
// double-double + double
constexpr dd_real ieee_add(const dd_real &a, double b) noexcept
{
// This one satisfies IEEE style error bound, due to K. Briggs and W. Kahan.
double s1 = 0.0;
double s2 = 0.0;
s1 = two_sum(a.x[0], b, s2);
s1 = quick_two_sum(s1, s2 + a.x[1], s2);
return dd_real(s1, s2);
}
// double-double - double-double
constexpr dd_real ieee_subtract(const dd_real &a, const dd_real &b) noexcept
{
// This one satisfies IEEE style error bound, due to K. Briggs and W. Kahan.
double s1 = 0.0;
double s2 = 0.0;
double t1 = 0.0;
double t2 = 0.0;
s1 = two_sum(a.x[0], -b.x[0], s2);
t1 = two_sum(a.x[1], -b.x[1], t2);
s2 += t1;
s1 = quick_two_sum(s1, s2, s2);
s2 += t2;
s1 = quick_two_sum(s1, s2, s2);
return dd_real(s1, s2);
}
// double - double-double
constexpr dd_real ieee_subtract(double a, const dd_real &b) noexcept
{
// This one satisfies IEEE style error bound, due to K. Briggs and W. Kahan.
double s1 = 0.0;
double s2 = 0.0;
s1 = two_sum(a, -b.x[0], s2);
s1 = quick_two_sum(s1, s2 - b.x[1], s2);
return dd_real(s1, s2);
}
// double-double + double-double
constexpr dd_real operator +(const dd_real &a, const dd_real &b) noexcept
{
return ieee_add(a, b);
}
// double-double + double
constexpr dd_real operator +(const dd_real &a, double b) noexcept
{
return ieee_add(a, b);
}
constexpr dd_real operator -(const dd_real &a, const dd_real &b) noexcept
{
return ieee_subtract(a, b);
}
constexpr dd_real operator -(double a, const dd_real &b) noexcept
{
return ieee_subtract(a, b);
}
constexpr dd_real &operator -=(dd_real &a, const dd_real &b) noexcept
{
a = (a - b);
return a;
}
// double-double * double-double
constexpr dd_real operator *(const dd_real &a, const dd_real &b) noexcept
{
double p1 = 0.0;
double p2 = 0.0;
p1 = two_prod(a.x[0], b.x[0], p2);
p2 += (a.x[0] * b.x[1] + a.x[1] * b.x[0]);
p1 = quick_two_sum(p1, p2, p2);
return dd_real(p1, p2);
}
// double-double * double
constexpr dd_real operator *(const dd_real &a, double b) noexcept
{
double p1 = 0.0;
double p2 = 0.0;
p1 = two_prod(a.x[0], b, p2);
p1 = quick_two_sum(p1, p2 + (a.x[1] * b), p2);
return dd_real(p1, p2);
}
// double * double-double
constexpr dd_real operator *(double a, const dd_real &b) noexcept
{
return (b * a);
}
constexpr dd_real &operator *=(dd_real &a, const dd_real &b) noexcept
{
double p1 = 0.0;
double p2 = 0.0;
p1 = two_prod(a.x[0], b.x[0], p2);
p2 += (a.x[0] * b.x[1] + a.x[1] * b.x[0]);
a.x[0] = quick_two_sum(p1, p2, a.x[1]);
return a;
}
constexpr dd_real accurate_div(const dd_real &a, const dd_real &b) noexcept
{
double q1 = 0.0;
double q2 = 0.0;
double q3 = 0.0;
q1 = a.x[0] / b.x[0]; // approximate quotient
dd_real r = a - q1 * b;
q2 = r.x[0] / b.x[0];
r -= (q2 * b);
q3 = r.x[0] / b.x[0];
q1 = quick_two_sum(q1, q2, q2);
double s1 = 0.0;
double s2 = 0.0;
s1 = two_sum(q1, q3, s2);
s1 = quick_two_sum(s1, s2 + q2, s2);
return dd_real(s1, s2);
}
constexpr dd_real accurate_div(double a, const dd_real &b) noexcept
{
double q1 = 0.0;
double q2 = 0.0;
double q3 = 0.0;
q1 = a / b.x[0]; // approximate quotient
dd_real r = a - q1 * b;
q2 = r.x[0] / b.x[0];
r -= (q2 * b);
q3 = r.x[0] / b.x[0];
q1 = quick_two_sum(q1, q2, q2);
double s1 = 0.0;
double s2 = 0.0;
s1 = two_sum(q1, q3, s2);
s1 = quick_two_sum(s1, s2 + q2, s2);
return dd_real(s1, s2);
}
// double / double-double
constexpr dd_real operator /(double a, const dd_real &b) noexcept
{
return accurate_div(a, b);
}
// double-double / double-double
constexpr dd_real operator /(const dd_real &a, const dd_real &b) noexcept
{
return accurate_div(a, b);
}
} // namespace dd_real
namespace concepts
{
template <typename T>
concept basic_floating_point = (std::is_same_v<float, std::remove_cvref_t<T>> || std::is_same_v<double, std::remove_cvref_t<T>>);
}
namespace limits
{
namespace detail
{
// long doubles vary between compilers and platforms. Windows MSVC and clang on Windows both use
// the same representation as double. For gcc and linux, etc., it is often represented by an extended
// precision data structure with 80 bits (64 bits of significand). sizeof(long double) on gcc on Windows
// (at least MSYS2) is 16, implying it is 128 bits, but std::numeric_limits<long double> returns values
// consistent with an 80 bit representation.
constexpr long double get_largest_fractional_long_double() noexcept
{
if constexpr (std::numeric_limits<long double>::digits == 64)
{
// if digits is 64, then long double is using extended precision, and we can
// just barely get away with casting to a long long to remove the fractional
// part and keep the rest of the bits, without overflow.
return 0x1.fffffffffffffffep+62L;
}
else
{
// assuming that long double does the same thing as double (which is true for
// MSVC and clang on windows).
return 0x1.fffffffffffffp+51L;
}
}
}
//
// largest_fractional_value
//
// the largest floating point value that has a fractional representation
template <cxcm::concepts::basic_floating_point T>
constexpr inline T largest_fractional_value = T();
template <>
constexpr inline double largest_fractional_value<double> = 0x1.fffffffffffffp+51;
template <>
constexpr inline float largest_fractional_value<float> = 0x1.fffffep+22f;
}
//
// floating-point negative zero support
//
template <cxcm::concepts::basic_floating_point T>
constexpr bool is_negative_zero(T) noexcept
{
return false;
}
template<>
constexpr bool is_negative_zero(float val) noexcept
{
return (0x80000000 == std::bit_cast<unsigned int>(val));
}
template<>
constexpr bool is_negative_zero(double val) noexcept
{
return (0x8000000000000000 == std::bit_cast<unsigned long long>(val));
}
template <cxcm::concepts::basic_floating_point T>
constexpr inline T negative_zero = T(-0);
template <>
constexpr inline float negative_zero<float> = std::bit_cast<float>(0x80000000);
template <>
constexpr inline double negative_zero<double> = std::bit_cast<double>(0x8000000000000000);
// don't worry about esoteric input.
// much faster than strict or standard when non constant evaluated,
// though standard library is a little better in debugger.
namespace relaxed
{
//
// abs(), fabs()
//
// absolute value
template <cxcm::concepts::basic_floating_point T>
constexpr T abs(T value) noexcept
{
return (value < T(0)) ? -value : value;
}
// undefined behavior if value is std::numeric_limits<T>::min()
template <std::signed_integral T>
constexpr T abs(T value) noexcept
{
return (value < T(0)) ? -value : value;
}
template <std::unsigned_integral T>
constexpr T abs(T value) noexcept
{
return value;
}
template <cxcm::concepts::basic_floating_point T>
constexpr T fabs(T value) noexcept
{
return abs(value);
}
template <std::integral T>
constexpr double fabs(T value) noexcept
{
return abs(value);
}
//
// trunc()
//
// this is the workhorse function for floor(), ceil(), and round().
// rounds towards zero
template <cxcm::concepts::basic_floating_point T>
constexpr T trunc(T value) noexcept
{
return static_cast<T>(static_cast<long long>(value));
}
// float specialization
template <>
constexpr float trunc(float value) noexcept
{
return static_cast<float>(static_cast<int>(value));
}
//
// floor()
//
// rounds towards negative infinity
template <cxcm::concepts::basic_floating_point T>
constexpr T floor(T value) noexcept
{
const T truncated_value = trunc(value);
// truncation rounds to zero which is right direction for positive values,
// but we need to go the other way for negative values.
// negative non-integral value
if (truncated_value > value)
return (truncated_value - T(1.0f));
// positive or integral value
return truncated_value;
}
//
// ceil()
//
// rounds towards positive infinity
template <cxcm::concepts::basic_floating_point T>
constexpr T ceil(T value) noexcept
{
const T truncated_value = trunc(value);
// truncation rounds to zero which is right direction for negative values,
// but we need to go the other way for positive values.
// positive non-integral value
if (truncated_value < value)
return (truncated_value + T(1.0f));
// negative or integral value
return truncated_value;
}
//
// round()
//
// rounds to nearest integral position, halfway cases away from zero
template <cxcm::concepts::basic_floating_point T>
constexpr T round(T value) noexcept
{
// zero could be handled either place, but here it is with the negative values.
// positive value, taking care of halfway case.
if (value > T(0))
return trunc(value + T(0.5f));
// negative or zero value, taking care of halfway case.
return trunc(value - T(0.5f));
}
//
// fract() - not in standard library
//
// the fractional part of a floating point number - always non-negative.
template <cxcm::concepts::basic_floating_point T>
constexpr T fract(T value) noexcept
{
return value - floor(value);
}
//
// fmod()
//
// the floating point remainder of division
template <cxcm::concepts::basic_floating_point T>
constexpr T fmod(T x, T y) noexcept
{
return x - trunc(x / y) * y;
}
//
// round_even() - not in standard library
//
// rounds to nearest integral position, halfway cases towards even
template <cxcm::concepts::basic_floating_point T>
constexpr T round_even(T value) noexcept
{
T trunc_value = trunc(value);
bool is_even = (fmod(trunc_value, T(2)) == T(0));
bool is_halfway = (fract(value) == T(0.5));
// the special case
if (is_halfway && is_even)
return trunc_value;
// zero could be handled either place, but here it is with the negative values.
// positive value, taking care of halfway case.
if (value > T(0))
return trunc(value + T(0.5f));
// negative or zero value, taking care of halfway case.
return trunc(value - T(0.5f));
}
//
// sqrt()
//
namespace detail
{
// "Improving the Accuracy of the Fast Inverse Square Root by Modifying Newton-Raphson Corrections" 2021
// https://www.mdpi.com/1099-4300/23/1/86
//
// in comparison to inverse_sqrt(double), this method gives pretty good results:
// 0 ulps: ~68.58%
// 1 ulps: ~31.00%
// 2 ulps: ~0.42%
//
// depending on compiler/platform, this may not be faster than rsqrt()
constexpr double fast_rsqrt(double x) noexcept
{
double halfx = 0.5 * x;
long long i = std::bit_cast<long long>(x);
i = 0x5FE6ED2102DCBFDA - (i >> 1);
double y = std::bit_cast<double>(i);
y *= 1.50087895511633457 - halfx * y * y;
y *= 1.50000057967625766 - halfx * y * y;
y *= 1.5000000000002520 - halfx * y * y;
y *= 1.5000000000000000 - halfx * y * y;
return y;
}
// float uses double internally, double uses dd_real internally
template <cxcm::concepts::basic_floating_point T>
constexpr T converging_sqrt(T arg) noexcept
{
const double boosted_arg = arg;
double init_value = boosted_arg * fast_rsqrt(boosted_arg);
if constexpr (std::is_same_v<T, double>)
{
// boosted_arg doesn't need to be a dd_real for [T = double]
auto current_value = dd_real::dd_real(init_value);
auto previous_value = dd_real::dd_real(0.0);
while ((current_value[0] != previous_value[0]) && (current_value[0] * current_value[0] != boosted_arg))
{
previous_value = current_value;
current_value = 0.5 * (current_value + (boosted_arg / current_value));
}
return static_cast<double>(current_value);
}
else if constexpr (std::is_same_v<T, float>)
{
double current_value = init_value;
double previous_value = 0.0;
while ((current_value != previous_value) && (current_value * current_value != boosted_arg))
{
previous_value = current_value;
current_value = 0.5 * (current_value + (boosted_arg / current_value));
}
return static_cast<float>(current_value);
}
}
// float uses double internally, double uses dd_real internally
template <cxcm::concepts::basic_floating_point T>
constexpr T inverse_sqrt(T arg) noexcept
{
// don't need this to be a dd_real
const double boosted_arg = arg;
if constexpr (std::is_same_v<T, double>)
{
// arg is already a double
auto current_value = dd_real::dd_real(fast_rsqrt(arg));
current_value *= (1.5 - ((0.5 * boosted_arg) * (current_value * current_value)));
return static_cast<double>(current_value);
}
else if constexpr (std::is_same_v<T, float>)
{
double current_value = fast_rsqrt(boosted_arg);
current_value *= (1.5 - (0.5 * boosted_arg * current_value * current_value));
// do a couple more refinements for floating point (this needs testing to see if necessary)
current_value *= (1.5 - (0.5 * boosted_arg * current_value * current_value));
current_value *= (1.5 - (0.5 * boosted_arg * current_value * current_value));
return static_cast<float>(current_value);
}
}
}
// constexpr square root, uses higher precision behind the scenes
template <cxcm::concepts::basic_floating_point T>
constexpr T sqrt(T value) noexcept
{
return detail::converging_sqrt(value);
}
// reciprocal of square root, uses higher precision behind the scenes
template <cxcm::concepts::basic_floating_point T>
constexpr T rsqrt(T value) noexcept
{
return detail::inverse_sqrt(value);
}
// fast reciprocal of square root
template <cxcm::concepts::basic_floating_point T>
constexpr T fast_rsqrt(T value) noexcept
{
return static_cast<T>(detail::fast_rsqrt(static_cast<double>(value)));
}
} // namespace relaxed
//
// isnan()
//
// make sure this isn't optimized away if used with fast-math
#if defined(_MSC_VER) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
#pragma float_control(precise, on, push)
#endif
template <cxcm::concepts::basic_floating_point T>
#if defined(__GNUC__) && !defined(__clang__)
__attribute__((optimize("-fno-fast-math")))
#endif
constexpr bool isnan(T value) noexcept
{
return (value != value);
}
#if defined(_MSC_VER) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
#pragma float_control(pop)
#endif
template <std::integral T>
constexpr bool isnan(T value) noexcept
{
return isnan(static_cast<double>(value));
}
//
// isinf()
//
// make sure this isn't optimized away if used with fast-math
#if defined(_MSC_VER) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
#pragma float_control(precise, on, push)
#endif
template <cxcm::concepts::basic_floating_point T>
#if defined(__GNUC__) && !defined(__clang__)
__attribute__((optimize("-fno-fast-math")))
#endif
constexpr bool isinf(T value) noexcept
{
return (value == -std::numeric_limits<T>::infinity()) || (value == std::numeric_limits<T>::infinity());
}
#if defined(_MSC_VER) || defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
#pragma float_control(pop)
#endif
template <std::integral T>
constexpr bool isinf(T value) noexcept
{
return isinf(static_cast<double>(value));
}
//
// fpclassify()
//
template <cxcm::concepts::basic_floating_point T>
constexpr int fpclassify(T value) noexcept
{
if (isnan(value))
return FP_NAN;
else if (isinf(value))
return FP_INFINITE;
else if (value == 0) // intentional use of the implicit cast of 0 to T.
return FP_ZERO;
else if (relaxed::abs(value) < std::numeric_limits<T>::min())
return FP_SUBNORMAL;
return FP_NORMAL;
}
template <std::integral T>
constexpr int fpclassify(T value) noexcept
{
return fpclassify(static_cast<double>(value));
}
//
// isnormal()
//
template <cxcm::concepts::basic_floating_point T>
constexpr bool isnormal(T value) noexcept
{
return (fpclassify(value) == FP_NORMAL);
}
template <std::integral T>
constexpr bool isnormal(T value) noexcept
{
return isnormal(static_cast<double>(value));
}
//
// isfinite()
//
template <cxcm::concepts::basic_floating_point T>
constexpr bool isfinite(T value) noexcept
{
return !isnan(value) && !isinf(value);
}
template <std::integral T>
constexpr bool isfinite(T value) noexcept
{
return isfinite(static_cast<double>(value));
}
//
// signbit()
//
// +0 returns false and -0 returns true
template <cxcm::concepts::basic_floating_point T>
constexpr bool signbit(T value) noexcept
{
if constexpr (sizeof(T) == 4)
{
unsigned int bits = std::bit_cast<unsigned int>(value);
return (bits & 0x80000000) != 0;
}
else if constexpr (sizeof(T) == 8)
{
unsigned long long bits = std::bit_cast<unsigned long long>(value);
return (bits & 0x8000000000000000) != 0;
}
}
template <std::integral T>
constexpr bool signbit(T value) noexcept
{
return signbit(static_cast<double>(value));
}
//
// copysign()
//
// +0 or -0 for sign is considered as *not* negative
template <cxcm::concepts::basic_floating_point T>
constexpr T copysign(T value, T sgn) noexcept
{
// +0 or -0 for sign is considered as *not* negative
bool is_neg = signbit(sgn);
if constexpr (sizeof(T) == 4)
{
unsigned int bits = std::bit_cast<unsigned int>(value);
if (is_neg)
bits |= 0x80000000;
else
bits &= 0x7FFFFFFF;
return std::bit_cast<T>(bits);
}
else if constexpr (sizeof(T) == 8)
{
unsigned long long bits = std::bit_cast<unsigned long long>(value);
if (is_neg)
bits |= 0x8000000000000000;
else
bits &= 0x7FFFFFFFFFFFFFFF;
return std::bit_cast<T>(bits);
}
}
template <std::integral T>
constexpr double copysign(T value, T sgn) noexcept
{
return copysign(static_cast<double>(value), static_cast<double>(sgn));
}
// try and match standard library requirements.
// this namespace is pulled into parent namespace via inline.
inline namespace strict
{
namespace detail
{
//
// make_nan_quiet()
//
// make a NaN into a quiet NaN - if input is not a NaN, it is returned unchanged
template <cxcm::concepts::basic_floating_point T>
constexpr T convert_to_quiet_nan(T value) noexcept
{
if (cxcm::isnan(value))
{
if constexpr (sizeof(T) == 4)
{
unsigned int bits = std::bit_cast<unsigned int>(value);
// set the is_quiet bit
bits |= 0x00400000;
return std::bit_cast<T>(bits);
}
else if constexpr (sizeof(T) == 8)
{
unsigned long long bits = std::bit_cast<unsigned long long>(value);
// set the is_quiet bit
bits |= 0x0008000000000000;
return std::bit_cast<T>(bits);
}
}
return value;
}
//
// isnormal_or_subnormal()
//
// standard library screening requirement for these functions
template <cxcm::concepts::basic_floating_point T>
constexpr bool isnormal_or_subnormal(T value) noexcept
{
// intentional use of the implicit cast of 0 to T.
return isfinite(value) && (value != 0);
}
//
// fails_fractional_input_constraints()
//
// the fractional functions,e.g., trunc(), floor(), ceil(), round(), need the input to satisfy
// certain constraints before it further processes the input. if this function returns true,
// the constraints weren't met, and the fractional functions will do no further work and return
// the value as is.
template <cxcm::concepts::basic_floating_point T>
constexpr bool fails_fractional_input_constraints(T value) noexcept
{
// if any of the following constraints are not met, return true: