Skip to content

Commit fb1e92c

Browse files
committed
code cleanup and type usage fixes.
1 parent 2260580 commit fb1e92c

File tree

6 files changed

+92
-81
lines changed

6 files changed

+92
-81
lines changed

include/fast_float/ascii_number.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ parse_int_string(UC const *p, UC const *pend, T &value,
602602
((digits.as_int + 0x46464646u) | (digits.as_int - 0x30303030u)) &
603603
0x80808080u;
604604
const auto tz =
605-
static_cast<uint32_t>(countr_zero_32(magic)); // 7, 15, 23, 31, or 32
606-
uint32_t nd = (tz == 32) ? 4 : (tz >> 3);
607-
nd = std::min(static_cast<uint32_t>(nd), len);
605+
static_cast<am_digits>(countr_zero_32(magic)); // 7, 15, 23, 31, or 32
606+
am_digits nd = (tz == 32) ? 4 : (tz >> 3);
607+
nd = std::min(nd, len);
608608
if (nd == 0) {
609609
if (has_leading_zeros) {
610610
value = 0;
@@ -618,7 +618,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
618618
}
619619
if (nd > 3) {
620620
const UC *q = p + nd;
621-
uint_fast8_t rem = len - nd;
621+
am_digits rem = len - nd;
622622
while (rem) {
623623
if (*q < UC('0') || *q > UC('9'))
624624
break;

include/fast_float/bigint.h

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ constexpr limb_t bigint_limbs = bigint_bits / limb_bits;
4141
template <limb_t size> struct stackvec {
4242
limb data[size];
4343
// we never need more than 150 limbs
44-
uint_fast8_t length{0};
44+
limb_t length{0};
4545

4646
FASTFLOAT_CONSTEXPR20 stackvec() noexcept = default;
4747
stackvec(stackvec const &) = delete;
@@ -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(limb_t(len() + s.len()));
103+
set_len(static_cast<limb_t>(len() + s.len()));
104104
}
105105

106106
// try to add items to the vector, returning if items were added
@@ -123,17 +123,20 @@ template <limb_t size> struct stackvec {
123123
limb *first = data + len();
124124
limb *last = first + count;
125125
::std::fill(first, last, value);
126+
set_len(new_len);
127+
} else {
128+
set_len(new_len);
126129
}
127-
set_len(new_len);
128130
}
129131

130132
// try to resize the vector, returning if the vector was resized.
131133
FASTFLOAT_CONSTEXPR20 bool try_resize(limb_t new_len, limb value) noexcept {
132134
if (new_len > capacity()) {
133135
return false;
136+
} else {
137+
resize_unchecked(new_len, value);
138+
return true;
134139
}
135-
resize_unchecked(new_len, value);
136-
return true;
137140
}
138141

139142
// check if any limbs are non-zero after the given index.
@@ -259,9 +262,10 @@ inline FASTFLOAT_CONSTEXPR20 bool small_add_from(stackvec<size> &vec, limb y,
259262
limb_t start) noexcept {
260263
limb carry = y;
261264
bool overflow;
262-
while (carry != 0 && start++ != vec.len()) {
265+
while (carry != 0 && start < vec.len()) {
263266
vec[start] = scalar_add(vec[start], carry, overflow);
264267
carry = limb(overflow);
268+
++start;
265269
}
266270
if (carry != 0) {
267271
FASTFLOAT_TRY(vec.try_push(carry));
@@ -297,8 +301,8 @@ FASTFLOAT_CONSTEXPR20 bool large_add_from(stackvec<size> &x, limb_span y,
297301
limb_t start) noexcept {
298302
// the effective x buffer is from `xstart..x.len()`, so exit early
299303
// if we can't get that current range.
300-
if (x.len() < start || y.len() > limb_t(x.len() - start)) {
301-
FASTFLOAT_TRY(x.try_resize(limb_t(y.len() + start), 0));
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));
302306
}
303307

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

318322
// handle overflow
319323
if (carry) {
320-
FASTFLOAT_TRY(small_add_from(x, 1, limb_t(y.len() + start)));
324+
FASTFLOAT_TRY(small_add_from(x, 1, static_cast<limb_t>(y.len() + start)));
321325
}
322326
return true;
323327
}
@@ -369,7 +373,7 @@ FASTFLOAT_CONSTEXPR20 bool large_mul(stackvec<size> &x, limb_span y) noexcept {
369373
}
370374

371375
template <typename = void> struct pow5_tables {
372-
static constexpr uint_fast8_t large_step = 135;
376+
static constexpr limb_t large_step = 135;
373377
static constexpr uint64_t small_power_of_5[] = {
374378
1UL,
375379
5UL,
@@ -413,7 +417,7 @@ template <typename = void> struct pow5_tables {
413417

414418
#if FASTFLOAT_DETAIL_MUST_DEFINE_CONSTEXPR_VARIABLE
415419

416-
template <typename T> constexpr uint_fast8_t pow5_tables<T>::large_step;
420+
template <typename T> constexpr limb_t pow5_tables<T>::large_step;
417421

418422
template <typename T> constexpr uint64_t pow5_tables<T>::small_power_of_5[];
419423

@@ -487,7 +491,7 @@ struct bigint : pow5_tables<> {
487491
} else if (vec.len() < other.vec.len()) {
488492
return -1;
489493
} else {
490-
for (limb_t index = vec.len(); index != 0; --index) {
494+
for (limb_t index = vec.len(); index > 0; --index) {
491495
limb xi = vec[index - 1];
492496
limb yi = other.vec[index - 1];
493497
if (xi > yi) {
@@ -502,7 +506,7 @@ struct bigint : pow5_tables<> {
502506

503507
// shift left each limb n bits, carrying over to the new limb
504508
// returns true if we were able to shift all the digits.
505-
FASTFLOAT_CONSTEXPR20 bool shl_bits(bigint_bits_t n) noexcept {
509+
FASTFLOAT_CONSTEXPR20 bool shl_bits(limb_t n) noexcept {
506510
// Internally, for each item, we shift left by n, and add the previous
507511
// right shifted limb-bits.
508512
// For example, we transform (for u8) shifted left 2, to:
@@ -511,10 +515,10 @@ struct bigint : pow5_tables<> {
511515
FASTFLOAT_DEBUG_ASSERT(n != 0);
512516
FASTFLOAT_DEBUG_ASSERT(n < sizeof(limb) * 8);
513517

514-
bigint_bits_t const shl = n;
515-
bigint_bits_t const shr = limb_bits - shl;
518+
limb_t const shl = n;
519+
limb_t const shr = limb_bits - shl;
516520
limb prev = 0;
517-
for (limb_t index = 0; index != vec.len(); ++index) {
521+
for (limb_t index = 0; index < vec.len(); ++index) {
518522
limb xi = vec[index];
519523
vec[index] = (xi << shl) | (prev >> shr);
520524
prev = xi;
@@ -528,13 +532,12 @@ struct bigint : pow5_tables<> {
528532
}
529533

530534
// move the limbs left by `n` limbs.
531-
FASTFLOAT_CONSTEXPR20 bool shl_limbs(bigint_bits_t n) noexcept {
535+
FASTFLOAT_CONSTEXPR20 bool shl_limbs(limb_t n) noexcept {
532536
FASTFLOAT_DEBUG_ASSERT(n != 0);
533537
if (n + vec.len() > vec.capacity()) {
534538
// we can't shift more than the capacity of the vector.
535539
return false;
536-
}
537-
if (!vec.is_empty()) {
540+
} else if (!vec.is_empty()) {
538541
// move limbs
539542
limb *dst = vec.data + n;
540543
limb const *src = vec.data;
@@ -543,15 +546,17 @@ struct bigint : pow5_tables<> {
543546
limb *first = vec.data;
544547
limb *last = first + n;
545548
::std::fill(first, last, 0);
546-
vec.set_len(limb_t(n + vec.len()));
549+
vec.set_len(n + vec.len());
550+
return true;
551+
} else {
552+
return true;
547553
}
548-
return true;
549554
}
550555

551556
// move the limbs left by `n` bits.
552557
FASTFLOAT_CONSTEXPR20 bool shl(bigint_bits_t n) noexcept {
553-
bigint_bits_t const rem = n % limb_bits;
554-
bigint_bits_t const div = n / limb_bits;
558+
auto const rem = static_cast<limb_t>(n % limb_bits);
559+
auto const div = static_cast<limb_t>(n / limb_bits);
555560
if (rem != 0) {
556561
FASTFLOAT_TRY(shl_bits(rem));
557562
}
@@ -566,14 +571,15 @@ struct bigint : pow5_tables<> {
566571
if (vec.is_empty()) {
567572
// empty vector, no bits, no zeros.
568573
return 0;
569-
}
574+
} else {
570575
#ifdef FASTFLOAT_64BIT_LIMB
571-
return leading_zeroes(vec.rindex(0));
576+
return leading_zeroes(vec.rindex(0));
572577
#else
573-
// no use defining a specialized leading_zeroes for a 32-bit type.
574-
uint64_t r0 = vec.rindex(0);
575-
return leading_zeroes(r0 << 32);
578+
// no use defining a specialized leading_zeroes for a 32-bit type.
579+
uint64_t r0 = vec.rindex(0);
580+
return leading_zeroes(r0 << 32);
576581
#endif
582+
}
577583
}
578584

579585
// get the number of bits in the bigint.

include/fast_float/decimal_to_binary.h

Lines changed: 3 additions & 4 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(int64_t q, uint64_t w, limb_t lz) noexcept {
75-
limb_t const hilz = static_cast<limb_t>((w >> 63) ^ 1);
75+
auto const hilz = static_cast<limb_t>((w >> 63) ^ 1);
7676
adjusted_mantissa answer;
7777
answer.mantissa = w << hilz;
7878
constexpr am_pow_t bias =
@@ -139,9 +139,8 @@ compute_float(int64_t q, uint64_t w) noexcept {
139139
// branchless approach: value128 product = compute_product(q, w); but in
140140
// practice, we can win big with the compute_product_approximation if its
141141
// additional branch is easily predicted. Which is best is data specific.
142-
auto const upperbit = limb_t(product.high >> 63);
143-
auto const shift =
144-
limb_t(upperbit + 64 - binary::mantissa_explicit_bits() - 3);
142+
limb_t const upperbit = static_cast<limb_t>(product.high >> 63);
143+
limb_t const shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3;
145144

146145
answer.mantissa = product.high >> shift;
147146

include/fast_float/digit_comparison.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fastfloat_really_inline FASTFLOAT_CONSTEXPR14 void
139139
round_nearest_tie_even(adjusted_mantissa &am, am_pow_t shift,
140140
callback cb) noexcept {
141141
am_mant_t const mask =
142-
(shift == 64) ? UINT64_MAX : (am_mant_t(1) << shift) - 1;
142+
(shift == 64) ? std::numeric_limits<am_mant_t>::max() : (am_mant_t(1) << shift) - 1;
143143
am_mant_t const halfway = (shift == 0) ? 0 : am_mant_t(1) << (shift - 1);
144144
am_mant_t truncated_bits = am.mantissa & mask;
145145
bool is_above = truncated_bits > halfway;

0 commit comments

Comments
 (0)