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

Classes

class  basic_big_uint
 Fixed capacity unsigned big integer, 32 bit limbs, least significant first. More...
class  flag_filter
 Reduces what a math function's internal operations raise to what its result justifies. More...
class  flag_quiet
 Sets the exception flags aside for its lifetime, and discards whatever is raised meanwhile. More...
struct  float_env
 The per thread state behind the float128 environment. More...

Typedefs

using big_uint = basic_big_uint<560>
 The big integer every conversion uses, 560 limbs or 17920 bits.
using wide_big_uint = basic_big_uint<3072>
 The big integer that holds MAX_EXACT_DIGITS digits scaled to the smallest subnormal, about 94600 bits. Twelve kilobytes, so it is only ever allocated on the heap.

Enumerations

enum class  rounding : uint8_t { nearest_even , toward_zero , upward , downward }
 The rounding-direction attributes IEEE 754-2008 requires of a binary format. More...
enum class  digit_rounding : uint8_t { nearest_even , toward_zero , away_from_zero }
 How to_decimal_digits() rounds the last digit it keeps. More...

Functions

std::string 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.
constexpr rounding current_rounding () noexcept
 The rounding direction every float128 operation applies.
constexpr void raise_flags (int flags) noexcept
 Raises exception flags. Does nothing without FP128_IEEE_ENV or during constant evaluation.
digit_rounding digit_rounding_for (uint32_t sign) noexcept
 The digit rounding the current direction comes down to for a value of a given sign.
void 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 shortest_digit_count (const float128 &value)
 Fewest significant digits that read back as the same value.
const char * 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 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 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.
int32_t to_decimal_digits (uint64_t mantissa_low, uint64_t mantissa_high, int32_t exponent, int32_t requested, char *digits, bool *exact=nullptr, digit_rounding direction=digit_rounding::nearest_even) noexcept
 Correctly rounded decimal digits of a finite, non zero binary128 value.
template<int32_t Capacity>
bool decimal_to_binary (basic_big_uint< Capacity > &value, const char *digits, int32_t count, int32_t max_kept, int32_t exponent10, uint64_t &mantissa_low, uint64_t &mantissa_high, int32_t &exponent, uint64_t *extra) noexcept
 Reads a decimal mantissa and exponent back to the nearest binary128.
bool from_decimal_digits (const char *digits, int32_t count, int32_t exponent10, uint64_t &mantissa_low, uint64_t &mantissa_high, int32_t &exponent, uint64_t *extra=nullptr) noexcept
 Reads a decimal mantissa and exponent back to the nearest binary128.

Variables

constexpr uint64_t two_over_pi_bits []
 The binary expansion of 2/pi after the binary point, most significant word first.
constexpr int32_t DEFAULT_PRECISION = 6
 Default precision the e, f and g styles use when none is given, as in printf.
constexpr int32_t MAX_SIGNIFICANT_DIGITS = 40
 Largest number of significant digits any conversion here produces.
constexpr int32_t MAX_EXACT_DIGITS = 11700
 Significant digits that settle the rounding of any decimal input exactly.
constexpr int32_t MAX_OUTPUT_DIGITS = 5000
 Largest number of digits to_decimal_digits() produces.
constexpr int32_t FRACTION_LIMBS = 520
 Limbs of the fraction buffer. 16640 bits reaches below the smallest subnormal's last bit.

Typedef Documentation

◆ big_uint

The big integer every conversion uses, 560 limbs or 17920 bits.

Definition at line 329 of file fp128_decimal.h.

◆ wide_big_uint

The big integer that holds MAX_EXACT_DIGITS digits scaled to the smallest subnormal, about 94600 bits. Twelve kilobytes, so it is only ever allocated on the heap.

Definition at line 348 of file fp128_decimal.h.

Enumeration Type Documentation

◆ rounding

enum class fp128::detail::rounding : uint8_t
strong

The rounding-direction attributes IEEE 754-2008 requires of a binary format.

Enumerator
nearest_even 

roundTiesToEven, the default

toward_zero 

roundTowardZero

upward 

