Remove the medium_prec_bin type

It's obsolete now that we have an Eisel algorithm implementation.

Binary size, before:
29416 gen/lib/c/clang-5.0-dynamic/wuffs-base-f64conv.lo
29688 gen/lib/c/clang-5.0-static/wuffs-base-f64conv.o
31792 gen/lib/c/gcc-dynamic/wuffs-base-f64conv.lo
31792 gen/lib/c/gcc-static/wuffs-base-f64conv.o

After:
28744 gen/lib/c/clang-5.0-dynamic/wuffs-base-f64conv.lo
28968 gen/lib/c/clang-5.0-static/wuffs-base-f64conv.o
31176 gen/lib/c/gcc-dynamic/wuffs-base-f64conv.lo
31176 gen/lib/c/gcc-static/wuffs-base-f64conv.o

Processing citylots.json from github.com/zemirco/sf-city-lots-json
$ g++ -O3 script/process-json-numbers.c
$ time ./a.out -parse-number-f64 < citylots.json
Before/After:
real    0m1.031s
real	0m0.975s
$ time ./a.out                   < citylots.json
Before/After:
real    0m0.807s
real	0m0.729s
This commit is contained in:
Nigel Tao committed 2020-07-07 16:07:35 +10:00
1 parent 86093c8194
commit 832edf7227
4 files changed
+110 -867

No files matched your search

