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

128-bit IEEE 754-2008 quadruple-precision floating-point type. More...

#include <algorithm>
#include <array>
#include <cfenv>
#include <cfloat>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <charconv>
#include <format>
#include <functional>
#include <istream>
#include <limits>
#include <memory>
#include <new>
#include <ostream>
#include "fp128_shared.h"
#include "fp128_decimal.h"
#include "uint128_t.h"

Go to the source code of this file.

Classes

struct  fp128::detail::float_env
 The per thread state behind the float128 environment. More...
class  fp128::detail::flag_quiet
 Sets the exception flags aside for its lifetime, and discards whatever is raised meanwhile. More...
class  fp128::detail::flag_filter
 Reduces what a math function's internal operations raise to what its result justifies. More...
class  fp128::float128
 128 bit floating point class. More...
struct  fp128::float128::narrowed
 A finite value rounded to a narrower binary format, see narrow(). More...
class  std::numeric_limits< fp128::float128 >
 Numeric properties of fp128::float128, the binary128 interchange format. More...
struct  std::formatter< fp128::float128, char >
 std::format support for fp128::float128. More...
struct  std::hash< fp128::float128 >
 Hash support, so a float128 can be a key in an unordered container. More...
struct  std::common_type< fp128::float128, T >
struct  std::common_type< T, fp128::float128 >
struct  std::common_type< fp128::float128, fp128::float128 >

Namespaces

namespace  fp128
 Everything the library provides.
namespace  fp128::detail
namespace  std
 STL namespace.

Enumerations

enum class  fp128::detail::rounding : uint8_t { fp128::detail::nearest_even , fp128::detail::toward_zero , fp128::detail::upward , fp128::detail::downward }
 The rounding-direction attributes IEEE 754-2008 requires of a binary format. More...

Functions

std::from_chars_result fp128::from_chars (const char *first, const char *last, float128 &value)
 Reads a decimal string into a float128. Defined below, declared here because the constructor from a string delegates to it.
std::from_chars_result fp128::from_chars (const char *first, const char *last, float128 &value, std::chars_format fmt)
 Reads a decimal or hexadecimal string into a float128. Defined below.
std::string fp128::detail::render (const float128 &value, char style, int32_t precision, bool uppercase, bool alternate, char sign_char, bool bare_hex=false)
 Renders a float128 as text. Defined below, declared here because the conversions to a string delegate to it.
float128 fp128::operator""_f128 (const char *literal)
 User-defined literal for constructing float128 from a string.
constexpr rounding fp128::detail::current_rounding () noexcept
 The rounding direction every float128 operation applies.
constexpr void fp128::detail::raise_flags (int flags) noexcept
 Raises exception flags. Does nothing without FP128_IEEE_ENV or during constant evaluation.
digit_rounding fp128::detail::digit_rounding_for (uint32_t sign) noexcept
 The digit rounding the current direction comes down to for a value of a given sign.
void fp128::detail::append_exponent (std::string &out, int32_t exponent, char marker, int32_t min_digits=2)
 Appends the decimal exponent of a scientific or hexadecimal form, with a sign.
int32_t fp128::detail::shortest_digit_count (const float128 &value)
 Fewest significant digits that read back as the same value.
std::string fp128::to_string (const float128 &value)
 The shortest decimal string that reads back as this exact value.
const char * fp128::detail::parse_special_value (const char *cursor, const char *last, bool negative, float128 &value)
 Reads one of the special values: inf, infinity, nan, nan(n-char-sequence) or snan.
std::from_chars_result fp128::detail::from_hex_chars (const char *first, const char *last, float128 &value)
 Reads a hexadecimal significand with an optional binary exponent, such as 1.8p1.
template<int32_t Capacity>
std::errc fp128::detail::round_decimal (basic_big_uint< Capacity > &workspace, const char *digits, int32_t count, int32_t max_kept, int32_t exponent10, uint32_t sign, float128 &value) noexcept
 Rounds digits * 10^exponent10, with the given sign, to a float128.
std::to_chars_result fp128::to_chars (char *first, char *last, const float128 &value, std::chars_format fmt, int precision)
 Converts a float128 to text, in the shape of std::to_chars.
std::to_chars_result fp128::to_chars (char *first, char *last, const float128 &value, std::chars_format fmt)
 Converts a float128 to text with the default precision for the layout.
std::to_chars_result fp128::to_chars (char *first, char *last, const float128 &value)
 Converts a float128 to the shortest text that reads back as the same value.
