fixed_point128 1.0.1.0
128 bit integer, fixed point and floating point arithmetic for C++20
Loading...
Searching...
No Matches
fp128::float128 Class Reference

128 bit floating point class. More...

#include <float128.h>

Classes

struct  narrowed
 A finite value rounded to a narrower binary format, see narrow(). More...

Public Member Functions

constexpr float128 () noexcept
 Default constructor, creates an instance with a value of zero.
constexpr float128 (const float128 &rhs) noexcept
 Copy constructor.
constexpr float128 (float128 &&rhs) noexcept
 Move constructor Doesn't modify the right hand side object. Acts like a copy constructor.
constexpr float128 (uint64_t l, uint64_t h) noexcept
 Low level constructor.
constexpr float128 (uint64_t lf, uint64_t hf, uint32_t e, uint32_t s) noexcept
 Low level constructor.
constexpr float128 (double x) noexcept
 Constructor from the double type.
template<typename T>
constexpr float128 (T x) noexcept
 Generic constructor for integral and floating-point types.
 float128 (const char *x) noexcept
 Construct from a string Allows creating very high precision values, approximately 34 decimal digits. Much slower than the other constructors.
 float128 (const std::string &x) noexcept
 Constructor from std::string. Allows creating very high precision values. Much slower than the other constructors.
constexpr ~float128 ()=default
 Destructor.
constexpr float128 & operator= (const float128 &rhs) noexcept
 Assignment operator.
constexpr float128 & operator= (float128 &&rhs) noexcept
 Move assignment operator.
 operator std::string () const noexcept
 Converts to a std::string (slow) string holds all meaningful fraction bits.
 operator char * () const noexcept
 Converts to a C string (slow) string holds all meaningful fraction bits.
void to_e_format (char *str, int32_t buff_size) const
 Writes the value in scientific notation into a caller supplied buffer.
constexpr float128 & operator>>= (int32_t shift) noexcept
 Shift right this object, dividing it by a power of two.
constexpr float128 & operator<<= (int32_t shift) noexcept
 Shift left this object, multiplying it by a power of two.
template<typename T>
constexpr float128 operator>> (T shift) const noexcept
 Performs right shift operation.
template<typename T>
constexpr float128 operator<< (T shift) const noexcept
 Performs left shift operation. Hinted rather than forced, see operator>> above.
constexpr float128 & operator+= (const float128 &rhs) noexcept
 Add a value to this object.
template<typename T>
constexpr float128 & operator+= (const T &rhs)
 Add a value to this object.
constexpr float128 & operator-= (const float128 &rhs) noexcept
 Subtract a value from this object.
template<typename T>
constexpr float128 & operator-= (const T &rhs)
 Subtract a value from this object.
constexpr float128 & operator*= (const float128 &rhs) noexcept
 Multiply a value to this object.
constexpr float128 & square () noexcept
 Squares this object in place.
template<typename T>
constexpr float128 & operator*= (const T &rhs)
 Multiply a value to this object.
float128 & operator/= (const float128 &rhs)
 Divide this object by a value.
template<typename T>
float128 & operator/= (const T &rhs)
 Divide this object by a value.
constexpr operator bool () const noexcept
 Convert to bool.
constexpr bool operator! () const noexcept
 Logical not (!). Opposite of operator bool. Uses is_zero() rather than testing the raw words: negative zero has its sign bit set and would otherwise be reported as a non zero value.
constexpr float128 operator+ () const noexcept
 Unary +. Returns a copy of the object.
constexpr float128 operator- () const noexcept
 Unary -. Returns a copy of the object with sign inverted.
constexpr bool is_positive () const noexcept
 Returns true if the value is positive (including zero and NaN)
constexpr bool is_negative () const noexcept
 Returns true if the value is negative (including zero and NaN).
constexpr bool is_zero () const noexcept
 Returns true if and only if the value is ±0.
constexpr bool is_finite () const noexcept
 Returns true if and only if x is zero, subnormal or normal (not infinite or NaN).
constexpr bool is_subnormal () const noexcept
 Tests if the value is subnormal: non zero, and below the smallest normal magnitude.
constexpr bool is_normal () const noexcept
 Tests if the value is normal (not zero, subnormal, infinite, or NaN)
constexpr bool is_nan () const
 Tests if this value is a NaN.
constexpr bool is_signaling () const
 Tests if this value is a signaling NaN.
constexpr bool is_inf () const
 Tests if this value is an Infinite (negative or positive)
constexpr bool is_exponent_of_2 () const
 Tests if the value is an exponent of 2 (fraction part is zero)
constexpr bool is_special () const
 return true when the value is either an inf or nan
constexpr bool is_int () const
 Returns if the value is an integer (fraction is zero).
constexpr int32_t get_bit (uint32_t bit) const noexcept
 get a specific bit within the float128 data
constexpr float128 get_fraction () const
 Return the fraction part as a float128.
constexpr uint64_t get_exponent_bits () const noexcept
 Reads the biased exponent field out of the high QWORD.
constexpr void set_exponent_bits (uint64_t e) noexcept
 Sets the biased exponent field, leaving the fraction and the sign alone.
constexpr uint64_t get_fraction_bits () const noexcept
 Reads the upper 48 bits of the fraction out of the high QWORD.
constexpr void set_fraction_bits (uint64_t f) noexcept
 Sets the upper 48 bits of the fraction, leaving the exponent and the sign alone.
constexpr void invert_sign () noexcept
 Inverts the sign.
constexpr void set_sign (uint64_t s) noexcept
 Sets the sign.
constexpr uint32_t get_sign () const noexcept
 Gets the sign.
constexpr void get_bits (uint64_t &l, uint64_t &h) const noexcept
 Reads the raw binary128 encoding.
constexpr float128_class_t get_class () const noexcept
 Classifies the float128 value according to IEEE 754.
constexpr int32_t get_exponent () const noexcept
 Returns the exponent of the object - like the base 2 exponent of a floating point A value of 2.1 would return 1, values in the range [0.5,1.0) would return -1.
constexpr void set_exponent (int32_t e) noexcept
 Set the exponent.
constexpr void get_components (uint64_t &l, uint64_t &h, int32_t &e, uint32_t &s) const noexcept
 break the float into its components. Normalizes subnormal values
constexpr bool fraction_to_fixed (uint64_t frac[FIXED_WORDS]) const noexcept
 Expands the fraction part of this value into an exact fixed point number.
Conversion to narrower floating point formats

IEEE 754-2008 5.4.2 convertFormat: the value is rounded once, in the current direction, to the destination's precision and range - including its subnormal range, which keeps fewer bits than its normal one. A NaN keeps its sign and as much of its payload as fits, and is quieted.

template<int32_t P, int32_t EMIN, int32_t EMAX>
constexpr narrowed narrow () const noexcept
 Rounds this finite, non zero value to a narrower binary format.
constexpr uint64_t nan_payload_top (int32_t shift) const noexcept
 The top of a NaN's payload, for a narrower format to carry.
constexpr operator double () const noexcept
 Operator double.
constexpr operator float () const noexcept
 operator float converts to a float
Conversion to integers

The C++ conversions truncate towards zero, which is IEEE 754-2008's convertToIntegerTowardZero. They used to round to nearest for a magnitude of one or more and truncate below it, which agreed with neither: 0.75 became 0 but 1.75 became 2.

A value outside the range of the integer type, or a NaN, is the invalid operation. The result is then defined rather than left undefined as it is for the builtin types: an out of range value saturates at the nearest limit, and a NaN converts to zero.

constexpr uint64_t truncated_magnitude (bool &overflow) const noexcept
 The integer part of |x|, truncated towards zero.
template<typename I>
constexpr I to_integer () const noexcept
 Converts to an integer type, truncating towards zero.
constexpr operator uint64_t () const noexcept
 operator uint64_t converts to a uint64_t, truncating towards zero
constexpr operator int64_t () const noexcept
 operator int64_t converts to a int64_t, truncating towards zero
constexpr operator uint32_t () const noexcept
 operator uint32_t converts to a uint32_t, truncating towards zero
constexpr operator int32_t () const noexcept
 operator int32_t converts to a int32_t, truncating towards zero

Static Public Member Functions

static constexpr uint64_t fixed_mul_extract (uint64_t frac[FIXED_WORDS], uint64_t mul) noexcept
 Multiplies the fixed point fraction by a small value and returns the part that crossed the binary point.
static constexpr bool fixed_is_zero (const uint64_t frac[FIXED_WORDS]) noexcept
 True when every word of the fixed point fraction is zero.
static constexpr uint64_t norm_product (uint64_t &l, uint64_t &h, int32_t &e, bool sticky) noexcept
 Normalizes the product of two mantissas to bit 112, handing back the bits it drops.
static constexpr float128 nextUp (float128 x)
 Produces the closest value larger than x nextUp(x) is the least floating-point number in the format of x that compares greater than x. If x is the negative number of least magnitude in x’s format, nextUp(x) is −0. nextUp(±0) is the positive number of least magnitude in x’s format. nextUp(+∞) is +∞, and nextUp(−∞) is the finite negative number largest in magnitude. When x is NaN, then the result is according to 6.2. nextUp(x) is quiet except for sNaNs.
static constexpr float128 nextDown (float128 x)
 Produces the closest value smaller than x.
static constexpr float128 inf ()
 Return the infinite constant.
static constexpr float128 nan ()
 Return the quiet (non-signaling) NaN constant.
static constexpr float128 signaling_nan ()
 Return a signaling NaN.
static constexpr float128 pi ()
 Return the value of pi.
static constexpr float128 half_pi ()
 Return the value of pi / 2.
static constexpr float128 quarter_pi ()
 Return the value of pi / 4.
static constexpr float128 e ()
 Return the value of e.
static constexpr float128 sqrt_2 () noexcept
 Returns a value of sqrt(2)
static constexpr float128 one () noexcept
 Returns a value of 1.
static constexpr float128 half () noexcept
 Returns a value of 0.5.
static constexpr float128 tenth () noexcept
 Return 0.1 using maximum precision.
static constexpr float128 exp10 (int32_t e) noexcept
 Calculates 10^e.
static constexpr float128 exp10_double_word (int32_t e) noexcept
 The power of exp10(), for an e whose result is inside the range of the format.
static float128 exp10_filtered (int32_t e) noexcept
 exp10_double_word() at run time, raising only the exceptions the result justifies.
static constexpr bool is754version1985 (void)
 Returns false; this type does not conform to IEEE 754-1985.
static constexpr bool is754version2008 (void)
 Whether the type conforms to IEEE 754-2008 (binary128).
static constexpr float128 round_integral (const float128 &x, detail::rounding mode, bool ties_away, bool signal_inexact) noexcept
 Rounds to an integral value (IEEE 754-2008 roundToIntegral, 5.3.1).
template<typename I>
static constexpr I integral_to (const float128 &x) noexcept
 Converts an integral value to an integer type, the way the C rounding functions do.
static constexpr int32_t square_compare (uint64_t rl, uint64_t rh, const uint64_t n[4]) noexcept
 Square of a 113 bit significand compared with a 256 bit value.
static float128 exp_reduced (const float128 &r) noexcept
 e to the power of a reduced argument, |r| <= ln2/2.
static float128 pown_magnitude (const float128 &a, int32_t n) noexcept
 a^n by repeated squaring, for a positive finite a.
static constexpr bool log_special (const float128 &x, float128 &result) noexcept
 The arguments every logarithm treats alike (IEEE 754-2008 9.2.1).
static constexpr float128 log1p_small_limit () noexcept
 Largest |t| the log1p_small() series is used for. Chosen so twelve terms suffice.
static float128 log1p_small (const float128 &t) noexcept
 Natural logarithm of 1+t for |t| <= 2^-4, accurate relative to the result.
static int64_t quadrant_of (const float128 &x) noexcept
 Nearest integer to x / (pi/2), the quadrant the argument falls in.
static int32_t reduce_large (const float128 &x, float128 &hi, float128 &lo) noexcept
 Reduces an argument of any size: x = n * pi/2 + r, with |r| <= pi/4 (Payne and Hanek).
static int32_t reduce_trig (const float128 &x, float128 &hi, float128 &lo) noexcept
 Reduces x to [-pi/4, pi/4], as an unevaluated sum, and returns the quadrant.
static void reduce_half_pi (const float128 &x, int64_t n, float128 &hi, float128 &lo) noexcept
 Subtracts n*(pi/2) from x, leaving the residual as an unevaluated sum.
static constexpr bool is_odd_int (const float128 &x) noexcept
 True when x is an integer whose last place is one, which is the parity a tie needs.
static float128 fmod_magnitude (const float128 &x, const float128 &y, uint64_t &quotient, int32_t &half) noexcept
 |x| mod |y|, along with the low bits of the quotient it implies.
static float128 lgamma_near_two (const float128 &z) noexcept
 lgamma(2 + z) for |z| <= 1/2, accurate relative to the result.
static float128 abs_sin_pi (const float128 &x) noexcept
 |sin(pi * x)| for a finite x that is not an integer, without the error of rounding pi * x.
NaN results
static constexpr float128 quiet_nan (const float128 &x) noexcept
 x with its quiet bit set. The payload, and the sign, are kept.
static constexpr float128 propagate_nan (const float128 &a, const float128 &b) noexcept
 The result of an operation with a NaN operand (IEEE 754-2008 6.2).
static constexpr float128 propagate_nan (const float128 &a, const float128 &b, const float128 &c) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
static constexpr float128 propagate_nan (const float128 &a) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
static constexpr float128 invalid_operation () noexcept
 The result of an invalid operation on non NaN operands, such as inf - inf or 0 / 0.
static constexpr uint32_t exact_zero_sign () noexcept
 The sign of an exact zero sum of two operands of opposite sign (IEEE 754-2008 6.3).
Mathematical constants

The binary128 nearest each constant, given as the encoding rather than parsed from a decimal string: the string constructor is accurate to about an ulp, which is one bit too few to define a constant the rest of the library computes from. The set mirrors <numbers>, so a generic function template written against std::numbers has the same names available here.

static constexpr float128 two_pi () noexcept
 2*pi
static constexpr float128 inv_pi () noexcept
 1/pi
static constexpr float128 inv_sqrt_pi () noexcept
 1/sqrt(pi)
static constexpr float128 ln2 () noexcept
 Natural logarithm of 2.
static constexpr float128 ln10 () noexcept
 Natural logarithm of 10.
static constexpr float128 log2_e () noexcept
 Base 2 logarithm of e.
static constexpr float128 log10_e () noexcept
 Base 10 logarithm of e.
static constexpr float128 log10_2 () noexcept
 Base 10 logarithm of 2.
static constexpr float128 sqrt_3 () noexcept
 Square root of 3.
static constexpr float128 inv_sqrt_3 () noexcept
 1/sqrt(3)
static constexpr float128 egamma () noexcept
 The Euler-Mascheroni constant.
static constexpr float128 phi () noexcept
 The golden ratio.
Unevaluated sums

Calculate the Sine function Ultimately uses sin() with a reduced range of [-pi/4, pi/4]

Parameters
xvalue in Radians
Returns
Sine of x

A value held as a pair of float128 whose sum is the quantity meant, with the second one below the last place of the first. That is 226 bits of significand out of two 113 bit values, which is what the trigonometric argument reduction needs: the residual it produces has to be known to the absolute precision of the result, and near a zero of the sine the result is many orders of magnitude below the argument it came from.

static void two_sum (const float128 &a, const float128 &b, float128 &sum, float128 &err) noexcept
 Exact sum of two values, as a rounded sum and the error it made.
static void quick_two_sum (const float128 &a, const float128 &b, float128 &sum, float128 &err) noexcept
 Exact sum of two values whose magnitudes are ordered.

Static Public Attributes

static constexpr int32_t SCALE_LIMIT = 2 * (EXP_BIAS + FRAC_BITS + 1)
 Largest scaling a shift or ldexp() ever needs: enough to take the largest finite value below half the smallest subnormal, or the smallest subnormal above the largest finite value. Larger counts are clamped to it, which keeps the exponent arithmetic in range.
static constexpr int32_t LOG2_TERMS = (FRAC_BITS + 1 + 8 + log2_reduction_bits - 1) / log2_reduction_bits
 Number of Mercator series terms log2() needs.
static constexpr int32_t FIXED_WORDS = 4
 Normalize the fraction so the msb (unity bit) is on bit 112. The fraction value must contain the unity value The exponent component is adjusted based on the bit shift direction required.
