Refactor f64conv's wuffs_base__parse_number_f64

Binary size, before:
27704 gen/lib/c/clang-5.0-dynamic/wuffs-base-f64conv.lo
27896 gen/lib/c/clang-5.0-static/wuffs-base-f64conv.o
29120 gen/lib/c/gcc-dynamic/wuffs-base-f64conv.lo
29120 gen/lib/c/gcc-static/wuffs-base-f64conv.o

After:
27848 gen/lib/c/clang-5.0-dynamic/wuffs-base-f64conv.lo
28040 gen/lib/c/clang-5.0-static/wuffs-base-f64conv.o
30392 gen/lib/c/gcc-dynamic/wuffs-base-f64conv.lo
30392 gen/lib/c/gcc-static/wuffs-base-f64conv.o

$ g++ -O3 script/process-json-numbers.c
$ time ./a.out -parse-number-f64 < citylots.json
Before/After:
real    0m1.306s
real	0m1.231s

citylots.json is from github.com/zemirco/sf-city-lots-json

name                                                old time/op  new time/op  delta
wuffs_strconv_parse_number_f64_1_lsh53_add0/clang5  73.2ns ± 2%  77.0ns ± 0%   +5.19%  (p=0.016 n=5+4)
wuffs_strconv_parse_number_f64_1_lsh53_add1/clang5   688ns ± 0%   690ns ± 0%     ~     (p=0.079 n=5+5)
wuffs_strconv_parse_number_f64_pi_long/clang5        113ns ± 1%    92ns ± 0%  -18.44%  (p=0.000 n=5+4)
wuffs_strconv_parse_number_f64_pi_short/clang5      35.4ns ± 7%  35.0ns ± 3%     ~     (p=1.000 n=5+5)
wuffs_strconv_parse_number_f64_1_lsh53_add0/gcc7    67.0ns ± 0%  67.0ns ± 0%     ~     (all equal)
wuffs_strconv_parse_number_f64_1_lsh53_add1/gcc7     721ns ± 0%   712ns ± 0%   -1.30%  (p=0.008 n=5+5)
wuffs_strconv_parse_number_f64_pi_long/gcc7         90.4ns ± 1%  88.4ns ± 1%   -2.21%  (p=0.008 n=5+5)
wuffs_strconv_parse_number_f64_pi_short/gcc7        32.2ns ± 6%  32.6ns ± 2%     ~     (p=0.730 n=5+5)
This commit is contained in:
Nigel Tao committed 2020-07-06 11:45:51 +10:00
1 parent 177aa7f0c6
commit 54e20636e8
3 files changed
+80 -76

No files matched your search

