diff --git a/include/fast_float/bigint.h b/include/fast_float/bigint.h index 45e7299..3040c50 100644 --- a/include/fast_float/bigint.h +++ b/include/fast_float/bigint.h @@ -535,19 +535,17 @@ struct bigint : pow5_tables<> { // we can't shift more than the capacity of the vector. return false; } - if (vec.is_empty()) { - // nothing to do - return true; + if (!vec.is_empty()) { + // move limbs + limb *dst = vec.data + n; + limb const *src = vec.data; + std::copy_backward(src, src + vec.len(), dst + vec.len()); + // fill in empty limbs + limb *first = vec.data; + limb *last = first + n; + ::std::fill(first, last, 0); + vec.set_len(limb_t(n + vec.len())); } - // move limbs - limb *dst = vec.data + n; - limb const *src = vec.data; - std::copy_backward(src, src + vec.len(), dst + vec.len()); - // fill in empty limbs - limb *first = vec.data; - limb *last = first + n; - ::std::fill(first, last, 0); - vec.set_len(limb_t(n + vec.len())); return true; } @@ -590,12 +588,14 @@ struct bigint : pow5_tables<> { FASTFLOAT_CONSTEXPR20 bool add(limb y) noexcept { return small_add(vec, y); } // multiply as if by 2 raised to a power. - FASTFLOAT_CONSTEXPR20 bool pow2(am_pow_t exp) noexcept { + FASTFLOAT_CONSTEXPR20 bool pow2(am_pow_t const exp) noexcept { + FASTFLOAT_ASSERT(exp >= 0); return shl(static_cast(exp)); } // multiply as if by 5 raised to a power. FASTFLOAT_CONSTEXPR20 bool pow5(am_pow_t exp) noexcept { + FASTFLOAT_ASSERT(exp >= 0); // multiply by a power of 5 limb_t const large_length = sizeof(large_power_of_5) / sizeof(limb); limb_span const large = limb_span(large_power_of_5, large_length); @@ -627,6 +627,7 @@ struct bigint : pow5_tables<> { // multiply as if by 10 raised to a power. FASTFLOAT_CONSTEXPR20 bool pow10(am_pow_t exp) noexcept { + FASTFLOAT_ASSERT(exp >= 0); FASTFLOAT_TRY(pow5(exp)); return pow2(exp); } diff --git a/include/fast_float/digit_comparison.h b/include/fast_float/digit_comparison.h index 2ccda2a..fa39efd 100644 --- a/include/fast_float/digit_comparison.h +++ b/include/fast_float/digit_comparison.h @@ -373,11 +373,9 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa positive_digit_comp( // we then need to scale by `2^(f- e)`, and then the two significant digits // are of the same magnitude. template -inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp( - bigint &bigmant, adjusted_mantissa am, am_pow_t const exponent) noexcept { - bigint &real_digits = bigmant; - am_pow_t const &real_exp = exponent; - +inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa +negative_digit_comp(bigint &real_digits, adjusted_mantissa am, + am_pow_t const real_exp) noexcept { // get the value of `b`, rounded down, and get a bigint representation of // b+h adjusted_mantissa am_b = am; @@ -391,12 +389,12 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp( false, #endif am_b, b); - adjusted_mantissa theor = to_extended_halfway(b); + adjusted_mantissa const theor = to_extended_halfway(b); bigint theor_digits(theor.mantissa); - am_pow_t theor_exp = theor.power2; + am_pow_t const theor_exp = theor.power2; // scale real digits and theor digits to be same power. - am_pow_t pow2_exp = theor_exp - real_exp; + am_pow_t const pow2_exp = theor_exp - real_exp; am_pow_t pow5_exp = -real_exp; if (pow5_exp != 0) { FASTFLOAT_ASSERT(theor_digits.pow5(pow5_exp)); @@ -408,7 +406,7 @@ inline FASTFLOAT_CONSTEXPR20 adjusted_mantissa negative_digit_comp( } // compare digits, and use it to direct rounding - int ord = real_digits.compare(theor_digits); + int const ord = real_digits.compare(theor_digits); round(am, [ord](adjusted_mantissa &a, am_pow_t shift) { round_nearest_tie_even( a, shift, [ord](bool is_odd, bool _, bool __) -> bool {