mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
floatconv: optimize parse_number_f64 fallback
The time taken running script/manual-test-parse-number-f64.cc on the parse-number-fxx-test-data suite falls from 3.565s to 2.491s (a 1.43x ratio; the inverse is 0.70 = 100% - 30%). Fixes #113
This commit is contained in:
1 parent
05f0172b93
commit
00d5e35865
5 files changed
+78
-24
No files matched your search
@@ -1304,24 +1304,41 @@ wuffs_base__private_implementation__high_prec_dec__to_f64(
|
||||
// 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) {
|
||||
//
|
||||
// When we have 19 or fewer mantissa digits, run Eisel-Lemire once (trying
|
||||
// for an exact result). When we have more than 19 mantissa digits, run it
|
||||
// twice to get a lower and upper bound. We still have an exact result
|
||||
// (within f64's rounding margin) if both bounds are equal (and valid).
|
||||
uint32_t i_max = h->num_digits;
|
||||
if (i_max > 19) {
|
||||
i_max = 19;
|
||||
}
|
||||
int32_t exp10 = h->decimal_point - ((int32_t)i_max);
|
||||
if ((-307 <= exp10) && (exp10 <= 288)) {
|
||||
uint64_t man = 0;
|
||||
uint32_t i;
|
||||
for (i = 0; i < h->num_digits; i++) {
|
||||
for (i = 0; i < i_max; i++) {
|
||||
man = (10 * man) + h->digits[i];
|
||||
}
|
||||
int32_t exp10 = h->decimal_point - ((int32_t)(h->num_digits));
|
||||
if ((man != 0) && (-307 <= exp10) && (exp10 <= 288)) {
|
||||
int64_t r =
|
||||
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, exp10);
|
||||
if (r >= 0) {
|
||||
wuffs_base__result_f64 ret;
|
||||
ret.status.repr = NULL;
|
||||
ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(
|
||||
((uint64_t)r) | (((uint64_t)(h->negative)) << 63));
|
||||
return ret;
|
||||
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);
|
||||
if (r1 != r0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
wuffs_base__result_f64 ret;
|
||||
ret.status.repr = NULL;
|
||||
ret.value = wuffs_base__ieee_754_bit_representation__from_u64_to_f64(
|
||||
((uint64_t)r0) | (((uint64_t)(h->negative)) << 63));
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user