Skip to content

Commit 65cf7af

Browse files
authored
[libc][math][c23] Add logf16 C23 math function (#106072)
Part of #95250.
1 parent 4c4b93d commit 65cf7af

File tree

14 files changed

+359
-1
lines changed

14 files changed

+359
-1
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
568568
libc.src.math.llrintf16
569569
libc.src.math.llroundf16
570570
libc.src.math.logbf16
571+
libc.src.math.logf16
571572
libc.src.math.lrintf16
572573
libc.src.math.lroundf16
573574
libc.src.math.modff16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
661661
libc.src.math.llrintf16
662662
libc.src.math.llroundf16
663663
libc.src.math.logbf16
664+
libc.src.math.logf16
664665
libc.src.math.lrintf16
665666
libc.src.math.lroundf16
666667
libc.src.math.modff16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Higher Math Functions
310310
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
311311
| lgamma | | | | | | 7.12.8.3 | F.10.5.3 |
312312
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
313-
| log | |check| | |check| | | | | 7.12.6.11 | F.10.3.11 |
313+
| log | |check| | |check| | | |check| | | 7.12.6.11 | F.10.3.11 |
314314
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
315315
| log10 | |check| | |check| | | | | 7.12.6.12 | F.10.3.12 |
316316
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

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

652652
FunctionSpec<"log", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
653653
FunctionSpec<"logf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
654+
GuardedFunctionSpec<"logf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
654655

655656
FunctionSpec<"logb", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
656657
FunctionSpec<"logbf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ add_math_entrypoint_object(log2f)
343343

344344
add_math_entrypoint_object(log)
345345
add_math_entrypoint_object(logf)
346+
add_math_entrypoint_object(logf16)
346347

347348
add_math_entrypoint_object(logb)
348349
add_math_entrypoint_object(logbf)

libc/src/math/generic/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,6 +2289,28 @@ add_entrypoint_object(
22892289
-O3
22902290
)
22912291

2292+
add_entrypoint_object(
2293+
logf16
2294+
SRCS
2295+
logf16.cpp
2296+
HDRS
2297+
../logf16.h
2298+
DEPENDS
2299+
.expxf16
2300+
libc.hdr.errno_macros
2301+
libc.hdr.fenv_macros
2302+
libc.src.__support.FPUtil.cast
2303+
libc.src.__support.FPUtil.except_value_utils
2304+
libc.src.__support.FPUtil.fenv_impl
2305+
libc.src.__support.FPUtil.fp_bits
2306+
libc.src.__support.FPUtil.multiply_add
2307+
libc.src.__support.FPUtil.polyeval
2308+
libc.src.__support.macros.optimization
2309+
libc.src.__support.macros.properties.cpu_features
2310+
COMPILE_OPTIONS
2311+
-O3
2312+
)
2313+
22922314
add_entrypoint_object(
22932315
logb
22942316
SRCS

libc/src/math/generic/expxf16.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,34 @@ template <bool IsSinh> LIBC_INLINE float16 eval_sinh_or_cosh(float16 x) {
288288
lo, half_p_odd * exp2_hi_mid_diff, half_p_even * exp2_hi_mid_sum));
289289
}
290290

291+
// Generated by Sollya with the following commands:
292+
// > display = hexadecimal;
293+
// > for i from 0 to 31 do print(round(log(1 + i * 2^-5), SG, RN));
294+
constexpr cpp::array<float, 32> LOGF_F = {
295+
0x0p+0f, 0x1.f829bp-6f, 0x1.f0a30cp-5f, 0x1.6f0d28p-4f,
296+
0x1.e27076p-4f, 0x1.29553p-3f, 0x1.5ff308p-3f, 0x1.9525aap-3f,
297+
0x1.c8ff7cp-3f, 0x1.fb9186p-3f, 0x1.1675cap-2f, 0x1.2e8e2cp-2f,
298+
0x1.4618bcp-2f, 0x1.5d1bdcp-2f, 0x1.739d8p-2f, 0x1.89a338p-2f,
299+
0x1.9f323ep-2f, 0x1.b44f78p-2f, 0x1.c8ff7cp-2f, 0x1.dd46ap-2f,
300+
0x1.f128f6p-2f, 0x1.02552ap-1f, 0x1.0be72ep-1f, 0x1.154c3ep-1f,
301+
0x1.1e85f6p-1f, 0x1.2795e2p-1f, 0x1.307d74p-1f, 0x1.393e0ep-1f,
302+
0x1.41d8fep-1f, 0x1.4a4f86p-1f, 0x1.52a2d2p-1f, 0x1.5ad404p-1f,
303+
};
304+
305+
// Generated by Sollya with the following commands:
306+
// > display = hexadecimal;
307+
// > for i from 0 to 31 do print(round(1 / (1 + i * 2^-5), SG, RN));
308+
constexpr cpp::array<float, 32> ONE_OVER_F_F = {
309+
0x1p+0f, 0x1.f07c2p-1f, 0x1.e1e1e2p-1f, 0x1.d41d42p-1f,
310+
0x1.c71c72p-1f, 0x1.bacf92p-1f, 0x1.af286cp-1f, 0x1.a41a42p-1f,
311+
0x1.99999ap-1f, 0x1.8f9c18p-1f, 0x1.861862p-1f, 0x1.7d05f4p-1f,
312+
0x1.745d18p-1f, 0x1.6c16c2p-1f, 0x1.642c86p-1f, 0x1.5c9882p-1f,
313+
0x1.555556p-1f, 0x1.4e5e0ap-1f, 0x1.47ae14p-1f, 0x1.414142p-1f,
314+
0x1.3b13b2p-1f, 0x1.3521dp-1f, 0x1.2f684cp-1f, 0x1.29e412p-1f,
315+
0x1.24924ap-1f, 0x1.1f7048p-1f, 0x1.1a7b96p-1f, 0x1.15b1e6p-1f,
316+
0x1.111112p-1f, 0x1.0c9714p-1f, 0x1.08421p-1f, 0x1.041042p-1f,
317+
};
318+
291319
} // namespace LIBC_NAMESPACE_DECL
292320