static constexpr int32_t EXP_TERMS = 28
 Calculates the exponent of x: e Using the Maclaurin series expansion, the formula is: 1 2 3 x x x exp(x) = 1 + — + — + — + ... 1! 2! 3!
static constexpr int32_t LOG1P_TERMS = 12
 Terms of the log1p_small() series. |s| stays below 2^-4.9, so s^24 is past the last bit.
static constexpr int32_t COS_TERMS = 16
 Terms of the cosine series cos1() runs. Up to pi/4, x^32/32! is below 2^-125.
static constexpr int32_t STIRLING_TERMS = 16
 Terms of the Stirling series lgamma() runs. Sixteen reach 2^-119 for an argument of 32.
static constexpr int32_t LGAMMA_NEAR_TWO_TERMS = 58
 Terms of the series lgamma_near_two() runs. For |z| <= 1/2 the last is below 2^-120 of the sum.
static constexpr int32_t GAMMA_RECURRENCE_LIMIT = 32
 Below this magnitude lgamma() and tgamma() move the argument next to two by the recurrence.

Friends

class fp128_gtest
template<typename T>
constexpr float128 operator+ (float128 lhs, const T &rhs) noexcept
 Adds 2 values and returns the result.
template<typename T>
constexpr float128 operator- (float128 lhs, const T &rhs) noexcept
 subtracts the right hand side operand to this object to and returns the result.
template<typename T>
constexpr float128 operator* (float128 lhs, const T &rhs) noexcept
 Multiplies the right hand side operand with this object to and returns the result.
template<typename T>
float128 operator/ (float128 lhs, const T &rhs)
 Divides this object by the right hand side operand and returns the result.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr float128 operator+ (const T &lhs, const float128 &rhs) noexcept
 Adds a scalar and a float128, in that order.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr float128 operator- (const T &lhs, const float128 &rhs) noexcept
 Subtracts a float128 from a scalar.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr float128 operator* (const T &lhs, const float128 &rhs) noexcept
 Multiplies a scalar and a float128, in that order.
template<typename T>
requires std::is_arithmetic_v<T>
float128 operator/ (const T &lhs, const float128 &rhs)
 Divides a scalar by a float128.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr float128 operator<< (const T &lhs, const float128 &rhs) noexcept
 Shifts a scalar left by a float128 shift count, scaling it by a power of two.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr float128 operator>> (const T &lhs, const float128 &rhs) noexcept
 Shifts a scalar right by a float128 shift count, scaling it by a power of two. The left operand is widened to float128 first, see operator<< above.
