@@ -41,7 +41,7 @@ constexpr limb_t bigint_limbs = bigint_bits / limb_bits;
4141template <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
371375template <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
418422template <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.
0 commit comments