Skip to content

Commit 054004f

Browse files
committed
cleanup and type usage fixes.
1 parent 37152ea commit 054004f

File tree

6 files changed

+28
-41
lines changed

6 files changed

+28
-41
lines changed

include/fast_float/ascii_number.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,6 @@ template <bool basic_json_fmt, typename UC>
291291
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 parsed_number_string_t<UC>
292292
parse_number_string(UC const *p, UC const *pend,
293293
parse_options_t<UC> const options) noexcept {
294-
// Cyclomatic complexity https://en.wikipedia.org/wiki/Cyclomatic_complexity
295-
// Consider refactoring the 'parse_number_string' function.
296-
// FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN fix this.
297294
parsed_number_string_t<UC> answer;
298295
// so dereference without checks
299296
FASTFLOAT_ASSUME(p < pend);
@@ -460,12 +457,12 @@ parse_number_string(UC const *p, UC const *pend,
460457

461458
// Now we can check for errors.
462459

463-
// TODO: If we frequently had to deal with long strings of digits,
460+
// If we frequently had to deal with long strings of digits,
464461
// we could extend our code by using a 128-bit integer instead
465462
// of a 64-bit integer. However, this is uncommon.
466-
463+
//
467464
// We can deal with up to 19 digits.
468-
if (digit_count > 19) {
465+
if (digit_count > 19) { // this is uncommon
469466
// It is possible that the integer had an overflow.
470467
// We have to handle the case where we have 0.0000somenumber.
471468
// We need to be mindful of the case where we only have zeroes...
@@ -479,8 +476,7 @@ parse_number_string(UC const *p, UC const *pend,
479476
++start;
480477
}
481478

482-
// We have to check if we have a number with more than 19 significant
483-
// digits.
479+
// We have to check if number has more than 19 significant digits.
484480
if (digit_count > 19) {
485481
answer.too_many_digits = true;
486482
// Let us start again, this time, avoiding overflows.
@@ -492,8 +488,8 @@ parse_number_string(UC const *p, UC const *pend,
492488
constexpr am_mant_t minimal_nineteen_digit_integer{1000000000000000000};
493489
while ((p != int_end) &&
494490
(answer.mantissa < minimal_nineteen_digit_integer)) {
495-
answer.mantissa = static_cast<am_mant_t>(
496-
answer.mantissa * 10 + static_cast<am_mant_t>(*p - UC('0')));
491+
answer.mantissa =
492+
answer.mantissa * 10 + static_cast<am_mant_t>(*p - UC('0'));
497493
++p;
498494
}
499495
if (answer.mantissa >= minimal_nineteen_digit_integer) {

include/fast_float/bigint.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ template <limb_t size> struct stackvec {
6767
// index from the end of the container
6868
FASTFLOAT_CONSTEXPR14 const limb &rindex(limb_t index) const noexcept {
6969
FASTFLOAT_DEBUG_ASSERT(index < length);
70-
limb_t rindex = static_cast<limb_t>(length - index - 1);
70+
auto rindex = length - index - 1;
7171
return data[rindex];
7272
}
7373

@@ -100,7 +100,7 @@ template <limb_t size> struct stackvec {
100100
FASTFLOAT_CONSTEXPR20 void extend_unchecked(limb_span s) noexcept {
101101
limb *ptr = data + length;
102102
std::copy_n(s.ptr, s.len(), ptr);
103-
set_len(static_cast<limb_t>(len() + s.len()));
103+
set_len(len() + static_cast<limb_t>(s.len()));
104104
}
105105

106106
// try to add items to the vector, returning if items were added
@@ -119,7 +119,7 @@ template <limb_t size> struct stackvec {
119119
FASTFLOAT_CONSTEXPR20
120120
void resize_unchecked(limb_t new_len, limb value) noexcept {
121121
if (new_len > len()) {
122-
limb_t count = new_len - len();
122+
auto count = new_len - len();
123123
limb *first = data + len();
124124
limb *last = first + count;
125125
::std::fill(first, last, value);
@@ -258,9 +258,8 @@ scalar_mul(limb x, limb y, limb &carry) noexcept {
258258
// add scalar value to bigint starting from offset.
259259
// used in grade school multiplication
260260
template <limb_t size>
261-
inline FASTFLOAT_CONSTEXPR20 bool small_add_from(stackvec<size> &vec, limb y,
262-
limb_t start) noexcept {
263-
limb carry = y;
261+
inline FASTFLOAT_CONSTEXPR20 bool
262+
small_add_from(stackvec<size> &vec, limb carry, limb_t start) noexcept {
264263
bool overflow;
265264
while (carry != 0 && start < vec.len()) {
266265
vec[start] = scalar_add(vec[start], carry, overflow);
@@ -301,8 +300,9 @@ FASTFLOAT_CONSTEXPR20 bool large_add_from(stackvec<size> &x, limb_span y,
301300
limb_t start) noexcept {
302301
// the effective x buffer is from `xstart..x.len()`, so exit early
303302
// if we can't get that current range.
304-
if (x.len() < start || y.len() > static_cast<limb_t>(x.len() - start)) {
305-
FASTFLOAT_TRY(x.try_resize(static_cast<limb_t>(y.len() + start), 0));
303+
if (x.len() < start ||
304+
y.len() > static_cast<uint_fast16_t>(x.len() - start)) {
305+
FASTFLOAT_TRY(x.try_resize(static_cast<limb_t>(y.len()) + start, 0));
306306
}
307307

308308
bool carry = false;
@@ -321,7 +321,7 @@ FASTFLOAT_CONSTEXPR20 bool large_add_from(stackvec<size> &x, limb_span y,
321321

322322
// handle overflow
323323
if (carry) {
324-
FASTFLOAT_TRY(small_add_from(x, 1, static_cast<limb_t>(y.len() + start)));
324+
FASTFLOAT_TRY(small_add_from(x, 1, static_cast<limb_t>(y.len()) + start));
325325
}
326326
return true;
327327
}
@@ -343,7 +343,7 @@ FASTFLOAT_CONSTEXPR20 bool long_mul(stackvec<size> &x, limb_span y) noexcept {
343343
if (y.len() != 0) {
344344
limb y0 = y[0];
345345
FASTFLOAT_TRY(small_mul(x, y0));
346-
for (limb_t index = 1; index != y.len(); ++index) {
346+
for (limb_t index = 1; index < y.len(); ++index) {
347347
limb yi = y[index];
348348
stackvec<size> zi;
349349
if (yi != 0) {
@@ -584,8 +584,8 @@ struct bigint : pow5_tables<> {
584584

585585
// get the number of bits in the bigint.
586586
FASTFLOAT_CONSTEXPR20 bigint_bits_t bit_length() const noexcept {
587-
bigint_bits_t lz = ctlz();
588-
return static_cast<fast_float::bigint_bits_t>(limb_bits * vec.len() - lz);
587+
auto lz = ctlz();
588+
return limb_bits * vec.len() - lz;
589589
}
590590

591591
FASTFLOAT_CONSTEXPR20 bool mul(limb y) noexcept { return small_mul(vec, y); }

include/fast_float/constexpr_feature_detect.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,6 @@
7676
#define FASTFLOAT_INLINE_VARIABLE static constexpr
7777
#endif
7878

79-
#if defined(__cpp_lib_is_constant_evaluated) && \
80-
__cpp_lib_is_constant_evaluated >= 201811L
81-
#define FASTFLOAT_HAS_IS_CONSTANT_EVALUATED 1
82-
#define FASTFLOAT_CONSTEVAL consteval
83-
#else
84-
#define FASTFLOAT_HAS_IS_CONSTANT_EVALUATED 0
85-
#define FASTFLOAT_CONSTEVAL FASTFLOAT_CONSTEXPR14
86-
#endif
87-
8879
// Testing for relevant C++20 constexpr library features
8980
#if FASTFLOAT_HAS_IS_CONSTANT_EVALUATED && FASTFLOAT_HAS_BIT_CAST && \
9081
defined(__cpp_lib_constexpr_algorithms) && \

include/fast_float/decimal_to_binary.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ constexpr fastfloat_really_inline am_pow_t power(am_pow_t q) noexcept {
7272
template <typename binary>
7373
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 adjusted_mantissa
7474
compute_error_scaled(am_pow_t q, am_mant_t w, am_bits_t lz) noexcept {
75-
auto const hilz = static_cast<am_pow_t>((w >> 63) ^ 1);
75+
auto const hilz = static_cast<am_bits_t>((w >> 63) ^ 1);
7676
adjusted_mantissa answer;
7777
answer.mantissa = w << hilz;
7878
constexpr am_pow_t bias =
@@ -138,8 +138,8 @@ compute_float(am_pow_t q, am_mant_t w) noexcept {
138138
// branchless approach: value128 product = compute_product(q, w); but in
139139
// practice, we can win big with the compute_product_approximation if its
140140
// additional branch is easily predicted. Which is best is data specific.
141-
auto const upperbit = static_cast<limb_t>(product.high >> 63);
142-
limb_t const shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3;
141+
auto const upperbit = static_cast<am_bits_t>(product.high >> 63);
142+
am_bits_t const shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3;
143143

144144
answer.mantissa = product.high >> shift;
145145

include/fast_float/digit_comparison.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ to_extended(T const value) noexcept {
6868
constexpr am_pow_t bias = binary_format<T>::mantissa_explicit_bits() -
6969
binary_format<T>::minimum_exponent();
7070

71-
equiv_uint const bits = bit_cast<equiv_uint, T>(value);
71+
auto const bits = bit_cast<equiv_uint, T>(value);
7272

7373
if ((bits & exponent_mask) == 0) {
7474
// denormal

include/fast_float/float_common.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ typedef uint_fast8_t limb_t;
4949
typedef int_fast8_t am_bits_t;
5050

5151
// 16 bit signed integer is used for power to cover all double exponents.
52-
typedef int16_t am_pow_t;
52+
typedef int_fast16_t am_pow_t;
5353
// Power bias is signed for handling a denormal float
5454
// or an invalid mantissa.
5555
// Bias so we can get the real exponent with an invalid adjusted_mantissa.
5656
constexpr static am_pow_t invalid_am_bias =
57-
std::numeric_limits<am_pow_t>::min() + 1;
57+
std::numeric_limits<int16_t>::min() + 1;
5858
constexpr static am_pow_t am_bias_limit =
59-
(std::numeric_limits<am_pow_t>::max() / 8) - 1;
59+
(std::numeric_limits<int16_t>::max() / 16) - 1;
6060

6161
// Type for enum chars_format.
6262
typedef uint_fast8_t chars_format_t;
6363

64-
// Type for base.
64+
// Type for base, only allowed from 2 to 36.
6565
typedef uint_fast8_t base_t;
6666

6767
enum class chars_format : chars_format_t;
@@ -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 */
113+
/** The character used as decimal point for floats */
114114
UC decimal_point;
115115
/** The base used for integers */
116-
base_t base; /* only allowed from 2 to 36 */
116+
base_t base;
117117
};
118118

119119
using parse_options = parse_options_t<char>;

0 commit comments

Comments
 (0)