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 17, 2024
1 parent 16f4c77 commit 0541aa4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 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 @@ -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
Expand Down
11 changes: 6 additions & 5 deletions libc/src/math/generic/logf16.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 @@ -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<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 @@ -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<float16>(
return fputil::cast<float16>(
fputil::multiply_add(static_cast<float>(m), LOGF_2, log_1_mant));
}

Expand Down
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 @@ -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(
Expand Down
10 changes: 6 additions & 4 deletions libc/test/src/math/smoke/logf16_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/logf16.h"
#include "test/UnitTest/FPMatcher.h"
Expand Down Expand Up @@ -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<float16>(1.0)));
EXPECT_FP_EQ_ALL_ROUNDING(
zero, LIBC_NAMESPACE::logf16(LIBC_NAMESPACE::fputil::cast<float16>(1.0)));
EXPECT_MATH_ERRNO(0);

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

0 comments on commit 0541aa4

Please sign in to comment.