Skip to content

Commit

Permalink
Migrate to fputil::cast
Browse files Browse the repository at this point in the history
  • Loading branch information
overmighty committed Oct 15, 2024
1 parent bcfd2cf commit 4ee5be6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions libc/src/math/generic/log2f16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<float16>((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<float16>(NORMALIZE_EXP));
x_u = x_bits.uintval();
m -= FPBits::FRACTION_LEN;
}
Expand All @@ -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<float16>(static_cast<float>(m) + log2_1_mant);
return fputil::cast<float16>(static_cast<float>(m) + log2_1_mant);
}

} // namespace LIBC_NAMESPACE_DECL
1 change: 1 addition & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 6 additions & 3 deletions libc/test/src/math/smoke/log2f16_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<float16>(1.0)));
EXPECT_FP_EQ_ALL_ROUNDING(
zero,
LIBC_NAMESPACE::log2f16(LIBC_NAMESPACE::fputil::cast<float16>(1.0)));
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ_ALL_ROUNDING(
aNaN, LIBC_NAMESPACE::log2f16(static_cast<float16>(-1.0)));
aNaN,
LIBC_NAMESPACE::log2f16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)));
EXPECT_MATH_ERRNO(EDOM);
}

0 comments on commit 4ee5be6

Please sign in to comment.