roundTowardPositive

downward 

roundTowardNegative

Definition at line 241 of file float128.h.

◆ digit_rounding

enum class fp128::detail::digit_rounding : uint8_t
strong

How to_decimal_digits() rounds the last digit it keeps.

Enumerator
nearest_even 

to the nearest digit, a tie to the even one

toward_zero 

truncate

away_from_zero 

up whenever anything was discarded

Definition at line 361 of file fp128_decimal.h.

Function Documentation

◆ render()

std::string fp128::detail::render ( const float128 & value,
char style,
int32_t precision,
bool uppercase,
bool alternate,
char sign_char,
bool bare_hex )
inlinenodiscard

Renders a float128 as text. Defined below, declared here because the conversions to a string delegate to it.

Renders a float128 as text.

The one place the character form of a value is produced. std::format, the stream inserter, to_chars and the conversion to std::string all come through here so that they cannot disagree with each other.

Parameters
valueValue to render
styleOne of 'a', 'e', 'f' or 'g', or '\0' for the shortest form that reads back exactly
precisionDigits after the point, or significant digits for 'g'. Negative selects the default for the style
uppercaseEmit the digits, the exponent marker and the special values in upper case
alternateKeep the decimal point and, for 'g', the trailing zeros
sign_charCharacter to emit for a non negative value: '-' means emit nothing
bare_hexFor the 'a' style, the form std::to_chars writes: no 0x prefix and the binary exponent with as few digits as it needs, 1.8p+1, which std::from_chars with chars_format::hex reads back. Otherwise the printf form, 0x1.8p+01.
Returns
The rendered text.

Definition at line 6817 of file float128.h.

◆ current_rounding()

rounding fp128::detail::current_rounding ( )
inlinenodiscardconstexprnoexcept

The rounding direction every float128 operation applies.

Constant evaluation always rounds to nearest: a thread local cannot be read there, and a constant whose value depended on the thread that happened to compile it would be meaningless anyway.

Never store the result in a const local: const rounding mode = current_rounding(); makes the initializer manifestly constant evaluated, so it would always see nearest_even. Pass the call as an argument, or keep the local non const.

Returns
The current rounding direction, or nearest_even without FP128_IEEE_ENV.

Definition at line 271 of file float128.h.

◆ raise_flags()

void fp128::detail::raise_flags ( int flags)
inlineconstexprnoexcept

Raises exception flags. Does nothing without FP128_IEEE_ENV or during constant evaluation.

Parameters
flagsFE_ bits to raise

Definition at line 284 of file float128.h.

◆ digit_rounding_for()

digit_rounding fp128::detail::digit_rounding_for ( uint32_t sign)
inlinenodiscardnoexcept

The digit rounding the current direction comes down to for a value of a given sign.

Parameters
signSign of the value being converted
Returns
How to round its last digit.

Definition at line 396 of file float128.h.

◆ append_exponent()

void fp128::detail::append_exponent ( std::string & out,
int32_t exponent,
char marker,
int32_t min_digits = 2 )
inline

Appends the decimal exponent of a scientific or hexadecimal form, with a sign.

Parameters
outText to append to
exponentThe exponent
markerThe character that introduces it: e, E, p or P
min_digitsFewest digits to show, zero padded: two as printf shows a double's decimal exponent, one as std::to_chars shows a hexadecimal one

Definition at line 6715 of file float128.h.

◆ shortest_digit_count()

int32_t fp128::detail::shortest_digit_count ( const float128 & value)
inlinenodiscard

Fewest significant digits that read back as the same value.

The search is a bisection rather than a scan because the property is monotone: a decimal with more digits is at least as close to the value as one with fewer, so if some length reads back correctly then every longer one does too.

Parameters
valueFinite, non zero value
Returns
Digit count in [1, 36].

Definition at line 6758 of file float128.h.

◆ parse_special_value()

const char * fp128::detail::parse_special_value ( const char * cursor,
const char * last,
bool negative,
float128 & value )
inline

Reads one of the special values: inf, infinity, nan, nan(n-char-sequence) or snan.

