Skip to content

Commit 6d347fd

Browse files
authored
[libc][math][c23] Add log2f16 C23 math function (#106084)
Part of #95250.
1 parent 0afe6e4 commit 6d347fd

File tree

14 files changed

+326
-2
lines changed

14 files changed

+326
-2
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
567567
libc.src.math.llogbf16
568568
libc.src.math.llrintf16
569569
libc.src.math.llroundf16
570+
libc.src.math.log2f16
570571
libc.src.math.logbf16
571572
libc.src.math.logf16
572573
libc.src.math.lrintf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
660660
libc.src.math.llogbf16
661661
libc.src.math.llrintf16
662662
libc.src.math.llroundf16
663+
libc.src.math.log2f16
663664
libc.src.math.logbf16
664665
libc.src.math.logf16
665666
libc.src.math.lrintf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ Higher Math Functions
318318
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
319319
| log1p | |check| | |check| | | | | 7.12.6.14 | F.10.3.14 |
320320
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
321-
| log2 | |check| | |check| | | | | 7.12.6.15 | F.10.3.15 |
321+
| log2 | |check| | |check| | | |check| | | 7.12.6.15 | F.10.3.15 |
322322
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
323323
| log2p1 | | | | | | 7.12.6.16 | F.10.3.16 |
324324
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ def StdC : StandardSpec<"stdc"> {
648648

649649
FunctionSpec<"log2", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
650650
FunctionSpec<"log2f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
651+
GuardedFunctionSpec<"log2f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
651652

652653
FunctionSpec<"log", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
653654
FunctionSpec<"logf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ add_math_entrypoint_object(log1pf)
340340

341341
add_math_entrypoint_object(log2)
342342
add_math_entrypoint_object(log2f)
343+
add_math_entrypoint_object(log2f16)
343344

344345
add_math_entrypoint_object(log)
345346
add_math_entrypoint_object(logf)

libc/src/math/generic/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,6 +2250,28 @@ add_entrypoint_object(
22502250
-O3
22512251
)
22522252

2253+
add_entrypoint_object(
2254+
log2f16
2255+
SRCS
2256+
log2f16.cpp
2257+
HDRS
2258+
../log2f16.h
2259+
DEPENDS
2260+
.expxf16
2261+
libc.hdr.errno_macros
2262+
libc.hdr.fenv_macros
2263+
libc.src.__support.FPUtil.cast
2264+
libc.src.__support.FPUtil.except_value_utils
2265+
libc.src.__support.FPUtil.fenv_impl
2266+
libc.src.__support.FPUtil.fp_bits
2267+
libc.src.__support.FPUtil.multiply_add
2268+
libc.src.__support.FPUtil.polyeval
2269+
libc.src.__support.macros.optimization
2270+
libc.src.__support.macros.properties.cpu_features
2271+
COMPILE_OPTIONS
2272+
-O3
2273+
)
2274+
22532275
add_entrypoint_object(
22542276
log
22552277
SRCS

libc/src/math/generic/expxf16.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,20 @@ constexpr cpp::array<float, 32> LOGF_F = {
302302
0x1.41d8fep-1f, 0x1.4a4f86p-1f, 0x1.52a2d2p-1f, 0x1.5ad404p-1f,
303303
};
304304

305+
// Generated by Sollya with the following commands:
306+
// > display = hexadecimal;
307+
// > for i from 0 to 31 do print(round(log2(1 + i * 2^-5), SG, RN));
308+
constexpr cpp::array<float, 32> LOG2F_F = {
309+
0x0p+0f, 0x1.6bad38p-5f, 0x1.663f7p-4f, 0x1.08c588p-3f,
310+
0x1.5c01a4p-3f, 0x1.acf5e2p-3f, 0x1.fbc16cp-3f, 0x1.24407ap-2f,
311+
0x1.49a784p-2f, 0x1.6e221cp-2f, 0x1.91bba8p-2f, 0x1.b47ecp-2f,
312+
0x1.d6753ep-2f, 0x1.f7a856p-2f, 0x1.0c105p-1f, 0x1.1bf312p-1f,
313+
0x1.2b8034p-1f, 0x1.3abb4p-1f, 0x1.49a784p-1f, 0x1.584822p-1f,
314+
0x1.66a008p-1f, 0x1.74b1fep-1f, 0x1.82809ep-1f, 0x1.900e62p-1f,
315+
0x1.9d5dap-1f, 0x1.aa709p-1f, 0x1.b74948p-1f, 0x1.c3e9cap-1f,
316+
0x1.d053f6p-1f, 0x1.dc899ap-1f, 0x1.e88c6cp-1f, 0x1.f45e08p-1f,
317+
};
318+
305319
// Generated by Sollya with the following commands:
306320
// > display = hexadecimal;
307321
// > for i from 0 to 31 do print(round(1 / (1 + i * 2^-5), SG, RN));

libc/src/math/generic/log2f16.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//===-- Half-precision log2(x) function -----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/log2f16.h"
10+
#include "expxf16.h"
11+
#include "hdr/errno_macros.h"
12+
#include "hdr/fenv_macros.h"
13+
#include "src/__support/FPUtil/FEnvImpl.h"
14+
#include "src/__support/FPUtil/FPBits.h"
15+
#include "src/__support/FPUtil/PolyEval.h"
16+
#include "src/__support/FPUtil/cast.h"
17+
#include "src/__support/FPUtil/except_value_utils.h"
18+
#include "src/__support/FPUtil/multiply_add.h"
19+
#include "src/__support/common.h"
20+
#include "src/__support/macros/config.h"
21+
#include "src/__support/macros/optimization.h"
22+
#include "src/__support/macros/properties/cpu_features.h"
23+
24+
namespace LIBC_NAMESPACE_DECL {
25+
26+
#ifdef LIBC_TARGET_CPU_HAS_FMA
27+
static constexpr size_t N_LOG2F16_EXCEPTS = 2;
28+
#else
29+
static constexpr size_t N_LOG2F16_EXCEPTS = 9;
30+
#endif
31+
32+
static constexpr fputil::ExceptValues<float16, N_LOG2F16_EXCEPTS>
33+
LOG2F16_EXCEPTS = {{
34+
// (input, RZ output, RU offset, RD offset, RN offset)
35+
#ifndef LIBC_TARGET_CPU_HAS_FMA
36+
// x = 0x1.224p-1, log2f16(x) = -0x1.a34p-1 (RZ)
37+
{0x3889U, 0xba8dU, 0U, 1U, 0U},
38+
// x = 0x1.e34p-1, log2f16(x) = -0x1.558p-4 (RZ)
39+
{0x3b8dU, 0xad56U, 0U, 1U, 0U},
40+
#endif
41+
// x = 0x1.e8cp-1, log2f16(x) = -0x1.128p-4 (RZ)
42+
{0x3ba3U, 0xac4aU, 0U, 1U, 0U},
43+
#ifndef LIBC_TARGET_CPU_HAS_FMA
44+
// x = 0x1.f98p-1, log2f16(x) = -0x1.2ep-6 (RZ)
45+
{0x3be6U, 0xa4b8U, 0U, 1U, 0U},
46+
// x = 0x1.facp-1, log2f16(x) = -0x1.e7p-7 (RZ)
47+
{0x3bebU, 0xa39cU, 0U, 1U, 1U},
48+
#endif
49+
// x = 0x1.fb4p-1, log2f16(x) = -0x1.b88p-7 (RZ)
50+
{0x3bedU, 0xa2e2U, 0U, 1U, 1U},
51+
#ifndef LIBC_TARGET_CPU_HAS_FMA
52+
// x = 0x1.fecp-1, log2f16(x) = -0x1.cep-9 (RZ)
53+
{0x3bfbU, 0x9b38U, 0U, 1U, 1U},
54+
// x = 0x1.ffcp-1, log2f16(x) = -0x1.714p-11 (RZ)
55+
{0x3bffU, 0x91c5U, 0U, 1U, 1U},
56+
// x = 0x1.224p+0, log2f16(x) = 0x1.72cp-3 (RZ)
57+
{0x3c89U, 0x31cbU, 1U, 0U, 1U},
58+
#endif
59+
}};
60+
61+
LLVM_LIBC_FUNCTION(float16, log2f16, (float16 x)) {
62+
using FPBits = fputil::FPBits<float16>;
63+
FPBits x_bits(x);
64+
65+
uint16_t x_u = x_bits.uintval();
66+
67+
// If x <= 0, or x is 1, or x is +inf, or x is NaN.
68+
if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) {
69+
// log2(NaN) = NaN
70+
if (x_bits.is_nan()) {
71+
if (x_bits.is_signaling_nan()) {
72+
fputil::raise_except_if_required(FE_INVALID);
73+
return FPBits::quiet_nan().get_val();
74+
}
75+
76+
return x;
77+
}
78+
79+
// log2(+/-0) = −inf
80+
if ((x_u & 0x7fffU) == 0U) {
81+
fputil::raise_except_if_required(FE_DIVBYZERO);
82+
return FPBits::inf(Sign::NEG).get_val();
83+
}
84+
85+
if (x_u == 0x3c00U)
86+
return FPBits::zero().get_val();
87+
88+
// When x < 0.
89+
if (x_u > 0x8000U) {
90+
fputil::set_errno_if_required(EDOM);
91+
fputil::raise_except_if_required(FE_INVALID);
92+
return FPBits::quiet_nan().get_val();
93+
}
94+
95+
// log2(+inf) = +inf
96+
return FPBits::inf().get_val();
97+
}
98+
99+
if (auto r = LOG2F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
100+
return r.value();
101+
102+
// To compute log2(x), we perform the following range reduction:
103+
// x = 2^m * 1.mant,
104+
// log2(x) = m + log2(1.mant).
105+
// To compute log2(1.mant), let f be the highest 6 bits including the hidden
106+
// bit, and d be the difference (1.mant - f), i.e., the remaining 5 bits of
107+
// the mantissa, then:
108+
// log2(1.mant) = log2(f) + log2(1.mant / f)
109+
// = log2(f) + log2(1 + d/f)
110+
// since d/f is sufficiently small.
111+
// We store log2(f) and 1/f in the lookup tables LOG2F_F and ONE_OVER_F_F
112+
// respectively.
113+
114+
int m = -FPBits::EXP_BIAS;
115+
116+
// When x is subnormal, normalize it.
117+
if ((x_u & FPBits::EXP_MASK) == 0U) {
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));
121+
x_u = x_bits.uintval();
122+
m -= FPBits::FRACTION_LEN;
123+
}
124+
125+
uint16_t mant = x_bits.get_mantissa();
126+
// Leading 10 - 5 = 5 bits of the mantissa.
127+
int f = mant >> 5;
128+
// Unbiased exponent.
129+
m += x_u >> FPBits::FRACTION_LEN;
130+
131+
// Set bits to 1.mant instead of 2^m * 1.mant.
132+
x_bits.set_biased_exponent(FPBits::EXP_BIAS);
133+
float mant_f = x_bits.get_val();
134+
// v = 1.mant * 1/f - 1 = d/f
135+
float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f);
136+
137+
// Degree-3 minimax polynomial generated by Sollya with the following
138+
// commands:
139+
// > display = hexadecimal;
140+
// > P = fpminimax(log2(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
141+
// > x * P;
142+
float log2p1_d_over_f =
143+
v * fputil::polyeval(v, 0x1.715476p+0f, -0x1.71771ap-1f, 0x1.ecb38ep-2f);
144+
// log2(1.mant) = log2(f) + log2(1 + d/f)
145+
float log2_1_mant = LOG2F_F[f] + log2p1_d_over_f;
146+
return fputil::cast<float16>(static_cast<float>(m) + log2_1_mant);
147+
}
148+
149+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/generic/logf16.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) {
115115
// log(1.mant) = log(f) + log(1.mant / f)
116116
// = log(f) + log(1 + d/f)
117117
// since d/f is sufficiently small.
118-
// We store log(f) and 1/f in the lookup tables LOGF_F and ONE_OVER_F
118+
// We store log(f) and 1/f in the lookup tables LOGF_F and ONE_OVER_F_F
119119
// respectively.
120120

121121
int m = -FPBits::EXP_BIAS;

libc/src/math/log2f16.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for log2f16 -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_MATH_LOG2F16_H
10+
#define LLVM_LIBC_SRC_MATH_LOG2F16_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/properties/types.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
float16 log2f16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_LOG2F16_H

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,17 @@ add_fp_unittest(
18091809
libc.src.__support.FPUtil.fp_bits
18101810
)
18111811

1812+
add_fp_unittest(
1813+
log2f16_test
1814+
NEED_MPFR
1815+
SUITE
1816+
libc-math-unittests
1817+
SRCS
1818+
log2f16_test.cpp
1819+
DEPENDS
1820+
libc.src.math.log2f16
1821+
)
1822+
18121823
add_fp_unittest(
18131824
log10_test
18141825
NEED_MPFR

libc/test/src/math/log2f16_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- Exhaustive test for log2f16 ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/math/log2f16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcLog2f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
15+
16+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
17+
18+
// Range: [0, Inf];
19+
static constexpr uint16_t POS_START = 0x0000U;
20+
static constexpr uint16_t POS_STOP = 0x7c00U;
21+
22+
// Range: [-Inf, 0];
23+
static constexpr uint16_t NEG_START = 0x8000U;
24+
static constexpr uint16_t NEG_STOP = 0xfc00U;
25+
26+
TEST_F(LlvmLibcLog2f16Test, PositiveRange) {
27+
for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
28+
float16 x = FPBits(v).get_val();
29+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,
30+
LIBC_NAMESPACE::log2f16(x), 0.5);
31+
}
32+
}
33+
34+
TEST_F(LlvmLibcLog2f16Test, NegativeRange) {
35+
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
36+
float16 x = FPBits(v).get_val();
37+
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Log2, x,
38+
LIBC_NAMESPACE::log2f16(x), 0.5);
39+
}
40+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,6 +3595,19 @@ add_fp_unittest(
35953595
libc.src.__support.FPUtil.fp_bits
35963596
)
35973597

3598+
add_fp_unittest(
3599+
log2f16_test
3600+
SUITE
3601+
libc-math-smoke-tests
3602+
SRCS
3603+
log2f16_test.cpp
3604+
DEPENDS
3605+
libc.hdr.fenv_macros
3606+
libc.src.errno.errno
3607+
libc.src.math.log2f16
3608+
libc.src.__support.FPUtil.cast
3609+
)
3610+
35983611
add_fp_unittest(
35993612
log10_test
36003613
SUITE

0 commit comments

Comments
 (0)