+37 -35
View File
@@ -10953,10 +10953,8 @@ fallback:
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //
wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {
wuffs_base__private_implementation__medium_prec_bin m;
wuffs_base__private_implementation__high_prec_dec h;
wuffs_base__private_implementation__parse_number_f64__fallback(
wuffs_base__private_implementation__high_prec_dec* h) {
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
@@ -10967,23 +10965,18 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {
33, 36, 39, 43, 46, 49, 53, 56, 59, //
};
wuffs_base__status status =
wuffs_base__private_implementation__high_prec_dec__parse(&h, s);
if (status.repr) {
return wuffs_base__parse_number_f64_special(s, status.repr);
}
// Handle zero and obvious extremes. The largest and smallest positive
// finite f64 values are approximately 1.8e+308 and 4.9e-324.
if ((h.num_digits == 0) || (h.decimal_point < -326)) {
if ((h->num_digits == 0) || (h->decimal_point < -326)) {
goto zero;
} else if (h.decimal_point > 310) {
} else if (h->decimal_point > 310) {
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);
&m, h, false);
if (mpb_result.status.repr == NULL) {
return mpb_result;
}
@@ -10993,39 +10986,37 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {
// far, ending with a value certainly below 1 and possibly below ½...
const int32_t f64_bias = -1023;
int32_t exp2 = 0;
while (h.decimal_point > 0) {
uint32_t n = (uint32_t)(+h.decimal_point);
while (h->decimal_point > 0) {
uint32_t n = (uint32_t)(+h->decimal_point);
uint32_t shift =
(n < num_powers)
? powers[n]
: WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
wuffs_base__private_implementation__high_prec_dec__small_rshift(&h,
shift);
if (h.decimal_point <
wuffs_base__private_implementation__high_prec_dec__small_rshift(h, shift);
if (h->decimal_point <
-WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
goto zero;
}
exp2 += (int32_t)shift;
}
// ...then we shift left, putting us in [½ .. 1].
while (h.decimal_point <= 0) {
while (h->decimal_point <= 0) {
uint32_t shift;
if (h.decimal_point == 0) {
if (h.digits[0] >= 5) {
if (h->decimal_point == 0) {
if (h->digits[0] >= 5) {
break;
}
shift = (h.digits[0] <= 2) ? 2 : 1;
shift = (h->digits[0] <= 2) ? 2 : 1;
} else {
uint32_t n = (uint32_t)(-h.decimal_point);
uint32_t n = (uint32_t)(-h->decimal_point);
shift = (n < num_powers)
? powers[n]
: WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
}
wuffs_base__private_implementation__high_prec_dec__small_lshift(&h,
shift);
if (h.decimal_point >
wuffs_base__private_implementation__high_prec_dec__small_lshift(h, shift);
if (h->decimal_point >
+WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__DECIMAL_POINT__RANGE) {
goto infinity;
}
@@ -11041,7 +11032,7 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {
if (n > WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL) {
n = WUFFS_BASE__PRIVATE_IMPLEMENTATION__HPD__SHIFT__MAX_INCL;
}
wuffs_base__private_implementation__high_prec_dec__small_rshift(&h, n);
wuffs_base__private_implementation__high_prec_dec__small_rshift(h, n);
exp2 += (int32_t)n;
}
@@ -11051,9 +11042,9 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {
}
// Extract 53 bits for the mantissa (in base-2).
wuffs_base__private_implementation__high_prec_dec__small_lshift(&h, 53);
wuffs_base__private_implementation__high_prec_dec__small_lshift(h, 53);
uint64_t man2 =
wuffs_base__private_implementation__high_prec_dec__rounded_integer(&h);
wuffs_base__private_implementation__high_prec_dec__rounded_integer(h);
// Rounding might have added one bit. If so, shift and re-check overflow.
if ((man2 >> 53) != 0) {
@@ -11071,10 +11062,10 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {
// Pack the bits and return.
uint64_t exp2_bits =
(uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1.
uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1.
(exp2_bits << 52) | //
(h.negative ? 0x8000000000000000 : 0); // (1 << 63).
(uint64_t)((exp2 - f64_bias) & 0x07FF); // (1 << 11) - 1.
uint64_t bits = (man2 & 0x000FFFFFFFFFFFFF) | // (1 << 52) - 1.
(exp2_bits << 52) | //
(h->negative ? 0x8000000000000000 : 0); // (1 << 63).
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
@@ -11084,7 +11075,7 @@ wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {
zero:
do {
uint64_t bits = h.negative ? 0x8000000000000000 : 0;
uint64_t bits = h->negative ? 0x8000000000000000 : 0;
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
@@ -11094,7 +11085,7 @@ zero:
infinity:
do {
uint64_t bits = h.negative ? 0xFFF0000000000000 : 0x7FF0000000000000;
uint64_t bits = h->negative ? 0xFFF0000000000000 : 0x7FF0000000000000;
wuffs_base__result_f64 ret;
ret.status.repr = NULL;
@@ -11103,6 +11094,17 @@ infinity:
} while (0);
}
WUFFS_BASE__MAYBE_STATIC wuffs_base__result_f64 //
wuffs_base__parse_number_f64(wuffs_base__slice_u8 s) {
wuffs_base__private_implementation__high_prec_dec h;
wuffs_base__status status =
wuffs_base__private_implementation__high_prec_dec__parse(&h, s);
if (status.repr) {
return wuffs_base__parse_number_f64_special(s, status.repr);
}
return wuffs_base__private_implementation__parse_number_f64__fallback(&h);
}
// --------
static inline size_t //