Case is ignored, as IEEE 754-2008 5.12.1 asks. A payload in parentheses is read the way fp128::nan() reads one, and the parentheses are only consumed when they are closed. "snan" reads as a signaling NaN, which the standard recommends and strtod() does not offer.

Parameters
cursorStart of the text, past any sign
lastEnd of the text
negativeThe sign that came before it
valueReceives the value
Returns
Past what was read, or nullptr when the text is none of these.

Definition at line 7152 of file float128.h.

◆ from_hex_chars()

std::from_chars_result fp128::detail::from_hex_chars ( const char * first,
const char * last,
float128 & value )
inline

Reads a hexadecimal significand with an optional binary exponent, such as 1.8p1.

The form printf's a writes, without its 0x prefix, as std::from_chars reads it for chars_format::hex. IEEE 754-2008 5.12.3 requires it, correctly rounded. Every digit is four bits of the significand, so the first 32 significant digits are collected exactly and any further non zero digit only counts towards the sticky bit; the value is then rounded once.

Parameters
firstStart of the text
lastOne past its end
valueReceives the parsed value, untouched when nothing was parsed
Returns
As from_chars().

Definition at line 7208 of file float128.h.

◆ round_decimal()

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 )
inlinenoexcept

Rounds digits * 10^exponent10, with the given sign, to a float128.

Template Parameters
CapacityLimbs of the workspace
Parameters
workspaceBig integer for decimal_to_binary() to compute in
digitsSignificant decimal digits, no sign and no point
countNumber of digits
max_keptDigits used as such; any after them only count as a sticky bit
exponent10Power of ten the digits are scaled by
sign1 for a negative value
valueReceives the result
Returns
result_out_of_range when it overflowed to an infinity or underflowed to zero, otherwise no error.

Definition at line 7329 of file float128.h.

◆ to_decimal_digits()

int32_t fp128::detail::to_decimal_digits ( uint64_t mantissa_low,
uint64_t mantissa_high,
int32_t exponent,
int32_t requested,
char * digits,
bool * exact = nullptr,
digit_rounding direction = digit_rounding::nearest_even )
inlinenoexcept

Correctly rounded decimal digits of a finite, non zero binary128 value.

The value is decomposed into an exact integer part and an exact fractional part, and the digits of each are produced by arithmetic that cannot round: repeated division by a power of ten for the integer part, repeated multiplication for the fraction. Only the last digit kept is rounded, once, from the exact remainder.

Parameters
mantissa_lowLow QWORD of the 113 bit mantissa, its leading one at bit 112
mantissa_highHigh QWORD of the mantissa
exponentUnbiased exponent, so the value is mantissa * 2^(exponent-112)
requestedSignificant digits wanted, clamped to MAX_SIGNIFICANT_DIGITS
digitsOutput buffer, receives requested digits as characters with no terminator
exactOptional, set to true when the digits are the whole value and nothing was discarded
directionHow the last digit is rounded: digit_rounding::nearest_even, the default, or towards zero or away from it, which is what the directed rounding modes come down to once the sign of the value is known
Returns
The decimal exponent: the value is 0.<digits> * 10^result.

Definition at line 386 of file fp128_decimal.h.

◆ decimal_to_binary()

template<int32_t Capacity>
bool fp128::detail::decimal_to_binary ( basic_big_uint< Capacity > & value,
const char * digits,
int32_t count,
int32_t max_kept,
int32_t exponent10,
uint64_t & mantissa_low,
uint64_t & mantissa_high,
int32_t & exponent,
uint64_t * extra )
inlinenoexcept

Reads a decimal mantissa and exponent back to the nearest binary128.

The value is digits * 10^exponent10. Both directions are computed exactly - a positive power of ten by multiplication, a negative one by division with the discarded remainder kept as a sticky bit - so the 113 bit result is the correctly rounded one and never two roundings deep.

