Skip to content

Commit 6de1bd9

Browse files
committed
Migrate to fputil::cast
1 parent 98ce723 commit 6de1bd9

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

libc/src/math/generic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,7 @@ add_entrypoint_object(
22602260
.expxf16
22612261
libc.hdr.errno_macros
22622262
libc.hdr.fenv_macros
2263+
libc.src.__support.FPUtil.cast
22632264
libc.src.__support.FPUtil.except_value_utils
22642265
libc.src.__support.FPUtil.fenv_impl
22652266
libc.src.__support.FPUtil.fp_bits

libc/src/math/generic/log2f16.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"
@@ -112,11 +113,11 @@ LLVM_LIBC_FUNCTION(float16, log2f16, (float16 x)) {
112113

113114
int m = -FPBits::EXP_BIAS;
114115

115-
// When x is subnormal.
116+
// When x is subnormal, normalize it.
116117
if ((x_u & FPBits::EXP_MASK) == 0U) {
117-
// Normalize x.
118-
x_bits = FPBits(x_bits.get_val() *
119-
static_cast<float16>((1U << FPBits::FRACTION_LEN)));
118+
// Can't pass an integer to fputil::cast directly.
119+
constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
120+
x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP));
120121
x_u = x_bits.uintval();
121122
m -= FPBits::FRACTION_LEN;
122123
}
@@ -142,7 +143,7 @@ LLVM_LIBC_FUNCTION(float16, log2f16, (float16 x)) {
142143
v * fputil::polyeval(v, 0x1.715476p+0f, -0x1.71771ap-1f, 0x1.ecb38ep-2f);
143144
// log2(1.mant) = log2(f) + log2(1 + d/f)
144145
float log2_1_mant = LOG2F_F[f] + log2p1_d_over_f;
145-
return static_cast<float16>(static_cast<float>(m) + log2_1_mant);
146+
return fputil::cast<float16>(static_cast<float>(m) + log2_1_mant);
146147
}
147148

148149
} // namespace LIBC_NAMESPACE_DECL

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,6 +3605,7 @@ add_fp_unittest(
36053605
libc.hdr.fenv_macros
36063606
libc.src.errno.errno
36073607
libc.src.math.log2f16
3608+
libc.src.__support.FPUtil.cast
36083609
)
36093610

36103611
add_fp_unittest(

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

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

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

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

0 commit comments

Comments
 (0)