From 0541aa477433ea1ae5c2c081bfb50379fe6e1c6a Mon Sep 17 00:00:00 2001 From: OverMighty Date: Tue, 15 Oct 2024 16:52:25 +0200 Subject: [PATCH] Migrate to fputil::cast --- libc/src/math/generic/CMakeLists.txt | 1 + libc/src/math/generic/logf16.cpp | 11 ++++++----- libc/test/src/math/smoke/CMakeLists.txt | 1 + libc/test/src/math/smoke/logf16_test.cpp | 10 ++++++---- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 10f1fb8aafdadef..b95672bc3968782 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -2299,6 +2299,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/logf16.cpp b/libc/src/math/generic/logf16.cpp index 43be27cc68203f9..9d1af2baa26153a 100644 --- a/libc/src/math/generic/logf16.cpp +++ b/libc/src/math/generic/logf16.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" @@ -119,11 +120,11 @@ LLVM_LIBC_FUNCTION(float16, logf16, (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; } @@ -149,7 +150,7 @@ LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) { v * fputil::polyeval(v, 0x1p+0f, -0x1.001804p-1f, 0x1.557ef6p-2f); // log(1.mant) = log(f) + log(1 + d/f) float log_1_mant = LOGF_F[f] + log1p_d_over_f; - return static_cast( + return fputil::cast( fputil::multiply_add(static_cast(m), LOGF_2, log_1_mant)); } diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index a1ccd261f97abbf..a3cd671269caa12 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -3568,6 +3568,7 @@ add_fp_unittest( libc.hdr.fenv_macros libc.src.errno.errno libc.src.math.logf16 + libc.src.__support.FPUtil.cast ) add_fp_unittest( diff --git a/libc/test/src/math/smoke/logf16_test.cpp b/libc/test/src/math/smoke/logf16_test.cpp index e2f21003e8d81b8..c7232aa1c1e323c 100644 --- a/libc/test/src/math/smoke/logf16_test.cpp +++ b/libc/test/src/math/smoke/logf16_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/logf16.h" #include "test/UnitTest/FPMatcher.h" @@ -37,11 +38,12 @@ TEST_F(LlvmLibcLogf16Test, SpecialNumbers) { neg_inf, LIBC_NAMESPACE::logf16(neg_zero), FE_DIVBYZERO); EXPECT_MATH_ERRNO(0); - EXPECT_FP_EQ_ALL_ROUNDING(zero, - LIBC_NAMESPACE::logf16(static_cast(1.0))); + EXPECT_FP_EQ_ALL_ROUNDING( + zero, LIBC_NAMESPACE::logf16(LIBC_NAMESPACE::fputil::cast(1.0))); EXPECT_MATH_ERRNO(0); - EXPECT_FP_EQ_ALL_ROUNDING(aNaN, - LIBC_NAMESPACE::logf16(static_cast(-1.0))); + EXPECT_FP_EQ_ALL_ROUNDING( + aNaN, + LIBC_NAMESPACE::logf16(LIBC_NAMESPACE::fputil::cast(-1.0))); EXPECT_MATH_ERRNO(EDOM); }