293321
#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H

libc/src/math/generic/logf16.cpp

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
//===-- Half-precision log(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/logf16.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_LOGF16_EXCEPTS = 5;
28+
#else
29+
static constexpr size_t N_LOGF16_EXCEPTS = 11;
30+
#endif
31+
32+
static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
33+
LOGF16_EXCEPTS = {{
34+
// (input, RZ output, RU offset, RD offset, RN offset)
35+
#ifndef LIBC_TARGET_CPU_HAS_FMA
36+
// x = 0x1.61cp-13, logf16(x) = -0x1.16p+3 (RZ)
37+
{0x0987U, 0xc858U, 0U, 1U, 0U},
38+
// x = 0x1.f2p-12, logf16(x) = -0x1.e98p+2 (RZ)
39+
{0x0fc8U, 0xc7a6U, 0U, 1U, 1U},
40+
#endif
41+
// x = 0x1.4d4p-9, logf16(x) = -0x1.7e4p+2 (RZ)
42+
{0x1935U, 0xc5f9U, 0U, 1U, 0U},
43+
// x = 0x1.5ep-8, logf16(x) = -0x1.4ecp+2 (RZ)
44+
{0x1d78U, 0xc53bU, 0U, 1U, 0U},
45+
#ifndef LIBC_TARGET_CPU_HAS_FMA
46+
// x = 0x1.fdp-1, logf16(x) = -0x1.81p-8 (RZ)
47+
{0x3bf4U, 0x9e04U, 0U, 1U, 1U},
48+
// x = 0x1.fep-1, logf16(x) = -0x1.008p-8 (RZ)
49+
{0x3bf8U, 0x9c02U, 0U, 1U, 0U},
50+
#endif
51+
// x = 0x1.ffp-1, logf16(x) = -0x1.004p-9 (RZ)
52+
{0x3bfcU, 0x9801U, 0U, 1U, 0U},
53+
// x = 0x1.ff8p-1, logf16(x) = -0x1p-10 (RZ)
54+
{0x3bfeU, 0x9400U, 0U, 1U, 1U},
55+
#ifdef LIBC_TARGET_CPU_HAS_FMA
56+
// x = 0x1.4c4p+1, logf16(x) = 0x1.e84p-1 (RZ)
57+
{0x4131U, 0x3ba1U, 1U, 0U, 1U},
58+
#else
59+
// x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
60+
{0x45d4U, 0x3f0dU, 1U, 0U, 0U},
61+
// x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
62+
{0x45d4U, 0x3f0dU, 1U, 0U, 0U},
63+
// x = 0x1.d5p+9, logf16(x) = 0x1.b5cp+2 (RZ)
64+
{0x6354U, 0x46d7U, 1U, 0U, 1U},
65+
#endif
66+
}};
67+
68+
LLVM_LIBC_FUNCTION(float16, logf16, (float16 x)) {
69+
using FPBits = fputil::FPBits<float16>;
70+
FPBits x_bits(x);
71+
72+
uint16_t x_u = x_bits.uintval();
73+
74+
// If x <= 0, or x is 1, or x is +inf, or x is NaN.
75+
if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) {
76+
// log(NaN) = NaN
77+
if (x_bits.is_nan()) {
78+
if (x_bits.is_signaling_nan()) {
79+
fputil::raise_except_if_required(FE_INVALID);
80+
return FPBits::quiet_nan().get_val();
81+
}
82+
83+
return x;
84+
}
85+
86+
// log(+/-0) = −inf
87+
if ((x_u & 0x7fffU) == 0U) {
88+
fputil::raise_except_if_required(FE_DIVBYZERO);
89+
return FPBits::inf(Sign::NEG).get_val();
90+
}
91+
92+
if (x_u == 0x3c00U)
93+
return FPBits::zero().get_val();
94+
95+
// When x < 0.
96+
if (x_u > 0x8000U) {
97+
fputil::set_errno_if_required(EDOM);
98+
fputil::raise_except_if_required(FE_INVALID);
99+
return FPBits::quiet_nan().get_val();
100+
}
101+
102+
// log(+inf) = +inf
103+
return FPBits::inf().get_val();
104+
}
105+
106+
if (auto r = LOGF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
107+
return r.value();
108+
109+
// To compute log(x), we perform the following range reduction:
110+
// x = 2^m * 1.mant,
111+
// log(x) = m * log(2) + log(1.mant).
112+
// To compute log(1.mant), let f be the highest 6 bits including the hidden
113+
// bit, and d be the difference (1.mant - f), i.e., the remaining 5 bits of
114+
// the mantissa, then:
115+
// log(1.mant) = log(f) + log(1.mant / f)
116+
// = log(f) + log(1 + d/f)
117+
// since d/f is sufficiently small.
118+
// We store log(f) and 1/f in the lookup tables LOGF_F and ONE_OVER_F
119+
// respectively.
120+
121+
int m = -FPBits::EXP_BIAS;
122+
123+
// When x is subnormal, normalize it.
124+
if ((x_u & FPBits::EXP_MASK) == 0U) {
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));
128+
x_u = x_bits.uintval();
129+
m -= FPBits::FRACTION_LEN;
130+
}
131+
132+
uint16_t mant = x_bits.get_mantissa();
133+
// Leading 10 - 5 = 5 bits of the mantissa.
134+
int f = mant >> 5;
135+
// Unbiased exponent.
136+
m += x_u >> FPBits::FRACTION_LEN;
137+
138+
// Set bits to 1.mant instead of 2^m * 1.mant.
139+
x_bits.set_biased_exponent(FPBits::EXP_BIAS);
140+
float mant_f = x_bits.get_val();
141+
// v = 1.mant * 1/f - 1 = d/f
142+
float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f);
143+
144+
// Degree-3 minimax polynomial generated by Sollya with the following
145+
// commands:
146+
// > display = hexadecimal;
147+
// > P = fpminimax(log(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
148+
// > x * P;
149+
float log1p_d_over_f =
150+
v * fputil::polyeval(v, 0x1p+0f, -0x1.001804p-1f, 0x1.557ef6p-2f);
151+
// log(1.mant) = log(f) + log(1 + d/f)
152+
float log_1_mant = LOGF_F[f] + log1p_d_over_f;
153+
return fputil::cast<float16>(
154+
fputil::multiply_add(static_cast<float>(m), LOGF_2, log_1_mant));
155+
}
156+
157+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/logf16.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for logf16 ------------------------*- 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_LOGF16_H
10+
#define LLVM_LIBC_SRC_MATH_LOGF16_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 logf16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_LOGF16_H

