-
Notifications
You must be signed in to change notification settings - Fork 4
/
soft_double.h
3101 lines (2619 loc) · 149 KB
/
soft_double.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2012 - 2024. //
// Distributed under the Boost Software License, //
// Version 1.0. (See accompanying file LICENSE_1_0.txt //
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
///////////////////////////////////////////////////////////////////
// This work uses (significantly) modified parts of
// SoftFloat IEEE Floating-Point Arithmetic Package,
// Release 3e, by John R. Hauser.
// Full original SoftFloat 3e copyright information follows.
/*----------------------------------------------------------------------------
This C header file is part of the SoftFloat IEEE Floating-Point Arithmetic
Package, Release 3e, by John R. Hauser.
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 The Regents of the
University of California. All rights reserved.
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 above copyright notice,
this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above 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 nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
-----------------------------------------------------------------------------*/
#ifndef SOFT_DOUBLE_2020_10_27_H // NOLINT(llvm-header-guard)
#define SOFT_DOUBLE_2020_10_27_H
//#define SOFT_DOUBLE_DISABLE_IOSTREAM
#define SOFT_DOUBLE_CONSTEXPR_BUILTIN_FLOATS 0 // NOLINT(cppcoreguidelines-macro-usage)
#include <array>
#if defined(__has_include)
#if ((__has_include(<bit>) != 0) && (defined(__cpp_lib_bit_cast) && (__cpp_lib_bit_cast == 201806L)))
#include <bit>
#undef SOFT_DOUBLE_CONSTEXPR_BUILTIN_FLOATS
#define SOFT_DOUBLE_CONSTEXPR_BUILTIN_FLOATS 1 // NOLINT(cppcoreguidelines-macro-usage)
#endif
#endif
#include <cstddef>
#include <cstdint>
#if !defined(SOFT_DOUBLE_DISABLE_IOSTREAM)
#include <iomanip>
#endif
#include <limits>
#if !defined(SOFT_DOUBLE_DISABLE_IOSTREAM)
#include <sstream>
#endif
#include <type_traits>
#if (defined(__clang__) && (__clang_major__ <= 9))
#define SOFT_DOUBLE_NUM_LIMITS_CLASS_TYPE struct // NOLINT(cppcoreguidelines-macro-usage)
#else
#define SOFT_DOUBLE_NUM_LIMITS_CLASS_TYPE class // NOLINT(cppcoreguidelines-macro-usage)
#endif
#if defined(__clang__)
#if(__cplusplus >= 201703L)
#define SOFT_DOUBLE_NODISCARD [[nodiscard]] // NOLINT(cppcoreguidelines-macro-usage)
#else
#define SOFT_DOUBLE_NODISCARD // NOLINT(cppcoreguidelines-macro-usage)
#endif
#else
#if (defined(_MSC_VER) && (_MSC_VER < 1920))
#define SOFT_DOUBLE_NODISCARD
#else
#if (defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard))
#define SOFT_DOUBLE_NODISCARD [[nodiscard]]
#else
#define SOFT_DOUBLE_NODISCARD
#endif
#endif
#endif
#if(__cplusplus >= 201703L)
namespace math::softfloat {
#else
namespace math { namespace softfloat { // NOLINT(modernize-concat-nested-namespaces)
#endif
// Forward declaration the math::softfloat::soft_double class.
class soft_double;
#if(__cplusplus >= 201703L)
} // namespace math::softfloat
#else
} // namespace softfloat
} // namespace math
#endif
namespace std {
// Forward declaration of the specialization of numeric_limits for soft_double.
template<>
SOFT_DOUBLE_NUM_LIMITS_CLASS_TYPE numeric_limits<::math::softfloat::soft_double>;
} // namespace std
#if(__cplusplus >= 201703L)
namespace math::softfloat {
#else
namespace math { namespace softfloat { // NOLINT(modernize-concat-nested-namespaces)
#endif
namespace detail {
template<typename T>
constexpr auto my_max(T a, T b) noexcept -> T { return ((a > b) ? a : b); }
template<const int BitCount,
typename EnableType = void>
struct uint_type_helper
{
static_assert(((BitCount >= static_cast<int>(INT8_C(8))) && (BitCount <= static_cast<int>(INT8_C(64)))),
"Error: uint_type_helper is not intended to be used for this BitCount");
using exact_unsigned_type = std::uintmax_t;
};
template<const int BitCount> struct uint_type_helper<BitCount, typename std::enable_if< (BitCount <= static_cast<int>(INT8_C( 8)))>::type> { using exact_unsigned_type = std::uint8_t; };
template<const int BitCount> struct uint_type_helper<BitCount, typename std::enable_if<(BitCount >= static_cast<int>(INT8_C( 9))) && (BitCount <= static_cast<int>(INT8_C(16)))>::type> { using exact_unsigned_type = std::uint16_t; };
template<const int BitCount> struct uint_type_helper<BitCount, typename std::enable_if<(BitCount >= static_cast<int>(INT8_C(17))) && (BitCount <= static_cast<int>(INT8_C(32)))>::type> { using exact_unsigned_type = std::uint32_t; };
template<const int BitCount> struct uint_type_helper<BitCount, typename std::enable_if<(BitCount >= static_cast<int>(INT8_C(33))) && (BitCount <= static_cast<int>(INT8_C(64)))>::type> { using exact_unsigned_type = std::uint64_t; };
struct uint128_compound
{
explicit constexpr uint128_compound(std::uint64_t a = std::uint64_t(), // NOLINT(bugprone-easily-swappable-parameters)
std::uint64_t b = std::uint64_t()) noexcept
: v0(a),
v1(b) { }
std::uint64_t v0; // NOLINT(misc-non-private-member-variables-in-classes)
std::uint64_t v1; // NOLINT(misc-non-private-member-variables-in-classes)
};
template<typename UnsignedIntegralType>
constexpr auto negate(UnsignedIntegralType u) -> typename std::enable_if< std::is_integral<UnsignedIntegralType>::value
&& std::is_unsigned<UnsignedIntegralType>::value, UnsignedIntegralType>::type
{
return
static_cast<UnsignedIntegralType>
(
static_cast<UnsignedIntegralType>(~u)
+ static_cast<UnsignedIntegralType>(UINT8_C(1))
);
}
template<typename SignedIntegralType>
constexpr auto negate(SignedIntegralType n) -> typename std::enable_if< std::is_integral<SignedIntegralType>::value
&& std::is_signed <SignedIntegralType>::value, SignedIntegralType>::type
{
return static_cast<SignedIntegralType>(detail::negate(static_cast<unsigned long long>(n))); // NOLINT(google-runtime-int)
}
constexpr auto signF32UI(std::uint32_t a) -> bool { return (static_cast<std::uint_fast8_t>(a >> static_cast<unsigned>(UINT8_C(31))) != static_cast<std::uint_fast8_t>(UINT8_C(0))); }
constexpr auto expF32UI (std::uint32_t a) -> std::int16_t { return static_cast<std::int16_t>(static_cast<std::int16_t>(a >> static_cast<unsigned>(UINT8_C(23))) & static_cast<std::int16_t>(INT16_C(0xFF))); } // NOLINT(hicpp-signed-bitwise)
constexpr auto fracF32UI(std::uint32_t a) -> std::uint32_t { return static_cast<std::uint32_t>(a & static_cast<std::uint32_t>(UINT32_C(0x007FFFFF))); }
constexpr auto signF64UI(std::uint64_t a) -> bool { return (static_cast<std::uint_fast8_t>(a >> static_cast<unsigned>(UINT8_C(63))) != static_cast<std::uint_fast8_t>(UINT8_C(0))); }
constexpr auto expF64UI (std::uint64_t a) -> std::int16_t { return static_cast<std::int16_t>(static_cast<std::int16_t>(a >> static_cast<unsigned>(UINT8_C(52))) & static_cast<std::int16_t>(INT16_C(0x7FF))); } // NOLINT(hicpp-signed-bitwise)
constexpr auto fracF64UI(std::uint64_t a) -> std::uint64_t { return static_cast<std::uint64_t>(a & static_cast<std::uint64_t>(UINT64_C(0x000FFFFFFFFFFFFF))); }
template<typename IntegralTypeExp,
typename IntegralTypeSig>
constexpr auto packToF64UI(bool sign, IntegralTypeExp expA, IntegralTypeSig sig) -> std::uint64_t
{
using local_unsigned_sig_type =
typename detail::uint_type_helper<std::numeric_limits<IntegralTypeSig>::digits>::exact_unsigned_type;
using local_unsigned_exp_type =
typename detail::uint_type_helper<std::numeric_limits<IntegralTypeExp>::digits>::exact_unsigned_type;
return
static_cast<std::uint64_t>
(
static_cast<std::uint64_t>(sign ? static_cast<std::uint64_t>(static_cast<std::uint64_t>(UINT8_C(1)) << static_cast<unsigned>(UINT8_C(63))) : static_cast<std::uint64_t>(UINT8_C(0)))
+ static_cast<std::uint64_t>(static_cast<std::uint64_t>(static_cast<local_unsigned_exp_type>(expA)) << static_cast<unsigned>(UINT8_C(52))) // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult)
+ static_cast<std::uint64_t>(static_cast<local_unsigned_sig_type>(sig))
);
}
template<typename IntegralTypeExp,
typename IntegralTypeSig>
constexpr auto packToF32UI(bool sign, IntegralTypeExp expA, IntegralTypeSig sig) -> std::uint32_t
{
return
static_cast<std::uint32_t>
(
static_cast<std::uint32_t>(sign ? static_cast<std::uint32_t>(UINT8_C(1)) << static_cast<unsigned>(UINT8_C(31)) : static_cast<std::uint32_t>(UINT8_C(0)))
+ static_cast<std::uint32_t>(static_cast<std::uint32_t>(expA) << static_cast<unsigned>(UINT8_C(23)))
+ static_cast<std::uint32_t>(sig)
);
}
constexpr auto softfloat_shortShiftRightJam64(std::uint64_t a, std::uint_fast16_t dist) -> std::uint64_t
{
return
static_cast<std::uint64_t>
(
static_cast<std::uint64_t>(a >> dist)
| static_cast<unsigned>
(
(static_cast<std::uint64_t>(a << static_cast<unsigned>(negate(dist) & static_cast<unsigned>(UINT8_C(63)))) != static_cast<std::uint64_t>(UINT8_C(0)))
? static_cast<unsigned>(UINT8_C(1))
: static_cast<unsigned>(UINT8_C(0))
)
);
}
constexpr auto softfloat_shiftRightJam64(std::uint64_t a, std::uint_fast16_t dist) -> std::uint64_t
{
return
static_cast<std::uint64_t>
(
(dist < static_cast<std::uint_fast16_t>(UINT8_C(63)))
? softfloat_shortShiftRightJam64(a, dist)
: static_cast<std::uint64_t>
(
(a != static_cast<std::uint64_t>(UINT8_C(0)))
? static_cast<unsigned>(UINT8_C(1))
: static_cast<unsigned>(UINT8_C(0))
)
);
}
constexpr auto softfloat_countLeadingZeros8_z_table(std::uint_fast8_t index) -> std::uint_fast8_t
{
// A constant table that translates an 8-bit unsigned integer (the array index)
// into the number of leading 0 bits before the most-significant 1 of that
// integer. For integer zero (index 0), the corresponding table element is 8.
return
static_cast<std::uint_fast8_t>
(
((index < static_cast<std::uint_fast8_t>(UINT8_C(0x1))) ? static_cast<std::uint_fast8_t>(UINT8_C(4))
: ((index < static_cast<std::uint_fast8_t>(UINT8_C(0x2))) ? static_cast<std::uint_fast8_t>(UINT8_C(3))
: ((index < static_cast<std::uint_fast8_t>(UINT8_C(0x4))) ? static_cast<std::uint_fast8_t>(UINT8_C(2))
: ((index < static_cast<std::uint_fast8_t>(UINT8_C(0x8))) ? static_cast<std::uint_fast8_t>(UINT8_C(1))
: static_cast<std::uint_fast8_t>(UINT8_C(0))))))
);
}
constexpr auto softfloat_countLeadingZeros8(std::uint8_t a) -> std::uint_fast8_t
{
return
static_cast<std::uint_fast8_t>
(
(a < static_cast<std::uint_fast8_t>(UINT8_C(0x10)))
? static_cast<std::uint_fast8_t>
(
static_cast<std::uint_fast8_t>(UINT8_C(4))
+ softfloat_countLeadingZeros8_z_table(static_cast<std::uint_fast8_t>(a & static_cast<std::uint8_t>(UINT8_C(0xF))))
)
: softfloat_countLeadingZeros8_z_table(static_cast<std::uint_fast8_t>(a >> static_cast<unsigned>(UINT8_C(4))))
);
}
constexpr auto softfloat_countLeadingZeros16(std::uint16_t a) -> std::uint_fast8_t
{
return
static_cast<std::uint_fast8_t>
(
(a < static_cast<std::uint16_t>(UINT16_C(0x100)))
? static_cast<std::uint_fast8_t>
(
static_cast<std::uint_fast8_t>(UINT8_C(8))
+ softfloat_countLeadingZeros8(static_cast<std::uint8_t>(a))
)
: softfloat_countLeadingZeros8(static_cast<std::uint8_t>(a >> static_cast<unsigned>(UINT8_C(8))))
);
}
constexpr auto softfloat_countLeadingZeros32(std::uint32_t a) -> std::uint_fast8_t
{
return
static_cast<std::uint_fast8_t>
(
(a < static_cast<std::uint32_t>(UINT32_C(0x10000)))
? static_cast<std::uint_fast8_t>
(
static_cast<std::uint_fast8_t>(UINT8_C(16))
+ softfloat_countLeadingZeros16(static_cast<std::uint16_t>(a))
)
: softfloat_countLeadingZeros16(static_cast<std::uint16_t>(a >> static_cast<unsigned>(UINT8_C(16))))
);
}
constexpr auto softfloat_countLeadingZeros64(std::uint64_t a) -> std::uint_fast8_t
{
return
static_cast<std::uint_fast8_t>
(
(a < static_cast<std::uint64_t>(UINT64_C(0x100000000)))
? static_cast<std::uint_fast8_t>
(
static_cast<unsigned>(UINT8_C(32))
+ softfloat_countLeadingZeros32(static_cast<std::uint32_t>(a))
)
: softfloat_countLeadingZeros32(static_cast<std::uint32_t>(a >> static_cast<unsigned>(UINT8_C(32))))
);
}
constexpr auto softfloat_approxRecip32_1(std::uint32_t a) -> std::uint32_t
{
// Returns an approximation to the reciprocal of the number represented by a,
// where a is interpreted as an unsigned fixed-point number with one integer
// bit and 31 fraction bits.
return
static_cast<std::uint32_t>
(
static_cast<std::uint64_t>(UINT64_C(0x7FFFFFFFFFFFFFFF))
/ a
);
}
constexpr auto softfloat_shiftRightJam64Extra(std::uint64_t a,
std::uint64_t extra,
std::uint32_t dist) -> uint128_compound
{
// Shifts the 128 bits formed by concatenating a and extra right by 64
// _plus_ the number of bits given in dist, which must not be zero. This
// shifted value is at most 64 nonzero bits and is returned in the v field
// of the struct uint64_extra result.
return
uint128_compound
{
static_cast<std::uint64_t>
(
(dist < static_cast<std::uint32_t>(UINT8_C(64)))
? static_cast<std::uint64_t>
( static_cast<std::uint64_t>
(
a << static_cast<unsigned>
(
static_cast<unsigned>(negate(dist)) & static_cast<std::uint32_t>(UINT8_C(63))
)
)
| static_cast<unsigned>
(
(extra != static_cast<std::uint64_t>(UINT8_C(0)))
? static_cast<unsigned>(UINT8_C(1))
: static_cast<unsigned>(UINT8_C(0))
)
)
:
static_cast<std::uint64_t>
(
(dist == static_cast<std::uint32_t>(UINT8_C(64)))
? a
: static_cast<std::uint64_t>
(
((a != static_cast<std::uint64_t>(UINT8_C(0))) || (extra != static_cast<std::uint64_t>(UINT8_C(0))))
? static_cast<unsigned>(UINT8_C(1))
: static_cast<unsigned>(UINT8_C(0))
)
)
),
static_cast<std::uint64_t>
(
(dist < static_cast<std::uint32_t>(UINT8_C(64)))
? static_cast<std::uint64_t>(a >> static_cast<unsigned>(dist))
: static_cast<std::uint64_t>(UINT8_C(0))
)
};
}
#if (defined(SOFT_DOUBLE_CONSTEXPR_BUILTIN_FLOATS) && (SOFT_DOUBLE_CONSTEXPR_BUILTIN_FLOATS == 0))
template<typename BuiltInFloatType,
typename ExactUnsignedIntegralType = typename uint_type_helper<std::numeric_limits<BuiltInFloatType>::digits>::exact_unsigned_type>
union uz_type
{
static_assert(( std::is_same<BuiltInFloatType, float>::value
|| std::is_same<BuiltInFloatType, double>::value),
"Error: This template is intended for either built-in float or double, but not for any other type(s)");
using float_type = BuiltInFloatType;
using unsigned_type = ExactUnsignedIntegralType;
const float_type my_f; // NOLINT(misc-non-private-member-variables-in-classes)
const unsigned_type my_u; // NOLINT(misc-non-private-member-variables-in-classes)
explicit constexpr uz_type(float_type f) noexcept : my_f(f) { }
explicit constexpr uz_type(unsigned_type u) noexcept : my_u(u) { }
SOFT_DOUBLE_NODISCARD constexpr auto get_f() const noexcept -> float_type { return my_f; }
SOFT_DOUBLE_NODISCARD constexpr auto get_u() const noexcept -> unsigned_type { return my_u; }
};
#elif (defined(SOFT_DOUBLE_CONSTEXPR_BUILTIN_FLOATS) && (SOFT_DOUBLE_CONSTEXPR_BUILTIN_FLOATS == 1))
template<typename BuiltInFloatType,
typename ExactUnsignedIntegralType = typename uint_type_helper<std::numeric_limits<BuiltInFloatType>::digits>::exact_unsigned_type>
struct uz_type
{
static_assert(( std::is_same<BuiltInFloatType, float>::value
|| std::is_same<BuiltInFloatType, double>::value),
"Error: This template is intended for either built-in float or double, but not for any other type(s)");
using float_type = BuiltInFloatType;
using unsigned_type = ExactUnsignedIntegralType;
const float_type my_f; // NOLINT(misc-non-private-member-variables-in-classes)
explicit constexpr uz_type(float_type f) noexcept : my_f(f) { }
explicit constexpr uz_type(unsigned_type u) noexcept : my_f(std::bit_cast<float_type>(u)) { }
SOFT_DOUBLE_NODISCARD constexpr auto get_u() const noexcept -> unsigned_type { return std::bit_cast<unsigned_type>(my_f); }
SOFT_DOUBLE_NODISCARD constexpr auto get_f() const noexcept -> float_type { return my_f; }
};
#else
#error Configuration error regarding SOFT_DOUBLE_CONSTEXPR_BUILTIN_FLOATS
#endif
struct nothing { };
} // namespace detail
constexpr auto operator+(const soft_double& a, const soft_double& b) -> soft_double;
constexpr auto operator-(const soft_double& a, const soft_double& b) -> soft_double;
constexpr auto operator*(const soft_double& a, const soft_double& b) -> soft_double;
constexpr auto operator/(const soft_double& a, const soft_double& b) -> soft_double;
template<typename UnsignedIntegralType> constexpr auto operator+(const soft_double& u, UnsignedIntegralType n) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, soft_double>::type;
template<typename UnsignedIntegralType> constexpr auto operator-(const soft_double& u, UnsignedIntegralType n) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, soft_double>::type;
template<typename UnsignedIntegralType> constexpr auto operator*(const soft_double& u, UnsignedIntegralType n) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, soft_double>::type;
template<typename UnsignedIntegralType> constexpr auto operator/(const soft_double& u, UnsignedIntegralType n) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, soft_double>::type;
template<typename UnsignedIntegralType> constexpr auto operator+(UnsignedIntegralType n, const soft_double& u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, soft_double>::type;
template<typename UnsignedIntegralType> constexpr auto operator-(UnsignedIntegralType n, const soft_double& u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, soft_double>::type;
template<typename UnsignedIntegralType> constexpr auto operator*(UnsignedIntegralType n, const soft_double& u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, soft_double>::type;
template<typename UnsignedIntegralType> constexpr auto operator/(UnsignedIntegralType n, const soft_double& u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, soft_double>::type;
template<typename SignedIntegralType> constexpr auto operator+(const soft_double& u, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, soft_double>::type;
template<typename SignedIntegralType> constexpr auto operator-(const soft_double& u, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, soft_double>::type;
template<typename SignedIntegralType> constexpr auto operator*(const soft_double& u, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, soft_double>::type;
template<typename SignedIntegralType> constexpr auto operator/(const soft_double& u, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, soft_double>::type;
template<typename SignedIntegralType> constexpr auto operator+(SignedIntegralType n, const soft_double& u) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, soft_double>::type;
template<typename SignedIntegralType> constexpr auto operator-(SignedIntegralType n, const soft_double& u) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, soft_double>::type;
template<typename SignedIntegralType> constexpr auto operator*(SignedIntegralType n, const soft_double& u) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, soft_double>::type;
template<typename SignedIntegralType> constexpr auto operator/(SignedIntegralType n, const soft_double& u) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, soft_double>::type;
constexpr auto operator+(const soft_double& u, float f) -> soft_double;
constexpr auto operator-(const soft_double& u, float f) -> soft_double;
constexpr auto operator*(const soft_double& u, float f) -> soft_double;
constexpr auto operator/(const soft_double& u, float f) -> soft_double;
constexpr auto operator+(float f, const soft_double& u) -> soft_double;
constexpr auto operator-(float f, const soft_double& u) -> soft_double;
constexpr auto operator*(float f, const soft_double& u) -> soft_double;
constexpr auto operator/(float f, const soft_double& u) -> soft_double;
constexpr auto operator+(const soft_double& u, double f) -> soft_double;
constexpr auto operator-(const soft_double& u, double f) -> soft_double;
constexpr auto operator*(const soft_double& u, double f) -> soft_double;
constexpr auto operator/(const soft_double& u, double f) -> soft_double;
constexpr auto operator+(double f, const soft_double& u) -> soft_double;
constexpr auto operator-(double f, const soft_double& u) -> soft_double;
constexpr auto operator*(double f, const soft_double& u) -> soft_double;
constexpr auto operator/(double f, const soft_double& u) -> soft_double;
constexpr auto operator+(const soft_double& u, long double f) -> soft_double;
constexpr auto operator-(const soft_double& u, long double f) -> soft_double;
constexpr auto operator*(const soft_double& u, long double f) -> soft_double;
constexpr auto operator/(const soft_double& u, long double f) -> soft_double;
constexpr auto operator+(long double f, const soft_double& u) -> soft_double;
constexpr auto operator-(long double f, const soft_double& u) -> soft_double;
constexpr auto operator*(long double f, const soft_double& u) -> soft_double;
constexpr auto operator/(long double f, const soft_double& u) -> soft_double;
constexpr auto operator< (const soft_double& a, const soft_double& b) -> bool;
constexpr auto operator<=(const soft_double& a, const soft_double& b) -> bool;
constexpr auto operator==(const soft_double& a, const soft_double& b) -> bool;
constexpr auto operator!=(const soft_double& a, const soft_double& b) -> bool;
constexpr auto operator>=(const soft_double& a, const soft_double& b) -> bool;
constexpr auto operator> (const soft_double& a, const soft_double& b) -> bool;
template<typename UnsignedIntegralType> constexpr auto operator< (const soft_double& a, UnsignedIntegralType u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator<=(const soft_double& a, UnsignedIntegralType u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator==(const soft_double& a, UnsignedIntegralType u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator!=(const soft_double& a, UnsignedIntegralType u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator>=(const soft_double& a, UnsignedIntegralType u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator> (const soft_double& a, UnsignedIntegralType u) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator< (UnsignedIntegralType u, const soft_double& a) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator<=(UnsignedIntegralType u, const soft_double& a) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator==(UnsignedIntegralType u, const soft_double& a) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator!=(UnsignedIntegralType u, const soft_double& a) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator>=(UnsignedIntegralType u, const soft_double& a) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename UnsignedIntegralType> constexpr auto operator> (UnsignedIntegralType u, const soft_double& a) -> typename std::enable_if<std::is_integral<UnsignedIntegralType>::value && std::is_unsigned<UnsignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator< (const soft_double& a, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator<=(const soft_double& a, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator==(const soft_double& a, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator!=(const soft_double& a, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator>=(const soft_double& a, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator> (const soft_double& a, SignedIntegralType n) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator< (SignedIntegralType n, const soft_double& a) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator<=(SignedIntegralType n, const soft_double& a) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator==(SignedIntegralType n, const soft_double& a) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator!=(SignedIntegralType n, const soft_double& a) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator>=(SignedIntegralType n, const soft_double& a) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
template<typename SignedIntegralType> constexpr auto operator> (SignedIntegralType n, const soft_double& a) -> typename std::enable_if<std::is_integral<SignedIntegralType>::value && std::is_signed<SignedIntegralType>::value, bool>::type;
constexpr auto operator< (const soft_double& a, float f) -> bool;
constexpr auto operator<=(const soft_double& a, float f) -> bool;
constexpr auto operator==(const soft_double& a, float f) -> bool;
constexpr auto operator!=(const soft_double& a, float f) -> bool;
constexpr auto operator>=(const soft_double& a, float f) -> bool;
constexpr auto operator> (const soft_double& a, float f) -> bool;
constexpr auto operator< (float f, const soft_double& a) -> bool;
constexpr auto operator<=(float f, const soft_double& a) -> bool;
constexpr auto operator==(float f, const soft_double& a) -> bool;
constexpr auto operator!=(float f, const soft_double& a) -> bool;
constexpr auto operator>=(float f, const soft_double& a) -> bool;
constexpr auto operator> (float f, const soft_double& a) -> bool;
constexpr auto operator< (const soft_double& a, double f) -> bool;
constexpr auto operator<=(const soft_double& a, double f) -> bool;
constexpr auto operator==(const soft_double& a, double f) -> bool;
constexpr auto operator!=(const soft_double& a, double f) -> bool;
constexpr auto operator>=(const soft_double& a, double f) -> bool;
constexpr auto operator> (const soft_double& a, double f) -> bool;
constexpr auto operator< (double f, const soft_double& a) -> bool;
constexpr auto operator<=(double f, const soft_double& a) -> bool;
constexpr auto operator==(double f, const soft_double& a) -> bool;
constexpr auto operator!=(double f, const soft_double& a) -> bool;
constexpr auto operator>=(double f, const soft_double& a) -> bool;
constexpr auto operator> (double f, const soft_double& a) -> bool;
constexpr auto operator< (const soft_double& a, long double f) -> bool;
constexpr auto operator<=(const soft_double& a, long double f) -> bool;
constexpr auto operator==(const soft_double& a, long double f) -> bool;
constexpr auto operator!=(const soft_double& a, long double f) -> bool;
constexpr auto operator>=(const soft_double& a, long double f) -> bool;
constexpr auto operator> (const soft_double& a, long double f) -> bool;
constexpr auto operator< (long double f, const soft_double& a) -> bool;
constexpr auto operator<=(long double f, const soft_double& a) -> bool;
constexpr auto operator==(long double f, const soft_double& a) -> bool;
constexpr auto operator!=(long double f, const soft_double& a) -> bool;
constexpr auto operator>=(long double f, const soft_double& a) -> bool;
constexpr auto operator> (long double f, const soft_double& a) -> bool;
#if !defined(SOFT_DOUBLE_DISABLE_IOSTREAM)
template<typename char_type, typename traits_type> auto operator<<(std::basic_ostream<char_type, traits_type>& os, const soft_double& f) -> std::basic_ostream<char_type, traits_type>&;
template<typename char_type, typename traits_type> auto operator>>(std::basic_istream<char_type, traits_type>& is, soft_double& f) -> std::basic_istream<char_type, traits_type>&;
#endif // !WIDE_DECIMAL_DISABLE_IOSTREAM
constexpr auto (isnan) (soft_double x) -> bool;
constexpr auto (isinf) (soft_double x) -> bool;
constexpr auto (isfinite)(soft_double x) -> bool;
constexpr auto abs (soft_double x) -> soft_double;
constexpr auto fabs (soft_double x) -> soft_double;
constexpr auto fmod (soft_double v1, soft_double v2) -> soft_double;
constexpr auto frexp (soft_double x, int* expptr) -> soft_double;
constexpr auto ldexp (soft_double x, int expval) -> soft_double;
constexpr auto floor (soft_double x) -> soft_double;
constexpr auto ceil (soft_double x) -> soft_double;
constexpr auto sqrt (soft_double x) -> soft_double;
constexpr auto exp (soft_double x) -> soft_double;
constexpr auto log (soft_double x) -> soft_double;
constexpr auto pow (soft_double x, soft_double a) -> soft_double;
constexpr auto sin (soft_double x) -> soft_double;
constexpr auto cos (soft_double x) -> soft_double;
constexpr auto tan (soft_double x) -> soft_double;
constexpr auto asin (soft_double x) -> soft_double;
constexpr auto acos (soft_double x) -> soft_double;
constexpr auto atan (soft_double x) -> soft_double;
constexpr auto sinh (soft_double x) -> soft_double;
constexpr auto cosh (soft_double x) -> soft_double;
constexpr auto tanh (soft_double x) -> soft_double;
template<typename UnsignedIntegralType,
typename std::enable_if<( std::is_integral<UnsignedIntegralType>::value
&& std::is_unsigned<UnsignedIntegralType>::value)>::type const* = nullptr>
constexpr auto pow(soft_double x, UnsignedIntegralType u) -> soft_double;
template<typename SignedIntegralType,
typename std::enable_if<( std::is_integral<SignedIntegralType>::value
&& std::is_signed <SignedIntegralType>::value)>::type const* = nullptr>
constexpr auto pow(soft_double x, SignedIntegralType n) -> soft_double;
class soft_double final
{
public:
static_assert(sizeof(float) == static_cast<std::size_t>(UINT8_C(4)),
"Error: This template is designed for built-in float having 4 bytes");
using representation_type = std::uint64_t;
constexpr soft_double() noexcept = default;
template<typename UnsignedIntegralType,
typename std::enable_if<( std::is_integral<UnsignedIntegralType>::value
&& std::is_unsigned<UnsignedIntegralType>::value
&& (sizeof(UnsignedIntegralType) <= sizeof(std::uint32_t)))>::type const* = nullptr>
constexpr soft_double(UnsignedIntegralType u) noexcept : my_value(my_ui32_to_f64(static_cast<std::uint32_t>(u))) { } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
template<typename UnsignedIntegralType,
typename std::enable_if<( std::is_integral<UnsignedIntegralType>::value
&& std::is_unsigned<UnsignedIntegralType>::value
&& (!(sizeof(UnsignedIntegralType) <= sizeof(std::uint32_t))))>::type const* = nullptr>
constexpr soft_double(UnsignedIntegralType u) noexcept : my_value(my_ui64_to_f64(static_cast<std::uint64_t>(u))) { } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
template<typename SignedIntegralType,
typename std::enable_if<( std::is_integral<SignedIntegralType>::value
&& std::is_signed <SignedIntegralType>::value
&& (sizeof(SignedIntegralType) <= sizeof(int32_t)))>::type const* = nullptr>
constexpr soft_double(SignedIntegralType n) noexcept : my_value(my__i32_to_f64(static_cast<std::int32_t>(n))) { } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
template<typename SignedIntegralType,
typename std::enable_if<( std::is_integral<SignedIntegralType>::value
&& std::is_signed <SignedIntegralType>::value
&& (!(sizeof(SignedIntegralType) <= sizeof(int32_t))))>::type const* = nullptr>
constexpr soft_double(SignedIntegralType n) noexcept : my_value(my__i64_to_f64(static_cast<std::int64_t>(n))) { } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr soft_double(float f) noexcept // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
: my_value
(
((detail::expF32UI(detail::uz_type<float>(f).get_u()) == static_cast<std::int16_t>(INT8_C(0))) && (detail::fracF32UI(detail::uz_type<float>(f).get_u()) == static_cast<std::uint32_t>(UINT8_C(0)))) // NOLINT(cppcoreguidelines-pro-type-union-access)
? detail::packToF64UI
(
detail::signF32UI(detail::uz_type<float>(f).get_u()), // NOLINT(cppcoreguidelines-pro-type-union-access)
static_cast<int>(INT8_C(0)),
static_cast<int>(INT8_C(0))
)
: detail::packToF64UI
(
detail::signF32UI(detail::uz_type<float>(f).get_u()), // NOLINT(cppcoreguidelines-pro-type-union-access)
static_cast<std::int32_t>
(
static_cast<std::int32_t>(detail::expF32UI(detail::uz_type<float>(f).get_u())) // NOLINT(cppcoreguidelines-pro-type-union-access)
+ static_cast<std::int32_t>(INT16_C(0x380))
),
static_cast<std::uint64_t>
(
static_cast<std::uint64_t>
(
detail::fracF32UI(detail::uz_type<float>(f).get_u()) // NOLINT(cppcoreguidelines-pro-type-union-access)
) << static_cast<unsigned>(UINT8_C(29))
)
)
) { }
constexpr soft_double(double d) noexcept // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
: my_value(detail::uz_type<double>(d).get_u()) { } // NOLINT(cppcoreguidelines-pro-type-union-access)
constexpr soft_double(long double ld) noexcept // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
: my_value(detail::uz_type<double>(static_cast<double>(ld)).get_u()) { } // NOLINT(cppcoreguidelines-pro-type-union-access)
constexpr soft_double(const soft_double& other) noexcept : my_value(other.my_value) { }; // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)
constexpr soft_double(soft_double&& other) noexcept
: my_value(other.my_value) { }
explicit constexpr soft_double(std::uint64_t n, detail::nothing&&) noexcept // NOLINT(hicpp-named-parameter,readability-named-parameter)
: my_value(static_cast<std::uint64_t>(n)) { }
constexpr auto operator=(const soft_double& other) noexcept -> soft_double&
{
if(this != &other)
{
my_value = other.my_value;
}
return *this;
}
constexpr auto operator=(soft_double&& other) noexcept -> soft_double&
{
my_value = other.my_value;
return *this;
}
~soft_double() = default;
SOFT_DOUBLE_NODISCARD constexpr auto representation() const -> representation_type { return my_value; }
SOFT_DOUBLE_NODISCARD constexpr auto crepresentation() const -> representation_type { return my_value; }
static constexpr auto get_rep(const soft_double& a) -> representation_type { return a.crepresentation(); }
constexpr operator signed char () const { return static_cast<signed char> (f64_to__i32(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr operator signed short () const { return static_cast<signed short> (f64_to__i32(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions,google-runtime-int)
constexpr operator signed int () const { return static_cast<signed int> (f64_to__i32(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr operator signed long () const { return static_cast<signed long> (f64_to__i64(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions,google-runtime-int)
constexpr operator signed long long() const { return static_cast<signed long long>(f64_to__i64(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions,google-runtime-int)
constexpr operator unsigned char () const { return static_cast<unsigned char> (f64_to_ui32(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr operator unsigned short () const { return static_cast<unsigned short> (f64_to_ui32(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions,google-runtime-int)
constexpr operator unsigned int () const { return static_cast<unsigned int> (f64_to_ui32(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr operator unsigned long () const { return static_cast<unsigned long> (f64_to_ui64(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions,google-runtime-int)
constexpr operator unsigned long long() const { return static_cast<unsigned long long>(f64_to_ui64(my_value)); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions,google-runtime-int)
private:
template<typename FloatingPointType>
SOFT_DOUBLE_NODISCARD constexpr auto to_float() const -> typename std::enable_if<( (sizeof(FloatingPointType) == 4)
&& std::numeric_limits<FloatingPointType>::is_iec559), FloatingPointType>::type
{
return f64_to_f32(my_value);
}
template<typename FloatingPointType>
SOFT_DOUBLE_NODISCARD constexpr auto to_float() const -> typename std::enable_if<( (sizeof(FloatingPointType) == static_cast<std::size_t>(UINT8_C(8)))
&& std::numeric_limits<FloatingPointType>::is_iec559), FloatingPointType>::type
{
return static_cast<FloatingPointType>(*static_cast<const volatile FloatingPointType*>(static_cast<const volatile void*>(this)));
}
public:
constexpr operator float () const noexcept { return to_float<float>(); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr operator double () const noexcept { return to_float<double>(); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr operator long double() const noexcept { return static_cast<long double>(to_float<double>()); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr auto operator+=(const soft_double& other) -> soft_double& { my_value = f64_add(my_value, other.my_value); return *this; }
constexpr auto operator-=(const soft_double& other) -> soft_double& { my_value = f64_sub(my_value, other.my_value); return *this; }
constexpr auto operator*=(const soft_double& other) -> soft_double& { my_value = f64_mul(my_value, other.my_value); return *this; }
constexpr auto operator/=(const soft_double& other) -> soft_double& { my_value = f64_div(my_value, other.my_value); return *this; }
// Operators pre-increment and pre-decrement.
constexpr auto operator++() -> soft_double& { return *this += my_value_one(); }
constexpr auto operator--() -> soft_double& { return *this -= my_value_one(); }
// Operators post-increment and post-decrement.
constexpr auto operator++(int) -> soft_double { const soft_double w(*this); static_cast<void>(++(*this)); return w; } // NOLINT(performance-no-automatic-move)
constexpr auto operator--(int) -> soft_double { const soft_double w(*this); static_cast<void>(--(*this)); return w; } // NOLINT(performance-no-automatic-move)
constexpr auto operator+() const -> const soft_double& { return *this; }
constexpr auto operator-() const -> soft_double { return soft_double { my_value ^ static_cast<std::uint64_t>(static_cast<std::uint64_t>(UINT8_C(1)) << static_cast<unsigned>(UINT8_C(63))), detail::nothing{} }; }
static constexpr auto my_value_zero () -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0)), detail::nothing{} }; }
static constexpr auto my_value_one () -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0x3FF0000000000000)), detail::nothing{} }; }
static constexpr auto my_value_two () -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0x4000000000000000)), detail::nothing{} }; }
static constexpr auto my_value_half () -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0x3FE0000000000000)), detail::nothing{} }; }
static constexpr auto my_value_pi () -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(4614256656552045848)), detail::nothing{} }; }
static constexpr auto my_value_pi_half() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0x3FF921FB54442D18)), detail::nothing{} }; }
static constexpr auto my_value_ln2 () -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(4604418534313441775)), detail::nothing{} }; }
static constexpr auto my_value_min() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(4503599627370496)), detail::nothing{} }; }
static constexpr auto my_value_max() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(9218868437227405311)), detail::nothing{} }; }
static constexpr auto my_value_lowest() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(18442240474082181119)),detail::nothing{} }; }
static constexpr auto my_value_epsilon() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(4372995238176751616)), detail::nothing{} }; }
static constexpr auto my_value_round_error() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0x3FE0000000000000)), detail::nothing{} }; }
static constexpr auto my_value_denorm_min() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(1)), detail::nothing{} }; }
static constexpr auto my_value_infinity() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0x7FF0000000000000)), detail::nothing{} }; }
static constexpr auto my_value_quiet_NaN() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0xFFF8000000000000)), detail::nothing{} }; }
static constexpr auto my_value_signaling_NaN() -> soft_double { return soft_double { static_cast<std::uint64_t>(UINT64_C(0x7FF8000000000000)), detail::nothing{} }; }
private:
representation_type my_value { };
static constexpr auto my_le(const soft_double& a, const soft_double& b) -> bool
{
return (detail::signF64UI(a.my_value) != detail::signF64UI(b.my_value))
? (detail::signF64UI(a.my_value) || (static_cast<std::uint64_t>(a.my_value | b.my_value) & static_cast<std::uint64_t>(UINT64_C(0x7FFFFFFFFFFFFFFF))) == static_cast<std::uint64_t>(UINT8_C(0)))
: (a.my_value == b.my_value) || (( static_cast<unsigned>(detail::signF64UI(a.my_value) ? static_cast<unsigned>(UINT8_C(1)) : static_cast<unsigned>(UINT8_C(0)))
^ static_cast<unsigned>((a.my_value < b.my_value) ? static_cast<unsigned>(UINT8_C(1)) : static_cast<unsigned>(UINT8_C(0)))) != static_cast<unsigned>(UINT8_C(0)));
}
static constexpr auto my_lt(const soft_double& a, const soft_double& b) -> bool
{
return (detail::signF64UI(a.my_value) != detail::signF64UI(b.my_value))
? (detail::signF64UI(a.my_value) && (static_cast<std::uint64_t>(a.my_value | b.my_value) & static_cast<std::uint64_t>(UINT64_C(0x7FFFFFFFFFFFFFFF))) != static_cast<std::uint64_t>(UINT8_C(0)))
: (a.my_value != b.my_value) && (( static_cast<unsigned>(detail::signF64UI(a.my_value) ? static_cast<unsigned>(UINT8_C(1)) : static_cast<unsigned>(UINT8_C(0)))
^ static_cast<unsigned>((a.my_value < b.my_value) ? static_cast<unsigned>(UINT8_C(1)) : static_cast<unsigned>(UINT8_C(0)))) != static_cast<unsigned>(UINT8_C(0)));
}
static constexpr auto sign_ab(const std::uint64_t a, const std::uint64_t b) -> bool
{
const auto signA = detail::signF64UI(a);
const auto signB = detail::signF64UI(b);
return
static_cast<std::uint_fast8_t>
(
static_cast<std::uint_fast8_t>(signA ? static_cast<std::uint_fast8_t>(UINT8_C(1)) : static_cast<std::uint_fast8_t>(UINT8_C(0)))
^ static_cast<std::uint_fast8_t>(signB ? static_cast<std::uint_fast8_t>(UINT8_C(1)) : static_cast<std::uint_fast8_t>(UINT8_C(0)))
) != static_cast<std::uint_fast8_t>(UINT8_C(0));
}
static constexpr auto f64_add(const std::uint64_t a, const std::uint64_t b) -> std::uint64_t
{
const auto signA = detail::signF64UI(a);
return ((signA == detail::signF64UI(b)) ? softfloat_addMagsF64(a, b, signA)
: softfloat_subMagsF64(a, b, signA));
}
static constexpr auto f64_sub(const std::uint64_t a, const std::uint64_t b) -> std::uint64_t
{
const auto signA = detail::signF64UI(a);
return ((signA == detail::signF64UI(b)) ? softfloat_subMagsF64(a, b, signA)
: softfloat_addMagsF64(a, b, signA));
}
static constexpr auto f64_mul(const std::uint64_t a, const std::uint64_t b) -> std::uint64_t
{
const auto expA = detail::expF64UI (a);
const auto expB = detail::expF64UI (b);
auto sigA = detail::fracF64UI(a);
auto sigB = detail::fracF64UI(b);
const auto signZ = sign_ab(a, b);
auto result = std::uint64_t { };
const auto a_is_zero = ( (expA == static_cast<std::int16_t> ( INT8_C(0)))
&& (sigA == static_cast<std::uint64_t>(UINT8_C(0))));
if( a_is_zero
|| ( (expB == static_cast<std::int16_t> ( INT8_C(0)))
&& (sigB == static_cast<std::uint64_t>(UINT8_C(0)))))
{
result = detail::packToF64UI(signZ, static_cast<int>(INT8_C(0)), static_cast<int>(INT8_C(0)));
}
else
{
auto expZ =
static_cast<std::int16_t>
(
static_cast<std::int16_t>(expA + expB)
- static_cast<std::int16_t>(INT16_C(0x3FF))
);
sigA =
static_cast<std::uint64_t>
(
static_cast<std::uint64_t>
(
sigA | static_cast<std::uint64_t>(UINT64_C(0x0010000000000000))
) << static_cast<unsigned>(UINT8_C(10))
);
sigB =
static_cast<std::uint64_t>
(
static_cast<std::uint64_t>
(
sigB | static_cast<std::uint64_t>(UINT64_C(0x0010000000000000))
) << static_cast<unsigned>(UINT8_C(11))
);
// Compute the 128-bit product of sigA and sigB.
const auto a32 = static_cast<std::uint32_t>(sigA >> static_cast<unsigned>(UINT8_C(32)));
const auto a0 = static_cast<std::uint32_t>(sigA);
const auto b32 = static_cast<std::uint32_t>(sigB >> static_cast<unsigned>(UINT8_C(32)));
const auto b0 = static_cast<std::uint32_t>(sigB);
const auto mid1 = static_cast<std::uint64_t>( (static_cast<std::uint64_t>(a32)) * b0);
auto mid = static_cast<std::uint64_t>(mid1 + static_cast<std::uint64_t>((static_cast<std::uint64_t>(b32)) * a0));
auto sig128Z =
detail::uint128_compound
{
static_cast<std::uint64_t>(static_cast<std::uint64_t>(a0) * b0),
static_cast<std::uint64_t>
(
static_cast<std::uint64_t>(static_cast<std::uint64_t>(a32) * b32)
+ static_cast<std::uint32_t>(mid >> static_cast<unsigned>(UINT8_C(32)))
)
};
if(mid < mid1)
{
sig128Z.v1 =
static_cast<std::uint64_t>
(
sig128Z.v1
+ static_cast<std::uint64_t>(UINT64_C(0x100000000))
);
}
mid = static_cast<std::uint64_t>(mid << static_cast<unsigned>(UINT8_C(32)));
sig128Z.v0 = static_cast<std::uint64_t>(sig128Z.v0 + mid);
sig128Z.v1 =
static_cast<std::uint64_t>
(
sig128Z.v1
+ static_cast<std::uint_fast8_t>
(
(sig128Z.v0 < mid)
? static_cast<std::uint_fast8_t>(UINT8_C(1))
: static_cast<std::uint_fast8_t>(UINT8_C(0))
)
);
if(sig128Z.v0 != static_cast<std::uint64_t>(UINT8_C(0)))
{
sig128Z.v1 =
static_cast<std::uint64_t>
(
sig128Z.v1
| static_cast<std::uint_fast8_t>(UINT8_C(1))
);
}
if(sig128Z.v1 < static_cast<std::uint64_t>(UINT64_C(0x4000000000000000)))
{
--expZ;
sig128Z.v1 <<= static_cast<unsigned>(UINT8_C(1));
}
result = softfloat_roundPackToF64(signZ, expZ, sig128Z.v1);
}
return result;
}
static constexpr auto f64_div(const std::uint64_t a, const std::uint64_t b) -> std::uint64_t
{
const auto expA = detail::expF64UI(a);
auto sigA = detail::fracF64UI(a);
const auto signZ = sign_ab(a, b);
auto result = std::uint64_t { };
const auto a_is_zero = ( (expA == static_cast<std::int16_t> ( INT8_C(0)))
&& (sigA == static_cast<std::uint64_t>(UINT8_C(0))));
if(a_is_zero)
{
result = detail::packToF64UI(signZ, static_cast<int>(INT8_C(0)), static_cast<int>(INT8_C(0)));
}
else
{
auto expZ =
static_cast<std::int16_t>
(
static_cast<std::int16_t>(expA - detail::expF64UI(b))
+ static_cast<std::int16_t>(INT16_C(0x3FE))
);
sigA =
static_cast<std::uint64_t>
(
sigA
| static_cast<std::uint64_t>(UINT64_C(0x0010000000000000))
);
auto sigB =
static_cast<std::uint64_t>
(
detail::fracF64UI(b)
| static_cast<std::uint64_t>(UINT64_C(0x0010000000000000))
);
if(sigA < sigB)
{
--expZ;
sigA = static_cast<std::uint64_t>(sigA << static_cast<unsigned>(UINT8_C(11)));
}
else
{
sigA = static_cast<std::uint64_t>(sigA << static_cast<unsigned>(UINT8_C(10)));
}
sigB <<= static_cast<unsigned>(UINT8_C(11));
const auto recip32 =
static_cast<std::uint32_t>
(
detail::softfloat_approxRecip32_1(static_cast<std::uint32_t>(sigB >> static_cast<unsigned>(UINT8_C(32))))
- static_cast<unsigned>(UINT8_C(2))