Skip to content

Commit 67699bf

Browse files
committed
Migrate to fputil::cast
1 parent d4ff8e3 commit 67699bf

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,7 @@ add_entrypoint_object(
21832183
.expxf16
21842184
libc.hdr.errno_macros
21852185
libc.hdr.fenv_macros
2186+
libc.src.__support.FPUtil.cast
21862187
libc.src.__support.FPUtil.except_value_utils
21872188
libc.src.__support.FPUtil.fenv_impl
21882189
libc.src.__support.FPUtil.fp_bits

libc/src/math/generic/log10f16.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "src/__support/FPUtil/FEnvImpl.h"
1414
#include "src/__support/FPUtil/FPBits.h"
1515
#include "src/__support/FPUtil/PolyEval.h"
16+
#include "src/__support/FPUtil/cast.h"
1617
#include "src/__support/FPUtil/except_value_utils.h"
1718
#include "src/__support/FPUtil/multiply_add.h"
1819
#include "src/__support/common.h"
@@ -126,11 +127,11 @@ LLVM_LIBC_FUNCTION(float16, log10f16, (float16 x)) {
126127

127128
int m = -FPBits::EXP_BIAS;
128129

129-
// When x is subnormal.
130+
// When x is subnormal, normalize it.
130131
if ((x_u & FPBits::EXP_MASK) == 0U) {
131-
// Normalize x.
132-
x_bits = FPBits(x_bits.get_val() *
133-
static_cast<float16>((1U << FPBits::FRACTION_LEN)));
132+
// Can't pass an integer to fputil::cast directly.
133+
constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
134+
x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP));
134135
x_u = x_bits.uintval();
135136
m -= FPBits::FRACTION_LEN;
136137
}
@@ -156,7 +157,7 @@ LLVM_LIBC_FUNCTION(float16, log10f16, (float16 x)) {
156157
v * fputil::polyeval(v, 0x1.bcb7bp-2f, -0x1.bce168p-3f, 0x1.28acb8p-3f);
157158
// log10(1.mant) = log10(f) + log10(1 + d/f)
158159
float log10_1_mant = LOG10F_F[f] + log10p1_d_over_f;
159-
return static_cast<float16>(
160+
return fputil::cast<float16>(
160161
fputil::multiply_add(static_cast<float>(m), LOG10F_2, log10_1_mant));
161162
}
162163

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,6 +3642,7 @@ add_fp_unittest(
36423642
libc.hdr.fenv_macros
36433643
libc.src.errno.errno
36443644
libc.src.math.log10f16
3645+
libc.src.__support.FPUtil.cast
36453646
)
36463647

36473648
add_fp_unittest(

libc/test/src/math/smoke/log10f16_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "hdr/fenv_macros.h"
10+
#include "src/__support/FPUtil/cast.h"
1011
#include "src/errno/libc_errno.h"
1112
#include "src/math/log10f16.h"
1213
#include "test/UnitTest/FPMatcher.h"
@@ -38,10 +39,12 @@ TEST_F(LlvmLibcLog10f16Test, SpecialNumbers) {
3839
EXPECT_MATH_ERRNO(0);
3940

4041
EXPECT_FP_EQ_ALL_ROUNDING(
41-
zero, LIBC_NAMESPACE::log10f16(static_cast<float16>(1.0)));
42+
zero,
43+
LIBC_NAMESPACE::log10f16(LIBC_NAMESPACE::fputil::cast<float16>(1.0)));
4244
EXPECT_MATH_ERRNO(0);
4345

4446
EXPECT_FP_EQ_ALL_ROUNDING(
45-
aNaN, LIBC_NAMESPACE::log10f16(static_cast<float16>(-1.0)));
47+
aNaN,
48+
LIBC_NAMESPACE::log10f16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)));
4649
EXPECT_MATH_ERRNO(EDOM);
4750
}

0 commit comments

Comments
 (0)