libc/test/UnitTest/FPMatcher.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,17 @@ struct ModifyMXCSR {
400400
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_MODE( \
401401
(expected), (actual), (expected_except), RoundingMode::TowardZero)
402402

403+
#define EXPECT_FP_EQ_WITH_EXCEPTION_ALL_ROUNDING(expected, actual, \
404+
expected_except) \
405+
do { \
406+
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST((expected), (actual), \
407+
(expected_except)); \
408+
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD((expected), (actual), \
409+
(expected_except)); \
410+
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD((expected), (actual), \
411+
(expected_except)); \
412+
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO((expected), (actual), \
413+
(expected_except)); \
414+
} while (0)
415+
403416
#endif // LLVM_LIBC_TEST_UNITTEST_FPMATCHER_H

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,17 @@ add_fp_unittest(
17721772
libc.src.__support.FPUtil.fp_bits
17731773
)
17741774

1775+
add_fp_unittest(
1776+
logf16_test
1777+
NEED_MPFR
1778+
SUITE
1779+
libc-math-unittests
1780+
SRCS
1781+
logf16_test.cpp
1782+
DEPENDS
1783+
libc.src.math.logf16
1784+
)
1785+
17751786
add_fp_unittest(
17761787
log2_test
17771788
NEED_MPFR

libc/test/src/math/logf16_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- Exhaustive test for logf16 ----------------------------------------===//
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/logf16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcLogf16Test = 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(LlvmLibcLogf16Test, 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::Log, x,
30+
LIBC_NAMESPACE::logf16(x), 0.5);
31+
}
32+
}
33+
34+
TEST_F(LlvmLibcLogf16Test, 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::Log, x,
38+
LIBC_NAMESPACE::logf16(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
@@ -3558,6 +3558,19 @@ add_fp_unittest(
35583558
libc.src.__support.FPUtil.fp_bits
35593559
)
35603560

3561+
add_fp_unittest(
3562+
logf16_test
3563+
SUITE
3564+
libc-math-smoke-tests
3565+
SRCS
3566+
logf16_test.cpp
3567+
DEPENDS
3568+
libc.hdr.fenv_macros
3569+
libc.src.errno.errno
3570+
libc.src.math.logf16
3571+
libc.src.__support.FPUtil.cast
3572+
)
3573+
35613574
add_fp_unittest(
35623575
log2_test
35633576
SUITE

0 commit comments

Comments
 (0)