|
fixed_point128 1.0.1.0
128 bit integer, fixed point and floating point arithmetic for C++20
|
Everything the library provides. More...
Namespaces | |
| namespace | detail |
Classes | |
| struct | Double |
| Bit level view of an IEEE 754 double-precision value. More... | |
| class | fixed_point128 |
| Google test class (friend). More... | |
| struct | Float |
| Bit level view of an IEEE 754 single-precision value. More... | |
| class | float128 |
| 128 bit floating point class. More... | |
| class | int128_base |
| 128 bit integer class, signed or unsigned depending on the template parameter. More... | |
Typedefs | |
| using | int128_t = int128_base<true> |
| 128 bit signed integer type. | |
| using | uint128_t = int128_base<false> |
| 128 bit unsigned integer type. | |
Functions | |
| bool | Divide256By128 (uint64_t *q, uint64_t *r, const uint64_t *u, const uint64_t *v) noexcept |
| Divides a 256 bit numerator by a 128 bit divisor: the one call of div_128bit() in this header. | |
| template<int32_t I> | |
| std::ostream & | operator<< (std::ostream &os, const fixed_point128< I > &value) |
| Writes a fixed_point128 to a stream, honouring its width, fill and adjustment. | |
| template<int32_t I> | |
| std::istream & | operator>> (std::istream &is, fixed_point128< I > &value) |
| Reads a fixed_point128 from a stream, the way the builtin arithmetic extractors do. | |
| 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 string delegates to it. | |
| std::from_chars_result | from_chars (const char *first, const char *last, float128 &value, std::chars_format fmt) |
| Reads a decimal or hexadecimal string into a float128. Defined below. | |
| float128 | operator""_f128 (const char *literal) |
| User-defined literal for constructing float128 from a string. | |
| std::string | to_string (const float128 &value) |
| The shortest decimal string that reads back as this exact value. | |
| 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. | |
| std::to_chars_result | to_chars (char *first, char *last, const float128 &value, std::chars_format fmt) |
| Converts a float128 to text with the default precision for the layout. | |
| std::to_chars_result | to_chars (char *first, char *last, const float128 &value) |
| Converts a float128 to the shortest text that reads back as the same value. | |
| std::ostream & | operator<< (std::ostream &os, const float128 &value) |
| Writes a float128 to a stream, honouring its formatting state. | |
| std::istream & | operator>> (std::istream &is, float128 &value) |
| Reads a float128 from a stream. | |
| static errno_t | strnlwr (char *dest, const char *src, size_t n) noexcept |
| Copy a string to lowercase, up to n characters. | |
| template<typename T> | |
| 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 = array_length(a); // returns 5 at build time. | |
| constexpr uint64_t | shift_right64_round (uint64_t x, int shift) noexcept |
| Right-shift a 64-bit value with rounding. | |
| 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 (checked via assert in debug builds) | |
| 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 assert in debug builds) | |
| 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 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 going away from zero. | |
| 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. | |
| template<int shift> | |
| 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 code for all shift values. | |
| template<int shift> | |
| 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 code for all shift values. | |
| constexpr uint64_t | shift_right128 (uint64_t l, uint64_t h, int shift) noexcept |
| Right shift a 128 bit integer. | |
| constexpr uint64_t | shift_right128_round (uint64_t l, uint64_t h, int shift) noexcept |
| Right shift a 128 bit integer with rounding. | |
| constexpr uint64_t | shift_left128 (uint64_t l, uint64_t h, int shift) noexcept |
| Left shift a 128 bit integer. | |
| constexpr void | twos_complement128 (uint64_t &l, uint64_t &h) noexcept |
| converts a 128 integer to negative via 2's complement. | |
| 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. | |
| template<int64_t m> | |
| 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, 64 bit limbs. | |
| static bool | div_128bit (uint64_t *q, uint64_t *r, const uint64_t *u, const uint64_t *v, int64_t m) noexcept |
| div_128bit() with the word count of the numerator chosen at run time. | |
| 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. | |
| constexpr uint64_t | lzcnt128 (uint64_t l, uint64_t h) noexcept |
| Left zero count 128 bit. | |
| constexpr uint64_t | log2 (uint64_t l, uint64_t h) noexcept |
| Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1. | |
| constexpr uint64_t | log2 (uint64_t x) noexcept |
| Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1. | |
| constexpr uint32_t | log2 (uint32_t x) noexcept |
| Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1. | |
| 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. | |
| template<bool IsSigned> | |
| std::ostream & | operator<< (std::ostream &os, const int128_base< IsSigned > &value) |
| Writes a 128 bit integer to a stream, honouring its width, fill and adjustment. | |
| template<bool IsSigned> | |
| std::istream & | operator>> (std::istream &is, int128_base< IsSigned > &value) |
| Reads a 128 bit integer from a stream, the way the builtin integer extractors do. | |
| int128_t | operator""_int128 (const char *literal) |
| User-defined literal for constructing int128_t from a string. | |
| uint128_t | operator""_uint128 (const char *literal) |
| User-defined literal for constructing uint128_t from a string. | |
CRT-Style Math Functions (Forward Declarations) | |
Free functions providing standard math library equivalents for float128. | |
| template<int32_t I> | |
| fixed_point128< I > | fabs (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | floor (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | ceil (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | trunc (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | round (const fixed_point128< I > &x) |
| template<int32_t I> | |
| int32_t | ilogb (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | copysign (const fixed_point128< I > &x, const fixed_point128< I > &y) |
| template<int32_t I> | |
| fixed_point128< I > | fmod (const fixed_point128< I > &x, const fixed_point128< I > &y) |
| template<int32_t I> | |
| fixed_point128< I > | modf (const fixed_point128< I > &x, fixed_point128< I > *iptr) |
| template<int32_t I> | |
| fixed_point128< I > | fdim (const fixed_point128< I > &x, const fixed_point128< I > &y) |
| template<int32_t I> | |
| fixed_point128< I > | fmin (const fixed_point128< I > &x, const fixed_point128< I > &y) |
| template<int32_t I> | |
| fixed_point128< I > | fmax (const fixed_point128< I > &x, const fixed_point128< I > &y) |
| template<int32_t I> | |
| fixed_point128< I > | hypot (const fixed_point128< I > &x, const fixed_point128< I > &y) |
| template<int32_t I> | |
| fixed_point128< I > | sqrt (const fixed_point128< I > &x, uint32_t iterations=3) |
| template<int32_t I> | |
| fixed_point128< I > | sin (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | asin (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | cos (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | acos (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | tan (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | atan (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | atan2 (fixed_point128< I > y, fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | sinh (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | asinh (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | cosh (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | acosh (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | tanh (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | atanh (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | exp (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | exp2 (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | expm1 (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | pow (const fixed_point128< I > &x, const fixed_point128< I > &y) |
| template<int32_t I> | |
| fixed_point128< I > | log (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | log2 (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | log10 (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | logb (fixed_point128< I > x) |
| template<int32_t I> | |
| fixed_point128< I > | log1p (fixed_point128< I > x) |
| constexpr float128 | fabs (const float128 &x) |
| constexpr float128 | floor (const float128 &x) |
| constexpr float128 | ceil (const float128 &x) |
| constexpr float128 | trunc (const float128 &x) |
| constexpr float128 | round (const float128 &x) |
| constexpr int64_t | llrint (const float128 &x) |
| constexpr int64_t | llround (const float128 &x) |
| constexpr int32_t | lrint (const float128 &x) |
| constexpr int32_t | lround (const float128 &x) |
| constexpr int32_t | ilogb (const float128 &x) |
| constexpr float128 | copysign (const float128 &x, const float128 &y) |
| float128 | fmod (const float128 &x, const float128 &y) |
| constexpr float128 | modf (const float128 &x, float128 *iptr) |
| constexpr float128 | fdim (const float128 &x, const float128 &y) |
| constexpr float128 | fmin (const float128 &x, const float128 &y) |
| constexpr float128 | fmax (const float128 &x, const float128 &y) |
| constexpr float128 | fma (float128 x, float128 y, float128 z) |
| float128 | hypot (const float128 &x, const float128 &y) |
| float128 | cbrt (const float128 x, uint32_t iterations=2) |
| float128 | sqrt (const float128 &x, uint32_t iterations=3) |
| float128 | erf (float128 x) |
| float128 | erfc (float128 x) |
| float128 | sin (float128 x) |
| float128 | asin (float128 x) |
| float128 | cos (float128 x) |
| float128 | acos (float128 x) |
| float128 | tan (float128 x) |
| float128 | atan (float128 x) |
| float128 | atan2 (float128 y, float128 x) |
| float128 | sinh (float128 x) |
| float128 | asinh (float128 x) |
| float128 | cosh (float128 x) |
| float128 | acosh (float128 x) |
| float128 | tanh (float128 x) |
| float128 | atanh (float128 x) |
| float128 | exp (const float128 &x) |
| float128 | exp2 (const float128 &x) |
| float128 | expm1 (const float128 &x) |
| float128 | pow (const float128 &x, const float128 &y) |
| float128 | pow (const float128 &x, int32_t y) |
| float128 | log (float128 x) |
| float128 | log2 (float128 x) |
| float128 | log10 (float128 x) |
| float128 | logb (float128 x) |
| float128 | log1p (float128 x) |
| constexpr float128 | frexp (float128 x, int *expptr) |
| constexpr float128 | ldexp (float128 x, int exp) |
| constexpr bool | isfinite (const float128 &x) |
| constexpr bool | signbit (const float128 &x) |
| constexpr bool | isnormal (const float128 &x) |
| constexpr int | fpclassify (const float128 &x) |
| constexpr bool | isunordered (const float128 &x, const float128 &y) |
| constexpr bool | isgreater (const float128 &x, const float128 &y) |
| constexpr bool | isgreaterequal (const float128 &x, const float128 &y) |
| constexpr bool | isless (const float128 &x, const float128 &y) |
| constexpr bool | islessequal (const float128 &x, const float128 &y) |
| constexpr bool | islessgreater (const float128 &x, const float128 &y) |
| constexpr float128 | rint (const float128 &x) |
| constexpr float128 | nearbyint (const float128 &x) |
| constexpr float128 | scalbn (const float128 &x, int n) |
| constexpr float128 | scalbln (const float128 &x, long n) |
| constexpr float128 | nextafter (const float128 &x, const float128 &y) |
| constexpr float128 | nexttoward (const float128 &x, const float128 &y) |
| float128 | remainder (const float128 &x, const float128 &y) |
| float128 | remquo (const float128 &x, const float128 &y, int *quo) |
| float128 | hypot (const float128 &x, const float128 &y, const float128 &z) |
| float128 | lgamma (float128 x) |
| float128 | tgamma (float128 x) |
| constexpr float128 | abs (const float128 &x) |
| constexpr bool | totalorder (const float128 &x, const float128 &y) |
| constexpr bool | totalordermag (const float128 &x, const float128 &y) |
| constexpr float128 | fmaxmag (const float128 &x, const float128 &y) |
| constexpr float128 | fminmag (const float128 &x, const float128 &y) |
| constexpr bool | iscanonical (const float128 &x) |
| constexpr bool | issignaling (const float128 &x) |
| constexpr bool | issubnormal (const float128 &x) |
| constexpr bool | iszero (const float128 &x) |
| float128 | nan (const char *payload) |
Non-CRT Utility Functions (Forward Declarations) | |
| template<int32_t I> | |
| uint64_t | lzcnt128 (const fixed_point128< I > &x) |
| template<int32_t I> | |
| fixed_point128< I > | reciprocal (const fixed_point128< I > &x) |
| template<int32_t I> | |
| void | fact_reciprocal (int x, fixed_point128< I > &res) |
| float128 | reciprocal (const float128 &x) |
| void | fact_reciprocal (int x, float128 &res) |
| float128 | double_factorial (int x) |
Floating point environment | |
IEEE 754-2008 asks two things of a binary format beyond its arithmetic. Clause 4 requires four rounding-direction attributes the program can choose between: to nearest with ties to even, the default, and towards zero, towards positive infinity and towards negative infinity. Clause 7 requires five exceptions - invalid operation, division by zero, overflow, underflow and inexact - each reported by a status flag that stays raised until the program lowers it. float128 provides both, as a per thread environment of its own that is entirely separate from the hardware one <cfenv> controls: setting the rounding direction here does not change how double rounds, and an exception raised by a float128 operation shows up here and not in fetestexcept(). The environment is opt in. It exists only when every translation unit that includes this header is compiled with FP128_IEEE_ENV defined. Without it, which is the default, every operation rounds to nearest with ties to even, no flag is ever raised, and the code that would maintain the environment compiles away, leaving the arithmetic exactly as fast as it was before the environment existed. The functions below are still declared, so the same source builds either way: they report the fixed default and refuse to change it. The macro changes the definition of inline functions, so it must have the same value in every translation unit of a program. Mixing the two violates the one definition rule. The rounding directions and exception flags are named with the FE_ constants of <cfenv>, so the calls read the way their hardware counterparts do: fp128::fesetround(FE_UPWARD);
float128 bound = a / b; // rounded towards +inf
int fetestexcept(int excepts) noexcept Tests float128 exception flags (IEEE 754 testFlags). Definition float128.h:452 int fesetround(int round) noexcept Selects the rounding direction float128 arithmetic uses on the calling thread. Definition float128.h:427 The calls have to be qualified: their arguments are plain integers, so argument dependent lookup cannot tell them from the <cfenv> functions of the same name. | |
| int | fegetround () noexcept |
| The rounding direction float128 arithmetic uses on the calling thread. | |
| int | fesetround (int round) noexcept |
| Selects the rounding direction float128 arithmetic uses on the calling thread. | |
| int | fetestexcept (int excepts) noexcept |
| Tests float128 exception flags (IEEE 754 testFlags). | |
| int | feclearexcept (int excepts) noexcept |
| Lowers float128 exception flags (IEEE 754 lowerFlags). | |
| int | feraiseexcept (int excepts) noexcept |
| Raises float128 exception flags (IEEE 754 raiseFlags). | |
| int | fegetexceptflag (std::fexcept_t *flagp, int excepts) noexcept |
| Saves the state of float128 exception flags (IEEE 754 saveAllFlags). | |
| int | fesetexceptflag (const std::fexcept_t *flagp, int excepts) noexcept |
| Restores float128 exception flags saved by fegetexceptflag (IEEE 754 restoreFlags). | |
Variables | |
| constexpr uint64_t | fp128_trig_series_table [][2] |
| 1 / (n(n+1)) for n = 1..40, with 125 fraction bits rounded to nearest, {high, low}. | |
| constexpr uint64_t | log2_recip_table [][2] |
| 1/(1 + j/64) as a 128 bit fraction, the reciprocals fixed_point128::log2() reduces with. | |
| constexpr uint64_t | log2_value_table [][2] |
| -log2 of the matching log2_recip_table entry, as a 128 bit fraction. | |
| 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)*ln2). | |
| constexpr int32_t | log2_reduction_bits = 6 |
| Bits of argument reduction fixed_point128::log2() applies; log2_recip_table has 2^this entries. | |
Library Version | |
The FP128_VERSION_* macros as constants, for code that reports the version rather than branching on it at preprocessing time. | |
| static constexpr int32_t | version_major = FP128_VERSION_MAJOR |
| Breaking changes to the public interface. | |
| static constexpr int32_t | version_minor = FP128_VERSION_MINOR |
| Backwards compatible additions. | |
| static constexpr int32_t | version_patch = FP128_VERSION_PATCH |
| Fixes that change neither. | |
| static constexpr int32_t | version_build = FP128_VERSION_BUILD |
| Rebuild of an unchanged source tree. | |
| static constexpr int32_t | version = FP128_VERSION |
| All four packed into one comparable integer. | |
| static constexpr const char * | version_string = FP128_VERSION_STRING |
| Dotted form, e.g. "0.9.0.0". | |
IEEE 754 Layout Constants | |
| static constexpr int32_t | flt_frac_bits = 23 |
| Mantissa bit count of an IEEE 754 float. | |
| static constexpr int32_t | flt_exp_bits = 8 |
| Exponent bit count of an IEEE 754 float. | |
| static constexpr int32_t | dbl_frac_bits = 52 |
| Mantissa bit count of an IEEE 754 double. | |
| static constexpr int32_t | dbl_exp_bits = 11 |
| Exponent bit count of an IEEE 754 double. | |
Everything the library provides.
The namespace holds four value types and the free functions that operate on them:
Each of them supports the operators, the <cmath> functions, the std::numeric_limits, std::hash and std::formatter specializations, and the stream insertion and extraction that the corresponding builtin type does, so they can stand in for one in generic code.
The rest of the namespace is the machinery those types are built from - 128 bit shifts, multiply, divide and bit counting over pairs of QWORDs - which is public because the types are header only and inline everything, not because a user is expected to call it.
| using fp128::int128_t = int128_base<true> |
128 bit signed integer type.
An alias of the signed instantiation of fp128::int128_base, which holds the entire implementation. This type implements the standard operators a signed integer type has.
All of int128_t's methods are inline for maximum performance.
Implementation notes:
Definition at line 76 of file int128_t.h.
| using fp128::uint128_t = int128_base<false> |
128 bit unsigned integer type.
An alias of the unsigned instantiation of fp128::int128_base, which holds the entire implementation. This type implements the standard operators an unsigned integer type has.
All of uint128_t's methods are inline for maximum performance.
Implementation notes:
Definition at line 73 of file uint128_t.h.
|
nodiscardnoexcept |
| x | Input value |
Definition at line 2720 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | Input value |
Definition at line 2733 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | Input value |
Definition at line 2745 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | Value to truncate |
Definition at line 2758 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | Value to round |
Definition at line 2769 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | The specified value. |
Definition at line 2782 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | The value that's returned as the magnitude of the result. |
| y | The sign of the result. |
Definition at line 2792 of file fixed_point128.h.
|
nodiscard |
| x | Numerator |
| y | Denominator |
| std::logic_error | when y is zero. This type has no NaN to return instead. |
Definition at line 2805 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | Input value |
| iptr | Pointer to fixed_point128 holding the integer part of x. |
Definition at line 2813 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | First value |
| y | Second value |
Definition at line 2829 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | First value |
| y | Second value |
Definition at line 2839 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | First value |
| y | Second value |
Definition at line 2846 of file fixed_point128.h.
|
nodiscardnoexcept |
Correctly rounded, and free of intermediate overflow: hypot(x, y) * 2^F is the square root of the sum of the squared raw values, which is formed as an exact 256 bit integer. Spelled as sqrt(sqr(x) + sqr(y)) instead, it overflowed as soon as |x| or |y| reached 2^(I/2), long before the result itself leaves the range.
| x | First value |
| y | Second value |
Definition at line 2859 of file fixed_point128.h.
|
nodiscardnoexcept |
sqrt(x) * 2^F is the square root of the raw value scaled up by another F bits, which makes it the integer square root of a 256 bit number: RoundedSqrt256() computes it rounded to nearest, so the result is the representable value closest to the exact root at every I and every magnitude. (A tie between two representable values cannot occur.)
| x | Value to calculate the root of |
| iterations | Ignored. It used to set the step count of an iterative refinement, which lost up to half of the integer bits for a large argument; it is kept so that existing calls still compile. |
Definition at line 2915 of file fixed_point128.h.
|
nodiscardnoexcept |
Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 2.5 ulp for |x| < 4 (1.4 ulp from I = 16 up). Beyond that the reduction x - n * pi/2 adds n times the rounding error of pi/2 in this instantiation, at most |x| / 3 ulp more: 320 ulp for |x| near 1000 at I = 32.
| x | value in Radians |
Definition at line 3224 of file fixed_point128.h.
|
nodiscardnoexcept |
Accuracy, measured against mpmath at I = 4, 16, 32, 62 and 63: within 3.5 ulp.
| x | value in radians in the range [-1,1] |
Definition at line 3257 of file fixed_point128.h.
|
nodiscardnoexcept |
Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 1.8 ulp for |x| < 4 (1.3 ulp from I = 16 up), plus at most |x| / 3 ulp of argument reduction beyond, see sin().
| x | value in Radians |
Definition at line 3283 of file fixed_point128.h.
|
nodiscardnoexcept |
Accuracy, measured against mpmath at I = 4, 16, 32, 62 and 63: within 4 ulp.
| x | value in radians in the range [-1,1] |
Definition at line 3314 of file fixed_point128.h.
|
nodiscardnoexcept |
The error of the cosine is divided by the cosine itself, so it grows towards the poles. Measured against mpmath for |x| < 4 at I = 4, 16, 32, 62 and 63: within 45 * max(1, |tan(x)|) ulp.
| x | value |
Definition at line 3339 of file fixed_point128.h.
|
nodiscardnoexcept |
|
nodiscardnoexcept |
| y | value |
| x | value |
Definition at line 3414 of file fixed_point128.h.
|
nodiscardnoexcept |
|
nodiscardnoexcept |
| x | value |
Definition at line 3503 of file fixed_point128.h.
|
nodiscardnoexcept |
|
nodiscardnoexcept |
| x | value in the range [1, inf] |
Definition at line 3559 of file fixed_point128.h.
|
nodiscardnoexcept |
|
nodiscardnoexcept |
| x | value in the range (-1, 1) |
Definition at line 3612 of file fixed_point128.h.
|
nodiscardnoexcept |
x
The Maclaurin series will quickly overflow as x's power increases rapidly.
x ix fx
Using the equality e = e * e Where ix is the integer part of x and fx is the fraction part. ix is computed via multiplications which won't overflow if the result value can be held. fx is computed via Maclaurin series expansion, but since fx < 1, it won't overflow. A negative x works with 1/e and the alternating series of e^-fx, so it never forms e^|x|.
Accuracy, measured against mpmath at I = 1, 2, 4, 16, 32, 62 and 63 where the function exists: within 6.2 * max(1, e^x) ulp. The argument itself is only known to half an ulp, which alone moves a large result by e^x / 2 ulp.
| x | A number specifying a power. |
Definition at line 3645 of file fixed_point128.h.
|
nodiscardnoexcept |
|
nodiscardnoexcept |
| x | A number specifying a power. |
Definition at line 3699 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | Base value, must be positive |
| y | Exponent value |
Definition at line 3732 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | The number to perform log on. |
| std::domain_error | when x is zero or negative. |
Definition at line 3812 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | The number to perform log2 on. |
Definition at line 3767 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | The number to perform log on. |
Definition at line 3840 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | The number to perform log on. |
| std::domain_error | when x is zero. The C function returns minus infinity for it, which this type cannot represent. |
Definition at line 3862 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | The number to perform log on. |
| std::domain_error | when x is -1 or below. |
Definition at line 3832 of file fixed_point128.h.
|
nodiscardnoexcept |
| x | input value. |
Definition at line 2895 of file fixed_point128.h.
|
nodiscardnoexcept |
Correctly rounded: the quotient comes from the same long division operator/=() uses, rounded to nearest with a tie going away from zero. The division is the faster of the two since div_128bit, and the Newton refinement of a double estimate this replaced lost accuracy as 1 / x grew - tens of thousands of ulp at fixed_point128<16>. It is called directly rather than through operator/=(), which divides by multiplying with this function when FP128_USE_RECIPROCAL_FOR_DIVISION is set.
| x | Input value |
Definition at line 3006 of file fixed_point128.h.
|
noexcept |
| x | Input value |
| res | Result of the function |
Definition at line 2945 of file fixed_point128.h.
|
inlinenodiscardnoexcept |
Divides a 256 bit numerator by a 128 bit divisor: the one call of div_128bit() in this header.
The long division, the remainder and the integer square root of fixed_point128 all divide by a divisor that needs both QWORDs, and all of them come through here. The word count is fixed at four by the template argument of div_128bit<4>(), so its loops are unrolled for this shape whether the compiler inlines it or not. That used to depend on inlining: with the count an ordinary argument and callers in int128_shared.h and float128.h sharing one out of line copy, clang++ compiled a generic division and the long division here lost 15%.
| q | (output) Quotient, three words, q[0] lowest. |
| r | (output, optional) Remainder, two words. Can be nullptr. |
| u | Numerator, four words, u[0] lowest. |
| v | Divisor, two words; v[1] must not be zero. |
Definition at line 160 of file fixed_point128.h.
|
inline |
Writes a fixed_point128 to a stream, honouring its width, fill and adjustment.
| I | Integer bit count of the type |
Definition at line 3879 of file fixed_point128.h.
|
inline |
Reads a fixed_point128 from a stream, the way the builtin arithmetic extractors do.
Reads what the constructor from a string reads - an optional sign, digits, an optional point and more digits - correctly rounded to the grid, and stops at the first character that is not part of it, leaving it in the stream: "2.5,3" gives 2.5 and leaves ",3".
Failure sets failbit: no digits at all stores zero, and a value outside [lowest(), max()] stores the nearer of the two. Leading white space is skipped unless skipws is off. This used to read a whole white space delimited token through the string constructor, which never failed: "xyz" read as zero and an overflow wrapped silently.
| I | Integer bit count of the type |
Definition at line 3913 of file fixed_point128.h.
|
inline |
Reads a decimal string into a float128. Defined below, declared here because the constructor from a string delegates to it.
Converts text to a float128, in the shape of std::from_chars.
Reads the longest prefix of [first, last) that forms a number: an optional sign, decimal digits with an optional point and an optional exponent, or one of inf, infinity, nan, nan(payload) and snan. The result is the representable value nearest the one the text names, rounded once in the current rounding direction - including when it is subnormal, where fewer than 113 bits are kept.
That holds for any number of digits. The first 40 significant ones settle the rounding unless the value lies within one unit of the fortieth digit of a point halfway between two binary128 values; only then are all of them read, up to the 11700 that decide any case, which costs a few milliseconds for an input that long. Digits past the fortieth used to be dropped, so 10384593717069655257060992658440193.000001, just above the tie between 2^113 and 2^113 + 2, read as the tie and rounded down to 2^113.
| first | Start of the text |
| last | One past the end of the text |
| value | Receives the parsed value, untouched when nothing was parsed |
Definition at line 7376 of file float128.h.
|
inline |
Reads a decimal or hexadecimal string into a float128. Defined below.
Converts text to a float128 in a given format, in the shape of std::from_chars.
chars_format::hex reads a hexadecimal significand and a binary exponent without the 0x prefix, as std::from_chars does; every other format reads the decimal forms from_chars() above accepts.
| first | Start of the text |
| last | One past the end of the text |
| value | Receives the parsed value, untouched when nothing was parsed |
| fmt | The format to read |
Definition at line 7568 of file float128.h.
| float128 fp128::operator""_f128 | ( | const char * | literal | ) |
User-defined literal for constructing float128 from a string.
User-defined literal implementation for constructing float128 from a string.
Definition at line 6693 of file float128.h.
| x | Input value |
Definition at line 3536 of file float128.h.
| x | Input value |
Definition at line 3545 of file float128.h.
| x | Value to truncate |
Definition at line 3554 of file float128.h.
| x | Value to round |
Definition at line 3564 of file float128.h.
|
constexprnoexcept |
| x | Input value |
Definition at line 3575 of file float128.h.
|
constexprnoexcept |
| x | Value to round |
Definition at line 3583 of file float128.h.
|
constexprnoexcept |
| x | Input value |
Definition at line 3589 of file float128.h.
|
constexprnoexcept |
| x | Value to round |
Definition at line 3597 of file float128.h.
|
constexprnoexcept |
IEEE 754-2008 logB (5.3.3) with an integer result format: a zero, an infinity and a NaN have no exponent the result can express, so each is the invalid operation.
| x | The specified value. |
Definition at line 3609 of file float128.h.
| x | The value that's returned as the magnitude of the result. |
| y | The sign of the result. |
Definition at line 3633 of file float128.h.
| x | Numerator |
| y | Denominator |
Definition at line 3645 of file float128.h.
| x | Input value |
| iptr | Pointer to float128 holding the integer part of x. |
Definition at line 3720 of file float128.h.
| x | First value |
| y | Second value |
Definition at line 3745 of file float128.h.
| x | First value |
| y | Second value |
Definition at line 3757 of file float128.h.
| x | First value |
| y | Second value |
Definition at line 3781 of file float128.h.
The whole point of a fused multiply add is that the product is not rounded before the addend reaches it, which is what makes the result correct to the last bit and what lets a caller recover the error of a multiplication as fma(x, y, -x*y). Writing it as x * y + z rounds twice and loses everything the addend cancels against.
The exact product of the two 113 bit mantissas is 226 bits, and the addend is another 113. Both are placed in a wide accumulator with their leading bits aligned, added or subtracted there, and the single rounding is applied when the result is read back out. An operand too far below the other one to reach the accumulator cannot change any bit of the result except through the tie break, so it is folded into the sticky bit.
| x | The first value to multiply. |
| y | The second value to multiply. |
| z | The value to add to the product. |
Definition at line 5785 of file float128.h.
| x | First value |
| y | Second value |
Definition at line 3799 of file float128.h.
| x | Floating point value |
| iterations | how many Halley to perform, usually 1 is enough |
Definition at line 3958 of file float128.h.
IEEE 754 requires the square root to be correctly rounded, the same as the four arithmetic operations, and Newton's method alone cannot promise that: every step rounds, and the last one can land a unit either side of the answer. It used to, for a quarter of all arguments.
So Newton's method only gets close, and the last bit is then settled exactly. The mantissa m (doubled when the exponent is odd, so that what is left of it halves exactly) is in [2^112, 2^114), which puts the square root of N = m * 2^112 in [2^112, 2^113) - exactly the 113 bit significand the result needs. Squaring a candidate r in integer arithmetic and comparing with N moves it onto floor(sqrt(N)), and the remainder N - r*r then says which way to round: the root is past the halfway point r + 1/2 exactly when the remainder exceeds r, and it can never sit on it, because (r + 1/2)^2 is not an integer.
Based on the book "Math toolkit for real time programming" by Jack W. Crenshaw.
| x | Value to calculate the root of |
| iterations | Ignored. The result is correctly rounded whatever its value; the parameter is kept so that existing calls still compile. |
Definition at line 3872 of file float128.h.
| x | A floating-point value. |
Definition at line 5626 of file float128.h.
Above one the answer is a small difference of two values close to one, so it cannot be reached through erf() - at x = 6 the subtraction would leave 55 of the 113 bits, and past 13 nothing at all. It is computed directly instead, from Laplace's continued fraction
2
-x
e 1
erfc(x) = ---------- * ------------------------
sqrt(pi) x + (1/2)/(x + 1/(x + ...))
evaluated with the modified Lentz algorithm. The fraction converges for every x above one, and fastest where the series form is weakest.
Accuracy, measured against mpmath and pinned by tests/float128_accuracy_gtest.cpp: within 56 ulp. The worst is just above one, 43 ulp at 1.32, where the fraction converges slowest and rounds once per step; from 2 on the error was measured below 28 ulp, and below one, where the answer is 1 - erf(x), below 8.
| x | Input value |
Definition at line 5708 of file float128.h.
Definition at line 5140 of file float128.h.
asin(x) = atan(x / sqrt((1-x)(1+x))). The Newton iteration on sin() this replaced divided by cos(), the derivative, which vanishes at the ends of the domain: it converged slowly there, to about 58 correct bits at asin(1). Near the ends 1-x is exact, so the argument of atan() keeps its accuracy however close to one x gets.
| x | value in the range [-1,1] |
Definition at line 5177 of file float128.h.
acos(x) = 2 * atan(sqrt((1-x)/(1+x))). The Newton iteration this replaced divided by sin(), which is zero at acos(1): that returned a NaN, and so did acos(1 - 2^-100). Near x = 1 the difference 1-x is exact, and near x = -1 the sum 1+x is, so the quotient keeps its accuracy at both ends.
| x | value in the range [-1,1] |
Definition at line 5245 of file float128.h.
| y | value |
| x | value |
Definition at line 5339 of file float128.h.
| x | value in Radians in the range [-0.5pi, 0.5pi] |
Definition at line 5474 of file float128.h.
| x | value in the range [1, inf] |
Definition at line 5498 of file float128.h.
| x | value in the range (-1, 1) |
Definition at line 5569 of file float128.h.
x is written as k*ln2 + r with k an integer and |r| <= ln2/2, which turns the answer into 2^k * exp(r): the power of two is an exact scaling and only the reduced argument goes through a series.
The reduction is where the accuracy of the whole function is decided. ln2 is irrational, so k*ln2 cannot be represented and subtracting a rounded one leaves an error of k ulp - at the top of the range k reaches 16000 and fourteen bits of the answer are gone before the series even starts. Splitting ln2 into a high part whose low 14 bits are zero, so that k times it is exact, and a low part carrying the rest, keeps r accurate to far below its last bit.
The previous implementation raised e to the integer part by repeated squaring, which doubles its own error at every step, and reached the negative half of the range through reciprocal().
| x | A number specifying a power. |
Definition at line 4190 of file float128.h.
| x | A number specifying a power. |
Definition at line 4269 of file float128.h.
The special values follow IEEE 754-2008 9.2.1 throughout. Among those the previous version got wrong: pow(0.5, inf) and pow(2, -inf) were infinite rather than zero, pow(2, NaN) was infinite, pow(-0, 0.5) a NaN, and every integer exponent beyond 11355 in magnitude overflowed whatever the base, so pow(1 + 2^-40, 12000), which is about 1.00000001, came out infinite.
Accuracy, measured against mpmath and pinned by tests/float128_accuracy_gtest.cpp: within 4 + 2.5 |y log2(x)| ulp, which is about 41000 ulp next to the overflow and underflow thresholds, where |y log2(x)| reaches 16383. Both methods below lose about two ulp per unit of that product - the relative error of log(x), or of every squaring, becomes an absolute error of the exponent, and the exponential makes it relative again - and holding it to a few ulp would take an intermediate wider than 113 bits. A y of 1/2 is the square root, correctly rounded.
| x | Base value |
| y | Exponent value |
Definition at line 4359 of file float128.h.
| x | Base value |
| y | Exponent value (integer) |
Definition at line 4333 of file float128.h.
Accurate relative to its own result, which matters most for an argument close to one: the answer is then proportional to x - 1, and that difference is carried through exactly rather than being rounded onto a grid it would barely register on. That holds on both sides of one; below it the exponent is -1, and the answer is formed without the -1 that would cancel. The earlier implementation built the answer as a fixed point fraction of 112 bits before returning it as a float, which left log2(1 + 2^-112) with no correct significant bits at all.
| x | The number to perform log2 on. |
Definition at line 4542 of file float128.h.
| x | The number to perform log on. |
Definition at line 4700 of file float128.h.
| x | The number to perform log on. |
Definition at line 4720 of file float128.h.
| x | The number to perform log on. |
Definition at line 4744 of file float128.h.
| x | Floating-point value. |
| expptr | Receives the exponent: zero for a zero, an infinity or a NaN. |
Definition at line 5923 of file float128.h.
| x | Floating-point value. |
| exp | Integer exponent. |
Definition at line 5948 of file float128.h.
|
constexprnoexcept |
| x | Input value |
Definition at line 5765 of file float128.h.
|
constexprnoexcept |
Definition at line 5971 of file float128.h.
|
constexprnoexcept |
Definition at line 5973 of file float128.h.
|
constexprnoexcept |
Definition at line 5975 of file float128.h.
This and the five predicates below are IEEE 754-2008's quiet comparisons (5.11): a quiet NaN operand is an ordinary unordered answer, a signaling one is the invalid operation. The relational operators are the signaling comparisons instead, which raise invalid for any NaN. The five below decide unorderedness through this function, which is where the signal comes from.
Definition at line 5993 of file float128.h.
Definition at line 6000 of file float128.h.
Definition at line 6005 of file float128.h.
Definition at line 6010 of file float128.h.
Definition at line 6015 of file float128.h.
Definition at line 6020 of file float128.h.
Definition at line 6165 of file float128.h.
Definition at line 6170 of file float128.h.
Definition at line 6181 of file float128.h.
Definition at line 6183 of file float128.h.
A quiet operation, like IEEE 754-2008's nextUp and nextDown that it is built on: stepping from the largest finite value to infinity, or into the subnormal range or onto zero, raises neither overflow nor underflow (C's Annex F would raise them). Only a signaling NaN operand raises an exception, invalid, and is quieted.
| x | Starting value |
| y | Value giving the direction |
Definition at line 6205 of file float128.h.
Definition at line 6214 of file float128.h.
Unlike fmod, whose result keeps the sign of x, this one lands in [-|y|/2, |y|/2].
| x | Dividend |
| y | Divisor |
Definition at line 6305 of file float128.h.
| x | Dividend |
| y | Divisor |
| quo | Receives at least the low three bits of the quotient, with the sign of x/y |
Definition at line 6318 of file float128.h.
| x | First side |
| y | Second side |
| z | Third side |
Definition at line 6365 of file float128.h.
Three regimes:
The result is the logarithm of the absolute value: the gamma function alternates sign between the poles at the non positive integers.
Accuracy, measured against mpmath and pinned by tests/float128_accuracy_gtest.cpp: within 6 ulp for x > 0. For x < 0 within the larger of 6 ulp and an absolute error of 2^-110. The absolute term matters next to the zeros lgamma has on the negative axis - two between each pair of poles from (-3, -2) to (-32, -31), about 1/n! from the poles - where the answer is a difference of terms of order log(n!): relative to a result that vanishes there it is unbounded.
| x | Input value |
Definition at line 6534 of file float128.h.
Accuracy, measured against mpmath and pinned by tests/float128_accuracy_gtest.cpp: exact for the integers up to 38, within 8 ulp for |x| < 32, and within 8 + 4.5 |lgamma(x)| ulp beyond that, which reaches about 51000 ulp at either end of the range.
| x | Input value |
Definition at line 6626 of file float128.h.
Definition at line 6690 of file float128.h.
The order every value takes part in, NaNs included: -NaN < -inf < negative values < -0 < +0 < positive values < +inf < +NaN, a signaling NaN before a quiet one of the same sign, and NaNs of the same kind ordered by payload. It never signals, even for a signaling NaN.
| x | First value |
| y | Second value |
Definition at line 6059 of file float128.h.
Definition at line 6067 of file float128.h.
| x | First value |
| y | Second value |
Definition at line 6079 of file float128.h.
| x | First value |
| y | Second value |
Definition at line 6092 of file float128.h.
|
constexprnoexcept |
Definition at line 6105 of file float128.h.
|
constexprnoexcept |
Definition at line 6107 of file float128.h.
|
constexprnoexcept |
Definition at line 6109 of file float128.h.
|
constexprnoexcept |
Definition at line 6111 of file float128.h.
|
noexcept |
The only one of these that a call cannot reach through argument dependent lookup: its argument is a plain character pointer, so the name has to be visible at namespace scope - and a call has to be qualified as fp128::nan(...) wherever <cmath> puts its own nan(const char*) in scope too.
Definition at line 6124 of file float128.h.
The division itself, which rounds once in the current direction. This used to refine a double seed with Newton iterations, which was a third of the speed of the division (11 M/s against 35 M/s on MSVC, 10.5 against 37 on Clang, P-core, 2026-10-10) and up to an ulp off: a subnormal result was rounded twice, and in a directed rounding mode the result could land on the wrong side.
| x | Input value |
Definition at line 4017 of file float128.h.
|
noexcept |
| x | Input value |
| res | Result of the function |
Definition at line 4029 of file float128.h.
|
noexcept |
The values below 100 come from a table built during compilation, every product rounded to nearest. Filling a function local table on first use, as this used to, was a data race when two threads made the first call together, and under FP128_IEEE_ENV the products took the rounding direction of whichever caller came first.
| x | The number to compute the double factorial of. |
< c[i] is i!!
Definition at line 4109 of file float128.h.
|
inlinenodiscardnoexcept |
The rounding direction float128 arithmetic uses on the calling thread.
Definition at line 411 of file float128.h.
|
inlinenoexcept |
Selects the rounding direction float128 arithmetic uses on the calling thread.
| round | One of FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD or FE_DOWNWARD |
Definition at line 427 of file float128.h.
|
inlinenodiscardnoexcept |
Tests float128 exception flags (IEEE 754 testFlags).
| excepts | FE_ bits to test |
excepts that is raised. Always zero without FP128_IEEE_ENV. Definition at line 452 of file float128.h.
|
inlinenoexcept |
Lowers float128 exception flags (IEEE 754 lowerFlags).
| excepts | FE_ bits to clear |
Definition at line 466 of file float128.h.
|
inlinenoexcept |
Raises float128 exception flags (IEEE 754 raiseFlags).
| excepts | FE_ bits to raise |
Definition at line 479 of file float128.h.
|
inlinenoexcept |
Saves the state of float128 exception flags (IEEE 754 saveAllFlags).
| flagp | Receives the saved state |
| excepts | FE_ bits to save |
Definition at line 495 of file float128.h.
|
inlinenoexcept |
Restores float128 exception flags saved by fegetexceptflag (IEEE 754 restoreFlags).
Only the flags named in excepts are written, each set to the state it had when saved.
| flagp | State saved by fegetexceptflag() |
| excepts | FE_ bits to restore |
Definition at line 511 of file float128.h.
|
inlinenodiscard |
The shortest decimal string that reads back as this exact value.
| value | Value to convert |
Definition at line 7132 of file float128.h.
|
inline |
Converts a float128 to text, in the shape of std::to_chars.
| first | Start of the output range |
| last | One past the end of the output range |
| value | Value to convert |
| fmt | Layout to use. chars_format::hex writes what std::to_chars writes for a double - no 0x prefix and as few exponent digits as needed, 1.8p+1 - which from_chars() with chars_format::hex reads back to the same value. A subnormal is written normalized, 1p-16494 for the smallest one. |
| precision | Digits after the point, or significant digits for general |
Definition at line 7588 of file float128.h.
|
inline |
Converts a float128 to text with the default precision for the layout.
Definition at line 7609 of file float128.h.
|
inline |
Converts a float128 to the shortest text that reads back as the same value.
Definition at line 7615 of file float128.h.
|
inline |
Writes a float128 to a stream, honouring its formatting state.
The stream's precision, its fixed, scientific and hexfloat flags, showpos, showpoint, uppercase, width, fill and adjustfield are all applied, so a float128 behaves in a stream the way a double does.
Definition at line 7633 of file float128.h.
|
inline |
Reads a float128 from a stream.
Accepts what the constructor from a string does: a decimal number with an optional exponent, a hexadecimal one after a 0x prefix with an optional binary exponent (0x1.8p1), and inf, infinity, nan, nan(payload) and snan in any case, each with an optional sign. Leading white space is skipped unless skipws is off.
Like the builtin extractors it reads only as far as the number goes and leaves the character that ends it in the stream, so "1.5,2.5" gives 1.5 and leaves ",2.5" to be read next. It used to read up to the next white space and fail the whole token. A value beyond the range is read as the infinity or zero it rounds to, as the constructor reads it.
Failure - no number at all, or one that stops halfway, such as "1e" or "0x" - leaves the value untouched and sets failbit.
Definition at line 7691 of file float128.h.
|
inlinestaticnoexcept |
Copy a string to lowercase, up to n characters.
| dest | Destination buffer. |
| src | Source string. |
| n | Maximum number of characters to copy. |
Definition at line 968 of file fp128_shared.h.
|
nodiscardconstexpr |
Calculates the element count of a C style array at build time Example: int a[5]; constexpr int len = array_length(a); // returns 5 at build time.
| T | array type |
| a | array instance |
Definition at line 990 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Right-shift a 64-bit value with rounding.
Undefined behavior when shift is outside the range [1, 63].
| x | Value to shift. |
| shift | Number of bits to shift. |
Definition at line 1004 of file fp128_shared.h.
|
inlineconstexprnoexcept |
Right shift a 128 bit unsigned integer (inplace). Limited range, inplace and no parameter checks (checked via assert in debug builds)
| l | Low QWORD |
| h | High QWORD |
| shift | Bits to shift, between 1-63 |
Definition at line 1018 of file fp128_shared.h.
|
inlineconstexprnoexcept |
Left shift a 128 bit integer (inplace). Limited range, inplace and no parameter checks (checked via assert in debug builds)
| l | Low QWORD |
| h | High QWORD |
| shift | Bits to shift, between 1-63 |
Definition at line 1031 of file fp128_shared.h.
|
inlineconstexprnoexcept |
Right shift of an unsigned 128 bit integer (inplace), rounded to nearest with a tie going up.
The magnitude is divided by 2^shift and rounded to the nearest integer; an exact tie rounds up, which for a magnitude is away from zero. That is the rule fixed_point128 applies to every rounded result - the multiply, the divisions and the signed shift below - so a value gets the same answer whichever of them produced it.
Rounding half up needs no sticky bit: the result moves up exactly when the highest dropped bit is set, whatever lies below it, which keeps this free of branches beyond the choice of QWORD.
| l | Low QWORD |
| h | High QWORD |
| shift | Bits to shift, any non negative count. 128 rounds 2^127 and above up to one, more than 128 always leaves zero. |
Definition at line 1103 of file fp128_shared.h.
|
nodiscardconstexprnoexcept |
The shifts by 64 bits or more of shift_right128_inplace_safe_signed().
Kept apart so that the common case stays small: the callers of that function, the shift operators of fixed_point128 among them, are inlined only while it is, and a shift by a constant count then folds down to a handful of instructions. The QWORDs travel by value: a reference would take their address and push the caller's value through memory on the common path as well.
Only the low QWORD of the result is returned. The high one is the sign of the input, or zero when the low QWORD is zero: the quotient fits in 64 bits, and a negative one is never zero there unless rounding carried it all the way up from minus one half.
| l | Low QWORD |
| h | High QWORD, holding the sign in its MSB |
| shift | Bits to shift, 64 or more |
Definition at line 1147 of file fp128_shared.h.
|
inlineconstexprnoexcept |
Arithmetic right shift of a 128 bit two's complement value (inplace), rounded to nearest with a tie going away from zero.
The signed counterpart of shift_right128_inplace_safe: the vacated top bits are filled with copies of the sign bit instead of zeros, so the shift divides by a power of two for negative values as well as positive ones. The result is the quotient rounded to the nearest integer, and an exact tie goes away from zero - the same answer as rounding the magnitude half up and putting the sign back, which is what the multiply and the divisions of fixed_point128 do.
The arithmetic shift itself rounds towards minus infinity. One is added back when the dropped part is above one half, or is exactly one half of a non negative value; the sticky bit, which tells those two apart, is every dropped bit below the highest one.
A shift of 128 or more leaves zero, except that lowest() shifted by exactly 128 is a tie at minus one half and so rounds away from zero, to minus one.
| l | Low QWORD |
| h | High QWORD, holding the sign in its MSB |
| shift | Bits to shift, any non negative count |
Definition at line 1188 of file fp128_shared.h.
|
inlineconstexprnoexcept |
Left shift a 128 bit integer (inplace). Handles any positive shift value.
| l | Low QWORD |
| h | High QWORD |
| shift | Bits to shift, between 1-inf |
Definition at line 1221 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Right shift a 128 bit integer. When shift is a compile time constant, this function generates optimal code for all shift values.
| l | Low QWORD |
| h | High QWORD @ |
Definition at line 1247 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Left shift a 128 bit integer. When shift is a compile time constant, this function generates optimal code for all shift values.
| l | Low QWORD |
| h | High QWORD @ |
Definition at line 1273 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Right shift a 128 bit integer.
| l | Low QWORD |
| h | High QWORD |
| shift | Bits to shift, between 0-127 |
Definition at line 1293 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Right shift a 128 bit integer with rounding.
| l | Low QWORD |
| h | High QWORD |
| shift | Bits to shift, between 0-127 |
Definition at line 1315 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Left shift a 128 bit integer.
| l | Low QWORD |
| h | High QWORD |
| shift | Bits to shift, between 0-127 |
Definition at line 1327 of file fp128_shared.h.
|
inlineconstexprnoexcept |
converts a 128 integer to negative via 2's complement.
| l | Low QWORD (ref) |
| h | High QWORD (ref) |
Definition at line 1345 of file fp128_shared.h.
|
inlinestaticnoexcept |
64 bit words unsigned divide function. Variation of the code from the book Hacker's Delight.
| q | (output) Pointer to receive the quotient. Expected to be initialized to zero |
| r | (output, optional) Pointer to receive the remainder. Can be nullptr |
| u | Pointer to Numerator, an array of uint64_t |
| v | denominator (uint64_t) |
| m | Count of elements in u |
Definition at line 1359 of file fp128_shared.h.
|
inlinestaticnoexcept |
Divides an unsigned integer of up to 256 bits by a 128 bit one. Knuth algorithm D, 64 bit limbs.
This is the divide every 128 bit type in this library reaches once the divisor needs more than 64 bits: int128_t and uint128_t divide 128 by 128, fixed_point128 and float128 divide 256 by 128 because they scale the numerator by 2^128 first to keep the fraction bits of the quotient.
The limb width is the whole point. The same algorithm over 32 bit limbs sees those shapes as m=4,n=4 and m=8,n=4 - up to five main loop passes with a four iteration multiply-subtract in each, where 64 bit limbs need one and three passes of two. The quotient estimate costs one DIV instruction either way. Measured on the 256/128 shape, pinned to a single core, the wider limbs are worth 3.1x under MSVC and 7.9x under clang-cl, and the float128 division benchmarks gain 109% and 382% respectively. Narrow limbs also cost Clang more than they cost MSVC: it used to trail by 1.6x on this division and is the faster of the two toolchains here.
The estimate needs a 128/64 divide, which x86-64 has and AArch64 does not; udiv128() provides it either way, falling back to the compiler runtime helper where there is no instruction.
u cost one wasted pass each but no accuracy, and a divisor whose high word is zero is rejected rather than handled, because that case belongs in div_64bit() which is cheaper again. Every caller here already branches on it.The word count is a template parameter so that every call site gets a division specialized for its own shape - the loops unrolled for that count - whether or not the compiler inlines it. Every caller in the library knows the count when it compiles. As a function argument it only became a constant where the compiler happened to inline the call or specialize it for its call sites, and with callers in three headers sharing one out of line copy, clang++ did neither: that copy cost fixed_point128's long division 15%. div_128bit(q, r, u, v, m) below takes the count at run time.
| m | Count of words in u. Must be 2, 3 or 4. |
| q | (output) Quotient, m - 1 words, q[0] lowest. Only those words are written, so a caller wanting a wider zero filled result must zero the rest itself. |
| r | (output, optional) Remainder, two words. Can be nullptr. |
| u | Numerator, m words, u[0] lowest. |
| v | Denominator, two words. v[1] must be non zero. |
Definition at line 1435 of file fp128_shared.h.
|
inlinestaticnoexcept |
div_128bit() with the word count of the numerator chosen at run time.
Dispatches to the specialization for m. The library's own callers all know the count when they compile and call div_128bit<m>() directly; this form is for a count that is only known at run time.
| q | (output) Quotient, m - 1 words, q[0] lowest. |
| r | (output, optional) Remainder, two words. Can be nullptr. |
| u | Numerator, m words, u[0] lowest. |
| v | Denominator, two words. v[1] must be non zero. |
| m | Count of words in u. Must be 2, 3 or 4. |
m is out of range or a parameter fails a condition of div_128bit<m>(). Definition at line 1526 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Counts the number of 1 bits (population count) in a 128-bit unsigned integer.
| l | Low QWORD |
| h | High QWORD |
Definition at line 1545 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Left zero count 128 bit.
| l | Low QWORD |
| h | High QWORD |
Definition at line 1555 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1.
| l | Lower QWORD of the value |
| h | High QWORD of the value |
Definition at line 1566 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1.
| x | The number to perform log2 on. |
Definition at line 1576 of file fp128_shared.h.
|
inlinenodiscardconstexprnoexcept |
Calculates the Log base 2 of x: log2(x) Rounding is always towards zero so the maximum error is close to 1.
| x | The number to perform log2 on. |
Definition at line 1586 of file fp128_shared.h.
|
inlineconstexprnoexcept |
Upper 128 bits of the product of two 128 bit unsigned values.
Used by the argument reduction in fixed_point128::log2(), which needs a multiplication of two pure fractions and only the leading half of the result. Working on the raw QWORDs rather than on fixed_point128 keeps the reduction independent of that type's template parameter: both operands are values in [0,1) scaled by 2^128 whatever the caller's scaling happens to be.
The lowest QWORD of the 256 bit product is discarded. Nothing is added into that column, so it produces no carry and the result is the exact product truncated - low by less than 2^-256 of a relative unit, which is far below anything the caller can represent.
| a_low | Low QWORD of the first operand. |
| a_high | High QWORD of the first operand. |
| b_low | Low QWORD of the second operand. |
| b_high | High QWORD of the second operand. |
| res_low | Receives the low QWORD of the upper half of the product. |
| res_high | Receives the high QWORD of the upper half of the product. |
Definition at line 1610 of file fp128_shared.h.
|
inline |
Writes a 128 bit integer to a stream, honouring its width, fill and adjustment.
The value is always written in decimal: that is the conversion the type provides, and a stream's hex and oct flags have no counterpart in it.
Definition at line 2136 of file int128_shared.h.
|
inline |
Reads a 128 bit integer from a stream, the way the builtin integer extractors do.
Reads an optional sign and the digits that follow it, and stops at the first character that is not one, leaving it in the stream: "12,34" gives 12 and leaves ",34". The digits are decimal, or hexadecimal after an 0x prefix; with std::hex or std::oct set they are read in that base, the prefix being optional for hex. A minus sign before an unsigned value wraps it around, as strtoull does.
Failure sets failbit, as for a builtin integer: no digits at all - "abc", or "0x" with nothing after it - stores zero, and a value outside the type stores the nearest end of its range, max() or min(). Leading white space is skipped unless skipws is off. This used to read a whole white space delimited token through the string constructor, which never failed: "abc" read as zero and an overflow wrapped silently.
Definition at line 2172 of file int128_shared.h.
|
inlinenodiscard |
User-defined literal for constructing int128_t from a string.
Defined at namespace scope rather than as a friend of the class template: its signature does not depend on the template parameter, so a friend definition inside the template would be emitted once per instantiation and collide. Literal operators are found by ordinary unqualified lookup only, never by ADL, so namespace scope is where they belong anyway.
| literal | Decimal or hexadecimal digits with an optional sign, see the int128_t(const char*) constructor. |
Definition at line 90 of file int128_t.h.
|
inlinenodiscard |
User-defined literal for constructing uint128_t from a string.
Defined at namespace scope rather than as a friend of the class template: its signature does not depend on the template parameter, so a friend definition inside the template would be emitted once per instantiation and collide. Literal operators are found by ordinary unqualified lookup only, never by ADL, so namespace scope is where they belong anyway.
| literal | Decimal or hexadecimal digits, see the uint128_t(const char*) constructor. |
Definition at line 86 of file uint128_t.h.
|
inlineconstexpr |
1 / (n(n+1)) for n = 1..40, with 125 fraction bits rounded to nearest, {high, low}.
The ratio of consecutive terms of the sine and cosine series: the sine term after x^n / n! is the previous one times x^2 / ((n+1)(n+2)), entries 2, 4, 6, ... of this table, and the cosine terms use entries 1, 3, 5, ... Generated with Python fractions (exact), see fixed_point128::TrigSeries().
Definition at line 172 of file fixed_point128.h.
|
staticconstexpr |
Breaking changes to the public interface.
Definition at line 838 of file fp128_shared.h.
|
staticconstexpr |
Backwards compatible additions.
Definition at line 839 of file fp128_shared.h.
|
staticconstexpr |
Fixes that change neither.
Definition at line 840 of file fp128_shared.h.
|
staticconstexpr |
Rebuild of an unchanged source tree.
Definition at line 841 of file fp128_shared.h.
|
staticconstexpr |
All four packed into one comparable integer.
Definition at line 842 of file fp128_shared.h.
|
staticconstexpr |
Dotted form, e.g. "0.9.0.0".
Definition at line 843 of file fp128_shared.h.
|
staticconstexpr |
Mantissa bit count of an IEEE 754 float.
Definition at line 848 of file fp128_shared.h.
|
staticconstexpr |
Exponent bit count of an IEEE 754 float.
Definition at line 849 of file fp128_shared.h.
|
staticconstexpr |
Mantissa bit count of an IEEE 754 double.
Definition at line 850 of file fp128_shared.h.
|
staticconstexpr |
Exponent bit count of an IEEE 754 double.
Definition at line 851 of file fp128_shared.h.
|
inlineconstexpr |
1/(1 + j/64) as a 128 bit fraction, the reciprocals fixed_point128::log2() reduces with.
Entry zero would be exactly one, which is not a 128 bit fraction, so it holds the largest value below one instead. That makes the reduction a near no-op rather than an exact one, and log2_value_table absorbs the difference.
Definition at line 1652 of file fp128_shared.h.
|
inlineconstexpr |
-log2 of the matching log2_recip_table entry, as a 128 bit fraction.
Derived from the reciprocal that is actually stored rather than from 1 + j/64, so that log2(m) = log2(m * recip) - log2(recip) holds exactly whatever the reciprocal rounded to. That is what keeps the argument reduction from contributing any error of its own.
Definition at line 1726 of file fp128_shared.h.
|
inlineconstexpr |
1/(n*ln2) for n = 1 upwards, as a raw 128 bit value with 127 fraction bits; entry [i] holds 1/((i+1)*ln2).
Stored pre-scaled because the series loop reads one entry per iteration, and bringing a value into place from a decimal string every time is out of the question.
The division by ln(2) that turns the natural logarithm into a base two one is folded into these constants rather than applied once at the end. That removes a multiply from every call and, more importantly, the rounding that came with it - which mattered for the instantiations whose own precision is close to the 127 bits the series runs at.
Two readings of the same bits, one per consumer:
Definition at line 1816 of file fp128_shared.h.
|
inlineconstexpr |
Bits of argument reduction fixed_point128::log2() applies; log2_recip_table has 2^this entries.
Definition at line 1844 of file fp128_shared.h.