std::ostream & fp128::operator<< (std::ostream &os, const float128 &value)
 Writes a float128 to a stream, honouring its formatting state.
std::istream & fp128::operator>> (std::istream &is, float128 &value)
 Reads a float128 from a stream.
CRT-Style Math Functions (Forward Declarations)

Free functions providing standard math library equivalents for float128.

constexpr float128 fp128::fabs (const float128 &x)
constexpr float128 fp128::floor (const float128 &x)
constexpr float128 fp128::ceil (const float128 &x)
constexpr float128 fp128::trunc (const float128 &x)
constexpr float128 fp128::round (const float128 &x)
constexpr int64_t fp128::llrint (const float128 &x)
constexpr int64_t fp128::llround (const float128 &x)
constexpr int32_t fp128::lrint (const float128 &x)
constexpr int32_t fp128::lround (const float128 &x)
constexpr int32_t fp128::ilogb (const float128 &x)
constexpr float128 fp128::copysign (const float128 &x, const float128 &y)
float128 fp128::fmod (const float128 &x, const float128 &y)
constexpr float128 fp128::modf (const float128 &x, float128 *iptr)
constexpr float128 fp128::fdim (const float128 &x, const float128 &y)
constexpr float128 fp128::fmin (const float128 &x, const float128 &y)
constexpr float128 fp128::fmax (const float128 &x, const float128 &y)
constexpr float128 fp128::fma (float128 x, float128 y, float128 z)
float128 fp128::hypot (const float128 &x, const float128 &y)
float128 fp128::cbrt (const float128 x, uint32_t iterations=2)
float128 fp128::sqrt (const float128 &x, uint32_t iterations=3)
float128 fp128::erf (float128 x)
float128 fp128::erfc (float128 x)
float128 fp128::sin (float128 x)
float128 fp128::asin (float128 x)
float128 fp128::cos (float128 x)
float128 fp128::acos (float128 x)
float128 fp128::tan (float128 x)
float128 fp128::atan (float128 x)
float128 fp128::atan2 (float128 y, float128 x)
float128 fp128::sinh (float128 x)
float128 fp128::asinh (float128 x)
float128 fp128::cosh (float128 x)
float128 fp128::acosh (float128 x)
float128 fp128::tanh (float128 x)
float128 fp128::atanh (float128 x)
float128 fp128::exp (const float128 &x)
float128 fp128::exp2 (const float128 &x)
float128 fp128::expm1 (const float128 &x)
float128 fp128::pow (const float128 &x, const float128 &y)
float128 fp128::pow (const float128 &x, int32_t y)
float128 fp128::log (float128 x)
float128 fp128::log2 (float128 x)
float128 fp128::log10 (float128 x)
float128 fp128::logb (float128 x)
float128 fp128::log1p (float128 x)
constexpr float128 fp128::frexp (float128 x, int *expptr)
constexpr float128 fp128::ldexp (float128 x, int exp)
constexpr bool fp128::isfinite (const float128 &x)
constexpr bool fp128::signbit (const float128 &x)
constexpr bool fp128::isnormal (const float128 &x)
constexpr int fp128::fpclassify (const float128 &x)
constexpr bool fp128::isunordered (const float128 &x, const float128 &y)
constexpr bool fp128::isgreater (const float128 &x, const float128 &y)
constexpr bool fp128::isgreaterequal (const float128 &x, const float128 &y)
constexpr bool fp128::isless (const float128 &x, const float128 &y)
constexpr bool fp128::islessequal (const float128 &x, const float128 &y)
constexpr bool fp128::islessgreater (const float128 &x, const float128 &y)
constexpr float128 fp128::rint (const float128 &x)
constexpr float128 fp128::nearbyint (const float128 &x)
constexpr float128 fp128::scalbn (const float128 &x, int n)
constexpr float128 fp128::scalbln (const float128 &x, long n)
constexpr float128 fp128::nextafter (const float128 &x, const float128 &y)
constexpr float128 fp128::nexttoward (const float128 &x, const float128 &y)
float128 fp128::remainder (const float128 &x, const float128 &y)
float128 fp128::remquo (const float128 &x, const float128 &y, int *quo)
float128 fp128::hypot (const float128 &x, const float128 &y, const float128 &z)
float128 fp128::lgamma (float128 x)
float128 fp128::tgamma (float128 x)
constexpr float128 fp128::abs (const float128 &x)
constexpr bool fp128::totalorder (const float128 &x, const float128 &y)
constexpr bool fp128::totalordermag (const float128 &x, const float128 &y)
constexpr float128 fp128::fmaxmag (const float128 &x, const float128 &y)
constexpr float128 fp128::fminmag (const float128 &x, const float128 &y)
constexpr bool fp128::iscanonical (const float128 &x)
constexpr bool fp128::issignaling (const float128 &x)
constexpr bool fp128::issubnormal (const float128 &x)
constexpr bool fp128::iszero (const float128 &x)
float128 fp128::nan (const char *payload)
Non-CRT Utility Functions (Forward Declarations)
float128 fp128::reciprocal (const float128 &x)
void fp128::fact_reciprocal (int x, float128 &res)
float128 fp128::double_factorial (int x)
Floating point environment

