-
Notifications
You must be signed in to change notification settings - Fork 47
/
complex
executable file
·2319 lines (1918 loc) · 83.8 KB
/
complex
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
// complex standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef _COMPLEX_
#define _COMPLEX_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <cmath>
#include <cstdint>
#include <limits>
#include <sstream>
#include <type_traits>
#include <xutility>
#include <ymath.h>
#ifdef _M_CEE_PURE
// no intrinsics for /clr:pure
#elif defined(_M_ARM64) || defined(_M_ARM64EC)
// https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#base-requirements
// Both floating-point and NEON support are presumed to be present in hardware.
#define _FMP_USING_STD_FMA
#elif defined(__clang__) // ^^^ defined(_M_ARM64) || defined(_M_ARM64EC) ^^^
// TRANSITION, not using x86/x64 FMA intrinsics for Clang yet
#elif defined(_M_IX86) || defined(_M_X64)
#define _FMP_USING_X86_X64_INTRINSICS
#include <__msvc_bit_utils.hpp>
#include <emmintrin.h>
extern "C" __m128d __cdecl _mm_fmsub_sd(__m128d, __m128d, __m128d);
#endif // ^^^ defined(_M_IX86) || defined(_M_X64) ^^^
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
#ifndef _C_COMPLEX_T
#define _C_COMPLEX_T // Also defined by UCRT <complex.h>
struct _C_double_complex {
double _Val[2];
};
struct _C_float_complex {
float _Val[2];
};
struct _C_ldouble_complex {
long double _Val[2];
};
#endif // _C_COMPLEX_T
// complex _Val offsets
#define _RE 0
#define _IM 1
_STD_BEGIN
// implements multi-precision floating-point arithmetic for numerical algorithms
#pragma float_control(precise, on, push)
namespace _Float_multi_prec {
// multi-precision floating-point types
template <class _Ty, int _Prec>
struct _Fmp_t;
template <class _Ty>
struct _Fmp_t<_Ty, 2> {
static_assert(is_floating_point_v<_Ty>, "_Ty must be floating-point");
_Ty _Val0; // most significant numeric_limits<_Ty>::precision bits
_Ty _Val1; // least significant numeric_limits<_Ty>::precision bits
};
// addition
// 1x precision + 1x precision -> 2x precision
// the result is exact when:
// 1) no internal overflow occurs
// 2) either underflow is gradual, or no internal underflow occurs
// 3) intermediate precision is either the same as _Ty, or greater than twice the precision of _Ty
// 4) parameters and local variables do not retain extra intermediate precision
// 5) rounding mode is rounding to nearest
// violation of condition 3 or 5 could lead to relative error on the order of epsilon^2
// violation of other conditions could lead to worse results
template <class _Ty>
_NODISCARD constexpr _Fmp_t<_Ty, 2> _Add_x2(const _Ty _Xval, const _Ty _Yval) noexcept {
const _Ty _Sum0 = _Xval + _Yval;
const _Ty _Ymod = _Sum0 - _Xval;
const _Ty _Xmod = _Sum0 - _Ymod;
const _Ty _Yerr = _Yval - _Ymod;
const _Ty _Xerr = _Xval - _Xmod;
return {_Sum0, _Xerr + _Yerr};
}
// 1x precision + 1x precision -> 2x precision
// requires: exponent(_Xval) + countr_zero(significand(_Xval)) >= exponent(_Yval) || _Xval == 0
// the result is exact when:
// 0) the requirement above is satisfied
// 1) the result doesn't overflow
// 2) either underflow is gradual, or no internal underflow occurs
// 3) intermediate precision is either the same as _Ty, or greater than twice the precision of _Ty
// 4) parameters and local variables do not retain extra intermediate precision
// 5) rounding mode is rounding to nearest
// violation of condition 3 or 5 could lead to relative error on the order of epsilon^2
// violation of other conditions could lead to worse results
template <class _Ty>
_NODISCARD constexpr _Fmp_t<_Ty, 2> _Add_small_x2(const _Ty _Xval, const _Ty _Yval) noexcept {
const _Ty _Sum0 = _Xval + _Yval;
const _Ty _Ymod = _Sum0 - _Xval;
const _Ty _Yerr = _Yval - _Ymod;
return {_Sum0, _Yerr};
}
// 1x precision + 2x precision -> 2x precision
// requires: exponent(_Xval) + countr_zero(significand(_Xval)) >= exponent(_Yval._Val0) || _Xval == 0
template <class _Ty>
_NODISCARD constexpr _Fmp_t<_Ty, 2> _Add_small_x2(const _Ty _Xval, const _Fmp_t<_Ty, 2>& _Yval) noexcept {
const _Fmp_t<_Ty, 2> _Sum0 = _Add_small_x2(_Xval, _Yval._Val0);
return _Add_small_x2(_Sum0._Val0, _Sum0._Val1 + _Yval._Val1);
}
// 2x precision + 2x precision -> 1x precision
template <class _Ty>
_NODISCARD constexpr _Ty _Add_x1(const _Fmp_t<_Ty, 2>& _Xval, const _Fmp_t<_Ty, 2>& _Yval) noexcept {
const _Fmp_t<_Ty, 2> _Sum00 = _Add_x2(_Xval._Val0, _Yval._Val0);
return _Sum00._Val0 + (_Sum00._Val1 + (_Xval._Val1 + _Yval._Val1));
}
// multiplication
// round to 26 significant bits, ties toward zero
_NODISCARD constexpr double _High_half(const double _Val) noexcept {
const auto _Bits = _Bit_cast<unsigned long long>(_Val);
const auto _High_half_bits = (_Bits + 0x3ff'ffffULL) & 0xffff'ffff'f800'0000ULL;
return _Bit_cast<double>(_High_half_bits);
}
// _Xval * _Xval - _Prod0
// the result is exact when:
// 1) _Prod0 is _Xval^2 faithfully rounded
// 2) no internal overflow or underflow occurs
// violation of condition 1 could lead to relative error on the order of epsilon
_NODISCARD constexpr double _Sqr_error_fallback(const double _Xval, const double _Prod0) noexcept {
const double _Xhigh = _High_half(_Xval);
const double _Xlow = _Xval - _Xhigh;
return ((_Xhigh * _Xhigh - _Prod0) + 2.0 * _Xhigh * _Xlow) + _Xlow * _Xlow;
}
#ifdef _FMP_USING_X86_X64_INTRINSICS
_NODISCARD inline double _Sqr_error_x86_x64_fma(const double _Xval, const double _Prod0) noexcept {
const __m128d _Mx = _mm_set_sd(_Xval);
const __m128d _Mprod0 = _mm_set_sd(_Prod0);
const __m128d _Mresult = _mm_fmsub_sd(_Mx, _Mx, _Mprod0);
double _Result;
_mm_store_sd(&_Result, _Mresult);
return _Result;
}
#endif // defined(_FMP_USING_X86_X64_INTRINSICS)
#ifdef _FMP_USING_STD_FMA
_NODISCARD inline double _Sqr_error_std_fma(const double _Xval, const double _Prod0) noexcept {
return _STD fma(_Xval, _Xval, -_Prod0);
}
#endif // defined(_FMP_USING_STD_FMA)
// square(1x precision) -> 2x precision
// the result is exact when no internal overflow or underflow occurs
_NODISCARD inline _Fmp_t<double, 2> _Sqr_x2(const double _Xval) noexcept {
const double _Prod0 = _Xval * _Xval;
#if defined(_FMP_USING_X86_X64_INTRINSICS)
#ifdef __AVX2__
return {_Prod0, _Sqr_error_x86_x64_fma(_Xval, _Prod0)};
#else // ^^^ defined(__AVX2__) / !defined(__AVX2__) vvv
const bool _Definitely_have_fma = __isa_available >= _Stl_isa_available_avx2;
if (_Definitely_have_fma) {
return {_Prod0, _Sqr_error_x86_x64_fma(_Xval, _Prod0)};
} else {
return {_Prod0, _Sqr_error_fallback(_Xval, _Prod0)};
}
#endif // ^^^ !defined(__AVX2__) ^^^
#elif defined(_FMP_USING_STD_FMA)
return {_Prod0, _Sqr_error_std_fma(_Xval, _Prod0)};
#else // ^^^ defined(_FMP_USING_STD_FMA) / not using intrinsics vvv
return {_Prod0, _Sqr_error_fallback(_Xval, _Prod0)};
#endif // ^^^ not using intrinsics ^^^
}
} // namespace _Float_multi_prec
#pragma float_control(pop)
#undef _FMP_USING_X86_X64_INTRINSICS
#undef _FMP_USING_STD_FMA
#define _FMP _STD _Float_multi_prec::
// implements numerical algorithms for <complex>
namespace _Math_algorithms {
// TRANSITION: sqrt() isn't constexpr
// _Hypot_leg_huge = _Ty{0.5} * _STD sqrt((_STD numeric_limits<_Ty>::max)());
// _Hypot_leg_tiny = _STD sqrt(_Ty{2.0} * (_STD numeric_limits<_Ty>::min)() / _STD numeric_limits<_Ty>::epsilon());
template <class _Ty>
struct _Hypot_leg_huge_helper {
static constexpr _Ty value{6.703903964971298e+153};
};
template <>
struct _Hypot_leg_huge_helper<float> {
static constexpr float value{9.2233715e+18f};
};
template <class _Ty>
_INLINE_VAR constexpr _Ty _Hypot_leg_huge = _Hypot_leg_huge_helper<_Ty>::value;
template <class _Ty>
struct _Hypot_leg_tiny_helper {
static constexpr _Ty value{1.4156865331029228e-146};
};
template <>
struct _Hypot_leg_tiny_helper<float> {
static constexpr float value{4.440892e-16f};
};
template <class _Ty>
_INLINE_VAR constexpr _Ty _Hypot_leg_tiny = _Hypot_leg_tiny_helper<_Ty>::value;
template <class _Ty>
_NODISCARD _Ty _Norm_minus_one(const _Ty _Xval, const _Ty _Yval) noexcept {
// requires |_Xval| >= |_Yval| and 0.5 <= |_Xval| < 2^12
// returns _Xval * _Xval + _Yval * _Yval - 1
const _FMP _Fmp_t<_Ty, 2> _Xsqr = _FMP _Sqr_x2(_Xval);
const _FMP _Fmp_t<_Ty, 2> _Ysqr = _FMP _Sqr_x2(_Yval);
const _FMP _Fmp_t<_Ty, 2> _Xsqr_m1 = _FMP _Add_small_x2(_Ty{-1.0}, _Xsqr);
return _Add_x1(_Xsqr_m1, _Ysqr);
}
_NODISCARD inline float _Norm_minus_one(const float _Xval, const float _Yval) noexcept {
const auto _Dx = static_cast<double>(_Xval);
const auto _Dy = static_cast<double>(_Yval);
return static_cast<float>((_Dx * _Dx - 1.0) + _Dy * _Dy);
}
// TRANSITION: CRT log1p can be inaccurate for tiny inputs under directed rounding modes
template <class _Ty>
_NODISCARD _Ty _Logp1(const _Ty _Xval) noexcept { // returns log(1 + _Xval)
static_assert(is_floating_point_v<_Ty>, "_Ty must be floating-point");
if (_Is_nan(_Xval)) { // NaN
return _Xval + _Xval; // raise FE_INVALID if _Xval is a signaling NaN
}
if (_Xval <= _Ty{-0.5} || _Ty{2.0} <= _Xval) { // naive formula is moderately accurate
if (_Xval == (numeric_limits<_Ty>::max)()) { // avoid overflow
return _STD log(_Xval);
}
return _STD log(_Ty{1.0} + _Xval);
}
const _Ty _Xabs = _Float_abs(_Xval);
if (_Xabs < numeric_limits<_Ty>::epsilon()) { // zero or tiny
if (_Xval == _Ty{0.0}) {
return _Xval;
}
// honor rounding mode, raise FE_INEXACT
return _Xval - _Ty{0.5} * _Xval * _Xval;
}
// compute log(1 + _Xval) with fixup for small _Xval
const _FMP _Fmp_t<_Ty, 2> _Xp1 = _FMP _Add_small_x2(_Ty{1.0}, _Xval);
return _STD log(_Xp1._Val0) + _Xp1._Val1 / _Xp1._Val0;
}
template <class _Ty>
_NODISCARD _Ty _Log_hypot(const _Ty _Xval, const _Ty _Yval) noexcept { // returns log(hypot(_Xval, _Yval))
static_assert(is_floating_point_v<_Ty>, "_Ty must be floating-point");
if (!_Is_finite(_Xval) || !_Is_finite(_Yval)) { // Inf or NaN
// raise FE_INVALID and return NaN if at least one of them is a signaling NaN
if (_Is_signaling_nan(_Xval) || _Is_signaling_nan(_Yval)) {
return _Xval + _Yval;
}
// return +Inf if at least one of them is an infinity, even when the other is a quiet NaN
if (_Is_inf(_Xval)) {
return _Float_abs(_Xval);
}
if (_Is_inf(_Yval)) {
return _Float_abs(_Yval);
}
// at least one of them is a quiet NaN, and the other is not an infinity
return _Xval + _Yval;
}
_Ty _Av = _Float_abs(_Xval);
_Ty _Bv = _Float_abs(_Yval);
if (_Av < _Bv) { // ensure that _Bv <= _Av
_STD swap(_Av, _Bv);
}
if (_Bv == 0) {
return _STD log(_Av);
}
if (_Hypot_leg_tiny<_Ty> < _Av && _Av < _Hypot_leg_huge<_Ty>) { // no overflow or harmful underflow
constexpr _Ty _Norm_small{0.5};
constexpr _Ty _Norm_big{3.0};
const _Ty _Bv_sqr = _Bv * _Bv;
if (_Av == _Ty{1.0}) { // correctly return +0 when _Av == 1 and _Bv * _Bv underflows
// _Norm_minus_one(_Av, _Bv) could return -0 under FE_DOWNWARD rounding mode
return _Logp1(_Bv_sqr) * _Ty{0.5};
}
const _Ty _Norm = _Av * _Av + _Bv_sqr;
if (_Norm_small < _Norm && _Norm < _Norm_big) { // avoid catastrophic cancellation
return _Logp1(_Norm_minus_one(_Av, _Bv)) * _Ty{0.5};
} else {
return _STD log(_Norm) * _Ty{0.5};
}
} else { // use 1 1/2 precision to preserve bits
constexpr _Ty _Cm = _Ty{22713.0L / 32768.0L};
constexpr _Ty _Cl = _Ty{1.4286068203094172321214581765680755e-6L};
const int _Exponent = _STD ilogb(_Av);
const _Ty _Av_scaled = _STD scalbn(_Av, -_Exponent);
const _Ty _Bv_scaled = _STD scalbn(_Bv, -_Exponent);
const _Ty _Bv_scaled_sqr = _Bv_scaled * _Bv_scaled;
const _Ty _Norm_scaled = _Av_scaled * _Av_scaled + _Bv_scaled_sqr;
const _Ty _Real_shifted = _STD log(_Norm_scaled) * _Ty{0.5};
const auto _Fexponent = static_cast<_Ty>(_Exponent);
return (_Real_shifted + _Fexponent * _Cl) + _Fexponent * _Cm;
}
}
} // namespace _Math_algorithms
#undef _FMP
using _Dcomplex_value = _CSTD _C_double_complex;
using _Fcomplex_value = _CSTD _C_float_complex;
using _Lcomplex_value = _CSTD _C_ldouble_complex;
_EXPORT_STD template <class _Ty>
class complex;
template <>
class complex<float>;
template <>
class complex<double>;
template <>
class complex<long double>;
template <class _Ty>
class _Ctraits {
public:
static constexpr _Ty _Flt_eps() { // get epsilon
return numeric_limits<_Ty>::epsilon();
}
static constexpr _Ty _Flt_max() {
return (numeric_limits<_Ty>::max)();
}
static constexpr _Ty _Flt_norm_min() {
return (numeric_limits<_Ty>::min)() > 0 ? (numeric_limits<_Ty>::min)() : 0;
}
static _Ty _Abs(_Ty _Left) {
return static_cast<_Ty>(_Signbit(_Left) ? -_Left : _Left);
}
static _Ty _Cosh(_Ty _Left, _Ty _Right) { // return cosh(_Left) * _Right
return static_cast<_Ty>(_CSTD _Cosh(static_cast<double>(_Left), static_cast<double>(_Right)));
}
static _Ty _Copysign(_Ty _Magnitude, _Ty _Sign) {
return static_cast<_Ty>(_Signbit(_Sign) ? -_Abs(_Magnitude) : _Abs(_Magnitude));
}
static short _Exp(_Ty* _Pleft, _Ty _Right, short _Exponent) { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
double _Tmp = static_cast<double>(*_Pleft);
short _Ans = _CSTD _Exp(&_Tmp, static_cast<double>(_Right), _Exponent);
*_Pleft = static_cast<_Ty>(_Tmp);
return _Ans;
}
static constexpr _Ty _Infv() { // return infinity
return numeric_limits<_Ty>::infinity();
}
static bool _Isinf(_Ty _Left) { // test for infinity
const auto _Tmp = static_cast<double>(_Left);
const auto _Uint = _Bit_cast<uint64_t>(_Tmp);
return (_Uint & 0x7fffffffffffffffU) == 0x7ff0000000000000U;
}
static _CONSTEXPR20 bool _Isnan(_Ty _Left) {
const auto _Tmp = static_cast<double>(_Left);
const auto _Uint = _Bit_cast<uint64_t>(_Tmp);
return (_Uint & 0x7fffffffffffffffU) > 0x7ff0000000000000U;
}
static constexpr _Ty _Nanv() { // return NaN
return numeric_limits<_Ty>::quiet_NaN();
}
static bool _Signbit(_Ty _Left) {
return (_STD signbit)(static_cast<double>(_Left));
}
static _Ty _Sinh(_Ty _Left, _Ty _Right) { // return sinh(_Left) * _Right
return static_cast<_Ty>(_CSTD _Sinh(static_cast<double>(_Left), static_cast<double>(_Right)));
}
static _Ty asinh(_Ty _Left) {
if (_Left == 0 || _Isnan(_Left) || _Isinf(_Left)) {
return _Left;
}
constexpr _Ty _Ln2 = 0.69314718055994530941723212145817658L;
const _Ty _Old_left = _Left;
_Ty _Ans;
_Left = _Abs(_Left);
if (_Left < 2 / _Flt_eps()) {
_Ans = log1p(_Left + _Left * _Left / (1 + sqrt(_Left * _Left + 1)));
} else {
_Ans = log(_Left) + _Ln2;
}
return _Copysign(_Ans, _Old_left);
}
static _Ty atan2(_Ty _Yval, _Ty _Xval) { // return atan(_Yval / _Xval)
return static_cast<_Ty>(_CSTD atan2(static_cast<double>(_Yval), static_cast<double>(_Xval)));
}
static _Ty cos(_Ty _Left) {
return static_cast<_Ty>(_CSTD cos(static_cast<double>(_Left)));
}
static _Ty exp(_Ty _Left) {
return static_cast<_Ty>(_CSTD exp(static_cast<double>(_Left)));
}
static _Ty ldexp(_Ty _Left, int _Exponent) { // return _Left * 2 ^ _Exponent
return static_cast<_Ty>(_CSTD ldexp(static_cast<double>(_Left), _Exponent));
}
static _Ty log(_Ty _Left) {
return static_cast<_Ty>(_CSTD log(static_cast<double>(_Left)));
}
static _Ty log1p(_Ty _Left) { // return log(1 + _Left)
if (_Left < -1) {
return _Nanv();
} else if (_Left == 0) {
return _Left;
} else { // compute log(1 + _Left) with fixup for small _Left
_Ty _Leftp1 = 1 + _Left;
return log(_Leftp1) - ((_Leftp1 - 1) - _Left) / _Leftp1;
}
}
static _Ty pow(_Ty _Left, _Ty _Right) {
return static_cast<_Ty>(_CSTD pow(static_cast<double>(_Left), static_cast<double>(_Right)));
}
static _Ty sin(_Ty _Left) {
return static_cast<_Ty>(_CSTD sin(static_cast<double>(_Left)));
}
static _Ty sqrt(_Ty _Left) {
return static_cast<_Ty>(_CSTD sqrt(static_cast<double>(_Left)));
}
static _Ty tan(_Ty _Left) {
return static_cast<_Ty>(_CSTD tan(static_cast<double>(_Left)));
}
static _Ty hypot(_Ty _Left, _Ty _Right) {
return static_cast<_Ty>(_CSTD hypot(static_cast<double>(_Left), static_cast<double>(_Right)));
}
};
template <>
class _Ctraits<long double> {
public:
using _Ty = long double;
static constexpr _Ty _Flt_eps() noexcept { // get epsilon
return numeric_limits<long double>::epsilon();
}
static constexpr _Ty _Flt_max() noexcept {
return (numeric_limits<long double>::max)();
}
static constexpr _Ty _Flt_norm_min() noexcept {
return (numeric_limits<long double>::min)();
}
static _Ty _Abs(_Ty _Left) noexcept {
// testing _Left < 0 would be incorrect when _Left is -0.0
return _CSTD fabsl(_Left);
}
static _Ty _Cosh(_Ty _Left, _Ty _Right) noexcept { // return cosh(_Left) * _Right
return _CSTD _LCosh(_Left, _Right);
}
static _Ty _Copysign(_Ty _Magnitude, _Ty _Sign) noexcept {
// testing _Sign < 0 would be incorrect when _Sign is -0.0
return _CSTD copysignl(_Magnitude, _Sign);
}
static short _Exp(_Ty* _Pleft, _Ty _Right, short _Exponent) noexcept {
// compute exp(*_Pleft) * _Right * 2 ^ _Exponent
return _CSTD _LExp(_Pleft, _Right, _Exponent);
}
static constexpr _Ty _Infv() noexcept { // return infinity
return numeric_limits<long double>::infinity();
}
static bool _Isinf(_Ty _Left) noexcept { // test for infinity
#if defined(__LDBL_DIG__) && __LDBL_DIG__ == 18
return _CSTD _LDtest(&_Left) == _INFCODE;
#else // ^^^ 80-bit long double (not supported by MSVC in general, see GH-1316) / 64-bit long double vvv
const auto _Uint = _Bit_cast<uint64_t>(_Left);
return (_Uint & 0x7fffffffffffffffU) == 0x7ff0000000000000U;
#endif // ^^^ 64-bit long double ^^^
}
static _CONSTEXPR20 bool _Isnan(_Ty _Left) noexcept {
#if defined(__LDBL_DIG__) && __LDBL_DIG__ == 18
return _CSTD _LDtest(&_Left) == _NANCODE;
#else // ^^^ 80-bit long double (not supported by MSVC in general, see GH-1316) / 64-bit long double vvv
const auto _Uint = _Bit_cast<uint64_t>(_Left);
return (_Uint & 0x7fffffffffffffffU) > 0x7ff0000000000000U;
#endif // ^^^ 64-bit long double ^^^
}
static constexpr _Ty _Nanv() noexcept { // return NaN
return numeric_limits<long double>::quiet_NaN();
}
static bool _Signbit(_Ty _Left) noexcept {
// testing _Left < 0 would be incorrect when _Left is -0.0
return (_STD signbit)(_Left);
}
static _Ty _Sinh(_Ty _Left, _Ty _Right) noexcept { // return sinh(_Left) * _Right
return _CSTD _LSinh(_Left, _Right);
}
static _Ty asinh(_Ty _Left) noexcept {
if (_Left == 0 || _Isnan(_Left) || _Isinf(_Left)) {
return _Left;
}
constexpr _Ty _Ln2 = 0.69314718055994530941723212145817658L;
const _Ty _Old_left = _Left;
_Ty _Ans;
_Left = _Abs(_Left);
if (_Left < 2 / _Flt_eps()) {
_Ans = log1p(_Left + _Left * _Left / (1 + sqrt(_Left * _Left + 1)));
} else {
_Ans = log(_Left) + _Ln2;
}
return _Copysign(_Ans, _Old_left);
}
static _Ty atan2(_Ty _Yval, _Ty _Xval) noexcept { // return atan(_Yval / _Xval)
return _CSTD atan2l(_Yval, _Xval);
}
static _Ty cos(_Ty _Left) noexcept {
return _CSTD cosl(_Left);
}
static _Ty exp(_Ty _Left) noexcept {
return _CSTD expl(_Left);
}
static _Ty ldexp(_Ty _Left, int _Exponent) noexcept { // return _Left * 2 ^ _Exponent
return _CSTD ldexpl(_Left, _Exponent);
}
static _Ty log(_Ty _Left) noexcept {
return _CSTD logl(_Left);
}
static _Ty log1p(_Ty _Left) noexcept { // return log(1 + _Left)
if (_Left < -1) {
return _Nanv();
} else if (_Left == 0) {
return _Left;
} else { // compute log(1 + _Left) with fixup for small _Left
_Ty _Leftp1 = 1 + _Left;
return log(_Leftp1) - ((_Leftp1 - 1) - _Left) / _Leftp1;
}
}
static _Ty pow(_Ty _Left, _Ty _Right) noexcept {
return _CSTD powl(_Left, _Right);
}
static _Ty sin(_Ty _Left) noexcept {
return _CSTD sinl(_Left);
}
static _Ty sqrt(_Ty _Left) noexcept {
return _CSTD sqrtl(_Left);
}
static _Ty tan(_Ty _Left) noexcept {
return _CSTD tanl(_Left);
}
static _Ty hypot(_Ty _Left, _Ty _Right) noexcept {
return _CSTD hypotl(_Left, _Right);
}
};
template <>
class _Ctraits<double> {
public:
using _Ty = double;
static constexpr _Ty _Flt_eps() noexcept { // get epsilon
return numeric_limits<double>::epsilon();
}
static constexpr _Ty _Flt_max() noexcept {
return (numeric_limits<double>::max)();
}
static constexpr _Ty _Flt_norm_min() noexcept {
return (numeric_limits<double>::min)();
}
static _Ty _Abs(_Ty _Left) noexcept {
// testing _Left < 0 would be incorrect when _Left is -0.0
return _CSTD fabs(_Left);
}
static _Ty _Cosh(_Ty _Left, _Ty _Right) noexcept { // return cosh(_Left) * _Right
return _CSTD _Cosh(_Left, _Right);
}
static _Ty _Copysign(_Ty _Magnitude, _Ty _Sign) noexcept {
// testing _Sign < 0 would be incorrect when _Sign is -0.0
return _CSTD copysign(_Magnitude, _Sign);
}
static short _Exp(_Ty* _Pleft, _Ty _Right, short _Exponent) noexcept {
// compute exp(*_Pleft) * _Right * 2 ^ _Exponent
return _CSTD _Exp(_Pleft, _Right, _Exponent);
}
static constexpr _Ty _Infv() noexcept { // return infinity
return numeric_limits<double>::infinity();
}
static bool _Isinf(_Ty _Left) noexcept { // test for infinity
const auto _Uint = _Bit_cast<uint64_t>(_Left);
return (_Uint & 0x7fffffffffffffffU) == 0x7ff0000000000000U;
}
static _CONSTEXPR20 bool _Isnan(_Ty _Left) noexcept {
const auto _Uint = _Bit_cast<uint64_t>(_Left);
return (_Uint & 0x7fffffffffffffffU) > 0x7ff0000000000000U;
}
static constexpr _Ty _Nanv() noexcept { // return NaN
return numeric_limits<double>::quiet_NaN();
}
static bool _Signbit(_Ty _Left) noexcept {
// testing _Left < 0 would be incorrect when _Left is -0.0
return (_STD signbit)(_Left);
}
static _Ty _Sinh(_Ty _Left, _Ty _Right) noexcept { // return sinh(_Left) * _Right
return _CSTD _Sinh(_Left, _Right);
}
static _Ty asinh(_Ty _Left) noexcept {
if (_Isnan(_Left) || _Isinf(_Left) || _Left == 0) {
return _Left;
} else { // _Left finite nonzero
const _Ty _Old_left = _Left;
_Ty _Ans;
_Left = _Abs(_Left);
if (_Left < 2 / _Flt_eps()) {
_Ans = log1p(_Left + _Left * _Left / (1 + sqrt(_Left * _Left + 1)));
} else {
_Ans = log(_Left) // _Left big, compute log(_Left+_Left)
+ static_cast<_Ty>(0.69314718055994530941723212145817658L);
}
return _Copysign(_Ans, _Old_left);
}
}
static _Ty atan2(_Ty _Yval, _Ty _Xval) noexcept { // return atan(_Yval / _Xval)
return _CSTD atan2(_Yval, _Xval);
}
static _Ty cos(_Ty _Left) noexcept {
return _CSTD cos(_Left);
}
static _Ty exp(_Ty _Left) noexcept {
return _CSTD exp(_Left);
}
static _Ty ldexp(_Ty _Left, int _Exponent) noexcept { // return _Left * 2 ^ _Exponent
return _CSTD ldexp(_Left, _Exponent);
}
static _Ty log(_Ty _Left) noexcept {
return _CSTD log(_Left);
}
static _Ty log1p(_Ty _Left) noexcept { // return log(1 + _Left)
if (_Isnan(_Left) || _Left == 0 || (_Isinf(_Left) && 0 < _Left)) {
return _Left;
} else if (_Left < -1) {
return _Nanv();
} else if (_Left == -1) {
return -_Infv();
} else if (_Left == 0) {
return _Left;
} else { // compute log(1 + _Left) with fixup for small _Left
_Ty _Leftp1 = 1 + _Left;
return log(_Leftp1) - ((_Leftp1 - 1) - _Left) / _Leftp1;
}
}
static _Ty pow(_Ty _Left, _Ty _Right) noexcept {
return _CSTD pow(_Left, _Right);
}
static _Ty sin(_Ty _Left) noexcept {
return _CSTD sin(_Left);
}
static _Ty sqrt(_Ty _Left) noexcept {
return _CSTD sqrt(_Left);
}
static _Ty tan(_Ty _Left) noexcept {
return _CSTD tan(_Left);
}
static _Ty hypot(_Ty _Left, _Ty _Right) noexcept {
return _CSTD hypot(_Left, _Right);
}
};
template <>
class _Ctraits<float> {
public:
using _Ty = float;
static constexpr _Ty _Flt_eps() noexcept { // get epsilon
return numeric_limits<float>::epsilon();
}
static constexpr _Ty _Flt_max() noexcept {
return (numeric_limits<float>::max)();
}
static constexpr _Ty _Flt_norm_min() noexcept {
return (numeric_limits<float>::min)();
}
static _Ty _Abs(_Ty _Left) noexcept {
// testing _Left < 0 would be incorrect when _Left is -0.0
return _CSTD fabsf(_Left);
}
static _Ty _Cosh(_Ty _Left, _Ty _Right) noexcept { // return cosh(_Left) * _Right
return _CSTD _FCosh(_Left, _Right);
}
static _Ty _Copysign(_Ty _Magnitude, _Ty _Sign) noexcept {
// testing _Sign < 0 would be incorrect when _Sign is -0.0
return _CSTD copysignf(_Magnitude, _Sign);
}
static short _Exp(_Ty* _Pleft, _Ty _Right, short _Exponent) noexcept {
// compute exp(*_Pleft) * _Right * 2 ^ _Exponent
return _CSTD _FExp(_Pleft, _Right, _Exponent);
}
static constexpr _Ty _Infv() noexcept { // return infinity
return numeric_limits<float>::infinity();
}
static bool _Isinf(_Ty _Left) noexcept { // test for infinity
const auto _Uint = _Bit_cast<uint32_t>(_Left);
return (_Uint & 0x7fffffffU) == 0x7f800000U;
}
static _CONSTEXPR20 bool _Isnan(_Ty _Left) noexcept {
const auto _Uint = _Bit_cast<uint32_t>(_Left);
return (_Uint & 0x7fffffffU) > 0x7f800000U;
}
static constexpr _Ty _Nanv() noexcept { // return NaN
return numeric_limits<float>::quiet_NaN();
}
static bool _Signbit(_Ty _Left) noexcept {
// testing _Left < 0 would be incorrect when _Left is -0.0
return (_STD signbit)(_Left);
}
static _Ty _Sinh(_Ty _Left, _Ty _Right) noexcept { // return sinh(_Left) * _Right
return _CSTD _FSinh(_Left, _Right);
}
static _Ty asinh(_Ty _Left) noexcept {
if (_Left == 0 || _Isnan(_Left) || _Isinf(_Left)) {
return _Left;
}
constexpr _Ty _Ln2 = 0.69314718055994530941723212145817658F;
const _Ty _Old_left = _Left;
_Ty _Ans;
_Left = _Abs(_Left);
if (_Left < 2 / _Flt_eps()) {
_Ans = log1p(_Left + _Left * _Left / (1 + sqrt(_Left * _Left + 1)));
} else {
_Ans = log(_Left) + _Ln2;
}
return _Copysign(_Ans, _Old_left);
}
static _Ty atan2(_Ty _Yval, _Ty _Xval) noexcept { // return atan(_Yval / _Xval)
return _CSTD atan2f(_Yval, _Xval);
}
static _Ty cos(_Ty _Left) noexcept {
return _CSTD cosf(_Left);
}
static _Ty exp(_Ty _Left) noexcept {
return _CSTD expf(_Left);
}
static _Ty ldexp(_Ty _Left, int _Exponent) noexcept { // return _Left * 2 ^ _Exponent
return _CSTD ldexpf(_Left, _Exponent);
}
static _Ty log(_Ty _Left) noexcept {
return _CSTD logf(_Left);
}
static _Ty log1p(_Ty _Left) noexcept { // return log(1 + _Left)
if (_Left < -1) {
return _Nanv();
} else if (_Left == 0) {
return _Left;
} else { // compute log(1 + _Left) with fixup for small _Left
_Ty _Leftp1 = 1 + _Left;
return log(_Leftp1) - ((_Leftp1 - 1) - _Left) / _Leftp1;
}
}
static _Ty pow(_Ty _Left, _Ty _Right) noexcept {
return _CSTD powf(_Left, _Right);
}
static _Ty sin(_Ty _Left) noexcept {
return _CSTD sinf(_Left);
}
static _Ty sqrt(_Ty _Left) noexcept {
return _CSTD sqrtf(_Left);
}
static _Ty tan(_Ty _Left) noexcept {
return _CSTD tanf(_Left);
}
static _Ty hypot(_Ty _Left, _Ty _Right) noexcept {
return _CSTD hypotf(_Left, _Right);
}
};
template <class _Ty>
_INLINE_VAR constexpr bool _Is_unqual_fp = _Is_any_of_v<_Ty, float, double, long double>;
template <class _Ty>
struct _Complex_value {
enum { _Re = 0, _Im = 1 };
_Ty _Val[2];
};
template <class _Ty, class _Valbase>
class _Complex_base : public _Valbase {
private:
static constexpr bool _Is_for_standard_complex = _Is_any_of_v<_Complex_base, _Complex_base<float, _Fcomplex_value>,
_Complex_base<double, _Dcomplex_value>, _Complex_base<long double, _Lcomplex_value>>;
public:
using _Myctraits = _Ctraits<_Ty>;
using value_type = _Ty;
constexpr _Complex_base(const _Ty& _Realval, const _Ty& _Imagval) noexcept(_Is_for_standard_complex)
: _Valbase{{_Realval, _Imagval}} {}
_CONSTEXPR20 void real(const _Ty& _Right) noexcept(_Is_for_standard_complex) /* strengthened */ {
// set real component
this->_Val[_RE] = _Right;
}
_CONSTEXPR20 void imag(const _Ty& _Right) noexcept(_Is_for_standard_complex) /* strengthened */ {
// set imaginary component
this->_Val[_IM] = _Right;
}
_NODISCARD constexpr _Ty real() const noexcept(_Is_for_standard_complex) /* strengthened */ {
// return real component
return this->_Val[_RE];
}
_NODISCARD constexpr _Ty imag() const noexcept(_Is_for_standard_complex) /* strengthened */ {
// return imaginary component
return this->_Val[_IM];
}
protected:
template <class _Other>
_CONSTEXPR20 void _Add(const complex<_Other>& _Right) noexcept(_Is_for_standard_complex&& _Is_unqual_fp<_Other>) {
this->_Val[_RE] = this->_Val[_RE] + static_cast<_Ty>(_Right.real());
this->_Val[_IM] = this->_Val[_IM] + static_cast<_Ty>(_Right.imag());
}
template <class _Other>
_CONSTEXPR20 void _Sub(const complex<_Other>& _Right) noexcept(_Is_for_standard_complex&& _Is_unqual_fp<_Other>) {
this->_Val[_RE] = this->_Val[_RE] - static_cast<_Ty>(_Right.real());
this->_Val[_IM] = this->_Val[_IM] - static_cast<_Ty>(_Right.imag());
}
template <class _Other>
_CONSTEXPR20 void _Mul(const complex<_Other>& _Right) noexcept(_Is_for_standard_complex&& _Is_unqual_fp<_Other>) {
_Ty _Rightreal = static_cast<_Ty>(_Right.real());
_Ty _Rightimag = static_cast<_Ty>(_Right.imag());
_Ty _Tmp = this->_Val[_RE] * _Rightreal - this->_Val[_IM] * _Rightimag;
this->_Val[_IM] = this->_Val[_RE] * _Rightimag + this->_Val[_IM] * _Rightreal;
this->_Val[_RE] = _Tmp;
}
template <class _Other>
_CONSTEXPR20 void _Div(const complex<_Other>& _Right) noexcept(_Is_for_standard_complex&& _Is_unqual_fp<_Other>) {
using _Myctraits = _Ctraits<_Ty>;
_Ty _Rightreal = static_cast<_Ty>(_Right.real());
_Ty _Rightimag = static_cast<_Ty>(_Right.imag());
if (_Myctraits::_Isnan(_Rightreal) || _Myctraits::_Isnan(_Rightimag)) { // set NaN result
this->_Val[_RE] = _Myctraits::_Nanv();
this->_Val[_IM] = this->_Val[_RE];
} else if ((_Rightimag < 0 ? -_Rightimag : +_Rightimag)
< (_Rightreal < 0 ? -_Rightreal : +_Rightreal)) { // |_Right.imag()| < |_Right.real()|
_Ty _Wr = _Rightimag / _Rightreal;
_Ty _Wd = _Rightreal + _Wr * _Rightimag;
if (_Myctraits::_Isnan(_Wd) || _Wd == 0) { // set NaN result
this->_Val[_RE] = _Myctraits::_Nanv();
this->_Val[_IM] = this->_Val[_RE];
} else { // compute representable result
_Ty _Tmp = (this->_Val[_RE] + this->_Val[_IM] * _Wr) / _Wd;
this->_Val[_IM] = (this->_Val[_IM] - this->_Val[_RE] * _Wr) / _Wd;
this->_Val[_RE] = _Tmp;
}
} else if (_Rightimag == 0) { // _Right.real() == 0 && _Right.imag() == 0