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 18, 2024
1 parent ad3e07d commit b02dbce
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 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 @@ -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
Expand Down
11 changes: 6 additions & 5 deletions libc/src/math/generic/log10f16.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 @@ -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<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 @@ -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<float16>(
return fputil::cast<float16>(
fputil::multiply_add(static_cast<float>(m), LOG10F_2, log10_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 @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions libc/test/src/math/smoke/log10f16_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/log10f16.h"
#include "test/UnitTest/FPMatcher.h"
Expand Down Expand Up @@ -38,10 +39,12 @@ TEST_F(LlvmLibcLog10f16Test, SpecialNumbers) {
EXPECT_MATH_ERRNO(0);

EXPECT_FP_EQ_ALL_ROUNDING(
zero, LIBC_NAMESPACE::log10f16(static_cast<float16>(1.0)));
zero,
LIBC_NAMESPACE::log10f16(LIBC_NAMESPACE::fputil::cast<float16>(1.0)));
EXPECT_MATH_ERRNO(0);

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

0 comments on commit b02dbce

Please sign in to comment.