diff --git a/libc/fuzzing/stdlib/strtofloat_fuzz.cpp b/libc/fuzzing/stdlib/strtofloat_fuzz.cpp --- a/libc/fuzzing/stdlib/strtofloat_fuzz.cpp +++ b/libc/fuzzing/stdlib/strtofloat_fuzz.cpp @@ -27,18 +27,22 @@ // This function calculates the effective precision for a given float type and // exponent. Subnormals have a lower effective precision since they don't // necessarily use all of the bits of the mantissa. -template inline int effective_precision(int exponent) { - int full_precision = FloatProperties::MANTISSA_PRECISION; +template inline constexpr int effective_precision(int exponent) { + const int full_precision = FloatProperties::MANTISSA_PRECISION; // This is intended to be 0 when the exponent is the lowest normal and // increase as the exponent's magnitude increases. - int bits_below_normal = (-exponent) - (FloatProperties::EXPONENT_BIAS - 1); + const int bits_below_normal = + (-exponent) - (FloatProperties::EXPONENT_BIAS - 1); - // This comparison is optimized out by the compiler. - if (bits_below_normal >= 0 && bits_below_normal < full_precision - 1) { - // The precision should be the normal, full precision, minus the bits lost - // by this being a subnormal, minus one for the implicit leading one. - return full_precision - bits_below_normal - 1; + // The precision should be the normal, full precision, minus the bits lost + // by this being a subnormal, minus one for the implicit leading one. + const int bits_if_subnormal = full_precision - bits_below_normal - 1; + + // bits_if_subnormal must be at least 2 because that's the minimum MPFR + // precision. + if (bits_below_normal >= 0 && bits_if_subnormal >= 2) { + return bits_if_subnormal; } return full_precision; }