45#ifndef FP128_DECIMAL_H
46#define FP128_DECIMAL_H
78 void set(uint64_t low, uint64_t high)
noexcept
81 limb[0] =
static_cast<uint32_t
>(low);
82 limb[1] =
static_cast<uint32_t
>(low >> 32);
83 limb[2] =
static_cast<uint32_t
>(high);
84 limb[3] =
static_cast<uint32_t
>(high >> 32);
92 for (int32_t i = 0; i < used; ++i)
97 [[nodiscard]]
bool is_zero() const noexcept {
return used == 0; }
98 [[nodiscard]] int32_t
limbs() const noexcept {
return used; }
99 [[nodiscard]] uint32_t
at(int32_t index)
const noexcept {
return (index < used) ? limb[index] : 0u; }
106 const uint32_t top = limb[used - 1];
108 for (uint32_t probe = top; probe != 0; probe >>= 1)
119 for (int32_t i = 0; i < used; ++i) {
120 const uint64_t product =
static_cast<uint64_t
>(limb[i]) * multiplier + carry;
121 limb[i] =
static_cast<uint32_t
>(product);
127 limb[used++] =
static_cast<uint32_t
>(carry);
136 uint64_t carry = addend;
137 for (int32_t i = 0; carry != 0; ++i) {
144 const uint64_t sum =
static_cast<uint64_t
>(limb[i]) + carry;
145 limb[i] =
static_cast<uint32_t
>(sum);
156 for (int32_t i = used - 1; i >= 0; --i) {
158 limb[i] =
static_cast<uint32_t
>(dividend / divisor);
195 if (bits <= 0 || used == 0)
198 const int32_t limb_shift = bits /
LIMB_BITS;
199 const int32_t bit_shift = bits %
LIMB_BITS;
200 const int32_t extra = (bit_shift != 0) ? 1 : 0;
201 if (used + limb_shift + extra >
CAPACITY)
205 for (int32_t i = used - 1 + extra; i >= 0; --i) {
207 if (bit_shift == 0) {
208 value = (i < used) ? limb[i] : 0u;
210 const uint32_t high = (i < used) ? limb[i] : 0u;
211 const uint32_t low = (i > 0) ? limb[i - 1] : 0u;
212 value =
static_cast<uint32_t
>((high << bit_shift) | (low >> (
LIMB_BITS - bit_shift)));
214 limb[i + limb_shift] = value;
216 for (int32_t i = 0; i < limb_shift; ++i)
219 used += limb_shift + extra;
227 if (bits <= 0 || used == 0)
229 const int32_t limb_shift = bits /
LIMB_BITS;
230 const int32_t bit_shift = bits %
LIMB_BITS;
231 if (limb_shift >= used) {
236 for (int32_t i = 0; i + limb_shift < used; ++i) {
237 const uint32_t low = limb[i + limb_shift];
238 if (bit_shift == 0) {
241 const uint32_t high = (i + limb_shift + 1 < used) ? limb[i + limb_shift + 1] : 0u;
242 limb[i] =
static_cast<uint32_t
>((low >> bit_shift) | (high << (
LIMB_BITS - bit_shift)));
245 const int32_t remaining = used - limb_shift;
246 for (int32_t i = remaining; i < used; ++i)
259 const int32_t limb_index = bits /
LIMB_BITS;
260 const int32_t bit_index = bits %
LIMB_BITS;
261 if (limb_index >= used)
264 for (int32_t i = limb_index + ((bit_index != 0) ? 1 : 0); i < used; ++i)
267 limb[limb_index] &= (1u << bit_index) - 1u;
285 for (int32_t i = 0; i < width; ++i) {
286 const uint64_t product =
static_cast<uint64_t
>(limb[i]) * multiplier + carry;
287 limb[i] =
static_cast<uint32_t
>(product);
292 return static_cast<uint32_t
>(carry);
299 const uint32_t top =
at(width - 1);
300 constexpr uint32_t half_limb = 1u << (
LIMB_BITS - 1);
305 for (int32_t i = 0; i < width - 1; ++i) {
313 static constexpr uint32_t
POWERS_OF_10[10] = {1u, 10u, 100u, 1000u, 10000u,
314 100000u, 1000000u, 10000000u, 100000000u, 1000000000u};
320 while (used > 0 && limb[used - 1] == 0)
386inline int32_t
to_decimal_digits(uint64_t mantissa_low, uint64_t mantissa_high, int32_t exponent, int32_t requested,
char* digits,
395 const int32_t binary_exponent = exponent - 112;
399 integer_part.
set(mantissa_low, mantissa_high);
401 if (binary_exponent >= 0) {
404 const int32_t below_point = -binary_exponent;
408 fraction.
set(mantissa_low, mantissa_high);
416 int32_t integer_digits = 0;
417 while (!integer_part.
is_zero() && integer_digits + 9 <=
static_cast<int32_t
>(
sizeof(reversed))) {
418 uint32_t block = integer_part.
divmod_small(1000000000u);
419 for (int32_t i = 0; i < 9; ++i) {
420 reversed[integer_digits++] =
static_cast<char>(
'0' + (block % 10));
425 while (integer_digits > 0 && reversed[integer_digits - 1] ==
'0')
430 int32_t decimal_exponent = integer_digits;
432 char rounding_digit =
'0';
433 bool have_rounding_digit =
false;
434 bool rest_nonzero =
false;
435 bool seen_significant = integer_digits > 0;
437 const auto append_digit = [&](
char digit) {
438 if (emitted < requested)
439 digits[emitted++] = digit;
440 else if (!have_rounding_digit) {
441 rounding_digit = digit;
442 have_rounding_digit =
true;
444 rest_nonzero = rest_nonzero || (digit !=
'0');
448 for (int32_t i = integer_digits - 1; i >= 0; --i)
449 append_digit(reversed[i]);
452 while (!fraction.
is_zero() && (emitted < requested || !have_rounding_digit)) {
454 char block_digits[9];
455 for (int32_t i = 8; i >= 0; --i) {
456 block_digits[i] =
static_cast<char>(
'0' + (block % 10));
459 for (int32_t i = 0; i < 9; ++i) {
460 if (!seen_significant) {
461 if (block_digits[i] ==
'0') {
465 seen_significant =
true;
467 append_digit(block_digits[i]);
471 rest_nonzero = rest_nonzero || !fraction.
is_zero();
474 while (emitted < requested)
475 digits[emitted++] =
'0';
477 if (exact !=
nullptr)
478 *exact = !rest_nonzero && (!have_rounding_digit || rounding_digit ==
'0');
481 bool round_up =
false;
483 if (rounding_digit >
'5')
485 else if (rounding_digit ==
'5')
486 round_up = rest_nonzero || ((digits[requested - 1] -
'0') % 2 != 0);
488 round_up = rest_nonzero || (have_rounding_digit && rounding_digit !=
'0');
492 int32_t index = requested - 1;
493 while (index >= 0 && digits[index] ==
'9')
494 digits[index--] =
'0';
504 return decimal_exponent;
532template <
int32_t Capacity>
534 uint64_t& mantissa_low, uint64_t& mantissa_high, int32_t& exponent, uint64_t* extra)
noexcept
536 mantissa_low = mantissa_high = 0;
541 while (first < count && digits[first] ==
'0')
545 while (count > first && digits[count - 1] ==
'0') {
554 int32_t kept = count - first;
556 if (kept > max_kept) {
557 for (int32_t i = first + max_kept; i < count; ++i)
558 sticky = sticky || (digits[i] !=
'0');
559 exponent10 += kept - max_kept;
564 for (int32_t i = 0; i < kept; ++i) {
565 value.mul_small(10u);
566 value.add_small(
static_cast<uint32_t
>(digits[first + i] -
'0'));
573 const int32_t magnitude = exponent10 + kept;
574 if (magnitude > 4935) {
578 if (magnitude < -4970) {
583 int32_t binary_exponent = 0;
584 if (exponent10 >= 0) {
585 if (!value.mul_pow10(exponent10)) {
595 const int32_t headroom = (-exponent10) * 10 / 3 + 160;
596 if (!value.shift_left(headroom)) {
600 binary_exponent -= headroom;
601 value.div_pow10(-exponent10, sticky);
604 if (value.is_zero()) {
610 const int32_t bits = value.bit_length();
611 const int32_t drop = bits - 113;
614 const int32_t guard_index = drop - 1;
616 for (int32_t i = 0; i < guard_index && !sticky; ++i)
619 for (int32_t i = 0; i < drop; ++i)
620 value.divmod_small(2);
621 binary_exponent += drop;
623 if (extra !=
nullptr) {
624 *extra = (
static_cast<uint64_t
>(guard) << 63) | (sticky ? 1u : 0u);
627 else if (guard != 0 && (sticky || (value.at(0) & 1u) != 0)) {
630 if (value.bit_length() > 113) {
631 value.divmod_small(2);
637 value.shift_left(-drop);
638 binary_exponent += drop;
640 if (extra !=
nullptr)
641 *extra = sticky ? 1u : 0u;
644 mantissa_low =
static_cast<uint64_t
>(value.at(0)) | (
static_cast<uint64_t
>(value.at(1)) << 32);
645 mantissa_high =
static_cast<uint64_t
>(value.at(2)) | (
static_cast<uint64_t
>(value.at(3)) << 32);
647 exponent = binary_exponent + 112;
657inline bool from_decimal_digits(
const char* digits, int32_t count, int32_t exponent10, uint64_t& mantissa_low, uint64_t& mantissa_high,
658 int32_t& exponent, uint64_t* extra =
nullptr) noexcept
Fixed capacity unsigned big integer, 32 bit limbs, least significant first.
bool mul_small(uint32_t multiplier) noexcept
Multiplies by a value that fits in a limb.
static constexpr uint64_t LIMB_BASE
static constexpr int32_t CAPACITY
int32_t compare_half(int32_t width) const noexcept
Compares the value against one half of 2^(width*32), the fraction's midpoint.
uint32_t fraction_mul_extract(uint32_t multiplier, int32_t width) noexcept
Treats the value as a fraction, multiplies it by a small value and peels off the integer part that cr...
void keep_low_bits(int32_t bits) noexcept
Clears every bit at or above the given position.
int32_t limbs() const noexcept
uint32_t divmod_small(uint32_t divisor) noexcept
Divides by a value that fits in a limb.
bool shift_left(int32_t bits) noexcept
Shifts left, growing the value.
void shift_right(int32_t bits) noexcept
Shifts right, discarding the bits that fall off the bottom.
bool mul_pow10(int32_t count) noexcept
Multiplies in place by 10^count, in blocks that fit in a limb.
basic_big_uint() noexcept
Constructs zero.
bool is_zero() const noexcept
static constexpr uint32_t POWERS_OF_10[10]
uint32_t at(int32_t index) const noexcept
void div_pow10(int32_t count, bool &sticky) noexcept
Divides in place by 10^count, reporting whether anything was discarded.
void set(uint64_t low, uint64_t high) noexcept
Sets the value to a 128 bit integer.
void clear() noexcept
Sets the value to zero.
int32_t bit_length() const noexcept
Index of the highest set bit, or -1 when the value is zero.
static constexpr int32_t LIMB_BITS
void add_small(uint32_t addend) noexcept
Adds a value that fits in a limb.
Shared definitions, intrinsics, and helper functions for 128-bit fixed-point arithmetic.
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.
constexpr int32_t FRACTION_LIMBS
Limbs of the fraction buffer. 16640 bits reaches below the smallest subnormal's last bit.
constexpr int32_t MAX_EXACT_DIGITS
Significant digits that settle the rounding of any decimal input exactly.
digit_rounding
How to_decimal_digits() rounds the last digit it keeps.
@ nearest_even
to the nearest digit, a tie to the even one
@ away_from_zero
up whenever anything was discarded
basic_big_uint< 3072 > wide_big_uint
The big integer that holds MAX_EXACT_DIGITS digits scaled to the smallest subnormal,...
basic_big_uint< 560 > big_uint
The big integer every conversion uses, 560 limbs or 17920 bits.
@ toward_zero
roundTowardZero
@ nearest_even
roundTiesToEven, the default
constexpr int32_t MAX_OUTPUT_DIGITS
Largest number of digits to_decimal_digits() produces.
constexpr int32_t MAX_SIGNIFICANT_DIGITS
Largest number of significant digits any conversion here produces.
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.
Everything the library provides.
float128 remainder(const float128 &x, const float128 &y) noexcept