diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h --- a/libc/src/__support/str_to_float.h +++ b/libc/src/__support/str_to_float.h @@ -78,6 +78,8 @@ } #endif +#ifndef LIBC_SIZE + // This Eisel-Lemire implementation is based on the algorithm described in the // paper Number Parsing at a Gigabyte per Second, Software: Practice and // Experience 51 (8), 2021 (https://arxiv.org/abs/2101.11408), as well as the @@ -110,8 +112,9 @@ uint32_t clz = leading_zeroes(mantissa); mantissa <<= clz; - uint32_t exp2 = static_cast(exp10_to_exp2(exp10)) + BITS_IN_MANTISSA + - fputil::FloatProperties::EXPONENT_BIAS - clz; + uint32_t exp2 = static_cast(exp10_to_exp2(exp10)) + + BITS_IN_MANTISSA + fputil::FloatProperties::EXPONENT_BIAS - + clz; // Multiplication const uint64_t *power_of_ten = @@ -149,8 +152,10 @@ } // Shifting to 54 bits for doubles and 25 bits for floats - BitsType msb = static_cast(high64(final_approx) >> (BITS_IN_MANTISSA - 1)); - BitsType final_mantissa = static_cast(high64(final_approx) >> + BitsType msb = + static_cast(high64(final_approx) >> (BITS_IN_MANTISSA - 1)); + BitsType final_mantissa = + static_cast(high64(final_approx) >> (msb + BITS_IN_MANTISSA - (fputil::FloatProperties::MANTISSA_WIDTH + 3))); exp2 -= static_cast(1 ^ msb); // same as !msb @@ -210,7 +215,8 @@ uint32_t clz = leading_zeroes(mantissa); mantissa <<= clz; - uint32_t exp2 = static_cast(exp10_to_exp2(exp10)) + BITS_IN_MANTISSA + + uint32_t exp2 = static_cast(exp10_to_exp2(exp10)) + + BITS_IN_MANTISSA + fputil::FloatProperties::EXPONENT_BIAS - clz; // Multiplication @@ -290,6 +296,8 @@ } #endif +#endif // LIBC_SIZE + // The nth item in POWERS_OF_TWO represents the greatest power of two less than // 10^n. This tells us how much we can safely shift without overshooting. constexpr uint8_t POWERS_OF_TWO[19] = { @@ -428,6 +436,7 @@ *outputExp2 = exp2; } +#ifndef LIBC_SIZE // This class is used for templating the constants for Clinger's Fast Path, // described as a method of approximation in // Clinger WD. How to Read Floating Point Numbers Accurately. SIGPLAN Not 1990 @@ -543,6 +552,7 @@ *outputExp2 = result.get_unbiased_exponent(); return true; } +#endif // LIBC_SIZE // Takes a mantissa and base 10 exponent and converts it into its closest // floating point type T equivalient. First we try the Eisel-Lemire algorithm, @@ -578,6 +588,7 @@ return; } +#ifndef LIBC_SIZE if (!truncated) { if (clinger_fast_path(mantissa, exp10, outputMantissa, outputExp2)) { return; @@ -600,6 +611,7 @@ } } } +#endif // LIBC_SIZE simple_decimal_conversion(numStart, outputMantissa, outputExp2); diff --git a/libc/test/src/__support/str_to_float_test.cpp b/libc/test/src/__support/str_to_float_test.cpp --- a/libc/test/src/__support/str_to_float_test.cpp +++ b/libc/test/src/__support/str_to_float_test.cpp @@ -14,6 +14,7 @@ class LlvmLibcStrToFloatTest : public __llvm_libc::testing::Test { public: +#ifndef LIBC_SIZE template void clinger_fast_path_test( const typename __llvm_libc::fputil::FPBits::UIntType inputMantissa, @@ -62,7 +63,7 @@ EXPECT_EQ(actual_output_mantissa, expectedOutputMantissa); EXPECT_EQ(actual_output_exp2, expectedOutputExp2); } - +#endif // LIBC_SIZE template void simple_decimal_conversion_test( const char *__restrict numStart, @@ -120,6 +121,7 @@ EXPECT_EQ(__llvm_libc::internal::leading_zeroes(0xffffffff), 0u); } +#ifndef LIBC_SIZE TEST_F(LlvmLibcStrToFloatTest, ClingerFastPathFloat64Simple) { clinger_fast_path_test(123, 0, 0xEC00000000000, 1029); clinger_fast_path_test(1234567890123456, 1, 0x5ee2a2eb5a5c0, 1076); @@ -187,6 +189,7 @@ ASSERT_FALSE(__llvm_libc::internal::eisel_lemire( 20040229, 0, &float_output_mantissa, &output_exp2)); } +#endif // LIBC_SIZE TEST_F(LlvmLibcStrToFloatTest, SimpleDecimalConversion64BasicWholeNumbers) { simple_decimal_conversion_test("123456789012345678900", @@ -262,6 +265,8 @@ EXPECT_EQ(errno, 0); } +#ifndef LIBC_SIZE + #if defined(LONG_DOUBLE_IS_DOUBLE) TEST_F(LlvmLibcStrToFloatTest, EiselLemireFloat64AsLongDouble) { eisel_lemire_test(123, 0, 0x1EC00000000000, 1029); @@ -344,3 +349,4 @@ &quadOutputMantissa, &outputExp2)); } #endif +#endif // LIBC_SIZE