From 67699bf4c7578bda03dd481621bc68d947581d2f Mon Sep 17 00:00:00 2001 From: OverMighty Date: Tue, 15 Oct 2024 17:04:56 +0200 Subject: [PATCH] Migrate to fputil::cast --- libc/src/math/generic/CMakeLists.txt | 1 + libc/src/math/generic/log10f16.cpp | 11 ++++++----- libc/test/src/math/smoke/CMakeLists.txt | 1 + libc/test/src/math/smoke/log10f16_test.cpp | 7 +++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index eb7e83611fcd2e..00cad6f85b4b98 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -2183,6 +2183,7 @@ add_entrypoint_object( .expxf16 libc.hdr.errno_macros libc.hdr.fenv_macros + libc.src.__support.FPUtil.cast libc.src.__support.FPUtil.except_value_utils libc.src.__support.FPUtil.fenv_impl libc.src.__support.FPUtil.fp_bits diff --git a/libc/src/math/generic/log10f16.cpp b/libc/src/math/generic/log10f16.cpp index 7de54e75af4507..65e91f05c3d819 100644 --- a/libc/src/math/generic/log10f16.cpp +++ b/libc/src/math/generic/log10f16.cpp @@ -13,6 +13,7 @@ #include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/PolyEval.h" +#include "src/__support/FPUtil/cast.h" #include "src/__support/FPUtil/except_value_utils.h" #include "src/__support/FPUtil/multiply_add.h" #include "src/__support/common.h" @@ -126,11 +127,11 @@ LLVM_LIBC_FUNCTION(float16, log10f16, (float16 x)) { int m = -FPBits::EXP_BIAS; - // When x is subnormal. + // When x is subnormal, normalize it. if ((x_u & FPBits::EXP_MASK) == 0U) { - // Normalize x. - x_bits = FPBits(x_bits.get_val() * - static_cast((1U << FPBits::FRACTION_LEN))); + // Can't pass an integer to fputil::cast directly. + constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN; + x_bits = FPBits(x_bits.get_val() * fputil::cast(NORMALIZE_EXP)); x_u = x_bits.uintval(); m -= FPBits::FRACTION_LEN; } @@ -156,7 +157,7 @@ LLVM_LIBC_FUNCTION(float16, log10f16, (float16 x)) { v * fputil::polyeval(v, 0x1.bcb7bp-2f, -0x1.bce168p-3f, 0x1.28acb8p-3f); // log10(1.mant) = log10(f) + log10(1 + d/f) float log10_1_mant = LOG10F_F[f] + log10p1_d_over_f; - return static_cast( + return fputil::cast( fputil::multiply_add(static_cast(m), LOG10F_2, log10_1_mant)); } diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 14e55ce10cb2bf..d41041c9bb0a4a 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -3642,6 +3642,7 @@ add_fp_unittest( libc.hdr.fenv_macros libc.src.errno.errno libc.src.math.log10f16 + libc.src.__support.FPUtil.cast ) add_fp_unittest( diff --git a/libc/test/src/math/smoke/log10f16_test.cpp b/libc/test/src/math/smoke/log10f16_test.cpp index 1c434fc18a4be5..471e1989333268 100644 --- a/libc/test/src/math/smoke/log10f16_test.cpp +++ b/libc/test/src/math/smoke/log10f16_test.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "hdr/fenv_macros.h" +#include "src/__support/FPUtil/cast.h" #include "src/errno/libc_errno.h" #include "src/math/log10f16.h" #include "test/UnitTest/FPMatcher.h" @@ -38,10 +39,12 @@ TEST_F(LlvmLibcLog10f16Test, SpecialNumbers) { EXPECT_MATH_ERRNO(0); EXPECT_FP_EQ_ALL_ROUNDING( - zero, LIBC_NAMESPACE::log10f16(static_cast(1.0))); + zero, + LIBC_NAMESPACE::log10f16(LIBC_NAMESPACE::fputil::cast(1.0))); EXPECT_MATH_ERRNO(0); EXPECT_FP_EQ_ALL_ROUNDING( - aNaN, LIBC_NAMESPACE::log10f16(static_cast(-1.0))); + aNaN, + LIBC_NAMESPACE::log10f16(LIBC_NAMESPACE::fputil::cast(-1.0))); EXPECT_MATH_ERRNO(EDOM); }