|
fixed_point128 1.0.1.0
128 bit integer, fixed point and floating point arithmetic for C++20
|
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. | |
| using fp128::detail::big_uint = basic_big_uint<560> |
The big integer every conversion uses, 560 limbs or 17920 bits.
Definition at line 329 of file fp128_decimal.h.
| using fp128::detail::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.
Definition at line 348 of file fp128_decimal.h.
|
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.
|
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.
|
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.
| value | Value to render |
| style | One of 'a', 'e', 'f' or 'g', or '\0' for the shortest form that reads back exactly |
| precision | Digits after the point, or significant digits for 'g'. Negative selects the default for the style |
| uppercase | Emit the digits, the exponent marker and the special values in upper case |
| alternate | Keep the decimal point and, for 'g', the trailing zeros |
| sign_char | Character to emit for a non negative value: '-' means emit nothing |
| bare_hex | For 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. |
Definition at line 6817 of file float128.h.
|
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.
Definition at line 271 of file float128.h.
|
inlineconstexprnoexcept |
Raises exception flags. Does nothing without FP128_IEEE_ENV or during constant evaluation.
| flags | FE_ bits to raise |
Definition at line 284 of file float128.h.
|
inlinenodiscardnoexcept |
The digit rounding the current direction comes down to for a value of a given sign.
| sign | Sign of the value being converted |
Definition at line 396 of file float128.h.
|
inline |
Appends the decimal exponent of a scientific or hexadecimal form, with a sign.
| out | Text to append to |
| exponent | The exponent |
| marker | The character that introduces it: e, E, p or P |
| min_digits | Fewest 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.
|
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.
| value | Finite, non zero value |
Definition at line 6758 of file float128.h.
|
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.
| cursor | Start of the text, past any sign |
| last | End of the text |
| negative | The sign that came before it |
| value | Receives the value |
Definition at line 7152 of file float128.h.
|
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.
| first | Start of the text |
| last | One past its end |
| value | Receives the parsed value, untouched when nothing was parsed |
Definition at line 7208 of file float128.h.
|
inlinenoexcept |
Rounds digits * 10^exponent10, with the given sign, to a float128.
| Capacity | Limbs of the workspace |
| workspace | Big integer for decimal_to_binary() to compute in |
| digits | Significant decimal digits, no sign and no point |
| count | Number of digits |
| max_kept | Digits used as such; any after them only count as a sticky bit |
| exponent10 | Power of ten the digits are scaled by |
| sign | 1 for a negative value |
| value | Receives the result |
Definition at line 7329 of file float128.h.
|
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.
| mantissa_low | Low QWORD of the 113 bit mantissa, its leading one at bit 112 |
| mantissa_high | High QWORD of the mantissa |
| exponent | Unbiased exponent, so the value is mantissa * 2^(exponent-112) |
| requested | Significant digits wanted, clamped to MAX_SIGNIFICANT_DIGITS |
| digits | Output buffer, receives requested digits as characters with no terminator |
| exact | Optional, set to true when the digits are the whole value and nothing was discarded |
| direction | How 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 |
Definition at line 386 of file fp128_decimal.h.
|
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.
| Capacity | Limbs of the workspace |
| value | Workspace, 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 |
| digits | Decimal digits, most significant first, no sign and no point |
| count | Number of digits |
| max_kept | Significant digits used as such; any after them only count as a sticky bit |
| exponent10 | Power of ten the digits are scaled by |
| mantissa_low | Receives the low QWORD of the 113 bit mantissa |
| mantissa_high | Receives the high QWORD |
| exponent | Receives the unbiased exponent, so the value is mantissa * 2^(exponent-112) |
| extra | Optional. 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. |
Definition at line 533 of file fp128_decimal.h.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.