Skip to content

Commit 4ee5be6

Browse files
committed
Migrate to fputil::cast
1 parent bcfd2cf commit 4ee5be6

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

libc/src/math/generic/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,7 @@ add_entrypoint_object(
22412241
.expxf16
22422242
libc.hdr.errno_macros
22432243
libc.hdr.fenv_macros
2244+
libc.src.__support.FPUtil.cast
22442245
libc.src.__support.FPUtil.except_value_utils
22452246
libc.src.__support.FPUtil.fenv_impl
22462247
libc.src.__support.FPUtil.fp_bits

libc/src/math/generic/log2f16.cpp

+6-5
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

+1
Original file line numberDiff line numberDiff line change
@@ -3594,6 +3594,7 @@ add_fp_unittest(
35943594
libc.hdr.fenv_macros
35953595
libc.src.errno.errno
35963596
libc.src.math.log2f16
3597+
libc.src.__support.FPUtil.cast
35973598
)
35983599

35993600
add_fp_unittest(

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

+6-3
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)