Skip to content

Commit a32a575

Browse files
committed
Minor tweaks.
1 parent e1ab2c9 commit a32a575

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

src/generic/stage2/numberparsing.h

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace numberparsing {
2222
// true, negate the result.
2323
// This function will only work in some cases, when it does not work, success is
2424
// set to false. This should work *most of the time* (like 99% of the time).
25-
// We assume that power is in the [FASTFLOAT_SMALLEST_POWER,
26-
// FASTFLOAT_LARGEST_POWER] interval: the caller is responsible for this check.
25+
// We assume that power is in the [simdjson_smallest_power,
26+
// simdjson_largest_power] interval: the caller is responsible for this check.
2727
simdjson_really_inline bool compute_float_64(int64_t power, uint64_t i, bool negative, double &d) {
2828
// we start with a fast path
2929
// It was described in
@@ -61,6 +61,7 @@ simdjson_really_inline bool compute_float_64(int64_t power, uint64_t i, bool neg
6161
}
6262
return true;
6363
}
64+
printf("OOO\n");
6465
// When 22 < power && power < 22 + 16, we could
6566
// hope for another, secondary fast path. It was
6667
// described by David M. Gay in "Correctly rounded
@@ -124,26 +125,24 @@ simdjson_really_inline bool compute_float_64(int64_t power, uint64_t i, bool neg
124125
// We are going to need to do some 64-bit arithmetic to get a precise product.
125126
// We use a table lookup approach.
126127
// It is safe because
127-
// power >= FASTFLOAT_SMALLEST_POWER
128-
// and power <= FASTFLOAT_LARGEST_POWER
128+
// power >= simdjson_smallest_power
129+
// and power <= simdjson_largest_power
129130
// We recover the mantissa of the power, it has a leading 1. It is always
130131
// rounded down.
131132
//
132133
// We want the most significant 64 bits of the product. We know
133134
// this will be non-zero because the most significant bit of i is
134135
// 1.
135-
136-
value128 firstproduct = full_multiplication(i, power_of_five_128[2 * (power - FASTFLOAT_SMALLEST_POWER)]);
137-
value128 secondproduct = full_multiplication(i, power_of_five_128[2 * (power - FASTFLOAT_SMALLEST_POWER) + 1]);
136+
const uint32_t index = 2 * uint32_t(power - simdjson_smallest_power);
137+
value128 firstproduct = full_multiplication(i, power_of_five_128[index]);
138+
value128 secondproduct = full_multiplication(i, power_of_five_128[index + 1]);
138139
firstproduct.low += secondproduct.high;
139-
if(secondproduct.high > firstproduct.low) {
140-
firstproduct.high++;
141-
}
140+
if(secondproduct.high > firstproduct.low) { firstproduct.high++; }
142141
uint64_t lower = firstproduct.low;
143142
uint64_t upper = firstproduct.high;
144143
// At this point, we might need to add at most one to firstproduct, but this
145144
// can only change the value of firstproduct.high if firstproduct.low is maximal.
146-
if(firstproduct.low == 0xFFFFFFFFFFFFFFFF) {
145+
if(simdjson_unlikely(firstproduct.low == 0xFFFFFFFFFFFFFFFF)) {
147146
// This is very unlikely, but if so, we need to do much more work!
148147
return false;
149148
}
@@ -161,26 +160,9 @@ simdjson_really_inline bool compute_float_64(int64_t power, uint64_t i, bool neg
161160
// which we guard against.
162161
// If we have lots of trailing zeros, we may fall right between two
163162
// floating-point values.
164-
if (simdjson_unlikely((lower == 0) && ((upper & 0x1FF) == 0) &&
163+
if (simdjson_unlikely((lower == 0) && (power >= 0) && (power <= 23) && ((upper & 0x1FF) == 0) &&
165164
((mantissa & 3) == 1))) {
166-
// if mantissa & 1 == 1 we might need to round up.
167-
//
168-
// Scenarios:
169-
// 1. We are not in the middle. Then we should round up.
170-
//
171-
// 2. We are right in the middle. Whether we round up depends
172-
// on the last significant bit: if it is "one" then we round
173-
// up (round to even) otherwise, we do not.
174-
//
175-
// So if the last significant bit is 1, we can safely round up.
176-
// Hence we only need to bail out if (mantissa & 3) == 1.
177-
// Otherwise we may need more accuracy or analysis to determine whether
178-
// we are exactly between two floating-point numbers.
179-
// It can be triggered with 1e23.
180-
// Note: because the factor_mantissa and factor_mantissa_low are
181-
// almost always rounded down (except for small positive powers),
182-
// almost always should round up.
183-
return false;
165+
mantissa ^= 1; // flip it so that we do not round up
184166
}
185167

186168
mantissa += mantissa & 1;
@@ -398,7 +380,7 @@ simdjson_really_inline error_code write_float(const uint8_t *const src, bool neg
398380
// NOTE: it's weird that the simdjson_unlikely() only wraps half the if, but it seems to get slower any other
399381
// way we've tried: https://github.com/simdjson/simdjson/pull/990#discussion_r448497331
400382
// To future reader: we'd love if someone found a better way, or at least could explain this result!
401-
if (simdjson_unlikely(exponent < FASTFLOAT_SMALLEST_POWER) || (exponent > FASTFLOAT_LARGEST_POWER)) {
383+
if (simdjson_unlikely(exponent < simdjson_smallest_power) || (exponent > simdjson_largest_power)) {
402384
// this is almost never going to get called!!!
403385
// we start anew, going slowly!!!
404386
// NOTE: This makes a *copy* of the writer and passes it to slow_float_parsing. This happens
@@ -724,7 +706,7 @@ SIMDJSON_UNUSED simdjson_really_inline simdjson_result<double> parse_double(cons
724706
if (p-start_exp_digits == 0 || p-start_exp_digits > 19) { return NUMBER_ERROR; }
725707

726708
exponent += exp_neg ? 0-exp : exp;
727-
overflow = overflow || exponent < FASTFLOAT_SMALLEST_POWER || exponent > FASTFLOAT_LARGEST_POWER;
709+
overflow = overflow || exponent < simdjson_smallest_power || exponent > simdjson_largest_power;
728710
}
729711

730712
//

src/jsoncharutils_tables.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ const uint32_t digit_to_val32[886] = {
201201
// per instruction set).
202202
///
203203

204-
constexpr int FASTFLOAT_SMALLEST_POWER = -344;
205-
constexpr int FASTFLOAT_LARGEST_POWER = 308;
204+
constexpr int simdjson_smallest_power = -344;
205+
constexpr int simdjson_largest_power = 308;
206206

207207
struct value128 {
208208
uint64_t low;

0 commit comments

Comments
 (0)