Skip to content

Commit cbc95e7

Browse files
committed
Migrate to fputil::cast
1 parent 871029c commit cbc95e7

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,7 @@ add_entrypoint_object(
22802280
.expxf16
22812281
libc.hdr.errno_macros
22822282
libc.hdr.fenv_macros
2283+
libc.src.__support.FPUtil.cast
22832284
libc.src.__support.FPUtil.except_value_utils
22842285
libc.src.__support.FPUtil.fenv_impl
22852286
libc.src.__support.FPUtil.fp_bits

libc/src/math/generic/logf16.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"
@@ -119,11 +120,11 @@ LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) {
119120

120121
int m = -FPBits::EXP_BIAS;
121122

122-
// When x is subnormal.
123+
// When x is subnormal, normalize it.
123124
if ((x_u & FPBits::EXP_MASK) == 0U) {
124-
// Normalize x.
125-
x_bits = FPBits(x_bits.get_val() *
126-
static_cast<float16>((1U << FPBits::FRACTION_LEN)));
125+
// Can't pass an integer to fputil::cast directly.
126+
constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
127+
x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP));
127128
x_u = x_bits.uintval();
128129
m -= FPBits::FRACTION_LEN;
129130
}
@@ -149,7 +150,7 @@ LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) {
149150
v * fputil::polyeval(v, 0x1p+0f, -0x1.001804p-1f, 0x1.557ef6p-2f);
150151
// log(1.mant) = log(f) + log(1 + d/f)
151152
float log_1_mant = LOGF_F[f] + log1p_d_over_f;
152-
return static_cast<float16>(
153+
return fputil::cast<float16>(
153154
fputil::multiply_add(static_cast<float>(m), LOGF_2, log_1_mant));
154155
}
155156

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,6 +3557,7 @@ add_fp_unittest(
35573557
libc.hdr.fenv_macros
35583558
libc.src.errno.errno
35593559
libc.src.math.logf16
3560+
libc.src.__support.FPUtil.cast
35603561
)
35613562

35623563
add_fp_unittest(

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

Lines changed: 6 additions & 4 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/logf16.h"
1213
#include "test/UnitTest/FPMatcher.h"
@@ -37,11 +38,12 @@ TEST_F(LlvmLibcLogf16Test, SpecialNumbers) {
3738
neg_inf, LIBC_NAMESPACE::logf16(neg_zero), FE_DIVBYZERO);
3839
EXPECT_MATH_ERRNO(0);
3940

40-
EXPECT_FP_EQ_ALL_ROUNDING(zero,
41-
LIBC_NAMESPACE::logf16(static_cast<float16>(1.0)));
41+
EXPECT_FP_EQ_ALL_ROUNDING(
42+
zero, LIBC_NAMESPACE::logf16(LIBC_NAMESPACE::fputil::cast<float16>(1.0)));
4243
EXPECT_MATH_ERRNO(0);
4344

44-
EXPECT_FP_EQ_ALL_ROUNDING(aNaN,
45-
LIBC_NAMESPACE::logf16(static_cast<float16>(-1.0)));
45+
EXPECT_FP_EQ_ALL_ROUNDING(
46+
aNaN,
47+
LIBC_NAMESPACE::logf16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)));
4648
EXPECT_MATH_ERRNO(EDOM);
4749
}

0 commit comments

Comments
 (0)