From 4ee5be6f755ea9e36c44657cd5fa587e13b11ddc Mon Sep 17 00:00:00 2001 From: OverMighty Date: Tue, 15 Oct 2024 16:58:28 +0200 Subject: [PATCH] Migrate to fputil::cast --- libc/src/math/generic/CMakeLists.txt | 1 + libc/src/math/generic/log2f16.cpp | 11 ++++++----- libc/test/src/math/smoke/CMakeLists.txt | 1 + libc/test/src/math/smoke/log2f16_test.cpp | 9 ++++++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 0bde9ff12a4588..0fbe99405aaef1 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -2241,6 +2241,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/log2f16.cpp b/libc/src/math/generic/log2f16.cpp index d72bc99d939ead..af84a1f1322467 100644 --- a/libc/src/math/generic/log2f16.cpp +++ b/libc/src/math/generic/log2f16.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" @@ -112,11 +113,11 @@ LLVM_LIBC_FUNCTION(float16, log2f16, (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; } @@ -142,7 +143,7 @@ LLVM_LIBC_FUNCTION(float16, log2f16, (float16 x)) { v * fputil::polyeval(v, 0x1.715476p+0f, -0x1.71771ap-1f, 0x1.ecb38ep-2f); // log2(1.mant) = log2(f) + log2(1 + d/f) float log2_1_mant = LOG2F_F[f] + log2p1_d_over_f; - return static_cast(static_cast(m) + log2_1_mant); + return fputil::cast(static_cast(m) + log2_1_mant); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 66f442c6c4d407..4f1cf3d78f7de2 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -3594,6 +3594,7 @@ add_fp_unittest( libc.hdr.fenv_macros libc.src.errno.errno libc.src.math.log2f16 + libc.src.__support.FPUtil.cast ) add_fp_unittest( diff --git a/libc/test/src/math/smoke/log2f16_test.cpp b/libc/test/src/math/smoke/log2f16_test.cpp index 3e0dda69f805d0..6d98482aa44996 100644 --- a/libc/test/src/math/smoke/log2f16_test.cpp +++ b/libc/test/src/math/smoke/log2f16_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/log2f16.h" #include "test/UnitTest/FPMatcher.h" @@ -37,11 +38,13 @@ TEST_F(LlvmLibcLog2f16Test, SpecialNumbers) { neg_inf, LIBC_NAMESPACE::log2f16(neg_zero), FE_DIVBYZERO); EXPECT_MATH_ERRNO(0); - EXPECT_FP_EQ_ALL_ROUNDING(zero, - LIBC_NAMESPACE::log2f16(static_cast(1.0))); + EXPECT_FP_EQ_ALL_ROUNDING( + zero, + LIBC_NAMESPACE::log2f16(LIBC_NAMESPACE::fputil::cast(1.0))); EXPECT_MATH_ERRNO(0); EXPECT_FP_EQ_ALL_ROUNDING( - aNaN, LIBC_NAMESPACE::log2f16(static_cast(-1.0))); + aNaN, + LIBC_NAMESPACE::log2f16(LIBC_NAMESPACE::fputil::cast(-1.0))); EXPECT_MATH_ERRNO(EDOM); }