Skip to content

Commit dad07cc

Browse files
committed
initialization cleanup and improve caches usage.
1 parent ebc2ee8 commit dad07cc

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

include/fast_float/ascii_number.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,20 @@ enum class parse_error : uint_fast8_t {
259259

260260
template <typename UC> struct parsed_number_string_t {
261261
am_mant_t mantissa{0};
262-
am_pow_t exponent{0};
263-
// contains the range of the significant digits
264-
span<UC const> integer{}; // non-nullable
265-
span<UC const> fraction{}; // nullable
266-
UC const *lastmatch{nullptr};
262+
267263
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
268264
bool negative{false};
269265
#endif
270266
bool invalid{false}; // be optimistic
271267
bool too_many_digits{false}; // be optimistic
272268
parse_error error{parse_error::no_error}; // be optimistic
269+
270+
am_pow_t exponent{0};
271+
272+
// contains the range of the significant digits
273+
span<UC const> integer; // non-nullable
274+
span<UC const> fraction; // nullable
275+
UC const *lastmatch;
273276
};
274277

275278
using byte_span = span<char const>;

include/fast_float/decimal_to_binary.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ compute_product_approximation(am_pow_t q, am_mant_t w) noexcept {
3131
constexpr uint64_t precision_mask =
3232
(bit_precision < 64) ? (uint64_t(0xFFFFFFFFFFFFFFFF) >> bit_precision)
3333
: uint64_t(0xFFFFFFFFFFFFFFFF);
34+
3435
if ((firstproduct.high & precision_mask) ==
3536
precision_mask) { // could further guard with (lower + w < lower)
3637
// regarding the second product, we only need secondproduct.high, but our
3738
// expectation is that the compiler will optimize this extra work away if
3839
// needed.
39-
value128 secondproduct =
40+
value128 const secondproduct =
4041
full_multiplication(w, powers::power_of_five_128[index + 1]);
4142
firstproduct.low += secondproduct.high;
43+
4244
if (secondproduct.high > firstproduct.low) {
4345
++firstproduct.high;
4446
}
@@ -62,7 +64,7 @@ namespace detail {
6264
* where
6365
* p = log(5**-q)/log(2) = -q * log(5)/log(2)
6466
*/
65-
constexpr fastfloat_really_inline am_pow_t power(am_pow_t q) noexcept {
67+
constexpr fastfloat_really_inline am_pow_t power(am_pow_t const q) noexcept {
6668
return (((152170 + 65536) * q) >> 16) + 63;
6769
}
6870
} // namespace detail
@@ -103,9 +105,9 @@ fastfloat_really_inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa
103105
compute_float(am_pow_t q, am_mant_t w) noexcept {
104106
adjusted_mantissa answer;
105107
if ((w == 0) || (q < binary::smallest_power_of_ten())) {
108+
// we want to get zero:
106109
answer.power2 = 0;
107110
answer.mantissa = 0;
108-
// result should be zero
109111
return answer;
110112
}
111113
if (q > binary::largest_power_of_ten()) {
@@ -114,6 +116,7 @@ compute_float(am_pow_t q, am_mant_t w) noexcept {
114116
answer.mantissa = 0;
115117
return answer;
116118
}
119+
117120
// At this point in time q is in [powers::smallest_power_of_five,
118121
// powers::largest_power_of_five].
119122

@@ -127,7 +130,7 @@ compute_float(am_pow_t q, am_mant_t w) noexcept {
127130
// 3. We might lose a bit due to the "upperbit" routine (result too small,
128131
// requiring a shift)
129132

130-
value128 product =
133+
value128 const product =
131134
compute_product_approximation<binary::mantissa_explicit_bits() + 3>(q, w);
132135
// The computed 'product' is always sufficient.
133136
// Mathematical proof:
@@ -141,6 +144,7 @@ compute_float(am_pow_t q, am_mant_t w) noexcept {
141144
auto const upperbit = static_cast<am_bits_t>(product.high >> 63);
142145
am_bits_t const shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3;
143146

147+
// Shift right the mantissa to the correct position
144148
answer.mantissa = product.high >> shift;
145149

146150
answer.power2 = detail::power(q) + upperbit - lz - binary::minimum_exponent();
@@ -192,13 +196,15 @@ compute_float(am_pow_t q, am_mant_t w) noexcept {
192196
}
193197
}
194198

199+
// Normal rounding
195200
answer.mantissa += (answer.mantissa & 1); // round up
196201
answer.mantissa >>= 1;
197202
if (answer.mantissa >= (am_mant_t(2) << binary::mantissa_explicit_bits())) {
198203
answer.mantissa = (am_mant_t(1) << binary::mantissa_explicit_bits());
199204
++answer.power2; // undo previous addition
200205
}
201206

207+
// Check if we have infinity after computation
202208
answer.mantissa &= ~(am_mant_t(1) << binary::mantissa_explicit_bits());
203209
if (answer.power2 >= binary::infinite_power()) { // infinity
204210
answer.power2 = binary::infinite_power();

include/fast_float/float_common.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ template <typename UC> struct parse_options_t {
110110

111111
/** Which number formats are accepted */
112112
chars_format format;
113-
/** The character used as decimal point for floats */
114-
UC decimal_point;
115113
/** The base used for integers */
116114
base_t base;
115+
/** The character used as decimal point for floats */
116+
UC decimal_point;
117117
};
118118

119119
using parse_options = parse_options_t<char>;
@@ -370,7 +370,7 @@ struct alignas(16) value128 {
370370
constexpr value128(uint64_t _low, uint64_t _high) noexcept
371371
: low(_low), high(_high) {}
372372

373-
constexpr value128() noexcept : low(0), high(0) {}
373+
constexpr value128() noexcept {}
374374
};
375375

376376
/* Helper C++14 constexpr generic implementation of leading_zeroes for 64-bit */
@@ -415,7 +415,7 @@ leading_zeroes(uint64_t input_num) noexcept {
415415
}
416416
#ifdef FASTFLOAT_VISUAL_STUDIO
417417
#if defined(_M_X64) || defined(_M_ARM64)
418-
unsigned long leading_zero = 0;
418+
unsigned long leading_zero;
419419
// Search the mask data from most significant bit (MSB)
420420
// to least significant bit (LSB) for a set bit (1).
421421
_BitScanReverse64(&leading_zero, input_num);
@@ -533,7 +533,7 @@ full_multiplication(uint64_t a, uint64_t b) noexcept {
533533
struct alignas(16) adjusted_mantissa {
534534
am_mant_t mantissa;
535535
am_pow_t power2;
536-
adjusted_mantissa() noexcept = default;
536+
adjusted_mantissa() noexcept {};
537537

538538
constexpr bool operator==(adjusted_mantissa const &o) const noexcept {
539539
return mantissa == o.mantissa && power2 == o.power2;

include/fast_float/parse_number.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ FASTFLOAT_CONSTEXPR20
417417
integer_times_pow10(am_sign_mant_t const mantissa,
418418
am_pow_t const decimal_exponent) noexcept {
419419
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
420-
const bool is_negative = mantissa < 0;
420+
const auto is_negative = mantissa < 0;
421421
const auto m = static_cast<am_mant_t>(is_negative ? -mantissa : mantissa);
422422
#else
423423
FASTFLOAT_ASSUME(mantissa >= 0);
@@ -431,8 +431,9 @@ FASTFLOAT_CONSTEXPR20
431431
value))
432432
return value;
433433

434-
adjusted_mantissa am =
434+
adjusted_mantissa const am =
435435
compute_float<binary_format<double>>(decimal_exponent, m);
436+
436437
to_float(
437438
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
438439
is_negative,

0 commit comments

Comments
 (0)