Template Parameters
CapacityLimbs of the workspace
Parameters
valueWorkspace, which has to hold max_kept digits scaled to the bottom of the format: a big_uint for MAX_SIGNIFICANT_DIGITS, a wide_big_uint for MAX_EXACT_DIGITS
digitsDecimal digits, most significant first, no sign and no point
countNumber of digits
max_keptSignificant digits used as such; any after them only count as a sticky bit
exponent10Power of ten the digits are scaled by
mantissa_lowReceives the low QWORD of the 113 bit mantissa
mantissa_highReceives the high QWORD
exponentReceives the unbiased exponent, so the value is mantissa * 2^(exponent-112)
extraOptional. When given, the mantissa is not rounded: it receives the top 113 bits, truncated, and this receives the bits below them in the form float128::round_pack() takes, the first one dropped in bit 63 and anything below it jammed into bit 0. A result that turns out subnormal keeps fewer than 113 bits, so only the caller can round it once.
Returns
False when the value is zero, or too large or too small for the format to hold, in which case the exponent says which: below -100000 for an underflow, above 100000 for an overflow.

Definition at line 533 of file fp128_decimal.h.

◆ from_decimal_digits()

bool fp128::detail::from_decimal_digits ( const char * digits,
int32_t count,
int32_t exponent10,
uint64_t & mantissa_low,
uint64_t & mantissa_high,
int32_t & exponent,
uint64_t * extra = nullptr )
inlinenoexcept

Reads a decimal mantissa and exponent back to the nearest binary128.

decimal_to_binary() with a big_uint of its own, keeping MAX_SIGNIFICANT_DIGITS digits and reading any after them as a sticky bit. See decimal_to_binary() for the parameters.

Definition at line 657 of file fp128_decimal.h.

Variable Documentation

◆ two_over_pi_bits

uint64_t fp128::detail::two_over_pi_bits[]
inlineconstexpr

The binary expansion of 2/pi after the binary point, most significant word first.

The trigonometric functions reduce a large argument by multiplying it with a window of these bits chosen by its exponent (see float128::reduce_large()). An argument near the top of the binary128 range reads about 16650 bits in. Generated by tools/gen_two_over_pi.py, which also verifies it; do not edit by hand.

Definition at line 533 of file float128.h.

◆ DEFAULT_PRECISION

int32_t fp128::detail::DEFAULT_PRECISION = 6
inlineconstexpr

Default precision the e, f and g styles use when none is given, as in printf.

Definition at line 6705 of file float128.h.

◆ MAX_SIGNIFICANT_DIGITS

int32_t fp128::detail::MAX_SIGNIFICANT_DIGITS = 40
inlineconstexpr

Largest number of significant digits any conversion here produces.

A binary128 needs 36 to survive a round trip, and one more is kept so that a caller asking for the maximum can still see the digit that decides the rounding.

Definition at line 335 of file fp128_decimal.h.

◆ MAX_EXACT_DIGITS

int32_t fp128::detail::MAX_EXACT_DIGITS = 11700
inlineconstexpr

Significant digits that settle the rounding of any decimal input exactly.

A rounding boundary - the point halfway between two neighbouring binary128 values - is an odd multiple of a power of two no smaller than 2^-16495, so its decimal expansion ends: at most 34 digits for the odd multiplier and 0.699 digits per binary place below one, about 11565 in all. Digits past this many cannot carry the value across a boundary, so they only need to be known to be non zero.

Definition at line 344 of file fp128_decimal.h.

◆ MAX_OUTPUT_DIGITS

int32_t fp128::detail::MAX_OUTPUT_DIGITS = 5000
inlineconstexpr

Largest number of digits to_decimal_digits() produces.

Enough to write any finite binary128 out in full in fixed notation, the widest of which has 4933 digits before the point. The exact expansion of a subnormal runs to 16494 places after the point instead, and a request past this cap is filled with zeros rather than continuing it.

Definition at line 355 of file fp128_decimal.h.

◆ FRACTION_LIMBS

int32_t fp128::detail::FRACTION_LIMBS = 520
inlineconstexpr

Limbs of the fraction buffer. 16640 bits reaches below the smallest subnormal's last bit.

Definition at line 358 of file fp128_decimal.h.