Skip to content

Commit eb3e640

Browse files
committed
Return bool from compute_float_64
1 parent 9475b94 commit eb3e640

File tree

1 file changed

+30
-38
lines changed

1 file changed

+30
-38
lines changed

src/generic/stage2/numberparsing.h

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace numberparsing {
2424
// set to false. This should work *most of the time* (like 99% of the time).
2525
// We assume that power is in the [FASTFLOAT_SMALLEST_POWER,
2626
// FASTFLOAT_LARGEST_POWER] interval: the caller is responsible for this check.
27-
simdjson_really_inline double compute_float_64(int64_t power, uint64_t i, bool negative, bool *success) {
27+
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
3030
// Clinger WD. How to read floating point numbers accurately.
@@ -40,7 +40,7 @@ simdjson_really_inline double compute_float_64(int64_t power, uint64_t i, bool n
4040
#endif
4141
// convert the integer into a double. This is lossless since
4242
// 0 <= i <= 2^53 - 1.
43-
double d = double(i);
43+
d = double(i);
4444
//
4545
// The general idea is as follows.
4646
// If 0 <= s < 2^53 and if 10^0 <= p <= 10^22 then
@@ -59,8 +59,7 @@ simdjson_really_inline double compute_float_64(int64_t power, uint64_t i, bool n
5959
if (negative) {
6060
d = -d;
6161
}
62-
*success = true;
63-
return d;
62+
return true;
6463
}
6564
// When 22 < power && power < 22 + 16, we could
6665
// hope for another, secondary fast path. It wa
@@ -85,7 +84,8 @@ simdjson_really_inline double compute_float_64(int64_t power, uint64_t i, bool n
8584
// In the slow path, we need to adjust i so that it is > 1<<63 which is always
8685
// possible, except if i == 0, so we handle i == 0 separately.
8786
if(i == 0) {
88-
return 0.0;
87+
d = 0.0;
88+
return true;
8989
}
9090

9191
// We are going to need to do some 64-bit arithmetic to get a more precise product.
@@ -135,8 +135,7 @@ simdjson_really_inline double compute_float_64(int64_t power, uint64_t i, bool n
135135
// This does happen, e.g. with 7.3177701707893310e+15.
136136
if (((product_middle + 1 == 0) && ((product_high & 0x1FF) == 0x1FF) &&
137137
(product_low + i < product_low))) { // let us be prudent and bail out.
138-
*success = false;
139-
return 0;
138+
return false;
140139
}
141140
upper = product_high;
142141
lower = product_middle;
@@ -157,25 +156,24 @@ simdjson_really_inline double compute_float_64(int64_t power, uint64_t i, bool n
157156
// floating-point values.
158157
if (simdjson_unlikely((lower == 0) && ((upper & 0x1FF) == 0) &&
159158
((mantissa & 3) == 1))) {
160-
// if mantissa & 1 == 1 we might need to round up.
161-
//
162-
// Scenarios:
163-
// 1. We are not in the middle. Then we should round up.
164-
//
165-
// 2. We are right in the middle. Whether we round up depends
166-
// on the last significant bit: if it is "one" then we round
167-
// up (round to even) otherwise, we do not.
168-
//
169-
// So if the last significant bit is 1, we can safely round up.
170-
// Hence we only need to bail out if (mantissa & 3) == 1.
171-
// Otherwise we may need more accuracy or analysis to determine whether
172-
// we are exactly between two floating-point numbers.
173-
// It can be triggered with 1e23.
174-
// Note: because the factor_mantissa and factor_mantissa_low are
175-
// almost always rounded down (except for small positive powers),
176-
// almost always should round up.
177-
*success = false;
178-
return 0;
159+
// if mantissa & 1 == 1 we might need to round up.
160+
//
161+
// Scenarios:
162+
// 1. We are not in the middle. Then we should round up.
163+
//
164+
// 2. We are right in the middle. Whether we round up depends
165+
// on the last significant bit: if it is "one" then we round
166+
// up (round to even) otherwise, we do not.
167+
//
168+
// So if the last significant bit is 1, we can safely round up.
169+
// Hence we only need to bail out if (mantissa & 3) == 1.
170+
// Otherwise we may need more accuracy or analysis to determine whether
171+
// we are exactly between two floating-point numbers.
172+
// It can be triggered with 1e23.
173+
// Note: because the factor_mantissa and factor_mantissa_low are
174+
// almost always rounded down (except for small positive powers),
175+
// almost always should round up.
176+
return false;
179177
}
180178

181179
mantissa += mantissa & 1;
@@ -193,15 +191,12 @@ simdjson_really_inline double compute_float_64(int64_t power, uint64_t i, bool n
193191
uint64_t real_exponent = c.exp - lz;
194192
// we have to check that real_exponent is in range, otherwise we bail out
195193
if (simdjson_unlikely((real_exponent < 1) || (real_exponent > 2046))) {
196-
*success = false;
197-
return 0;
194+
return false;
198195
}
199196
mantissa |= real_exponent << 52;
200197
mantissa |= (((uint64_t)negative) << 63);
201-
double d;
202198
memcpy(&d, &mantissa, sizeof(d));
203-
*success = true;
204-
return d;
199+
return true;
205200
}
206201

207202
static bool parse_float_strtod(const uint8_t *ptr, double *outDouble) {
@@ -392,9 +387,8 @@ simdjson_really_inline error_code write_float(const uint8_t *const src, bool neg
392387
writer.skip_double();
393388
return error;
394389
}
395-
bool success = true;
396-
double d = compute_float_64(exponent, i, negative, &success);
397-
if (!success) {
390+
double d;
391+
if (!compute_float_64(exponent, i, negative, d)) {
398392
// we are almost never going to get here.
399393
if (!parse_float_strtod(src, &d)) { return INVALID_NUMBER(src); }
400394
}
@@ -713,12 +707,10 @@ SIMDJSON_UNUSED simdjson_really_inline simdjson_result<double> parse_double(cons
713707
//
714708
// Assemble (or slow-parse) the float
715709
//
710+
double d;
716711
if (simdjson_likely(!overflow)) {
717-
bool success = true;
718-
double d = compute_float_64(exponent, i, negative, &success);
719-
if (success) { return d; }
712+
if (compute_float_64(exponent, i, negative, d)) { return d; }
720713
}
721-
double d;
722714
if (!parse_float_strtod(src-negative, &d)) {
723715
return NUMBER_ERROR;
724716
}

0 commit comments

Comments
 (0)