@@ -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
103105compute_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 ();
0 commit comments