58#ifndef FP128_FLOAT128_H
59#define FP128_FLOAT128_H
91std::from_chars_result
from_chars(
const char* first,
const char* last,
float128& value);
93std::from_chars_result
from_chars(
const char* first,
const char* last,
float128& value, std::chars_format fmt);
99[[nodiscard]] std::string
render(
const float128& value,
char style, int32_t precision,
bool uppercase,
bool alternate,
char sign_char,
100 bool bare_hex =
false);
104float128 operator""_f128(
const char*);
274 if (!std::is_constant_evaluated())
287 if (!std::is_constant_evaluated())
318 [[maybe_unused]]
int saved = 0;
345 env.
flags = saved | kept;
358 template <
typename T> [[nodiscard]] T
inexact(
const T& result)
noexcept
361 return (*
this)(result);
370 template <
typename T> [[nodiscard]] T
operator()(
const T& result)
noexcept
373 const int raised = env.
flags;
374 int justified = raised & FE_INEXACT;
376 justified |= FE_INVALID;
377 else if (result.is_inf())
378 justified |= FE_OVERFLOW | FE_INEXACT;
379 else if ((result.is_zero() || result.is_subnormal()) && (raised & FE_INEXACT) != 0)
380 justified |= FE_UNDERFLOW;
387 [[maybe_unused]]
int saved = 0;
388 [[maybe_unused]]
int kept = 0;
417 default:
return FE_TONEAREST;
430 if (
round == FE_TOWARDZERO)
432 else if (
round == FE_UPWARD)
434 else if (
round == FE_DOWNWARD)
436 else if (
round != FE_TONEAREST)
440 detail::env.mode = mode;
452[[nodiscard]]
inline int fetestexcept([[maybe_unused]]
int excepts)
noexcept
455 return detail::env.flags & excepts & FE_ALL_EXCEPT;
469 detail::env.flags &= ~excepts;
482 detail::env.flags |= excepts & FE_ALL_EXCEPT;
485 return (excepts & FE_ALL_EXCEPT) != 0 ? 1 : 0;
497 if (flagp !=
nullptr)
498 *flagp =
static_cast<std::fexcept_t
>(
fetestexcept(excepts));
511inline int fesetexceptflag([[maybe_unused]]
const std::fexcept_t* flagp, [[maybe_unused]]
int excepts)
noexcept
514 if (flagp !=
nullptr) {
515 const int mask = excepts & FE_ALL_EXCEPT;
516 detail::env.flags = (detail::env.flags & ~mask) | (
static_cast<int>(*flagp) & mask);
534 0xA2F9836E4E441529, 0xFC2757D1F534DDC0, 0xDB6295993C439041, 0xFE5163ABDEBBC561,
535 0xB7246E3A424DD2E0, 0x06492EEA09D1921C, 0xFE1DEB1CB129A73E, 0xE88235F52EBB4484,
536 0xE99C7026B45F7E41, 0x3991D639835339F4, 0x9C845F8BBDF9283B, 0x1FF897FFDE05980F,
537 0xEF2F118B5A0A6D1F, 0x6D367ECF27CB09B7, 0x4F463F669E5FEA2D, 0x7527BAC7EBE5F17B,
538 0x3D0739F78A5292EA, 0x6BFB5FB11F8D5D08, 0x56033046FC7B6BAB, 0xF0CFBC209AF4361D,
539 0xA9E391615EE61B08, 0x6599855F14A06840, 0x8DFFD8804D732731, 0x06061556CA73A8C9,
540 0x60E27BC08C6B47C4, 0x19C367CDDCE8092A, 0x8359C4768B961CA6, 0xDDAF44D15719053E,
541 0xA5FF07053F7E33E8, 0x32C2DE4F98327DBB, 0xC33D26EF6B1E5EF8, 0x9F3A1F35CAF27F1D,
542 0x87F121907C7C246A, 0xFA6ED5772D30433B, 0x15C614B59D19C3C2, 0xC4AD414D2C5D000C,
543 0x467D862D71E39AC6, 0x9B0062337CD2B497, 0xA7B4D55537F63ED7, 0x1810A3FC764D2A9D,
544 0x64ABD770F87C6357, 0xB07AE715175649C0, 0xD9D63B3884A7CB23, 0x24778AD623545AB9,
545 0x1F001B0AF1DFCE19, 0xFF319F6A1E666157, 0x9947FBACD87F7EB7, 0x652289E83260BFE6,
546 0xCDC4EF09366CD43F, 0x5DD7DE16DE3B5892, 0x9BDE2822D2E88628, 0x4D58E232CAC616E3,
547 0x08CB7DE050C017A7, 0x1DF35BE01834132E, 0x6212830148835B8E, 0xF57FB0ADF2E91E43,
548 0x4A48D36710D8DDAA, 0x425FAECE616AA428, 0x0AB499D3F2A6067F, 0x775C83C2A3883C61,
549 0x78738A5A8CAFBDD7, 0x6F63A62DCBBFF4EF, 0x818D67C12645CA55, 0x36D9CAD2A8288D61,
550 0xC277C9121426049B, 0x4612C459C444C5C8, 0x91B24DF31700AD43, 0xD4E5492910D5FDFC,
551 0xBE00CC941EEECE70, 0xF53E1380F1ECC3E7, 0xB328F8C79405933E, 0x71C1B3092EF3450B,
552 0x9C12887B20AB9FB5, 0x2EC292472F327B6D, 0x550C90A7721FE76B, 0x96CB314A1679E279,
553 0x4189DFF49794E884, 0xE6E29731996BED88, 0x365F5F0EFDBBB49A, 0x486CA46742727132,
554 0x5D8DB8159F09E5BC, 0x25318D3974F71C05, 0x30010C0D68084B58, 0xEE2C90AA4702E774,
555 0x24D6BDA67DF77248, 0x6EEF169FA6948EF6, 0x91B45153D1F20ACF, 0x3398207E4BF56863,
556 0xB25F3EDD035D407F, 0x8985295255C06437, 0x10D86D324832754C, 0x5BD4714E6E5445C1,
557 0x090B69F52AD56614, 0x9D072750045DDB3B, 0xB4C576EA17F9877D, 0x6B49BA271D296996,
558 0xACCCC65414AD6AE2, 0x9089D98850722CBE, 0xA4049407777030F3, 0x27FC00A871EA49C2,
559 0x663DE06483DD9797, 0x3FA3FD94438C860D, 0xDE41319D39928C70, 0xDDE7B7173BDF082B,
560 0x3715A0805C93805A, 0x921110D8E80FAF80, 0x6C4BFFDB0F903876, 0x185915A562BBCB61,
561 0xB989C7BD401004F2, 0xD2277549F6B6EBBB, 0x22DBAA140A2F2689, 0x768364333B091A94,
562 0x0EAA3A51C2A31DAE, 0xEDAF12265C4DC26D, 0x9C7A2D9756C0833F, 0x03F6F0098C402B99,
563 0x316D07B43915200C, 0x5BC3D8C492F54BAD, 0xC6A5CA4ECD37A736, 0xA9E69492AB6842DD,
564 0xDE6319EF8C76528B, 0x6837DBFCABA1AE31, 0x15DFA1AE00DAFB0C, 0x664D64B705ED3065,
565 0x29BF56573AFF47B9, 0xF96AF3BE75DF9328, 0x3080ABF68C6615CB, 0x040622FA1DE4D9A4,
566 0xB33D8F1B5709CD36, 0xE9424EA4BE13B523, 0x331AAAF0A8654FA5, 0xC1D20F3F0BCD785B,
567 0x76F923048B7B7217, 0x8953A6C6E26E6F00, 0xEBEF584A9BB7DAC4, 0xBA66AACFCF761D02,
568 0xD12DF1B1C1998C77, 0xADC3DA4886A05DF7, 0xF480C62FF0AC9AEC, 0xDDBC5C3F6DDED01F,
569 0xC790B6DB2A3A25A3, 0x9AAF009353AD0457, 0xB6B42D297E804BA7, 0x07DA0EAA76A1597B,
570 0x2A12162DB7DCFDE5, 0xFAFEDB89FDBE896C, 0x76E4FCA90670803E, 0x156E85FF87FD073E,
571 0x2833676186182AEA, 0xBD4DAFE7B36E6D8F, 0x3967955BBF3148D7, 0x8416DF30432DC735,
572 0x6125CE70C9B8CB30, 0xFD6CBFA200A4E46C, 0x05A0DD5A476F21D2, 0x1262845CB9496170,
573 0xE0566B0152993755, 0x50B7D51EC4F1335F, 0x6E13E4305DA92E85, 0xC3B21D3632A1A4B7,
574 0x08D4B1EA21F716E4, 0x698F77FF2780030C, 0x2D408DA0CD4F99A5, 0x20D3A2B30A5D2F42,
575 0xF9B4CBDA11D0BE7D, 0xC1DB9BBD17AB81A2, 0xCA5C6A0817552E55, 0x0027F0147F8607E1,
576 0x640B148D4196DEBE, 0x872AFDDAB6256B34, 0x897BFEF3059EBFB9, 0x4F6A68A82A4A5AC4,
577 0x4FBCF82D985AD795, 0xC7F48D4D0DA63A20, 0x5F57A4B13F149538, 0x800120CC86DD71B6,
578 0xDEC9F560BF11654D, 0x6B0701ACB08CD0C0, 0xB24855510EFB1EC3, 0x72953B06A33540C0,
579 0x7BDC06CC45E0FA29, 0x4EC8CAD641F3E8DE, 0x647CD8649B31BED9, 0xC397A4D45877C5E3,
580 0x6913DAF03C3ABA46, 0x18465F7555F5BDD2, 0xC6926E5D2EACED44, 0x0E423E1C87C461E9,
581 0xFD29F3D6E7CA7C22, 0x35916FC5E0088DD7, 0xFFE26A6EC6FDB0C1, 0x0893745D7CB2AD6B,
582 0x9D6ECD7B723E6A11, 0xC6A9CFF7DF7329BA, 0xC9B55100B70DB2E2, 0x24BA74607DE58AD8,
583 0x742C150D0C188194, 0x667E162901767A9F, 0xBEFDFDEF4556367E, 0xD913D9ECB9BA8BFC,
584 0x97C427A831C36EF1, 0x36C59456A8D8B5A8, 0xB40ECCCF2D891234, 0x576F89562CE3CE99,
585 0xB920D6AA5E6B9C2A, 0x3ECC5F114A0BFDFB, 0xF4E16D3B8E2C86E2, 0x84D4E9A9B4FCD1EE,
586 0xEFC9352E61392F44, 0x2138C8D91B0AFC81, 0x6A4AFBD81C2F84B4, 0x538C994ECC2254DC,
587 0x552AD6C6C096190B, 0xB8701A649569605A, 0x26EE523F0F117F11, 0xB5F4F5CBFC2DBC34,
588 0xEEBC34CC5DE8605E, 0xDD9B8E67EF3392B8, 0x17C99B5861BC57E1, 0xC68351103ED84871,
589 0xDDDD1C2DA118AF46, 0x2C21D7F359987AD9, 0xC0549EFA864FFC06, 0x56AE79E536228922,
590 0xAD38DC9367AAE855, 0x3826829BE7CAA40D, 0x51B133990ED7A948, 0x0569F0B265A7887F,
591 0x974C8836D1F9B392, 0x214A827B21CF98DC, 0x9F405547DC3A74E1, 0x42EB67DF9DFE5FD4,
592 0x5EA4677B7AACBAA2, 0xF65523882B55BA41, 0x086E59862A218347, 0x39E6E389D49EE540,
593 0xFB49E956FFCA0F1C, 0x8A59C52BFA94C5C1, 0xD3CFC50FAE5ADB86, 0xC5476243853B8621,
594 0x94792C8761107B4C, 0x2A1A2C8012BF4390, 0x2688893C78E4C4A8, 0x7BDBE5C23AC4EAF4,
595 0x268A67F7BF920D2B, 0xA365B1933D0B7CBD, 0xDC51A463DD27DDE1, 0x6919949A9529A828,
596 0xCE68B4ED09209F44, 0xCA984E638270237C, 0x7E32B90F8EF5A7E7, 0x561408F1212A9DB5,
597 0x4D7E6F5119A5ABF9, 0xB5D6DF8261DD9602, 0x36169F3AC4A1A283, 0x6DED727A8D39A9B8,
598 0x825C326B5B2746ED, 0x34007700D255F4FC, 0x4D59018071E0E13F, 0x89B295F364A8F1AE,
599 0xA74B38FC4CEAB2BB, 0x47270BABC3A734BA, 0x6052DD34F8563AEB, 0x7E8A31BB365895B7,
674 static_assert(
sizeof(
void*) == 8,
"float128 is supported in 64 bit builds only!");
677 static constexpr int32_t EXP_BITS = 15;
678 static constexpr int32_t EXP_BIAS = 0x3FFF;
679 static constexpr int32_t ZERO_EXP_BIASED = -EXP_BIAS;
680 static constexpr int32_t ZERO_EXP_UNBIASED = 0;
681 static constexpr int32_t SUBNORM_EXP_BIASED = 0;
682 static constexpr int32_t SUBNORM_EXP_UNBIASED = -EXP_BIAS;
683 static constexpr int32_t INF_EXP_BIASED = 0x7FFF;
684 static constexpr int32_t INF_EXP_UNBIASED = INF_EXP_BIASED - EXP_BIAS;
685 static constexpr uint64_t EXP_MASK = INF_EXP_BIASED;
686 static constexpr int32_t FRAC_BITS = 112;
687 static constexpr int32_t EXP_SHIFT = FRAC_BITS - 64;
689 static constexpr uint64_t FRAC_UNITY =
FP128_ONE_SHIFT(FRAC_BITS - 64);
690 static constexpr uint64_t SIGN_MASK = 1ull << 63;
691 static constexpr uint64_t QUIET_NAN_BIT = 1ull << (EXP_SHIFT - 1);
699 struct _float128_bits {
706 enum float128_class_t {
780 auto msb = 64 -
static_cast<int32_t
>(lzcnt64(d.
f()));
782 int32_t x_expo =
static_cast<int32_t
>(d.
e()) - 1023;
785 low = d.
f() & ~(1ull << (msb - 1));
786 auto shift =
static_cast<int32_t
>(FRAC_BITS - msb + 1);
791 else if (d.
e() == 0x7FF) {
802 high |= QUIET_NAN_BIT;
830 if constexpr (std::is_floating_point_v<T>) {
831 if constexpr (std::is_same_v<T, long double> && LDBL_MANT_DIG != 53) {
833 }
else if constexpr (std::is_same_v<T, float>) {
838 const uint32_t bits = std::bit_cast<uint32_t>(x);
839 if ((bits & 0x7F800000u) == 0x7F800000u && (bits & 0x003FFFFFu) != 0 && (bits & 0x00400000u) == 0) {
841 *
this =
float128(
static_cast<double>(std::bit_cast<float>(bits | 0x00400000u)));
843 *
this =
float128(
static_cast<double>(x));
846 *
this =
float128(
static_cast<double>(x));
849 }
else if constexpr (std::is_same_v<char*, T> || std::is_same_v<unsigned char*, T> || std::is_same_v<const unsigned char*, T>) {
850 *
this =
float128(
static_cast<const char*
>(x));
852 }
else if constexpr (std::is_integral_v<T>) {
858 low =
static_cast<uint64_t
>(x);
859 if constexpr (std::is_signed_v<T>) {
871 auto shift =
static_cast<int32_t
>(FRAC_BITS - expo);
900 while (*x != 0 && isspace(
static_cast<unsigned char>(*x)))
902 const char*
const end = x + strlen(x);
903 const char* body = x;
904 if (*body ==
'-' || *body ==
'+')
906 if (body[0] ==
'0' && (body[1] ==
'x' || body[1] ==
'X')) {
910 if (
from_chars(body + 2, end, *
this, std::chars_format::hex).ec != std::errc::invalid_argument && *x ==
'-') {
992 static_assert(P >= 2 && P <= 64,
"the significand has to fit in a QWORD");
993 constexpr uint64_t all_ones = (P == 64) ? UINT64_MAX : ((1ull << P) - 1);
996 uint64_t l = 0, h = 0;
1006 res.
lsb = ((
e < EMIN) ? EMIN :
e) - (P - 1);
1007 const int32_t drop = res.
lsb - (
e - FRAC_BITS);
1011 bool tiny =
e < EMIN;
1012 if (
e == EMIN - 1) {
1013 uint64_t tl = l, th = h, textra = 0;
1029 const bool carry = (P == 64) ? (res.
sig == 0) : ((res.
sig >> (P % 64)) != 0);
1033 res.
sig = 1ull << (P - 1);
1039 if (res.
lsb + (P - 1) > EMAX) {
1043 res.
lsb = EMAX - (P - 1);
1061 return (shift >= 64) ? (frac_high >> (shift - 64)) : ((frac_high << (64 - shift)) | (
low >> shift));
1072 const uint64_t sign =
static_cast<uint64_t
>(
get_sign()) << 63;
1077 return std::bit_cast<double>(sign | (0x7FFull << 52) | (1ull << 51) |
nan_payload_top(FRAC_BITS - 1 - 51));
1079 return std::bit_cast<double>(sign | (0x7FFull << 52));
1082 return std::bit_cast<double>(sign);
1086 return std::bit_cast<double>(sign | (0x7FFull << 52));
1090 return std::bit_cast<double>(sign + (
static_cast<uint64_t
>(n.
lsb + 52 + 1023 - 1) << 52) + n.
sig);
1091 return std::bit_cast<double>(sign | n.
sig);
1101 const uint32_t sign =
get_sign() << 31;
1106 return std::bit_cast<float>(sign | 0x7F800000u | 0x00400000u |
static_cast<uint32_t
>(
nan_payload_top(FRAC_BITS - 1 - 22)));
1108 return std::bit_cast<float>(sign | 0x7F800000u);
1111 return std::bit_cast<float>(sign);
1115 return std::bit_cast<float>(sign | 0x7F800000u);
1116 const uint32_t sig =
static_cast<uint32_t
>(n.
sig);
1118 return std::bit_cast<float>(sign + (
static_cast<uint32_t
>(n.
lsb + 23 + 127 - 1) << 23) + sig);
1119 return std::bit_cast<float>(sign | sig);
1143 uint64_t l = 0, h = 0;
1153 const int32_t shift = FRAC_BITS -
e;
1154 return (shift >= 64) ? (h >> (shift - 64)) : ((l >> shift) | (h << (64 - shift)));
1169 const bool negative =
get_sign() != 0;
1170 bool overflow =
false;
1172 if constexpr (std::is_signed_v<I>) {
1174 const uint64_t limit =
static_cast<uint64_t
>(std::numeric_limits<I>::max()) + (negative ? 1u : 0u);
1175 if (overflow || magnitude > limit) {
1177 return negative ? std::numeric_limits<I>::min() : std::numeric_limits<I>::max();
1180 return static_cast<I
>(negative ? (0ull - magnitude) : magnitude);
1183 if (negative && magnitude != 0) {
1187 if (overflow || magnitude > std::numeric_limits<I>::max()) {
1189 return std::numeric_limits<I>::max();
1191 return static_cast<I
>(magnitude);
1222#if LDBL_MANT_DIG == 64
1226 uint16_t sign_exponent;
1227 unsigned char padding[
sizeof(
long double) - 10];
1229 static_assert(
sizeof(x87_bits) ==
sizeof(
long double),
"unexpected x87 long double layout");
1230#elif LDBL_MANT_DIG == 113
1232 struct binary128_bits {
1236 static_assert(
sizeof(binary128_bits) ==
sizeof(
long double),
"unexpected binary128 long double layout");
1246#if LDBL_MANT_DIG == 64
1247 const x87_bits bits = std::bit_cast<x87_bits>(x);
1248 const uint32_t sign =
static_cast<uint32_t
>(bits.sign_exponent >> 15);
1249 const int32_t biased = bits.sign_exponent & 0x7FFF;
1250 if (biased == 0x7FFF) {
1253 const uint64_t fraction = bits.mantissa & ~(1ull << 63);
1254 float128 res(fraction << 49, (
static_cast<uint64_t
>(sign) << 63) | (
static_cast<uint64_t
>(INF_EXP_BIASED) << EXP_SHIFT) | (fraction >> 15));
1262 return norm_round_pack(sign, ((biased == 0) ? 1 : biased) - EXP_BIAS - 63 + FRAC_BITS, bits.mantissa, 0,
false);
1263#elif LDBL_MANT_DIG == 113
1264 const binary128_bits bits = std::bit_cast<binary128_bits>(x);
1265 return float128(bits.low, bits.high);
1269 return float128(
static_cast<double>(x));
1283#if LDBL_MANT_DIG == 64
1285 const uint16_t sign =
static_cast<uint16_t
>(
get_sign() << 15);
1287 bits.sign_exponent =
static_cast<uint16_t
>(sign | 0x7FFF);
1288 bits.mantissa = 1ull << 63;
1295 bits.sign_exponent = sign;
1301 bits.sign_exponent =
static_cast<uint16_t
>(sign | 0x7FFF);
1302 bits.mantissa = 1ull << 63;
1304 bits.mantissa = n.
sig;
1305 const int32_t biased = (n.
sig >> 63) ? (n.
lsb + 63 + EXP_BIAS) : 0;
1306 bits.sign_exponent =
static_cast<uint16_t
>(sign |
static_cast<uint16_t
>(biased));
1309 return std::bit_cast<long double>(bits);
1310#elif LDBL_MANT_DIG == 113
1311 return std::bit_cast<long double>(binary128_bits {
low,
high});
1313 return operator double();
1321 [[nodiscard]]
FP128_INLINE operator std::string() const noexcept {
return operator char*(); }
1331 static thread_local char str[64];
1332 const std::string text =
detail::render(*
this,
'\0', -1,
false,
false,
'-');
1333 const size_t length = (text.size() <
sizeof(str) - 1) ? text.size() :
sizeof(str) - 1;
1334 for (
size_t i = 0; i < length; ++i)
1351 if (str ==
nullptr || buff_size < 1)
1354 const std::string text =
detail::render(*
this,
'e', -1,
false,
false,
'-');
1355 const size_t length = (text.size() <
static_cast<size_t>(buff_size) - 1) ? text.size() :
static_cast<size_t>(buff_size) - 1;
1356 for (
size_t i = 0; i < length; ++i)
1367 static constexpr int32_t
SCALE_LIMIT = 2 * (EXP_BIAS + FRAC_BITS + 1);
1432 return temp >>=
static_cast<int32_t
>(shift);
1443 return temp <<= static_cast<int32_t>(shift);
1457 if (
is_nan() || rhs.is_nan()) {
1475 if (rhs.is_zero()) {
1485 uint32_t sign, rhs_sign;
1486 int32_t expo, rhs_expo;
1487 uint64_t l1, h1, l2, h2;
1489 rhs.get_components(l2, h2, rhs_expo, rhs_sign);
1494 constexpr int32_t room = 127 - 2 - FRAC_BITS;
1500 if (expo - rhs_expo > FRAC_BITS + room) {
1504 if (rhs_expo - expo > FRAC_BITS + room) {
1513 if (expo < rhs_expo) {
1516 std::swap(expo, rhs_expo);
1517 std::swap(sign, rhs_sign);
1520 const int32_t diff = expo - rhs_expo;
1533 if (sign == rhs_sign) {
1534 const uint8_t carry = addcarryx_u64(0, l1, l2, &l1);
1535 addcarryx_u64(carry, h1, h2, &h1);
1537 uint8_t borrow = subborrow_u64(0, l1, l2, &l1);
1538 borrow = subborrow_u64(borrow, h1, h2, &h1);
1545 if ((l1 | h1) == 0) {
1583 if (
is_nan() || rhs.is_nan()) {
1587 if ((
is_inf() && rhs.is_zero()) || (
is_zero() && rhs.is_inf())) {
1593 const uint32_t res_sign =
get_sign() ^ rhs.get_sign();
1597 }
else if (
is_zero() || rhs.is_zero()) {
1599 const uint32_t res_sign =
get_sign() ^ rhs.get_sign();
1605 uint32_t sign, rhs_sign;
1606 int32_t expo, rhs_expo;
1607 uint64_t l1, h1, l2, h2;
1609 rhs.get_components(l2, h2, rhs_expo, rhs_sign);
1616 bool rhs_is_exp2 = rhs.is_exponent_of_2();
1622 if (is_exp2 || rhs_is_exp2) {
1637 uint64_t temp1[2], temp2[2];
1640 res[0] = mulx_u64(l1, l2, &res[1]);
1643 res[2] = mulx_u64(h1, h2, &res[3]);
1646 temp1[0] = mulx_u64(l1, h2, &temp1[1]);
1647 uint8_t carry = addcarryx_u64(0, res[1], temp1[0], &res[1]);
1648 res[3] += addcarryx_u64(carry, res[2], temp1[1], &res[2]);
1651 temp2[0] = mulx_u64(h1, l2, &temp2[1]);
1652 carry = addcarryx_u64(0, res[1], temp2[0], &res[1]);
1653 res[3] += addcarryx_u64(carry, res[2], temp2[1], &res[2]);
1657 constexpr int32_t index = 1;
1658 constexpr int32_t lsb = (FRAC_BITS & 63) - 1;
1665 const uint64_t extra =
norm_product(l1, h1, expo, sticky_bits != 0);
1666 *
this =
round_pack(sign ^ rhs_sign, expo, l1, h1, extra);
1717 res[0] = mulx_u64(l, l, &res[1]);
1720 res[2] = mulx_u64(h, h, &res[3]);
1723 cross[0] = mulx_u64(l, h, &cross[1]);
1725 uint8_t carry = addcarryx_u64(0, res[1], cross[0], &res[1]);
1726 res[3] += addcarryx_u64(carry, res[2], cross[1], &res[2]);
1728 carry = addcarryx_u64(0, res[1], cross[0], &res[1]);
1729 res[3] += addcarryx_u64(carry, res[2], cross[1], &res[2]);
1732 constexpr int32_t index = 1;
1733 constexpr int32_t lsb = (FRAC_BITS & 63) - 1;
1740 const uint64_t extra =
norm_product(l, h, expo, sticky_bits != 0);
1788 uint32_t sign, rhs_sign;
1789 int32_t expo, rhs_expo;
1790 uint64_t l1, h1, l2, h2;
1805 const uint64_t nom[4] = {0, 0, l1, h1};
1806 const uint64_t denom[2] = {l2, h2};
1823 const int32_t top =
static_cast<int32_t
>(q[2] & 1);
1824 const int32_t shift = (127 - FRAC_BITS) + top;
1827 const uint64_t extra = (q[0] << (64 - shift)) | (((rem[0] | rem[1]) != 0) ? 1 : 0);
1831 *
this =
round_pack(sign ^ rhs_sign, expo, l1, h1, extra);
1921 return is_nan() && (
high & QUIET_NAN_BIT) == 0;
1966 if (expo >= FRAC_BITS)
1988 int32_t frac_bits =
static_cast<int32_t
>(FRAC_BITS) - expo;
1990 if (frac_bits > FRAC_BITS)
1997 if (frac_bits <= 64) {
2005 if (l == 0 && h == 0) {
2010 int32_t msb =
static_cast<int32_t
>(
log2(l, h));
2011 int32_t shift = FRAC_BITS - msb;
2081 return is_positive() ? positiveInfinity : negativeInfinity;
2084 return is_positive() ? positiveZero : negativeZero;
2087 return is_positive() ? positiveSubnormal : negativeSubnormal;
2090 return is_positive() ? positiveNormal : negativeNormal;
2106 assert(
e <= INF_EXP_BIASED);
2140 auto shift = FRAC_BITS -
static_cast<int32_t
>(
log2(l, h));
2151 e = SUBNORM_EXP_UNBIASED + 1 - shift;
2202 uint64_t sticky = extra;
2204 extra = l << (64 - dist);
2205 l = (l >> dist) | (h << (64 - dist));
2214 extra = h << (128 - dist);
2215 l = h >> (dist - 64);
2219 extra = (dist == 128) ? h : ((h != 0) ? 1 : 0);
2225 extra |= (sticky != 0) ? 1 : 0;
2247 const uint64_t lost = l << (64 - dist);
2248 l = (l >> dist) | (h << (64 - dist)) | ((lost != 0) ? 1 : 0);
2250 }
else if (dist < 128) {
2251 const uint64_t lost = (dist == 64) ? l : (l | (h << (128 - dist)));
2252 l = ((dist == 64) ? h : (h >> (dist - 64))) | ((lost != 0) ? 1 : 0);
2255 l = ((l | h) != 0) ? 1 : 0;
2270 return extra >= (1ull << 63);
2297 const bool tiny = (
exp < -1) || !increment || h !=
MAX_SIG_HIGH || l != UINT64_MAX;
2300 if (tiny && extra != 0)
2303 }
else if (
exp > INF_EXP_BIASED - 2 || (
exp == INF_EXP_BIASED - 2 && increment && h ==
MAX_SIG_HIGH && l == UINT64_MAX)) {
2308 return float128(0, (
static_cast<uint64_t
>(sign) << 63) | (
static_cast<uint64_t
>(INF_EXP_BIASED) << EXP_SHIFT));
2309 return float128(UINT64_MAX, (
static_cast<uint64_t
>(sign) << 63) | (
static_cast<uint64_t
>(INF_EXP_BIASED - 1) << EXP_SHIFT) | UPPER_FRAC_MASK);
2316 h += (l == 0) ? 1 : 0;
2319 }
else if ((l | h) == 0) {
2322 return float128(l, (
static_cast<uint64_t
>(sign) << 63) + (
static_cast<uint64_t
>(
exp) << EXP_SHIFT) + h);
2357 const int32_t
exp =
e + (EXP_BIAS - 1);
2360 if (
static_cast<uint32_t
>(
exp) >=
static_cast<uint32_t
>(INF_EXP_BIASED - 2))
2367 const uint64_t increment =
round_increment(mode, sign, extra) ? 1 : 0;
2368 h += addcarryx_u64(0, l, increment, &l);
2371 l &= ~(((extra << 1) == 0) ? increment : 0);
2372 return float128(l, (
static_cast<uint64_t
>(sign) << 63) + (
static_cast<uint64_t
>(
exp) << EXP_SHIFT) + h);
2391 return float128(0,
static_cast<uint64_t
>(sign) << 63);
2393 const int32_t msb =
static_cast<int32_t
>(
log2(l, h));
2394 uint64_t extra = sticky ? 1 : 0;
2395 if (msb > FRAC_BITS) {
2397 }
else if (msb < FRAC_BITS) {
2400 return round_pack(sign,
e + msb - FRAC_BITS, l, h, extra);
2433 *
this =
float128(0,
static_cast<uint64_t
>(s != 0) << 63);
2436 *
this =
round_pack((s != 0) ? 1u : 0u,
e, l, h, 0);
2460 if (a.is_signaling() || b.is_signaling())
2467 if (a.is_signaling() || b.is_signaling() || c.is_signaling())
2469 return quiet_nan(a.is_nan() ? a : (b.is_nan() ? b : c));
2474 if (a.is_signaling())
2540 const int32_t keep = FRAC_BITS -
e;
2549 if (l == 0 && h == 0)
2554 const int32_t shift =
e + 2 * 64 + (
FIXED_WORDS * 64 - 2 * 64) - FRAC_BITS;
2556 const int32_t word = shift >> 6;
2557 const int32_t bit = shift & 63;
2564 frac[word] = l << bit;
2566 frac[word + 1] = (l >> (64 - bit)) | (h << bit);
2568 frac[word + 2] = h >> (64 - bit);
2584 const uint64_t lo = mulx_u64(frac[i], mul, &hi);
2585 const uint8_t c = addcarryx_u64(0, lo, carry, &frac[i]);
2633 res[0] = mulx_u64(al, bl, &hi);
2638 uint64_t lo = mulx_u64(al, bh, &hi);
2639 unsigned char c = addcarryx_u64(0, res[1], lo, &res[1]);
2640 c = addcarryx_u64(c, res[2], hi, &res[2]);
2643 lo = mulx_u64(ah, bl, &hi);
2644 c = addcarryx_u64(0, res[1], lo, &res[1]);
2645 c = addcarryx_u64(c, res[2], hi, &res[2]);
2648 lo = mulx_u64(ah, bh, &hi);
2649 c = addcarryx_u64(0, res[2], lo, &res[2]);
2660 for (int32_t i = words - 1; i >= 0; --i) {
2662 return i * 64 + 63 -
static_cast<int32_t
>(lzcnt64(a[i]));
2680 [[nodiscard]]
FP128_INLINE static constexpr bool wide_place(uint64_t* acc,
const uint64_t* src, int32_t words, int32_t offset)
noexcept
2683 const int32_t word = offset >> 6;
2684 const int32_t bit = offset & 63;
2685 for (int32_t i = 0; i < words; ++i) {
2686 const int32_t dst = i + word;
2689 acc[dst] |= src[i] << bit;
2691 acc[dst + 1] |= src[i] >> (64 - bit);
2696 const int32_t drop = -offset;
2697 if (drop >= words * 64) {
2699 for (int32_t i = 0; i < words; ++i)
2704 const int32_t word = drop >> 6;
2705 const int32_t bit = drop & 63;
2707 for (int32_t i = 0; i < word; ++i)
2712 for (int32_t i = 0; word + i < words; ++i) {
2713 uint64_t value = src[word + i] >> bit;
2714 if (bit != 0 && word + i + 1 < words)
2715 value |= src[word + i + 1] << (64 - bit);
2725 unsigned char c = 0;
2727 c = addcarryx_u64(c, a[i], b[i], &a[i]);
2733 unsigned char borrow = 0;
2735 const uint64_t rhs = b[i];
2736 const uint64_t diff = a[i] - rhs - borrow;
2737 borrow = (a[i] < rhs || (borrow && a[i] == rhs)) ? 1 : 0;
2754 for (int32_t i =
WIDE_WORDS - 1; i >= 0; --i) {
2756 return (a[i] > b[i]) ? 1 : -1;
2782 const int32_t shift = 1 +
static_cast<int32_t
>(h >> 50);
2785 const uint64_t extra = (l << (64 - shift)) | (sticky ? 1 : 0);
2806 case positiveInfinity:
2808 case negativeInfinity:
2809 return float128(UINT64_MAX, UINT64_MAX, EXP_MASK - 1, 1);
2815 case positiveSubnormal:
2816 case positiveNormal:
2822 case negativeSubnormal:
2823 case negativeNormal:
2825 if (x.
low == UINT64_MAX) {
2843 case negativeInfinity:
2845 case positiveInfinity:
2846 return float128(UINT64_MAX, UINT64_MAX, EXP_MASK - 1, 0);
2852 case negativeSubnormal:
2853 case negativeNormal:
2859 case positiveSubnormal:
2860 case positiveNormal:
2862 if (x.
low == UINT64_MAX) {
2912 return float128(0x95355FB8AC404E7A, 0x5BF0A8B14576, 0x4000, 0);
2922 return float128(0xC908B2FB1366EA95, 0x00006A09E667F3BC, 0x3FFF, 0);
2941 return float128(0x999999999999999A, 0x999999999999, EXP_BIAS - 4, 0u);
3008 if (std::is_constant_evaluated())
3030 base_lo =
float128(0x999999999999999A, 0xBF89999999999999);
3038 const float128 cross = error + (hi * bl + lo * bh);
3040 lo = cross - (hi - p);
3044 multiply(res_hi, res_lo, base_hi, base_lo);
3048 multiply(base_hi, base_lo, base_hi, base_lo);
3050 return res_hi + res_lo;
3080#ifdef FP128_IEEE_ENV
3155 template <
typename T>
3156 requires std::is_arithmetic_v<T>
3162 template <
typename T>
3163 requires std::is_arithmetic_v<T>
3169 template <
typename T>
3170 requires std::is_arithmetic_v<T>
3176 template <
typename T>
3177 requires std::is_arithmetic_v<T>
3196 template <
typename T>
3197 requires std::is_arithmetic_v<T>
3200 return float128(lhs) <<=
static_cast<int32_t
>(rhs);
3209 template <
typename T>
3210 requires std::is_arithmetic_v<T>
3213 return float128(lhs) >>=
static_cast<int32_t
>(rhs);
3230 if (lhs.is_nan() || rhs.is_nan()) {
3231 if (lhs.is_signaling() || rhs.is_signaling())
3236 if (lhs.is_zero() && rhs.is_zero())
3238 return lhs.high == rhs.high && lhs.low == rhs.low;
3245 template <
typename T>
3246 requires std::is_arithmetic_v<T>
3250 template <
typename T>
3251 requires std::is_arithmetic_v<T>
3265 template <
typename T>
3266 requires std::is_arithmetic_v<T>
3270 template <
typename T>
3271 requires std::is_arithmetic_v<T>
3284 if (lhs.is_nan() || rhs.is_nan()) {
3290 if (lhs.is_zero() && rhs.is_zero())
3293 auto rhs_sign = rhs.get_sign();
3294 auto lhs_sign = lhs.get_sign();
3297 if (lhs_sign != rhs_sign)
3298 return lhs_sign > rhs_sign;
3301 if (lhs.high == rhs.high)
3302 return (lhs_sign) ? lhs.low > rhs.low : lhs.low < rhs.low;
3304 return (lhs_sign) ? lhs.high > rhs.high : lhs.high < rhs.high;
3307 template <
typename T>
3310 template <
typename T>
3322 if (lhs.is_nan() || rhs.is_nan()) {
3326 return !(lhs > rhs);
3329 template <
typename T>
3332 template <
typename T>
3345 if (lhs.is_nan() || rhs.is_nan()) {
3350 if (lhs.is_zero() && rhs.is_zero())
3353 auto rhs_sign = rhs.get_sign();
3354 auto lhs_sign = lhs.get_sign();
3357 if (lhs_sign != rhs_sign)
3358 return lhs_sign < rhs_sign;
3361 if (lhs.high == rhs.high)
3362 return (lhs_sign) ? lhs.low < rhs.low : lhs.low > rhs.low;
3364 return (lhs_sign) ? lhs.high < rhs.high : lhs.high > rhs.high;
3367 template <
typename T>
3370 template <
typename T>
3381 if (lhs.is_nan() || rhs.is_nan()) {
3385 return !(lhs < rhs);
3388 template <
typename T>
3391 template <
typename T>
3449 bool signal_inexact)
noexcept
3453 const int32_t expo = x.get_exponent();
3455 if (expo >= FRAC_BITS || x.is_zero())
3458 const uint32_t sign = x.get_sign();
3459 const uint64_t sign_bit =
static_cast<uint64_t
>(sign) << 63;
3467 one = expo == -1 && (x.get_fraction_bits() | x.low) != 0;
3469 one = round_up_away;
3472 return float128(0, sign_bit | (
one ? (
static_cast<uint64_t
>(EXP_BIAS) << EXP_SHIFT) : 0));
3477 const int32_t units = FRAC_BITS - expo;
3478 const uint64_t mask_low = (units >= 64) ? UINT64_MAX : ((1ull << units) - 1);
3479 const uint64_t mask_high = (units >= 64) ? ((1ull << (units - 64)) - 1) : 0;
3480 uint64_t l = x.low, h = x.high;
3481 if (((l & mask_low) | (h & mask_high)) == 0)
3484 const int32_t half_index = units - 1;
3485 const uint64_t half_low = (half_index < 64) ? (1ull << half_index) : 0;
3486 const uint64_t half_high = (half_index < 64) ? 0 : (1ull << (half_index - 64));
3487 const bool at_or_above_half = ((l & half_low) | (h & half_high)) != 0;
3488 const bool rest = ((l & mask_low & ~half_low) | (h & mask_high & ~half_high)) != 0;
3489 const bool odd = ((units < 64) ? (l >> units) : (h >> (units - 64))) & 1;
3493 up = at_or_above_half;
3495 up = at_or_above_half && (rest || odd);
3505 h += 1ull << (units - 64);
3507 const uint64_t unit = 1ull << units;
3509 h += (l < unit) ? 1 : 0;
3524 if (x.is_nan() || x > std::numeric_limits<I>::max() || x < std::numeric_limits<I>::min()) {
3528 return x.to_integer<I>();
3614 if (x.is_zero() || x.is_special()) {
3618 return x.is_nan() ? FP_ILOGBNAN : INT_MAX;
3621 uint64_t l = 0, h = 0;
3624 x.get_components(l, h, expo, sign);
3648 if (x.is_nan() || y.is_nan())
3653 if (y.is_zero() || x.is_inf())
3670 uint64_t lx = 0, hx = 0, ly = 0, hy = 0;
3671 int32_t ex = 0, ey = 0;
3672 uint32_t sx = 0, sy = 0;
3673 x.get_components(lx, hx, ex, sx);
3674 y.get_components(ly, hy, ey, sy);
3685 int32_t shift = ex - ey;
3689 constexpr int32_t block = 15;
3690 while (shift > 0 && !rem.
is_zero()) {
3691 const int32_t step = (shift < block) ? shift : block;
3704 uint64_t rl = 0, rh = 0;
3706 const int32_t msb =
static_cast<int32_t
>(
log2(rl, rh));
3722 if (iptr ==
nullptr)
3732 return float128(0,
static_cast<uint64_t
>(x.get_sign()) << 63);
3747 if (x.is_nan() || y.is_nan())
3749 return (x > y) ? x - y :
float128();
3763 if (x.is_signaling() || y.is_signaling())
3771 if (x.is_zero() && y.is_zero())
3772 return x.is_negative() ? x : y;
3773 return (x < y) ? x : y;
3783 if (x.is_signaling() || y.is_signaling())
3789 if (x.is_zero() && y.is_zero())
3790 return x.is_positive() ? x : y;
3791 return (x > y) ? x : y;
3802 if (x.is_signaling() || y.is_signaling())
3804 if (x.is_inf() || y.is_inf())
3806 if (x.is_nan() || y.is_nan())
3817 const int32_t expo =
ilogb(largest);
3842 for (int32_t i = 3; i >= 0; --i) {
3844 return (sq[i] > n[i]) ? 1 : -1;
3878 if (x.is_negative())
3886 uint64_t l = 0, h = 0;
3889 x.get_components(l, h, expo, sign);
3890 const int32_t odd = expo & 1;
3891 const float128 norm_x(l, h,
static_cast<uint32_t
>(EXP_BIAS + odd), 0);
3892 const int32_t half_expo = (expo - odd) / 2;
3903 root =
::sqrt(
static_cast<double>(norm_x));
3904 for (int32_t i = 0; i < 2; ++i)
3905 root = (norm_x / root + root) >> 1;
3911 uint64_t rl = 0, rh = 0;
3912 int32_t root_expo = 0;
3913 uint32_t root_sign = 0;
3915 if (root_expo > 0) {
3923 const uint64_t n[4] = {0, l << 48, (l >> 16) | (h << 48), h >> 16};
3932 uint64_t nl = rl + 1, nh = rh + ((rl == UINT64_MAX) ? 1 : 0);
3942 uint64_t rem_l = 0, rem_h = 0;
3943 const uint8_t borrow = subborrow_u64(0, n[0], sq[0], &rem_l);
3944 subborrow_u64(borrow, n[1], sq[1], &rem_h);
3946 const bool exact = (rem_l | rem_h) == 0;
3947 const bool above_half = (rem_h > rh) || (rem_h == rh && rem_l > rl);
3948 const uint64_t extra = above_half ? ((1ull << 63) | 1) : (exact ? 0 : 1);
3949 return round_pack(0, half_expo, rl, rh, extra);
3965 if (x.is_zero() || x.is_inf())
3976 uint64_t l = 0, h = 0;
3979 x.get_components(l, h, expo, sign);
3980 int32_t rem = expo % 3;
3983 const float128 norm_x(l, h,
static_cast<uint32_t
>(EXP_BIAS + rem), 0);
3984 const int32_t third_expo = (expo - rem) / 3;
3995 const auto x2 = norm_x << 1;
3996 for (
auto i = iterations; i != 0; --i) {
3997 float128 r_cube = root * root * root;
3998 root = root * (r_cube + x2) / ((r_cube << 1) + norm_x);
4001 root =
ldexp(root, third_expo);
4003 return filter(root);
4037 float128(0x0000000000000000, 0x000000000000, 0x3FFF, 0),
4038 float128(0x0000000000000000, 0x000000000000, 0x3FFF, 0),
4039 float128(0x0000000000000000, 0x000000000000, 0x3FFE, 0),
4040 float128(0x5555555555555555, 0x555555555555, 0x3FFC, 0),
4041 float128(0x5555555555555555, 0x555555555555, 0x3FFA, 0),
4042 float128(0x1111111111111111, 0x111111111111, 0x3FF8, 0),
4043 float128(0x6C16C16C16C16C17, 0x6C16C16C16C1, 0x3FF5, 0),
4044 float128(0xA01A01A01A01A01A, 0xA01A01A01A01, 0x3FF2, 0),
4045 float128(0xA01A01A01A01A01A, 0xA01A01A01A01, 0x3FEF, 0),
4046 float128(0x38FAAC1C88E50017, 0x71DE3A556C73, 0x3FEC, 0),
4047 float128(0xC72EF016D3EA6679, 0x27E4FB7789F5, 0x3FE9, 0),
4048 float128(0x38FE747E4B837DC7, 0xAE64567F544E, 0x3FE5, 0),
4049 float128(0x7B544DA987ACFE85, 0x1EED8EFF8D89, 0x3FE2, 0),
4050 float128(0x97CA38331D23AF68, 0x6124613A86D0, 0x3FDE, 0),
4051 float128(0xD20BADF145DFA3E5, 0x93974A8C07C9, 0x3FDA, 0),
4052 float128(0xF11D8656B0EE8CB0, 0xAE7F3E733B81, 0x3FD6, 0),
4053 float128(0xF11D8656B0EE8CB0, 0xAE7F3E733B81, 0x3FD2, 0),
4054 float128(0xA6B2605197771B00, 0x952C77030AD4, 0x3FCE, 0),
4055 float128(0x77BB004886A2C2AB, 0x6827863B97D9, 0x3FCA, 0),
4056 float128(0x724CA1EC3B7B9675, 0x2F49B4681415, 0x3FC6, 0),
4057 float128(0x507A9CAD2BF8F0BB, 0xE542BA402022, 0x3FC1, 0),
4058 float128(0x18BEF146FCEE6E45, 0x71B8EF6DCF57, 0x3FBD, 0),
4059 float128(0x29450C90B7F338EC, 0x0CE396DB7F85, 0x3FB9, 0),
4060 float128(0x9D97B8704DD7F628, 0x761B41316381, 0x3FB4, 0),
4061 float128(0x7CCA4B4067CA9D8A, 0xF2CF01972F57, 0x3FAF, 0),
4062 float128(0x8D4E44A419776F11, 0x3F3CCDD165FA, 0x3FAB, 0),
4063 float128(0x9A38F2050BA6B015, 0x88E85FC6A4E5, 0x3FA6, 0),
4064 float128(0x320A9A18F15D4277, 0xD1AB1C2DCCEA, 0x3FA1, 0),
4065 float128(0xD373C5C51C354A8D, 0x0A18A2635085, 0x3F9D, 0),
4066 float128(0xD7ABE30E7766F129, 0x259F98B4358A, 0x3F98, 0),
4067 float128(0xE60CADED4C2989C5, 0x3932C5047D60, 0x3F93, 0),
4068 float128(0xC42E1EE46FA6BFC4, 0x434D2E783F5B, 0x3F8E, 0),
4069 float128(0xC42E1EE46FA6BFC4, 0x434D2E783F5B, 0x3F89, 0),
4070 float128(0x1B5382CDFFA97422, 0x3981254DD0D5, 0x3F84, 0),
4071 float128(0xA13F8A2B4AF9D6B7, 0x2710231C0FD7, 0x3F7F, 0),
4072 float128(0xF2833C7F5A7E0624, 0x0DC59C716D91, 0x3F7A, 0),
4073 float128(0x92B06B8D12A7275C, 0xDF983290C2CA, 0x3F74, 0),
4074 float128(0xAF4C78B15C3D89D3, 0x9EC8D1C94E85, 0x3F6F, 0),
4075 float128(0xAE913D370A4EC4E8, 0x5D4ACB9C0C3A, 0x3F6A, 0),
4076 float128(0xDE0104476AEB4C3B, 0x1E99449A4BAC, 0x3F65, 0),
4077 float128(0x3001A07244ABAD2B, 0xCA8ED42A12AE, 0x3F5F, 0),
4078 float128(0x0C7E25CFD1B1B2DD, 0x65E61C39D024, 0x3F5A, 0),
4079 float128(0x836C4D9225DCB90A, 0x10AF527530DE, 0x3F55, 0),
4080 float128(0x22DCBAE56DEF3720, 0x95DB45257E51, 0x3F4F, 0),
4081 float128(0xA4FD9F327E7F6DE9, 0x272B1B03FEC6, 0x3F4A, 0),
4082 float128(0x78E02C5E91C64CAC, 0xA3CB87222064, 0x3F44, 0),
4083 float128(0x062CA46E4F25C608, 0x240804F65951, 0x3F3F, 0),
4084 float128(0x9B78B454D8B628E5, 0x8DA8E0A127EB, 0x3F39, 0),
4085 float128(0x67A5CD8DE5CEC5EE, 0x091B406B6FF2, 0x3F34, 0),
4086 float128(0x5D94A3FD410E1232, 0x5A42F0DFEB08, 0x3F2E, 0),
4087 float128(0x8205F0A053453602, 0xBB36F6E12CD7, 0x3F28, 0),
4090 static_assert(series_len == 51);
4092 res = (x >= 0 && x < series_len) ? c[x] :
float128();
4111 constexpr int32_t arr_size = 100;
4115 static constexpr table_t table = [] {
4120 for (int32_t i = 2; i < arr_size - 1; i += 2) {
4121 t.c[i] = t.c[i - 2] * i;
4122 t.c[i + 1] = t.c[i - 1] * (i + 1);
4164 for (int32_t n =
EXP_TERMS - 1; n >= 0; --n) {
4166 acc = factorial + r * acc;
4195 return x.is_negative() ?
float128() : x;
4215 constexpr float128 ln2_hi(0xF35793C767300000, 0x62E42FEFA39E, 0x3FFE, 0);
4216 constexpr float128 ln2_mid(0x93394C5B16C50000, 0xF97B57A079A1, 0x3F98, 0);
4217 constexpr float128 ln2_lo(0x7CF70EC40DBD7593, 0xA2EB71755F45, 0x3F32, 0);
4223 const float128 r = ((x - kf * ln2_hi) - kf * ln2_mid) - kf * ln2_lo;
4243 return x.is_negative() ?
float128() : x;
4257 const int32_t k =
static_cast<int32_t
>(
llround(x));
4285 constexpr int32_t terms = 30;
4288 for (int32_t n = terms - 1; n >= 0; --n) {
4290 acc = factorial + x * acc;
4292 return filter(x * acc);
4311 uint32_t expo = (n < 0) ? (0u -
static_cast<uint32_t
>(n)) :
static_cast<uint32_t
>(n);
4365 if (y.is_zero() || x == one_value) {
4366 if (x.is_signaling() || y.is_signaling())
4370 if (x.is_nan() || y.is_nan())
4375 const bool negative_odd = x.is_negative() && y_odd;
4377 if (y.is_negative()) {
4381 return negative_odd ? -
inf() :
inf();
4383 return negative_odd ? x :
float128();
4389 return ((a > one_value) != y.is_negative()) ?
inf() :
float128();
4392 if (y.is_negative())
4394 return negative_odd ? -
inf() :
inf();
4398 if (x.is_negative() && !y.is_int())
4416 const int32_t a_expo =
ilogb(a);
4422 return filter(negative_odd ? -res : res);
4446 if (x.is_negative()) {
4487 return t - (
sqr(t) >> 1);
4495 static const auto odd_reciprocal = [] {
4496 std::array<float128, LOG1P_TERMS + 1> table {};
4504 acc = odd_reciprocal[
static_cast<size_t>(k)] + w * acc;
4506 return (s * acc) << 1;
4558 uint64_t frac_low = 0, frac_high = 0;
4560 uint32_t mantissa_sign = 0;
4561 x.get_components(frac_low, frac_high, expo, mantissa_sign);
4562 frac_high &= UPPER_FRAC_MASK;
4565 if ((frac_low | frac_high) == 0) {
4570 const size_t j =
static_cast<size_t>(frac_high >> (FRAC_BITS - 64 -
log2_reduction_bits));
4575 const bool just_below_one = (expo == -1) && (j >= entries - 2);
4578 uint64_t z_low = 0, z_high = 0;
4579 uint32_t z_sign = 0;
4585 z_low = frac_low << 16;
4586 z_high = (frac_high << 16) | (frac_low >> 48);
4587 }
else if (just_below_one) {
4594 const uint64_t m_low = frac_low << 15;
4595 const uint64_t m_high = (1ull << 63) | (frac_high << 15) | (frac_low >> 49);
4596 const uint8_t borrow = subborrow_u64(0, 0, m_low, &z_low);
4597 subborrow_u64(borrow, 0, m_high, &z_high);
4601 const uint64_t m_low = frac_low << 15;
4602 const uint64_t m_high = (1ull << 63) | (frac_high << 15) | (frac_low >> 49);
4603 uint64_t p_low = 0, p_high = 0;
4608 constexpr uint64_t one_half = 1ull << 63;
4609 uint64_t d_low = 0, d_high = 0;
4610 if (p_high >= one_half) {
4612 d_high = p_high - one_half;
4614 d_low = 0ull - p_low;
4615 d_high = one_half - p_high - ((p_low != 0) ? 1ull : 0ull);
4619 z_high = (d_high << 1) | (d_low >> 63);
4630 for (int32_t n =
LOG2_TERMS - 1; n >= 1; --n) {
4631 uint64_t term_low = 0, term_high = 0;
4632 mul128_high(z_low, z_high, acc_low, acc_high, term_low, term_high);
4635 const uint8_t carry = addcarryx_u64(0, q_low, term_low, &acc_low);
4636 addcarryx_u64(carry, q_high, term_high, &acc_high);
4638 const uint8_t borrow = subborrow_u64(0, q_low, term_low, &acc_low);
4639 subborrow_u64(borrow, q_high, term_high, &acc_high);
4649 if (expo == -1 && j != 0 && !just_below_one) {
4650 uint64_t s_low = 0, s_high = 0;
4651 mul128_high(z_low, z_high, acc_low, acc_high, s_low, s_high);
4652 s_high = (s_high << 1) | (s_low >> 63);
4656 uint64_t r_low = 0, r_high = 0;
4657 const uint8_t borrow = subborrow_u64(0, 0,
log2_value_table[j][1], &r_low);
4660 const uint8_t carry = addcarryx_u64(0, r_low, s_low, &r_low);
4661 addcarryx_u64(carry, r_high, s_high, &r_high);
4663 const uint8_t borrow_s = subborrow_u64(0, r_low, s_low, &r_low);
4664 subborrow_u64(borrow_s, r_high, s_high, &r_high);
4683 if (just_below_one) {
4684 return filter(series);
4693 return filter(
float128(expo) + (table_value + series));
4733 uint64_t l = 0, h = 0;
4736 x.get_components(l, h, expo, sign);
4748 if (x.is_zero() || (x.is_inf() && x.is_positive()))
4754 if (x == -one_value) {
4768 if (
fabs(x) < one_value) {
4769 const float128 sum = one_value + x;
4770 const float128 rounding = (sum - one_value) - x;
4771 return filter(
log(sum) - rounding / sum);
4774 return filter(
log(one_value + x));
4802 for (
int i = 3, sign = 1; i <= 51; i += 2, sign = 1 - sign) {
4805 float128 elem = elem_nom * elem_denom;
4809 x += (sign) ? -elem : elem;
4850 for (int32_t k =
COS_TERMS - 1; k >= 1; --k) {
4852 tail = factor - xx * tail;
4888 err = (a - (sum - b_part)) + (b - b_part);
4901 err = b - (sum - a);
4918 constexpr float128 two_over_pi(0x2A53F84EAFA3EA6A, 0x45F306DC9C88, 0x3FFE, 0);
4919 const float128 scaled = x * two_over_pi;
4950 constexpr float128 half_pi_hi(0x8469898CC51701B8, 0x921FB54442D1, 0x3FFF, 0);
4951 constexpr float128 half_pi_mid(0xA67CC74020BBEA64, 0xCD129024E088, 0x3F8C, 0);
4952 constexpr int32_t window_words = 6;
4953 constexpr int32_t window_bits = window_words * 64;
4954 constexpr int32_t product_words = window_words + 2;
4956 uint64_t ml = 0, mh = 0;
4959 x.get_components(ml, mh,
e, sign);
4963 const int32_t first = (
e - 113 > 1) ? (
e - 113) : 1;
4964 const int32_t frac_bits = (first + window_bits - 1) - (
e - FRAC_BITS);
4967 uint64_t window[window_words] {};
4968 const int32_t word = (first - 1) >> 6;
4969 const int32_t bit = (first - 1) & 63;
4970 for (int32_t k = 0; k < window_words; ++k) {
4973 window[window_words - 1 - k] = (bit == 0) ? w0 : ((w0 << bit) | (w1 >> (64 - bit)));
4977 uint64_t p[product_words] {};
4978 const uint64_t m[2] = {ml, mh};
4979 for (int32_t i = 0; i < 2; ++i) {
4981 for (int32_t j = 0; j < window_words; ++j) {
4982 uint64_t high_word = 0;
4983 uint64_t low_word = mulx_u64(m[i], window[j], &high_word);
4984 uint8_t c = addcarryx_u64(0, low_word, carry, &low_word);
4986 c = addcarryx_u64(0, p[i + j], low_word, &p[i + j]);
4987 carry = high_word + c;
4989 p[i + window_words] = carry;
4994 const auto bit_at = [&p](int32_t index) {
4995 return ((index >> 6) < product_words) ?
static_cast<uint32_t
>((p[index >> 6] >> (index & 63)) & 1) : 0u;
4997 uint32_t quadrant = bit_at(frac_bits) | (bit_at(frac_bits + 1) << 1);
5000 for (int32_t i = 0; i < product_words; ++i) {
5001 const int32_t lowest = i * 64;
5002 if (lowest >= frac_bits)
5004 else if (lowest + 64 > frac_bits)
5009 const bool negative = bit_at(frac_bits - 1) != 0;
5013 for (int32_t i = 0; i < product_words; ++i)
5014 borrow = subborrow_u64(borrow, 0, p[i], &p[i]);
5015 for (int32_t i = 0; i < product_words; ++i) {
5016 const int32_t lowest = i * 64;
5017 if (lowest >= frac_bits)
5019 else if (lowest + 64 > frac_bits)
5025 for (int32_t i = product_words - 1; i >= 0 && msb < 0; --i) {
5027 msb = i * 64 + 63 -
static_cast<int32_t
>(lzcnt64(p[i]));
5034 const auto extract = [&p](int32_t pos, uint64_t& l, uint64_t& h) {
5035 const int32_t from = (pos > 0) ? pos : 0;
5036 const int32_t w = from >> 6;
5037 const int32_t b = from & 63;
5038 const uint64_t p0 = (w < product_words) ? p[w] : 0;
5039 const uint64_t p1 = (w + 1 < product_words) ? p[w + 1] : 0;
5040 const uint64_t p2 = (w + 2 < product_words) ? p[w + 2] : 0;
5041 l = (b == 0) ? p0 : ((p0 >> b) | (p1 << (64 - b)));
5042 h = (b == 0) ? p1 : ((p1 >> b) | (p2 << (64 - b)));
5049 uint64_t l = 0, h = 0;
5050 extract(msb - FRAC_BITS, l, h);
5053 extract(msb - 2 * FRAC_BITS - 1, l, h);
5057 const float128 product = f_hi * half_pi_hi;
5058 const float128 product_err =
fma(f_hi, half_pi_hi, -product);
5059 quick_two_sum(product, product_err + (f_hi * half_pi_mid + f_lo * half_pi_hi), hi, lo);
5070 quadrant = 0u - quadrant;
5072 return static_cast<int32_t
>(quadrant & 3);
5087 if (x.get_exponent() < 60) {
5090 return static_cast<int32_t
>(n & 3);
5115 constexpr float128 half_pi_parts[3] = {
5116 float128(0x8469898CC51701B8, 0x921FB54442D1, 0x3FFF, 0),
5117 float128(0xA67CC74020BBEA64, 0xCD129024E088, 0x3F8C, 0),
5118 float128(0xE19C72FEC8841ABA, 0x3B19376BAD7D, 0x3F1A, 1),
5127 for (
const float128& part : half_pi_parts) {
5129 const float128 product = nf * part;
5130 const float128 product_err =
fma(nf, part, -product);
5133 two_sum(hi, -product, sum, err);
5156 return filter(
sin1(hi) +
cos1(hi) * lo);
5158 return filter(
cos1(hi) -
sin1(hi) * lo);
5160 return filter(-(
sin1(hi) +
cos1(hi) * lo));
5163 return filter(-(
cos1(hi) -
sin1(hi) * lo));
5193 else if (a <
ldexp(one_value, -57)) {
5198 res =
atan(a /
sqrt((one_value - a) * (one_value + a)));
5224 return filter(
cos1(hi) -
sin1(hi) * lo);
5226 return filter(-(
sin1(hi) +
cos1(hi) * lo));
5228 return filter(-(
cos1(hi) -
sin1(hi) * lo));
5231 return filter(
sin1(hi) +
cos1(hi) * lo);
5250 if (
fabs(x) > one_value)
5254 if (x == -one_value)
5259 return filter(
atan(
sqrt((one_value - x) / (one_value + x))) << 1);
5285 constexpr int max_iterations = 6;
5288 auto sign = x.get_sign();
5315 for (
int i = 0; i < max_iterations; ++i) {
5318 float128 e = cos_xn * (sin_xn - x * cos_xn);
5344 constexpr float128 three_quarter_pi(0x234F272993D1414A, 0x2D97C7F3321D, 0x4000, 0);
5346 if (x.is_nan() || y.is_nan())
5352 if (y.is_zero() || x.is_zero() || y.is_inf() || x.is_inf()) {
5355 else if (y.is_inf())
5357 else if (x.is_zero())
5372 ratio = (comp) ? x / y : y / x;
5382 return filter((y < 0) ? res -
pi : res +
pi);
5397 if (x.is_inf() || x.is_zero())
5411 res = (
exp(a) -
exp(-a)) >> 1;
5416 res = half_power * (half_power >> 1);
5419 return filter((x.is_negative()) ? -res : res);
5433 if (x.is_inf() || x.is_zero())
5454 if (a <
ldexp(one_value, -57)) {
5457 }
else if (a >
ldexp(one_value, 57)) {
5461 res =
log1p(a + a2 / (one_value +
sqrt(one_value + a2)));
5464 return filter((x.is_positive()) ? res : -res);
5486 return filter(half_power * (half_power >> 1));
5488 return filter((
exp(a) +
exp(-a)) >> 1);
5520 if (x >
ldexp(one_value, 57))
5523 return filter(
log(x +
sqrt(
sqr(x) - one_value)));
5546 if (x.is_inf() || a >
float128(40)) {
5559 return filter((x.is_negative()) ? -res : res);
5582 if (a == one_value) {
5584 return x.is_negative() ? -
inf() :
inf();
5595 res =
log1p((a << 1) + (
sqr(a) << 1) / (one_value - a)) >> 1;
5597 res =
log1p((a << 1) / (one_value - a)) >> 1;
5599 return filter((x.is_negative()) ? -res : res);
5614 for (int32_t i = 1; i < count; ++i) {
5635 const uint32_t sign = x.get_sign();
5641 if (x.is_inf() || a >=
float128(9)) {
5666 for (int32_t n = 1; n < 1024; ++n) {
5667 term = term * two_xx /
float128(2 * n + 1);
5669 two_sum(sum, term, rounded, err);
5722 if (x.is_negative())
5725 return filter(one_value -
erf(x));
5730 constexpr int32_t max_iterations = 4096;
5737 for (int32_t n = 1; n < max_iterations; ++n) {
5751 if (
fabs(delta - one_value) < epsilon)
5757 const float128 gaussian =
exp(-xx) * (one_value - xx_err);
5787 const uint32_t product_sign = x.get_sign() ^ y.get_sign();
5792 if ((x.is_inf() && y.is_zero()) || (y.is_inf() && x.is_zero())) {
5797 if (x.is_nan() || y.is_nan() || z.is_nan())
5802 if (x.is_inf() || y.is_inf()) {
5803 if (z.is_inf() && z.get_sign() != product_sign)
5814 if (x.is_zero() || y.is_zero()) {
5825 uint64_t lx = 0, hx = 0, ly = 0, hy = 0, lz = 0, hz = 0;
5826 int32_t ex = 0, ey = 0, ez = 0;
5827 uint32_t sx = 0, sy = 0, sz = 0;
5828 x.get_components(lx, hx, ex, sx);
5829 y.get_components(ly, hy, ey, sy);
5830 z.get_components(lz, hz, ez, sz);
5835 uint64_t product[4] {};
5837 const int32_t product_bits =
wide_msb(product, 4);
5840 const int32_t product_msb = ex + ey - 2 * FRAC_BITS + product_bits;
5841 const int32_t top = (product_msb > ez) ? product_msb : ez;
5846 const uint64_t addend[2] = {lz, hz};
5847 bool sticky =
wide_place(product_acc, product, 4,
WIDE_TOP - (top - product_msb) - product_bits);
5848 sticky =
wide_place(addend_acc, addend, 2,
WIDE_TOP - (top - ez) - FRAC_BITS) || sticky;
5852 uint64_t* acc = product_acc;
5853 uint32_t sign = product_sign;
5854 if (product_sign == sz) {
5857 const int32_t cmp =
wide_cmp(product_acc, addend_acc);
5878 int32_t expo = top -
WIDE_TOP + msb;
5882 uint64_t l = 0, h = 0;
5884 const int32_t lsb = msb - FRAC_BITS;
5892 const int32_t word = lsb >> 6;
5893 const int32_t bit = lsb & 63;
5894 l = acc[word] >> bit;
5895 h = (word + 1 <
WIDE_WORDS) ? (acc[word + 1] >> bit) : 0;
5898 l |= acc[word + 1] << (64 - bit);
5900 h |= acc[word + 2] << (64 - bit);
5902 h &= FRAC_UNITY | UPPER_FRAC_MASK;
5905 for (int32_t i = 0; i < ((lsb - 1) >> 6); ++i)
5906 sticky = sticky || acc[i] != 0;
5907 const int32_t below = (lsb - 1) & 63;
5914 return round_pack(sign, expo, l, h, (guard << 63) | (sticky ? 1 : 0));
5929 if (x.is_inf() || x.is_zero()) {
5937 x.get_components(l, h,
e, s);
5939 float128 res(l, h, EXP_BIAS - 1, s);
5983 return x.is_subnormal() ? FP_SUBNORMAL : FP_NORMAL;
5995 if (x.is_signaling() || y.is_signaling())
5997 return x.is_nan() || y.is_nan();
6043 const uint64_t flip = (
get_sign() != 0) ? UINT64_MAX : 0;
6045 h =
high ^ (flip | SIGN_MASK);
6061 uint64_t xl = 0, xh = 0, yl = 0, yh = 0;
6062 x.total_order_key(xl, xh);
6063 y.total_order_key(yl, yh);
6064 return (xh != yh) ? (xh < yh) : (xl <= yl);
6081 if (!x.is_nan() && !y.is_nan()) {
6094 if (!x.is_nan() && !y.is_nan()) {
6127 if (payload !=
nullptr && *payload !=
'\0') {
6128 const uint64_t parsed = strtoull(payload,
nullptr, 0);
6135 res.
high |= QUIET_NAN_BIT;
6142 uint64_t l = 0, h = 0;
6145 x.get_components(l, h, expo, sign);
6148 if (expo < 0 || expo > FRAC_BITS)
6150 const int32_t bit = FRAC_BITS - expo;
6151 return (bit < 64) ? ((l >> bit) & 1) != 0 : ((h >> (bit - 64)) & 1) != 0;
6188 const long clamped = (n > limit) ? limit : ((n < -limit) ? -limit : n);
6189 return ldexp(x,
static_cast<int>(clamped));
6207 if (x.is_nan() || y.is_nan())
6239 uint64_t lx = 0, hx = 0, ly = 0, hy = 0;
6240 int32_t ex = 0, ey = 0;
6241 uint32_t sx = 0, sy = 0;
6242 x.get_components(lx, hx, ex, sx);
6243 y.get_components(ly, hy, ey, sy);
6247 if (ex < ey || (ex == ey && mx < mod)) {
6253 const uint128_t twice = (ex == ey) ? (mx << 1) : mx;
6254 half = (twice < mod) ? -1 : ((twice == mod) ? 0 : 1);
6261 rem -= block_quotient * mod;
6266 int32_t shift = ex - ey;
6267 constexpr int32_t block = 15;
6269 const int32_t step = (shift < block) ? shift : block;
6272 block_quotient = rem / mod;
6273 rem -= block_quotient * mod;
6276 quotient = (quotient << step) +
low;
6281 half = (twice < mod) ? -1 : ((twice == mod) ? 0 : 1);
6286 uint64_t rl = 0, rh = 0;
6288 const int32_t msb =
static_cast<int32_t
>(
log2(rl, rh));
6308 return remquo(x, y, "ient);
6322 if (x.is_nan() || y.is_nan())
6324 if (x.is_inf() || y.is_zero())
6326 if (y.is_inf() || x.is_zero())
6340 const bool round_up =
half > 0 || (
half == 0 && (bits & 1) != 0);
6347 if (quo !=
nullptr) {
6350 const int low_bits =
static_cast<int>(bits & 0x7F);
6351 *quo = (x.get_sign() != y.get_sign()) ? -low_bits : low_bits;
6367 if (x.is_signaling() || y.is_signaling() || z.is_signaling())
6369 if (x.is_inf() || y.is_inf() || z.is_inf())
6371 if (x.is_nan() || y.is_nan() || z.is_nan())
6382 const int32_t expo =
ilogb(largest);
6421 float128(0xE16CB90701FBFAB5ull, 0x3FFDB0EE6072093Cull),
6422 float128(0x6461CF62448802EBull, 0x3FFD4A34CC4A60FAull),
6423 float128(0x68125D0741DF7858ull, 0xBFFB13E001A55760ull),
6424 float128(0x36BF2242232DCA47ull, 0x3FF951322AC7D848ull),
6425 float128(0x186D6760C385330Eull, 0xBFF7E404FC218F5Full),
6426 float128(0xFD490FAE70050172ull, 0x3FF67ADD6EADB6C2ull),
6427 float128(0x7CEB67C604C626F6ull, 0xBFF538AC5C2BF8E0ull),
6428 float128(0x8BE59CA4DDB5A64Dull, 0x3FF40B36AF86396Eull),
6429 float128(0x7C70754606688FA5ull, 0xBFF2D3FD4C76D2FCull),
6430 float128(0xA33A61354E004CF2ull, 0x3FF1A127B0F17D65ull),
6431 float128(0xEF2EFD5F190DC2CDull, 0xBFF078DE5BD7C81Eull),
6432 float128(0x224C0AE964B1EE44ull, 0x3FEF580DCEE66EB0ull),
6433 float128(0x2856A464E0AB35D5ull, 0xBFEE3CBC963CE224ull),
6434 float128(0xB901BBAE75609FD8ull, 0x3FED2597A39F34AAull),
6435 float128(0x11C76B0E65AC639Bull, 0xBFEC11B2EB767954ull),
6436 float128(0xF3A02AD5FFB78217ull, 0x3FEB0064CDEB22F0ull),
6437 float128(0xEBB3D4F1868FBE6Bull, 0xBFE9E2600D93CFD2ull),
6438 float128(0xD7668ADDDDA6D48Full, 0x3FE8C76BBB3F07A4ull),
6439 float128(0x7657C89FA5B60F88ull, 0xBFE7AF5A6CBBF8A9ull),
6440 float128(0xF40C9C591CD909EAull, 0x3FE699B93C2070B0ull),
6441 float128(0xC6CCA00AFB036818ull, 0xBFE5862C734DF3EAull),
6442 float128(0xCB258B1DCC5155DFull, 0x3FE47469DACCFADCull),
6443 float128(0xD035EE5DBF0EDF9Aull, 0xBFE36434A8447AEAull),
6444 float128(0x29E2BE683656F13Bull, 0x3FE2555A877FFD2Cull),
6445 float128(0xE413CDB83A1538F4ull, 0xBFE147B1679258D0ull),
6446 float128(0xBC50C1202229AE9Aull, 0x3FE03B15D2B2FC10ull),
6447 float128(0xF97A755322CE1DB6ull, 0xBFDF2F69A9FABE3Dull),
6448 float128(0xC41820A7091423A3ull, 0x3FDE24932A337434ull),
6449 float128(0xC53D3AF6D2812D29ull, 0xBFDD1A7C26EC2523ull),
6450 float128(0x78E3F2CAD8600E57ull, 0x3FDC11116E693ED9ull),
6451 float128(0x8281DF041B793D4Bull, 0xBFDB08424CBC543Dull),
6452 float128(0xF4D51A9815322258ull, 0x3FDA000026E3F644ull),
6453 float128(0xA0BADB5F2A2D6045ull, 0xBFD8F07C514FC9F0ull),
6454 float128(0x7A755E9E6B1E780Aull, 0x3FD7E1E2026AAFCDull),
6455 float128(0x1D78ED65B062543Eull, 0xBFD6D41D56E5EE2Eull),
6456 float128(0x6BFBE2E26CB30374ull, 0x3FD5C71C7F6F10E3ull),
6457 float128(0xAD6B4BBD43F5EE38ull, 0xBFD4BACF9A27BC89ull),
6458 float128(0xE281AFE37085970Eull, 0x3FD3AF28718A10D6ull),
6459 float128(0x5A75058C37B49BF0ull, 0xBFD2A41A45603E5Bull),
6460 float128(0x8B187BC1BEF29131ull, 0x3FD199999C0716EEull),
6461 float128(0x7E625A92BB8E6DFAull, 0xBFD08F9C1A8DF9D7ull),
6462 float128(0x4CC5056234AEF19Eull, 0x3FCF8618628D2890ull),
6463 float128(0x05C6EE82DF30CE0Bull, 0xBFCE7D05F4C31C56ull),
6464 float128(0xA674E2F0035C28C7ull, 0x3FCD745D17B56BA4ull),
6465 float128(0x66BB45CB97243D92ull, 0xBFCC6C16C1B4D645ull),
6466 float128(0xC887EB79BCA14271ull, 0x3FCB642C85C023D9ull),
6467 float128(0xC9A37039036FD052ull, 0xBFCA5C9882D825E9ull),
6468 float128(0x639EB90C92EA148Aull, 0x3FC9555555698A86ull),
6469 float128(0x8DAE8C46EE149FD0ull, 0xBFC84E5E0A8022BCull),
6470 float128(0xE882E7B3E6F672F6ull, 0x3FC747AE14838081ull),
6471 float128(0x13C8EE7D4261693Full, 0xBFC641414146E3E3ull),
6472 float128(0xA0A0E2D83D68A5C6ull, 0x3FC53B13B13EC2F3ull),
6473 float128(0x94896C42A8F2EC41ull, 0xBFC43521CFB52085ull),
6474 float128(0x922DAAC30B4BE8C9ull, 0x3FC32F684BDBA6A9ull),
6475 float128(0x3B3843DA7A1E5BB0ull, 0xBFC229E4129F4967ull),
6476 float128(0xBBBA2C3DB2CD6133ull, 0x3FC1249249253F4Cull),
6477 float128(0xB6A6D988E3DBA6D1ull, 0xBFC01F7047DC8348ull),
6478 float128(0xBB412D63F4093EF5ull, 0x3FBF1A7B9611F1F7ull),
6482 acc = coefficients[k] + z * acc;
6502 constexpr float128 pi_low(0xA67CC74020BBEA64ull, 0x3F8DCD129024E088ull);
6541 if (x.is_zero() || (x.is_negative() && x.is_int())) {
6555 const int32_t n =
static_cast<int32_t
>(n_value);
6559 for (int32_t j = 2; j < n; ++j) {
6566 for (int32_t k = n; k <= 0; ++k) {
6572 if (x.is_negative()) {
6577 float128(0x5555555555555555, 0x555555555555, 0x3FFB, 0),
6578 float128(0x6C16C16C16C16C17, 0x6C16C16C16C1, 0x3FF6, 1),
6579 float128(0xA01A01A01A01A01A, 0xA01A01A01A01, 0x3FF4, 0),
6580 float128(0x3813813813813814, 0x381381381381, 0x3FF4, 1),
6581 float128(0x3570EA73806E5479, 0xB951E2B18FF2, 0x3FF4, 0),
6582 float128(0xC81F6AB0D9993C7D, 0xF6AB0D9993C7, 0x3FF5, 1),
6583 float128(0xA41A41A41A41A41A, 0xA41A41A41A41, 0x3FF7, 0),
6584 float128(0x7DC2064A8ED3175C, 0xE4286CB0F539, 0x3FF9, 1),
6585 float128(0xFFA1876FE96381E0, 0x6FE96381E067, 0x3FFC, 0),
6586 float128(0x9EDBDB9CE625987D, 0x6476701181F3, 0x3FFF, 1),
6587 float128(0x5A74F53910C8B380, 0xACE44322CE00, 0x4002, 0),
6588 float128(0xAAB67EE25D73C0F9, 0x39B2525CCCC1, 0x4006, 1),
6589 float128(0x1B4E81B4E81B4E82, 0x12234E81B4E8, 0x400A, 0),
6590 float128(0x7EB3FEDDD8496920, 0x1A198AE1C4AB, 0x400E, 1),
6591 float128(0xA38433DC9FB888D4, 0x51A2089A6E11, 0x4012, 0),
6592 float128(0x77880C2D3577880C, 0xD1089B142D35, 0x4016, 1),
6594 constexpr float128 half_log_two_pi(0x4A69297920028832, 0xD67F1C864BEB, 0x3FFE, 0);
6599 series = (series + coefficients[i]) * inv_xx;
6600 series = (series + coefficients[0]) / x;
6635 return x.is_negative() ? -
inf() :
inf();
6637 if (x.is_negative() && x.is_int())
6647 if (x.is_int() && x <=
float128(38)) {
6651 return filter(result);
6657 const int32_t n =
static_cast<int32_t
>(n_value);
6660 for (int32_t j = 2; j < n; ++j) {
6663 return filter(result);
6673 for (int32_t k = n; k < 0; ++k) {
6676 return filter((result / product) / z);
6680 if (x.is_positive())
6681 return filter(magnitude);
6686 return filter(
is_odd_int(floor_x) ? -magnitude : magnitude);
6696static_assert(
sizeof(float128) ==
sizeof(uint64_t) * 2);
6715inline void append_exponent(std::string& out, int32_t exponent,
char marker, int32_t min_digits = 2)
6718 out += (exponent < 0) ?
'-' :
'+';
6721 uint32_t magnitude = (exponent < 0) ? (0u -
static_cast<uint32_t
>(exponent)) :
static_cast<uint32_t
>(exponent);
6731 constexpr int32_t max_digits = 10;
6732 char digits[max_digits];
6733 int32_t index = max_digits;
6735 digits[--index] =
static_cast<char>(
'0' + (magnitude % 10));
6742 while (index > max_digits - min_digits)
6743 digits[--index] =
'0';
6745 out.append(digits + index,
static_cast<size_t>(max_digits - index));
6760 uint64_t low = 0, high = 0;
6761 int32_t exponent = 0;
6764 uint64_t want_low = 0, want_high = 0;
6765 value.
get_bits(want_low, want_high);
6766 want_high &= ~(1ull << 63);
6774 while (lower < upper) {
6775 const int32_t middle = (lower + upper) / 2;
6776 const int32_t exponent10 =
to_decimal_digits(low, high, exponent, middle, digits);
6781 uint64_t back_low = 0, back_high = 0, extra = 0;
6782 int32_t back_exponent = 0;
6784 if (
from_decimal_digits(digits, middle, exponent10 - middle, back_low, back_high, back_exponent, &extra)) {
6786 uint64_t got_low = 0, got_high = 0;
6788 same = got_low == want_low && got_high == want_high;
6817[[nodiscard]]
inline std::string
render(
const float128& value,
char style, int32_t precision,
bool uppercase,
bool alternate,
char sign_char,
6823 else if (sign_char !=
'-')
6827 out += uppercase ?
"NAN" :
"nan";
6831 out += uppercase ?
"INF" :
"inf";
6835 const char*
const hex_digits = uppercase ?
"0123456789ABCDEF" :
"0123456789abcdef";
6841 out += uppercase ?
"0X" :
"0x";
6843 const int32_t exponent_digits = bare_hex ? 1 : 2;
6846 if (alternate || precision > 0) {
6848 for (int32_t i = 0; i < precision; ++i)
6855 uint64_t low = 0, high = 0;
6856 int32_t exponent = 0;
6862 for (int32_t i = 0; i < 28; ++i) {
6863 const int32_t shift = 108 - 4 * i;
6864 const uint64_t nibble = (shift >= 64) ? ((high >> (shift - 64)) & 0xF) : ((low >> shift) & 0xF);
6865 fraction[i] = hex_digits[nibble];
6868 const auto hex_value = [](
char c) {
6869 if (c >=
'0' && c <=
'9')
6871 return ((c >=
'a') ? (c -
'a') : (c -
'A')) + 10;
6874 int32_t kept = (precision >= 0) ? precision : 28;
6880 bool leading_two =
false;
6881 if (precision >= 0 && precision < 28) {
6882 const int32_t first_dropped = hex_value(fraction[precision]);
6884 for (int32_t i = precision + 1; i < 28; ++i)
6885 rest = rest || fraction[i] !=
'0';
6886 const int32_t last_kept = (precision > 0) ? hex_value(fraction[precision - 1]) : 1;
6893 if (first_dropped != 0 || rest)
6896 int32_t index = precision - 1;
6897 while (index >= 0) {
6898 const int32_t digit = hex_value(fraction[index]);
6900 fraction[index] = hex_digits[digit + 1];
6903 fraction[index] =
'0';
6910 if (precision < 0) {
6912 while (kept > 0 && fraction[kept - 1] ==
'0')
6916 out += leading_two ?
'2' :
'1';
6917 if (kept > 0 || alternate || precision > 28) {
6919 out.append(fraction,
static_cast<size_t>(kept));
6922 if (precision > 28) {
6923 out.append(
static_cast<size_t>(precision - 28),
'0');
6926 append_exponent(out, exponent, uppercase ?
'P' :
'p', exponent_digits);
6935 if (style ==
'f' || style ==
'e')
6940 if (zeros > 0 || alternate) {
6942 out.append(
static_cast<size_t>(zeros),
'0');
6949 uint64_t low = 0, high = 0;
6950 int32_t exponent = 0;
6955 int32_t significant = 0;
6956 int32_t exponent10 = 0;
6962 if (style ==
'\0') {
6964 exponent10 =
to_decimal_digits(low, high, exponent, significant, digits, &exact);
6965 }
else if (style ==
'e') {
6969 exponent10 =
to_decimal_digits(low, high, exponent, significant, digits, &exact, direction);
6970 }
else if (style ==
'g') {
6971 significant = (precision > 0) ? precision : ((precision == 0) ? 1 :
DEFAULT_PRECISION);
6974 exponent10 =
to_decimal_digits(low, high, exponent, significant, digits, &exact, direction);
6980 int32_t probe_exponent10 =
to_decimal_digits(low, high, exponent, 1, digits, &exact);
6981 significant = probe_exponent10 + after_point;
6988 const int32_t actual_exponent10 =
to_decimal_digits(low, high, exponent, significant, digits,
nullptr, direction);
6989 if (actual_exponent10 != probe_exponent10) {
6990 probe_exponent10 = actual_exponent10;
6991 significant = actual_exponent10 + after_point;
6995 if (significant <= 0) {
7004 bool round_up =
false;
7008 bool rest_is_zero =
false;
7010 round_up = (leading_exponent10 == -after_point) && (digits[0] >
'5' || (digits[0] ==
'5' && !rest_is_zero));
7014 out += (round_up && after_point == 0) ?
'1' :
'0';
7015 if (after_point > 0 || alternate) {
7017 for (int32_t i = 0; i < after_point; ++i)
7018 out += (round_up && i == after_point - 1) ?
'1' :
'0';
7033 std::string body(digits,
static_cast<size_t>(significant));
7034 body.append(
static_cast<size_t>(padding),
'0');
7036 if (exponent10 <= 0) {
7038 out.append(
static_cast<size_t>(-exponent10),
'0');
7041 out.append(body, 0,
static_cast<size_t>(exponent10));
7043 out.append(body,
static_cast<size_t>(exponent10), std::string::npos);
7048 exponent10 =
to_decimal_digits(low, high, exponent, significant, digits, &exact, direction);
7053 const int32_t scientific_exponent = exponent10 - 1;
7055 int32_t kept = significant;
7056 if ((style ==
'g' || style ==
'\0') && !alternate) {
7057 while (kept > 1 && digits[kept - 1] ==
'0')
7061 bool scientific = (style ==
'e');
7064 scientific = (scientific_exponent < -4) || (scientific_exponent >= significant);
7065 }
else if (style ==
'\0') {
7070 int32_t fixed_length = 0;
7071 if (exponent10 <= 0)
7072 fixed_length = 2 - exponent10 + kept;
7073 else if (exponent10 >= kept)
7074 fixed_length = exponent10;
7076 fixed_length = kept + 1;
7078 int32_t exponent_digits = 2;
7079 for (int32_t magnitude = (scientific_exponent < 0) ? -scientific_exponent : scientific_exponent; magnitude >= 100; magnitude /= 10)
7081 const int32_t scientific_length = kept + ((kept > 1) ? 1 : 0) + 2 + exponent_digits;
7083 scientific = scientific_length < fixed_length;
7088 if (kept > 1 || alternate) {
7090 out.append(digits + 1,
static_cast<size_t>(kept - 1));
7096 if (exponent10 <= 0) {
7098 out.append(
static_cast<size_t>(-exponent10),
'0');
7099 out.append(digits,
static_cast<size_t>(kept));
7100 }
else if (exponent10 >= kept) {
7101 out.append(digits,
static_cast<size_t>(kept));
7102 out.append(
static_cast<size_t>(exponent10 - kept),
'0');
7106 out.append(digits,
static_cast<size_t>(exponent10));
7108 out.append(digits + exponent10,
static_cast<size_t>(kept - exponent10));
7114 const size_t point = out.find(
'.');
7115 const size_t written = (point == std::string::npos) ? 0 : (out.size() - point - 1);
7116 if (after_point > 0) {
7117 if (point == std::string::npos)
7119 for (
size_t i = written; i < static_cast<size_t>(after_point); ++i)
7154 const auto lower = [](
char c) {
return static_cast<char>((c >=
'A' && c <=
'Z') ? (c -
'A' +
'a') : c); };
7155 const auto matches = [&](
const char* word,
size_t length) {
7156 if (
static_cast<size_t>(last - cursor) < length)
7158 for (
size_t i = 0; i < length; ++i) {
7159 if (lower(cursor[i]) != word[i])
7165 if (matches(
"inf", 3)) {
7167 if (matches(
"inity", 5))
7172 if (matches(
"snan", 4)) {
7177 if (matches(
"nan", 3)) {
7180 if (cursor < last && *cursor ==
'(') {
7181 const char* close = cursor + 1;
7182 while (close < last && (isalnum(
static_cast<unsigned char>(*close)) || *close ==
'_'))
7184 if (close < last && *close ==
')') {
7185 value =
fp128::nan(std::string(cursor + 1, close).c_str());
7210 std::from_chars_result result {first, std::errc {}};
7211 const char* cursor = first;
7212 if (cursor == last) {
7213 result.ec = std::errc::invalid_argument;
7217 bool negative =
false;
7218 if (*cursor ==
'-' || *cursor ==
'+') {
7219 negative = (*cursor ==
'-');
7227 const auto hex_digit = [](
char c) -> int32_t {
7228 if (c >=
'0' && c <=
'9')
7230 if (c >=
'a' && c <=
'f')
7231 return c -
'a' + 10;
7232 if (c >=
'A' && c <=
'F')
7233 return c -
'A' + 10;
7238 uint64_t l = 0, h = 0;
7240 int64_t binary_exponent = 0;
7241 bool sticky =
false, any_digit =
false, nonzero =
false;
7242 const auto take = [&](int32_t digit,
bool after_point) {
7244 if (!nonzero && digit == 0) {
7247 binary_exponent -= 4;
7252 h = (h << 4) | (l >> 60);
7253 l = (l << 4) | static_cast<uint64_t>(digit);
7256 binary_exponent -= 4;
7258 sticky = sticky || digit != 0;
7260 binary_exponent += 4;
7264 while (cursor < last && hex_digit(*cursor) >= 0)
7265 take(hex_digit(*cursor++),
false);
7266 if (cursor < last && *cursor ==
'.') {
7268 while (cursor < last && hex_digit(*cursor) >= 0)
7269 take(hex_digit(*cursor++),
true);
7272 result.ec = std::errc::invalid_argument;
7275 result.ptr = cursor;
7278 if (cursor < last && (*cursor ==
'p' || *cursor ==
'P')) {
7279 const char* probe = cursor + 1;
7280 bool exponent_negative =
false;
7281 if (probe < last && (*probe ==
'-' || *probe ==
'+')) {
7282 exponent_negative = (*probe ==
'-');
7285 if (probe < last && *probe >=
'0' && *probe <=
'9') {
7286 int64_t magnitude = 0;
7287 while (probe < last && *probe >=
'0' && *probe <=
'9') {
7288 if (magnitude < 1000000)
7289 magnitude = magnitude * 10 + (*probe -
'0');
7292 binary_exponent += exponent_negative ? -magnitude : magnitude;
7305 const int64_t clamped = (binary_exponent < -100000) ? -100000 : ((binary_exponent > 100000) ? 100000 : binary_exponent);
7308 result.ec = std::errc::result_out_of_range;
7328template <
int32_t Capacity>
7330 uint32_t sign,
float128& value)
noexcept
7332 uint64_t low = 0, high = 0, extra = 0;
7333 int32_t exponent = 0;
7334 if (!
decimal_to_binary(workspace, digits, count, max_kept, exponent10, low, high, exponent, &extra)) {
7335 if (exponent == 0) {
7338 value.set_sign(sign);
7339 return std::errc {};
7345 return std::errc::result_out_of_range;
7350 return (value.is_inf() || value.is_zero()) ? std::errc::result_out_of_range : std::errc {};
7378 std::from_chars_result result {first, std::errc {}};
7379 const char* cursor = first;
7380 if (cursor == last) {
7381 result.ec = std::errc::invalid_argument;
7385 bool negative =
false;
7386 if (*cursor ==
'-' || *cursor ==
'+') {
7387 negative = (*cursor ==
'-');
7399 int32_t exponent10 = 0;
7400 bool any_digit =
false;
7401 bool seen_significant =
false;
7402 bool dropped =
false;
7403 const char*
const significand = cursor;
7409 const auto take_integer = [&](
char digit) {
7411 seen_significant = seen_significant || (digit !=
'0');
7412 if (!seen_significant)
7415 digits[count++] = digit;
7418 dropped = dropped || (digit !=
'0');
7421 const auto take_fraction = [&](
char digit) {
7423 seen_significant = seen_significant || (digit !=
'0');
7424 if (!seen_significant) {
7429 digits[count++] = digit;
7432 dropped = dropped || (digit !=
'0');
7436 while (cursor < last && *cursor >=
'0' && *cursor <=
'9')
7437 take_integer(*cursor++);
7439 if (cursor < last && *cursor ==
'.') {
7441 while (cursor < last && *cursor >=
'0' && *cursor <=
'9')
7442 take_fraction(*cursor++);
7446 result.ec = std::errc::invalid_argument;
7449 result.ptr = cursor;
7450 const char*
const significand_end = cursor;
7451 int32_t exponent_part = 0;
7454 if (cursor < last && (*cursor ==
'e' || *cursor ==
'E')) {
7455 const char* probe = cursor + 1;
7456 bool exponent_negative =
false;
7457 if (probe < last && (*probe ==
'-' || *probe ==
'+')) {
7458 exponent_negative = (*probe ==
'-');
7461 if (probe < last && *probe >=
'0' && *probe <=
'9') {
7462 int64_t magnitude = 0;
7463 while (probe < last && *probe >=
'0' && *probe <=
'9') {
7464 if (magnitude < 1000000)
7465 magnitude = magnitude * 10 + (*probe -
'0');
7468 exponent_part =
static_cast<int32_t
>(exponent_negative ? -magnitude : magnitude);
7469 exponent10 += exponent_part;
7474 const uint32_t sign = negative ? 1u : 0u;
7485 digits[count] =
'1';
7490 int32_t upper_exponent10 = exponent10;
7491 for (int32_t i = 0; i < count; ++i)
7492 upper[i] = digits[i];
7493 int32_t index = count - 1;
7494 while (index >= 0 && upper[index] ==
'9')
7495 upper[index--] =
'0';
7506 uint64_t below_low = 0, below_high = 0, above_low = 0, above_high = 0;
7507 below.
get_bits(below_low, below_high);
7508 above.
get_bits(above_low, above_high);
7509 if (below_low == above_low && below_high == above_high) {
7511 result.ec = below_ec;
7519 if (!wide || !all) {
7521 result.ec = below_ec;
7524 int32_t all_count = 0;
7525 int32_t all_exponent10 = exponent_part;
7526 bool in_fraction =
false, significant =
false, rest =
false;
7527 for (
const char* q = significand; q < significand_end; ++q) {
7532 significant = significant || (*q !=
'0');
7539 all[
static_cast<size_t>(all_count++)] = *q;
7545 rest = rest || (*q !=
'0');
7549 all[
static_cast<size_t>(all_count++)] =
'1';
7568inline std::from_chars_result
from_chars(
const char* first,
const char* last,
float128& value, std::chars_format fmt)
7570 if (fmt == std::chars_format::hex)
7588inline std::to_chars_result
to_chars(
char* first,
char* last,
const float128& value, std::chars_format fmt,
int precision)
7592 case std::chars_format::scientific: style =
'e';
break;
7593 case std::chars_format::fixed: style =
'f';
break;
7594 case std::chars_format::hex: style =
'a';
break;
7595 default: style =
'g';
break;
7599 const std::string text =
detail::render(value, style, precision,
false,
false,
'-', fmt == std::chars_format::hex);
7600 if (text.size() >
static_cast<size_t>(last - first))
7601 return {last, std::errc::value_too_large};
7603 for (
size_t i = 0; i < text.size(); ++i)
7605 return {first + text.size(), std::errc {}};
7609inline std::to_chars_result
to_chars(
char* first,
char* last,
const float128& value, std::chars_format fmt)
7611 return to_chars(first, last, value, fmt, -1);
7617 const std::string text =
detail::render(value,
'\0', -1,
false,
false,
'-');
7618 if (text.size() >
static_cast<size_t>(last - first))
7619 return {last, std::errc::value_too_large};
7621 for (
size_t i = 0; i < text.size(); ++i)
7623 return {first + text.size(), std::errc {}};
7635 const std::ios_base::fmtflags flags = os.flags();
7636 const std::ios_base::fmtflags style_flags = flags & std::ios_base::floatfield;
7639 int32_t precision =
static_cast<int32_t
>(os.precision());
7640 if (style_flags == std::ios_base::fixed) {
7642 }
else if (style_flags == std::ios_base::scientific) {
7644 }
else if (style_flags == (std::ios_base::fixed | std::ios_base::scientific)) {
7653 const char sign_char = (flags & std::ios_base::showpos) ?
'+' :
'-';
7654 const bool uppercase = (flags & std::ios_base::uppercase) != 0;
7655 const bool alternate = (flags & std::ios_base::showpoint) != 0;
7656 std::string text =
detail::render(value, style, precision, uppercase, alternate, sign_char);
7658 const std::streamsize width = os.width();
7660 if (
static_cast<std::streamsize
>(text.size()) < width) {
7661 const size_t padding =
static_cast<size_t>(width) - text.size();
7662 const std::ios_base::fmtflags adjust = flags & std::ios_base::adjustfield;
7663 if (adjust == std::ios_base::left) {
7664 text.append(padding, os.fill());
7665 }
else if (adjust == std::ios_base::internal && !text.empty() && (text[0] ==
'-' || text[0] ==
'+')) {
7667 text.insert(1, padding, os.fill());
7669 text.insert(0, padding, os.fill());
7693 const std::istream::sentry sentry(is);
7697 std::streambuf*
const buffer = is.rdbuf();
7698 using traits = std::char_traits<char>;
7700 int c = buffer->sgetc();
7701 const auto take = [&]() {
7702 text += traits::to_char_type(c);
7704 c = buffer->sgetc();
7706 const auto is_digit = [](
int ch) {
return ch >=
'0' && ch <=
'9'; };
7707 const auto is_hex_digit = [](
int ch) {
return (ch >=
'0' && ch <=
'9') || (ch >=
'a' && ch <=
'f') || (ch >=
'A' && ch <=
'F'); };
7708 const auto lower = [](
int ch) {
return (ch >=
'A' && ch <=
'Z') ? ch -
'A' +
'a' : ch; };
7710 if (c ==
'+' || c ==
'-') {
7713 const size_t body = text.size();
7718 if (c ==
'x' || c ==
'X') {
7724 if (text.size() == body && c != traits::eof() && lower(c) >=
'a' && lower(c) <=
'z') {
7726 static constexpr const char* words[] = {
"infinity",
"snan",
"nan"};
7728 while (c != traits::eof()) {
7729 const std::string next = word +
static_cast<char>(lower(c));
7730 bool prefix =
false;
7731 for (
const char* candidate : words) {
7732 prefix = prefix || std::string(candidate).compare(0, next.size(), next) == 0;
7739 if ((word ==
"nan" || word ==
"snan") && c ==
'(') {
7741 while (c != traits::eof() && (is_digit(c) || (lower(c) >=
'a' && lower(c) <=
'z') || c ==
'_')) {
7749 const auto mantissa_digit = [&](
int ch) {
return hex ? is_hex_digit(ch) : is_digit(ch); };
7750 while (c != traits::eof() && mantissa_digit(c)) {
7755 while (c != traits::eof() && mantissa_digit(c)) {
7759 if (c != traits::eof() && lower(c) == (hex ?
'p' :
'e')) {
7761 if (c ==
'+' || c ==
'-') {
7764 while (c != traits::eof() && is_digit(c)) {
7769 if (c == traits::eof()) {
7770 is.setstate(std::ios_base::eofbit);
7775 const char*
const first = text.data();
7776 const char*
const last = first + text.size();
7779 const std::from_chars_result result =
from_chars(first + body + 2, last, parsed, std::chars_format::hex);
7780 ok = result.ec != std::errc::invalid_argument && result.ptr == last;
7781 if (ok && body != 0 && text[0] ==
'-') {
7785 const std::from_chars_result result =
from_chars(first, last, parsed);
7786 ok = result.ec != std::errc::invalid_argument && result.ptr == last;
7789 is.setstate(std::ios_base::failbit);
7814template <>
class numeric_limits<
fp128::float128>
7883template <>
struct formatter<
fp128::float128, char>
7885 constexpr auto parse(basic_format_parse_context<char>& context)
7887 auto it = context.begin();
7888 const auto end = context.end();
7889 if (it == end || *it ==
'}')
7893 const auto is_align = [](
char c) {
return c ==
'<' || c ==
'>' || c ==
'^'; };
7894 if (it + 1 != end && is_align(*(it + 1))) {
7898 }
else if (is_align(*it)) {
7903 if (it != end && (*it ==
'+' || *it ==
'-' || *it ==
' '))
7907 if (it != end && *it ==
'#') {
7913 if (it != end && *it ==
'0') {
7919 while (it != end && *it >=
'0' && *it <=
'9')
7923 if (it != end && *it ==
'.') {
7926 while (it != end && *it >=
'0' && *it <=
'9')
7931 if (it != end && *it !=
'}') {
7944 throw format_error(
"invalid type in the format specification for float128");
7948 if (it != end && *it !=
'}')
7949 throw format_error(
"unmatched brace in the format specification for float128");
7955 const string text =
render(value);
7956 return std::copy(text.begin(), text.end(), context.out());
7962 const bool uppercase = (
type >=
'A' &&
type <=
'Z');
7963 const char style =
static_cast<char>(uppercase ? (
type -
'A' +
'a') :
type);
7966 if (
static_cast<int>(text.size()) >=
width)
7969 const size_t padding =
static_cast<size_t>(
width) - text.size();
7973 const size_t offset = (!text.empty() && (text[0] ==
'-' || text[0] ==
'+' || text[0] ==
' ')) ? 1u : 0u;
7974 text.insert(offset, padding,
'0');
7980 text.append(padding,
fill);
7983 text.insert(0, padding / 2,
fill);
7984 text.append(padding - padding / 2,
fill);
7989 text.insert(0, padding,
fill);
8015 uint64_t low = 0, high = 0;
8016 value.get_bits(low, high);
8017 if (value.is_zero())
8021 uint64_t state = low + 0x9E3779B97F4A7C15ull;
8022 state = (state ^ (state >> 30)) * 0xBF58476D1CE4E5B9ull;
8023 state = (state ^ (state >> 27)) * 0x94D049BB133111EBull;
8024 state ^= high + 0x9E3779B97F4A7C15ull + (state << 6) + (state >> 2);
8025 state = (state ^ (state >> 30)) * 0xBF58476D1CE4E5B9ull;
8026 return static_cast<size_t>(state ^ (state >> 31));
8036template <
typename T>
8037 requires is_arithmetic_v<T>
8042template <
typename T>
8043 requires is_arithmetic_v<T>
Fixed capacity unsigned big integer, 32 bit limbs, least significant first.
Reduces what a math function's internal operations raise to what its result justifies.
T operator()(const T &result) noexcept
Records the flags a result justifies and passes the result through.
flag_filter(const flag_filter &)=delete
T inexact(const T &result) noexcept
The same as operator(), for a result known to be inexact without an operation having said so: an over...
flag_filter & operator=(const flag_filter &)=delete
Sets the exception flags aside for its lifetime, and discards whatever is raised meanwhile.
flag_quiet(const flag_quiet &)=delete
flag_quiet & operator=(const flag_quiet &)=delete
128 bit floating point class.
friend constexpr bool islessgreater(const float128 &x, const float128 &y) noexcept
x < y or x > y, false when either is a NaN.
static constexpr int32_t LOG1P_TERMS
Terms of the log1p_small() series. |s| stays below 2^-4.9, so s^24 is past the last bit.
friend constexpr float128 fmaxmag(const float128 &x, const float128 &y) noexcept
maxNumMag: whichever of x and y has the larger magnitude, or fmax() when they tie.
static constexpr void shift_right_jam128_extra(uint64_t &l, uint64_t &h, uint64_t &extra, int32_t dist) noexcept
Shifts a significand and its extra word right, keeping a sticky record of what fell off.
friend constexpr bool totalorder(const float128 &x, const float128 &y) noexcept
totalOrder(x, y): true when x precedes y, or equals it, in the IEEE 754 total order.
constexpr float128 operator>>(T shift) const noexcept
Performs right shift operation.
static constexpr float128 egamma() noexcept
The Euler-Mascheroni constant.
friend constexpr float128 fabs(const float128 &x) noexcept
Returns the absolute value of x.
static float128 pown_magnitude(const float128 &a, int32_t n) noexcept
a^n by repeated squaring, for a positive finite a.
friend constexpr float128 scalbn(const float128 &x, int n) noexcept
x * 2^n, the same operation ldexp performs.
friend float128 acos(float128 x) noexcept
Calculate the inverse cosine function.
static constexpr int32_t GAMMA_RECURRENCE_LIMIT
Below this magnitude lgamma() and tgamma() move the argument next to two by the recurrence.
friend float128 atan2(float128 y, float128 x) noexcept
Calculate the inverse tangent function of the ratio y / x.
static constexpr int32_t COS_TERMS
Terms of the cosine series cos1() runs. Up to pi/4, x^32/32! is below 2^-125.
friend constexpr bool isfinite(const float128 &x) noexcept
Tests whether x is finite (not infinite and not NaN).
static constexpr float128 nan()
Return the quiet (non-signaling) NaN constant.
friend float128 log1p(float128 x) noexcept
Calculates the natural Log (base e) of 1 + x: log(1 + x)
friend float128 hypot(const float128 &x, const float128 &y) noexcept
Calculates the hypotenuse. i.e. sqrt(x^2 + y^2)
static constexpr float128 from_long_double(long double x) noexcept
Converts a long double wider than a double, exactly.
static constexpr int32_t SCALE_LIMIT
Largest scaling a shift or ldexp() ever needs: enough to take the largest finite value below half the...
static constexpr float128 sqrt_2() noexcept
Returns a value of sqrt(2)
constexpr bool is_signaling() const
Tests if this value is a signaling NaN.
friend constexpr float128 ldexp(float128 x, int exp) noexcept
Multiplies a floating-point number by an integral power of two.
friend constexpr bool operator==(const float128 &lhs, const T &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
static constexpr bool fixed_is_zero(const uint64_t frac[FIXED_WORDS]) noexcept
True when every word of the fixed point fraction is zero.
static constexpr bool is754version1985(void)
Returns false; this type does not conform to IEEE 754-1985.
friend float128 sinh(const float128 x) noexcept
Calculate the hyperbolic sine function Use the exponent function which produces more accurate results...
static constexpr float128 exp10(int32_t e) noexcept
Calculates 10^e.
friend constexpr bool operator<=(const T &lhs, const float128 &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr bool is_positive() const noexcept
Returns true if the value is positive (including zero and NaN)
constexpr void set_fraction_bits(uint64_t f) noexcept
Sets the upper 48 bits of the fraction, leaving the exponent and the sign alone.
friend constexpr float128 operator<<(const T &lhs, const float128 &rhs) noexcept
Shifts a scalar left by a float128 shift count, scaling it by a power of two.
constexpr float128 & operator+=(const T &rhs)
Add a value to this object.
constexpr uint64_t truncated_magnitude(bool &overflow) const noexcept
The integer part of |x|, truncated towards zero.
static constexpr void wide_add(uint64_t *a, const uint64_t *b) noexcept
Adds b into a. The accumulator is wide enough that the sum cannot carry out.
friend float128 pow(const float128 &x, const float128 &y) noexcept
Computes x to the power of y.
friend constexpr float128 sqr(float128 x) noexcept
Calculates the square of a value. i.e. x^2.
friend void erf_constants(float128 *a, int32_t count)
Calculates the Maclaurin series constants for the erf function. The array will hold 1 / (n!...
friend constexpr float128 operator>>(const T &lhs, const float128 &rhs) noexcept
Shifts a scalar right by a float128 shift count, scaling it by a power of two. The left operand is wi...
friend constexpr bool issignaling(const float128 &x) noexcept
isSignaling: true for a signaling NaN.
constexpr bool is_nan() const
Tests if this value is a NaN.
constexpr float128 & operator=(const float128 &rhs) noexcept
Assignment operator.
friend float128 nan(const char *payload) noexcept
A quiet NaN carrying the payload spelled out in the argument.
friend constexpr float128 operator-(float128 lhs, const T &rhs) noexcept
subtracts the right hand side operand to this object to and returns the result.
static float128 abs_sin_pi(const float128 &x) noexcept
|sin(pi * x)| for a finite x that is not an integer, without the error of rounding pi * x.
constexpr bool is_special() const
return true when the value is either an inf or nan
constexpr void set_exponent(int32_t e) noexcept
Set the exponent.
constexpr float128 get_fraction() const
Return the fraction part as a float128.
friend float128 reciprocal(const float128 &x) noexcept
Calculates the reciprocal of a value, 1 / x, correctly rounded.
friend constexpr bool operator>=(const float128 &lhs, const T &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
static constexpr int32_t EXP_TERMS
Calculates the exponent of x: e Using the Maclaurin series expansion, the formula is: 1 2 3 x x x exp...
constexpr float128 & operator*=(const float128 &rhs) noexcept
Multiply a value to this object.
friend constexpr float128 rint(const float128 &x) noexcept
x rounded to an integral value in the current direction, raising inexact when that changes it.
static constexpr float128 log10_e() noexcept
Base 10 logarithm of e.
static constexpr bool wide_place(uint64_t *acc, const uint64_t *src, int32_t words, int32_t offset) noexcept
Writes a value into a zeroed accumulator, shifted to a given bit position.
constexpr float128(double x) noexcept
Constructor from the double type.
_float128_bits high_bits
Upper 64 bits (bit-field view).
constexpr bool operator!() const noexcept
Logical not (!). Opposite of operator bool. Uses is_zero() rather than testing the raw words: negativ...
constexpr void set_sign(uint64_t s) noexcept
Sets the sign.
friend constexpr bool operator==(const T &lhs, const float128 &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
static void reduce_half_pi(const float128 &x, int64_t n, float128 &hi, float128 &lo) noexcept
Subtracts n*(pi/2) from x, leaving the residual as an unevaluated sum.
constexpr float128(uint64_t lf, uint64_t hf, uint32_t e, uint32_t s) noexcept
Low level constructor.
friend constexpr float128 operator-(const T &lhs, const float128 &rhs) noexcept
Subtracts a float128 from a scalar.
friend constexpr bool operator!=(const float128 &lhs, const T &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr bool is_zero() const noexcept
Returns true if and only if the value is ±0.
static constexpr float128 propagate_nan(const float128 &a, const float128 &b, const float128 &c) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
static constexpr float128 norm_round_pack(uint32_t sign, int32_t e, uint64_t l, uint64_t h, bool sticky) noexcept
Normalizes an exact non negative 128 bit significand and rounds it once.
friend constexpr float128 fdim(const float128 &x, const float128 &y) noexcept
Determines the positive difference between the first and second values.
static constexpr float128 half() noexcept
Returns a value of 0.5.
static constexpr bool log_special(const float128 &x, float128 &result) noexcept
The arguments every logarithm treats alike (IEEE 754-2008 9.2.1).
constexpr float128 & operator*=(const T &rhs)
Multiply a value to this object.
friend constexpr bool iszero(const float128 &x) noexcept
isZero: true for either zero.
friend constexpr bool islessequal(const float128 &x, const float128 &y) noexcept
x <= y, false when either is a NaN.
friend constexpr float128 fmin(const float128 &x, const float128 &y) noexcept
Returns the minimum between x and y.
static constexpr bool is754version2008(void)
Whether the type conforms to IEEE 754-2008 (binary128).
static constexpr float128 ln10() noexcept
Natural logarithm of 10.
static constexpr int32_t square_compare(uint64_t rl, uint64_t rh, const uint64_t n[4]) noexcept
Square of a 113 bit significand compared with a 256 bit value.
friend float128 expm1(const float128 &x) noexcept
Calculates the exponent of x and reduces 1 from the result: (e^x) - 1.
friend constexpr bool operator<=(const float128 &lhs, const float128 &rhs) noexcept
Return true this object is small or equal than the other.
friend constexpr bool operator>(const T &lhs, const float128 &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr float128() noexcept
Default constructor, creates an instance with a value of zero.
static constexpr uint64_t norm_product(uint64_t &l, uint64_t &h, int32_t &e, bool sticky) noexcept
Normalizes the product of two mantissas to bit 112, handing back the bits it drops.
friend constexpr bool operator<(const float128 &lhs, const T &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
static constexpr float128 from_fraction128(uint64_t low, uint64_t high) noexcept
Builds a float128 from a 128 bit fraction, a value in [0,1).
static int32_t reduce_large(const float128 &x, float128 &hi, float128 &lo) noexcept
Reduces an argument of any size: x = n * pi/2 + r, with |r| <= pi/4 (Payne and Hanek).
friend constexpr bool isless(const float128 &x, const float128 &y) noexcept
x < y, false when either is a NaN.
friend float128 tan(float128 x) noexcept
Calculate the tangent function tan(x) = sin(x)/cos(x)
friend float128 exp2(const float128 &x) noexcept
Computes 2 to the power of x.
static float128 lgamma_near_two(const float128 &z) noexcept
lgamma(2 + z) for |z| <= 1/2, accurate relative to the result.
static void quick_two_sum(const float128 &a, const float128 &b, float128 &sum, float128 &err) noexcept
Exact sum of two values whose magnitudes are ordered.
friend constexpr float128 copysign(const float128 &x, const float128 &y) noexcept
returns the value of x with the sign of y.
constexpr float128 & operator-=(const float128 &rhs) noexcept
Subtract a value from this object.
constexpr float128 & operator-=(const T &rhs)
Subtract a value from this object.
friend float128 double_factorial(int x) noexcept
Returns the double factorial of a number, x!! = x * (x - 2) * (x - 4) * ...
static constexpr float128 round_integral(const float128 &x, detail::rounding mode, bool ties_away, bool signal_inexact) noexcept
Rounds to an integral value (IEEE 754-2008 roundToIntegral, 5.3.1).
static constexpr float128 propagate_nan(const float128 &a, const float128 &b) noexcept
The result of an operation with a NaN operand (IEEE 754-2008 6.2).
friend constexpr int32_t lrint(const float128 &x) noexcept
Rounds x to an integer in the current rounding direction and returns it as int32_t.
friend float128 operator/(const T &lhs, const float128 &rhs)
Divides a scalar by a float128.
friend constexpr float128 fma(float128 x, float128 y, float128 z) noexcept
Computes (x * y) + z with a single rounding at the end.
friend float128 tanh(const float128 x) noexcept
Calculates the hyperbolic tangent e^x - e^(-x) tanh(x) = ---------— e^x + e^(-x)
constexpr bool is_subnormal() const noexcept
Tests if the value is subnormal: non zero, and below the smallest normal magnitude.
static constexpr float128 inv_sqrt_3() noexcept
1/sqrt(3)
constexpr int32_t get_exponent() const noexcept
Returns the exponent of the object - like the base 2 exponent of a floating point A value of 2....
constexpr float128_class_t get_class() const noexcept
Classifies the float128 value according to IEEE 754.
static constexpr int32_t wide_cmp(const uint64_t *a, const uint64_t *b) noexcept
Three way comparison of two accumulators.
friend float128 cos(float128 x) noexcept
Calculate the cosine function Ultimately uses sin1() with a reduced range of [-pi/4,...
friend float128 operator/(float128 lhs, const T &rhs)
Divides this object by the right hand side operand and returns the result.
constexpr bool is_inf() const
Tests if this value is an Infinite (negative or positive)
static constexpr float128 log10_2() noexcept
Base 10 logarithm of 2.
friend constexpr bool operator!=(const T &lhs, const float128 &rhs) noexcept
constexpr void total_order_key(uint64_t &l, uint64_t &h) const noexcept
The encoding as a key whose unsigned order is the total order of IEEE 754-2008 5.10.
friend constexpr float128 operator+(float128 lhs, const T &rhs) noexcept
Adds 2 values and returns the result.
friend float128 pow(const float128 &x, int32_t y) noexcept
Computes x to the power of y (IEEE 754-2008 pown)
static int32_t reduce_trig(const float128 &x, float128 &hi, float128 &lo) noexcept
Reduces x to [-pi/4, pi/4], as an unevaluated sum, and returns the quadrant.
uint64_t high
Upper 64 bits (raw).
static constexpr float128 round_pack(uint32_t sign, int32_t e, uint64_t l, uint64_t h, uint64_t extra, detail::rounding mode) noexcept
friend constexpr bool isinf(const float128 &x)
Tests if the value is an Infinite (negative or positive)
static constexpr float128 round_pack(uint32_t sign, int32_t e, uint64_t l, uint64_t h, uint64_t extra) noexcept
Rounds an exact result once, in the current rounding direction, and encodes it.
friend constexpr bool iscanonical(const float128 &) noexcept
isCanonical: always true, every binary128 encoding is canonical.
friend float128 tgamma(float128 x) noexcept
The gamma function.
friend float128 acosh(const float128 x) noexcept
Calculates the inverse hyperbolic cosine For x >= 1: acosh(x) = log(x + sqrt(x^2 - 1)) For x < 1,...
static float128 log1p_small(const float128 &t) noexcept
Natural logarithm of 1+t for |t| <= 2^-4, accurate relative to the result.
static constexpr void mul128to256(uint64_t al, uint64_t ah, uint64_t bl, uint64_t bh, uint64_t res[4]) noexcept
Full 256 bit product of two 128 bit values.
friend float128 cos1(const float128 &x) noexcept
Calculate the cosine function over a limited range [-0.5pi, 0.5pi].
constexpr bool is_exponent_of_2() const
Tests if the value is an exponent of 2 (fraction part is zero)
void to_e_format(char *str, int32_t buff_size) const
Writes the value in scientific notation into a caller supplied buffer.
constexpr bool is_normal() const noexcept
Tests if the value is normal (not zero, subnormal, infinite, or NaN)
friend float128 remainder(const float128 &x, const float128 &y) noexcept
IEEE 754 remainder: x - n*y with n the quotient rounded to the nearest even integer.
friend float128 cbrt(const float128 x, uint32_t iterations) noexcept
Calculates the cube root. Uses the Halley's method.
friend constexpr float128 operator*(const T &lhs, const float128 &rhs) noexcept
Multiplies a scalar and a float128, in that order.
friend constexpr bool operator<=(const float128 &lhs, const T &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
static constexpr bool round_increment(detail::rounding mode, uint32_t sign, uint64_t extra) noexcept
Whether rounding should add one unit in the last place.
constexpr void set_components(uint64_t l, uint64_t h, int32_t e, uint32_t s) noexcept
Stores a significand and exponent, rounding when the value is subnormal.
friend constexpr float128 fminmag(const float128 &x, const float128 &y) noexcept
minNumMag: whichever of x and y has the smaller magnitude, or fmin() when they tie.
constexpr uint64_t get_fraction_bits() const noexcept
Reads the upper 48 bits of the fraction out of the high QWORD.
constexpr float128 & square() noexcept
Squares this object in place.
friend void fact_reciprocal(int x, float128 &res) noexcept
Factorial reciprocal (inverse). Calculates 1 / x! Maximum supported value of x is 50.
friend float128 sqrt(const float128 &x, uint32_t iterations) noexcept
Calculates the square root, correctly rounded.
static constexpr float128 signaling_nan()
Return a signaling NaN.
static float128 fmod_magnitude(const float128 &x, const float128 &y, uint64_t "ient, int32_t &half) noexcept
|x| mod |y|, along with the low bits of the quotient it implies.
constexpr float128 & operator+=(const float128 &rhs) noexcept
Add a value to this object.
static constexpr float128 quarter_pi()
Return the value of pi / 4.
friend constexpr float128 modf(const float128 &x, float128 *iptr) noexcept
Split into integer and fraction parts. Both results carry the sign of the input variable.
static constexpr float128 log2_e() noexcept
Base 2 logarithm of e.
static constexpr int32_t LGAMMA_NEAR_TWO_TERMS
Terms of the series lgamma_near_two() runs. For |z| <= 1/2 the last is below 2^-120 of the sum.
static constexpr float128 phi() noexcept
The golden ratio.
friend float128 log10(float128 x) noexcept
Calculates Log base 10 of x: log10(x)
static constexpr float128 sqrt_3() noexcept
Square root of 3.
friend float128 atanh(const float128 x) noexcept
Calculates the inverse hyperbolic tangent 1 + x atanh(x) = 0.5 * log( --—) 1 - x.
constexpr float128 & operator=(float128 &&rhs) noexcept
Move assignment operator.
float128 & operator/=(const T &rhs)
Divide this object by a value.
friend constexpr bool isgreaterequal(const float128 &x, const float128 &y) noexcept
x >= y, false when either is a NaN.
static constexpr float128 ln2() noexcept
Natural logarithm of 2.
constexpr bool fraction_to_fixed(uint64_t frac[FIXED_WORDS]) const noexcept
Expands the fraction part of this value into an exact fixed point number.
friend constexpr float128 nan(const float128 &)
Return the NaN constant.
constexpr ~float128()=default
Destructor.
static constexpr int32_t wide_msb(const uint64_t *a, int32_t words) noexcept
Index of the highest set bit of a multi word value, or -1 when it is zero.
constexpr void invert_sign() noexcept
Inverts the sign.
constexpr float128(uint64_t l, uint64_t h) noexcept
Low level constructor.
friend constexpr bool operator!=(const float128 &lhs, const float128 &rhs) noexcept
Return true when objects are not equal. Can be used as logical XOR.
static constexpr float128 inv_sqrt_pi() noexcept
1/sqrt(pi)
friend constexpr float128 operator*(float128 lhs, const T &rhs) noexcept
Multiplies the right hand side operand with this object to and returns the result.
friend float128 lgamma(float128 x) noexcept
Natural logarithm of the absolute value of the gamma function.
static constexpr float128 quiet_nan(const float128 &x) noexcept
x with its quiet bit set. The payload, and the sign, are kept.
friend constexpr float128 scalbln(const float128 &x, long n) noexcept
x * 2^n for a long exponent, clamped to what the format can reach.
friend constexpr float128 fmax(const float128 &x, const float128 &y) noexcept
Returns the maximum between x and y.
friend constexpr int32_t ilogb(const float128 &x) noexcept
Retrieves an integer that represents the base-2 exponent of the specified value.
friend float128 exp(const float128 &x) noexcept
Computes e to the power of x.
friend constexpr float128 frexp(float128 x, int *expptr) noexcept
Gets the mantissa and exponent of a floating-point number.
static constexpr float128 propagate_nan(const float128 &a) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
static constexpr int32_t WIDE_TOP
Bit the leading one of the larger operand is placed on.
constexpr float128 operator-() const noexcept
Unary -. Returns a copy of the object with sign inverted.
uint64_t low
Lower 64 bits of the float128 encoding.
constexpr I to_integer() const noexcept
Converts to an integer type, truncating towards zero.
friend constexpr bool operator>(const float128 &lhs, const T &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr void get_bits(uint64_t &l, uint64_t &h) const noexcept
Reads the raw binary128 encoding.
float128(const std::string &x) noexcept
Constructor from std::string. Allows creating very high precision values. Much slower than the other ...
constexpr uint64_t nan_payload_top(int32_t shift) const noexcept
The top of a NaN's payload, for a narrower format to carry.
friend float128 cosh(const float128 x) noexcept
Calculate the hyperbolic cosine function over a limited range [-0.5pi, 0.5pi] e^x + e^(-x) cosh(x) = ...
constexpr uint64_t get_exponent_bits() const noexcept
Reads the biased exponent field out of the high QWORD.
static constexpr float128 nextDown(float128 x)
Produces the closest value smaller than x.
friend float128 log(float128 x) noexcept
Calculates the natural Log (base e) of x: log(x)
static constexpr float128 one() noexcept
Returns a value of 1.
constexpr float128 operator<<(T shift) const noexcept
Performs left shift operation. Hinted rather than forced, see operator>> above.
static constexpr float128 exp10_double_word(int32_t e) noexcept
The power of exp10(), for an e whose result is inside the range of the format.
static constexpr float128 inv_pi() noexcept
1/pi
constexpr bool is_int() const
Returns if the value is an integer (fraction is zero).
static constexpr float128 pi()
Return the value of pi.
friend float128 sin1(float128 x) noexcept
Calculate the sine function over a limited range [-0.5pi, 0.5pi] Using the Maclaurin series expansion...
friend constexpr bool totalordermag(const float128 &x, const float128 &y) noexcept
totalOrderMag(x, y): totalorder() of the absolute values.
friend constexpr float128 nearbyint(const float128 &x) noexcept
x rounded to an integral value in the current direction.
constexpr narrowed narrow() const noexcept
Rounds this finite, non zero value to a narrower binary format.
static void two_sum(const float128 &a, const float128 &b, float128 &sum, float128 &err) noexcept
Exact sum of two values, as a rounded sum and the error it made.
static constexpr float128 e()
Return the value of e.
friend constexpr bool isgreater(const float128 &x, const float128 &y) noexcept
x > y, false rather than unordered when either is a NaN.
static constexpr uint64_t MAX_SIG_HIGH
The largest finite magnitude's significand, high QWORD, with its leading one at bit 48.
friend constexpr bool operator>(const float128 &lhs, const float128 &rhs) noexcept
Return true this object is larger than the other.
static constexpr int32_t WIDE_WORDS
Words in the accumulator.
static constexpr int32_t STIRLING_TERMS
Terms of the Stirling series lgamma() runs. Sixteen reach 2^-119 for an argument of 32.
static constexpr void wide_dec(uint64_t *a) noexcept
Subtracts one from the accumulator, which must not be zero.
constexpr float128(T x) noexcept
Generic constructor for integral and floating-point types.
static int64_t quadrant_of(const float128 &x) noexcept
Nearest integer to x / (pi/2), the quadrant the argument falls in.
static constexpr float128 inf()
Return the infinite constant.
constexpr float128(float128 &&rhs) noexcept
Move constructor Doesn't modify the right hand side object. Acts like a copy constructor.
constexpr float128 & operator>>=(int32_t shift) noexcept
Shift right this object, dividing it by a power of two.
constexpr void get_components(uint64_t &l, uint64_t &h, int32_t &e, uint32_t &s) const noexcept
break the float into its components. Normalizes subnormal values
constexpr int32_t get_bit(uint32_t bit) const noexcept
get a specific bit within the float128 data
friend float128 fmod(const float128 &x, const float128 &y) noexcept
Performs the fmod() function, similar to libc's fmod(), returns the remainder of a division x/root.
friend constexpr float128 round(const float128 &x) noexcept
Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.
friend constexpr float128 nexttoward(const float128 &x, const float128 &y) noexcept
The neighbour of x towards y. The same as nextafter: there is no wider type to convert from.
static constexpr float128 two_pi() noexcept
2*pi
static constexpr float128 round_pack_edge(uint32_t sign, int32_t exp, uint64_t l, uint64_t h, uint64_t extra, detail::rounding mode) noexcept
The slow half of round_pack(): results that overflow or are subnormal.
constexpr float128 operator+() const noexcept
Unary +. Returns a copy of the object.
static constexpr float128 nextUp(float128 x)
Produces the closest value larger than x nextUp(x) is the least floating-point number in the format o...
friend float128 erf(float128 x) noexcept
Computes the error function of a value. The erf function return a value in the range -1....
friend float128 asin(float128 x) noexcept
Calculate the inverse sine function.
friend constexpr int64_t llround(const float128 &x) noexcept
Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.
friend constexpr bool isunordered(const float128 &x, const float128 &y) noexcept
True when either operand is a NaN, so that the two do not compare in any order.
static float128 exp_reduced(const float128 &r) noexcept
e to the power of a reduced argument, |r| <= ln2/2.
friend float128 hypot(const float128 &x, const float128 &y, const float128 &z) noexcept
Length of the diagonal of a box, computed without an intermediate overflow.
friend constexpr int fpclassify(const float128 &x) noexcept
One of FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL or FP_NORMAL.
friend float128 log2(float128 x) noexcept
Calculates the Log base 2 of x: y = log2(x)
friend constexpr int32_t lround(const float128 &x) noexcept
Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.
friend float128 erfc(float128 x) noexcept
Computes the complementary error function, 1 - erf(x).
static constexpr float128 tenth() noexcept
Return 0.1 using maximum precision.
static constexpr int32_t FIXED_WORDS
Normalize the fraction so the msb (unity bit) is on bit 112. The fraction value must contain the unit...
constexpr void set_exponent_bits(uint64_t e) noexcept
Sets the biased exponent field, leaving the fraction and the sign alone.
float128(const char *x) noexcept
Construct from a string Allows creating very high precision values, approximately 34 decimal digits....
static constexpr float128 log1p_small_limit() noexcept
Largest |t| the log1p_small() series is used for. Chosen so twelve terms suffice.
friend constexpr int64_t llrint(const float128 &x) noexcept
Rounds x to an integer in the current rounding direction and returns it as int64_t.
constexpr float128 & operator<<=(int32_t shift) noexcept
Shift left this object, multiplying it by a power of two.
static constexpr float128 invalid_operation() noexcept
The result of an invalid operation on non NaN operands, such as inf - inf or 0 / 0.
friend constexpr float128 abs(const float128 &x) noexcept
Absolute value, the name <cmath> gives the floating point overload alongside fabs.
float128 & operator/=(const float128 &rhs)
Divide this object by a value.
static constexpr bool is_odd_int(const float128 &x) noexcept
True when x is an integer whose last place is one, which is the parity a tie needs.
static constexpr float128 half_pi()
Return the value of pi / 2.
friend constexpr bool signbit(const float128 &x) noexcept
True when the sign bit is set, including for -0 and for a negative NaN.
friend constexpr float128 floor(const float128 &x) noexcept
Performs the floor() function, similar to libc's floor(), rounds down towards -infinity.
friend constexpr float128 operator+(const T &lhs, const float128 &rhs) noexcept
Adds a scalar and a float128, in that order.
static constexpr uint64_t fixed_mul_extract(uint64_t frac[FIXED_WORDS], uint64_t mul) noexcept
Multiplies the fixed point fraction by a small value and returns the part that crossed the binary poi...
friend constexpr bool operator>=(const float128 &lhs, const float128 &rhs) noexcept
Return true this object is larger or equal than the other.
static constexpr I integral_to(const float128 &x) noexcept
Converts an integral value to an integer type, the way the C rounding functions do.
friend float128 sin(float128 x) noexcept
friend constexpr float128 ceil(const float128 &x) noexcept
Performs the ceil() function, similar to libc's ceil(), rounds up towards infinity.
static float128 exp10_filtered(int32_t e) noexcept
exp10_double_word() at run time, raising only the exceptions the result justifies.
constexpr bool is_negative() const noexcept
Returns true if the value is negative (including zero and NaN).
friend constexpr bool operator<(const T &lhs, const float128 &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
friend float128 remquo(const float128 &x, const float128 &y, int *quo) noexcept
The IEEE remainder together with the low bits of the quotient.
friend constexpr bool operator<(const float128 &lhs, const float128 &rhs) noexcept
Return true if this object is small than the other.
friend constexpr bool operator>=(const T &lhs, const float128 &rhs) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
friend float128 logb(float128 x) noexcept
Calculates Log base 2 of x as an integer ignoring the sign of x. Similar to: floor(log2(fabs(x)))
friend constexpr bool isnormal(const float128 &x) noexcept
True when x is neither zero, subnormal, infinite nor NaN.
friend constexpr bool issubnormal(const float128 &x) noexcept
isSubnormal: true for a subnormal value, which zero is not.
static constexpr void shift_right_jam128(uint64_t &l, uint64_t &h, int32_t dist) noexcept
Shifts a 128 bit value right, jamming any set bit that falls off into the lowest bit.
friend constexpr bool isnan(const float128 &x)
Tests if the value is a NaN.
static constexpr uint32_t exact_zero_sign() noexcept
The sign of an exact zero sum of two operands of opposite sign (IEEE 754-2008 6.3).
constexpr float128(const float128 &rhs) noexcept
Copy constructor.
static constexpr int32_t LOG2_TERMS
Number of Mercator series terms log2() needs.
constexpr uint32_t get_sign() const noexcept
Gets the sign.
friend constexpr float128 trunc(const float128 &x) noexcept
Rounds towards zero.
friend constexpr bool operator==(const float128 &lhs, const float128 &rhs) noexcept
Compare logical/bitwise equal.
static constexpr void wide_sub(uint64_t *a, const uint64_t *b) noexcept
Subtracts b from a, which must not be smaller.
friend float128 asinh(const float128 x) noexcept
Calculates the inverse hyperbolic sine For positive x: asinh(x) = log(x + sqrt(x^2 + 1)) For negative...
static constexpr int32_t WIDE_BITS
friend float128 atan(float128 x) noexcept
Calculate the inverse tangent function.
friend constexpr float128 nextafter(const float128 &x, const float128 &y) noexcept
The representable value next to x in the direction of y.
constexpr bool is_finite() const noexcept
Returns true if and only if x is zero, subnormal or normal (not infinite or NaN).
constexpr bool is_zero() const noexcept
Returns true if the value is zero.
constexpr void get_components(uint64_t &l, uint64_t &h) const noexcept
Extracts the low and high QWORD.
static constexpr bool has_denorm_loss
static constexpr fp128::float128 quiet_NaN() noexcept
static constexpr bool has_quiet_NaN
static constexpr fp128::float128 denorm_min() noexcept
Smallest positive subnormal, 2^-16494.
static constexpr bool tinyness_before
Tininess is detected after rounding, as x86 does for double.
static constexpr bool is_specialized
static constexpr float_round_style round_style
static constexpr bool is_modulo
static constexpr bool is_signed
static constexpr fp128::float128 lowest() noexcept
Most negative finite value.
static constexpr bool has_infinity
static constexpr fp128::float128 signaling_NaN() noexcept
static constexpr int digits10
Decimal digits that survive a round trip through the type.
static constexpr int min_exponent10
static constexpr int min_exponent
static constexpr bool is_iec559
True only with FP128_IEEE_ENV, which adds the rounding directions and the exception flags IEEE 754 re...
static constexpr fp128::float128 infinity() noexcept
static constexpr fp128::float128 epsilon() noexcept
Difference between one and the next larger value, 2^-112.
static constexpr bool is_integer
static constexpr bool has_signaling_NaN
static constexpr bool traps
Exceptions raise flags (with FP128_IEEE_ENV) but never trap.
static constexpr int digits
Mantissa bits including the implicit leading one.
static constexpr int max_digits10
Decimal digits needed to distinguish every value of the type.
static constexpr bool is_bounded
static constexpr fp128::float128 round_error() noexcept
Largest rounding error in units in the last place, one half.
static FP128_SUPPRESS_DEPRECATED_BEGIN constexpr float_denorm_style has_denorm
static constexpr int max_exponent
static constexpr bool is_exact
static constexpr fp128::float128 max() noexcept
Largest finite value, (2 - 2^-112) * 2^16383.
static constexpr int max_exponent10
static FP128_SUPPRESS_DEPRECATED_END constexpr fp128::float128 min() noexcept
Smallest positive normal value, 2^-16382.
static constexpr int radix
Exact conversion between binary128 and decimal.
Shared definitions, intrinsics, and helper functions for 128-bit fixed-point arithmetic.
#define FP128_SUPPRESS_DEPRECATED_END
Closes the region opened by FP128_SUPPRESS_DEPRECATED_BEGIN.
#define FP128_SUPPRESS_DEPRECATED_BEGIN
Opens a region in which the use of a deprecated declaration is not reported.
#define FP128_ONE_SHIFT(x)
#define FP128_GET_BIT(x, n)
#define FP128_MAX_VALUE_64(x)
#define FP128_FORCE_INLINE
Marks a function the compiler must inline rather than merely may.
int32_t to_decimal_digits(uint64_t mantissa_low, uint64_t mantissa_high, int32_t exponent, int32_t requested, char *digits, bool *exact=nullptr, digit_rounding direction=digit_rounding::nearest_even) noexcept
Correctly rounded decimal digits of a finite, non zero binary128 value.
std::from_chars_result from_hex_chars(const char *first, const char *last, float128 &value)
Reads a hexadecimal significand with an optional binary exponent, such as 1.8p1.
digit_rounding digit_rounding_for(uint32_t sign) noexcept
The digit rounding the current direction comes down to for a value of a given sign.
constexpr int32_t MAX_EXACT_DIGITS
Significant digits that settle the rounding of any decimal input exactly.
int32_t shortest_digit_count(const float128 &value)
Fewest significant digits that read back as the same value.
digit_rounding
How to_decimal_digits() rounds the last digit it keeps.
@ nearest_even
to the nearest digit, a tie to the even one
@ away_from_zero
up whenever anything was discarded
basic_big_uint< 3072 > wide_big_uint
The big integer that holds MAX_EXACT_DIGITS digits scaled to the smallest subnormal,...
void append_exponent(std::string &out, int32_t exponent, char marker, int32_t min_digits=2)
Appends the decimal exponent of a scientific or hexadecimal form, with a sign.
basic_big_uint< 560 > big_uint
The big integer every conversion uses, 560 limbs or 17920 bits.
constexpr void raise_flags(int flags) noexcept
Raises exception flags. Does nothing without FP128_IEEE_ENV or during constant evaluation.
rounding
The rounding-direction attributes IEEE 754-2008 requires of a binary format.
@ toward_zero
roundTowardZero
@ nearest_even
roundTiesToEven, the default
@ upward
roundTowardPositive
@ downward
roundTowardNegative
constexpr int32_t MAX_OUTPUT_DIGITS
Largest number of digits to_decimal_digits() produces.
constexpr int32_t MAX_SIGNIFICANT_DIGITS
Largest number of significant digits any conversion here produces.
constexpr int32_t DEFAULT_PRECISION
Default precision the e, f and g styles use when none is given, as in printf.
bool decimal_to_binary(basic_big_uint< Capacity > &value, const char *digits, int32_t count, int32_t max_kept, int32_t exponent10, uint64_t &mantissa_low, uint64_t &mantissa_high, int32_t &exponent, uint64_t *extra) noexcept
Reads a decimal mantissa and exponent back to the nearest binary128.
constexpr uint64_t two_over_pi_bits[]
The binary expansion of 2/pi after the binary point, most significant word first.
std::string render(const float128 &value, char style, int32_t precision, bool uppercase, bool alternate, char sign_char, bool bare_hex=false)
Renders a float128 as text. Defined below, declared here because the conversions to a string delegate...
const char * parse_special_value(const char *cursor, const char *last, bool negative, float128 &value)
Reads one of the special values: inf, infinity, nan, nan(n-char-sequence) or snan.
std::errc round_decimal(basic_big_uint< Capacity > &workspace, const char *digits, int32_t count, int32_t max_kept, int32_t exponent10, uint32_t sign, float128 &value) noexcept
Rounds digits * 10^exponent10, with the given sign, to a float128.
bool from_decimal_digits(const char *digits, int32_t count, int32_t exponent10, uint64_t &mantissa_low, uint64_t &mantissa_high, int32_t &exponent, uint64_t *extra=nullptr) noexcept
Reads a decimal mantissa and exponent back to the nearest binary128.
constexpr rounding current_rounding() noexcept
The rounding direction every float128 operation applies.
Everything the library provides.
constexpr bool isunordered(const float128 &x, const float128 &y) noexcept
constexpr float128 fmaxmag(const float128 &x, const float128 &y) noexcept
constexpr bool islessequal(const float128 &x, const float128 &y) noexcept
constexpr bool iszero(const float128 &x) noexcept
constexpr float128 fminmag(const float128 &x, const float128 &y) noexcept
void fact_reciprocal(int x, fixed_point128< I > &res) noexcept
int fegetexceptflag(std::fexcept_t *flagp, int excepts) noexcept
Saves the state of float128 exception flags (IEEE 754 saveAllFlags).
fixed_point128< I > sqrt(const fixed_point128< I > &x, uint32_t iterations=3) noexcept
constexpr uint32_t array_length(const T &a)
Calculates the element count of a C style array at build time Example: int a[5]; constexpr int len = ...
fixed_point128< I > atan(fixed_point128< I > x) noexcept
constexpr void mul128_high(uint64_t a_low, uint64_t a_high, uint64_t b_low, uint64_t b_high, uint64_t &res_low, uint64_t &res_high) noexcept
Upper 128 bits of the product of two 128 bit unsigned values.
fixed_point128< I > ceil(const fixed_point128< I > &x) noexcept
constexpr int64_t llrint(const float128 &x) noexcept
constexpr bool iscanonical(const float128 &x) noexcept
fixed_point128< I > hypot(const fixed_point128< I > &x, const fixed_point128< I > &y) noexcept
fixed_point128< I > asin(fixed_point128< I > x) noexcept
fixed_point128< I > cos(fixed_point128< I > x) noexcept
fixed_point128< I > exp2(const fixed_point128< I > &x) noexcept
constexpr int32_t log2_reduction_bits
Bits of argument reduction fixed_point128::log2() applies; log2_recip_table has 2^this entries.
fixed_point128< I > log2(fixed_point128< I > x) noexcept
constexpr int32_t lround(const float128 &x) noexcept
constexpr bool isfinite(const float128 &x) noexcept
constexpr void twos_complement128(uint64_t &l, uint64_t &h) noexcept
converts a 128 integer to negative via 2's complement.
int32_t ilogb(const fixed_point128< I > &x) noexcept
float128 nan(const char *payload) noexcept
int fesetexceptflag(const std::fexcept_t *flagp, int excepts) noexcept
Restores float128 exception flags saved by fegetexceptflag (IEEE 754 restoreFlags).
std::to_chars_result to_chars(char *first, char *last, const float128 &value, std::chars_format fmt, int precision)
Converts a float128 to text, in the shape of std::to_chars.
static bool div_128bit(uint64_t *q, uint64_t *r, const uint64_t *u, const uint64_t *v) noexcept
Divides an unsigned integer of up to 256 bits by a 128 bit one. Knuth algorithm D,...
constexpr int32_t lrint(const float128 &x) noexcept
int feclearexcept(int excepts) noexcept
Lowers float128 exception flags (IEEE 754 lowerFlags).
constexpr float128 abs(const float128 &x) noexcept
constexpr bool totalorder(const float128 &x, const float128 &y) noexcept
constexpr float128 scalbln(const float128 &x, long n) noexcept
constexpr bool islessgreater(const float128 &x, const float128 &y) noexcept
constexpr int fpclassify(const float128 &x) noexcept
std::istream & operator>>(std::istream &is, fixed_point128< I > &value)
Reads a fixed_point128 from a stream, the way the builtin arithmetic extractors do.
constexpr uint64_t log2_recip_table[][2]
1/(1 + j/64) as a 128 bit fraction, the reciprocals fixed_point128::log2() reduces with.
float128 cbrt(const float128 x, uint32_t iterations=2) noexcept
std::string to_string(const float128 &value)
The shortest decimal string that reads back as this exact value.
std::from_chars_result from_chars(const char *first, const char *last, float128 &value)
Reads a decimal string into a float128. Defined below, declared here because the constructor from a s...
fixed_point128< I > fmax(const fixed_point128< I > &x, const fixed_point128< I > &y) noexcept
constexpr float128 scalbn(const float128 &x, int n) noexcept
fixed_point128< I > exp(const fixed_point128< I > &x) noexcept
fixed_point128< I > asinh(const fixed_point128< I > &x) noexcept
fixed_point128< I > atan2(fixed_point128< I > y, fixed_point128< I > x) noexcept
constexpr bool isgreaterequal(const float128 &x, const float128 &y) noexcept
fixed_point128< I > modf(const fixed_point128< I > &x, fixed_point128< I > *iptr) noexcept
float128 tgamma(float128 x) noexcept
constexpr bool isless(const float128 &x, const float128 &y) noexcept
int fegetround() noexcept
The rounding direction float128 arithmetic uses on the calling thread.
fixed_point128< I > tan(fixed_point128< I > x) noexcept
constexpr float128 frexp(float128 x, int *expptr) noexcept
constexpr bool issubnormal(const float128 &x) noexcept
constexpr float128 rint(const float128 &x) noexcept
constexpr float128 fma(float128 x, float128 y, float128 z) noexcept
fixed_point128< I > logb(fixed_point128< I > x) noexcept
constexpr bool isnormal(const float128 &x) noexcept
int128_base< false > uint128_t
128 bit unsigned integer type.
float128 erf(float128 x) noexcept
fixed_point128< I > floor(const fixed_point128< I > &x) noexcept
fixed_point128< I > fmod(const fixed_point128< I > &x, const fixed_point128< I > &y)
fixed_point128< I > sinh(const fixed_point128< I > &x) noexcept
constexpr bool totalordermag(const float128 &x, const float128 &y) noexcept
fixed_point128< I > fdim(const fixed_point128< I > &x, const fixed_point128< I > &y) noexcept
fixed_point128< I > round(const fixed_point128< I > &x) noexcept
fixed_point128< I > log1p(fixed_point128< I > x) noexcept
constexpr int64_t llround(const float128 &x) noexcept
int fetestexcept(int excepts) noexcept
Tests float128 exception flags (IEEE 754 testFlags).
constexpr bool issignaling(const float128 &x) noexcept
fixed_point128< I > sin(fixed_point128< I > x) noexcept
constexpr float128 nearbyint(const float128 &x) noexcept
fixed_point128< I > copysign(const fixed_point128< I > &x, const fixed_point128< I > &y) noexcept
fixed_point128< I > log10(fixed_point128< I > x) noexcept
constexpr bool isgreater(const float128 &x, const float128 &y) noexcept
static constexpr int32_t dbl_frac_bits
Mantissa bit count of an IEEE 754 double.
fixed_point128< I > cosh(const fixed_point128< I > &x) noexcept
int feraiseexcept(int excepts) noexcept
Raises float128 exception flags (IEEE 754 raiseFlags).
constexpr void shift_left128_inplace_safe(uint64_t &l, uint64_t &h, int shift) noexcept
Left shift a 128 bit integer (inplace). Handles any positive shift value.
std::ostream & operator<<(std::ostream &os, const fixed_point128< I > &value)
Writes a fixed_point128 to a stream, honouring its width, fill and adjustment.
constexpr float128 nexttoward(const float128 &x, const float128 &y) noexcept
constexpr float128 nextafter(const float128 &x, const float128 &y) noexcept
fixed_point128< I > acos(fixed_point128< I > x) noexcept
fixed_point128< I > pow(const fixed_point128< I > &x, const fixed_point128< I > &y) noexcept
float128 remainder(const float128 &x, const float128 &y) noexcept
float128 double_factorial(int x) noexcept
fixed_point128< I > tanh(const fixed_point128< I > &x) noexcept
fixed_point128< I > fabs(const fixed_point128< I > &x) noexcept
constexpr bool signbit(const float128 &x) noexcept
constexpr uint64_t log2_inv_n_table[][2]
1/(n*ln2) for n = 1 upwards, as a raw 128 bit value with 127 fraction bits; entry [i] holds 1/((i+1)*...
constexpr uint64_t shift_right128(uint64_t l, uint64_t h) noexcept
Right shift a 128 bit integer. When shift is a compile time constant, this function generates optimal...
float128 remquo(const float128 &x, const float128 &y, int *quo) noexcept
constexpr uint64_t log2_value_table[][2]
-log2 of the matching log2_recip_table entry, as a 128 bit fraction.
float128 erfc(float128 x) noexcept
fixed_point128< I > acosh(const fixed_point128< I > &x) noexcept
float128 lgamma(float128 x) noexcept
fixed_point128< I > trunc(const fixed_point128< I > &x) noexcept
fixed_point128< I > reciprocal(const fixed_point128< I > &x) noexcept
int fesetround(int round) noexcept
Selects the rounding direction float128 arithmetic uses on the calling thread.
fixed_point128< I > fmin(const fixed_point128< I > &x, const fixed_point128< I > &y) noexcept
fixed_point128< I > expm1(const fixed_point128< I > &x) noexcept
fixed_point128< I > atanh(const fixed_point128< I > &x) noexcept
constexpr float128 ldexp(float128 x, int exp) noexcept
fixed_point128< I > log(fixed_point128< I > x) noexcept
Bit level view of an IEEE 754 double-precision value.
constexpr uint64_t f() const noexcept
Mantissa (fraction) bits.
constexpr uint64_t s() const noexcept
Sign bit (0 = positive, 1 = negative).
constexpr uint64_t e() const noexcept
Biased exponent bits.
The per thread state behind the float128 environment.
int flags
Raised exceptions, as FE_ bits.
rounding mode
Rounding direction in effect.
A finite value rounded to a narrower binary format, see narrow().
bool to_inf
The value overflowed to infinity.
int32_t lsb
Weight of the significand's lowest bit, as a power of two.
uint64_t sig
Significand, below 2^P. Zero for a zero result.
uint32_t sign
Sign of the result.
size_t operator()(const fp128::float128 &value) const noexcept
128-bit unsigned integer type.