44#ifndef FP128_INT128_SHARED_H
45#define FP128_INT128_SHARED_H
120 static_assert(
sizeof(
void*) == 8,
"int128_base is supported in 64 bit builds only!");
138 template <
typename T> [[nodiscard]]
static constexpr uint64_t HighFromIntegral(T x)
noexcept
140 if constexpr (std::is_signed_v<T>) {
141 return (x < T(0)) ? UINT64_MAX : 0;
156 [[nodiscard]]
FP128_FORCE_INLINE constexpr bool MagnitudeGreaterThan(
const int128_base& rhs)
const noexcept
158 return high > rhs.high || (high == rhs.high && low > rhs.low);
177 [[nodiscard]]
FP128_INLINE constexpr uint64_t RoundedMantissa(uint64_t expo, int32_t frac_bits)
const noexcept
179 const int32_t shift =
static_cast<int32_t
>(expo) - frac_bits;
192 }
else if (shift <= 64) {
193 sticky = (low << (65 - shift)) != 0;
194 }
else if (shift == 65) {
197 sticky = low != 0 || (high << (129 - shift)) != 0;
201 if (sticky || (mant & 1))
223 template <
typename F> [[nodiscard]]
FP128_INLINE constexpr int32_t CompareFloating(F x)
const noexcept
225 const double d =
static_cast<double>(x);
231 constexpr double two_64 = 18446744073709551616.0;
232 if constexpr (is_signed) {
233 constexpr double two_127 = two_64 * 9223372036854775808.0;
239 if (d >= two_64 * two_64)
246 const int128_base t(d);
248 return (*
this < t) ? -1 : 1;
251 const double td =
static_cast<double>(t);
255 return (d > td) ? -1 : 1;
323 const int32_t e =
static_cast<int32_t
>(d.
e()) - 1023;
331 high = 0x8000000000000000ull - 1;
334 high = 0x8000000000000000ull;
341 high = low = UINT64_MAX;
355 if (bits_to_shift > 0) {
356 *
this <<= bits_to_shift;
360 *
this >>= -bits_to_shift;
388 template <
typename T>
389 requires std::is_integral_v<T>
414 const auto x_len = 1 + strlen(x);
417 auto str_ptr = std::unique_ptr<char[]>(
new (std::nothrow)
char[x_len]);
418 char* p = str_ptr.get();
426 while (*p && isspace(
static_cast<unsigned char>(*p)))
431 bool negative =
false;
432 if (*p ==
'-' || *p ==
'+') {
433 negative = (*p ==
'-');
438 const uint32_t base = (0 == strncmp(
"0x", p, 2)) ? 16u : 10u;
447 while (*p && !isspace(
static_cast<unsigned char>(*p)) && *p !=
'.') {
448 uint64_t d =
static_cast<unsigned char>(*p);
449 if (d >=
'0' && d <=
'9')
451 else if (base == 16 && (d >=
'a' && d <=
'f'))
458 else if (d ==
',' || d ==
'\'') {
546 [[nodiscard]]
FP128_FORCE_INLINE constexpr operator int64_t() const noexcept {
return static_cast<int64_t
>(low); }
551 [[nodiscard]]
FP128_FORCE_INLINE constexpr operator uint32_t() const noexcept {
return static_cast<uint32_t
>(low); }
556 [[nodiscard]]
FP128_FORCE_INLINE constexpr operator int32_t() const noexcept {
return static_cast<int32_t
>(low); }
579 const uint64_t expo =
log2(temp.low, temp.high);
583 return Float::make(sign,
static_cast<uint32_t
>(expo) + 127,
static_cast<uint32_t
>(temp.low << (
flt_frac_bits - expo)));
590 const uint64_t mant = temp.RoundedMantissa(expo,
flt_frac_bits);
592 return Float::make(sign,
static_cast<uint32_t
>(expo + carry) + 127,
static_cast<uint32_t
>(mant >> carry));
615 const uint64_t expo =
log2(temp.low, temp.high);
625 const uint64_t mant = temp.RoundedMantissa(expo,
dbl_frac_bits);
627 return Double::make(sign, expo + carry + 1023, mant >> carry);
633 [[nodiscard]]
FP128_FORCE_INLINE constexpr operator long double() const noexcept {
return operator double(); }
643 [[nodiscard]]
FP128_INLINE operator std::string()
const {
return operator char*(); }
653 constexpr int buff_size = 45;
654 static thread_local char str[buff_size];
662 if (high ==
static_cast<uint64_t
>(
static_cast<int64_t
>(low) >> 63)) {
663 snprintf(str, buff_size,
"%lld",
static_cast<int64_t
>(low));
668 snprintf(str, buff_size,
"%llu", low);
683 str[buff_size - 1] = 0;
684 char* p = str + buff_size - 1;
689 const uint64_t nom[2] = {temp.low, temp.high};
690 div_64bit((uint64_t*)q, &digit, (uint64_t*)nom, 10ull, 2);
693 *--p =
static_cast<char>(digit +
'0');
697 uint64_t r = temp.low % 10ull;
699 *--p =
static_cast<char>(r +
'0');
721 return temp >>= shift;
731 return temp <<= shift;
741 const uint8_t carry = addcarryx_u64(0, low, rhs.low, &low);
742 addcarryx_u64(carry, high, rhs.high, &high);
765 const uint8_t borrow = subborrow_u64(0, low, rhs.low, &low);
766 subborrow_u64(borrow, high, rhs.high, &high);
792 const uint64_t l = low, h = high;
793 const uint64_t rhs_l = rhs.low, rhs_h = rhs.high;
796 low = mulx_u64(l, rhs_l, &high);
799 high += l * rhs_h + h * rhs_l;
817 const uint64_t l = low, h = high;
820 low = mulx_u64(l, l, &high);
843 template <
typename T>
844 requires std::is_arithmetic_v<T>
850 if constexpr (std::is_floating_point_v<T>) {
855 else if constexpr (std::is_signed_v<T>) {
861 const uint64_t uval =
static_cast<uint64_t
>(x);
863 low = mulx_u64(low, uval, &temp);
864 high = high * uval + temp;
903 const bool res_is_negative = sign != rhs_sign;
912 if (rhs.MagnitudeGreaterThan(*
this)) {
921 const auto bits =
static_cast<int32_t
>(
log2(rhs.low, rhs.high));
926 high = (bits < 64) ? (high >> bits) : 0;
930 const uint64_t nom[2] = {low, high};
934 if (!
div_64bit((uint64_t*)q,
nullptr, (uint64_t*)nom, rhs.low, 2)) {
941 const uint64_t denom[2] = {rhs.low, rhs.high};
951 if (res_is_negative) {
983 if constexpr (std::is_floating_point_v<T>) {
988 else if constexpr (std::is_signed_v<T>) {
993 uint64_t uval =
static_cast<uint64_t
>(x);
996 if (
is_zero() || *
this < uval) {
1001 if (*
this == uval) {
1008 if (1 == popcnt64(uval)) {
1009 return *
this >>= (int32_t)
log2(uval);
1024 if (!
div_64bit(&low,
nullptr, &low, uval, 2)) {
1049 *
this -= rhs * quotient;
1063 const uint64_t nom[2] = {low, high};
1066 if (rhs.high == 0) {
1067 if (!
div_64bit((uint64_t*)q, &low, (uint64_t*)nom, rhs.low, 2)) {
1072 const uint64_t denom[2] = {rhs.low, rhs.high};
1105 const int64_t temp =
static_cast<int64_t
>(high);
1106 const uint64_t sign_bits =
static_cast<uint64_t
>(temp >> 63);
1110 low = FP128_SHIFTRIGHT128(low, high,
static_cast<uint8_t
>(shift));
1111 high =
static_cast<uint64_t
>(temp >> shift);
1115 else if (shift > 127) {
1116 low = high = sign_bits;
1118 low =
static_cast<uint64_t
>(temp >> (shift - 64));
1124 low = FP128_SHIFTRIGHT128(low, high,
static_cast<uint8_t
>(shift));
1129 else if (shift > 127) {
1132 low = high >> (shift - 64);
1149 high = FP128_SHIFTLEFT128(low, high,
static_cast<uint8_t
>(shift));
1154 else if (shift > 127) {
1157 high = low << (shift - 64);
1258 [[nodiscard]]
FP128_FORCE_INLINE constexpr operator bool() const noexcept {
return high != 0 || low != 0; }
1312 if constexpr (std::is_floating_point_v<T>) {
1313 const int32_t r = CompareFloating(rhs);
1332 if constexpr (std::is_floating_point_v<T>) {
1333 const int32_t r = CompareFloating(rhs);
1348 const bool rhs_sign = rhs.is_negative();
1350 if (sign != rhs_sign) {
1356 return high < rhs.high || (high == rhs.high && low < rhs.low);
1365 if constexpr (std::is_floating_point_v<T>) {
1366 const int32_t r = CompareFloating(rhs);
1385 if constexpr (std::is_floating_point_v<T>) {
1386 const int32_t r = CompareFloating(rhs);
1387 return r == -1 || r == 0;
1401 const bool rhs_sign = rhs.is_negative();
1403 if (sign != rhs_sign) {
1409 return high > rhs.high || (high == rhs.high && low > rhs.low);
1418 if constexpr (std::is_floating_point_v<T>) {
1419 const int32_t r = CompareFloating(rhs);
1438 if constexpr (std::is_floating_point_v<T>) {
1439 const int32_t r = CompareFloating(rhs);
1440 return r == 0 || r == 1;
1462 return 0ull == high >> 63;
1475 return 1ull == high >> 63;
1520 constexpr int buff_size = 35;
1521 static thread_local char str[buff_size];
1522 snprintf(str, buff_size,
"0x%016llX%016llX", high, low);
1574 template <
typename T>
1575 requires requires(
int128_base l,
const T& r) { l *= r; }
1658 template <
typename T>
1659 requires std::is_arithmetic_v<T>
1665 template <
typename T>
1666 requires std::is_arithmetic_v<T>
1672 template <
typename T>
1673 requires std::is_arithmetic_v<T>
1679 template <
typename T>
1680 requires std::is_arithmetic_v<T>
1686 template <
typename T>
1687 requires std::is_arithmetic_v<T>
1693 template <
typename T>
1694 requires std::is_arithmetic_v<T>
1700 template <
typename T>
1701 requires std::is_arithmetic_v<T>
1707 template <
typename T>
1708 requires std::is_arithmetic_v<T>
1724 template <
typename T>
1725 requires std::is_arithmetic_v<T>
1728 return int128_base(lhs) <<=
static_cast<int32_t
>(rhs);
1737 template <
typename T>
1738 requires std::is_arithmetic_v<T>
1741 return int128_base(lhs) >>=
static_cast<int32_t
>(rhs);
1757 template <
typename T>
1758 requires std::is_arithmetic_v<T>
1761 if constexpr (std::is_floating_point_v<T>) {
1763 const int32_t r = rhs.CompareFloating(lhs);
1770 template <
typename T>
1771 requires std::is_arithmetic_v<T>
1774 if constexpr (std::is_floating_point_v<T>) {
1776 const int32_t r = rhs.CompareFloating(lhs);
1783 template <
typename T>
1784 requires std::is_arithmetic_v<T>
1787 if constexpr (std::is_floating_point_v<T>) {
1789 const int32_t r = rhs.CompareFloating(lhs);
1796 template <
typename T>
1797 requires std::is_arithmetic_v<T>
1800 if constexpr (std::is_floating_point_v<T>) {
1802 const int32_t r = rhs.CompareFloating(lhs);
1803 return r == 0 || r == 1;
1809 template <
typename T>
1810 requires std::is_arithmetic_v<T>
1813 if constexpr (std::is_floating_point_v<T>) {
1815 const int32_t r = rhs.CompareFloating(lhs);
1822 template <
typename T>
1823 requires std::is_arithmetic_v<T>
1826 if constexpr (std::is_floating_point_v<T>) {
1828 const int32_t r = rhs.CompareFloating(lhs);
1829 return r == -1 || r == 0;
1853 return x.is_positive() ? x : -x;
1871 return (x.high != 0) ? lzcnt64(x.high) : 64 + lzcnt64(x.low);
1884 if (x.is_negative() || x.is_zero())
1889 const auto expo =
static_cast<uint32_t
>(
log2(x));
1896 root <<= ((expo + 1) >> 1);
1902 e = ((root > temp) ? (root - temp) : (temp - root)) >> 1;
1903 root = (root + temp) >> 1;
1918 throw std::domain_error(
"Math domain error! Function accepts positive, non-zero values only.");
1932 throw std::domain_error(
"Math domain error! Function accepts positive, non-zero values only.");
1982 "11719142372802612",
1983 "31855931757113757",
1984 "86593400423993747",
1985 "235385266837019986",
1986 "639843493530054950",
1987 "1739274941520501048",
1988 "4727839468229346562",
1989 "12851600114359308276",
1990 "34934271057485095349",
1991 "94961194206024488746",
1992 "258131288619006739624",
1993 "701673591209763173866",
1994 "1907346572495099690526",
1995 "5184705528587072464088",
1996 "14093490824269387964493",
1997 "38310080007165768493036",
1998 "104137594330290877971835",
1999 "283075330327469390044207",
2000 "769478526514201713818275",
2001 "2091659496012996153907072",
2002 "5685719999335932222640349",
2003 "15455389355901039303530767",
2004 "42012104037905142549565935",
2005 "114200738981568428366295719",
2006 "310429793570191990870734215",
2007 "843835666874145448907332949",
2008 "2293783159469609879099352841",
2009 "6235149080811616882909238709",
2010 "16948892444103337141417836115",
2011 "46071866343312915426773184429",
2012 "125236317084221378051352196075",
2013 "340427604993174052137690718701",
2014 "925378172558778760024239791669",
2015 "2515438670919167006265781174253",
2016 "6837671229762743866755892826678",
2017 "18586717452841279803403701812546",
2018 "50523936302761041945570383321858",
2019 "137338297954017618778418852980854",
2020 "373324199679900164025490831726471",
2021 "1014800388113888727832461784131717",
2022 "2758513454523170206286469819902662",
2023 "7498416996990120434675630591224061",
2024 "20382810665126687668323137537172633",
2025 "55406223843935100525711733958316613",
2026 "150609731458503054835259413016767499",
2027 "409399696212745469666091422932782905",
2028 "1112863754791759412087071478183940806",
2029 "3025077322201142338266566396443428743",
2030 "8223012714622913510304328016407774696",
2031 "22352466037347150474430657323327147399",
2032 "60760302250568721495223289381302760753",
2033 "165163625499400185552832979626485876707"
2035 static constexpr uint64_t lan_table_len =
array_length(lan_table);
2038 if (x >= lan_table[lan_table_len - 1])
2039 return lan_table_len - 1;
2042 uint64_t l = 0, h = lan_table_len - 1, res = (h + l) >> 1;
2045 if (x < lan_table[res]) {
2047 }
else if (l == res)
2065 throw std::domain_error(
"Math domain error! Function accepts positive, non-zero values only.");
2068 static constexpr uint64_t log10_max = 38;
2073 static const auto log10_table = []() {
2074 std::array<int128_base, log10_max + 1> table;
2076 for (uint32_t i = 1u; i <= log10_max; ++i) {
2077 table[i] = table[i - 1] * 10ull;
2082 if (x >= log10_table[log10_max])
2086 uint64_t l = 0, h = log10_max, res = (h + l) >> 1;
2089 if (x < log10_table[res]) {
2091 }
else if (l == res)
2138 std::string text =
static_cast<std::string
>(value);
2139 if ((os.flags() & std::ios_base::showpos) && !text.empty() && text[0] !=
'-')
2140 text.insert(text.begin(),
'+');
2142 const std::streamsize width = os.width();
2144 if (
static_cast<std::streamsize
>(text.size()) < width) {
2145 const size_t padding =
static_cast<size_t>(width) - text.size();
2146 const std::ios_base::fmtflags adjust = os.flags() & std::ios_base::adjustfield;
2147 if (adjust == std::ios_base::left)
2148 text.append(padding, os.fill());
2149 else if (adjust == std::ios_base::internal && !text.empty() && (text[0] ==
'-' || text[0] ==
'+'))
2150 text.insert(1, padding, os.fill());
2152 text.insert(0, padding, os.fill());
2174 const std::istream::sentry sentry(is);
2179 using traits = std::char_traits<char>;
2180 std::streambuf*
const buffer = is.rdbuf();
2181 int c = buffer->sgetc();
2182 const auto next = [&]() {
2184 c = buffer->sgetc();
2186 const auto digit_value = [](
int ch) ->
int {
2187 if (ch >=
'0' && ch <=
'9')
2189 if (ch >=
'a' && ch <=
'f')
2190 return ch -
'a' + 10;
2191 if (ch >=
'A' && ch <=
'F')
2192 return ch -
'A' + 10;
2196 bool negative =
false;
2197 if (c ==
'+' || c ==
'-') {
2198 negative = (c ==
'-');
2202 const std::ios_base::fmtflags basefield = is.flags() & std::ios_base::basefield;
2203 uint32_t base = (basefield == std::ios_base::hex) ? 16u : ((basefield == std::ios_base::oct) ? 8u : 10u);
2204 bool any_digit =
false;
2205 if (base != 8 && c ==
'0') {
2209 if (c ==
'x' || c ==
'X') {
2217 const magnitude_type limit = IsSigned ? (negative ? magnitude_type(0, 1ull << 63) : magnitude_type(UINT64_MAX, UINT64_MAX >> 1))
2218 : magnitude_type(UINT64_MAX, UINT64_MAX);
2219 magnitude_type magnitude;
2220 bool overflow =
false;
2221 while (c != traits::eof() && digit_value(c) <
static_cast<int>(base)) {
2222 const uint32_t digit =
static_cast<uint32_t
>(digit_value(c));
2224 if (magnitude > (limit - magnitude_type(digit)) / magnitude_type(base)) {
2227 magnitude = magnitude * magnitude_type(base) + magnitude_type(digit);
2233 if (c == traits::eof()) {
2234 is.setstate(std::ios_base::eofbit);
2239 is.setstate(std::ios_base::failbit);
2244 is.setstate(std::ios_base::failbit);
2248 uint64_t low = 0, high = 0;
2249 magnitude.get_components(low, high);
2273template <
bool IsSigned>
class numeric_limits<
fp128::int128_base<IsSigned>>
2308 static constexpr int digits = IsSigned ? 127 : 128;
2319 [[nodiscard]]
static constexpr value_type
min() noexcept
2322 return IsSigned ? value_type(0ull, 1ull << 63) : value_type(0ull, 0ull);
2324 [[nodiscard]]
static constexpr value_type
max() noexcept
2326 return IsSigned ? value_type(UINT64_MAX, UINT64_MAX >> 1) : value_type(UINT64_MAX, UINT64_MAX);
2328 [[nodiscard]]
static constexpr value_type
lowest() noexcept {
return min(); }
2329 [[nodiscard]]
static constexpr value_type
epsilon() noexcept {
return value_type(); }
2330 [[nodiscard]]
static constexpr value_type
round_error() noexcept {
return value_type(); }
2331 [[nodiscard]]
static constexpr value_type
infinity() noexcept {
return value_type(); }
2332 [[nodiscard]]
static constexpr value_type
quiet_NaN() noexcept {
return value_type(); }
2333 [[nodiscard]]
static constexpr value_type
signaling_NaN() noexcept {
return value_type(); }
2334 [[nodiscard]]
static constexpr value_type
denorm_min() noexcept {
return value_type(); }
2338template <
bool IsSigned>
struct hash<
fp128::int128_base<IsSigned>>
2342 uint64_t low = 0, high = 0;
2343 value.get_components(low, high);
2346 uint64_t state = low + 0x9E3779B97F4A7C15ull;
2347 state = (state ^ (state >> 30)) * 0xBF58476D1CE4E5B9ull;
2348 state = (state ^ (state >> 27)) * 0x94D049BB133111EBull;
2349 state ^= high + 0x9E3779B97F4A7C15ull + (state << 6) + (state >> 2);
2350 state = (state ^ (state >> 30)) * 0xBF58476D1CE4E5B9ull;
2351 return static_cast<size_t>(state ^ (state >> 31));
2362template <
bool IsSigned>
struct formatter<
fp128::int128_base<IsSigned>, char>
2364 constexpr auto parse(basic_format_parse_context<char>& context)
2366 auto it = context.begin();
2367 const auto end = context.end();
2368 if (it == end || *it ==
'}')
2371 const auto is_align = [](
char c) {
return c ==
'<' || c ==
'>' || c ==
'^'; };
2372 if (it + 1 != end && is_align(*(it + 1))) {
2376 }
else if (is_align(*it)) {
2380 if (it != end && (*it ==
'+' || *it ==
'-' || *it ==
' '))
2383 if (it != end && *it ==
'0') {
2388 while (it != end && *it >=
'0' && *it <=
'9')
2391 if (it != end && *it ==
'd')
2394 if (it != end && *it !=
'}')
2395 throw format_error(
"invalid format specification for a 128 bit integer");
2401 string text =
static_cast<string>(value);
2402 if (
sign !=
'-' && !text.empty() && text[0] !=
'-')
2403 text.insert(text.begin(),
sign);
2405 if (
static_cast<int>(text.size()) <
width) {
2406 const size_t padding =
static_cast<size_t>(
width) - text.size();
2408 const size_t offset = (!text.empty() && (text[0] ==
'-' || text[0] ==
'+' || text[0] ==
' ')) ? 1u : 0u;
2409 text.insert(offset, padding,
'0');
2410 }
else if (
align ==
'<') {
2411 text.append(padding,
fill);
2412 }
else if (
align ==
'^') {
2413 text.insert(0, padding / 2,
fill);
2414 text.append(padding - padding / 2,
fill);
2416 text.insert(0, padding,
fill);
2419 return std::copy(text.begin(), text.end(), context.out());
128 bit integer class, signed or unsigned depending on the template parameter.
constexpr bool is_zero() const noexcept
constexpr int128_base operator~() const noexcept
Bitwise not (~).
static constexpr bool is_signed
constexpr bool operator>=(const T &rhs) const noexcept
Return true this object is larger or equal than the rhs.
constexpr int128_base & operator*=(T x) noexcept
Multiplies a value to this object.
int128_base & operator/=(const int128_base &rhs_in)
Divide this object by rhs.
constexpr int128_base(const int128_base &rhs) noexcept
Copy constructor.
constexpr bool operator==(const int128_base &rhs) const noexcept
Compare logical/bitwise equal.
friend int128_base operator/(const T &lhs, const int128_base &rhs)
Divides a scalar by an int128_base.
constexpr int128_base & operator^=(const int128_base &rhs) noexcept
Bitwise XOR=.
int128_base & operator/=(T x)
Divide this object by x.
friend constexpr int128_base operator+(int128_base lhs, const T &rhs) noexcept
Adds 2 values and returns the result.
constexpr int128_base(T x) noexcept
Constructor from any builtin integral type.
constexpr int128_base & square() noexcept
Squares this object in place.
constexpr bool operator==(const T &rhs) const noexcept
Compare logical/bitwise equal.
constexpr bool operator!() const noexcept
Logical not (!). Opposite of operator bool.
friend constexpr int128_base operator+(const T &lhs, const int128_base &rhs) noexcept
Adds a scalar and an int128_base, in that order.
constexpr int128_base & operator--() noexcept
Prefix – operation (–a)
static constexpr int128_base one() noexcept
Return an instance of int128_base with the value of 1.
constexpr int128_base() noexcept
Default constructor, creates an instance with a value of zero. Explicitly zeroing both QWORDs rather ...
constexpr int128_base & operator=(const T &rhs) noexcept
Assignment operator.
constexpr int128_base & operator-=(const T &rhs) noexcept
Subtract a value from this object like other builtin integer types, underflow wraps around silently.
constexpr int128_base operator++(int32_t) noexcept
Postfix ++ operation (a++)
friend constexpr int128_base operator&(int128_base lhs, const T &rhs) noexcept
Performs bitwise AND (&).
int128_base * ptr_type
Pointer type alias.
friend constexpr bool operator<(const T &lhs, const int128_base &rhs) noexcept
Returns true when a scalar is smaller than an int128_base.
constexpr bool operator>(const int128_base &rhs) const noexcept
Return true this object is larger than the rhs.
constexpr int128_base operator--(int32_t) noexcept
Postfix – operation (a–)
constexpr ~int128_base() noexcept=default
Destructor.
friend constexpr int128_base operator|(int128_base lhs, const T &rhs) noexcept
Performs bitwise OR (|).
int128_base type
Self type alias.
friend constexpr int128_base operator|(const T &lhs, const int128_base &rhs) noexcept
Performs bitwise OR of a scalar and an int128_base.
constexpr int128_base & operator&=(const int128_base &rhs) noexcept
Bitwise AND=.
constexpr int128_base & operator|=(const int128_base &rhs) noexcept
Bitwise OR=.
constexpr int128_base & operator=(int128_base &&rhs) noexcept
Move assignment operator Doesn't modify the right hand side object. Acts like a copy assignment opera...
constexpr int128_base(int128_base &&rhs) noexcept
Move constructor Doesn't modify the right hand side object. Acts like a copy constructor.
friend constexpr int128_base operator^(int128_base lhs, const T &rhs) noexcept
Performs bitwise XOR (^).
friend constexpr bool operator>=(const T &lhs, const int128_base &rhs) noexcept
Returns true when a scalar is larger or equal to an int128_base.
friend constexpr int128_base abs(const int128_base &x) noexcept
Calculates the absolute value of x. Only declared for the signed instantiation. An unqualified abs() ...
constexpr int128_base operator-() const noexcept
Unary -. Returns a copy of the object with sign inverted. Performs a 2's complement operation just li...
constexpr int128_base & operator&=(const T &rhs) noexcept
Bitwise AND=.
friend uint64_t log10(const int128_base &x)
Calculates Log base 10 of x: log10(x), rounded down to the nearest integer.
constexpr int128_base(uint64_t l, uint64_t h) noexcept
Constructor from the 2 int128_base elements, useful for creating special constants.
friend constexpr uint64_t log2(const int128_base &x)
constexpr int128_base & operator-=(const int128_base &rhs) noexcept
Subtract a value from this object like other builtin integer types, underflow wraps around silently.
friend constexpr int128_base operator*(const T &lhs, const int128_base &rhs) noexcept
Multiplies a scalar and an int128_base, in that order.
friend constexpr int128_base operator-(int128_base lhs, const T &rhs) noexcept
Subtracts 2 values and returns the result.
constexpr bool operator<(const int128_base &rhs) const noexcept
Return true if this object is small than the rhs.
friend constexpr int128_base operator&(const T &lhs, const int128_base &rhs) noexcept
Performs bitwise AND of a scalar and an int128_base.
constexpr int128_base & operator+=(const T &rhs) noexcept
Add a value to this object like other builtin integer types, overflow wraps around silently.
constexpr int128_base & operator<<=(int32_t shift) noexcept
Shift left this object.
int128_base & operator%=(const int128_base &rhs)
%= operator
friend constexpr int128_base operator-(const T &lhs, const int128_base &rhs) noexcept
Subtracts an int128_base from a scalar.
int128_base(const char *x) noexcept
Constructor from const char* (C string). Allows creating 128 bit values from a string....
constexpr bool operator<=(const int128_base &rhs) const noexcept
Return true this object is small or equal than the rhs.
friend int128_base operator%(int128_base lhs, const T &rhs)
Performs modulo and returns the result.
friend int128_base operator/(int128_base lhs, const T &rhs)
Divides 2 values and returns the result.
constexpr bool operator>=(const int128_base &rhs) const noexcept
Return true this object is larger or equal than the rhs.
constexpr int128_base & operator>>=(int32_t shift) noexcept
Shift right this object. The signed type shifts arithmetically, replicating the sign bit; the unsigne...
friend int128_base operator%(const T &lhs, const int128_base &rhs)
Performs modulo of a scalar by an int128_base.
friend constexpr int128_base pow(const int128_base &x, uint32_t y) noexcept
Calculates x to the power of y.
constexpr bool is_negative() const noexcept
constexpr bool is_int() const noexcept
Returns true if the value is an int (fraction is zero)
constexpr int128_base & operator*=(const int128_base &rhs) noexcept
Multiplies a value to this object.
friend constexpr bool operator==(const T &lhs, const int128_base &rhs) noexcept
Compares a scalar and an int128_base for equality.
constexpr int128_base(double x) noexcept
Constructor from the double type Underflow goes to zero. Overflow, NaN and +-INF go to the largest su...
friend constexpr int128_base operator>>(const T &lhs, const int128_base &rhs) noexcept
Shifts a scalar right by an int128_base shift count. The left operand is widened to int128_base first...
constexpr int32_t get_bit(uint32_t bit) const noexcept
get a specific bit within the 128 data
constexpr int128_base & operator++() noexcept
Prefix ++ operation (++a)
friend constexpr uint64_t lzcnt128(const int128_base &x) noexcept
Calculates the left zero count of value x.
constexpr bool operator!=(const T &rhs) const noexcept
Return true when objects are not equal. Can be used as logical XOR.
friend constexpr int128_base sqr(int128_base x) noexcept
Calculates the square of a value. i.e. x^2.
char * hex() const noexcept
Converts this object to a hex C string. The returned string is a statically thread-allocated buffer....
int128_base & ref_type
Reference type alias.
constexpr int128_base operator>>(int32_t shift) const noexcept
Performs right shift operation.
constexpr int128_base & operator|=(const T &rhs) noexcept
Bitwise OR=.
friend constexpr int128_base operator^(const T &lhs, const int128_base &rhs) noexcept
Performs bitwise XOR of a scalar and an int128_base.
friend uint64_t log(const int128_base &x)
Calculates the natural Log (base e) of x: log(x), rounded down to the nearest integer.
friend constexpr int128_base operator*(int128_base lhs, const T &rhs) noexcept
Multiplies 2 values and returns the result.
constexpr bool is_positive() const noexcept
Returns true if the value is positive (including zero) Always true for the unsigned type,...
friend constexpr bool operator!=(const T &lhs, const int128_base &rhs) noexcept
Compares a scalar and an int128_base for inequality.
constexpr int128_base & operator^=(const T &rhs) noexcept
Bitwise XOR=.
constexpr void get_components(uint64_t &l, uint64_t &h) const noexcept
Extracts the low and high QWORD.
constexpr int128_base operator+() const noexcept
Unary +. Returns a copy of the object.
constexpr bool operator>(const T &rhs) const noexcept
Return true this object is larger than the rhs.
int128_base & operator%=(T x)
%= operator
constexpr int128_base & operator+=(const int128_base &rhs) noexcept
Add a value to this object like other builtin integer types, overflow wraps around silently.
int128_base(const std::string &x) noexcept
Constructor from std::string. Allows creating very high precision values. Much slower than the other ...
constexpr bool operator!=(const int128_base &rhs) const noexcept
Return true when objects are not equal. Can be used as logical XOR.
constexpr int128_base operator<<(int32_t shift) const noexcept
Performs left shift operation.
friend constexpr bool operator<=(const T &lhs, const int128_base &rhs) noexcept
Returns true when a scalar is smaller or equal to an int128_base.
friend constexpr bool operator>(const T &lhs, const int128_base &rhs) noexcept
Returns true when a scalar is larger than an int128_base.
friend uint64_t sqrt(const int128_base &x) noexcept
Calculates the square root using Newton's method. Based on the book "Math toolkit for real time progr...
friend constexpr int128_base operator<<(const T &lhs, const int128_base &rhs) noexcept
Shifts a scalar left by an int128_base shift count.
constexpr bool operator<(const T &rhs) const noexcept
Return true if this object is small than the rhs.
char * to_string() const noexcept
Converts to a char* (slow) string holds all meaningful fraction bits.
constexpr bool operator<=(const T &rhs) const noexcept
Return true this object is small or equal than the rhs.
static constexpr int digits
Value bits, which excludes the sign bit for the signed type.
static constexpr bool is_signed
static constexpr int min_exponent10
static constexpr int max_exponent10
static FP128_SUPPRESS_DEPRECATED_BEGIN constexpr bool has_denorm_loss
static constexpr value_type epsilon() noexcept
static constexpr int min_exponent
static constexpr value_type infinity() noexcept
static constexpr bool has_infinity
static constexpr bool is_specialized
static constexpr bool has_signaling_NaN
static constexpr bool has_quiet_NaN
static constexpr int max_exponent
static constexpr float_denorm_style has_denorm
static constexpr bool is_integer
static constexpr bool is_bounded
static constexpr value_type denorm_min() noexcept
static constexpr bool traps
Division by zero throws std::logic_error rather than producing a value.
static constexpr bool is_iec559
static constexpr value_type quiet_NaN() noexcept
static constexpr int max_digits10
static FP128_SUPPRESS_DEPRECATED_END constexpr float_round_style round_style
static constexpr int radix
static constexpr value_type min() noexcept
static constexpr bool is_modulo
Both types wrap around: +, - and * keep the low 128 bits of the exact result, which differs from it b...
static constexpr int digits10
Decimal digits that can be represented without change: floor(digits * log10(2)), which is 38 for both...
static constexpr value_type max() noexcept
static constexpr bool is_exact
static constexpr value_type round_error() noexcept
static constexpr bool tinyness_before
static constexpr value_type lowest() noexcept
static constexpr value_type signaling_NaN() noexcept
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_FORCE_INLINE
Marks a function the compiler must inline rather than merely may.
#define FP128_INT_DIVIDE_BY_ZERO_EXCEPTION
Everything the library provides.
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 = ...
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 void twos_complement128(uint64_t &l, uint64_t &h) noexcept
converts a 128 integer to negative via 2's complement.
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_frac_bits
Mantissa bit count of an IEEE 754 float.
std::istream & operator>>(std::istream &is, fixed_point128< I > &value)
Reads a fixed_point128 from a stream, the way the builtin arithmetic extractors do.
static errno_t strnlwr(char *dest, const char *src, size_t n) noexcept
Copy a string to lowercase, up to n characters.
static constexpr int32_t dbl_frac_bits
Mantissa bit count of an IEEE 754 double.
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 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...
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.
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).
static constexpr double make(uint64_t s, uint64_t e, uint64_t f) noexcept
Assembles a double out of its three fields.
constexpr uint64_t e() const noexcept
Biased exponent bits.
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().
size_t operator()(const fp128::int128_base< IsSigned > &value) const noexcept