IEEE 754-2008 asks two things of a binary format beyond its arithmetic. Clause 4 requires four rounding-direction attributes the program can choose between: to nearest with ties to even, the default, and towards zero, towards positive infinity and towards negative infinity. Clause 7 requires five exceptions - invalid operation, division by zero, overflow, underflow and inexact - each reported by a status flag that stays raised until the program lowers it.

float128 provides both, as a per thread environment of its own that is entirely separate from the hardware one <cfenv> controls: setting the rounding direction here does not change how double rounds, and an exception raised by a float128 operation shows up here and not in fetestexcept().

The environment is opt in. It exists only when every translation unit that includes this header is compiled with FP128_IEEE_ENV defined. Without it, which is the default, every operation rounds to nearest with ties to even, no flag is ever raised, and the code that would maintain the environment compiles away, leaving the arithmetic exactly as fast as it was before the environment existed. The functions below are still declared, so the same source builds either way: they report the fixed default and refuse to change it.

The macro changes the definition of inline functions, so it must have the same value in every translation unit of a program. Mixing the two violates the one definition rule.

The rounding directions and exception flags are named with the FE_ constants of <cfenv>, so the calls read the way their hardware counterparts do:

fp128::fesetround(FE_UPWARD);
float128 bound = a / b; // rounded towards +inf
if (fp128::fetestexcept(FE_INEXACT)) { ... } // the quotient was not exact
128 bit floating point class.
Definition float128.h:672
int fetestexcept(int excepts) noexcept
Tests float128 exception flags (IEEE 754 testFlags).
Definition float128.h:452
int fesetround(int round) noexcept
Selects the rounding direction float128 arithmetic uses on the calling thread.
Definition float128.h:427

The calls have to be qualified: their arguments are plain integers, so argument dependent lookup cannot tell them from the <cfenv> functions of the same name.

int fp128::fegetround () noexcept
 The rounding direction float128 arithmetic uses on the calling thread.
int fp128::fesetround (int round) noexcept
 Selects the rounding direction float128 arithmetic uses on the calling thread.
int fp128::fetestexcept (int excepts) noexcept
 Tests float128 exception flags (IEEE 754 testFlags).
int fp128::feclearexcept (int excepts) noexcept
 Lowers float128 exception flags (IEEE 754 lowerFlags).
int fp128::feraiseexcept (int excepts) noexcept
 Raises float128 exception flags (IEEE 754 raiseFlags).
int fp128::fegetexceptflag (std::fexcept_t *flagp, int excepts) noexcept
 Saves the state of float128 exception flags (IEEE 754 saveAllFlags).
int fp128::fesetexceptflag (const std::fexcept_t *flagp, int excepts) noexcept
 Restores float128 exception flags saved by fegetexceptflag (IEEE 754 restoreFlags).

Variables

constexpr uint64_t fp128::detail::two_over_pi_bits []
 The binary expansion of 2/pi after the binary point, most significant word first.
constexpr int32_t fp128::detail::DEFAULT_PRECISION = 6
 Default precision the e, f and g styles use when none is given, as in printf.

Detailed Description

128-bit IEEE 754-2008 quadruple-precision floating-point type.

Provides the fp128::float128 class with the same bit layout as IEEE 754 binary128: 112-bit fraction, 15-bit exponent, and 1-bit sign. Supports full arithmetic, comparison, and conversion operators, as well as a comprehensive set of math functions (sqrt, cbrt, sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, asinh, acosh, atanh, exp, exp2, log, log2, log10, pow, erf, erfc, etc.). All methods are inline for maximum performance.

The arithmetic, the conversions and the operations of IEEE 754-2008 clause 5 are correctly rounded. The rounding directions and exception flags the standard also requires are opt in, through FP128_IEEE_ENV; see the floating point environment section below.

See also
fp128_shared.h for supporting intrinsics and utilities.

Definition in file float128.h.