+25 -333
View File
@@ -9857,13 +9857,6 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// --------
// The wuffs_base__private_implementation__etc_powers_of_10 tables were printed
// by script/print-mpb-powers-of-10.go. That script has an optional -detail
// flag, whose output is not copied here, which prints further detail.
//
// These tables are used in
// wuffs_base__private_implementation__medium_prec_bin__assign_from_hpd.
// wuffs_base__private_implementation__powers_of_10 contains truncated
// approximations to the powers of 10, ranging from 1e-326 to 1e+310 inclusive,
// as 637 uint32_t quintuples (128-bit mantissa, 32-bit base-2 exponent biased
@@ -9887,6 +9880,8 @@ wuffs_base__private_implementation__high_prec_dec__round_just_enough(
// Similarly, 1e+68 is approximated by the uint64_t quintuple
// (0x63EE4BDD, 0x4CA7AAA8, 0xD4C4FB27, 0xED63A231, 0x0520) which means:
// 0xED63A231_D4C4FB27.4CA7AAA8_63EE4BDD * (2 ** (0x0520 - 0x04BE))
//
// This table was generated by by script/print-mpb-powers-of-10.go
static const uint32_t wuffs_base__private_implementation__powers_of_10[3185] = {
0xF7604B57, 0x014BB630, 0xFE98746D, 0x84A57695, 0x0004, // 1e-326
0x35385E2D, 0x419EA3BD, 0x7E3E9188, 0xA5CED43B, 0x0007, // 1e-325
@@ -10672,326 +10667,6 @@ wuffs_base__private_implementation__parse_number_f64_eisel(uint64_t man,
// --------
// wuffs_base__private_implementation__medium_prec_bin (abbreviated as MPB) is
// a fixed precision floating point binary number. Unlike IEEE 754 Floating
// Point, it cannot represent infinity or NaN (Not a Number).
//
// "Medium precision" means that the mantissa holds 64 binary digits, a little
// more than "double precision", and sizeof(MPB) > sizeof(double). 64 is
// obviously the number of bits in a uint64_t.
//
// An MPB isn't for general purpose arithmetic, only for conversions to and
// from IEEE 754 double-precision floating point.
//
// There is no implicit mantissa bit. The mantissa field is zero if and only if
// the overall floating point value is ±0. An MPB is normalized if the mantissa
// is zero or its high bit (the 1<<63 bit) is set.
//
// There is no negative bit. An MPB can only represent non-negative numbers.
//
// The "all fields are zero" value is valid, and represents the number +0.
//
// This is the "Do It Yourself Floating Point" data structure from Loitsch,
// "Printing Floating-Point Numbers Quickly and Accurately with Integers"
// (https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf).
//
// Florian Loitsch is also the primary contributor to
// https://github.com/google/double-conversion
typedef struct {
uint64_t mantissa;
int32_t exp2;
} wuffs_base__private_implementation__medium_prec_bin;
static uint32_t //
wuffs_base__private_implementation__medium_prec_bin__normalize(
wuffs_base__private_implementation__medium_prec_bin* m) {
if (m->mantissa == 0) {
return 0;
}
uint32_t shift = wuffs_base__count_leading_zeroes_u64(m->mantissa);
m->mantissa <<= shift;
m->exp2 -= (int32_t)shift;
return shift;
}
// wuffs_base__private_implementation__medium_prec_bin__mul_pow_10 sets m to be
// (m * pow), where pow comes from an etc__powers_of_10 triple starting at p.
//
// The result is rounded, but not necessarily normalized.
//
// Preconditions:
// - m is non-NULL.
// - m->mantissa is non-zero.
// - m->mantissa's high bit is set (i.e. m is normalized).
//
// The etc__powers_of_10 triple is already normalized.
static void //
wuffs_base__private_implementation__medium_prec_bin__mul_pow_10(
wuffs_base__private_implementation__medium_prec_bin* m,
const uint32_t* p) {
uint64_t p_mantissa = ((uint64_t)p[2]) | (((uint64_t)p[3]) << 32);
int32_t p_exp2 = (int32_t)p[4];
wuffs_base__multiply_u64__output o =
wuffs_base__multiply_u64(m->mantissa, p_mantissa);
// Round the mantissa up. It cannot overflow because the maximum possible
// value of o.hi is 0xFFFFFFFFFFFFFFFE.
m->mantissa = o.hi + (o.lo >> 63);
m->exp2 = m->exp2 + p_exp2 + 128 - 1214;
}
// wuffs_base__private_implementation__medium_prec_bin__as_f64 converts m to a
// double (what C calls a double-precision float64).
//
// Preconditions:
// - m is non-NULL.
// - m->mantissa is non-zero.
// - m->mantissa's high bit is set (i.e. m is normalized).
static double //
wuffs_base__private_implementation__medium_prec_bin__as_f64(
const wuffs_base__private_implementation__medium_prec_bin* m,
bool negative) {
uint64_t mantissa64 = m->mantissa;
// An mpb's mantissa has the implicit (binary) decimal point at the right
// hand end of the mantissa's explicit digits. A double-precision's mantissa
// has that decimal point near the left hand end. There's also an explicit
// versus implicit leading 1 bit (binary digit). Together, the difference in
// semantics corresponds to adding 63.
int32_t exp2 = m->exp2 + 63;
// Ensure that exp2 is at least -1022, the minimum double-precision exponent
// for normal (as opposed to subnormal) numbers.
if (-1022 > exp2) {
uint32_t n = (uint32_t)(-1022 - exp2);
mantissa64 >>= n;
exp2 += (int32_t)n;
}
// Extract the (1 + 52) bits from the 64-bit mantissa64. 52 is the number of
// explicit mantissa bits in a double-precision f64.
//
// Before, we have 64 bits and due to normalization, the high bit 'H' is 1.
// 63 55 47 etc 15 7
// H210_9876_5432_1098_7654_etc_etc_etc_5432_1098_7654_3210
// ++++_++++_++++_++++_++++_etc_etc_etc_++++_+..._...._.... Kept bits.
// ...._...._...H_2109_8765_etc_etc_etc_6543_2109_8765_4321 After shifting.
// After, we have 53 bits (and bit #52 is this 'H' bit).
uint64_t mantissa53 = mantissa64 >> 11;
// Round up if the old bit #10 (the highest bit dropped by shifting) was set.
// We also fix any overflow from rounding up.
if (mantissa64 & 1024) {
mantissa53++;
if ((mantissa53 >> 53) != 0) {
mantissa53 >>= 1;
exp2++;
}
}
// Handle double-precision infinity (a nominal exponent of 1024) and
// subnormals (an exponent of -1023 and no implicit mantissa bit, bit #52).
if (exp2 >= 1024) {
mantissa53 = 0;
exp2 = 1024;
} else if ((mantissa53 >> 52) == 0) {
exp2 = -1023;
}
// Pack the bits and return.
const int32_t f64_bias = -1023;
uint64_t exp2_bits =
(uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1.
uint64_t bits = (mantissa53 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1.
(exp2_bits << 52) | //
(negative ? 0x8000000000000000 : 0); // (1 << 63).
return wuffs_base__ieee_754_bit_representation__to_f64(bits);
}
// wuffs_base__private_implementation__medium_prec_bin__parse_number_f64
// converts from an HPD to a double, using an MPB as scratch space. It returns
// a NULL status.repr if there is no ambiguity in the truncation or rounding to
// a float64 (an IEEE 754 double-precision floating point value).
//
// It may modify m even if it returns a non-NULL status.repr.
static wuffs_base__result_f64 //
wuffs_base__private_implementation__medium_prec_bin__parse_number_f64(
wuffs_base__private_implementation__medium_prec_bin* m,
const wuffs_base__private_implementation__high_prec_dec* h,
bool skip_fast_path_for_tests) {
do {
// m->mantissa is a uint64_t, which is an integer approximation to a
// rational value - h's underlying digits after m's normalization. This
// error is an upper bound on the difference between the approximate and
// actual value.
//
// The DiyFpStrtod function in https://github.com/google/double-conversion
// uses a finer grain (1/8th of the ULP, Unit in the Last Place) when
// tracking error. This implementation is coarser (1 ULP) but simpler.
//
// It is an error in the "numerical approximation" sense, not in the
// typical programming sense (as in "bad input" or "a result type").
uint64_t error = 0;
// Convert up to 19 decimal digits (in h->digits) to 64 binary digits (in
// m->mantissa): (1e19 < (1<<64)) and ((1<<64) < 1e20). If we have more
// than 19 digits, we're truncating (with error).
uint32_t i;
uint32_t i_end = h->num_digits;
if (i_end > 19) {
i_end = 19;
error = 1;
}
uint64_t mantissa = 0;
for (i = 0; i < i_end; i++) {
mantissa = (10 * mantissa) + h->digits[i];
}
m->mantissa = mantissa;
m->exp2 = 0;
// Check that exp10 lies in the etc__powers_of_10 range (637 triples).
int32_t exp10 = h->decimal_point - ((int32_t)(i_end));
if ((exp10 < -326) || (+310 < exp10)) {
goto fail;
}
// Try a fast path, if float64 math would be exact.
//
// 15 is such that 1e15 can be losslessly represented in a float64
// mantissa: (1e15 < (1<<53)) and ((1<<53) < 1e16).
//
// 22 is the maximum valid index for the
// wuffs_base__private_implementation__f64_powers_of_10 array.
do {
if (skip_fast_path_for_tests || ((mantissa >> 52) != 0)) {
break;
}
double d = (double)mantissa;
if (exp10 == 0) {
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
ret.value = h->negative ? -d : +d;
return ret;
} else if (exp10 > 0) {
if (exp10 > 22) {
if (exp10 > (15 + 22)) {
break;
}
// If exp10 is in the range 23 ..= 37, try moving a few of the zeroes
// from the exponent to the mantissa. If we're still under 1e15, we
// haven't truncated any mantissa bits.
d *= wuffs_base__private_implementation__f64_powers_of_10[exp10 - 22];
exp10 = 22;
if (d >= 1e15) {
break;
}
}
d *= wuffs_base__private_implementation__f64_powers_of_10[exp10];
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
ret.value = h->negative ? -d : +d;
return ret;
} else { // "if (exp10 < 0)" is effectively "if (true)" here.
if (exp10 < -22) {
break;
}
d /= wuffs_base__private_implementation__f64_powers_of_10[-exp10];
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
ret.value = h->negative ? -d : +d;
return ret;
}
} while (0);
// Normalize (and scale the error).
error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m);
// Multiplying two MPB values nominally multiplies two mantissas, call them
// A and B, which are integer approximations to the precise values (A+a)
// and (B+b) for some error terms a and b.
//
// MPB multiplication calculates (((A+a) * (B+b)) >> 64) to be ((A*B) >>
// 64). Shifting (truncating) and rounding introduces further error. The
// difference between the calculated result:
// ((A*B ) >> 64)
// and the true result:
// ((A*B + A*b + a*B + a*b) >> 64) + rounding_error
// is:
// (( A*b + a*B + a*b) >> 64) + rounding_error
// which can be re-grouped as:
// ((A*b) >> 64) + ((a*(B+b)) >> 64) + rounding_error
//
// Now, let A and a be "m->mantissa" and "error", and B and b be the
// pre-calculated power of 10. A and B are both less than (1 << 64), a is
// the "error" local variable and b is less than 1.
//
// An upper bound (in absolute value) on ((A*b) >> 64) is therefore 1.
//
// An upper bound on ((a*(B+b)) >> 64) is a, also known as error.
//
// Finally, the rounding_error is at most 1.
//
// In total, calling mpb__mul_pow_10 will raise the worst-case error by 2.
// The subsequent re-normalization can multiply that by a further factor.
// Multiply by powers_of_10[etc].
wuffs_base__private_implementation__medium_prec_bin__mul_pow_10(
m,
&wuffs_base__private_implementation__powers_of_10[5 * (exp10 + 326)]);
error += 2;
error <<= wuffs_base__private_implementation__medium_prec_bin__normalize(m);
// We have a good approximation of h, but we still have to check whether
// the error is small enough. Equivalently, whether the number of surplus
// mantissa bits (the bits dropped when going from m's 64 mantissa bits to
// the smaller number of double-precision mantissa bits) would always round
// up or down, even when perturbed by ±error. We start at 11 surplus bits
// (m has 64, double-precision has 1+52), but it can be higher for
// subnormals.
//
// In many cases, the error is small enough and we return true.
const int32_t f64_bias = -1023;
int32_t subnormal_exp2 = f64_bias - 63;
uint32_t surplus_bits = 11;
if (subnormal_exp2 >= m->exp2) {
surplus_bits += 1 + ((uint32_t)(subnormal_exp2 - m->exp2));
}
uint64_t surplus_mask =
(((uint64_t)1) << surplus_bits) - 1; // e.g. 0x07FF.
uint64_t surplus = m->mantissa & surplus_mask;
uint64_t halfway = ((uint64_t)1) << (surplus_bits - 1); // e.g. 0x0400.
// Do the final calculation in *signed* arithmetic.
int64_t i_surplus = (int64_t)surplus;
int64_t i_halfway = (int64_t)halfway;
int64_t i_error = (int64_t)error;
if ((i_surplus > (i_halfway - i_error)) &&
(i_surplus < (i_halfway + i_error))) {
goto fail;
}
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
ret.value = wuffs_base__private_implementation__medium_prec_bin__as_f64(
m, h->negative);
return ret;
} while (0);
fail:
do {
wuffs_base__result_f64 ret;
ret.status.repr = "#base: mpb__parse_number_f64 failed";
ret.value = 0;
return ret;
} while (0);
}
// --------
static wuffs_base__result_f64 //
wuffs_base__parse_number_f64_special(wuffs_base__slice_u8 s,
const char* fallback_status_repr) {
@@ -11114,12 +10789,29 @@ wuffs_base__private_implementation__parse_number_f64__fallback(
goto infinity;
}
wuffs_base__private_implementation__medium_prec_bin m;
wuffs_base__result_f64 mpb_result =
wuffs_base__private_implementation__medium_prec_bin__parse_number_f64(
&m, h, false);
if (mpb_result.status.repr == NULL) {
return mpb_result;
// Try the fast Eisel algorithm again. Calculating the (man, exp10) pair
// from the high_prec_dec h is more correct but slower than the approach
// taken in wuffs_base__parse_number_f64. The latter is optimized for the
// common cases (e.g. assuming no underscores or a leading '+' sign) rather
// than the full set of cases allowed by the Wuffs API.
if (h->num_digits <= 19) {
uint64_t man = 0;
uint32_t i;
for (i = 0; i < h->num_digits; i++) {
man = (10 * man) + h->digits[i];
}
int32_t exp10 = h->decimal_point - ((int32_t)(h->num_digits));
if ((man != 0) && (-326 <= exp10) && (exp10 <= 310)) {
int64_t r = wuffs_base__private_implementation__parse_number_f64_eisel(
man, exp10);
if (r >= 0) {
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
ret.value = wuffs_base__ieee_754_bit_representation__to_f64(
((uint64_t)r) | (((uint64_t)(h->negative)) << 63));
return ret;
}
}
}
// Scale by powers of 2 until we're in the range [½ .. 1], which gives us