constexpr bool operator== (const float128 &lhs, const float128 &rhs) noexcept
 Compare logical/bitwise equal.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr bool operator== (const float128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr bool operator== (const T &lhs, const float128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator!= (const float128 &lhs, const float128 &rhs) noexcept
 Return true when objects are not equal. Can be used as logical XOR.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr bool operator!= (const float128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
requires std::is_arithmetic_v<T>
constexpr bool operator!= (const T &lhs, const float128 &rhs) noexcept
constexpr bool operator< (const float128 &lhs, const float128 &rhs) noexcept
 Return true if this object is small than the other.
template<typename T>
constexpr bool operator< (const float128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator< (const T &lhs, const float128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator<= (const float128 &lhs, const float128 &rhs) noexcept
 Return true this object is small or equal than the other.
template<typename T>
constexpr bool operator<= (const float128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator<= (const T &lhs, const float128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator> (const float128 &lhs, const float128 &rhs) noexcept
 Return true this object is larger than the other.
template<typename T>
constexpr bool operator> (const float128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator> (const T &lhs, const float128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr bool operator>= (const float128 &lhs, const float128 &rhs) noexcept
 Return true this object is larger or equal than the other.
template<typename T>
constexpr bool operator>= (const float128 &lhs, const T &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
template<typename T>
constexpr bool operator>= (const T &lhs, const float128 &rhs) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
constexpr float128 nan (const float128 &)
 Return the NaN constant.
constexpr bool isnan (const float128 &x)
 Tests if the value is a NaN.
constexpr bool isinf (const float128 &x)
 Tests if the value is an Infinite (negative or positive)
constexpr float128 fabs (const float128 &x)
 Returns the absolute value of x.
constexpr float128 floor (const float128 &x)
 Performs the floor() function, similar to libc's floor(), rounds down towards -infinity.
constexpr float128 ceil (const float128 &x)
 Performs the ceil() function, similar to libc's ceil(), rounds up towards infinity.
constexpr float128 trunc (const float128 &x)
 Rounds towards zero.
constexpr float128 round (const float128 &x)
 Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.
constexpr int64_t llrint (const float128 &x)
 Rounds x to an integer in the current rounding direction and returns it as int64_t.
constexpr int64_t llround (const float128 &x)
 Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.
constexpr int32_t lrint (const float128 &x)
 Rounds x to an integer in the current rounding direction and returns it as int32_t.
constexpr int32_t lround (const float128 &x)
 Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.
constexpr int32_t ilogb (const float128 &x)
 Retrieves an integer that represents the base-2 exponent of the specified value.
constexpr float128 copysign (const float128 &x, const float128 &y)
 returns the value of x with the sign of y.
float128 fmod (const float128 &x, const float128 &y)
 Performs the fmod() function, similar to libc's fmod(), returns the remainder of a division x/root.
constexpr float128 modf (const float128 &x, float128 *iptr)
 Split into integer and fraction parts. Both results carry the sign of the input variable.
constexpr float128 fdim (const float128 &x, const float128 &y)
 Determines the positive difference between the first and second values.
constexpr float128 fmin (const float128 &x, const float128 &y)
 Returns the minimum between x and y.
constexpr float128 fmax (const float128 &x, const float128 &y)
 Returns the maximum between x and y.
float128 hypot (const float128 &x, const float128 &y)
 Calculates the hypotenuse. i.e. sqrt(x^2 + y^2)
constexpr float128 sqr (float128 x) noexcept
 Calculates the square of a value. i.e. x^2.
float128 sqrt (const float128 &x, uint32_t iterations=3)
 Calculates the square root, correctly rounded.
float128 cbrt (const float128 x, uint32_t iterations=2)
 Calculates the cube root. Uses the Halley's method.
float128 reciprocal (const float128 &x)
 Calculates the reciprocal of a value, 1 / x, correctly rounded.
void fact_reciprocal (int x, float128 &res)
 Factorial reciprocal (inverse). Calculates 1 / x! Maximum supported value of x is 50.
float128 double_factorial (int x)
 Returns the double factorial of a number, x!! = x * (x - 2) * (x - 4) * ...
float128 exp (const float128 &x)
 Computes e to the power of x.
float128 exp2 (const float128 &x)
 Computes 2 to the power of x.
float128 expm1 (const float128 &x)
 Calculates the exponent of x and reduces 1 from the result: (e^x) - 1.
float128 pow (const float128 &x, int32_t y)
 Computes x to the power of y (IEEE 754-2008 pown)
float128 pow (const float128 &x, const float128 &y)
 Computes x to the power of y.
float128 log (float128 x)
 Calculates the natural Log (base e) of x: log(x)
float128 log2 (float128 x)
 Calculates the Log base 2 of x: y = log2(x)
float128 log10 (float128 x)
 Calculates Log base 10 of x: log10(x)
float128 logb (float128 x)
 Calculates Log base 2 of x as an integer ignoring the sign of x. Similar to: floor(log2(fabs(x)))
float128 log1p (float128 x)
 Calculates the natural Log (base e) of 1 + x: log(1 + x)
float128 sin1 (float128 x) noexcept
 Calculate the sine function over a limited range [-0.5pi, 0.5pi] Using the Maclaurin series expansion, the formula is: x^3 x^5 x^7 sin(x) = x - — + — - — + ... 3! 5! 7!
float128 cos1 (const float128 &x) noexcept
 Calculate the cosine function over a limited range [-0.5pi, 0.5pi].
float128 sin (float128 x)
float128 asin (float128 x)
 Calculate the inverse sine function.
float128 cos (float128 x)
 Calculate the cosine function Ultimately uses sin1() with a reduced range of [-pi/4, pi/4] Sine's Maclaurin series converges faster than Cosine's.
float128 acos (float128 x)
 Calculate the inverse cosine function.
float128 tan (float128 x)
 Calculate the tangent function tan(x) = sin(x)/cos(x)
float128 atan (float128 x)
 Calculate the inverse tangent function.
float128 atan2 (float128 y, float128 x)
 Calculate the inverse tangent function of the ratio y / x.
float128 sinh (const float128 x)
 Calculate the hyperbolic sine function Use the exponent function which produces more accurate results than the power series. e^x - e^(-x) sinh(x) = ---------— 2.
float128 asinh (const float128 x)
 Calculates the inverse hyperbolic sine For positive x: asinh(x) = log(x + sqrt(x^2 + 1)) For negative x, the function returns the result with the sign inverted.
float128 cosh (const float128 x)
 Calculate the hyperbolic cosine function over a limited range [-0.5pi, 0.5pi] e^x + e^(-x) cosh(x) = ---------— 2.
float128 acosh (const float128 x)
 Calculates the inverse hyperbolic cosine For x >= 1: acosh(x) = log(x + sqrt(x^2 - 1)) For x < 1, the function return zero.
float128 tanh (const float128 x)
 Calculates the hyperbolic tangent e^x - e^(-x) tanh(x) = ---------— e^x + e^(-x)
float128 atanh (const float128 x)
 Calculates the inverse hyperbolic tangent 1 + x atanh(x) = 0.5 * log( --—) 1 - x.
void erf_constants (float128 *a, int32_t count)
 Calculates the Maclaurin series constants for the erf function. The array will hold 1 / (n! * (2n + 1))
float128 erf (float128 x)
 Computes the error function of a value. The erf function return a value in the range -1.0 to 1.0. There's no error return.
float128 erfc (float128 x)
 Computes the complementary error function, 1 - erf(x).
constexpr bool isfinite (const float128 &x)
 Tests whether x is finite (not infinite and not NaN).
constexpr float128 fma (float128 x, float128 y, float128 z)
 Computes (x * y) + z with a single rounding at the end.
constexpr float128 frexp (float128 x, int *expptr)
 Gets the mantissa and exponent of a floating-point number.
constexpr float128 ldexp (float128 x, int exp)
 Multiplies a floating-point number by an integral power of two.
float128 nan (const char *payload)
 A quiet NaN carrying the payload spelled out in the argument.
constexpr float128 nextafter (const float128 &x, const float128 &y)
 The representable value next to x in the direction of y.
constexpr float128 nexttoward (const float128 &x, const float128 &y)
 The neighbour of x towards y. The same as nextafter: there is no wider type to convert from.
float128 remainder (const float128 &x, const float128 &y)
 IEEE 754 remainder: x - n*y with n the quotient rounded to the nearest even integer.
float128 remquo (const float128 &x, const float128 &y, int *quo)
 The IEEE remainder together with the low bits of the quotient.
float128 hypot (const float128 &x, const float128 &y, const float128 &z)
 Length of the diagonal of a box, computed without an intermediate overflow.
float128 lgamma (float128 x)
 Natural logarithm of the absolute value of the gamma function.
float128 tgamma (float128 x)
 The gamma function.
constexpr float128 abs (const float128 &x)
 Absolute value, the name <cmath> gives the floating point overload alongside fabs.
float128 operator""_f128 (const char *literal)
 User-defined literal implementation for constructing float128 from a string.
Classification and comparison

The <cmath> predicates. They are functions rather than the macros the C header defines, in the same way the standard library's own C++ overloads are, so they take part in overload resolution and can be found by argument dependent lookup.

constexpr bool signbit (const float128 &x)
 True when the sign bit is set, including for -0 and for a negative NaN.
constexpr bool isnormal (const float128 &x)
 True when x is neither zero, subnormal, infinite nor NaN.
constexpr int fpclassify (const float128 &x)
 One of FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL or FP_NORMAL.
constexpr bool isunordered (const float128 &x, const float128 &y)
 True when either operand is a NaN, so that the two do not compare in any order.
constexpr bool isgreater (const float128 &x, const float128 &y)
 x > y, false rather than unordered when either is a NaN.
constexpr bool isgreaterequal (const float128 &x, const float128 &y)
 x >= y, false when either is a NaN.
constexpr bool isless (const float128 &x, const float128 &y)
 x < y, false when either is a NaN.
constexpr bool islessequal (const float128 &x, const float128 &y)
 x <= y, false when either is a NaN.
constexpr bool islessgreater (const float128 &x, const float128 &y)
 x < y or x > y, false when either is a NaN.
Rounding to the current mode

Both round in the current rounding direction, which is to nearest with ties to even unless FP128_IEEE_ENV is defined and fesetround() chose another. They differ in one respect: rint is IEEE 754's roundToIntegralExact and raises the inexact exception when the result differs from its argument, nearbyint never does. round() differs from both: it breaks a tie away from zero whatever the rounding direction.

constexpr float128 rint (const float128 &x)
 x rounded to an integral value in the current direction, raising inexact when that changes it.
constexpr float128 nearbyint (const float128 &x)
 x rounded to an integral value in the current direction.
Exponent manipulation
constexpr float128 scalbn (const float128 &x, int n)
 x * 2^n, the same operation ldexp performs.
constexpr float128 scalbln (const float128 &x, long n)
 x * 2^n for a long exponent, clamped to what the format can reach.

IEEE 754-2008 operations beyond <cmath>

Operations clause 5 requires that the C library of the time did not name. They are spelled the way ISO/IEC TS 18661-1, and after it C23, binds them to C.

constexpr bool totalorder (const float128 &x, const float128 &y)
 totalOrder(x, y): true when x precedes y, or equals it, in the IEEE 754 total order.
constexpr bool totalordermag (const float128 &x, const float128 &y)
 totalOrderMag(x, y): totalorder() of the absolute values.
constexpr float128 fmaxmag (const float128 &x, const float128 &y)
 maxNumMag: whichever of x and y has the larger magnitude, or fmax() when they tie.
constexpr float128 fminmag (const float128 &x, const float128 &y)
 minNumMag: whichever of x and y has the smaller magnitude, or fmin() when they tie.
constexpr bool iscanonical (const float128 &x)
 isCanonical: always true, every binary128 encoding is canonical.
constexpr bool issignaling (const float128 &x)
 isSignaling: true for a signaling NaN.
constexpr bool issubnormal (const float128 &x)
 isSubnormal: true for a subnormal value, which zero is not.
constexpr bool iszero (const float128 &x)
 isZero: true for either zero.
constexpr void total_order_key (uint64_t &l, uint64_t &h) const noexcept
 The encoding as a key whose unsigned order is the total order of IEEE 754-2008 5.10.

Rounding

Every operation whose exact result may not be representable ends in round_pack(), which is the one place a result is rounded. IEEE 754 requires each operation to behave as if it first computed the exact result and then rounded it once, to the destination format, in the current rounding direction - and the destination format includes its subnormal range, where fewer than 113 bits are kept. The operations used to round to 113 bits first and then let set_components() round a second time when the result turned out to be subnormal, which gave the wrong last bit for about one subnormal result in ten. They also decided the direction from a window of three bits, which cannot tell a tie from a value just above one when the bits that settle it lie further down.

The shape follows Berkeley SoftFloat's roundPackToF128: the caller hands over a 113 bit significand and a 64 bit word of the bits below it, left aligned, with anything further down jammed into its lowest bit. The top bit of that word is the first bit dropped, and the word being non zero means the result is inexact - which is all any rounding direction needs.

static constexpr uint64_t MAX_SIG_HIGH = FRAC_UNITY | UPPER_FRAC_MASK
 The largest finite magnitude's significand, high QWORD, with its leading one at bit 48.
constexpr void set_components (uint64_t l, uint64_t h, int32_t e, uint32_t s) noexcept
 Stores a significand and exponent, rounding when the value is subnormal.
static constexpr void shift_right_jam128_extra (uint64_t &l, uint64_t &h, uint64_t &extra, int32_t dist) noexcept
 Shifts a significand and its extra word right, keeping a sticky record of what fell off.
static constexpr void shift_right_jam128 (uint64_t &l, uint64_t &h, int32_t dist) noexcept
 Shifts a 128 bit value right, jamming any set bit that falls off into the lowest bit.
static constexpr bool round_increment (detail::rounding mode, uint32_t sign, uint64_t extra) noexcept
 Whether rounding should add one unit in the last place.
static constexpr float128 round_pack_edge (uint32_t sign, int32_t exp, uint64_t l, uint64_t h, uint64_t extra, detail::rounding mode) noexcept
 The slow half of round_pack(): results that overflow or are subnormal.
static constexpr float128 round_pack (uint32_t sign, int32_t e, uint64_t l, uint64_t h, uint64_t extra) noexcept
 Rounds an exact result once, in the current rounding direction, and encodes it.
static constexpr float128 round_pack (uint32_t sign, int32_t e, uint64_t l, uint64_t h, uint64_t extra, detail::rounding mode) noexcept
static constexpr float128 norm_round_pack (uint32_t sign, int32_t e, uint64_t l, uint64_t h, bool sticky) noexcept
 Normalizes an exact non negative 128 bit significand and rounds it once.
static constexpr float128 from_fraction128 (uint64_t low, uint64_t high) noexcept
 Builds a float128 from a 128 bit fraction, a value in [0,1).

Wide accumulator

A fixed 384 bit unsigned integer, used by fma() to hold the exact sum of a 226 bit product and a 113 bit addend before a single rounding is applied to it. The width is what the two together need in the worst case: whichever of them is larger is placed with its leading bit at the top of the accumulator, which leaves 383-225 = 158 bits below the product and 383-112 = 271 bits below the addend. An operand that does not fit under the other one is more than 158 bits smaller than it, so it cannot influence anything but the sticky bit, and is folded into that instead of being placed.

These are implementation details of fma() rather than part of the interface.

static constexpr int32_t WIDE_WORDS = 6
 Words in the accumulator.
static constexpr int32_t WIDE_BITS = WIDE_WORDS * 64
static constexpr int32_t WIDE_TOP = WIDE_BITS - 2
 Bit the leading one of the larger operand is placed on.
static constexpr void mul128to256 (uint64_t al, uint64_t ah, uint64_t bl, uint64_t bh, uint64_t res[4]) noexcept
 Full 256 bit product of two 128 bit values.
static constexpr int32_t wide_msb (const uint64_t *a, int32_t words) noexcept
 Index of the highest set bit of a multi word value, or -1 when it is zero.
static constexpr bool wide_place (uint64_t *acc, const uint64_t *src, int32_t words, int32_t offset) noexcept
 Writes a value into a zeroed accumulator, shifted to a given bit position.
static constexpr void wide_add (uint64_t *a, const uint64_t *b) noexcept
 Adds b into a. The accumulator is wide enough that the sum cannot carry out.
static constexpr void wide_sub (uint64_t *a, const uint64_t *b) noexcept
 Subtracts b from a, which must not be smaller.
static constexpr void wide_dec (uint64_t *a) noexcept
 Subtracts one from the accumulator, which must not be zero.
static constexpr int32_t wide_cmp (const uint64_t *a, const uint64_t *b) noexcept
 Three way comparison of two accumulators.

long double

What a long double is depends on the platform: a plain double under MSVC, the x87 80 bit extended format on x86 under GCC and Clang, and binary128 itself on AArch64 Linux. The two wider ones are converted through their encoding rather than through double, which would round away the bits they have over it.

constexpr operator long double () const noexcept
 operator long double
static constexpr float128 from_long_double (long double x) noexcept
 Converts a long double wider than a double, exactly.

Detailed Description

128 bit floating point class.

This class implements the standard operators a floating point data type.
All of float128's methods are inline for maximum performance.

IEEE 754-2008:

  • Addition, subtraction, multiplication, division, sqrt(), fma(), the remainders, the rounding to an integral value and the conversions to and from double, float, long double, the integer types and text are correctly rounded - each computes the exact result and rounds it once, to the destination's width, its subnormal range included (see round_pack()).
  • Every one of them follows the standard's rules for zeros, infinities and NaNs: the sign of an exact zero sum, the invalid operations, a signaling NaN quieted, the payload of the first NaN operand delivered.
  • Round to nearest, ties to even, is the only rounding direction and no exception flag is kept, unless FP128_IEEE_ENV is defined; with it fp128::fesetround() chooses among the four directions clause 4 requires, and the five flags of clause 7 are raised and tested through fp128::fetestexcept() and its siblings. Only with the macro does the type claim conformance (numeric_limits::is_iec559, is754version2008()).
  • The math functions of clause 9 follow its special values but are not correctly rounded, which the standard recommends rather than requires. Each one's error is measured and stated in tests/float128_accuracy_gtest.cpp.

Implementation notes:

  • Same bit layout as binary128: 112 bit fraction, 15 bit exponent and 1 bit for the sign.
  • A float128 object is not thread safe. Accessing a const object from multiple threads is safe.
  • Only 64 bit builds are supported.

Compile time evaluation:
Everything that stays within the 128 bit encoding is constexpr:

  • Construction from any builtin arithmetic type and from the raw QWORDs, copy, move, assignment and the conversions to the integer and floating point types.
  • Addition, subtraction, multiplication, square(), the shifts, the unary operators and the comparisons.
  • The queries and the component accessors: is_zero(), is_normal(), is_nan(), is_inf(), is_int(), get_exponent(), get_class(), get_components(), set_components() and the rest, along with every named constant (one(), pi(), inf(), nan(), ...).
  • The math functions fabs, floor, ceil, trunc, round, llround, lround, llrint, lrint, ilogb, copysign, modf, fdim, fmin, fmax, fma, frexp, ldexp, sqr, isnan, isinf, isfinite, and the nextUp/nextDown/exp10 helpers.

Two things make that possible. The bit counting and extended arithmetic intrinsics are not constant expressions, so fp128_shared.h wraps each one in a constexpr function that serves a constant evaluated call from a portable implementation of the same operation. And the fields of the high QWORD are read through the shift and mask accessors rather than the _float128_bits view, because reading the inactive member of a union is not allowed during constant evaluation. A runtime call is unaffected by either.

The rest cannot be constexpr, for one of two reasons:

  • Division and modulo, and everything built on them. Nothing in div_128bit itself is barred from a constant expression, so what keeps operator/=() out of one is the surrounding code rather than the long division.
  • The string conversions allocate, and the transcendental functions parse their constants from strings held in function local statics, which a constexpr function may not declare.

Definition at line 671 of file float128.h.

Constructor & Destructor Documentation

◆ float128() [1/9]

fp128::float128::float128 ( )
inlineconstexprnoexcept

Default constructor, creates an instance with a value of zero.

Definition at line 732 of file float128.h.

◆ float128() [2/9]

fp128::float128::float128 ( const float128 & rhs)
inlineconstexprnoexcept

Copy constructor.

Parameters
rhsObject to copy from

Definition at line 737 of file float128.h.

◆ float128() [3/9]

fp128::float128::float128 ( float128 && rhs)
inlineconstexprnoexcept

Move constructor Doesn't modify the right hand side object. Acts like a copy constructor.

Parameters
rhsObject to copy from

Definition at line 743 of file float128.h.

◆ float128() [4/9]

fp128::float128::float128 ( uint64_t l,
uint64_t h )
inlineconstexprnoexcept

Low level constructor.

Parameters
lLow QWORD
hHigh QWORD

Definition at line 749 of file float128.h.

◆ float128() [5/9]

fp128::float128::float128 ( uint64_t lf,
uint64_t hf,
uint32_t e,
uint32_t s )
inlineconstexprnoexcept

Low level constructor.

Parameters
lfLow fraction part (bits 63:0)
hfHigh fraction part (bits 111:64)
eExponent
ssign

Definition at line 757 of file float128.h.

◆ float128() [6/9]

fp128::float128::float128 ( double x)
inlineconstexprnoexcept

Constructor from the double type.

Parameters
xInput value

Definition at line 765 of file float128.h.

◆ float128() [7/9]

template<typename T>
fp128::float128::float128 ( T x)
inlineconstexprnoexcept

Generic constructor for integral and floating-point types.

Every value of every builtin arithmetic type is representable, so the conversion is exact. float goes through double, which holds it exactly. A long double wider than a double - the x87 extended format, or binary128 itself - is read from its encoding, since a double would round it. Integral types are converted directly. Character pointer types delegate to the const char* constructor.

Template Parameters
TSource type (must be arithmetic or a character pointer type)
Parameters
xInput value

Definition at line 828 of file float128.h.

◆ float128() [8/9]

fp128::float128::float128 ( const char * x)
inlinenoexcept

Construct from a string Allows creating very high precision values, approximately 34 decimal digits. Much slower than the other constructors.

Parameters
xInput string

Definition at line 884 of file float128.h.

◆ float128() [9/9]

fp128::float128::float128 ( const std::string & x)
inlinenoexcept

Constructor from std::string. Allows creating very high precision values. Much slower than the other constructors.

Parameters
xInput string

Definition at line 922 of file float128.h.

◆ ~float128()

fp128::float128::~float128 ( )
constexprdefault

Destructor.

Member Function Documentation

◆ operator=() [1/2]

float128 & fp128::float128::operator= ( const float128 & rhs)
inlineconstexprnoexcept

Assignment operator.

Parameters
rhsObject to copy from
Returns
This object.

Definition at line 937 of file float128.h.

◆ operator=() [2/2]

float128 & fp128::float128::operator= ( float128 && rhs)
inlineconstexprnoexcept

Move assignment operator.

Parameters
rhsObject to copy from
Returns
This object.

Definition at line 948 of file float128.h.

◆ narrow()

template<int32_t P, int32_t EMIN, int32_t EMAX>
narrowed fp128::float128::narrow ( ) const
inlinenodiscardconstexprnoexcept

Rounds this finite, non zero value to a narrower binary format.

Overflow produces infinity, or the largest finite value in the two directions that round towards it, and raises the overflow and inexact flags. A result that is tiny after rounding and inexact raises underflow.

Template Parameters
PPrecision of the destination, counting its leading bit whether stored or implicit. At most 64, so the significand fits a single word.
EMINExponent of the destination's smallest normal value
EMAXExponent of its largest finite value
Returns
The rounded significand and the weight of its lowest bit.

Definition at line 990 of file float128.h.

◆ nan_payload_top()

uint64_t fp128::float128::nan_payload_top ( int32_t shift) const
inlinenodiscardconstexprnoexcept

The top of a NaN's payload, for a narrower format to carry.

The payload is the fraction below the quiet bit, bits 110:0. A narrower format keeps its top bits, which is where the conversion from that format puts them, so a payload survives the round trip.

Parameters
shiftFRAC_BITS - 1 less the payload width of the destination, at least 47
Returns
Bits 110:shift of the fraction, right aligned.

Definition at line 1058 of file float128.h.

◆ operator double()

fp128::float128::operator double ( ) const
inlinenodiscardconstexprnoexcept

Operator double.

Correctly rounded in the current direction. Overflow keeps its sign, as does a value too small for the subnormal range, and a NaN keeps its sign and the top 51 bits of its payload.

Definition at line 1070 of file float128.h.

◆ operator float()

fp128::float128::operator float ( ) const
inlinenodiscardconstexprnoexcept

operator float converts to a float

Rounded once, straight to single precision. Going through double, as this used to, rounds twice: 1 + 2^-24 + 2^-82 became 1 + 2^-24 in double, a tie, which then went to 1.0f.

Definition at line 1099 of file float128.h.

◆ truncated_magnitude()

uint64_t fp128::float128::truncated_magnitude ( bool & overflow) const
inlinenodiscardconstexprnoexcept

The integer part of |x|, truncated towards zero.

Parameters
overflowSet when |x| is 2^64 or more, an infinity included
Returns
The truncated magnitude, or UINT64_MAX on overflow.

Definition at line 1141 of file float128.h.

◆ to_integer()

template<typename I>
I fp128::float128::to_integer ( ) const
inlinenodiscardconstexprnoexcept

Converts to an integer type, truncating towards zero.

Template Parameters
IThe integer type
Returns
The converted value, saturated when out of range, zero for a NaN.

Definition at line 1162 of file float128.h.

◆ operator uint64_t()

fp128::float128::operator uint64_t ( ) const
inlinenodiscardconstexprnoexcept

operator uint64_t converts to a uint64_t, truncating towards zero

Definition at line 1198 of file float128.h.

◆ operator int64_t()

fp128::float128::operator int64_t ( ) const
inlinenodiscardconstexprnoexcept

operator int64_t converts to a int64_t, truncating towards zero

Definition at line 1202 of file float128.h.

◆ operator uint32_t()

fp128::float128::operator uint32_t ( ) const
inlinenodiscardconstexprnoexcept

operator uint32_t converts to a uint32_t, truncating towards zero

Definition at line 1206 of file float128.h.

◆ operator int32_t()

fp128::float128::operator int32_t ( ) const
inlinenodiscardconstexprnoexcept

operator int32_t converts to a int32_t, truncating towards zero

Definition at line 1210 of file float128.h.

◆ from_long_double()

constexpr float128 fp128::float128::from_long_double ( long double x)
inlinestaticnodiscardconstexprnoexcept

Converts a long double wider than a double, exactly.

Parameters
xValue to convert
Returns
The same value. A signaling NaN is quieted, the conversion being an operation.

Definition at line 1244 of file float128.h.

◆ operator long double()

fp128::float128::operator long double ( ) const
inlinenodiscardconstexprnoexcept

operator long double

The same as the conversion to double where long double is one. Elsewhere correctly rounded to the wider format, or exact where long double is binary128.

Returns
Object value.

Definition at line 1281 of file float128.h.

◆ operator std::string()

fp128::float128::operator std::string ( ) const
inlinenodiscardnoexcept

Converts to a std::string (slow) string holds all meaningful fraction bits.

Returns
object string representation

Definition at line 1321 of file float128.h.

◆ operator char *()

fp128::float128::operator char * ( ) const
inlineexplicitnodiscardnoexcept

Converts to a C string (slow) string holds all meaningful fraction bits.

Returns
object string representation

Definition at line 1326 of file float128.h.

◆ to_e_format()

void fp128::float128::to_e_format ( char * str,
int32_t buff_size ) const
inline

Writes the value in scientific notation into a caller supplied buffer.

Kept for the callers that predate std::format support. Everything it does is also reachable as fp128::to_chars(..., std::chars_format::scientific) or as std::format("{:e}", value), both of which say what they produce more clearly.

Parameters
strOutput buffer
buff_sizeOutput buffer size in bytes

Definition at line 1349 of file float128.h.

◆ operator>>=()

float128 & fp128::float128::operator>>= ( int32_t shift)
inlineconstexprnoexcept

Shift right this object, dividing it by a power of two.

Parameters
shiftBits to shift. Values less than 1 do nothing, high values can cause the value to reach zero.
Returns
This object.

Definition at line 1374 of file float128.h.

◆ operator<<=()

float128 & fp128::float128::operator<<= ( int32_t shift)
inlineconstexprnoexcept

Shift left this object, multiplying it by a power of two.

Parameters
shiftBits to shift. Values less than 1 do nothing, high values can cause the value to reach infinity.
Returns
This object.

Definition at line 1398 of file float128.h.

◆ operator>>()

template<typename T>
float128 fp128::float128::operator>> ( T shift) const
inlineconstexprnoexcept

Performs right shift operation.

Hinted rather than forced, unlike the other one line forwarders (see FP128_FORCE_INLINE). What it forwards to is not cheap: operator>>= splits the value into components and reassembles it, which brings the subnormal handling of get_components() and set_components() along with it. Forcing this pair open cost ~9% of the float128 Mandelbrot benchmark, which shifts inside a loop that already inlines an add, a multiply and two squares. Left as a hint, the shift is still inlined wherever the caller has room for it.

Parameters
shiftbits to shift
Returns
Temporary object with the result of the operation

Definition at line 1429 of file float128.h.

◆ operator<<()

template<typename T>
float128 fp128::float128::operator<< ( T shift) const
inlineconstexprnoexcept

Performs left shift operation. Hinted rather than forced, see operator>> above.

Parameters
shiftbits to shift
Returns
Temporary object with the result of the operation

Definition at line 1440 of file float128.h.

◆ operator+=() [1/2]

float128 & fp128::float128::operator+= ( const float128 & rhs)
inlineconstexprnoexcept

Add a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1451 of file float128.h.

◆ operator+=() [2/2]

template<typename T>
float128 & fp128::float128::operator+= ( const T & rhs)
inlineconstexpr

Add a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1559 of file float128.h.

◆ operator-=() [1/2]

float128 & fp128::float128::operator-= ( const float128 & rhs)
inlineconstexprnoexcept

Subtract a value from this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1565 of file float128.h.

◆ operator-=() [2/2]

template<typename T>
float128 & fp128::float128::operator-= ( const T & rhs)
inlineconstexpr

Subtract a value from this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1571 of file float128.h.

◆ operator*=() [1/2]

float128 & fp128::float128::operator*= ( const float128 & rhs)
inlineconstexprnoexcept

Multiply a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1577 of file float128.h.

◆ square()

float128 & fp128::float128::square ( )
inlineconstexprnoexcept

Squares this object in place.

Cheaper than operator*=(*this) on several counts:

  • a square is symmetric, so the two cross products of the fraction multiply are the same value and only three 64x64->128 bit multiplies are needed instead of four
  • the result sign is always positive, so no sign combining is needed
  • only one operand has to be tested for the special cases and for being an exponent of 2, and in the exponent of 2 case the fraction does not have to be copied
  • the exponents are added to each other, which is a doubling

The result is identical to (*this) * (*this) for every value, specials included.

Returns
This object.

Definition at line 1684 of file float128.h.

◆ operator*=() [2/2]

template<typename T>
float128 & fp128::float128::operator*= ( const T & rhs)
inlineconstexpr

Multiply a value to this object.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1749 of file float128.h.

◆ operator/=() [1/2]

float128 & fp128::float128::operator/= ( const float128 & rhs)
inline

Divide this object by a value.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1755 of file float128.h.

◆ operator/=() [2/2]

template<typename T>
float128 & fp128::float128::operator/= ( const T & rhs)
inline

Divide this object by a value.

Parameters
rhsRight hand side operand
Returns
This object.

Definition at line 1842 of file float128.h.

◆ operator bool()

fp128::float128::operator bool ( ) const
inlinenodiscardconstexprnoexcept

Convert to bool.

Definition at line 1850 of file float128.h.

◆ operator!()

bool fp128::float128::operator! ( ) const
inlinenodiscardconstexprnoexcept

Logical not (!). Opposite of operator bool. Uses is_zero() rather than testing the raw words: negative zero has its sign bit set and would otherwise be reported as a non zero value.

Definition at line 1856 of file float128.h.

◆ operator+()

float128 fp128::float128::operator+ ( ) const
inlinenodiscardconstexprnoexcept

Unary +. Returns a copy of the object.

Definition at line 1860 of file float128.h.

◆ operator-()

float128 fp128::float128::operator- ( ) const
inlinenodiscardconstexprnoexcept

Unary -. Returns a copy of the object with sign inverted.

Definition at line 1864 of file float128.h.

◆ is_positive()

bool fp128::float128::is_positive ( ) const
inlinenodiscardconstexprnoexcept

Returns true if the value is positive (including zero and NaN)

Returns
True when the sign is 0

Definition at line 1873 of file float128.h.

◆ is_negative()

bool fp128::float128::is_negative ( ) const
inlinenodiscardconstexprnoexcept

Returns true if the value is negative (including zero and NaN).

Returns
True when the sign is 1

Definition at line 1878 of file float128.h.

◆ is_zero()

bool fp128::float128::is_zero ( ) const
inlinenodiscardconstexprnoexcept

Returns true if and only if the value is ±0.

Returns
Returns true if the value is zero

Definition at line 1883 of file float128.h.

◆ is_finite()

bool fp128::float128::is_finite ( ) const
inlinenodiscardconstexprnoexcept

Returns true if and only if x is zero, subnormal or normal (not infinite or NaN).

Returns
True if and only if x is zero, subnormal or normal (not infinite or NaN).

Definition at line 1888 of file float128.h.

◆ is_subnormal()

bool fp128::float128::is_subnormal ( ) const
inlinenodiscardconstexprnoexcept

Tests if the value is subnormal: non zero, and below the smallest normal magnitude.

A zero shares the exponent field of the subnormals but is not one of them, as IEEE 754's isSubnormal has it. The arithmetic itself tests the raw field (see get_components()), where treating a zero alike is harmless and saves the test of the fraction.

Returns
True when the value is subnormal, false for a zero.

Definition at line 1898 of file float128.h.

◆ is_normal()

bool fp128::float128::is_normal ( ) const
inlinenodiscardconstexprnoexcept

Tests if the value is normal (not zero, subnormal, infinite, or NaN)

Returns
True if and only if the value is normal

Definition at line 1903 of file float128.h.

◆ is_nan()

bool fp128::float128::is_nan ( ) const
inlinenodiscardconstexpr

Tests if this value is a NaN.

Returns
True when the value is a NaN

Definition at line 1908 of file float128.h.

◆ is_signaling()

bool fp128::float128::is_signaling ( ) const
inlinenodiscardconstexpr

Tests if this value is a signaling NaN.

Returns
True if this value is a signaling NaN

Definition at line 1917 of file float128.h.

◆ is_inf()

bool fp128::float128::is_inf ( ) const
inlinenodiscardconstexpr

Tests if this value is an Infinite (negative or positive)

Returns
True when the value is an Infinite

Definition at line 1927 of file float128.h.

◆ is_exponent_of_2()

bool fp128::float128::is_exponent_of_2 ( ) const
inlinenodiscardconstexpr

Tests if the value is an exponent of 2 (fraction part is zero)

Returns
True when the value is an exponent of 2

Definition at line 1936 of file float128.h.

◆ is_special()

bool fp128::float128::is_special ( ) const
inlinenodiscardconstexpr

return true when the value is either an inf or nan

Returns
true for inf and nan

Definition at line 1945 of file float128.h.

◆ is_int()

bool fp128::float128::is_int ( ) const
inlinenodiscardconstexpr

Returns if the value is an integer (fraction is zero).

Returns
True when the value is an integer, either zero included; false for an infinity or a NaN.

Definition at line 1954 of file float128.h.

◆ get_bit()

int32_t fp128::float128::get_bit ( uint32_t bit) const
inlinenodiscardconstexprnoexcept

get a specific bit within the float128 data

Parameters
bitbit to get [0,127]
Returns
0 or 1. Undefined when bit > 127

Definition at line 1975 of file float128.h.

◆ get_fraction()

float128 fp128::float128::get_fraction ( ) const
inlinenodiscardconstexpr

Return the fraction part as a float128.

Definition at line 1985 of file float128.h.

◆ get_exponent_bits()

uint64_t fp128::float128::get_exponent_bits ( ) const
inlinenodiscardconstexprnoexcept

Reads the biased exponent field out of the high QWORD.

This accessor and the three below it are the only places that know where the fields sit within the high QWORD, which is the same layout the (l, h, e, s) constructor assembles. They deliberately shift and mask rather than going through the _float128_bits view: that would be a read of the inactive member of a union, which constant evaluation rejects.

Returns
The biased exponent, in [0, 0x7FFF].

Definition at line 2027 of file float128.h.

◆ set_exponent_bits()

void fp128::float128::set_exponent_bits ( uint64_t e)
inlineconstexprnoexcept

Sets the biased exponent field, leaving the fraction and the sign alone.

Parameters
eBiased exponent. Bits above the 15 the field holds are dropped.

Definition at line 2032 of file float128.h.

◆ get_fraction_bits()

uint64_t fp128::float128::get_fraction_bits ( ) const
inlinenodiscardconstexprnoexcept

Reads the upper 48 bits of the fraction out of the high QWORD.

Returns
Bits [111:64] of the fraction.

Definition at line 2037 of file float128.h.

◆ set_fraction_bits()

void fp128::float128::set_fraction_bits ( uint64_t f)
inlineconstexprnoexcept

Sets the upper 48 bits of the fraction, leaving the exponent and the sign alone.

Parameters
fFraction bits. Bits above the 48 the field holds are dropped.

Definition at line 2042 of file float128.h.

◆ invert_sign()

void fp128::float128::invert_sign ( )
inlineconstexprnoexcept

Inverts the sign.

Definition at line 2046 of file float128.h.

◆ set_sign()

void fp128::float128::set_sign ( uint64_t s)
inlineconstexprnoexcept

Sets the sign.

Definition at line 2050 of file float128.h.

◆ get_sign()

uint32_t fp128::float128::get_sign ( ) const
inlinenodiscardconstexprnoexcept

Gets the sign.

Definition at line 2054 of file float128.h.

◆ get_bits()

void fp128::float128::get_bits ( uint64_t & l,
uint64_t & h ) const
inlineconstexprnoexcept

Reads the raw binary128 encoding.

The counterpart of the float128(low, high) constructor: the two together are what a std::bit_cast to and from the storage would be, without needing the layout to be spelled out at the call site. Nothing is interpreted, so a NaN keeps its payload and a subnormal is not normalized - use get_components() for the value the encoding stands for.

Parameters
lReceives the low QWORD (fraction bits 63:0)
hReceives the high QWORD (sign, exponent, fraction bits 111:64)

Definition at line 2066 of file float128.h.

◆ get_class()

float128_class_t fp128::float128::get_class ( ) const
inlinenodiscardconstexprnoexcept

Classifies the float128 value according to IEEE 754.

Returns
The classification category (normal, subnormal, zero, inf, or NaN).

Definition at line 2075 of file float128.h.

◆ get_exponent()

int32_t fp128::float128::get_exponent ( ) const
inlinenodiscardconstexprnoexcept

Returns the exponent of the object - like the base 2 exponent of a floating point A value of 2.1 would return 1, values in the range [0.5,1.0) would return -1.

Returns
Exponent of the number

Definition at line 2097 of file float128.h.

◆ set_exponent()

void fp128::float128::set_exponent ( int32_t e)
inlineconstexprnoexcept

Set the exponent.

Parameters
eExponent value

Definition at line 2102 of file float128.h.

◆ get_components()

void fp128::float128::get_components ( uint64_t & l,
uint64_t & h,
int32_t & e,
uint32_t & s ) const
inlineconstexprnoexcept

break the float into its components. Normalizes subnormal values

Forced open rather than merely offered for inlining, which is an exception to the rule stated on FP128_FORCE_INLINE: this has a body of its own, so it would otherwise be FP128_INLINE. The body is a handful of shifts and masks off the object's own bits, and every arithmetic operator and math function opens with two of these calls, so what the caller gains is not the call itself but the constant folding across it - the exponent and sign arithmetic that follows collapses only once the components are visible. Clang declined the invitation where MSVC accepted it, which is what made this measurable rather than academic: on 2026-08-15 clang-cl left this and the rounding step after the division (then norm_fraction_sticky(), now round_pack(), which is forced open for the same reason) out of line in operator/=, and float128 division measured 13.0 M/s against MSVC's 20.9 M/s.

Parameters
lReference to receive the low fraction
hReference to receive the high fraction
eReference to receive the unbiased exponent
sReference to receive the sign

Definition at line 2129 of file float128.h.

◆ shift_right_jam128_extra()

constexpr void fp128::float128::shift_right_jam128_extra ( uint64_t & l,
uint64_t & h,
uint64_t & extra,
int32_t dist )
inlinestaticconstexprnoexcept

Shifts a significand and its extra word right, keeping a sticky record of what fell off.

The bits shifted out of the significand move into the top of extra, and whatever drops out of extra is jammed into its lowest bit rather than lost, so the word still tells an exact result, a tie and a value above the tie apart.

Parameters
lLow QWORD of the significand
hHigh QWORD of the significand
extraBits below the significand, left aligned
distBits to shift, at least one

Definition at line 2199 of file float128.h.

◆ shift_right_jam128()

constexpr void fp128::float128::shift_right_jam128 ( uint64_t & l,
uint64_t & h,
int32_t dist )
inlinestaticconstexprnoexcept

Shifts a 128 bit value right, jamming any set bit that falls off into the lowest bit.

The alignment step of an addition: the shifted operand only has to be accurate to the bit below the guard bit of the sum, and a jammed lowest bit is enough to say there was more.

Forced open, as are shift_right_jam128_extra() and norm_round_pack(), which addition reaches through. Offered for inlining only, Clang kept all three out of line, the three outputs went through memory on every call, and float128 addition measured 95 M/s against 174 M/s inlined (P-core, 2026-10-05). MSVC inlined them either way.

Parameters
lLow QWORD
hHigh QWORD
distBits to shift, at least one

Definition at line 2243 of file float128.h.

◆ round_increment()

constexpr bool fp128::float128::round_increment ( detail::rounding mode,
uint32_t sign,
uint64_t extra )
inlinestaticnodiscardconstexprnoexcept

Whether rounding should add one unit in the last place.

Parameters
modeRounding direction
signSign of the result
extraBits below the significand, left aligned and jammed
Returns
True when the magnitude has to be rounded up.

Definition at line 2267 of file float128.h.

◆ round_pack_edge()

constexpr float128 fp128::float128::round_pack_edge ( uint32_t sign,
int32_t exp,
uint64_t l,
uint64_t h,
uint64_t extra,
detail::rounding mode )
inlinestaticnodiscardconstexprnoexcept

The slow half of round_pack(): results that overflow or are subnormal.

Parameters
signSign of the result
expBiased exponent less one, as round_pack() computes it
lLow QWORD of the significand
hHigh QWORD of the significand
extraBits below the significand, left aligned and jammed
modeRounding direction
Returns
The rounded result.

Definition at line 2289 of file float128.h.

◆ round_pack() [1/2]

constexpr float128 fp128::float128::round_pack ( uint32_t sign,
int32_t e,
uint64_t l,
uint64_t h,
uint64_t extra )
inlinestaticnodiscardconstexprnoexcept

Rounds an exact result once, in the current rounding direction, and encodes it.

The significand is added to the exponent field rather than merged into it: its leading one sits on the lowest exponent bit, which is why the exponent is passed in less one. That makes the carry of a significand that rounds up to the next power of two land in the exponent on its own, and the carry out of the largest finite value land on infinity.

Parameters
signSign of the result
eUnbiased exponent of bit 112 of the significand
lLow QWORD of the significand
hHigh QWORD of the significand. The leading one must be at bit 48: a zero significand is not accepted, the callers deal with an exact zero themselves.
extraBits below the significand, left aligned, with anything further down jammed into bit 0. Zero when the significand is exact.
Returns
The correctly rounded result, including its subnormal and overflow cases.

Definition at line 2342 of file float128.h.

◆ round_pack() [2/2]

constexpr float128 fp128::float128::round_pack ( uint32_t sign,
int32_t e,
uint64_t l,
uint64_t h,
uint64_t extra,
detail::rounding mode )
inlinestaticnodiscardconstexprnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
signSign of the result
eUnbiased exponent of bit 112 of the significand
lLow QWORD of the significand
hHigh QWORD of the significand, its leading one at bit 48
extraBits below the significand, left aligned, the rest jammed into bit 0
modeRounding direction to use instead of the current one
Returns
The correctly rounded result, including its subnormal and overflow cases.

Definition at line 2354 of file float128.h.

◆ norm_round_pack()

constexpr float128 fp128::float128::norm_round_pack ( uint32_t sign,
int32_t e,
uint64_t l,
uint64_t h,
bool sticky )
inlinestaticnodiscardconstexprnoexcept

Normalizes an exact non negative 128 bit significand and rounds it once.

For results whose leading one can be anywhere: a sum after cancellation, a remainder, a fraction. The value is (h:l) * 2^(e - 112).

Parameters
signSign of the result
eExponent the value would have if its leading one were at bit 112
lLow QWORD
hHigh QWORD
stickyTrue when set bits below (h:l) were already dropped
Returns
The correctly rounded result.

Definition at line 2388 of file float128.h.

◆ from_fraction128()

constexpr float128 fp128::float128::from_fraction128 ( uint64_t low,
uint64_t high )
inlinestaticnodiscardconstexprnoexcept

Builds a float128 from a 128 bit fraction, a value in [0,1).

The counterpart of reading a mantissa out with get_components(): log2() does its argument reduction and series on plain 128 bit fractions, and this is how the two results it ends up with re-enter floating point.

Parameters
lowLow QWORD of the fraction.
highHigh QWORD of the fraction.
Returns
The value the fraction stands for, correctly rounded, or zero if it is zero.

Definition at line 2414 of file float128.h.

◆ set_components()

void fp128::float128::set_components ( uint64_t l,
uint64_t h,
int32_t e,
uint32_t s )
inlineconstexprnoexcept

Stores a significand and exponent, rounding when the value is subnormal.

Parameters
lLow part of the significand
hHigh part of the significand, which must have its leading one at bit 48 (bit 112 of the whole) unless the significand is zero
eUnbiased exponent, can be any value: one past the format's range produces an infinity, one below it a subnormal or a zero, rounded once in the current direction.
sSign (1 is negative)

Definition at line 2430 of file float128.h.

◆ quiet_nan()

constexpr float128 fp128::float128::quiet_nan ( const float128 & x)
inlinestaticnodiscardconstexprnoexcept

x with its quiet bit set. The payload, and the sign, are kept.

Definition at line 2445 of file float128.h.

◆ propagate_nan() [1/3]

constexpr float128 fp128::float128::propagate_nan ( const float128 & a,
const float128 & b )
inlinestaticnodiscardconstexprnoexcept

The result of an operation with a NaN operand (IEEE 754-2008 6.2).

A signaling NaN operand is the invalid operation. Either way the result is a quiet NaN, and the standard recommends it be one of the inputs so that a payload survives the computation: the first NaN operand is returned, quieted.

Parameters
aFirst operand
bSecond operand
Returns
The quiet NaN to deliver.

Definition at line 2458 of file float128.h.

◆ propagate_nan() [2/3]

constexpr float128 fp128::float128::propagate_nan ( const float128 & a,
const float128 & b,
const float128 & c )
inlinestaticnodiscardconstexprnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2465 of file float128.h.

◆ propagate_nan() [3/3]

constexpr float128 fp128::float128::propagate_nan ( const float128 & a)
inlinestaticnodiscardconstexprnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 2472 of file float128.h.

◆ invalid_operation()

constexpr float128 fp128::float128::invalid_operation ( )
inlinestaticnodiscardconstexprnoexcept

The result of an invalid operation on non NaN operands, such as inf - inf or 0 / 0.

Returns
The default quiet NaN, after raising the invalid flag.

Definition at line 2483 of file float128.h.

◆ exact_zero_sign()

constexpr uint32_t fp128::float128::exact_zero_sign ( )
inlinestaticnodiscardconstexprnoexcept

The sign of an exact zero sum of two operands of opposite sign (IEEE 754-2008 6.3).

Returns
1 (negative) when rounding downward, 0 in every other direction.

Definition at line 2493 of file float128.h.

◆ fraction_to_fixed()

bool fp128::float128::fraction_to_fixed ( uint64_t frac[FIXED_WORDS]) const
inlinenodiscardconstexprnoexcept

Expands the fraction part of this value into an exact fixed point number.

The result is the fraction scaled by 2^256 and held in four words, frac[0] being the least significant. Nothing is lost: a fraction bit of a binary128 has weight 2^(e-112) with e no smaller than -111 on the path that calls this, so scaling by 2^256 always lands on an integer, and the fraction is below one so the product stays under 2^256.

Printing the digits from this expansion is exact. Deriving them by repeatedly multiplying a float128 by 100000 instead, as this used to, rounds once per group and the error compounds over the eight or so groups that a full precision value needs.

Parameters
fracOutput, four words holding the fraction scaled by 2^256
Returns
True when a fraction exists, false when the value is an integer

Definition at line 2524 of file float128.h.

◆ fixed_mul_extract()

constexpr uint64_t fp128::float128::fixed_mul_extract ( uint64_t frac[FIXED_WORDS],
uint64_t mul )
inlinestaticnodiscardconstexprnoexcept

Multiplies the fixed point fraction by a small value and returns the part that crossed the binary point.

Parameters
fracFraction scaled by 2^256, updated in place to hold the new fraction
mulMultiplier, small enough that the result stays below 2^64
Returns
The integer part produced by the multiply, which is the next group of digits

Definition at line 2579 of file float128.h.

◆ fixed_is_zero()

constexpr bool fp128::float128::fixed_is_zero ( const uint64_t frac[FIXED_WORDS])
inlinestaticnodiscardconstexprnoexcept

True when every word of the fixed point fraction is zero.

Definition at line 2592 of file float128.h.

◆ mul128to256()

constexpr void fp128::float128::mul128to256 ( uint64_t al,
uint64_t ah,
uint64_t bl,
uint64_t bh,
uint64_t res[4] )
inlinestaticconstexprnoexcept

Full 256 bit product of two 128 bit values.

Parameters
alLow QWORD of the first operand
ahHigh QWORD of the first operand
blLow QWORD of the second operand
bhHigh QWORD of the second operand
resOutput, four words with res[0] the least significant

Definition at line 2630 of file float128.h.

◆ wide_msb()

constexpr int32_t fp128::float128::wide_msb ( const uint64_t * a,
int32_t words )
inlinestaticnodiscardconstexprnoexcept

Index of the highest set bit of a multi word value, or -1 when it is zero.

Parameters
aWords, a[0] the least significant
wordsWord count

Definition at line 2658 of file float128.h.

◆ wide_place()

constexpr bool fp128::float128::wide_place ( uint64_t * acc,
const uint64_t * src,
int32_t words,
int32_t offset )
inlinestaticnodiscardconstexprnoexcept

Writes a value into a zeroed accumulator, shifted to a given bit position.

A negative offset puts part or all of the value below the accumulator's least significant bit. Those bits are not representable there but still decide the rounding, so whether any of them was set is reported back rather than dropped.

Parameters
accAccumulator, must be zero on entry
srcValue to place, src[0] the least significant word
wordsWord count of src
offsetBit position the least significant bit of src lands on, may be negative
Returns
True when a set bit fell below the accumulator.

Definition at line 2680 of file float128.h.

◆ wide_add()

constexpr void fp128::float128::wide_add ( uint64_t * a,
const uint64_t * b )
inlinestaticconstexprnoexcept

Adds b into a. The accumulator is wide enough that the sum cannot carry out.

Definition at line 2723 of file float128.h.

◆ wide_sub()

constexpr void fp128::float128::wide_sub ( uint64_t * a,
const uint64_t * b )
inlinestaticconstexprnoexcept

Subtracts b from a, which must not be smaller.

Definition at line 2731 of file float128.h.

◆ wide_dec()

constexpr void fp128::float128::wide_dec ( uint64_t * a)
inlinestaticconstexprnoexcept

Subtracts one from the accumulator, which must not be zero.

Definition at line 2743 of file float128.h.

◆ wide_cmp()

constexpr int32_t fp128::float128::wide_cmp ( const uint64_t * a,
const uint64_t * b )
inlinestaticnodiscardconstexprnoexcept

Three way comparison of two accumulators.

Definition at line 2752 of file float128.h.

◆ norm_product()

constexpr uint64_t fp128::float128::norm_product ( uint64_t & l,
uint64_t & h,
int32_t & e,
bool sticky )
inlinestaticnodiscardconstexprnoexcept

Normalizes the product of two mantissas to bit 112, handing back the bits it drops.

The multiply and the square both arrive here with (h:l) equal to their 256 bit product shifted right by 111. Both operands were normalized to [2^112, 2^113), so the product is in [2^224, 2^226) and (h:l) is in [2^113, 2^115): its leading one is at bit 113 or bit 114 and nowhere else, which makes the remaining shift 1 or 2 and the choice a single bit test.

Nothing is rounded here. The bits shifted out, and the sticky bit for everything the caller dropped before them, are returned in the form round_pack() takes, so that the result is rounded once and to its final width - which for a subnormal product is narrower than 113 bits.

Parameters
lLow part of the shifted product, replaced by the normalized fraction
hHigh part of the shifted product, replaced by the normalized fraction
eUnbiased exponent, adjusted to match the normalized fraction
stickyTrue when the caller already dropped one or more set bits below l
Returns
The extra word for round_pack(): the dropped bits left aligned, the sticky bit in bit 0.

Definition at line 2779 of file float128.h.

◆ nextUp()

constexpr float128 fp128::float128::nextUp ( float128 x)
inlinestaticnodiscardconstexpr

Produces the closest value larger than x nextUp(x) is the least floating-point number in the format of x that compares greater than x. If x is the negative number of least magnitude in x’s format, nextUp(x) is −0. nextUp(±0) is the positive number of least magnitude in x’s format. nextUp(+∞) is +∞, and nextUp(−∞) is the finite negative number largest in magnitude. When x is NaN, then the result is according to 6.2. nextUp(x) is quiet except for sNaNs.

Parameters
xSource value
Returns
Higher value closest to x

Definition at line 2800 of file float128.h.

◆ nextDown()

constexpr float128 fp128::float128::nextDown ( float128 x)
inlinestaticnodiscardconstexpr

Produces the closest value smaller than x.

Parameters
xSource value
Returns
Lower value closest to x

Definition at line 2837 of file float128.h.

◆ inf()

constexpr float128 fp128::float128::inf ( )
inlinestaticnodiscardconstexpr

Return the infinite constant.

Returns
INF

Definition at line 2874 of file float128.h.

◆ nan()

constexpr float128 fp128::float128::nan ( )
inlinestaticnodiscardconstexpr

Return the quiet (non-signaling) NaN constant.

Returns
NaN

Definition at line 2879 of file float128.h.

◆ signaling_nan()

constexpr float128 fp128::float128::signaling_nan ( )
inlinestaticnodiscardconstexpr

Return a signaling NaN.

Nothing in the library raises an exception on encountering one - there is no floating point status to raise it in - but the encoding is distinguishable, which is what numeric_limits::signaling_NaN() and is_signaling() need.

Returns
A signaling NaN

Definition at line 2888 of file float128.h.

◆ pi()

constexpr float128 fp128::float128::pi ( )
inlinestaticnodiscardconstexpr

Return the value of pi.

Returns
pi

Definition at line 2893 of file float128.h.

◆ half_pi()

constexpr float128 fp128::float128::half_pi ( )
inlinestaticnodiscardconstexpr

Return the value of pi / 2.

Returns
pi / 2

Definition at line 2898 of file float128.h.

◆ quarter_pi()

constexpr float128 fp128::float128::quarter_pi ( )
inlinestaticnodiscardconstexpr

Return the value of pi / 4.

Returns
pi / 4

Definition at line 2903 of file float128.h.

◆ e()

constexpr float128 fp128::float128::e ( )
inlinestaticnodiscardconstexpr

Return the value of e.

Returns
e

Definition at line 2908 of file float128.h.

◆ sqrt_2()

constexpr float128 fp128::float128::sqrt_2 ( )
inlinestaticnodiscardconstexprnoexcept

Returns a value of sqrt(2)

Returns

Definition at line 2918 of file float128.h.

◆ one()

constexpr float128 fp128::float128::one ( )
inlinestaticnodiscardconstexprnoexcept

Returns a value of 1.

Returns
1

Definition at line 2928 of file float128.h.

◆ half()

constexpr float128 fp128::float128::half ( )
inlinestaticnodiscardconstexprnoexcept

Returns a value of 0.5.

Returns
0.5

Definition at line 2933 of file float128.h.

◆ tenth()

constexpr float128 fp128::float128::tenth ( )
inlinestaticnodiscardconstexprnoexcept

Return 0.1 using maximum precision.

Returns

Definition at line 2938 of file float128.h.

◆ two_pi()

constexpr float128 fp128::float128::two_pi ( )
inlinestaticnodiscardconstexprnoexcept

2*pi

Definition at line 2954 of file float128.h.

◆ inv_pi()

constexpr float128 fp128::float128::inv_pi ( )
inlinestaticnodiscardconstexprnoexcept

1/pi

Definition at line 2956 of file float128.h.

◆ inv_sqrt_pi()

constexpr float128 fp128::float128::inv_sqrt_pi ( )
inlinestaticnodiscardconstexprnoexcept

1/sqrt(pi)

Definition at line 2958 of file float128.h.

◆ ln2()

constexpr float128 fp128::float128::ln2 ( )
inlinestaticnodiscardconstexprnoexcept

Natural logarithm of 2.

Definition at line 2960 of file float128.h.

◆ ln10()

constexpr float128 fp128::float128::ln10 ( )
inlinestaticnodiscardconstexprnoexcept

Natural logarithm of 10.

Definition at line 2962 of file float128.h.

◆ log2_e()

constexpr float128 fp128::float128::log2_e ( )
inlinestaticnodiscardconstexprnoexcept

Base 2 logarithm of e.

Definition at line 2964 of file float128.h.

◆ log10_e()

constexpr float128 fp128::float128::log10_e ( )
inlinestaticnodiscardconstexprnoexcept

Base 10 logarithm of e.

Definition at line 2966 of file float128.h.

◆ log10_2()

constexpr float128 fp128::float128::log10_2 ( )
inlinestaticnodiscardconstexprnoexcept

Base 10 logarithm of 2.

Definition at line 2968 of file float128.h.

◆ sqrt_3()

constexpr float128 fp128::float128::sqrt_3 ( )
inlinestaticnodiscardconstexprnoexcept

Square root of 3.

Definition at line 2970 of file float128.h.

◆ inv_sqrt_3()

constexpr float128 fp128::float128::inv_sqrt_3 ( )
inlinestaticnodiscardconstexprnoexcept

1/sqrt(3)

Definition at line 2972 of file float128.h.

◆ egamma()

constexpr float128 fp128::float128::egamma ( )
inlinestaticnodiscardconstexprnoexcept

The Euler-Mascheroni constant.

Definition at line 2974 of file float128.h.

◆ phi()

constexpr float128 fp128::float128::phi ( )
inlinestaticnodiscardconstexprnoexcept

The golden ratio.

Definition at line 2976 of file float128.h.

◆ exp10()

constexpr float128 fp128::float128::exp10 ( int32_t e)
inlinestaticnodiscardconstexprnoexcept

Calculates 10^e.

Exact for 0 <= e <= 48, where 10^e is representable. Otherwise the power is formed in double word arithmetic - a pair (hi, lo) whose sum carries about 226 bits, the error of each product recovered exactly with fma() - and rounded once, at the end, in the current direction.

Measured against exact rationals for every e from -4970 to 4935 in all four rounding directions (2026-10-10): correctly rounded, with exactly the exception flags of a single rounding, everywhere except exp10(-4931) rounding upward, which is one ulp high. Close to the subnormal range the cross terms of the last products fall below the subnormal grid and are rounded there, which can cost the last bit; the bound is 1 ulp in every direction.

The previous version squared and multiplied the rounded 0.1 itself, carrying its error and every rounding along: exp10(-100) was 66 ulp off and exp10(-4000) 1809, and the directed modes drifted further still, up to 3453 ulp.

Parameters
eInteger exponent. Beyond the range of the format the result overflows (e > 4932) or underflows (e < -4965) as rounding the exact power would, in the current direction and with the same exception flags.
Returns
10^e

Definition at line 3000 of file float128.h.

◆ exp10_double_word()

constexpr float128 fp128::float128::exp10_double_word ( int32_t e)
inlinestaticnodiscardconstexprnoexcept

The power of exp10(), for an e whose result is inside the range of the format.

Binary exponentiation on (hi, lo) pairs. The product of two pairs is hi * bh, whose rounding error fma() recovers exactly, plus the cross terms, which are 2^-113 of it and are rounded; the pair is then renormalized so that lo stays below an ulp of hi.

Parameters
eExponent, with 10^e inside the range of the format
Returns
10^e, rounded once in the current direction.

Definition at line 3023 of file float128.h.

◆ exp10_filtered()

float128 fp128::float128::exp10_filtered ( int32_t e)
inlinestaticnodiscardnoexcept

exp10_double_word() at run time, raising only the exceptions the result justifies.

The cross terms of the products can underflow while the power itself does not.

Parameters
eExponent, with 10^e inside the range of the format
Returns
10^e

Definition at line 3061 of file float128.h.

◆ is754version1985()

constexpr bool fp128::float128::is754version1985 ( void )
inlinestaticconstexpr

Returns false; this type does not conform to IEEE 754-1985.

Definition at line 3068 of file float128.h.

◆ is754version2008()

constexpr bool fp128::float128::is754version2008 ( void )
inlinestaticconstexpr

Whether the type conforms to IEEE 754-2008 (binary128).

The arithmetic, the conversions and the operations of clause 5 conform in every build, but the standard also requires the rounding-direction attributes of clause 4 and the status flags of clause 7, which exist only when FP128_IEEE_ENV is defined.

Returns
True when the program is built with FP128_IEEE_ENV, false otherwise.

Definition at line 3078 of file float128.h.

◆ round_integral()

constexpr float128 fp128::float128::round_integral ( const float128 & x,
detail::rounding mode,
bool ties_away,
bool signal_inexact )
inlinestaticnodiscardconstexprnoexcept

Rounds to an integral value (IEEE 754-2008 roundToIntegral, 5.3.1).

The shared engine of floor, ceil, trunc, round, rint and nearbyint, which differ only in the direction they round. It works on the encoding: the fraction bits below the units bit are cleared, and when the value has to move up one unit is added at the units bit, a carry out of the fraction landing in the exponent on its own. Nothing is subtracted, so nothing rounds, and the sign is left alone - a negative value that rounds to zero gives -0.

The previous implementations subtracted the fraction from the value, which made the zero results of a negative argument positive, and round() added one half and truncated, which rounded the largest value below one half up to one and 2^112 + 1 to 2^112 + 2.

Parameters
xValue to round
modeDirection, ignored when ties_away is set
ties_awayRound to nearest with ties away from zero instead, as round() does
signal_inexactRaise the inexact exception when the result differs from x, which distinguishes roundToIntegralExact (rint) from the other five
Returns
The integral value. A NaN comes back quieted, an infinity unchanged.

Definition at line 3448 of file float128.h.

◆ integral_to()

template<typename I>
constexpr I fp128::float128::integral_to ( const float128 & x)
inlinestaticnodiscardconstexprnoexcept

Converts an integral value to an integer type, the way the C rounding functions do.

Template Parameters
IThe integer type
Parameters
xAn integral value, an infinity or a NaN
Returns
The value, or zero when it is a NaN or out of range for the type, which is the invalid operation.

Definition at line 3522 of file float128.h.

◆ square_compare()

constexpr int32_t fp128::float128::square_compare ( uint64_t rl,
uint64_t rh,
const uint64_t n[4] )
inlinestaticnodiscardconstexprnoexcept

Square of a 113 bit significand compared with a 256 bit value.

Parameters
rlLow QWORD of the significand
rhHigh QWORD of the significand
nThe value to compare against, n[0] the least significant word
Returns
Negative, zero or positive as r*r is below, equal to or above n.

Definition at line 3838 of file float128.h.

◆ exp_reduced()

float128 fp128::float128::exp_reduced ( const float128 & r)
inlinestaticnodiscardnoexcept

e to the power of a reduced argument, |r| <= ln2/2.

Parameters
rReduced argument
Returns
exp(r)

Definition at line 4160 of file float128.h.

◆ pown_magnitude()

float128 fp128::float128::pown_magnitude ( const float128 & a,
int32_t n )
inlinestaticnodiscardnoexcept

a^n by repeated squaring, for a positive finite a.

A negative exponent inverts the power, unless the power overflowed or underflowed on its way there while its reciprocal would not have: then exp() and log() reach it directly.

Parameters
aBase, positive and finite
nExponent
Returns
a^n.

Definition at line 4307 of file float128.h.

◆ log_special()

constexpr bool fp128::float128::log_special ( const float128 & x,
float128 & result )
inlinestaticnodiscardconstexprnoexcept

The arguments every logarithm treats alike (IEEE 754-2008 9.2.1).

A NaN propagates, a zero is a pole - the division by zero exception, and -inf - any other negative argument is the invalid operation, and +inf is its own logarithm. log2() used to return -inf for a negative argument, a finite value for +inf, and a finite value for a NaN.

Parameters
xArgument
resultReceives the answer when there is one
Returns
True when x was one of these and result holds the answer.

Definition at line 4435 of file float128.h.

◆ log1p_small_limit()

constexpr float128 fp128::float128::log1p_small_limit ( )
inlinestaticnodiscardconstexprnoexcept

Largest |t| the log1p_small() series is used for. Chosen so twelve terms suffice.

Definition at line 4458 of file float128.h.

◆ log1p_small()

float128 fp128::float128::log1p_small ( const float128 & t)
inlinestaticnodiscardnoexcept

Natural logarithm of 1+t for |t| <= 2^-4, accurate relative to the result.

log(1+t) = 2 * atanh(t / (2+t)). The substitution replaces the Mercator series in t, which would need a hundred terms at this precision, with one in s^2 where |s| < 2^-4.9, so twelve terms reach the last bit of the mantissa.

What matters more than the term count is that the result stays accurate relative to its own size. Near one the logarithm is proportional to t, and t reaches this function exactly - the subtraction that produced it cancelled two values within a factor of two of each other, which is exact. Computing the same thing as a difference of two numbers near one loses a bit for every power of two the argument sits closer to one: at t = 2^-60 barely half the mantissa survives.

Parameters
tOffset from one, must satisfy |t| <= 2^-4
Returns
log(1+t)

Definition at line 4479 of file float128.h.

◆ two_sum()

void fp128::float128::two_sum ( const float128 & a,
const float128 & b,
float128 & sum,
float128 & err )
inlinestaticnoexcept

Exact sum of two values, as a rounded sum and the error it made.

Dekker's algorithm. Nothing here is an approximation: a + b is exactly sum + err for any two finite operands.

Parameters
aFirst operand
bSecond operand
sumReceives the rounded sum
errReceives a + b - sum, exactly

Definition at line 4884 of file float128.h.

◆ quick_two_sum()

void fp128::float128::quick_two_sum ( const float128 & a,
const float128 & b,
float128 & sum,
float128 & err )
inlinestaticnoexcept

Exact sum of two values whose magnitudes are ordered.

Parameters
aFirst operand, must not be smaller in magnitude than b
bSecond operand
sumReceives the rounded sum
errReceives a + b - sum, exactly

Definition at line 4898 of file float128.h.

◆ quadrant_of()

int64_t fp128::float128::quadrant_of ( const float128 & x)
inlinestaticnodiscardnoexcept

Nearest integer to x / (pi/2), the quadrant the argument falls in.

Beyond 2^62 the quadrant no longer fits in the integer the reduction counts with, and an argument that large has fewer bits below the binary point than a quadrant is wide - its sine is not determined by the value in any useful sense. Everything up to there reduces exactly, see reduce_half_pi().

Parameters
xArgument, below 2^60 in magnitude; reduce_large() takes the larger ones
Returns
Quadrant index, zero for an argument too large to reduce.

Definition at line 4916 of file float128.h.

◆ reduce_large()

int32_t fp128::float128::reduce_large ( const float128 & x,
float128 & hi,
float128 & lo )
inlinestaticnoexcept

Reduces an argument of any size: x = n * pi/2 + r, with |r| <= pi/4 (Payne and Hanek).

Subtracting multiples of a stored pi/2, as reduce_half_pi() does, needs the multiple n as an integer and pi/2 to as many bits as n has plus the 226 the residual keeps - out of reach once n runs to thousands of bits. Here x * 2/pi is formed instead, in integer arithmetic, from the 113 bit mantissa and a 384 bit window of the binary expansion of 2/pi chosen by the exponent. The bits of 2/pi above the window only add multiples of four to the product, which leave the quadrant where it is; the bits below it only reach past the fraction the window keeps.

That fraction has 380 bits or more, which leaves 226 significant ones even when x is closer to a multiple of pi/2 than any binary128 argument can be: for a random argument the fraction leads with about as many zero bits as the logarithm of the count of binary128 values, 127. Rounded to the nearest quadrant and multiplied by pi/2, it becomes the residual.

Before this existed sin(), cos() and tan() reduced nothing above 2^62, and returned values such as 2^3240 for sin(1.5e21). Above 2^321 the series they evaluated never terminated.

Parameters
xArgument, finite and non zero
hiReceives the residual
loReceives what did not fit in hi
Returns
n mod 4, the quadrant.

Definition at line 4948 of file float128.h.

◆ reduce_trig()

int32_t fp128::float128::reduce_trig ( const float128 & x,
float128 & hi,
float128 & lo )
inlinestaticnoexcept

Reduces x to [-pi/4, pi/4], as an unevaluated sum, and returns the quadrant.

Parameters
xArgument, finite
hiReceives the residual
loReceives what did not fit in hi
Returns
n mod 4, where x = n * pi/2 + hi + lo.

Definition at line 5082 of file float128.h.

◆ reduce_half_pi()

void fp128::float128::reduce_half_pi ( const float128 & x,
int64_t n,
float128 & hi,
float128 & lo )
inlinestaticnoexcept

Subtracts n*(pi/2) from x, leaving the residual as an unevaluated sum.

pi/2 is irrational, so no single float128 multiple of it can be subtracted without leaving an error of n ulp behind - and the sine of the residual is only as accurate as the residual itself. Where the answer is small, which is exactly where an argument lands near a multiple of pi/2, that error is the whole answer: sin() used to return a value with 55 correct bits for an argument a few ulp away from pi.

Two things fix it. pi/2 is carried in three pieces covering 342 bits rather than 113, and each product n*piece is split into its rounded value and its exact error with fma(), so no multiplication is lost either. The residual accumulates as a pair, giving it 226 bits.

Parameters
xArgument
nMultiple of pi/2 to remove
hiReceives the residual
loReceives what did not fit in hi

Definition at line 5113 of file float128.h.

◆ total_order_key()

void fp128::float128::total_order_key ( uint64_t & l,
uint64_t & h ) const
inlineconstexprnoexcept

The encoding as a key whose unsigned order is the total order of IEEE 754-2008 5.10.

Positive encodings already ascend with their value, NaNs above infinity and quiet NaNs above signaling ones; negative encodings descend. Flipping every bit of a negative encoding and only the sign bit of a positive one puts the whole range in one ascending sequence.

Definition at line 6041 of file float128.h.

◆ is_odd_int()

constexpr bool fp128::float128::is_odd_int ( const float128 & x)
inlinestaticnodiscardconstexprnoexcept

True when x is an integer whose last place is one, which is the parity a tie needs.

Definition at line 6140 of file float128.h.

◆ fmod_magnitude()

float128 fp128::float128::fmod_magnitude ( const float128 & x,
const float128 & y,
uint64_t & quotient,
int32_t & half )
inlinestaticnodiscardnoexcept

|x| mod |y|, along with the low bits of the quotient it implies.

The shared engine behind fmod, remainder and remquo. All three are exact operations on the mantissas, and all three need the same long division; only what they do with the quotient differs.

Parameters
xDividend, must be finite and non zero
yDivisor, must be finite and non zero
quotientReceives the low bits of the integer quotient
halfReceives how twice the remainder compares with |y|: negative, zero or positive. remainder() decides on it, and it is decided here, on the integer mantissas, because doubling the remainder in floating point overflows once |y| is above half the largest finite value.
Returns
The remainder, with a positive sign.

Definition at line 6235 of file float128.h.

◆ lgamma_near_two()

float128 fp128::float128::lgamma_near_two ( const float128 & z)
inlinestaticnodiscardnoexcept

lgamma(2 + z) for |z| <= 1/2, accurate relative to the result.

The Taylor series of log(gamma) about 2:

                                inf      k  zeta(k) - 1   k
lgamma(2 + z) = (1 - gamma) z +  sum  (-1)  ----------- z
                                k=2              k

zeta(k) - 1 is about 2^-k, so a term is about (z/2)^k / k and 58 of them reach past the last bit for |z| <= 1/2. Every term carries a power of z, so the sum is accurate relative to itself however close z is to zero - which is what lgamma near its zeros at 1 and 2 needs, and what the recurrence and Stirling series it replaced there could not give: they formed the answer as a difference of two values near 80, 2^-106 wide in absolute terms, which was 2^28 ulp at lgamma(1 + 2^-20) and every bit of lgamma(2 - 2^-100).

The coefficients are the series' own, rounded to binary128 (ties to even) from mpmath at 400 bits; the first is 1 - gamma, gamma being Euler's constant.

Parameters
zOffset from two, |z| <= 1/2
Returns
lgamma(2 + z)

Definition at line 6418 of file float128.h.

◆ abs_sin_pi()

float128 fp128::float128::abs_sin_pi ( const float128 & x)
inlinestaticnodiscardnoexcept

|sin(pi * x)| for a finite x that is not an integer, without the error of rounding pi * x.

The integer part of x only changes the sign, and x - round(x) is exact, so the reduction costs nothing. What is left, f in [-1/2, 1/2], is multiplied by pi as an unevaluated sum: the rounded product, its exact error from fma(), and the part of pi below the last place of float128::pi(). sin(pi * f) is then sin(p) + cos(p) * e to well past the last bit. Forming sin(pi() * x) instead, as lgamma() did, rounded the argument before reducing it: next to a pole, where the sine is as small as x is close to an integer, that rounding was most of the answer.

Parameters
xFinite value, not an integer, below 2^112 in magnitude
Returns
|sin(pi * x)|, which is positive.

Definition at line 6500 of file float128.h.

◆ fp128_gtest

friend class fp128_gtest
friend

Definition at line 675 of file float128.h.

◆ operator+ [1/2]

template<typename T>
float128 operator+ ( float128 lhs,
const T & rhs )
friend

Adds 2 values and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
Result of the operation

Definition at line 3105 of file float128.h.

◆ operator- [1/2]

template<typename T>
float128 operator- ( float128 lhs,
const T & rhs )
friend

subtracts the right hand side operand to this object to and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The float128 result

Definition at line 3116 of file float128.h.

◆ operator* [1/2]

template<typename T>
float128 operator* ( float128 lhs,
const T & rhs )
friend

Multiplies the right hand side operand with this object to and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The float128 result

Definition at line 3127 of file float128.h.

◆ operator/ [1/2]

template<typename T>
float128 operator/ ( float128 lhs,
const T & rhs )
friend

Divides this object by the right hand side operand and returns the result.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
The float128 result

Definition at line 3138 of file float128.h.

◆ operator+ [2/2]

template<typename T>
requires std::is_arithmetic_v<T>
float128 operator+ ( const T & lhs,
const float128 & rhs )
friend

Adds a scalar and a float128, in that order.

Parameters
lhsLeft operand
rhsRight operand
Returns
The float128 result

Definition at line 3157 of file float128.h.

◆ operator- [2/2]

template<typename T>
requires std::is_arithmetic_v<T>
float128 operator- ( const T & lhs,
const float128 & rhs )
friend

Subtracts a float128 from a scalar.

Parameters
lhsLeft operand
rhsRight operand
Returns
The float128 result

Definition at line 3164 of file float128.h.

◆ operator* [2/2]

template<typename T>
requires std::is_arithmetic_v<T>
float128 operator* ( const T & lhs,
const float128 & rhs )
friend

Multiplies a scalar and a float128, in that order.

Parameters
lhsLeft operand
rhsRight operand
Returns
The float128 result

Definition at line 3171 of file float128.h.

◆ operator/ [2/2]

template<typename T>
requires std::is_arithmetic_v<T>
float128 operator/ ( const T & lhs,
const float128 & rhs )
friend

Divides a scalar by a float128.

Parameters
lhsLeft operand
rhsRight operand
Returns
The float128 result

Definition at line 3178 of file float128.h.

◆ operator<<

template<typename T>
requires std::is_arithmetic_v<T>
float128 operator<< ( const T & lhs,
const float128 & rhs )
friend

Shifts a scalar left by a float128 shift count, scaling it by a power of two.

The left operand is widened to float128 first, so the result is 128 bit rather than the builtin type of lhs.

The count is rhs converted to int32_t, the same conversion the float128 on the left overload applies, and that conversion truncates, so a count of 3.75 shifts by 3 - as it does in fixed_point128, whose conversion is a plain shift.

Parameters
lhsLeft operand, the value being shifted
rhsRight operand, the shift count
Returns
The float128 result

Definition at line 3198 of file float128.h.

◆ operator>>

template<typename T>
requires std::is_arithmetic_v<T>
float128 operator>> ( const T & lhs,
const float128 & rhs )
friend

Shifts a scalar right by a float128 shift count, scaling it by a power of two. The left operand is widened to float128 first, see operator<< above.

Parameters
lhsLeft operand, the value being shifted
rhsRight operand, the shift count
Returns
The float128 result

Definition at line 3211 of file float128.h.

◆ operator== [1/3]

bool operator== ( const float128 & lhs,
const float128 & rhs )
friend

Compare logical/bitwise equal.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True if lhs and rhs are equal.

Definition at line 3226 of file float128.h.

◆ operator== [2/3]

template<typename T>
requires std::is_arithmetic_v<T>
bool operator== ( const float128 & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3247 of file float128.h.

◆ operator== [3/3]

template<typename T>
requires std::is_arithmetic_v<T>
bool operator== ( const T & lhs,
const float128 & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3252 of file float128.h.

◆ operator!= [1/3]

bool operator!= ( const float128 & lhs,
const float128 & rhs )
friend

Return true when objects are not equal. Can be used as logical XOR.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True if not equal.

Definition at line 3262 of file float128.h.

◆ operator!= [2/3]

template<typename T>
requires std::is_arithmetic_v<T>
bool operator!= ( const float128 & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3267 of file float128.h.

◆ operator!= [3/3]

template<typename T>
requires std::is_arithmetic_v<T>
bool operator!= ( const T & lhs,
const float128 & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Return true when objects are not equal. Can be used as logical XOR.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True if not equal.

Definition at line 3272 of file float128.h.

◆ operator< [1/3]

bool operator< ( const float128 & lhs,
const float128 & rhs )
friend

Return true if this object is small than the other.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True when this object is smaller.

Definition at line 3279 of file float128.h.

◆ operator< [2/3]

template<typename T>
bool operator< ( const float128 & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3308 of file float128.h.

◆ operator< [3/3]

template<typename T>
bool operator< ( const T & lhs,
const float128 & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3311 of file float128.h.

◆ operator<= [1/3]

bool operator<= ( const float128 & lhs,
const float128 & rhs )
friend

Return true this object is small or equal than the other.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True when this object is smaller or equal.

Definition at line 3318 of file float128.h.

◆ operator<= [2/3]

template<typename T>
bool operator<= ( const float128 & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3330 of file float128.h.

◆ operator<= [3/3]

template<typename T>
bool operator<= ( const T & lhs,
const float128 & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3333 of file float128.h.

◆ operator> [1/3]

bool operator> ( const float128 & lhs,
const float128 & rhs )
friend

Return true this object is larger than the other.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True when this object is larger.

Definition at line 3340 of file float128.h.

◆ operator> [2/3]

template<typename T>
bool operator> ( const float128 & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3368 of file float128.h.

◆ operator> [3/3]

template<typename T>
bool operator> ( const T & lhs,
const float128 & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3371 of file float128.h.

◆ operator>= [1/3]

bool operator>= ( const float128 & lhs,
const float128 & rhs )
friend

Return true this object is larger or equal than the other.

Parameters
lhsleft hand side operand
rhsRight hand side operand
Returns
True when this objext is larger or equal.

Definition at line 3378 of file float128.h.

◆ operator>= [2/3]

template<typename T>
bool operator>= ( const float128 & lhs,
const T & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3389 of file float128.h.

◆ operator>= [3/3]

template<typename T>
bool operator>= ( const T & lhs,
const float128 & rhs )
friend

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 3392 of file float128.h.

◆ nan [1/2]

float128 nan ( const float128 & )
friend

Return the NaN constant.

Returns
A quiet NaN. The argument only exists so that the call can be found by argument dependent lookup, its value is ignored.

Definition at line 3399 of file float128.h.

◆ isnan

bool isnan ( const float128 & x)
friend

Tests if the value is a NaN.

Parameters
xValue to test
Returns
True when the value is a NaN

Definition at line 3405 of file float128.h.

◆ isinf

bool isinf ( const float128 & x)
friend

Tests if the value is an Infinite (negative or positive)

Parameters
xValue to test
Returns
True when the value is an Infinite

Definition at line 3415 of file float128.h.

◆ fabs

float128 fabs ( const float128 & x)
friend

Returns the absolute value of x.

Parameters
xInput value
Returns
|x|

Definition at line 3422 of file float128.h.

◆ floor

float128 floor ( const float128 & x)
friend

Performs the floor() function, similar to libc's floor(), rounds down towards -infinity.

Parameters
xInput value
Returns
A float128 holding the integer value.

Definition at line 3536 of file float128.h.

◆ ceil

float128 ceil ( const float128 & x)
friend

Performs the ceil() function, similar to libc's ceil(), rounds up towards infinity.

Parameters
xInput value
Returns
A float128 holding the integer value. A negative value above -1 gives -0.

Definition at line 3545 of file float128.h.

◆ trunc

float128 trunc ( const float128 & x)
friend

Rounds towards zero.

Parameters
xValue to truncate
Returns
Integer value, rounded towards zero. A negative value above -1 gives -0.

Definition at line 3554 of file float128.h.

◆ round

float128 round ( const float128 & x)
friend

Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.

Parameters
xValue to round
Returns
Integer value, rounded towards the nearest integer.

Definition at line 3564 of file float128.h.

◆ llrint

int64_t llrint ( const float128 & x)
friend

Rounds x to an integer in the current rounding direction and returns it as int64_t.

Parameters
xInput value
Returns
Nearest integer as int64_t. Returns 0 on overflow or for a NaN, raising invalid.

Definition at line 3575 of file float128.h.

◆ llround

int64_t llround ( const float128 & x)
friend

Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.

Parameters
xValue to round
Returns
Integer value, rounded towards the nearest integer. Returns 0 on overflow or for a NaN, raising invalid.

Definition at line 3583 of file float128.h.

◆ lrint

int32_t lrint ( const float128 & x)
friend

Rounds x to an integer in the current rounding direction and returns it as int32_t.

Parameters
xInput value
Returns
Nearest integer as int32_t. Returns 0 on overflow or for a NaN, raising invalid.

Definition at line 3589 of file float128.h.

◆ lround

int32_t lround ( const float128 & x)
friend

Rounds towards the nearest integer. The halfway value (0.5) is rounded away from zero.

Parameters
xValue to round
Returns
Integer value, rounded towards the nearest integer. Returns 0 on overflow or for a NaN, raising invalid.

Definition at line 3597 of file float128.h.

◆ ilogb

int32_t ilogb ( const float128 & x)
friend

Retrieves an integer that represents the base-2 exponent of the specified value.

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.

Parameters
xThe specified value.
Returns
The unbiased exponent of x - that of its leading bit for a subnormal. FP_ILOGB0 for a zero, INT_MAX for an infinity and FP_ILOGBNAN for a NaN, each raising invalid.

Definition at line 3609 of file float128.h.

◆ copysign

float128 copysign ( const float128 & x,
const float128 & y )
friend

returns the value of x with the sign of y.

Parameters
xThe value that's returned as the magnitude of the result.
yThe sign of the result.
Returns
The copysign functions return a floating-point value that combines the magnitude of x and the sign of y.

Definition at line 3633 of file float128.h.

◆ fmod

float128 fmod ( const float128 & x,
const float128 & y )
friend

Performs the fmod() function, similar to libc's fmod(), returns the remainder of a division x/root.

Parameters
xNumerator
yDenominator
Returns
The modulo value.

Definition at line 3645 of file float128.h.

◆ modf

float128 modf ( const float128 & x,
float128 * iptr )
friend

Split into integer and fraction parts. Both results carry the sign of the input variable.

Parameters
xInput value
iptrPointer to float128 holding the integer part of x.
Returns
The fraction part of x. Undefined when iptr is nullptr.

Definition at line 3720 of file float128.h.

◆ fdim

float128 fdim ( const float128 & x,
const float128 & y )
friend

Determines the positive difference between the first and second values.

Parameters
xFirst value
ySecond value
Returns
If x > y returns x - y. Otherwise zero.

Definition at line 3745 of file float128.h.

◆ fmin

float128 fmin ( const float128 & x,
const float128 & y )
friend

Returns the minimum between x and y.

Parameters
xFirst value
ySecond value
Returns
If x < y returns x. Otherwise y.

Definition at line 3757 of file float128.h.

◆ fmax

float128 fmax ( const float128 & x,
const float128 & y )
friend

Returns the maximum between x and y.

Parameters
xFirst value
ySecond value
Returns
If x > y returns x. Otherwise y.

Definition at line 3781 of file float128.h.

◆ hypot [1/2]

float128 hypot ( const float128 & x,
const float128 & y )
friend

Calculates the hypotenuse. i.e. sqrt(x^2 + y^2)

Parameters
xFirst value
ySecond value
Returns
sqrt(x^2 + y^2).

Definition at line 3799 of file float128.h.

◆ sqr

float128 sqr ( float128 x)
friend

Calculates the square of a value. i.e. x^2.

Faster than x * x and identical to it, see float128::square().

Parameters
xValue to square
Returns
x^2, which is never negative.

Definition at line 3830 of file float128.h.

◆ sqrt

float128 sqrt ( const float128 & x,
uint32_t iterations = 3 )
friend

Calculates the square root, correctly rounded.

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.

Parameters
xValue to calculate the root of
iterationsIgnored. The result is correctly rounded whatever its value; the parameter is kept so that existing calls still compile.
Returns
Square root of x. sqrt(-0) is -0, and any other negative argument is the invalid operation and returns a NaN.

Definition at line 3872 of file float128.h.

◆ cbrt

float128 cbrt ( const float128 x,
uint32_t iterations = 2 )
friend

Calculates the cube root. Uses the Halley's method.

Parameters
xFloating point value
iterationshow many Halley to perform, usually 1 is enough
Returns
cube root of x

Definition at line 3958 of file float128.h.

◆ reciprocal

float128 reciprocal ( const float128 & x)
friend

Calculates the reciprocal of a value, 1 / x, correctly rounded.

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.

Parameters
xInput value
Returns
1 / x. +-0 gives +-inf and raises division by zero, +-inf gives +-0, a NaN is propagated.

Definition at line 4017 of file float128.h.

◆ fact_reciprocal

void fact_reciprocal ( int x,
float128 & res )
friend

Factorial reciprocal (inverse). Calculates 1 / x! Maximum supported value of x is 50.

Parameters
xInput value
resResult of the function

Definition at line 4029 of file float128.h.

◆ double_factorial

float128 double_factorial ( int x)
friend

Returns the double factorial of a number, x!! = x * (x - 2) * (x - 4) * ...

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.

Parameters
xThe number to compute the double factorial of.
Returns
x!!: exact for every odd x up to 51 and every even x up to 74, and within 1 ulp of the correctly rounded value up to 99 (each product of the table rounds once, so the error can accumulate). (-1)!! is 1, the empty product (2n - 1)!! takes for n = 0. Any other negative x is the invalid operation and returns a NaN; x of 100 or more returns infinity.

< c[i] is i!!

Definition at line 4109 of file float128.h.

◆ exp

float128 exp ( const float128 & x)
friend

Computes e to the power of x.

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().

Parameters
xA number specifying a power.
Returns
e^x

Definition at line 4190 of file float128.h.

◆ exp2

float128 exp2 ( const float128 & x)
friend

Computes 2 to the power of x.

Parameters
xExponent value
Returns
2^x

Definition at line 4232 of file float128.h.

◆ expm1

float128 expm1 ( const float128 & x)
friend

Calculates the exponent of x and reduces 1 from the result: (e^x) - 1.

Parameters
xA number specifying a power.
Returns
Exponent of x

Definition at line 4269 of file float128.h.

◆ pow [1/2]

float128 pow ( const float128 & x,
int32_t y )
friend

Computes x to the power of y (IEEE 754-2008 pown)

Parameters
xBase value
yExponent value (integer)
Returns
x^y

Definition at line 4333 of file float128.h.

◆ pow [2/2]

float128 pow ( const float128 & x,
const float128 & y )
friend

Computes x to the power of y.

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.

Parameters
xBase value
yExponent value
Returns
x^y

Definition at line 4359 of file float128.h.

◆ log

float128 log ( float128 x)
friend

Calculates the natural Log (base e) of x: log(x)

Parameters
xThe number to perform log on.
Returns
log(x)

Definition at line 4513 of file float128.h.

◆ log2

float128 log2 ( float128 x)
friend

Calculates the Log base 2 of x: y = log2(x)

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.

Parameters
xThe number to perform log2 on.
Returns
log2(x)

Definition at line 4542 of file float128.h.

◆ log10

float128 log10 ( float128 x)
friend

Calculates Log base 10 of x: log10(x)

Parameters
xThe number to perform log on.
Returns
log10(x)

Definition at line 4700 of file float128.h.

◆ logb

float128 logb ( float128 x)
friend

Calculates Log base 2 of x as an integer ignoring the sign of x. Similar to: floor(log2(fabs(x)))

Parameters
xThe number to perform log on.
Returns
logb(x)

Definition at line 4720 of file float128.h.

◆ log1p

float128 log1p ( float128 x)
friend

Calculates the natural Log (base e) of 1 + x: log(1 + x)

Parameters
xThe number to perform log on.
Returns
log1p(x)

Definition at line 4744 of file float128.h.

◆ sin1

float128 sin1 ( float128 x)
friend

Calculate the sine function over a limited range [-0.5pi, 0.5pi] Using the Maclaurin series expansion, the formula is: x^3 x^5 x^7 sin(x) = x - — + — - — + ... 3! 5! 7!

Parameters
xvalue in Radians in the range [-0.5pi, 0.5pi]
Returns
Sine of x

Definition at line 4790 of file float128.h.

◆ cos1

float128 cos1 ( const float128 & x)
friend

Calculate the cosine function over a limited range [-0.5pi, 0.5pi].

Up to pi/4, the range the trigonometric functions reduce their argument to, the Maclaurin series is evaluated as one minus its tail:

             2       4       6
            x       x       x

cos(x) = 1 - ( — - ( — - ( — - ...))) 2! 4! 6!

The tail is at most 0.3 and is formed from the argument alone, so it carries its own last bits and the subtraction from one rounds once. A zero argument gives exactly one, and so does any argument whose square is below the last place of one. This used to be computed as sin1(pi/2 - x), whose series sums to 1 - 2^-113 for x = 0: cos(0) and sin(pi/2) were not one.

Beyond pi/4 the cosine is the sine of the complement, which stays accurate near pi/2 where the series above would cancel down to nothing.

Parameters
xvalue in Radians in the range [-0.5pi, 0.5pi]
Returns
Cosine of x

Definition at line 4839 of file float128.h.

◆ sin

float128 sin ( float128 x)
friend

Definition at line 5140 of file float128.h.

◆ asin

float128 asin ( float128 x)
friend

Calculate the inverse sine function.

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.

Parameters
xvalue in the range [-1,1]
Returns
Inverse sine of x. Outside the domain the result is a NaN, the invalid operation.

Definition at line 5177 of file float128.h.

◆ cos

float128 cos ( float128 x)
friend

Calculate the cosine function Ultimately uses sin1() with a reduced range of [-pi/4, pi/4] Sine's Maclaurin series converges faster than Cosine's.

Parameters
xvalue in Radians
Returns
Cosine of x

Definition at line 5210 of file float128.h.

◆ acos

float128 acos ( float128 x)
friend

Calculate the inverse cosine function.

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.

Parameters
xvalue in the range [-1,1]
Returns
Inverse cosine of x. Outside the domain the result is a NaN, the invalid operation.

Definition at line 5245 of file float128.h.

◆ tan

float128 tan ( float128 x)
friend

Calculate the tangent function tan(x) = sin(x)/cos(x)

Parameters
xvalue
Returns
Tangent of x

Definition at line 5267 of file float128.h.

◆ atan

float128 atan ( float128 x)
friend

Calculate the inverse tangent function.

Parameters
xvalue
Returns
Arctangent of x

Definition at line 5273 of file float128.h.

◆ atan2

float128 atan2 ( float128 y,
float128 x )
friend

Calculate the inverse tangent function of the ratio y / x.

Parameters
yvalue
xvalue
Returns
Arctangent of y / x in the range [-pi, pi]. The zeros and infinities follow IEEE 754-2008 9.2.1: the sign of a zero y is kept, a negative x (-0 included) puts the result at +-pi, and two infinities give +-pi/4 or +-3pi/4.

Definition at line 5339 of file float128.h.

◆ sinh

float128 sinh ( const float128 x)
friend

Calculate the hyperbolic sine function Use the exponent function which produces more accurate results than the power series. e^x - e^(-x) sinh(x) = ---------— 2.

Parameters
xvalue
Returns
Sine of x

Definition at line 5393 of file float128.h.

◆ asinh

float128 asinh ( const float128 x)
friend

Calculates the inverse hyperbolic sine For positive x: asinh(x) = log(x + sqrt(x^2 + 1)) For negative x, the function returns the result with the sign inverted.

Parameters
xvalue
Returns
Inverse hyperbolic sine of x

Definition at line 5429 of file float128.h.

◆ cosh

float128 cosh ( const float128 x)
friend

Calculate the hyperbolic cosine function over a limited range [-0.5pi, 0.5pi] e^x + e^(-x) cosh(x) = ---------— 2.

Parameters
xvalue in Radians in the range [-0.5pi, 0.5pi]
Returns
Sine of x

Definition at line 5474 of file float128.h.

◆ acosh

float128 acosh ( const float128 x)
friend

Calculates the inverse hyperbolic cosine For x >= 1: acosh(x) = log(x + sqrt(x^2 - 1)) For x < 1, the function return zero.

Parameters
xvalue in the range [1, inf]
Returns
Inverse hyperbolic cosine of x

Definition at line 5498 of file float128.h.

◆ tanh

float128 tanh ( const float128 x)
friend

Calculates the hyperbolic tangent e^x - e^(-x) tanh(x) = ---------— e^x + e^(-x)

Parameters
xvalue
Returns
hyperbolic tangent of x

Definition at line 5533 of file float128.h.

◆ atanh

float128 atanh ( const float128 x)
friend

Calculates the inverse hyperbolic tangent 1 + x atanh(x) = 0.5 * log( --—) 1 - x.

Parameters
xvalue in the range (-1, 1)
Returns
Inverse hyperbolic tangent of x

Definition at line 5569 of file float128.h.

◆ erf_constants

void erf_constants ( float128 * a,
int32_t count )
friend

Calculates the Maclaurin series constants for the erf function. The array will hold 1 / (n! * (2n + 1))

Parameters
apointer to array that receives the results. The array must be preallocated.
countElement count in the array

Definition at line 5607 of file float128.h.

◆ erf

float128 erf ( float128 x)
friend

Computes the error function of a value. The erf function return a value in the range -1.0 to 1.0. There's no error return.

Parameters
xA floating-point value.
Returns
The erf functions return the Gauss error function of x.

Definition at line 5626 of file float128.h.

◆ erfc

float128 erfc ( float128 x)
friend

Computes the complementary error function, 1 - erf(x).

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.

Parameters
xInput value
Returns
1 - erf(x)

Definition at line 5708 of file float128.h.

◆ isfinite

bool isfinite ( const float128 & x)
friend

Tests whether x is finite (not infinite and not NaN).

Parameters
xInput value
Returns
Non-zero if finite, zero otherwise.

Definition at line 5765 of file float128.h.

◆ fma

float128 fma ( float128 x,
float128 y,
float128 z )
friend

Computes (x * y) + z with a single rounding at the end.

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.

Parameters
xThe first value to multiply.
yThe second value to multiply.
zThe value to add to the product.
Returns
(x * y) + z, correctly rounded.

Definition at line 5785 of file float128.h.

◆ frexp

float128 frexp ( float128 x,
int * expptr )
friend

Gets the mantissa and exponent of a floating-point number.

Parameters
xFloating-point value.
expptrReceives the exponent: zero for a zero, an infinity or a NaN.
Returns
Mantissa in the [0.5,1) range with the sign of x. A zero or an infinity comes back unchanged, a NaN quieted (a signaling one raising invalid, as for every operation).

Definition at line 5923 of file float128.h.

◆ ldexp

float128 ldexp ( float128 x,
int exp )
friend

Multiplies a floating-point number by an integral power of two.

Parameters
xFloating-point value.
expInteger exponent.
Returns
The ldexp functions return the value of x * 2^exp if successful. On overflow, and depending on the sign of x, ldexp returns +/- inf

Definition at line 5948 of file float128.h.

◆ signbit

bool signbit ( const float128 & x)
friend

True when the sign bit is set, including for -0 and for a negative NaN.

Definition at line 5971 of file float128.h.

◆ isnormal

bool isnormal ( const float128 & x)
friend

True when x is neither zero, subnormal, infinite nor NaN.

Definition at line 5973 of file float128.h.

◆ fpclassify

int fpclassify ( const float128 & x)
friend

One of FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL or FP_NORMAL.

Definition at line 5975 of file float128.h.

◆ isunordered

bool isunordered ( const float128 & x,
const float128 & y )
friend

True when either operand is a NaN, so that the two do not compare in any order.

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.

◆ isgreater

bool isgreater ( const float128 & x,
const float128 & y )
friend

x > y, false rather than unordered when either is a NaN.

Definition at line 6000 of file float128.h.

◆ isgreaterequal

bool isgreaterequal ( const float128 & x,
const float128 & y )
friend

x >= y, false when either is a NaN.

Definition at line 6005 of file float128.h.

◆ isless

bool isless ( const float128 & x,
const float128 & y )
friend

x < y, false when either is a NaN.

Definition at line 6010 of file float128.h.

◆ islessequal

bool islessequal ( const float128 & x,
const float128 & y )
friend

x <= y, false when either is a NaN.

Definition at line 6015 of file float128.h.

◆ islessgreater

bool islessgreater ( const float128 & x,
const float128 & y )
friend

x < y or x > y, false when either is a NaN.

Definition at line 6020 of file float128.h.

◆ totalorder

bool totalorder ( const float128 & x,
const float128 & y )
friend

totalOrder(x, y): true when x precedes y, or equals it, in the IEEE 754 total order.

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.

Parameters
xFirst value
ySecond value
Returns
True when x is ordered before or the same as y.

Definition at line 6059 of file float128.h.

◆ totalordermag

bool totalordermag ( const float128 & x,
const float128 & y )
friend

totalOrderMag(x, y): totalorder() of the absolute values.

Definition at line 6067 of file float128.h.

◆ fmaxmag

float128 fmaxmag ( const float128 & x,
const float128 & y )
friend

maxNumMag: whichever of x and y has the larger magnitude, or fmax() when they tie.

Parameters
xFirst value
ySecond value
Returns
The larger magnitude. A quiet NaN is treated as missing; a signaling one is the invalid operation and gives a quiet NaN.

Definition at line 6079 of file float128.h.

◆ fminmag

float128 fminmag ( const float128 & x,
const float128 & y )
friend

minNumMag: whichever of x and y has the smaller magnitude, or fmin() when they tie.

Parameters
xFirst value
ySecond value
Returns
The larger magnitude. A quiet NaN is treated as missing; a signaling one is the invalid operation and gives a quiet NaN.

Definition at line 6092 of file float128.h.

◆ iscanonical

bool iscanonical ( const float128 & x)
friend

isCanonical: always true, every binary128 encoding is canonical.

Definition at line 6105 of file float128.h.

◆ issignaling

bool issignaling ( const float128 & x)
friend

isSignaling: true for a signaling NaN.

Definition at line 6107 of file float128.h.

◆ issubnormal

bool issubnormal ( const float128 & x)
friend

isSubnormal: true for a subnormal value, which zero is not.

Definition at line 6109 of file float128.h.

◆ iszero

bool iszero ( const float128 & x)
friend

isZero: true for either zero.

Definition at line 6111 of file float128.h.

◆ nan [2/2]

float128 nan ( const char * payload)
friend

A quiet NaN carrying the payload spelled out in the argument.

Mirrors the C library's nan(): the string is read as an unsigned integer, decimal by default, hexadecimal after an 0x prefix, and its low bits become the NaN's fraction. An empty or unparsable string gives the default NaN.

Parameters
payloadDigits of the payload, may be empty
Returns
A quiet NaN.

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.

◆ rint

float128 rint ( const float128 & x)
friend

x rounded to an integral value in the current direction, raising inexact when that changes it.

Definition at line 6165 of file float128.h.

◆ nearbyint

float128 nearbyint ( const float128 & x)
friend

x rounded to an integral value in the current direction.

Definition at line 6170 of file float128.h.

◆ scalbn

float128 scalbn ( const float128 & x,
int n )
friend

x * 2^n, the same operation ldexp performs.

Definition at line 6181 of file float128.h.

◆ scalbln

float128 scalbln ( const float128 & x,
long n )
friend

x * 2^n for a long exponent, clamped to what the format can reach.

Definition at line 6183 of file float128.h.

◆ nextafter

float128 nextafter ( const float128 & x,
const float128 & y )
friend

The representable value next to x in the direction of y.

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.

Parameters
xStarting value
yValue giving the direction
Returns
The neighbour of x towards y, or y itself when the two are equal.

Definition at line 6205 of file float128.h.

◆ nexttoward

float128 nexttoward ( const float128 & x,
const float128 & y )
friend

The neighbour of x towards y. The same as nextafter: there is no wider type to convert from.

Definition at line 6214 of file float128.h.

◆ remainder

float128 remainder ( const float128 & x,
const float128 & y )
friend

IEEE 754 remainder: x - n*y with n the quotient rounded to the nearest even integer.

Unlike fmod, whose result keeps the sign of x, this one lands in [-|y|/2, |y|/2].

Parameters
xDividend
yDivisor
Returns
The remainder.

Definition at line 6305 of file float128.h.

◆ remquo

float128 remquo ( const float128 & x,
const float128 & y,
int * quo )
friend

The IEEE remainder together with the low bits of the quotient.

Parameters
xDividend
yDivisor
quoReceives at least the low three bits of the quotient, with the sign of x/y
Returns
The remainder.

Definition at line 6318 of file float128.h.

◆ hypot [2/2]

float128 hypot ( const float128 & x,
const float128 & y,
const float128 & z )
friend

Length of the diagonal of a box, computed without an intermediate overflow.

Parameters
xFirst side
ySecond side
zThird side
Returns
sqrt(x*x + y*y + z*z)

Definition at line 6365 of file float128.h.

◆ lgamma

float128 lgamma ( float128 x)
friend

Natural logarithm of the absolute value of the gamma function.

Three regimes:

  • |x| < 32: x = n + z with n = round(x), and the recurrence gamma(x + 1) = x * gamma(x) moves the argument to 2 + z, where lgamma_near_two() is accurate relative to its result. Every factor the recurrence takes is x moved by an integer towards zero, which is exact, so the answer is lgamma(2 + z) plus or minus the logarithm of their product: rounded a few times rather than cancelled. Next to a pole the factor z is the small one, and its logarithm is the answer.
  • x >= 32: Stirling's asymptotic series.
  • x <= -32: the reflection formula, as |gamma(x)| = pi / (|sin(pi x)| |x| gamma(-x)), with the sine reduced exactly by abs_sin_pi().

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.

Parameters
xInput value
Returns
log(|gamma(x)|)

Definition at line 6534 of file float128.h.

◆ tgamma

float128 tgamma ( float128 x)
friend

The gamma function.

  • The integers up to 38: the factorial, exactly. 37! is the largest factorial a binary128 holds without rounding - its odd part has 110 bits - so no partial product of the integers below x rounds either.
  • |x| < 32: exp(lgamma(2 + z)) multiplied or divided by the exact factors the recurrence takes, see lgamma(). The exponential is of a value below 0.3 and the product rounds once per factor, so the result is within a few ulp - also next to a pole, where it used to be 2^39 ulp off at -1 + 2^-40, and at a tiny argument, where gamma(x) is about 1/x.
  • Elsewhere exp(lgamma(x)), with the sign of the interval between the poles. The exponential is what limits the accuracy there: a logarithm as large as 11000 carries its own last bit into the fourteenth bit from the bottom of the result.

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.

Parameters
xInput value
Returns
gamma(x)

Definition at line 6626 of file float128.h.

◆ abs

float128 abs ( const float128 & x)
friend

Absolute value, the name <cmath> gives the floating point overload alongside fabs.

Definition at line 6690 of file float128.h.

◆ operator""_f128

float128 operator""_f128 ( const char * literal)
friend

User-defined literal implementation for constructing float128 from a string.

Definition at line 6693 of file float128.h.

Member Data Documentation

◆ low

uint64_t fp128::float128::low

Lower 64 bits of the float128 encoding.

Definition at line 721 of file float128.h.

◆ high

uint64_t fp128::float128::high

Upper 64 bits (raw).

Definition at line 723 of file float128.h.

◆ high_bits

_float128_bits fp128::float128::high_bits

Upper 64 bits (bit-field view).

Definition at line 724 of file float128.h.

◆ SCALE_LIMIT

int32_t fp128::float128::SCALE_LIMIT = 2 * (EXP_BIAS + FRAC_BITS + 1)
staticconstexpr

Largest scaling a shift or ldexp() ever needs: enough to take the largest finite value below half the smallest subnormal, or the smallest subnormal above the largest finite value. Larger counts are clamped to it, which keeps the exponent arithmetic in range.

Definition at line 1367 of file float128.h.

◆ LOG2_TERMS

int32_t fp128::float128::LOG2_TERMS = (FRAC_BITS + 1 + 8 + log2_reduction_bits - 1) / log2_reduction_bits
staticconstexpr

Number of Mercator series terms log2() needs.

The reduction leaves |z| <= 2^-6, so term n is bounded by 2^(-6n), and the series is cut off once that is below the last bit of a 113 bit mantissa with eight bits to spare.

Definition at line 2165 of file float128.h.

◆ MAX_SIG_HIGH

uint64_t fp128::float128::MAX_SIG_HIGH = FRAC_UNITY | UPPER_FRAC_MASK
staticconstexpr

The largest finite magnitude's significand, high QWORD, with its leading one at bit 48.

Definition at line 2277 of file float128.h.

◆ FIXED_WORDS

int32_t fp128::float128::FIXED_WORDS = 4
staticconstexpr

Normalize the fraction so the msb (unity bit) is on bit 112. The fraction value must contain the unity value The exponent component is adjusted based on the bit shift direction required.

Parameters
lLow part of the fraction
hHigh part of the fraction
eUnbiased exponent, can be any value.

Word count of the fixed point expansion used to print the fraction digits.

Definition at line 2507 of file float128.h.

◆ WIDE_WORDS

int32_t fp128::float128::WIDE_WORDS = 6
staticconstexpr

Words in the accumulator.

Definition at line 2614 of file float128.h.

◆ WIDE_BITS

int32_t fp128::float128::WIDE_BITS = WIDE_WORDS * 64
staticconstexpr

Bits in the accumulator.

Definition at line 2615 of file float128.h.

◆ WIDE_TOP

int32_t fp128::float128::WIDE_TOP = WIDE_BITS - 2
staticconstexpr

Bit the leading one of the larger operand is placed on.

One below the top, so that adding two operands of the same magnitude cannot carry out of the accumulator.

Definition at line 2620 of file float128.h.

◆ EXP_TERMS

int32_t fp128::float128::EXP_TERMS = 28
staticconstexpr

Calculates the exponent of x: e Using the Maclaurin series expansion, the formula is: 1 2 3 x x x exp(x) = 1 + — + — + — + ... 1! 2! 3!

                                  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.

Parameters
xA number specifying a power.
Returns
Exponent of x

Terms of the Maclaurin series exp_reduced() runs. |r| <= ln2/2, so r^28/28! is past the last bit.

Definition at line 4153 of file float128.h.

◆ LOG1P_TERMS

int32_t fp128::float128::LOG1P_TERMS = 12
staticconstexpr

Terms of the log1p_small() series. |s| stays below 2^-4.9, so s^24 is past the last bit.

Definition at line 4460 of file float128.h.

◆ COS_TERMS

int32_t fp128::float128::COS_TERMS = 16
staticconstexpr

Terms of the cosine series cos1() runs. Up to pi/4, x^32/32! is below 2^-125.

Definition at line 4815 of file float128.h.

◆ STIRLING_TERMS

int32_t fp128::float128::STIRLING_TERMS = 16
staticconstexpr

Terms of the Stirling series lgamma() runs. Sixteen reach 2^-119 for an argument of 32.

Definition at line 6390 of file float128.h.

◆ LGAMMA_NEAR_TWO_TERMS

int32_t fp128::float128::LGAMMA_NEAR_TWO_TERMS = 58
staticconstexpr

Terms of the series lgamma_near_two() runs. For |z| <= 1/2 the last is below 2^-120 of the sum.

Definition at line 6392 of file float128.h.

◆ GAMMA_RECURRENCE_LIMIT

int32_t fp128::float128::GAMMA_RECURRENCE_LIMIT = 32
staticconstexpr

Below this magnitude lgamma() and tgamma() move the argument next to two by the recurrence.

Definition at line 6394 of file float128.h.


The documentation for this class was generated from the following file: