strconv: drop base from wuffs_base__private_impl

This commit is contained in:
Nigel Tao committed 2023-10-29 21:59:52 +11:00
1 parent 2890d76845
commit 054c6c964b
5 files changed
+433 -534

No files matched your search

+179 -223
View File
@@ -140,27 +140,27 @@ wuffs_base__ieee_754_bit_representation__from_f64_to_u32_truncate(double f) {
// --------
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE 2047
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION 800
#define WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE 2047
#define WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION 800
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL is the largest N
// such that ((10 << N) < (1 << 64)).
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL 60
// WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL is the largest N such that
// ((10 << N) < (1 << 64)).
#define WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL 60
// wuffs_base__private_implementation__high_prec_dec (abbreviated as HPD) is a
// fixed precision floating point decimal number, augmented with ±infinity
// values, but it cannot represent NaN (Not a Number).
// wuffs_private_impl__high_prec_dec (abbreviated as HPD) is a fixed precision
// floating point decimal number, augmented with ±infinity values, but it
// cannot represent NaN (Not a Number).
//
// "High precision" means that the mantissa holds 800 decimal digits. 800 is
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION.
// WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION.
//
// An HPD isn't for general purpose arithmetic, only for conversions to and
// from IEEE 754 double-precision floating point, where the largest and
// smallest positive, finite values are approximately 1.8e+308 and 4.9e-324.
// HPD exponents above +2047 mean infinity, below -2047 mean zero. The ±2047
// bounds are further away from zero than ±(324 + 800), where 800 and 2047 is
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION and
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.
// WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION and
// WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE.
//
// digits[.. num_digits] are the number's digits in big-endian order. The
// uint8_t values are in the range [0 ..= 9], not ['0' ..= '9'], where e.g. '7'
@@ -186,41 +186,38 @@ wuffs_base__ieee_754_bit_representation__from_f64_to_u32_truncate(double f) {
// negative is a sign bit. An HPD can distinguish positive and negative zero.
//
// truncated is whether there are more than
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION digits, and at
// least one of those extra digits are non-zero. The existence of long-tail
// digits can affect rounding.
// WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION digits, and at least one of those
// extra digits are non-zero. The existence of long-tail digits can affect
// rounding.
//
// The "all fields are zero" value is valid, and represents the number +0.
typedef struct wuffs_base__private_implementation__high_prec_dec__struct {
typedef struct wuffs_private_impl__high_prec_dec__struct {
uint32_t num_digits;
int32_t decimal_point;
bool negative;
bool truncated;
uint8_t digits[WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION];
} wuffs_base__private_implementation__high_prec_dec;
uint8_t digits[WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION];
} wuffs_private_impl__high_prec_dec;
// wuffs_base__private_implementation__high_prec_dec__trim trims trailing
// zeroes from the h->digits[.. h->num_digits] slice. They have no benefit,
// since we explicitly track h->decimal_point.
// wuffs_private_impl__high_prec_dec__trim trims trailing zeroes from the
// h->digits[.. h->num_digits] slice. They have no benefit, since we explicitly
// track h->decimal_point.
//
// Preconditions:
// - h is non-NULL.
static inline void //
wuffs_base__private_implementation__high_prec_dec__trim(
wuffs_base__private_implementation__high_prec_dec* h) {
wuffs_private_impl__high_prec_dec__trim(wuffs_private_impl__high_prec_dec* h) {
while ((h->num_digits > 0) && (h->digits[h->num_digits - 1] == 0)) {
h->num_digits--;
}
}
// wuffs_base__private_implementation__high_prec_dec__assign sets h to
// represent the number x.
// wuffs_private_impl__high_prec_dec__assign sets h to represent the number x.
//
// Preconditions:
// - h is non-NULL.
static void //
wuffs_base__private_implementation__high_prec_dec__assign(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__assign(wuffs_private_impl__high_prec_dec* h,
uint64_t x,
bool negative) {
uint32_t n = 0;
@@ -252,12 +249,11 @@ wuffs_base__private_implementation__high_prec_dec__assign(
h->decimal_point = (int32_t)n;
h->negative = negative;
h->truncated = false;
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
}
static wuffs_base__status //
wuffs_base__private_implementation__high_prec_dec__parse(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h,
wuffs_base__slice_u8 s,
uint32_t options) {
if (!h) {
@@ -348,7 +344,7 @@ wuffs_base__private_implementation__high_prec_dec__parse(
if (p >= q) {
goto after_all;
} else if (('0' <= *p) && (*p <= '9')) {
if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (nd < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[nd++] = (uint8_t)(*p - '0');
dp = (int32_t)nd;
} else if ('0' != *p) {
@@ -390,12 +386,11 @@ after_sep:
if (nd == 0) {
// Track leading zeroes implicitly.
dp--;
} else if (nd <
WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
} else if (nd < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[nd++] = (uint8_t)(*p - '0');
}
} else if (('0' < *p) && (*p <= '9')) {
if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (nd < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[nd++] = (uint8_t)(*p - '0');
} else {
// Long-tail non-zeroes set the truncated bit.
@@ -431,9 +426,8 @@ after_exp:
}
int32_t exp = 0;
const int32_t exp_large =
WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE +
WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;
const int32_t exp_large = WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE +
WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION;
bool saw_exp_digits = false;
for (; p < q; p++) {
if ((*p == '_') &&
@@ -464,44 +458,39 @@ after_all:
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
h->decimal_point = 0;
} else if (dp <
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
h->decimal_point =
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE - 1;
} else if (dp >
+WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
h->decimal_point =
+WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE + 1;
} else if (dp < -WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
h->decimal_point = -WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE - 1;
} else if (dp > +WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
h->decimal_point = +WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE + 1;
} else {
h->decimal_point = dp;
}
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
return wuffs_base__make_status(NULL);
}
// --------
// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits
// returns the number of additional decimal digits when left-shifting by shift.
// wuffs_private_impl__high_prec_dec__lshift_num_new_digits returns the number
// of additional decimal digits when left-shifting by shift.
//
// See below for preconditions.
static uint32_t //
wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__lshift_num_new_digits(
wuffs_private_impl__high_prec_dec* h,
uint32_t shift) {
// Masking with 0x3F should be unnecessary (assuming the preconditions) but
// it's cheap and ensures that we don't overflow the
// wuffs_base__private_implementation__hpd_left_shift array.
// wuffs_private_impl__hpd_left_shift array.
shift &= 63;
uint32_t x_a = wuffs_base__private_implementation__hpd_left_shift[shift];
uint32_t x_b = wuffs_base__private_implementation__hpd_left_shift[shift + 1];
uint32_t x_a = wuffs_private_impl__hpd_left_shift[shift];
uint32_t x_b = wuffs_private_impl__hpd_left_shift[shift + 1];
uint32_t num_new_digits = x_a >> 11;
uint32_t pow5_a = 0x7FF & x_a;
uint32_t pow5_b = 0x7FF & x_b;
const uint8_t* pow5 =
&wuffs_base__private_implementation__powers_of_5[pow5_a];
const uint8_t* pow5 = &wuffs_private_impl__powers_of_5[pow5_a];
uint32_t i = 0;
uint32_t n = pow5_b - pow5_a;
for (; i < n; i++) {
@@ -520,9 +509,9 @@ wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(
// --------
// wuffs_base__private_implementation__high_prec_dec__rounded_integer returns
// the integral (non-fractional) part of h, provided that it is 18 or fewer
// decimal digits. For 19 or more digits, it returns UINT64_MAX. Note that:
// wuffs_private_impl__high_prec_dec__rounded_integer returns the integral
// (non-fractional) part of h, provided that it is 18 or fewer decimal digits.
// For 19 or more digits, it returns UINT64_MAX. Note that:
// - (1 << 53) is 9007199254740992, which has 16 decimal digits.
// - (1 << 56) is 72057594037927936, which has 17 decimal digits.
// - (1 << 59) is 576460752303423488, which has 18 decimal digits.
@@ -535,8 +524,8 @@ wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(
//
// See below for preconditions.
static uint64_t //
wuffs_base__private_implementation__high_prec_dec__rounded_integer(
wuffs_base__private_implementation__high_prec_dec* h) {
wuffs_private_impl__high_prec_dec__rounded_integer(
wuffs_private_impl__high_prec_dec* h) {
if ((h->num_digits == 0) || (h->decimal_point < 0)) {
return 0;
} else if (h->decimal_point > 18) {
@@ -567,8 +556,8 @@ wuffs_base__private_implementation__high_prec_dec__rounded_integer(
return n;
}
// wuffs_base__private_implementation__high_prec_dec__small_xshift shifts h's
// number (where 'x' is 'l' or 'r' for left or right) by a small shift value.
// wuffs_private_impl__high_prec_dec__small_xshift shifts h's number (where 'x'
// is 'l' or 'r' for left or right) by a small shift value.
//
// Preconditions:
// - h is non-NULL.
@@ -576,31 +565,29 @@ wuffs_base__private_implementation__high_prec_dec__rounded_integer(
// - shift is non-zero.
// - shift is "a small shift".
//
// "Not extreme" means within
// ±WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.
// "Not extreme" means within ±WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE.
//
// "A small shift" means not more than
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.
// WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL.
//
// wuffs_base__private_implementation__high_prec_dec__rounded_integer and
// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits
// have the same preconditions.
// wuffs_private_impl__high_prec_dec__rounded_integer and
// wuffs_private_impl__high_prec_dec__lshift_num_new_digits have the same
// preconditions.
//
// wuffs_base__private_implementation__high_prec_dec__lshift keeps the first
// two preconditions but not the last two. Its shift argument is signed and
// does not need to be "small": zero is a no-op, positive means left shift and
// negative means right shift.
// wuffs_private_impl__high_prec_dec__lshift keeps the first two preconditions
// but not the last two. Its shift argument is signed and does not need to be
// "small": zero is a no-op, positive means left shift and negative means right
// shift.
static void //
wuffs_base__private_implementation__high_prec_dec__small_lshift(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__small_lshift(
wuffs_private_impl__high_prec_dec* h,
uint32_t shift) {
if (h->num_digits == 0) {
return;
}
uint32_t num_new_digits =
wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(
h, shift);
wuffs_private_impl__high_prec_dec__lshift_num_new_digits(h, shift);
uint32_t rx = h->num_digits - 1; // Read index.
uint32_t wx = h->num_digits - 1 + num_new_digits; // Write index.
uint64_t n = 0;
@@ -610,7 +597,7 @@ wuffs_base__private_implementation__high_prec_dec__small_lshift(
n += ((uint64_t)(h->digits[rx])) << shift;
uint64_t quo = n / 10;
uint64_t rem = n - (10 * quo);
if (wx < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (wx < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[wx] = (uint8_t)rem;
} else if (rem > 0) {
h->truncated = true;
@@ -624,7 +611,7 @@ wuffs_base__private_implementation__high_prec_dec__small_lshift(
while (n > 0) {
uint64_t quo = n / 10;
uint64_t rem = n - (10 * quo);
if (wx < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (wx < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[wx] = (uint8_t)rem;
} else if (rem > 0) {
h->truncated = true;
@@ -635,17 +622,16 @@ wuffs_base__private_implementation__high_prec_dec__small_lshift(
// Finish.
h->num_digits += num_new_digits;
if (h->num_digits >
WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
h->num_digits = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;
if (h->num_digits > WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->num_digits = WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION;
}
h->decimal_point += (int32_t)num_new_digits;
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
}
static void //
wuffs_base__private_implementation__high_prec_dec__small_rshift(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__small_rshift(
wuffs_private_impl__high_prec_dec* h,
uint32_t shift) {
uint32_t rx = 0; // Read index.
uint32_t wx = 0; // Write index.
@@ -669,8 +655,7 @@ wuffs_base__private_implementation__high_prec_dec__small_rshift(
}
}
h->decimal_point -= ((int32_t)(rx - 1));
if (h->decimal_point <
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
if (h->decimal_point < -WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
// After the shift, h's number is effectively zero.
h->num_digits = 0;
h->decimal_point = 0;
@@ -690,7 +675,7 @@ wuffs_base__private_implementation__high_prec_dec__small_rshift(
while (n > 0) {
uint8_t new_digit = ((uint8_t)(n >> shift));
n = 10 * (n & mask);
if (wx < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (wx < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[wx++] = new_digit;
} else if (new_digit > 0) {
h->truncated = true;
@@ -699,62 +684,58 @@ wuffs_base__private_implementation__high_prec_dec__small_rshift(
// Finish.
h->num_digits = wx;
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
}
static void //
wuffs_base__private_implementation__high_prec_dec__lshift(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__lshift(wuffs_private_impl__high_prec_dec* h,
int32_t shift) {
if (shift > 0) {
while (shift > +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {
wuffs_base__private_implementation__high_prec_dec__small_lshift(
h, WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL);
shift -= WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
while (shift > +WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL) {
wuffs_private_impl__high_prec_dec__small_lshift(
h, WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL);
shift -= WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
}
wuffs_base__private_implementation__high_prec_dec__small_lshift(
h, ((uint32_t)(+shift)));
wuffs_private_impl__high_prec_dec__small_lshift(h, ((uint32_t)(+shift)));
} else if (shift < 0) {
while (shift < -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {
wuffs_base__private_implementation__high_prec_dec__small_rshift(
h, WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL);
shift += WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
while (shift < -WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL) {
wuffs_private_impl__high_prec_dec__small_rshift(
h, WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL);
shift += WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
}
wuffs_base__private_implementation__high_prec_dec__small_rshift(
h, ((uint32_t)(-shift)));
wuffs_private_impl__high_prec_dec__small_rshift(h, ((uint32_t)(-shift)));
}
}
// --------
// wuffs_base__private_implementation__high_prec_dec__round_etc rounds h's
// number. For those functions that take an n argument, rounding produces at
// most n digits (which is not necessarily at most n decimal places). Negative
// n values are ignored, as well as any n greater than or equal to h's number
// of digits. The etc__round_just_enough function implicitly chooses an n to
// implement WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION.
// wuffs_private_impl__high_prec_dec__round_etc rounds h's number. For those
// functions that take an n argument, rounding produces at most n digits (which
// is not necessarily at most n decimal places). Negative n values are ignored,
// as well as any n greater than or equal to h's number of digits. The
// etc__round_just_enough function implicitly chooses an n to implement
// WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION.
//
// Preconditions:
// - h is non-NULL.
// - h->decimal_point is "not extreme".
//
// "Not extreme" means within
// ±WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.
// "Not extreme" means within ±WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE.
static void //
wuffs_base__private_implementation__high_prec_dec__round_down(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__round_down(
wuffs_private_impl__high_prec_dec* h,
int32_t n) {
if ((n < 0) || (h->num_digits <= (uint32_t)n)) {
return;
}
h->num_digits = (uint32_t)(n);
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
}
static void //
wuffs_base__private_implementation__high_prec_dec__round_up(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__round_up(
wuffs_private_impl__high_prec_dec* h,
int32_t n) {
if ((n < 0) || (h->num_digits <= (uint32_t)n)) {
return;
@@ -775,8 +756,8 @@ wuffs_base__private_implementation__high_prec_dec__round_up(
}
static void //
wuffs_base__private_implementation__high_prec_dec__round_nearest(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__round_nearest(
wuffs_private_impl__high_prec_dec* h,
int32_t n) {
if ((n < 0) || (h->num_digits <= (uint32_t)n)) {
return;
@@ -788,15 +769,15 @@ wuffs_base__private_implementation__high_prec_dec__round_nearest(
}
if (up) {
wuffs_base__private_implementation__high_prec_dec__round_up(h, n);
wuffs_private_impl__high_prec_dec__round_up(h, n);
} else {
wuffs_base__private_implementation__high_prec_dec__round_down(h, n);
wuffs_private_impl__high_prec_dec__round_down(h, n);
}
}
static void //
wuffs_base__private_implementation__high_prec_dec__round_just_enough(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__round_just_enough(
wuffs_private_impl__high_prec_dec* h,
int32_t exp2,
uint64_t mantissa) {
// The magic numbers 52 and 53 in this function are because IEEE 754 double
@@ -834,11 +815,10 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
l_exp2 = exp2 - 1;
l_mantissa = (2 * mantissa) - 1;
}
wuffs_base__private_implementation__high_prec_dec lower;
wuffs_base__private_implementation__high_prec_dec__assign(
&lower, (2 * l_mantissa) + 1, false);
wuffs_base__private_implementation__high_prec_dec__lshift(&lower,
l_exp2 - 53);
wuffs_private_impl__high_prec_dec lower;
wuffs_private_impl__high_prec_dec__assign(&lower, (2 * l_mantissa) + 1,
false);
wuffs_private_impl__high_prec_dec__lshift(&lower, l_exp2 - 53);
// Next, the upper bound. Our number f is:
// ((mantissa + 0) * (2 ** (exp2 - 52)))
@@ -848,10 +828,9 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
//
// The upper bound is halfway between them (noting that 52 became 53):
// (((2 * mantissa) + 1) * (2 ** (exp2 - 53)))
wuffs_base__private_implementation__high_prec_dec upper;
wuffs_base__private_implementation__high_prec_dec__assign(
&upper, (2 * mantissa) + 1, false);
wuffs_base__private_implementation__high_prec_dec__lshift(&upper, exp2 - 53);
wuffs_private_impl__high_prec_dec upper;
wuffs_private_impl__high_prec_dec__assign(&upper, (2 * mantissa) + 1, false);
wuffs_private_impl__high_prec_dec__lshift(&upper, exp2 - 53);
// The lower and upper bounds are possible outputs only if the original
// mantissa is even, so that IEEE round-to-even would round to the original
@@ -934,17 +913,15 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// way, do it. If we can't round, continue the loop.
if (can_round_down) {
if (can_round_up) {
wuffs_base__private_implementation__high_prec_dec__round_nearest(
h, hi + 1);
wuffs_private_impl__high_prec_dec__round_nearest(h, hi + 1);
return;
} else {
wuffs_base__private_implementation__high_prec_dec__round_down(h,
hi + 1);
wuffs_private_impl__high_prec_dec__round_down(h, hi + 1);
return;
}
} else {
if (can_round_up) {
wuffs_base__private_implementation__high_prec_dec__round_up(h, hi + 1);
wuffs_private_impl__high_prec_dec__round_up(h, hi + 1);
return;
}
}
@@ -953,9 +930,9 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// --------
// wuffs_base__private_implementation__parse_number_f64_eisel_lemire produces
// the IEEE 754 double-precision value for an exact mantissa and base-10
// exponent. For example:
// wuffs_private_impl__parse_number_f64_eisel_lemire produces the IEEE 754
// double-precision value for an exact mantissa and base-10 exponent. For
// example:
// - when parsing "12345.678e+02", man is 12345678 and exp10 is -1.
// - when parsing "-12", man is 12 and exp10 is 0. Processing the leading
// minus sign is the responsibility of the caller, not this function.
@@ -974,7 +951,7 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// Preconditions:
// - man is non-zero.
// - exp10 is in the range [-307 ..= 288], the same range of the
// wuffs_base__private_implementation__powers_of_10 array.
// wuffs_private_impl__powers_of_10 array.
//
// The exp10 range (and the fact that man is in the range [1 ..= UINT64_MAX],
// approximately [1 ..= 1.85e+19]) means that (man * (10 ** exp10)) is in the
@@ -982,14 +959,11 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// (neither subnormal nor non-finite) f64 values: DBL_MIN and DBL_MAX are
// approximately 2.23e–308 and 1.80e+308.
static int64_t //
wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
uint64_t man,
int32_t exp10) {
wuffs_private_impl__parse_number_f64_eisel_lemire(uint64_t man, int32_t exp10) {
// Look up the (possibly truncated) base-2 representation of (10 ** exp10).
// The look-up table was constructed so that it is already normalized: the
// table entry's mantissa's MSB (most significant bit) is on.
const uint64_t* po10 =
&wuffs_base__private_implementation__powers_of_10[exp10 + 307][0];
const uint64_t* po10 = &wuffs_private_impl__powers_of_10[exp10 + 307][0];
// Normalize the man argument. The (man != 0) precondition means that a
// non-zero bit exists.
@@ -1147,8 +1121,7 @@ wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
// --------
static wuffs_base__result_f64 //
wuffs_base__private_implementation__parse_number_f64_special(
wuffs_base__slice_u8 s,
wuffs_private_impl__parse_number_f64_special(wuffs_base__slice_u8 s,
uint32_t options) {
do {
if (options & WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN) {
@@ -1253,17 +1226,16 @@ fail:
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //
wuffs_base__private_implementation__high_prec_dec__to_f64(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__to_f64(wuffs_private_impl__high_prec_dec* h,
uint32_t options) {
do {
// powers converts decimal powers of 10 to binary powers of 2. For example,
// (10000 >> 13) is 1. It stops before the elements exceed 60, also known
// as WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.
// as WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL.
//
// This rounds down (1<<13 is a lower bound for 1e4). Adding 1 to the array
// element value rounds up (1<<14 is an upper bound for 1e4) while staying
// at or below WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.
// at or below WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL.
//
// When starting in the range [1e+1 .. 1e+2] (i.e. h->decimal_point == +2),
// powers[2] == 6 and so:
@@ -1318,14 +1290,12 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
}
while (man != 0) { // The 'while' is just an 'if' that we can 'break'.
int64_t r0 =
wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
man + 0, exp10);
wuffs_private_impl__parse_number_f64_eisel_lemire(man + 0, exp10);
if (r0 < 0) {
break;
} else if (h->num_digits > 19) {
int64_t r1 =
wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
man + 1, exp10);
wuffs_private_impl__parse_number_f64_eisel_lemire(man + 1, exp10);
if (r1 != r0) {
break;
}
@@ -1349,14 +1319,12 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
int32_t exp2 = 0;
while (h->decimal_point > 1) {
uint32_t n = (uint32_t)(+h->decimal_point);
uint32_t shift =
(n < num_powers)
uint32_t shift = (n < num_powers)
? powers[n]
: WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
: WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
wuffs_base__private_implementation__high_prec_dec__small_rshift(h, shift);
if (h->decimal_point <
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
wuffs_private_impl__high_prec_dec__small_rshift(h, shift);
if (h->decimal_point < -WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
goto zero;
}
exp2 += (int32_t)shift;
@@ -1369,11 +1337,10 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
// The +1 is per "when targeting h->decimal_point being +0 or
// +1... when left shifting" in the powers comment above.
? (powers[n] + 1)
: WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
: WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
wuffs_base__private_implementation__high_prec_dec__small_lshift(h, shift);
if (h->decimal_point >
+WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
wuffs_private_impl__high_prec_dec__small_lshift(h, shift);
if (h->decimal_point > +WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
goto infinity;
}
exp2 -= (int32_t)shift;
@@ -1414,10 +1381,10 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
// The minimum normal exponent is (f64_bias + 1).
while ((f64_bias + 1) > exp2) {
uint32_t n = (uint32_t)((f64_bias + 1) - exp2);
if (n > WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {
n = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
if (n > WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL) {
n = WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
}
wuffs_base__private_implementation__high_prec_dec__small_rshift(h, n);
wuffs_private_impl__high_prec_dec__small_rshift(h, n);
exp2 += (int32_t)n;
}
@@ -1427,10 +1394,8 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
}
// Extract 53 bits for the mantissa (in base-2).
wuffs_base__private_implementation__high_prec_dec__small_lshift(
h, final_lshift);
uint64_t man2 =
wuffs_base__private_implementation__high_prec_dec__rounded_integer(h);
wuffs_private_impl__high_prec_dec__small_lshift(h, final_lshift);
uint64_t man2 = wuffs_private_impl__high_prec_dec__rounded_integer(h);
// Rounding might have added one bit. If so, shift and re-check overflow.
if ((man2 >> 53) != 0) {
@@ -1488,7 +1453,7 @@ infinity:
}
static inline bool //
wuffs_base__private_implementation__is_decimal_digit(uint8_t c) {
wuffs_private_impl__is_decimal_digit(uint8_t c) {
return ('0' <= c) && (c <= '9');
}
@@ -1549,13 +1514,13 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
if (*p == '0') {
man = 0;
p++;
if (wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (wuffs_private_impl__is_decimal_digit(*p)) {
goto fallback;
}
} else if (wuffs_base__private_implementation__is_decimal_digit(*p)) {
} else if (wuffs_private_impl__is_decimal_digit(*p)) {
man = ((uint8_t)(*p - '0'));
p++;
for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {
for (; wuffs_private_impl__is_decimal_digit(*p); p++) {
man = (10 * man) + ((uint8_t)(*p - '0'));
}
} else {
@@ -1571,12 +1536,12 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
: '.')) {
p++;
const uint8_t* first_after_separator_ptr = p;
if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (!wuffs_private_impl__is_decimal_digit(*p)) {
goto fallback;
}
man = (10 * man) + ((uint8_t)(*p - '0'));
p++;
for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {
for (; wuffs_private_impl__is_decimal_digit(*p); p++) {
man = (10 * man) + ((uint8_t)(*p - '0'));
}
exp10 = ((int32_t)(first_after_separator_ptr - p));
@@ -1601,7 +1566,7 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
} else if (*p == '+') {
p++;
}
if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (!wuffs_private_impl__is_decimal_digit(*p)) {
goto fallback;
}
int32_t exp_num = ((uint8_t)(*p - '0'));
@@ -1609,15 +1574,15 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
// The rest of the exp_num walking has a peculiar control flow but, once
// again, the "script/process-json-numbers.c with -p" benchmark is
// sensitive to alternative formulations.
if (wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (wuffs_private_impl__is_decimal_digit(*p)) {
exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));
p++;
}
if (wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (wuffs_private_impl__is_decimal_digit(*p)) {
exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));
p++;
}
while (wuffs_base__private_implementation__is_decimal_digit(*p)) {
while (wuffs_private_impl__is_decimal_digit(*p)) {
if (exp_num > 0x1000000) {
goto fallback;
}
@@ -1661,8 +1626,8 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
}
}
// The wuffs_base__private_implementation__parse_number_f64_eisel_lemire
// preconditions include that exp10 is in the range [-307 ..= 288].
// The wuffs_private_impl__parse_number_f64_eisel_lemire preconditions
// include that exp10 is in the range [-307 ..= 288].
if ((exp10 < -307) || (288 < exp10)) {
goto fallback;
}
@@ -1672,9 +1637,9 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
if ((-22 <= exp10) && (exp10 <= 22) && ((man >> 53) == 0)) {
double d = (double)man;
if (exp10 >= 0) {
d *= wuffs_base__private_implementation__f64_powers_of_10[+exp10];
d *= wuffs_private_impl__f64_powers_of_10[+exp10];
} else {
d /= wuffs_base__private_implementation__f64_powers_of_10[-exp10];
d /= wuffs_private_impl__f64_powers_of_10[-exp10];
}
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
@@ -1682,17 +1647,15 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
return ret;
}
// The wuffs_base__private_implementation__parse_number_f64_eisel_lemire
// preconditions include that man is non-zero. Parsing "0" should be caught
// by the "If both man and (10 ** exp10)" above, but "0e99" might not.
// The wuffs_private_impl__parse_number_f64_eisel_lemire preconditions
// include that man is non-zero. Parsing "0" should be caught by the "If
// both man and (10 ** exp10)" above, but "0e99" might not.
if (man == 0) {
goto fallback;
}
// Our man and exp10 are in range. Run the Eisel-Lemire algorithm.
int64_t r =
wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
man, exp10);
int64_t r = wuffs_private_impl__parse_number_f64_eisel_lemire(man, exp10);
if (r < 0) {
goto fallback;
}
@@ -1705,23 +1668,20 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
fallback:
do {
wuffs_base__private_implementation__high_prec_dec h;
wuffs_private_impl__high_prec_dec h;
wuffs_base__status status =
wuffs_base__private_implementation__high_prec_dec__parse(&h, s,
options);
wuffs_private_impl__high_prec_dec__parse(&h, s, options);
if (status.repr) {
return wuffs_base__private_implementation__parse_number_f64_special(
s, options);
return wuffs_private_impl__parse_number_f64_special(s, options);
}
return wuffs_base__private_implementation__high_prec_dec__to_f64(&h,
options);
return wuffs_private_impl__high_prec_dec__to_f64(&h, options);
} while (0);
}
// --------
static inline size_t //
wuffs_base__private_implementation__render_inf(wuffs_base__slice_u8 dst,
wuffs_private_impl__render_inf(wuffs_base__slice_u8 dst,
bool neg,
uint32_t options) {
if (neg) {
@@ -1748,7 +1708,7 @@ wuffs_base__private_implementation__render_inf(wuffs_base__slice_u8 dst,
}
static inline size_t //
wuffs_base__private_implementation__render_nan(wuffs_base__slice_u8 dst) {
wuffs_private_impl__render_nan(wuffs_base__slice_u8 dst) {
if (dst.len < 3) {
return 0;
}
@@ -1757,9 +1717,9 @@ wuffs_base__private_implementation__render_nan(wuffs_base__slice_u8 dst) {
}
static size_t //
wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(
wuffs_private_impl__high_prec_dec__render_exponent_absent(
wuffs_base__slice_u8 dst,
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec* h,
uint32_t precision,
uint32_t options) {
size_t n = (h->negative ||
@@ -1824,9 +1784,9 @@ wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(
}
static size_t //
wuffs_base__private_implementation__high_prec_dec__render_exponent_present(
wuffs_private_impl__high_prec_dec__render_exponent_present(
wuffs_base__slice_u8 dst,
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec* h,
uint32_t precision,
uint32_t options) {
int32_t exp = 0;
@@ -1923,9 +1883,9 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
// unless x is subnormal. Also take care of Inf and NaN.
if (exp2 == 0x7FF) {
if (man != 0) {
return wuffs_base__private_implementation__render_nan(dst);
return wuffs_private_impl__render_nan(dst);
}
return wuffs_base__private_implementation__render_inf(dst, neg, options);
return wuffs_private_impl__render_inf(dst, neg, options);
} else if (exp2 == 0) {
exp2 = -1022;
} else {
@@ -1939,11 +1899,11 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
}
// Convert from the (neg, exp2, man) tuple to an HPD.
wuffs_base__private_implementation__high_prec_dec h;
wuffs_base__private_implementation__high_prec_dec__assign(&h, man, neg);
wuffs_private_impl__high_prec_dec h;
wuffs_private_impl__high_prec_dec__assign(&h, man, neg);
if (h.num_digits > 0) {
wuffs_base__private_implementation__high_prec_dec__lshift(
&h, exp2 - 52); // 52 mantissa bits.
wuffs_private_impl__high_prec_dec__lshift(&h,
exp2 - 52); // 52 mantissa bits.
}
// Handle the "%e" and "%f" formats.
@@ -1951,27 +1911,25 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT)) {
case WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT: // The "%"f" format.
if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {
wuffs_base__private_implementation__high_prec_dec__round_just_enough(
&h, exp2, man);
wuffs_private_impl__high_prec_dec__round_just_enough(&h, exp2, man);
int32_t p = ((int32_t)(h.num_digits)) - h.decimal_point;
precision = ((uint32_t)(wuffs_base__i32__max(0, p)));
} else {
wuffs_base__private_implementation__high_prec_dec__round_nearest(
wuffs_private_impl__high_prec_dec__round_nearest(
&h, ((int32_t)precision) + h.decimal_point);
}
return wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(
return wuffs_private_impl__high_prec_dec__render_exponent_absent(
dst, &h, precision, options);
case WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT: // The "%e" format.
if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {
wuffs_base__private_implementation__high_prec_dec__round_just_enough(
&h, exp2, man);
wuffs_private_impl__high_prec_dec__round_just_enough(&h, exp2, man);
precision = (h.num_digits > 0) ? (h.num_digits - 1) : 0;
} else {
wuffs_base__private_implementation__high_prec_dec__round_nearest(
wuffs_private_impl__high_prec_dec__round_nearest(
&h, ((int32_t)precision) + 1);
}
return wuffs_base__private_implementation__high_prec_dec__render_exponent_present(
return wuffs_private_impl__high_prec_dec__render_exponent_present(
dst, &h, precision, options);
}
@@ -1980,16 +1938,14 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
// rounding and determine whether to use "%e" or "%f".
int32_t e_threshold = 0;
if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {
wuffs_base__private_implementation__high_prec_dec__round_just_enough(
&h, exp2, man);
wuffs_private_impl__high_prec_dec__round_just_enough(&h, exp2, man);
precision = h.num_digits;
e_threshold = 6;
} else {
if (precision == 0) {
precision = 1;
}
wuffs_base__private_implementation__high_prec_dec__round_nearest(
&h, ((int32_t)precision));
wuffs_private_impl__high_prec_dec__round_nearest(&h, ((int32_t)precision));
e_threshold = ((int32_t)precision);
int32_t nd = ((int32_t)(h.num_digits));
if ((e_threshold > nd) && (nd >= h.decimal_point)) {
@@ -2001,7 +1957,7 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
int32_t e = h.decimal_point - 1;
if ((e < -4) || (e_threshold <= e)) {
uint32_t p = wuffs_base__u32__min(precision, h.num_digits);
return wuffs_base__private_implementation__high_prec_dec__render_exponent_present(
return wuffs_private_impl__high_prec_dec__render_exponent_present(
dst, &h, (p > 0) ? (p - 1) : 0, options);
}
@@ -2011,6 +1967,6 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
p = ((int32_t)(h.num_digits));
}
precision = ((uint32_t)(wuffs_base__i32__max(0, p - h.decimal_point)));
return wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(
return wuffs_private_impl__high_prec_dec__render_exponent_absent(
dst, &h, precision, options);
}
+19 -20
View File
@@ -15,14 +15,14 @@
// whose output is not copied here, which prints further detail.
//
// These tables are used in
// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits.
// wuffs_private_impl__high_prec_dec__lshift_num_new_digits.
// wuffs_base__private_implementation__hpd_left_shift[i] encodes the number of
// new digits created after multiplying a positive integer by (1 << i): the
// additional length in the decimal representation. For example, shifting "234"
// by 3 (equivalent to multiplying by 8) will produce "1872". Going from a
// 3-length string to a 4-length string means that 1 new digit was added (and
// existing digits may have changed).
// wuffs_private_impl__hpd_left_shift[i] encodes the number of new digits
// created after multiplying a positive integer by (1 << i): the additional
// length in the decimal representation. For example, shifting "234" by 3
// (equivalent to multiplying by 8) will produce "1872". Going from a 3-length
// string to a 4-length string means that 1 new digit was added (and existing
// digits may have changed).
//
// Shifting by i can add either N or N-1 new digits, depending on whether the
// original positive integer compares >= or < to the i'th power of 5 (as 10
@@ -52,7 +52,7 @@
// is the string "\x06\x02\x05", so the relevant power of 5 is "625".
//
// Thanks to Ken Thompson for the original idea.
static const uint16_t wuffs_base__private_implementation__hpd_left_shift[65] = {
static const uint16_t wuffs_private_impl__hpd_left_shift[65] = {
0x0000, 0x0800, 0x0801, 0x0803, 0x1006, 0x1009, 0x100D, 0x1812, 0x1817,
0x181D, 0x2024, 0x202B, 0x2033, 0x203C, 0x2846, 0x2850, 0x285B, 0x3067,
0x3073, 0x3080, 0x388E, 0x389C, 0x38AB, 0x38BB, 0x40CC, 0x40DD, 0x40EF,
@@ -63,9 +63,9 @@ static const uint16_t wuffs_base__private_implementation__hpd_left_shift[65] = {
0x051C, 0x051C,
};
// wuffs_base__private_implementation__powers_of_5 contains the powers of 5,
// concatenated together: "5", "25", "125", "625", "3125", etc.
static const uint8_t wuffs_base__private_implementation__powers_of_5[0x051C] = {
// wuffs_private_impl__powers_of_5 contains the powers of 5, concatenated
// together: "5", "25", "125", "625", "3125", etc.
static const uint8_t wuffs_private_impl__powers_of_5[0x051C] = {
5, 2, 5, 1, 2, 5, 6, 2, 5, 3, 1, 2, 5, 1, 5, 6, 2, 5, 7, 8, 1, 2, 5, 3, 9,
0, 6, 2, 5, 1, 9, 5, 3, 1, 2, 5, 9, 7, 6, 5, 6, 2, 5, 4, 8, 8, 2, 8, 1, 2,
5, 2, 4, 4, 1, 4, 0, 6, 2, 5, 1, 2, 2, 0, 7, 0, 3, 1, 2, 5, 6, 1, 0, 3, 5,
@@ -123,15 +123,15 @@ static const uint8_t wuffs_base__private_implementation__powers_of_5[0x051C] = {
// --------
// wuffs_base__private_implementation__powers_of_10 contains truncated
// approximations to the powers of 10, ranging from 1e-307 to 1e+288 inclusive,
// as 596 pairs of uint64_t values (a 128-bit mantissa).
// wuffs_private_impl__powers_of_10 contains truncated approximations to the
// powers of 10, ranging from 1e-307 to 1e+288 inclusive, as 596 pairs of
// uint64_t values (a 128-bit mantissa).
//
// There's also an implicit third column (implied by a linear formula involving
// the base-10 exponent) that is the base-2 exponent, biased by a magic
// constant. That constant (1214 or 0x04BE) equals 1023 + 191. 1023 is the bias
// for IEEE 754 double-precision floating point. 191 is ((3 * 64) - 1) and
// wuffs_base__private_implementation__parse_number_f64_eisel_lemire works with
// wuffs_private_impl__parse_number_f64_eisel_lemire works with
// multiples-of-64-bit mantissas.
//
// For example, the third row holds the approximation to 1e-305:
@@ -144,8 +144,7 @@ static const uint8_t wuffs_base__private_implementation__powers_of_5[0x051C] = {
// 0xED63A231_D4C4FB27_4CA7AAA8_63EE4BDD * (2 ** (0x0520 - 0x04BE))
//
// This table was generated by by script/print-mpb-powers-of-10.go
static const uint64_t wuffs_base__private_implementation__powers_of_10[596][2] =
{
static const uint64_t wuffs_private_impl__powers_of_10[596][2] = {
{0xA5D3B6D479F8E056, 0x8FD0C16206306BAB}, // 1e-307
{0x8F48A4899877186C, 0xB3C4F1BA87BC8696}, // 1e-306
{0x331ACDABFE94DE87, 0xE0B62E2929ABA83C}, // 1e-305
@@ -744,9 +743,9 @@ static const uint64_t wuffs_base__private_implementation__powers_of_10[596][2] =
{0x73832EEC6FFF3111, 0xD226FC195C6A2F8C}, // 1e288
};
// wuffs_base__private_implementation__f64_powers_of_10 holds powers of 10 that
// can be exactly represented by a float64 (what C calls a double).
static const double wuffs_base__private_implementation__f64_powers_of_10[23] = {
// wuffs_private_impl__f64_powers_of_10 holds powers of 10 that can be exactly
// represented by a float64 (what C calls a double).
static const double wuffs_private_impl__f64_powers_of_10[23] = {
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22,
};
+8 -10
View File
@@ -97,7 +97,7 @@ static const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = {
// 8 9 A B C D E F
};
static const uint8_t wuffs_base__private_implementation__encode_base16[16] = {
static const uint8_t wuffs_private_impl__encode_base16[16] = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // 0x00 ..= 0x07.
0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, // 0x08 ..= 0x0F.
};
@@ -350,7 +350,7 @@ static const uint8_t wuffs_base__render_number__first_hundred[200] = {
};
static size_t //
wuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst,
wuffs_private_impl__render_number_u64(wuffs_base__slice_u8 dst,
uint64_t x,
uint32_t options,
bool neg) {
@@ -407,16 +407,14 @@ wuffs_base__render_number_i64(wuffs_base__slice_u8 dst,
if (neg) {
u = 1 + ~u;
}
return wuffs_base__private_implementation__render_number_u64(dst, u, options,
neg);
return wuffs_private_impl__render_number_u64(dst, u, options, neg);
}
WUFFS_BASE__MAYBE_STATIC size_t //
wuffs_base__render_number_u64(wuffs_base__slice_u8 dst,
uint64_t x,
uint32_t options) {
return wuffs_base__private_implementation__render_number_u64(dst, x, options,
false);
return wuffs_private_impl__render_number_u64(dst, x, options, false);
}
// ---------------- Base-16
@@ -523,8 +521,8 @@ wuffs_base__base_16__encode2(wuffs_base__slice_u8 dst,
while (n--) {
uint8_t c = *s;
d[0] = wuffs_base__private_implementation__encode_base16[c >> 4];
d[1] = wuffs_base__private_implementation__encode_base16[c & 0x0F];
d[0] = wuffs_private_impl__encode_base16[c >> 4];
d[1] = wuffs_private_impl__encode_base16[c & 0x0F];
d += 2;
s += 1;
}
@@ -562,8 +560,8 @@ wuffs_base__base_16__encode4(wuffs_base__slice_u8 dst,
uint8_t c = *s;
d[0] = '\\';
d[1] = 'x';
d[2] = wuffs_base__private_implementation__encode_base16[c >> 4];
d[3] = wuffs_base__private_implementation__encode_base16[c & 0x0F];
d[2] = wuffs_private_impl__encode_base16[c >> 4];
d[3] = wuffs_private_impl__encode_base16[c & 0x0F];
d += 4;
s += 1;
}
+206 -253
View File
@@ -16279,14 +16279,14 @@ wuffs_base__token_decoder__workbuf_len(
// whose output is not copied here, which prints further detail.
//
// These tables are used in
// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits.
// wuffs_private_impl__high_prec_dec__lshift_num_new_digits.
// wuffs_base__private_implementation__hpd_left_shift[i] encodes the number of
// new digits created after multiplying a positive integer by (1 << i): the
// additional length in the decimal representation. For example, shifting "234"
// by 3 (equivalent to multiplying by 8) will produce "1872". Going from a
// 3-length string to a 4-length string means that 1 new digit was added (and
// existing digits may have changed).
// wuffs_private_impl__hpd_left_shift[i] encodes the number of new digits
// created after multiplying a positive integer by (1 << i): the additional
// length in the decimal representation. For example, shifting "234" by 3
// (equivalent to multiplying by 8) will produce "1872". Going from a 3-length
// string to a 4-length string means that 1 new digit was added (and existing
// digits may have changed).
//
// Shifting by i can add either N or N-1 new digits, depending on whether the
// original positive integer compares >= or < to the i'th power of 5 (as 10
@@ -16316,7 +16316,7 @@ wuffs_base__token_decoder__workbuf_len(
// is the string "\x06\x02\x05", so the relevant power of 5 is "625".
//
// Thanks to Ken Thompson for the original idea.
static const uint16_t wuffs_base__private_implementation__hpd_left_shift[65] = {
static const uint16_t wuffs_private_impl__hpd_left_shift[65] = {
0x0000, 0x0800, 0x0801, 0x0803, 0x1006, 0x1009, 0x100D, 0x1812, 0x1817,
0x181D, 0x2024, 0x202B, 0x2033, 0x203C, 0x2846, 0x2850, 0x285B, 0x3067,
0x3073, 0x3080, 0x388E, 0x389C, 0x38AB, 0x38BB, 0x40CC, 0x40DD, 0x40EF,
@@ -16327,9 +16327,9 @@ static const uint16_t wuffs_base__private_implementation__hpd_left_shift[65] = {
0x051C, 0x051C,
};
// wuffs_base__private_implementation__powers_of_5 contains the powers of 5,
// concatenated together: "5", "25", "125", "625", "3125", etc.
static const uint8_t wuffs_base__private_implementation__powers_of_5[0x051C] = {
// wuffs_private_impl__powers_of_5 contains the powers of 5, concatenated
// together: "5", "25", "125", "625", "3125", etc.
static const uint8_t wuffs_private_impl__powers_of_5[0x051C] = {
5, 2, 5, 1, 2, 5, 6, 2, 5, 3, 1, 2, 5, 1, 5, 6, 2, 5, 7, 8, 1, 2, 5, 3, 9,
0, 6, 2, 5, 1, 9, 5, 3, 1, 2, 5, 9, 7, 6, 5, 6, 2, 5, 4, 8, 8, 2, 8, 1, 2,
5, 2, 4, 4, 1, 4, 0, 6, 2, 5, 1, 2, 2, 0, 7, 0, 3, 1, 2, 5, 6, 1, 0, 3, 5,
@@ -16387,15 +16387,15 @@ static const uint8_t wuffs_base__private_implementation__powers_of_5[0x051C] = {
// --------
// wuffs_base__private_implementation__powers_of_10 contains truncated
// approximations to the powers of 10, ranging from 1e-307 to 1e+288 inclusive,
// as 596 pairs of uint64_t values (a 128-bit mantissa).
// wuffs_private_impl__powers_of_10 contains truncated approximations to the
// powers of 10, ranging from 1e-307 to 1e+288 inclusive, as 596 pairs of
// uint64_t values (a 128-bit mantissa).
//
// There's also an implicit third column (implied by a linear formula involving
// the base-10 exponent) that is the base-2 exponent, biased by a magic
// constant. That constant (1214 or 0x04BE) equals 1023 + 191. 1023 is the bias
// for IEEE 754 double-precision floating point. 191 is ((3 * 64) - 1) and
// wuffs_base__private_implementation__parse_number_f64_eisel_lemire works with
// wuffs_private_impl__parse_number_f64_eisel_lemire works with
// multiples-of-64-bit mantissas.
//
// For example, the third row holds the approximation to 1e-305:
@@ -16408,8 +16408,7 @@ static const uint8_t wuffs_base__private_implementation__powers_of_5[0x051C] = {
// 0xED63A231_D4C4FB27_4CA7AAA8_63EE4BDD * (2 ** (0x0520 - 0x04BE))
//
// This table was generated by by script/print-mpb-powers-of-10.go
static const uint64_t wuffs_base__private_implementation__powers_of_10[596][2] =
{
static const uint64_t wuffs_private_impl__powers_of_10[596][2] = {
{0xA5D3B6D479F8E056, 0x8FD0C16206306BAB}, // 1e-307
{0x8F48A4899877186C, 0xB3C4F1BA87BC8696}, // 1e-306
{0x331ACDABFE94DE87, 0xE0B62E2929ABA83C}, // 1e-305
@@ -17008,9 +17007,9 @@ static const uint64_t wuffs_base__private_implementation__powers_of_10[596][2] =
{0x73832EEC6FFF3111, 0xD226FC195C6A2F8C}, // 1e288
};
// wuffs_base__private_implementation__f64_powers_of_10 holds powers of 10 that
// can be exactly represented by a float64 (what C calls a double).
static const double wuffs_base__private_implementation__f64_powers_of_10[23] = {
// wuffs_private_impl__f64_powers_of_10 holds powers of 10 that can be exactly
// represented by a float64 (what C calls a double).
static const double wuffs_private_impl__f64_powers_of_10[23] = {
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22,
};
@@ -17147,27 +17146,27 @@ wuffs_base__ieee_754_bit_representation__from_f64_to_u32_truncate(double f) {
// --------
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE 2047
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION 800
#define WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE 2047
#define WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION 800
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL is the largest N
// such that ((10 << N) < (1 << 64)).
#define WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL 60
// WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL is the largest N such that
// ((10 << N) < (1 << 64)).
#define WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL 60
// wuffs_base__private_implementation__high_prec_dec (abbreviated as HPD) is a
// fixed precision floating point decimal number, augmented with ±infinity
// values, but it cannot represent NaN (Not a Number).
// wuffs_private_impl__high_prec_dec (abbreviated as HPD) is a fixed precision
// floating point decimal number, augmented with ±infinity values, but it
// cannot represent NaN (Not a Number).
//
// "High precision" means that the mantissa holds 800 decimal digits. 800 is
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION.
// WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION.
//
// An HPD isn't for general purpose arithmetic, only for conversions to and
// from IEEE 754 double-precision floating point, where the largest and
// smallest positive, finite values are approximately 1.8e+308 and 4.9e-324.
// HPD exponents above +2047 mean infinity, below -2047 mean zero. The ±2047
// bounds are further away from zero than ±(324 + 800), where 800 and 2047 is
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION and
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.
// WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION and
// WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE.
//
// digits[.. num_digits] are the number's digits in big-endian order. The
// uint8_t values are in the range [0 ..= 9], not ['0' ..= '9'], where e.g. '7'
@@ -17193,41 +17192,38 @@ wuffs_base__ieee_754_bit_representation__from_f64_to_u32_truncate(double f) {
// negative is a sign bit. An HPD can distinguish positive and negative zero.
//
// truncated is whether there are more than
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION digits, and at
// least one of those extra digits are non-zero. The existence of long-tail
// digits can affect rounding.
// WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION digits, and at least one of those
// extra digits are non-zero. The existence of long-tail digits can affect
// rounding.
//
// The "all fields are zero" value is valid, and represents the number +0.
typedef struct wuffs_base__private_implementation__high_prec_dec__struct {
typedef struct wuffs_private_impl__high_prec_dec__struct {
uint32_t num_digits;
int32_t decimal_point;
bool negative;
bool truncated;
uint8_t digits[WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION];
} wuffs_base__private_implementation__high_prec_dec;
uint8_t digits[WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION];
} wuffs_private_impl__high_prec_dec;
// wuffs_base__private_implementation__high_prec_dec__trim trims trailing
// zeroes from the h->digits[.. h->num_digits] slice. They have no benefit,
// since we explicitly track h->decimal_point.
// wuffs_private_impl__high_prec_dec__trim trims trailing zeroes from the
// h->digits[.. h->num_digits] slice. They have no benefit, since we explicitly
// track h->decimal_point.
//
// Preconditions:
// - h is non-NULL.
static inline void //
wuffs_base__private_implementation__high_prec_dec__trim(
wuffs_base__private_implementation__high_prec_dec* h) {
wuffs_private_impl__high_prec_dec__trim(wuffs_private_impl__high_prec_dec* h) {
while ((h->num_digits > 0) && (h->digits[h->num_digits - 1] == 0)) {
h->num_digits--;
}
}
// wuffs_base__private_implementation__high_prec_dec__assign sets h to
// represent the number x.
// wuffs_private_impl__high_prec_dec__assign sets h to represent the number x.
//
// Preconditions:
// - h is non-NULL.
static void //
wuffs_base__private_implementation__high_prec_dec__assign(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__assign(wuffs_private_impl__high_prec_dec* h,
uint64_t x,
bool negative) {
uint32_t n = 0;
@@ -17259,12 +17255,11 @@ wuffs_base__private_implementation__high_prec_dec__assign(
h->decimal_point = (int32_t)n;
h->negative = negative;
h->truncated = false;
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
}
static wuffs_base__status //
wuffs_base__private_implementation__high_prec_dec__parse(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__parse(wuffs_private_impl__high_prec_dec* h,
wuffs_base__slice_u8 s,
uint32_t options) {
if (!h) {
@@ -17355,7 +17350,7 @@ wuffs_base__private_implementation__high_prec_dec__parse(
if (p >= q) {
goto after_all;
} else if (('0' <= *p) && (*p <= '9')) {
if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (nd < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[nd++] = (uint8_t)(*p - '0');
dp = (int32_t)nd;
} else if ('0' != *p) {
@@ -17397,12 +17392,11 @@ after_sep:
if (nd == 0) {
// Track leading zeroes implicitly.
dp--;
} else if (nd <
WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
} else if (nd < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[nd++] = (uint8_t)(*p - '0');
}
} else if (('0' < *p) && (*p <= '9')) {
if (nd < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (nd < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[nd++] = (uint8_t)(*p - '0');
} else {
// Long-tail non-zeroes set the truncated bit.
@@ -17438,9 +17432,8 @@ after_exp:
}
int32_t exp = 0;
const int32_t exp_large =
WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE +
WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;
const int32_t exp_large = WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE +
WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION;
bool saw_exp_digits = false;
for (; p < q; p++) {
if ((*p == '_') &&
@@ -17471,44 +17464,39 @@ after_all:
return wuffs_base__make_status(wuffs_base__error__bad_argument);
}
h->decimal_point = 0;
} else if (dp <
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
h->decimal_point =
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE - 1;
} else if (dp >
+WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
h->decimal_point =
+WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE + 1;
} else if (dp < -WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
h->decimal_point = -WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE - 1;
} else if (dp > +WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
h->decimal_point = +WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE + 1;
} else {
h->decimal_point = dp;
}
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
return wuffs_base__make_status(NULL);
}
// --------
// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits
// returns the number of additional decimal digits when left-shifting by shift.
// wuffs_private_impl__high_prec_dec__lshift_num_new_digits returns the number
// of additional decimal digits when left-shifting by shift.
//
// See below for preconditions.
static uint32_t //
wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__lshift_num_new_digits(
wuffs_private_impl__high_prec_dec* h,
uint32_t shift) {
// Masking with 0x3F should be unnecessary (assuming the preconditions) but
// it's cheap and ensures that we don't overflow the
// wuffs_base__private_implementation__hpd_left_shift array.
// wuffs_private_impl__hpd_left_shift array.
shift &= 63;
uint32_t x_a = wuffs_base__private_implementation__hpd_left_shift[shift];
uint32_t x_b = wuffs_base__private_implementation__hpd_left_shift[shift + 1];
uint32_t x_a = wuffs_private_impl__hpd_left_shift[shift];
uint32_t x_b = wuffs_private_impl__hpd_left_shift[shift + 1];
uint32_t num_new_digits = x_a >> 11;
uint32_t pow5_a = 0x7FF & x_a;
uint32_t pow5_b = 0x7FF & x_b;
const uint8_t* pow5 =
&wuffs_base__private_implementation__powers_of_5[pow5_a];
const uint8_t* pow5 = &wuffs_private_impl__powers_of_5[pow5_a];
uint32_t i = 0;
uint32_t n = pow5_b - pow5_a;
for (; i < n; i++) {
@@ -17527,9 +17515,9 @@ wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(
// --------
// wuffs_base__private_implementation__high_prec_dec__rounded_integer returns
// the integral (non-fractional) part of h, provided that it is 18 or fewer
// decimal digits. For 19 or more digits, it returns UINT64_MAX. Note that:
// wuffs_private_impl__high_prec_dec__rounded_integer returns the integral
// (non-fractional) part of h, provided that it is 18 or fewer decimal digits.
// For 19 or more digits, it returns UINT64_MAX. Note that:
// - (1 << 53) is 9007199254740992, which has 16 decimal digits.
// - (1 << 56) is 72057594037927936, which has 17 decimal digits.
// - (1 << 59) is 576460752303423488, which has 18 decimal digits.
@@ -17542,8 +17530,8 @@ wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(
//
// See below for preconditions.
static uint64_t //
wuffs_base__private_implementation__high_prec_dec__rounded_integer(
wuffs_base__private_implementation__high_prec_dec* h) {
wuffs_private_impl__high_prec_dec__rounded_integer(
wuffs_private_impl__high_prec_dec* h) {
if ((h->num_digits == 0) || (h->decimal_point < 0)) {
return 0;
} else if (h->decimal_point > 18) {
@@ -17574,8 +17562,8 @@ wuffs_base__private_implementation__high_prec_dec__rounded_integer(
return n;
}
// wuffs_base__private_implementation__high_prec_dec__small_xshift shifts h's
// number (where 'x' is 'l' or 'r' for left or right) by a small shift value.
// wuffs_private_impl__high_prec_dec__small_xshift shifts h's number (where 'x'
// is 'l' or 'r' for left or right) by a small shift value.
//
// Preconditions:
// - h is non-NULL.
@@ -17583,31 +17571,29 @@ wuffs_base__private_implementation__high_prec_dec__rounded_integer(
// - shift is non-zero.
// - shift is "a small shift".
//
// "Not extreme" means within
// ±WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.
// "Not extreme" means within ±WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE.
//
// "A small shift" means not more than
// WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.
// WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL.
//
// wuffs_base__private_implementation__high_prec_dec__rounded_integer and
// wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits
// have the same preconditions.
// wuffs_private_impl__high_prec_dec__rounded_integer and
// wuffs_private_impl__high_prec_dec__lshift_num_new_digits have the same
// preconditions.
//
// wuffs_base__private_implementation__high_prec_dec__lshift keeps the first
// two preconditions but not the last two. Its shift argument is signed and
// does not need to be "small": zero is a no-op, positive means left shift and
// negative means right shift.
// wuffs_private_impl__high_prec_dec__lshift keeps the first two preconditions
// but not the last two. Its shift argument is signed and does not need to be
// "small": zero is a no-op, positive means left shift and negative means right
// shift.
static void //
wuffs_base__private_implementation__high_prec_dec__small_lshift(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__small_lshift(
wuffs_private_impl__high_prec_dec* h,
uint32_t shift) {
if (h->num_digits == 0) {
return;
}
uint32_t num_new_digits =
wuffs_base__private_implementation__high_prec_dec__lshift_num_new_digits(
h, shift);
wuffs_private_impl__high_prec_dec__lshift_num_new_digits(h, shift);
uint32_t rx = h->num_digits - 1; // Read index.
uint32_t wx = h->num_digits - 1 + num_new_digits; // Write index.
uint64_t n = 0;
@@ -17617,7 +17603,7 @@ wuffs_base__private_implementation__high_prec_dec__small_lshift(
n += ((uint64_t)(h->digits[rx])) << shift;
uint64_t quo = n / 10;
uint64_t rem = n - (10 * quo);
if (wx < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (wx < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[wx] = (uint8_t)rem;
} else if (rem > 0) {
h->truncated = true;
@@ -17631,7 +17617,7 @@ wuffs_base__private_implementation__high_prec_dec__small_lshift(
while (n > 0) {
uint64_t quo = n / 10;
uint64_t rem = n - (10 * quo);
if (wx < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (wx < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[wx] = (uint8_t)rem;
} else if (rem > 0) {
h->truncated = true;
@@ -17642,17 +17628,16 @@ wuffs_base__private_implementation__high_prec_dec__small_lshift(
// Finish.
h->num_digits += num_new_digits;
if (h->num_digits >
WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
h->num_digits = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION;
if (h->num_digits > WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->num_digits = WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION;
}
h->decimal_point += (int32_t)num_new_digits;
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
}
static void //
wuffs_base__private_implementation__high_prec_dec__small_rshift(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__small_rshift(
wuffs_private_impl__high_prec_dec* h,
uint32_t shift) {
uint32_t rx = 0; // Read index.
uint32_t wx = 0; // Write index.
@@ -17676,8 +17661,7 @@ wuffs_base__private_implementation__high_prec_dec__small_rshift(
}
}
h->decimal_point -= ((int32_t)(rx - 1));
if (h->decimal_point <
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
if (h->decimal_point < -WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
// After the shift, h's number is effectively zero.
h->num_digits = 0;
h->decimal_point = 0;
@@ -17697,7 +17681,7 @@ wuffs_base__private_implementation__high_prec_dec__small_rshift(
while (n > 0) {
uint8_t new_digit = ((uint8_t)(n >> shift));
n = 10 * (n & mask);
if (wx < WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DIGITS_PRECISION) {
if (wx < WUFFS_PRIVATE_IMPL__HPD__DIGITS_PRECISION) {
h->digits[wx++] = new_digit;
} else if (new_digit > 0) {
h->truncated = true;
@@ -17706,62 +17690,58 @@ wuffs_base__private_implementation__high_prec_dec__small_rshift(
// Finish.
h->num_digits = wx;
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
}
static void //
wuffs_base__private_implementation__high_prec_dec__lshift(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__lshift(wuffs_private_impl__high_prec_dec* h,
int32_t shift) {
if (shift > 0) {
while (shift > +WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {
wuffs_base__private_implementation__high_prec_dec__small_lshift(
h, WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL);
shift -= WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
while (shift > +WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL) {
wuffs_private_impl__high_prec_dec__small_lshift(
h, WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL);
shift -= WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
}
wuffs_base__private_implementation__high_prec_dec__small_lshift(
h, ((uint32_t)(+shift)));
wuffs_private_impl__high_prec_dec__small_lshift(h, ((uint32_t)(+shift)));
} else if (shift < 0) {
while (shift < -WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {
wuffs_base__private_implementation__high_prec_dec__small_rshift(
h, WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL);
shift += WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
while (shift < -WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL) {
wuffs_private_impl__high_prec_dec__small_rshift(
h, WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL);
shift += WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
}
wuffs_base__private_implementation__high_prec_dec__small_rshift(
h, ((uint32_t)(-shift)));
wuffs_private_impl__high_prec_dec__small_rshift(h, ((uint32_t)(-shift)));
}
}
// --------
// wuffs_base__private_implementation__high_prec_dec__round_etc rounds h's
// number. For those functions that take an n argument, rounding produces at
// most n digits (which is not necessarily at most n decimal places). Negative
// n values are ignored, as well as any n greater than or equal to h's number
// of digits. The etc__round_just_enough function implicitly chooses an n to
// implement WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION.
// wuffs_private_impl__high_prec_dec__round_etc rounds h's number. For those
// functions that take an n argument, rounding produces at most n digits (which
// is not necessarily at most n decimal places). Negative n values are ignored,
// as well as any n greater than or equal to h's number of digits. The
// etc__round_just_enough function implicitly chooses an n to implement
// WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION.
//
// Preconditions:
// - h is non-NULL.
// - h->decimal_point is "not extreme".
//
// "Not extreme" means within
// ±WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE.
// "Not extreme" means within ±WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE.
static void //
wuffs_base__private_implementation__high_prec_dec__round_down(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__round_down(
wuffs_private_impl__high_prec_dec* h,
int32_t n) {
if ((n < 0) || (h->num_digits <= (uint32_t)n)) {
return;
}
h->num_digits = (uint32_t)(n);
wuffs_base__private_implementation__high_prec_dec__trim(h);
wuffs_private_impl__high_prec_dec__trim(h);
}
static void //
wuffs_base__private_implementation__high_prec_dec__round_up(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__round_up(
wuffs_private_impl__high_prec_dec* h,
int32_t n) {
if ((n < 0) || (h->num_digits <= (uint32_t)n)) {
return;
@@ -17782,8 +17762,8 @@ wuffs_base__private_implementation__high_prec_dec__round_up(
}
static void //
wuffs_base__private_implementation__high_prec_dec__round_nearest(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__round_nearest(
wuffs_private_impl__high_prec_dec* h,
int32_t n) {
if ((n < 0) || (h->num_digits <= (uint32_t)n)) {
return;
@@ -17795,15 +17775,15 @@ wuffs_base__private_implementation__high_prec_dec__round_nearest(
}
if (up) {
wuffs_base__private_implementation__high_prec_dec__round_up(h, n);
wuffs_private_impl__high_prec_dec__round_up(h, n);
} else {
wuffs_base__private_implementation__high_prec_dec__round_down(h, n);
wuffs_private_impl__high_prec_dec__round_down(h, n);
}
}
static void //
wuffs_base__private_implementation__high_prec_dec__round_just_enough(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__round_just_enough(
wuffs_private_impl__high_prec_dec* h,
int32_t exp2,
uint64_t mantissa) {
// The magic numbers 52 and 53 in this function are because IEEE 754 double
@@ -17841,11 +17821,10 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
l_exp2 = exp2 - 1;
l_mantissa = (2 * mantissa) - 1;
}
wuffs_base__private_implementation__high_prec_dec lower;
wuffs_base__private_implementation__high_prec_dec__assign(
&lower, (2 * l_mantissa) + 1, false);
wuffs_base__private_implementation__high_prec_dec__lshift(&lower,
l_exp2 - 53);
wuffs_private_impl__high_prec_dec lower;
wuffs_private_impl__high_prec_dec__assign(&lower, (2 * l_mantissa) + 1,
false);
wuffs_private_impl__high_prec_dec__lshift(&lower, l_exp2 - 53);
// Next, the upper bound. Our number f is:
// ((mantissa + 0) * (2 ** (exp2 - 52)))
@@ -17855,10 +17834,9 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
//
// The upper bound is halfway between them (noting that 52 became 53):
// (((2 * mantissa) + 1) * (2 ** (exp2 - 53)))
wuffs_base__private_implementation__high_prec_dec upper;
wuffs_base__private_implementation__high_prec_dec__assign(
&upper, (2 * mantissa) + 1, false);
wuffs_base__private_implementation__high_prec_dec__lshift(&upper, exp2 - 53);
wuffs_private_impl__high_prec_dec upper;
wuffs_private_impl__high_prec_dec__assign(&upper, (2 * mantissa) + 1, false);
wuffs_private_impl__high_prec_dec__lshift(&upper, exp2 - 53);
// The lower and upper bounds are possible outputs only if the original
// mantissa is even, so that IEEE round-to-even would round to the original
@@ -17941,17 +17919,15 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// way, do it. If we can't round, continue the loop.
if (can_round_down) {
if (can_round_up) {
wuffs_base__private_implementation__high_prec_dec__round_nearest(
h, hi + 1);
wuffs_private_impl__high_prec_dec__round_nearest(h, hi + 1);
return;
} else {
wuffs_base__private_implementation__high_prec_dec__round_down(h,
hi + 1);
wuffs_private_impl__high_prec_dec__round_down(h, hi + 1);
return;
}
} else {
if (can_round_up) {
wuffs_base__private_implementation__high_prec_dec__round_up(h, hi + 1);
wuffs_private_impl__high_prec_dec__round_up(h, hi + 1);
return;
}
}
@@ -17960,9 +17936,9 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// --------
// wuffs_base__private_implementation__parse_number_f64_eisel_lemire produces
// the IEEE 754 double-precision value for an exact mantissa and base-10
// exponent. For example:
// wuffs_private_impl__parse_number_f64_eisel_lemire produces the IEEE 754
// double-precision value for an exact mantissa and base-10 exponent. For
// example:
// - when parsing "12345.678e+02", man is 12345678 and exp10 is -1.
// - when parsing "-12", man is 12 and exp10 is 0. Processing the leading
// minus sign is the responsibility of the caller, not this function.
@@ -17981,7 +17957,7 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// Preconditions:
// - man is non-zero.
// - exp10 is in the range [-307 ..= 288], the same range of the
// wuffs_base__private_implementation__powers_of_10 array.
// wuffs_private_impl__powers_of_10 array.
//
// The exp10 range (and the fact that man is in the range [1 ..= UINT64_MAX],
// approximately [1 ..= 1.85e+19]) means that (man * (10 ** exp10)) is in the
@@ -17989,14 +17965,11 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// (neither subnormal nor non-finite) f64 values: DBL_MIN and DBL_MAX are
// approximately 2.23e–308 and 1.80e+308.
static int64_t //
wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
uint64_t man,
int32_t exp10) {
wuffs_private_impl__parse_number_f64_eisel_lemire(uint64_t man, int32_t exp10) {
// Look up the (possibly truncated) base-2 representation of (10 ** exp10).
// The look-up table was constructed so that it is already normalized: the
// table entry's mantissa's MSB (most significant bit) is on.
const uint64_t* po10 =
&wuffs_base__private_implementation__powers_of_10[exp10 + 307][0];
const uint64_t* po10 = &wuffs_private_impl__powers_of_10[exp10 + 307][0];
// Normalize the man argument. The (man != 0) precondition means that a
// non-zero bit exists.
@@ -18154,8 +18127,7 @@ wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
// --------
static wuffs_base__result_f64 //
wuffs_base__private_implementation__parse_number_f64_special(
wuffs_base__slice_u8 s,
wuffs_private_impl__parse_number_f64_special(wuffs_base__slice_u8 s,
uint32_t options) {
do {
if (options & WUFFS_BASE__PARSE_NUMBER_FXX__REJECT_INF_AND_NAN) {
@@ -18260,17 +18232,16 @@ fail:
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //
wuffs_base__private_implementation__high_prec_dec__to_f64(
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec__to_f64(wuffs_private_impl__high_prec_dec* h,
uint32_t options) {
do {
// powers converts decimal powers of 10 to binary powers of 2. For example,
// (10000 >> 13) is 1. It stops before the elements exceed 60, also known
// as WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.
// as WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL.
//
// This rounds down (1<<13 is a lower bound for 1e4). Adding 1 to the array
// element value rounds up (1<<14 is an upper bound for 1e4) while staying
// at or below WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL.
// at or below WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL.
//
// When starting in the range [1e+1 .. 1e+2] (i.e. h->decimal_point == +2),
// powers[2] == 6 and so:
@@ -18325,14 +18296,12 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
}
while (man != 0) { // The 'while' is just an 'if' that we can 'break'.
int64_t r0 =
wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
man + 0, exp10);
wuffs_private_impl__parse_number_f64_eisel_lemire(man + 0, exp10);
if (r0 < 0) {
break;
} else if (h->num_digits > 19) {
int64_t r1 =
wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
man + 1, exp10);
wuffs_private_impl__parse_number_f64_eisel_lemire(man + 1, exp10);
if (r1 != r0) {
break;
}
@@ -18356,14 +18325,12 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
int32_t exp2 = 0;
while (h->decimal_point > 1) {
uint32_t n = (uint32_t)(+h->decimal_point);
uint32_t shift =
(n < num_powers)
uint32_t shift = (n < num_powers)
? powers[n]
: WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
: WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
wuffs_base__private_implementation__high_prec_dec__small_rshift(h, shift);
if (h->decimal_point <
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
wuffs_private_impl__high_prec_dec__small_rshift(h, shift);
if (h->decimal_point < -WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
goto zero;
}
exp2 += (int32_t)shift;
@@ -18376,11 +18343,10 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
// The +1 is per "when targeting h->decimal_point being +0 or
// +1... when left shifting" in the powers comment above.
? (powers[n] + 1)
: WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
: WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
wuffs_base__private_implementation__high_prec_dec__small_lshift(h, shift);
if (h->decimal_point >
+WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
wuffs_private_impl__high_prec_dec__small_lshift(h, shift);
if (h->decimal_point > +WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
goto infinity;
}
exp2 -= (int32_t)shift;
@@ -18421,10 +18387,10 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
// The minimum normal exponent is (f64_bias + 1).
while ((f64_bias + 1) > exp2) {
uint32_t n = (uint32_t)((f64_bias + 1) - exp2);
if (n > WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {
n = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
if (n > WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL) {
n = WUFFS_PRIVATE_IMPL__HPD__SHIFT__MAX_INCL;
}
wuffs_base__private_implementation__high_prec_dec__small_rshift(h, n);
wuffs_private_impl__high_prec_dec__small_rshift(h, n);
exp2 += (int32_t)n;
}
@@ -18434,10 +18400,8 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
}
// Extract 53 bits for the mantissa (in base-2).
wuffs_base__private_implementation__high_prec_dec__small_lshift(
h, final_lshift);
uint64_t man2 =
wuffs_base__private_implementation__high_prec_dec__rounded_integer(h);
wuffs_private_impl__high_prec_dec__small_lshift(h, final_lshift);
uint64_t man2 = wuffs_private_impl__high_prec_dec__rounded_integer(h);
// Rounding might have added one bit. If so, shift and re-check overflow.
if ((man2 >> 53) != 0) {
@@ -18495,7 +18459,7 @@ infinity:
}
static inline bool //
wuffs_base__private_implementation__is_decimal_digit(uint8_t c) {
wuffs_private_impl__is_decimal_digit(uint8_t c) {
return ('0' <= c) && (c <= '9');
}
@@ -18556,13 +18520,13 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
if (*p == '0') {
man = 0;
p++;
if (wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (wuffs_private_impl__is_decimal_digit(*p)) {
goto fallback;
}
} else if (wuffs_base__private_implementation__is_decimal_digit(*p)) {
} else if (wuffs_private_impl__is_decimal_digit(*p)) {
man = ((uint8_t)(*p - '0'));
p++;
for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {
for (; wuffs_private_impl__is_decimal_digit(*p); p++) {
man = (10 * man) + ((uint8_t)(*p - '0'));
}
} else {
@@ -18578,12 +18542,12 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
: '.')) {
p++;
const uint8_t* first_after_separator_ptr = p;
if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (!wuffs_private_impl__is_decimal_digit(*p)) {
goto fallback;
}
man = (10 * man) + ((uint8_t)(*p - '0'));
p++;
for (; wuffs_base__private_implementation__is_decimal_digit(*p); p++) {
for (; wuffs_private_impl__is_decimal_digit(*p); p++) {
man = (10 * man) + ((uint8_t)(*p - '0'));
}
exp10 = ((int32_t)(first_after_separator_ptr - p));
@@ -18608,7 +18572,7 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
} else if (*p == '+') {
p++;
}
if (!wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (!wuffs_private_impl__is_decimal_digit(*p)) {
goto fallback;
}
int32_t exp_num = ((uint8_t)(*p - '0'));
@@ -18616,15 +18580,15 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
// The rest of the exp_num walking has a peculiar control flow but, once
// again, the "script/process-json-numbers.c with -p" benchmark is
// sensitive to alternative formulations.
if (wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (wuffs_private_impl__is_decimal_digit(*p)) {
exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));
p++;
}
if (wuffs_base__private_implementation__is_decimal_digit(*p)) {
if (wuffs_private_impl__is_decimal_digit(*p)) {
exp_num = (10 * exp_num) + ((uint8_t)(*p - '0'));
p++;
}
while (wuffs_base__private_implementation__is_decimal_digit(*p)) {
while (wuffs_private_impl__is_decimal_digit(*p)) {
if (exp_num > 0x1000000) {
goto fallback;
}
@@ -18668,8 +18632,8 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
}
}
// The wuffs_base__private_implementation__parse_number_f64_eisel_lemire
// preconditions include that exp10 is in the range [-307 ..= 288].
// The wuffs_private_impl__parse_number_f64_eisel_lemire preconditions
// include that exp10 is in the range [-307 ..= 288].
if ((exp10 < -307) || (288 < exp10)) {
goto fallback;
}
@@ -18679,9 +18643,9 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
if ((-22 <= exp10) && (exp10 <= 22) && ((man >> 53) == 0)) {
double d = (double)man;
if (exp10 >= 0) {
d *= wuffs_base__private_implementation__f64_powers_of_10[+exp10];
d *= wuffs_private_impl__f64_powers_of_10[+exp10];
} else {
d /= wuffs_base__private_implementation__f64_powers_of_10[-exp10];
d /= wuffs_private_impl__f64_powers_of_10[-exp10];
}
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
@@ -18689,17 +18653,15 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
return ret;
}
// The wuffs_base__private_implementation__parse_number_f64_eisel_lemire
// preconditions include that man is non-zero. Parsing "0" should be caught
// by the "If both man and (10 ** exp10)" above, but "0e99" might not.
// The wuffs_private_impl__parse_number_f64_eisel_lemire preconditions
// include that man is non-zero. Parsing "0" should be caught by the "If
// both man and (10 ** exp10)" above, but "0e99" might not.
if (man == 0) {
goto fallback;
}
// Our man and exp10 are in range. Run the Eisel-Lemire algorithm.
int64_t r =
wuffs_base__private_implementation__parse_number_f64_eisel_lemire(
man, exp10);
int64_t r = wuffs_private_impl__parse_number_f64_eisel_lemire(man, exp10);
if (r < 0) {
goto fallback;
}
@@ -18712,23 +18674,20 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s, uint32_t options) {
fallback:
do {
wuffs_base__private_implementation__high_prec_dec h;
wuffs_private_impl__high_prec_dec h;
wuffs_base__status status =
wuffs_base__private_implementation__high_prec_dec__parse(&h, s,
options);
wuffs_private_impl__high_prec_dec__parse(&h, s, options);
if (status.repr) {
return wuffs_base__private_implementation__parse_number_f64_special(
s, options);
return wuffs_private_impl__parse_number_f64_special(s, options);
}
return wuffs_base__private_implementation__high_prec_dec__to_f64(&h,
options);
return wuffs_private_impl__high_prec_dec__to_f64(&h, options);
} while (0);
}
// --------
static inline size_t //
wuffs_base__private_implementation__render_inf(wuffs_base__slice_u8 dst,
wuffs_private_impl__render_inf(wuffs_base__slice_u8 dst,
bool neg,
uint32_t options) {
if (neg) {
@@ -18755,7 +18714,7 @@ wuffs_base__private_implementation__render_inf(wuffs_base__slice_u8 dst,
}
static inline size_t //
wuffs_base__private_implementation__render_nan(wuffs_base__slice_u8 dst) {
wuffs_private_impl__render_nan(wuffs_base__slice_u8 dst) {
if (dst.len < 3) {
return 0;
}
@@ -18764,9 +18723,9 @@ wuffs_base__private_implementation__render_nan(wuffs_base__slice_u8 dst) {
}
static size_t //
wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(
wuffs_private_impl__high_prec_dec__render_exponent_absent(
wuffs_base__slice_u8 dst,
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec* h,
uint32_t precision,
uint32_t options) {
size_t n = (h->negative ||
@@ -18831,9 +18790,9 @@ wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(
}
static size_t //
wuffs_base__private_implementation__high_prec_dec__render_exponent_present(
wuffs_private_impl__high_prec_dec__render_exponent_present(
wuffs_base__slice_u8 dst,
wuffs_base__private_implementation__high_prec_dec* h,
wuffs_private_impl__high_prec_dec* h,
uint32_t precision,
uint32_t options) {
int32_t exp = 0;
@@ -18930,9 +18889,9 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
// unless x is subnormal. Also take care of Inf and NaN.
if (exp2 == 0x7FF) {
if (man != 0) {
return wuffs_base__private_implementation__render_nan(dst);
return wuffs_private_impl__render_nan(dst);
}
return wuffs_base__private_implementation__render_inf(dst, neg, options);
return wuffs_private_impl__render_inf(dst, neg, options);
} else if (exp2 == 0) {
exp2 = -1022;
} else {
@@ -18946,11 +18905,11 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
}
// Convert from the (neg, exp2, man) tuple to an HPD.
wuffs_base__private_implementation__high_prec_dec h;
wuffs_base__private_implementation__high_prec_dec__assign(&h, man, neg);
wuffs_private_impl__high_prec_dec h;
wuffs_private_impl__high_prec_dec__assign(&h, man, neg);
if (h.num_digits > 0) {
wuffs_base__private_implementation__high_prec_dec__lshift(
&h, exp2 - 52); // 52 mantissa bits.
wuffs_private_impl__high_prec_dec__lshift(&h,
exp2 - 52); // 52 mantissa bits.
}
// Handle the "%e" and "%f" formats.
@@ -18958,27 +18917,25 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT)) {
case WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_ABSENT: // The "%"f" format.
if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {
wuffs_base__private_implementation__high_prec_dec__round_just_enough(
&h, exp2, man);
wuffs_private_impl__high_prec_dec__round_just_enough(&h, exp2, man);
int32_t p = ((int32_t)(h.num_digits)) - h.decimal_point;
precision = ((uint32_t)(wuffs_base__i32__max(0, p)));
} else {
wuffs_base__private_implementation__high_prec_dec__round_nearest(
wuffs_private_impl__high_prec_dec__round_nearest(
&h, ((int32_t)precision) + h.decimal_point);
}
return wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(
return wuffs_private_impl__high_prec_dec__render_exponent_absent(
dst, &h, precision, options);
case WUFFS_BASE__RENDER_NUMBER_FXX__EXPONENT_PRESENT: // The "%e" format.
if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {
wuffs_base__private_implementation__high_prec_dec__round_just_enough(
&h, exp2, man);
wuffs_private_impl__high_prec_dec__round_just_enough(&h, exp2, man);
precision = (h.num_digits > 0) ? (h.num_digits - 1) : 0;
} else {
wuffs_base__private_implementation__high_prec_dec__round_nearest(
wuffs_private_impl__high_prec_dec__round_nearest(
&h, ((int32_t)precision) + 1);
}
return wuffs_base__private_implementation__high_prec_dec__render_exponent_present(
return wuffs_private_impl__high_prec_dec__render_exponent_present(
dst, &h, precision, options);
}
@@ -18987,16 +18944,14 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
// rounding and determine whether to use "%e" or "%f".
int32_t e_threshold = 0;
if (options & WUFFS_BASE__RENDER_NUMBER_FXX__JUST_ENOUGH_PRECISION) {
wuffs_base__private_implementation__high_prec_dec__round_just_enough(
&h, exp2, man);
wuffs_private_impl__high_prec_dec__round_just_enough(&h, exp2, man);
precision = h.num_digits;
e_threshold = 6;
} else {
if (precision == 0) {
precision = 1;
}
wuffs_base__private_implementation__high_prec_dec__round_nearest(
&h, ((int32_t)precision));
wuffs_private_impl__high_prec_dec__round_nearest(&h, ((int32_t)precision));
e_threshold = ((int32_t)precision);
int32_t nd = ((int32_t)(h.num_digits));
if ((e_threshold > nd) && (nd >= h.decimal_point)) {
@@ -19008,7 +18963,7 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
int32_t e = h.decimal_point - 1;
if ((e < -4) || (e_threshold <= e)) {
uint32_t p = wuffs_base__u32__min(precision, h.num_digits);
return wuffs_base__private_implementation__high_prec_dec__render_exponent_present(
return wuffs_private_impl__high_prec_dec__render_exponent_present(
dst, &h, (p > 0) ? (p - 1) : 0, options);
}
@@ -19018,7 +18973,7 @@ wuffs_base__render_number_f64(wuffs_base__slice_u8 dst,
p = ((int32_t)(h.num_digits));
}
precision = ((uint32_t)(wuffs_base__i32__max(0, p - h.decimal_point)));
return wuffs_base__private_implementation__high_prec_dec__render_exponent_absent(
return wuffs_private_impl__high_prec_dec__render_exponent_absent(
dst, &h, precision, options);
}
@@ -19118,7 +19073,7 @@ static const uint8_t wuffs_base__parse_number__hexadecimal_digits[256] = {
// 8 9 A B C D E F
};
static const uint8_t wuffs_base__private_implementation__encode_base16[16] = {
static const uint8_t wuffs_private_impl__encode_base16[16] = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // 0x00 ..= 0x07.
0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, // 0x08 ..= 0x0F.
};
@@ -19371,7 +19326,7 @@ static const uint8_t wuffs_base__render_number__first_hundred[200] = {
};
static size_t //
wuffs_base__private_implementation__render_number_u64(wuffs_base__slice_u8 dst,
wuffs_private_impl__render_number_u64(wuffs_base__slice_u8 dst,
uint64_t x,
uint32_t options,
bool neg) {
@@ -19428,16 +19383,14 @@ wuffs_base__render_number_i64(wuffs_base__slice_u8 dst,
if (neg) {
u = 1 + ~u;
}
return wuffs_base__private_implementation__render_number_u64(dst, u, options,
neg);
return wuffs_private_impl__render_number_u64(dst, u, options, neg);
}
WUFFS_BASE__MAYBE_STATIC size_t //
wuffs_base__render_number_u64(wuffs_base__slice_u8 dst,
uint64_t x,
uint32_t options) {
return wuffs_base__private_implementation__render_number_u64(dst, x, options,
false);
return wuffs_private_impl__render_number_u64(dst, x, options, false);
}
// ---------------- Base-16
@@ -19544,8 +19497,8 @@ wuffs_base__base_16__encode2(wuffs_base__slice_u8 dst,
while (n--) {
uint8_t c = *s;
d[0] = wuffs_base__private_implementation__encode_base16[c >> 4];
d[1] = wuffs_base__private_implementation__encode_base16[c & 0x0F];
d[0] = wuffs_private_impl__encode_base16[c >> 4];
d[1] = wuffs_private_impl__encode_base16[c & 0x0F];
d += 2;
s += 1;
}
@@ -19583,8 +19536,8 @@ wuffs_base__base_16__encode4(wuffs_base__slice_u8 dst,
uint8_t c = *s;
d[0] = '\\';
d[1] = 'x';
d[2] = wuffs_base__private_implementation__encode_base16[c >> 4];
d[3] = wuffs_base__private_implementation__encode_base16[c & 0x0F];
d[2] = wuffs_private_impl__encode_base16[c >> 4];
d[3] = wuffs_private_impl__encode_base16[c & 0x0F];
d += 4;
s += 1;
}
+21 -28
View File
@@ -134,11 +134,11 @@ test_wuffs_core_multiply_u64() {
// ---------------- String Conversions Tests
// wuffs_base__private_implementation__high_prec_dec__to_debug_string converts
// hpd into a human-readable NUL-terminated C string.
// wuffs_private_impl__high_prec_dec__to_debug_string converts hpd into a
// human-readable NUL-terminated C string.
const char* //
wuffs_base__private_implementation__high_prec_dec__to_debug_string(
wuffs_base__private_implementation__high_prec_dec* hpd,
wuffs_private_impl__high_prec_dec__to_debug_string(
wuffs_private_impl__high_prec_dec* hpd,
wuffs_base__slice_u8 dst) {
if (!hpd) {
return "high_prec_dec__to_debug_string: invalid hpd";
@@ -153,8 +153,7 @@ wuffs_base__private_implementation__high_prec_dec__to_debug_string(
*p++ = hpd->negative ? '-' : '+';
// Digits and decimal point.
if (hpd->decimal_point >
+WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
if (hpd->decimal_point > +WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
// We have "infinity".
if ((q - p) < 3) {
goto too_short;
@@ -165,7 +164,7 @@ wuffs_base__private_implementation__high_prec_dec__to_debug_string(
goto nul_terminator;
} else if (hpd->decimal_point <
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
-WUFFS_PRIVATE_IMPL__HPD__DECIMAL_POINT__RANGE) {
// We have "epsilon": a very small number, equivalent to zero.
if ((q - p) < 3) {
goto too_short;
@@ -184,8 +183,8 @@ wuffs_base__private_implementation__high_prec_dec__to_debug_string(
goto nul_terminator;
} else if (hpd->decimal_point < 0) {
// Referring to the wuffs_base__private_implementation__high_prec_dec
// typedef's comment, we have something like ".00789".
// Referring to the wuffs_private_impl__high_prec_dec typedef's comment, we
// have something like ".00789".
if ((q - p) < (hpd->num_digits + ((uint32_t)(-hpd->decimal_point)) + 1)) {
goto too_short;
}
@@ -208,8 +207,8 @@ wuffs_base__private_implementation__high_prec_dec__to_debug_string(
}
} else if (((uint32_t)(hpd->decimal_point)) <= hpd->num_digits) {
// Referring to the wuffs_base__private_implementation__high_prec_dec
// typedef's comment, we have something like "78.9".
// Referring to the wuffs_private_impl__high_prec_dec typedef's comment, we
// have something like "78.9".
if ((q - p) < (hpd->num_digits + 1)) {
goto too_short;
}
@@ -231,8 +230,8 @@ wuffs_base__private_implementation__high_prec_dec__to_debug_string(
}
} else {
// Referring to the wuffs_base__private_implementation__high_prec_dec
// typedef's comment, we have something like "78900.".
// Referring to the wuffs_private_impl__high_prec_dec typedef's comment, we
// have something like "78900.".
if ((q - p) < (((uint32_t)(hpd->decimal_point)) + 1)) {
goto too_short;
}
@@ -338,17 +337,15 @@ test_wuffs_strconv_hpd_rounded_integer() {
};
for (size_t tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
wuffs_base__private_implementation__high_prec_dec hpd;
wuffs_private_impl__high_prec_dec hpd;
CHECK_STATUS(
"hpd__parse",
wuffs_base__private_implementation__high_prec_dec__parse(
wuffs_private_impl__high_prec_dec__parse(
&hpd,
wuffs_base__make_slice_u8((uint8_t*)(void*)test_cases[tc].str,
strlen(test_cases[tc].str)),
WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
uint64_t have =
wuffs_base__private_implementation__high_prec_dec__rounded_integer(
&hpd);
uint64_t have = wuffs_private_impl__high_prec_dec__rounded_integer(&hpd);
if (have != test_cases[tc].want) {
RETURN_FAIL("\"%s\": have %" PRIu64 ", want %" PRIu64, test_cases[tc].str,
have, test_cases[tc].want);
@@ -384,28 +381,24 @@ test_wuffs_strconv_hpd_shift() {
};
for (size_t tc = 0; tc < WUFFS_TESTLIB_ARRAY_SIZE(test_cases); tc++) {
wuffs_base__private_implementation__high_prec_dec hpd;
wuffs_private_impl__high_prec_dec hpd;
CHECK_STATUS(
"hpd__parse",
wuffs_base__private_implementation__high_prec_dec__parse(
wuffs_private_impl__high_prec_dec__parse(
&hpd,
wuffs_base__make_slice_u8((uint8_t*)(void*)test_cases[tc].str,
strlen(test_cases[tc].str)),
WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS));
int32_t shift = test_cases[tc].shift;
if (shift > 0) {
wuffs_base__private_implementation__high_prec_dec__small_rshift(
&hpd, (uint32_t)(+shift));
wuffs_private_impl__high_prec_dec__small_rshift(&hpd, (uint32_t)(+shift));
} else if (shift < 0) {
wuffs_base__private_implementation__high_prec_dec__small_lshift(
&hpd, (uint32_t)(-shift));
wuffs_private_impl__high_prec_dec__small_lshift(&hpd, (uint32_t)(-shift));
}
uint8_t have[1024];
CHECK_STRING(
wuffs_base__private_implementation__high_prec_dec__to_debug_string(
&hpd,
wuffs_base__make_slice_u8(have, WUFFS_TESTLIB_ARRAY_SIZE(have))));
CHECK_STRING(wuffs_private_impl__high_prec_dec__to_debug_string(
&hpd, wuffs_base__make_slice_u8(have, WUFFS_TESTLIB_ARRAY_SIZE(have))));
if (strcmp(((const char*)(void*)(have)), test_cases[tc].want)) {
RETURN_FAIL("\"%s\" %s %" PRId32 ":\n have: \"%s\"\n want: \"%s\"",
test_cases[tc].str, ((shift > 0) ? ">>" : "<<"),