73#define FP128_VERSION_MAJOR 1
74#define FP128_VERSION_MINOR 0
75#define FP128_VERSION_PATCH 1
76#define FP128_VERSION_BUILD 0
79#define FP128_MAKE_VERSION(major, minor, patch, build) ((major) * 1000000 + (minor) * 10000 + (patch) * 100 + (build))
82#define FP128_VERSION FP128_MAKE_VERSION(FP128_VERSION_MAJOR, FP128_VERSION_MINOR, FP128_VERSION_PATCH, FP128_VERSION_BUILD)
86#define FP128_STRINGIFY_IMPL(x) #x
87#define FP128_STRINGIFY(x) FP128_STRINGIFY_IMPL(x)
90#define FP128_VERSION_STRING \
91 FP128_STRINGIFY(FP128_VERSION_MAJOR) \
92 "." FP128_STRINGIFY(FP128_VERSION_MINOR) "." FP128_STRINGIFY(FP128_VERSION_PATCH) "." FP128_STRINGIFY(FP128_VERSION_BUILD)
99#if defined(__GNUC__) || defined(__clang__)
101#elif defined(_MSC_VER)
107#if defined(FP128_CLANG) && (defined(__aarch64__) || defined(_M_ARM64))
115#if defined(FP128_CLANG) && (defined(__x86_64__) || defined(_M_X64))
120#ifndef FP128_DISABLE_INLINE
121#define FP128_DISABLE_INLINE 0
124#ifndef FP128_NO_INLINE
125#if defined(FP128_MSVC)
126#define FP128_NO_INLINE __declspec(noinline)
128#define FP128_NO_INLINE __attribute__((noinline))
162#if FP128_DISABLE_INLINE != 0
163#define FP128_INLINE FP128_NO_INLINE
164#define FP128_FORCE_INLINE FP128_NO_INLINE
167#if defined(FP128_MSVC)
168#define FP128_INLINE inline
169#define FP128_FORCE_INLINE __forceinline
171#define FP128_INLINE inline
172#define FP128_FORCE_INLINE inline __attribute__((always_inline))
193#if defined(FP128_MSVC)
194#define FP128_SUPPRESS_DEPRECATED_BEGIN __pragma(warning(push)) __pragma(warning(disable : 4996))
195#define FP128_SUPPRESS_DEPRECATED_END __pragma(warning(pop))
197#define FP128_SUPPRESS_DEPRECATED_BEGIN _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
198#define FP128_SUPPRESS_DEPRECATED_END _Pragma("GCC diagnostic pop")
251#ifndef FP128_USE_RECIPROCAL_FOR_DIVISION
252#define FP128_USE_RECIPROCAL_FOR_DIVISION 0
263#define FP128_ONE_SHIFT(x) (1ull << (x))
264#define FP128_MAX_VALUE_64(x) (UINT64_MAX >> (64 - (x)))
265#define FP128_GET_BIT(x, n) (((x) >> (n)) & 1)
266#define FP128_GET_BITS(x, b, count) (((x) >> (b)) & FP128_MAX_VALUE_64(count))
267#define FP128_INT_DIVIDE_BY_ZERO_EXCEPTION throw std::logic_error("Integer divide by zero!")
268#define FP128_FLOAT_DIVIDE_BY_ZERO_EXCEPTION throw std::logic_error("Floating point divide by zero!")
269#define FP128_NOT_IMPLEMENTED_EXCEPTION throw std::runtime_error("Not implemented!")
270#if defined _DEBUG || defined DEBUG
271#define FP128_ASSERT assert
272#define FP128_THROW_ONLY_IN_DEBUG
274#define FP128_ASSERT(x)
275#define FP128_THROW_ONLY_IN_DEBUG noexcept
286#if defined(FP128_MSVC)
287#pragma warning(disable : 4996)
290#include <immintrin.h>
292#define udiv64 _udiv64
293#define udiv128 _udiv128
294#define alloca _alloca
298#define FP128_SHIFTRIGHT128 fp128_shiftright128
299#define FP128_SHIFTLEFT128 fp128_shiftleft128
325FP128_FORCE_INLINE constexpr uint64_t fp128_shiftright128(uint64_t l, uint64_t h,
unsigned char shift)
noexcept
327 if (std::is_constant_evaluated()) {
328 const unsigned char s = shift & 63;
330 return (s == 0) ? l : (l >> s) | (h << (64 - s));
332 return __shiftright128(l, h, shift);
346FP128_FORCE_INLINE constexpr uint64_t fp128_shiftleft128(uint64_t l, uint64_t h,
unsigned char shift)
noexcept
348 if (std::is_constant_evaluated()) {
349 const unsigned char s = shift & 63;
350 return (s == 0) ? h : (h << s) | (l >> (64 - s));
352 return __shiftleft128(l, h, shift);
362 if (std::is_constant_evaluated()) {
363 return static_cast<uint32_t
>(std::countl_zero(x));
375 if (std::is_constant_evaluated()) {
376 return static_cast<uint64_t
>(std::countl_zero(x));
388 if (std::is_constant_evaluated()) {
389 return static_cast<uint32_t
>(std::popcount(x));
401 if (std::is_constant_evaluated()) {
402 return static_cast<uint64_t
>(std::popcount(x));
404 return __popcnt64(x);
418FP128_FORCE_INLINE constexpr uint64_t mulx_u64(uint64_t a, uint64_t b, uint64_t* hi)
noexcept
421 if (std::is_constant_evaluated()) {
422 const uint64_t a_lo = a & UINT32_MAX, a_hi = a >> 32;
423 const uint64_t b_lo = b & UINT32_MAX, b_hi = b >> 32;
424 const uint64_t p_ll = a_lo * b_lo;
425 const uint64_t p_lh = a_lo * b_hi;
426 const uint64_t p_hl = a_hi * b_lo;
427 const uint64_t p_hh = a_hi * b_hi;
430 const uint64_t mid = (p_ll >> 32) + (p_lh & UINT32_MAX) + (p_hl & UINT32_MAX);
431 *hi = p_hh + (p_lh >> 32) + (p_hl >> 32) + (mid >> 32);
432 return (mid << 32) | (p_ll & UINT32_MAX);
434 return _mulx_u64(a, b, hi);
445FP128_FORCE_INLINE constexpr unsigned char addcarryx_u64(
unsigned char c, uint64_t a, uint64_t b, uint64_t* out)
noexcept
448 if (std::is_constant_evaluated()) {
451 const uint64_t sum = a + b;
452 const uint64_t res = sum + c;
454 return static_cast<unsigned char>((sum < a) | (res < sum));
456 return _addcarryx_u64(c, a, b, out);
467FP128_FORCE_INLINE constexpr unsigned char subborrow_u64(
unsigned char b, uint64_t a, uint64_t c, uint64_t* out)
noexcept
470 if (std::is_constant_evaluated()) {
474 const uint64_t diff = a - c;
475 const uint64_t res = diff - b;
477 return static_cast<unsigned char>((diff > a) | (res > diff));
479 return _subborrow_u64(b, a, c, out);
485#elif defined(FP128_CLANG)
493#define FP128_SHIFTRIGHT128 shift_right128
494#define FP128_SHIFTLEFT128 shift_left128
496#if defined(FP128_ARM64)
499#include <immintrin.h>
515FP128_FORCE_INLINE constexpr uint32_t udiv64(uint64_t dividend, uint32_t divisor, uint32_t* remainder)
517#if defined(FP128_ARM64)
519 if (!std::is_constant_evaluated()) {
520 const uint64_t divisor64 = divisor;
521 uint64_t quot64 = 0, rem64 = 0;
524 __asm__(
"udiv %[q], %[n], %[d]\n\t"
525 "msub %[r], %[q], %[d], %[n]"
526 : [q]
"=&r"(quot64), [r]
"=r"(rem64)
527 : [n]
"r"(dividend), [d]
"r"(divisor64));
529 *
remainder =
static_cast<uint32_t
>(rem64);
531 return static_cast<uint32_t
>(quot64);
534 uint32_t quot = dividend / divisor;
569FP128_FORCE_INLINE constexpr uint64_t udiv128(uint64_t hi_dividend, uint64_t lo_dividend, uint64_t divisor, uint64_t* remainder)
571#if defined(FP128_X64)
573 if (!std::is_constant_evaluated()) {
574 uint64_t quot = 0, rem = 0;
581 __asm__(
"div %[d]" :
"=a"(quot),
"=d"(rem) : [d]
"r"(divisor),
"a"(lo_dividend),
"d"(hi_dividend) :
"cc");
588 __uint128_t dividend = (
static_cast<__uint128_t
>(hi_dividend) << 64) | lo_dividend;
589 uint64_t quot =
static_cast<uint64_t
>(dividend / divisor);
591 *
remainder =
static_cast<uint64_t
>(dividend % divisor);
607FP128_FORCE_INLINE static constexpr uint64_t mulx_u64(uint64_t a, uint64_t b, uint64_t* hi)
noexcept
610#if defined(FP128_ARM64)
612 if (!std::is_constant_evaluated()) {
613 uint64_t lo_res = 0, hi_res = 0;
614 __asm__(
"umulh %[hi], %[a], %[b]\n\t"
615 "mul %[lo], %[a], %[b]"
616 : [hi]
"=&r"(hi_res), [lo]
"=r"(lo_res)
617 : [a]
"r"(a), [b]
"r"(b));
622 __uint128_t r = (__uint128_t)a * b;
623 *hi = (uint64_t)(r >> 64);
640FP128_FORCE_INLINE static constexpr unsigned char addcarryx_u64(
unsigned char c, uint64_t a, uint64_t b, uint64_t* out)
noexcept
643#if defined(FP128_ARM64)
645 if (!std::is_constant_evaluated()) {
647 uint32_t carry_out = 0;
648 __asm__(
"cmp %w[cin], #1\n\t"
649 "adcs %[sum], %[a], %[b]\n\t"
651 : [sum]
"=&r"(sum), [cout]
"=&r"(carry_out)
652 : [cin]
"r"((uint32_t)c), [a]
"r"(a), [b]
"r"(b)
655 return (
unsigned char)carry_out;
658 __uint128_t r = (__uint128_t)a + b + c;
660 return (
unsigned char)(r >> 64);
684FP128_FORCE_INLINE static constexpr unsigned char subborrow_u64(
unsigned char b, uint64_t a, uint64_t c, uint64_t* out)
noexcept
687#if !defined(FP128_ARM64)
689 if (!std::is_constant_evaluated()) {
693 unsigned long long diff = 0;
694 const unsigned char borrow = _subborrow_u64(b, a, c, &diff);
695 *out =
static_cast<uint64_t
>(diff);
700 __uint128_t r = (__uint128_t)a - c - b;
703 return (
unsigned char)((r >> 64) & 1);
714#if defined(FP128_ARM64)
716 if (!std::is_constant_evaluated()) {
718 __asm__(
"clz %w[res], %w[val]" : [res]
"=r"(res) : [val]
"r"(x));
722 return (x == 0) ? 32u : (uint32_t)__builtin_clz(x);
732#if defined(FP128_ARM64)
734 if (!std::is_constant_evaluated()) {
736 __asm__(
"clz %[res], %[val]" : [res]
"=r"(res) : [val]
"r"(x));
740 return (x == 0) ? 64u : (uint64_t)__builtin_clzll(x);
752#if defined(FP128_ARM64)
754 if (!std::is_constant_evaluated()) {
757 __asm__(
"fmov %d[tmp], %[val]\n\t"
758 "cnt %[tmp].8b, %[tmp].8b\n\t"
759 "addv %b[tmp], %[tmp].8b\n\t"
760 "fmov %w[res], %s[tmp]"
761 : [res]
"=r"(res), [tmp]
"=&w"(tmp)
766 return (uint64_t)__builtin_popcountll(x);
777#if defined(FP128_ARM64)
779 if (!std::is_constant_evaluated()) {
782 __asm__(
"fmov %s[tmp], %w[val]\n\t"
783 "cnt %[tmp].8b, %[tmp].8b\n\t"
784 "addv %b[tmp], %[tmp].8b\n\t"
785 "fmov %w[res], %s[tmp]"
786 : [res]
"=r"(res), [tmp]
"=&w"(tmp)
791 return (uint32_t)__builtin_popcount(x);
797#if defined(FP128_MSVC)
798#define FP128_ALIGN(_a) __declspec(align(_a))
802#elif defined(FP128_CLANG)
803#define FP128_ALIGN(_a) __attribute__((aligned(_a)))
806#define FP128_ALIGN16 FP128_ALIGN(16)
873 constexpr Double(
double v = 0) noexcept :
bits(
std::bit_cast<uint64_t>(v)) {}
876 [[nodiscard]]
constexpr uint64_t
f() const noexcept {
return bits & FRAC_MASK; }
880 [[nodiscard]]
constexpr uint64_t
s() const noexcept {
return bits >> SIGN_SHIFT; }
882 [[nodiscard]]
constexpr double val() const noexcept {
return std::bit_cast<double>(
bits); }
885 constexpr void set_f(uint64_t v)
noexcept {
bits = (
bits & ~FRAC_MASK) | (v & FRAC_MASK); }
889 constexpr void set_s(uint64_t v)
noexcept {
bits = (
bits & ~(1ull << SIGN_SHIFT)) | ((v & 1) << SIGN_SHIFT); }
903 [[nodiscard]]
static constexpr double make(uint64_t
s, uint64_t
e, uint64_t
f)
noexcept
905 return std::bit_cast<double>(((
s & 1) << SIGN_SHIFT) | ((
e & EXP_MASK) <<
dbl_frac_bits) | (
f & FRAC_MASK));
909 static constexpr uint64_t FRAC_MASK = (1ull <<
dbl_frac_bits) - 1;
910 static constexpr uint64_t EXP_MASK = (1ull <<
dbl_exp_bits) - 1;
913static_assert(
sizeof(Double) ==
sizeof(double),
"The Double view should have the same size as a double variable!");
925 constexpr Float(
float v = 0) noexcept :
bits(
std::bit_cast<uint32_t>(v)) {}
928 [[nodiscard]]
constexpr uint32_t
f() const noexcept {
return bits & FRAC_MASK; }
932 [[nodiscard]]
constexpr uint32_t
s() const noexcept {
return bits >> SIGN_SHIFT; }
934 [[nodiscard]]
constexpr float val() const noexcept {
return std::bit_cast<float>(
bits); }
937 constexpr void set_f(uint32_t v)
noexcept {
bits = (
bits & ~FRAC_MASK) | (v & FRAC_MASK); }
941 constexpr void set_s(uint32_t v)
noexcept {
bits = (
bits & ~(1u << SIGN_SHIFT)) | ((v & 1) << SIGN_SHIFT); }
945 [[nodiscard]]
static constexpr float make(uint32_t
s, uint32_t
e, uint32_t
f)
noexcept
947 return std::bit_cast<float>(((
s & 1) << SIGN_SHIFT) | ((
e & EXP_MASK) <<
flt_frac_bits) | (
f & FRAC_MASK));
951 static constexpr uint32_t FRAC_MASK = (1u <<
flt_frac_bits) - 1;
952 static constexpr uint32_t EXP_MASK = (1u <<
flt_exp_bits) - 1;
955static_assert(
sizeof(Float) ==
sizeof(float),
"The Float view should have the same size as a float variable!");
970 if (src ==
nullptr || dest ==
nullptr)
973 for (; i < n && src[i] !=
'\0'; ++i) {
974 dest[i] =
static_cast<char>(::tolower(
static_cast<unsigned char>(src[i])));
990template <
typename T>[[nodiscard]]
constexpr uint32_t
array_length(
const T& a)
992 static_assert(
sizeof(a[0]) != 0,
"Requires an array of non-zero sized elements!");
993 return sizeof(a) /
sizeof(a[0]);
1007 x += 1ull << (shift - 1);
1021 l = (l >> shift) | (h << (64 - shift));
1034 h = (h << shift) | (l >> (64 - shift));
1086#define FP128_ADD_ROUND_BIT(l, h, round_up) ((h) += addcarryx_u64(0, (l), static_cast<uint64_t>(round_up), &(l)))
1109 uint64_t round_bit = 0;
1110 switch (shift >> 6) {
1112 round_bit = (l >> (shift - 1)) & 1;
1113 l = (l >> shift) | (h << (64 - shift));
1117 round_bit = (shift == 64) ? (l >> 63) : ((h >> (shift - 65)) & 1);
1118 l = h >> (shift - 64);
1122 round_bit = (shift == 128) ? (h >> 63) : 0;
1128 h +=
static_cast<uint64_t
>(l < round_bit);
1150 const int64_t value =
static_cast<int64_t
>(h);
1151 const uint64_t sign_bits =
static_cast<uint64_t
>(value >> 63);
1155 return (shift == 128 && h == (1ull << 63) && l == 0) ? UINT64_MAX : 0;
1158 const int32_t s = shift - 64;
1160 const uint64_t round_bit = (((h << 1) | (l >> 63)) >> s) & 1;
1163 const bool sticky = ((l << static_cast<int32_t>(s == 0)) | ((h << 2) << (63 - s))) != 0;
1164 const uint64_t round_up = round_bit & (
static_cast<uint64_t
>(sticky) | (~sign_bits & 1));
1165 return static_cast<uint64_t
>(value >> s) + round_up;
1194 const uint64_t sign =
static_cast<uint64_t
>(
static_cast<int64_t
>(h) >> 63);
1196 h = (l == 0) ? 0 : sign;
1200 const uint64_t sign_bits =
static_cast<uint64_t
>(
static_cast<int64_t
>(h) >> 63);
1201 const uint64_t round_bit = (l >> (shift - 1)) & 1;
1204 const bool sticky = ((l << 1) << (64 - shift)) != 0;
1205 l = (l >> shift) | (h << (64 - shift));
1206 h =
static_cast<uint64_t
>(
static_cast<int64_t
>(h) >> shift);
1210 const uint64_t round_up = round_bit & (
static_cast<uint64_t
>(sticky) | (~sign_bits & 1));
1212 h +=
static_cast<uint64_t
>(l < round_up);
1227 switch (shift >> 6) {
1229 h = (h << shift) | (l >> (64 - shift));
1233 h = l << (shift - 64);
1250 if constexpr (shift == 0) {
1252 }
else if constexpr (shift < 64) {
1259 return (l >> shift) | (h << (64 - shift));
1260 }
else if constexpr (shift < 128) {
1261 return h >> (shift - 64);
1276 if constexpr (shift == 0) {
1278 }
else if constexpr (shift < 64) {
1279 return (h << shift) | (l >> (64 - shift));
1280 }
else if constexpr (shift < 128) {
1281 return l << (shift - 64);
1296 switch (shift >> 6) {
1301 return (l >> shift) | ((h << 1) << (63 - shift));
1303 return h >> (shift ^ 64);
1330 switch (shift >> 6) {
1333 return (h << shift) | ((l >> 1) >> (63 - shift));
1335 return l << (shift - 64);
1361 if (u ==
nullptr || q ==
nullptr)
1363 uint64_t dummy_reminder {};
1365 r = &dummy_reminder;
1371 while (m > 0 && u[m - 1] == 0)
1376 if (m == 0 || u[0] < v) {
1377 *r = (m == 0) ? 0 : u[0];
1388 for (
auto j = m - 1; j >= 0; --j) {
1390 q[j] = udiv128(k[1], k[0], v, &k[1]);
1435template <
int64_t m>
FP128_INLINE static bool div_128bit(uint64_t* q, uint64_t* r,
const uint64_t* u,
const uint64_t* v)
noexcept
1437 constexpr int64_t MAX_WORDS = 4;
1438 static_assert(m >= 2 && m <= MAX_WORDS,
"div_128bit divides a numerator of 2 to 4 words");
1439 if (q ==
nullptr || u ==
nullptr || v ==
nullptr || v[1] == 0)
1444 const int32_t s =
static_cast<int32_t
>(lzcnt64(v[1]));
1445 const uint64_t vn1 = (s != 0) ? ((v[1] << s) | (v[0] >> (64 - s))) : v[1];
1446 const uint64_t vn0 = v[0] << s;
1448 uint64_t un[MAX_WORDS + 1] {};
1449 un[m] = (s != 0) ? (u[m - 1] >> (64 - s)) : 0;
1450 for (int64_t i = m - 1; i > 0; --i) {
1451 un[i] = (s != 0) ? ((u[i] << s) | (u[i - 1] >> (64 - s))) : u[i];
1455 for (int64_t j = m - 2; j >= 0; --j) {
1456 uint64_t qhat, rhat;
1457 bool correct =
true;
1458 if (un[j + 2] >= vn1) {
1462 rhat = un[j + 1] + vn1;
1463 correct = (rhat >= vn1);
1465 qhat = udiv128(un[j + 2], un[j + 1], vn1, &rhat);
1471 const uint64_t plo = mulx_u64(qhat, vn0, &phi);
1472 if (phi < rhat || (phi == rhat && plo <= un[j])) {
1476 const uint64_t next = rhat + vn1;
1484 uint64_t p0hi, p1hi, product1;
1485 const uint64_t p0 = mulx_u64(qhat, vn0, &p0hi);
1486 const uint64_t p1 = mulx_u64(qhat, vn1, &p1hi);
1487 const uint64_t product2 = p1hi + addcarryx_u64(0, p1, p0hi, &product1);
1489 unsigned char borrow = subborrow_u64(0, un[j], p0, &un[j]);
1490 borrow = subborrow_u64(borrow, un[j + 1], product1, &un[j + 1]);
1491 borrow = subborrow_u64(borrow, un[j + 2], product2, &un[j + 2]);
1499 unsigned char carry = addcarryx_u64(0, un[j], vn0, &un[j]);
1500 carry = addcarryx_u64(carry, un[j + 1], vn1, &un[j + 1]);
1506 r[0] = (s != 0) ? ((un[0] >> s) | (un[1] << (64 - s))) : un[0];
1547 return popcnt64(l) + popcnt64(h);
1557 return (h != 0) ? lzcnt64(h) : 64 + lzcnt64(l);
1568 return (h != 0 || l != 0) ? 127 -
lzcnt128(l, h) : 0;
1578 return (x) ? 63ull - lzcnt64(x) : 0;
1588 return (x) ? 31ull - lzcnt32(x) : 0;
1610FP128_INLINE 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
1612 uint64_t ll_high = 0, lh_high = 0, hl_high = 0, hh_high = 0;
1613 [[maybe_unused]]
const uint64_t ll_low = mulx_u64(a_low, b_low, &ll_high);
1614 const uint64_t lh_low = mulx_u64(a_low, b_high, &lh_high);
1615 const uint64_t hl_low = mulx_u64(a_high, b_low, &hl_high);
1616 const uint64_t hh_low = mulx_u64(a_high, b_high, &hh_high);
1621 uint8_t carry = addcarryx_u64(0, ll_high, lh_low, &mid);
1622 uint64_t mid_carries = carry;
1623 carry = addcarryx_u64(0, mid, hl_low, &mid);
1624 mid_carries += carry;
1627 uint64_t low = 0, high = 0;
1628 carry = addcarryx_u64(0, hh_low, lh_high, &low);
1629 high = hh_high + carry;
1630 carry = addcarryx_u64(0, low, hl_high, &low);
1632 carry = addcarryx_u64(0, low, mid_carries, &low);
1638 carry = addcarryx_u64(0, low, mid >> 63, &low);
1653 {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull},
1654 {0xFC0FC0FC0FC0FC0Full, 0xC0FC0FC0FC0FC0FCull},
1655 {0xF83E0F83E0F83E0Full, 0x83E0F83E0F83E0F8ull},
1656 {0xF4898D5F85BB3950ull, 0x3D226357E16ECE54ull},
1657 {0xF0F0F0F0F0F0F0F0ull, 0xF0F0F0F0F0F0F0F1ull},
1658 {0xED7303B5CC0ED730ull, 0x3B5CC0ED7303B5CCull},
1659 {0xEA0EA0EA0EA0EA0Eull, 0xA0EA0EA0EA0EA0EAull},
1660 {0xE6C2B4481CD85689ull, 0x039B0AD12073615Aull},
1661 {0xE38E38E38E38E38Eull, 0x38E38E38E38E38E4ull},
1662 {0xE070381C0E070381ull, 0xC0E070381C0E0704ull},
1663 {0xDD67C8A60DD67C8Aull, 0x60DD67C8A60DD67Dull},
1664 {0xDA740DA740DA740Dull, 0xA740DA740DA740DAull},
1665 {0xD79435E50D79435Eull, 0x50D79435E50D7943ull},
1666 {0xD4C77B03531DEC0Dull, 0x4C77B03531DEC0D5ull},
1667 {0xD20D20D20D20D20Dull, 0x20D20D20D20D20D2ull},
1668 {0xCF6474A8819EC8E9ull, 0x51033D91D2A2067Bull},
1669 {0xCCCCCCCCCCCCCCCCull, 0xCCCCCCCCCCCCCCCDull},
1670 {0xCA4587E6B74F0329ull, 0x161F9ADD3C0CA458ull},
1671 {0xC7CE0C7CE0C7CE0Cull, 0x7CE0C7CE0C7CE0C8ull},
1672 {0xC565C87B5F9D4D1Bull, 0xC2503159721ED7E7ull},
1673 {0xC30C30C30C30C30Cull, 0x30C30C30C30C30C3ull},
1674 {0xC0C0C0C0C0C0C0C0ull, 0xC0C0C0C0C0C0C0C1ull},
1675 {0xBE82FA0BE82FA0BEull, 0x82FA0BE82FA0BE83ull},
1676 {0xBC52640BC52640BCull, 0x52640BC52640BC52ull},
1677 {0xBA2E8BA2E8BA2E8Bull, 0xA2E8BA2E8BA2E8BAull},
1678 {0xB81702E05C0B8170ull, 0x2E05C0B81702E05Cull},
1679 {0xB60B60B60B60B60Bull, 0x60B60B60B60B60B6ull},
1680 {0xB40B40B40B40B40Bull, 0x40B40B40B40B40B4ull},
1681 {0xB21642C8590B2164ull, 0x2C8590B21642C859ull},
1682 {0xB02C0B02C0B02C0Bull, 0x02C0B02C0B02C0B0ull},
1683 {0xAE4C415C9882B931ull, 0x0572620AE4C415CAull},
1684 {0xAC7691840AC76918ull, 0x40AC7691840AC769ull},
1685 {0xAAAAAAAAAAAAAAAAull, 0xAAAAAAAAAAAAAAABull},
1686 {0xA8E83F5717C0A8E8ull, 0x3F5717C0A8E83F57ull},
1687 {0xA72F05397829CBC1ull, 0x4E5E0A72F0539783ull},
1688 {0xA57EB50295FAD40Aull, 0x57EB50295FAD40A5ull},
1689 {0xA3D70A3D70A3D70Aull, 0x3D70A3D70A3D70A4ull},
1690 {0xA237C32B16CFD772ull, 0x0F353A4C0A237C33ull},
1691 {0xA0A0A0A0A0A0A0A0ull, 0xA0A0A0A0A0A0A0A1ull},
1692 {0x9F1165E7254813E2ull, 0x2CBCE4A9027C4598ull},
1693 {0x9D89D89D89D89D89ull, 0xD89D89D89D89D89Eull},
1694 {0x9C09C09C09C09C09ull, 0xC09C09C09C09C09Cull},
1695 {0x9A90E7D95BC609A9ull, 0x0E7D95BC609A90E8ull},
1696 {0x991F1A515885FB37ull, 0x072D753BD02647C7ull},
1697 {0x97B425ED097B425Eull, 0xD097B425ED097B42ull},
1698 {0x964FDA6C0964FDA6ull, 0xC0964FDA6C0964FEull},
1699 {0x94F2094F2094F209ull, 0x4F2094F2094F2095ull},
1700 {0x939A85C40939A85Cull, 0x40939A85C40939A8ull},
1701 {0x9249249249249249ull, 0x2492492492492492ull},
1702 {0x90FDBC090FDBC090ull, 0xFDBC090FDBC090FEull},
1703 {0x8FB823EE08FB823Eull, 0xE08FB823EE08FB82ull},
1704 {0x8E78356D1408E783ull, 0x56D1408E78356D14ull},
1705 {0x8D3DCB08D3DCB08Dull, 0x3DCB08D3DCB08D3Eull},
1706 {0x8C08C08C08C08C08ull, 0xC08C08C08C08C08Cull},
1707 {0x8AD8F2FBA9386822ull, 0xB63CBEEA4E1A08AEull},
1708 {0x89AE4089AE4089AEull, 0x4089AE4089AE408Aull},
1709 {0x8888888888888888ull, 0x8888888888888889ull},
1710 {0x8767AB5F34E47EF1ull, 0x30A9419637021D9Full},
1711 {0x864B8A7DE6D1D608ull, 0x64B8A7DE6D1D6086ull},
1712 {0x8534085340853408ull, 0x5340853408534085ull},
1713 {0x8421084210842108ull, 0x4210842108421084ull},
1714 {0x83126E978D4FDF3Bull, 0x645A1CAC083126E9ull},
1715 {0x8208208208208208ull, 0x2082082082082082ull},
1716 {0x8102040810204081ull, 0x0204081020408102ull},
1727 {0x0000000000000000ull, 0x0000000000000001ull},
1728 {0x05B9E5A170B48A62ull, 0x9B89F8846042BE52ull},
1729 {0x0B5D69BAC77EC398ull, 0x9B03784B5BE08491ull},
1730 {0x10EB389FA29F9AB3ull, 0xCF74BAB999217067ull},
1731 {0x1663F6FAC913167Cull, 0xCC53826144575AC4ull},
1732 {0x1BC84240ADABBA63ull, 0xB2C5A6E5197AB879ull},
1733 {0x2118B119B4F3C72Cull, 0x4F78DFA14AA5157Bull},
1734 {0x2655D3C4F15C343Eull, 0xA3E580EB4E974C9Bull},
1735 {0x2B803473F7AD0F3Full, 0x401624140D175BA2ull},
1736 {0x309857A05E0765FBull, 0xA4491DCEC752AE1Eull},
1737 {0x359EBC5B69D927DFull, 0xC23D9780306C6969ull},
1738 {0x3A93DC9864B2DF91ull, 0xE96ACA04740A8839ull},
1739 {0x3F782D7204D01447ull, 0x51B3314F09DE6BE5ull},
1740 {0x444C1F6B4C2DD72Cull, 0x25C169E5693A7F06ull},
1741 {0x49101EAC381CE609ull, 0x16E52E91300EFEEFull},
1742 {0x4DC4933A9337B366ull, 0x44CDB2581FB9186Full},
1743 {0x5269E12F346E2BF9ull, 0x24AFDBFD36BF6D33ull},
1744 {0x570068E7EF5A1E7Eull, 0x802C48281A2EB745ull},
1745 {0x5B8887367433795Eull, 0x35482D13DC0F110Cull},
1746 {0x6002958C587150CAull, 0xBAD827D37DEB2237ull},
1747 {0x646EEA247C5C22D2ull, 0xCAD415AE1A715618ull},
1748 {0x68CDD829FD814275ull, 0xF1035E5E7B16C7F7ull},
1749 {0x6D1FAFDCE20A8290ull, 0x51BBE3F6289E3AB7ull},
1750 {0x7164BEB4A56D59F9ull, 0xFB952BBBCCC314F1ull},
1751 {0x759D4F80CBA83BF8ull, 0xFAF866415554D6C0ull},
1752 {0x79C9AA879D534831ull, 0x46784BD1C44CCD5Full},
1753 {0x7DEA15A32C1B3B38ull, 0x64C6001143D6C8D6ull},
1754 {0x81FED45CBCCBF99Cull, 0xA1A3202B3D68F965ull},
1755 {0x86082806B1D532C4ull, 0x12BA94DB12EF0AA8ull},
1756 {0x8A064FD50F2A1CF0ull, 0xAD29518B0252C226ull},
1757 {0x8DF988F4AE806F1Dull, 0xA89D4EE66C3700E3ull},
1758 {0x91E20EA1393E4040ull, 0x76630D4C409DD918ull},
1759 {0x95C01A39FBD6879Full, 0xA00B120A068BADD0ull},
1760 {0x9993E355A4E53643ull, 0x5C902FD21101093Aull},
1761 {0x9D5D9FD5010B3666ull, 0x5592074827CB508Eull},
1762 {0xA11D83F4C3554B38ull, 0x3B0E8A55626C3263ull},
1763 {0xA4D3C25E68DC57F2ull, 0x495FB7FA6D7EDA66ull},
1764 {0xA8808C384547C6EFull, 0x4A49BC591348F145ull},
1765 {0xAC241134C4E99E1Cull, 0x6C5E946B4AE30894ull},
1766 {0xAFBE7FA0F04D75C6ull, 0x58D602E66B04D3B5ull},
1767 {0xB35004723C465E69ull, 0x76DA1C872983511Dull},
1768 {0xB6D8CB53B0CA4ECBull, 0xEF83F1AB5130C34Cull},
1769 {0xBA58FEB2703A9E37ull, 0x2BC1FE8A8648E9EBull},
1770 {0xBDD0C7C9A817204Full, 0x55BBF90CE3F6815Aull},
1771 {0xC1404EADF38396DEull, 0xE021361E13A30974ull},
1772 {0xC4A7BA58377C5A03ull, 0x75163EC8D56242F8ull},
1773 {0xC80730B0001667F2ull, 0x1FA8423E8C1443F3ull},
1774 {0xCB5ED69565AFAF7Full, 0x6248A98A36F8173Cull},
1775 {0xCEAECFEA80859B33ull, 0x2AC903A413E5A848ull},
1776 {0xD1F73F9C70C0F683ull, 0xCC68D510B4A2B099ull},
1777 {0xD53847AC00A69BE6ull, 0xF1BE4359106A19B6ull},
1778 {0xD8720935E6435EBDull, 0x376A70D849AE77DCull},
1779 {0xDBA4A47AA996D25Aull, 0x5B8A19B1C637671Eull},
1780 {0xDED038E633F36DA8ull, 0xB6F0409B369AACC0ull},
1781 {0xE1F4E5170D02A99Bull, 0x4C5A724DBD8180F7ull},
1782 {0xE512C6E54998B1AFull, 0xF71C8605583D030Aull},
1783 {0xE829FB693044B398ull, 0xC4BAEE073D4B1B03ull},
1784 {0xEB3A9F01975077F1ull, 0xF5F0CC82AAA9AD7Eull},
1785 {0xEE44CD59FFAB62F3ull, 0x39D5D6A218C633A0ull},
1786 {0xF148A170700A00FDull, 0xD5533F1DE29ABEDEull},
1787 {0xF446359B13539551ull, 0x0D1E3F80FBC71454ull},
1788 {0xF73DA38D9D4A83EBull, 0x6E0F93F7A43E479Cull},
1789 {0xFA2F045E7832AA72ull, 0x6ADF27B820FD03EAull},
1790 {0xFD1A708BBE119B14ull, 0x945CF6BA73D491EAull},
1817 {0xB8AA3B295C17F0BBull, 0xBE87FED0691D3E89ull},
1818 {0x5C551D94AE0BF85Dull, 0xDF43FF68348E9F44ull},
1819 {0x3D8E13B87407FAE9ull, 0x3F82AA45785F14D8ull},
1820 {0x2E2A8ECA5705FC2Eull, 0xEFA1FFB41A474FA2ull},
1821 {0x24EED8A1DF37FCF2ull, 0x594E6629AE9F72E8ull},
1822 {0x1EC709DC3A03FD74ull, 0x9FC15522BC2F8A6Cull},
1823 {0x1A61762A7ADED93Full, 0x645C921DC5DF9B38ull},
1824 {0x171547652B82FE17ull, 0x77D0FFDA0D23A7D1ull},
1825 {0x1484B13D7C02A8F8ull, 0x6A80E36C7D7506F3ull},
1826 {0x12776C50EF9BFE79ull, 0x2CA73314D74FB974ull},
1827 {0x10C9A84994022D28ull, 0x5723A2CD20D41CF5ull},
1828 {0x0F6384EE1D01FEBAull, 0x4FE0AA915E17C536ull},
1829 {0x0E347AB4698BB00Eull, 0x711E274B1BC72C32ull},
1830 {0x0D30BB153D6F6C9Full, 0xB22E490EE2EFCD9Cull},
1831 {0x0C4F9D8B4A67FEFBull, 0x731A220DE4DFD0F8ull},
1832 {0x0B8AA3B295C17F0Bull, 0xBBE87FED0691D3E9ull},
1833 {0x0ADCD64DBA1F86A1ull, 0xA1CBC3B1E810C771ull},
1834 {0x0A42589EBE01547Cull, 0x354071B63EBA8379ull},
1835 {0x09B81E0FA687FF32ull, 0x4D65793363D91E3Dull},
1836 {0x093BB62877CDFF3Cull, 0x9653998A6BA7DCBAull},
1837 {0x08CB27637E4A486Aull, 0x76C98609EC9FDE68ull},
1838 {0x0864D424CA011694ull, 0x2B91D166906A0E7Bull},
1839 {0x080766BF04010A77ull, 0x77969BC6475A50A2ull},
1840 {0x07B1C2770E80FF5Dull, 0x27F05548AF0BE29Bull},
#define FP128_VERSION_BUILD
Rebuild of an unchanged source tree.
static constexpr bool FP128_CPP_STYLE_MODULO
Use C++ modulo semantics (false = Python-style).
#define FP128_VERSION_MAJOR
Breaking changes to the public interface.
#define FP128_VERSION
This library's version as a single comparable integer, e.g. 90000 for 0.9.0.0.
#define FP128_FORCE_INLINE
Marks a function the compiler must inline rather than merely may.
#define FP128_VERSION_MINOR
Backwards compatible additions.
#define FP128_VERSION_PATCH
Fixes that change neither.
#define FP128_VERSION_STRING
This library's version as a dotted string literal, e.g. "0.9.0.0".
Everything the library provides.
constexpr void shift_right128_inplace_safe(uint64_t &l, uint64_t &h, int shift) noexcept
Right shift of an unsigned 128 bit integer (inplace), rounded to nearest with a tie going up.
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 = ...
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.
constexpr void shift_right128_inplace(uint64_t &l, uint64_t &h, int shift) noexcept
Right shift a 128 bit unsigned integer (inplace). Limited range, inplace and no parameter checks (che...
static bool div_64bit(uint64_t *q, uint64_t *r, const uint64_t *u, uint64_t v, int64_t m) noexcept
64 bit words unsigned divide function. Variation of the code from the book Hacker's Delight.
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 uint64_t shift_right128_round(uint64_t l, uint64_t h, int shift) noexcept
Right shift a 128 bit integer with rounding.
constexpr void twos_complement128(uint64_t &l, uint64_t &h) noexcept
converts a 128 integer to negative via 2's complement.
uint64_t lzcnt128(const fixed_point128< I > &x) noexcept
constexpr uint64_t shift_left128(uint64_t l, uint64_t h) noexcept
Left shift a 128 bit integer. When shift is a compile time constant, this function generates optimal ...
constexpr void shift_left128_inplace(uint64_t &l, uint64_t &h, int shift) noexcept
Left shift a 128 bit integer (inplace). Limited range, inplace and no parameter checks (checked via a...
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,...
static constexpr int32_t flt_exp_bits
Exponent bit count of an IEEE 754 float.
static constexpr int32_t flt_frac_bits
Mantissa bit count of an IEEE 754 float.
static constexpr int32_t version_patch
Fixes that change neither.
constexpr uint64_t log2_recip_table[][2]
1/(1 + j/64) as a 128 bit fraction, the reciprocals fixed_point128::log2() reduces with.
static constexpr const char * version_string
Dotted form, e.g. "0.9.0.0".
static errno_t strnlwr(char *dest, const char *src, size_t n) noexcept
Copy a string to lowercase, up to n characters.
constexpr uint64_t shift_right128_wide_signed_low(uint64_t l, uint64_t h, int shift) noexcept
The shifts by 64 bits or more of shift_right128_inplace_safe_signed().
constexpr void shift_right128_inplace_safe_signed(uint64_t &l, uint64_t &h, int shift) noexcept
Arithmetic right shift of a 128 bit two's complement value (inplace), rounded to nearest with a tie g...
static constexpr int32_t version_major
Breaking changes to the public interface.
constexpr uint64_t shift_right64_round(uint64_t x, int shift) noexcept
Right-shift a 64-bit value with rounding.
static constexpr int32_t dbl_frac_bits
Mantissa bit count of an IEEE 754 double.
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.
static constexpr int32_t version_build
Rebuild of an unchanged source tree.
float128 remainder(const float128 &x, const float128 &y) 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...
static constexpr int32_t version_minor
Backwards compatible additions.
constexpr uint64_t log2_value_table[][2]
-log2 of the matching log2_recip_table entry, as a 128 bit fraction.
static constexpr int32_t dbl_exp_bits
Exponent bit count of an IEEE 754 double.
constexpr uint64_t popcnt128(uint64_t l, uint64_t h) noexcept
Counts the number of 1 bits (population count) in a 128-bit unsigned integer.
static constexpr int32_t version
All four packed into one comparable integer.
constexpr void set_s(uint64_t v) noexcept
Sets the sign bit.
constexpr uint64_t f() const noexcept
Mantissa (fraction) bits.
uint64_t bits
The raw bit pattern of the double.
constexpr uint64_t s() const noexcept
Sign bit (0 = positive, 1 = negative).
constexpr double val() const noexcept
The double these bits encode.
constexpr void set_e(uint64_t v) noexcept
Sets the biased exponent. Bits above the field width are dropped.
constexpr void set_f(uint64_t v) noexcept
Sets the mantissa. Bits above the field width are dropped, as a bit-field assignment would.
static constexpr double make(uint64_t s, uint64_t e, uint64_t f) noexcept
Assembles a double out of its three fields.
constexpr Double(double v=0) noexcept
Constructs from a double, or from positive zero when no value is given.
constexpr uint64_t e() const noexcept
Biased exponent bits.
constexpr void set_s(uint32_t v) noexcept
Sets the sign bit.
constexpr uint32_t f() const noexcept
Mantissa (fraction) bits.
constexpr void set_e(uint32_t v) noexcept
Sets the biased exponent. Bits above the field width are dropped.
constexpr void set_f(uint32_t v) noexcept
Sets the mantissa. Bits above the field width are dropped, as a bit-field assignment would.
uint32_t bits
The raw bit pattern of the float.
static constexpr float make(uint32_t s, uint32_t e, uint32_t f) noexcept
Assembles a float out of its three fields, see Double::make().
constexpr uint32_t e() const noexcept
Biased exponent bits.
constexpr float val() const noexcept
The float these bits encode.
constexpr uint32_t s() const noexcept
Sign bit (0 = positive, 1 = negative).
constexpr Float(float v=0) noexcept
Constructs from a float, or from positive zero when no value is given.