Skip to content

Commit b61ed01

Browse files
authored
Merge pull request #51 from fastfloat/dlemire/alt_long
Improves long-significand performance
2 parents ca51b64 + cf1a4ec commit b61ed01

File tree

4 files changed

+62
-84
lines changed

4 files changed

+62
-84
lines changed

include/fast_float/ascii_number.h

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fastfloat_really_inline
6060
parsed_number_string parse_number_string(const char *p, const char *pend, chars_format fmt) noexcept {
6161
parsed_number_string answer;
6262
answer.valid = false;
63+
answer.too_many_digits = false;
6364
answer.negative = (*p == '-');
6465
if ((*p == '-') || (*p == '+')) {
6566
++p;
@@ -81,10 +82,11 @@ parsed_number_string parse_number_string(const char *p, const char *pend, chars_
8182
uint64_t(*p - '0'); // might overflow, we will handle the overflow later
8283
++p;
8384
}
85+
const char *const end_of_integer_part = p;
86+
int64_t digit_count = int64_t(end_of_integer_part - start_digits);
8487
int64_t exponent = 0;
8588
if ((p != pend) && (*p == '.')) {
8689
++p;
87-
const char *first_after_period = p;
8890
#if FASTFLOAT_IS_BIG_ENDIAN == 0
8991
// Fast approach only tested under little endian systems
9092
if ((p + 8 <= pend) && is_made_of_eight_digits_fast(p)) {
@@ -101,19 +103,16 @@ parsed_number_string parse_number_string(const char *p, const char *pend, chars_
101103
++p;
102104
i = i * 10 + digit; // in rare cases, this will overflow, but that's ok
103105
}
104-
exponent = first_after_period - p;
106+
exponent = end_of_integer_part + 1 - p;
107+
digit_count -= exponent;
105108
}
106109
// we must have encountered at least one integer!
107-
if ((start_digits == p) || ((start_digits == p - 1) && (*start_digits == '.') )) {
110+
if (digit_count == 0) {
108111
return answer;
109112
}
110-
// digit_count is the exact number of digits.
111-
int32_t digit_count =
112-
int32_t(p - start_digits); // used later to guard against overflows
113-
if(exponent > 0) {digit_count--;}
113+
int64_t exp_number = 0; // explicit exponential part
114114
if ((fmt & chars_format::scientific) && (p != pend) && (('e' == *p) || ('E' == *p))) {
115115
const char * location_of_e = p;
116-
int64_t exp_number = 0; // exponential part
117116
++p;
118117
bool neg_exp = false;
119118
if ((p != pend) && ('-' == *p)) {
@@ -137,7 +136,8 @@ parsed_number_string parse_number_string(const char *p, const char *pend, chars_
137136
}
138137
++p;
139138
}
140-
exponent += (neg_exp ? -exp_number : exp_number);
139+
if(neg_exp) { exp_number = - exp_number; }
140+
exponent += exp_number;
141141
}
142142
} else {
143143
// If it scientific and not fixed, we have to bail out.
@@ -151,25 +151,40 @@ parsed_number_string parse_number_string(const char *p, const char *pend, chars_
151151
// of a 64-bit integer. However, this is uncommon.
152152
//
153153
// We can deal with up to 19 digits.
154-
if (((digit_count > 19))) { // this is uncommon
154+
if (digit_count > 19) { // this is uncommon
155155
// It is possible that the integer had an overflow.
156156
// We have to handle the case where we have 0.0000somenumber.
157157
// We need to be mindful of the case where we only have zeroes...
158158
// E.g., 0.000000000...000.
159159
const char *start = start_digits;
160160
while ((start != pend) && (*start == '0' || *start == '.')) {
161-
if(*start == '.') { digit_count++; } // We will subtract it again later.
161+
if(*start == '0') { digit_count --; }
162162
start++;
163163
}
164-
// We over-decrement by one when there is a decimal separator
165-
digit_count -= int(start - start_digits);
166164
if (digit_count > 19) {
167-
answer.mantissa = 0xFFFFFFFFFFFFFFFF; // important: we don't want the mantissa to be used in a fast path uninitialized.
168165
answer.too_many_digits = true;
169-
return answer;
166+
// Let us start again, this time, avoiding overflows.
167+
i = 0;
168+
p = start_digits;
169+
const uint64_t minimal_nineteen_digit_integer{1000000000000000000};
170+
while((i < minimal_nineteen_digit_integer) && (p != pend) && is_integer(*p)) {
171+
i = i * 10 + uint64_t(*p - '0');
172+
++p;
173+
}
174+
if (i >= minimal_nineteen_digit_integer) { // We have a big integers
175+
exponent = end_of_integer_part - p + exp_number;
176+
} else { // We have a value with a fractional component.
177+
p++; // skip the '.'
178+
const char *first_after_period = p;
179+
while((i < minimal_nineteen_digit_integer) && (p != pend) && is_integer(*p)) {
180+
i = i * 10 + uint64_t(*p - '0');
181+
++p;
182+
}
183+
exponent = first_after_period - p + exp_number;
184+
}
185+
// We have now corrected both exponent and i, to a truncated value
170186
}
171187
}
172-
answer.too_many_digits = false;
173188
answer.exponent = exponent;
174189
answer.mantissa = i;
175190
return answer;

include/fast_float/float_common.h

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ struct adjusted_mantissa {
184184
bool operator==(const adjusted_mantissa &o) const {
185185
return mantissa == o.mantissa && power2 == o.power2;
186186
}
187+
bool operator!=(const adjusted_mantissa &o) const {
188+
return mantissa != o.mantissa || power2 != o.power2;
189+
}
187190
};
188191

189192
struct decimal {
@@ -200,44 +203,6 @@ struct decimal {
200203
// Moves are allowed:
201204
decimal(decimal &&) = default;
202205
decimal &operator=(decimal &&other) = default;
203-
// Generates a mantissa by truncating to 19 digits.
204-
// This function should be reasonably fast.
205-
// Note that the user is responsible to ensure that digits are
206-
// initialized to zero when there are fewer than 19.
207-
inline uint64_t to_truncated_mantissa() {
208-
#if FASTFLOAT_IS_BIG_ENDIAN == 1
209-
uint64_t mantissa = 0;
210-
for (uint32_t i = 0; i < max_digit_without_overflow;
211-
i++) {
212-
mantissa = mantissa * 10 + digits[i]; // can be accelerated
213-
}
214-
return mantissa;
215-
#else
216-
uint64_t val;
217-
// 8 first digits
218-
::memcpy(&val, digits, sizeof(uint64_t));
219-
val = val * 2561 >> 8;
220-
val = (val & 0x00FF00FF00FF00FF) * 6553601 >> 16;
221-
uint64_t mantissa =
222-
uint32_t((val & 0x0000FFFF0000FFFF) * 42949672960001 >> 32);
223-
// 8 more digits for a total of 16
224-
::memcpy(&val, digits + sizeof(uint64_t), sizeof(uint64_t));
225-
val = val * 2561 >> 8;
226-
val = (val & 0x00FF00FF00FF00FF) * 6553601 >> 16;
227-
uint32_t eight_digits_value =
228-
uint32_t((val & 0x0000FFFF0000FFFF) * 42949672960001 >> 32);
229-
mantissa = 100000000 * mantissa + eight_digits_value;
230-
for (uint32_t i = 2 * sizeof(uint64_t); i < max_digit_without_overflow;
231-
i++) {
232-
mantissa = mantissa * 10 + digits[i]; // can be accelerated
233-
}
234-
return mantissa;
235-
#endif
236-
}
237-
// Generate an exponent matching to_truncated_mantissa()
238-
inline int32_t to_truncated_exponent() {
239-
return decimal_point - int32_t(max_digit_without_overflow);
240-
}
241206
};
242207

243208
constexpr static double powers_of_ten_double[] = {
@@ -372,4 +337,4 @@ inline OStream& operator<<(OStream &out, const fast_float::decimal &d) {
372337
return out;
373338
}
374339

375-
#endif
340+
#endif

include/fast_float/parse_number.h

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ from_chars_result parse_infnan(const char *first, const char *last, T &value) n
6666
answer.ptr = first;
6767
return answer;
6868
}
69+
70+
template<typename T>
71+
fastfloat_really_inline void to_float(bool negative, adjusted_mantissa am, T &value) {
72+
uint64_t word = am.mantissa;
73+
word |= uint64_t(am.power2) << binary_format<T>::mantissa_explicit_bits();
74+
word = negative
75+
? word | (uint64_t(1) << binary_format<T>::sign_index()) : word;
76+
#if FASTFLOAT_IS_BIG_ENDIAN == 1
77+
if (std::is_same<T, float>::value) {
78+
::memcpy(&value, (char *)&word + 4, sizeof(T)); // extract value at offset 4-7 if float on big-endian
79+
} else {
80+
::memcpy(&value, &word, sizeof(T));
81+
}
82+
#else
83+
// For little-endian systems:
84+
::memcpy(&value, &word, sizeof(T));
85+
#endif
86+
}
87+
6988
} // namespace
7089

7190

@@ -92,31 +111,23 @@ from_chars_result from_chars(const char *first, const char *last,
92111
answer.ec = std::errc(); // be optimistic
93112
answer.ptr = pns.lastmatch;
94113
// Next is Clinger's fast path.
95-
if (binary_format<T>::min_exponent_fast_path() <= pns.exponent && pns.exponent <= binary_format<T>::max_exponent_fast_path() && pns.mantissa <=binary_format<T>::max_mantissa_fast_path()) {
114+
if (binary_format<T>::min_exponent_fast_path() <= pns.exponent && pns.exponent <= binary_format<T>::max_exponent_fast_path() && pns.mantissa <=binary_format<T>::max_mantissa_fast_path() && !pns.too_many_digits) {
96115
value = T(pns.mantissa);
97116
if (pns.exponent < 0) { value = value / binary_format<T>::exact_power_of_ten(-pns.exponent); }
98117
else { value = value * binary_format<T>::exact_power_of_ten(pns.exponent); }
99118
if (pns.negative) { value = -value; }
100119
return answer;
101120
}
102-
adjusted_mantissa am = pns.too_many_digits ? parse_long_mantissa<binary_format<T>>(first,last) : compute_float<binary_format<T>>(pns.exponent, pns.mantissa);
121+
adjusted_mantissa am = compute_float<binary_format<T>>(pns.exponent, pns.mantissa);
122+
if(pns.too_many_digits) {
123+
if(am != compute_float<binary_format<T>>(pns.exponent, pns.mantissa + 1)) {
124+
am.power2 = -1; // value is invalid.
125+
}
126+
}
103127
// If we called compute_float<binary_format<T>>(pns.exponent, pns.mantissa) and we have an invalid power (am.power2 < 0),
104128
// then we need to go the long way around again. This is very uncommon.
105129
if(am.power2 < 0) { am = parse_long_mantissa<binary_format<T>>(first,last); }
106-
uint64_t word = am.mantissa;
107-
word |= uint64_t(am.power2) << binary_format<T>::mantissa_explicit_bits();
108-
word = pns.negative
109-
? word | (uint64_t(1) << binary_format<T>::sign_index()) : word;
110-
#if FASTFLOAT_IS_BIG_ENDIAN == 1
111-
if (std::is_same<T, float>::value) {
112-
::memcpy(&value, (char *)&word + 4, sizeof(T)); // extract value at offset 4-7 if float on big-endian
113-
} else {
114-
::memcpy(&value, &word, sizeof(T));
115-
}
116-
#else
117-
// For little-endian systems:
118-
::memcpy(&value, &word, sizeof(T));
119-
#endif
130+
to_float(pns.negative, am, value);
120131
return answer;
121132
}
122133

include/fast_float/simple_decimal_conversion.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,19 +353,6 @@ adjusted_mantissa compute_float(decimal &d) {
353353
template <typename binary>
354354
adjusted_mantissa parse_long_mantissa(const char *first, const char* last) {
355355
decimal d = parse_decimal(first, last);
356-
// In some cases we can get lucky and looking at only the first 19 digits is enough.
357-
// Let us try that.
358-
const uint64_t mantissa = d.to_truncated_mantissa();
359-
const int64_t exponent = d.to_truncated_exponent();
360-
// credit: R. Oudompheng who first implemented this fast path (to my knowledge).
361-
// It is rough, but it does the job of accelerating the slow path since most
362-
// long streams of digits are determined after 19 digits.
363-
// Note that mantissa+1 cannot overflow since mantissa < 10**19 and so
364-
// mantissa+1 <= 10**19 < 2**64.
365-
adjusted_mantissa am1 = compute_float<binary>(exponent, mantissa);
366-
adjusted_mantissa am2 = compute_float<binary>(exponent, mantissa+1);
367-
// They must both agree and be both a successful result.
368-
if(( am1 == am2 ) && (am1.power2 >= 0)) { return am1; }
369356
return compute_float<binary>(d);
370357
}
371358

0 commit comments

Comments
 (0)