Skip to content

Commit 69d3a44

Browse files
authored
[libc][math][c23] Add log10f16 C23 math function (llvm#106091)
Part of llvm#95250.
1 parent 0138adb commit 69d3a44

File tree

13 files changed

+340
-1
lines changed

13 files changed

+340
-1
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.log10f16
570571
libc.src.math.log2f16
571572
libc.src.math.logbf16
572573
libc.src.math.logf16

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.log10f16
663664
libc.src.math.log2f16
664665
libc.src.math.logbf16
665666
libc.src.math.logf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Higher Math Functions
312312
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
313313
| log | |check| | |check| | | |check| | | 7.12.6.11 | F.10.3.11 |
314314
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
315-
| log10 | |check| | |check| | | | | 7.12.6.12 | F.10.3.12 |
315+
| log10 | |check| | |check| | | |check| | | 7.12.6.12 | F.10.3.12 |
316316
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
317317
| log10p1 | | | | | | 7.12.6.13 | F.10.3.13 |
318318
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

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

643643
FunctionSpec<"log10", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
644644
FunctionSpec<"log10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
645+
GuardedFunctionSpec<"log10f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
645646

646647
FunctionSpec<"log1p", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
647648
FunctionSpec<"log1pf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ add_math_entrypoint_object(ldexpf128)
334334

335335
add_math_entrypoint_object(log10)
336336
add_math_entrypoint_object(log10f)
337+
add_math_entrypoint_object(log10f16)
337338

338339
add_math_entrypoint_object(log1p)
339340
add_math_entrypoint_object(log1pf)

libc/src/math/generic/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,28 @@ add_entrypoint_object(
21732173
-O3
21742174
)
21752175

2176+
add_entrypoint_object(
2177+
log10f16
2178+
SRCS
2179+
log10f16.cpp
2180+
HDRS
2181+
../log10f16.h
2182+
DEPENDS
2183+
.expxf16
2184+
libc.hdr.errno_macros
2185+
libc.hdr.fenv_macros
2186+
libc.src.__support.FPUtil.cast
2187+
libc.src.__support.FPUtil.except_value_utils
2188+
libc.src.__support.FPUtil.fenv_impl
2189+
libc.src.__support.FPUtil.fp_bits
2190+
libc.src.__support.FPUtil.multiply_add
2191+
libc.src.__support.FPUtil.polyeval
2192+
libc.src.__support.macros.optimization
2193+
libc.src.__support.macros.properties.cpu_features
2194+
COMPILE_OPTIONS
2195+
-O3
2196+
)
2197+
21762198
add_entrypoint_object(
21772199
log1p
21782200
SRCS

libc/src/math/generic/expxf16.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,20 @@ constexpr cpp::array<float, 32> LOG2F_F = {
316316
0x1.d053f6p-1f, 0x1.dc899ap-1f, 0x1.e88c6cp-1f, 0x1.f45e08p-1f,
317317
};
318318

319+
// Generated by Sollya with the following commands:
320+
// > display = hexadecimal;
321+
// > for i from 0 to 31 do print(round(log10(1 + i * 2^-5), SG, RN));
322+
constexpr cpp::array<float, 32> LOG10F_F = {
323+
0x0p+0f, 0x1.b5e908p-7f, 0x1.af5f92p-6f, 0x1.3ed11ap-5f,
324+
0x1.a30a9ep-5f, 0x1.02428cp-4f, 0x1.31b306p-4f, 0x1.5fe804p-4f,
325+
0x1.8cf184p-4f, 0x1.b8de4ep-4f, 0x1.e3bc1ap-4f, 0x1.06cbd6p-3f,
326+
0x1.1b3e72p-3f, 0x1.2f3b6ap-3f, 0x1.42c7e8p-3f, 0x1.55e8c6p-3f,
327+
0x1.68a288p-3f, 0x1.7af974p-3f, 0x1.8cf184p-3f, 0x1.9e8e7cp-3f,
328+
0x1.afd3e4p-3f, 0x1.c0c514p-3f, 0x1.d1653p-3f, 0x1.e1b734p-3f,
329+
0x1.f1bdeep-3f, 0x1.00be06p-2f, 0x1.087a08p-2f, 0x1.101432p-2f,
330+
0x1.178da6p-2f, 0x1.1ee778p-2f, 0x1.2622bp-2f, 0x1.2d404cp-2f,
331+
};
332+
319333
// Generated by Sollya with the following commands:
320334
// > display = hexadecimal;
321335
// > for i from 0 to 31 do print(round(1 / (1 + i * 2^-5), SG, RN));

libc/src/math/generic/log10f16.cpp

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
//===-- Half-precision log10(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/log10f16.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_LOG10F16_EXCEPTS = 11;
28+
#else
29+
static constexpr size_t N_LOG10F16_EXCEPTS = 17;
30+
#endif
31+
32+
static constexpr fputil::ExceptValues<float16, N_LOG10F16_EXCEPTS>
33+
LOG10F16_EXCEPTS = {{
34+
// (input, RZ output, RU offset, RD offset, RN offset)
35+
// x = 0x1.e3cp-3, log10f16(x) = -0x1.40cp-1 (RZ)
36+
{0x338fU, 0xb903U, 0U, 1U, 0U},
37+
// x = 0x1.fep-3, log10f16(x) = -0x1.35p-1 (RZ)
38+
{0x33f8U, 0xb8d4U, 0U, 1U, 1U},
39+
#ifndef LIBC_TARGET_CPU_HAS_FMA
40+
// x = 0x1.394p-1, log10f16(x) = -0x1.b4cp-3 (RZ)
41+
{0x38e5U, 0xb2d3U, 0U, 1U, 1U},
42+
#endif
43+
// x = 0x1.ea8p-1, log10f16(x) = -0x1.31p-6 (RZ)
44+
{0x3baaU, 0xa4c4U, 0U, 1U, 1U},
45+
// x = 0x1.ebp-1, log10f16(x) = -0x1.29cp-6 (RZ)
46+
{0x3bacU, 0xa4a7U, 0U, 1U, 1U},
47+
// x = 0x1.f3p-1, log10f16(x) = -0x1.6dcp-7 (RZ)
48+
{0x3bccU, 0xa1b7U, 0U, 1U, 1U},
49+
// x = 0x1.f38p-1, log10f16(x) = -0x1.5f8p-7 (RZ)
50+
#ifndef LIBC_TARGET_CPU_HAS_FMA
51+
{0x3bceU, 0xa17eU, 0U, 1U, 1U},
52+
// x = 0x1.fd8p-1, log10f16(x) = -0x1.168p-9 (RZ)
53+
{0x3bf6U, 0x985aU, 0U, 1U, 1U},
54+
// x = 0x1.ff8p-1, log10f16(x) = -0x1.bccp-12 (RZ)
55+
{0x3bfeU, 0x8ef3U, 0U, 1U, 1U},
56+
// x = 0x1.374p+0, log10f16(x) = 0x1.5b8p-4 (RZ)
57+
{0x3cddU, 0x2d6eU, 1U, 0U, 1U},
58+
// x = 0x1.3ecp+1, log10f16(x) = 0x1.958p-2 (RZ)
59+
{0x40fbU, 0x3656U, 1U, 0U, 1U},
60+
#endif
61+
// x = 0x1.4p+3, log10f16(x) = 0x1p+0 (RZ)
62+
{0x4900U, 0x3c00U, 0U, 0U, 0U},
63+
// x = 0x1.9p+6, log10f16(x) = 0x1p+1 (RZ)
64+
{0x5640U, 0x4000U, 0U, 0U, 0U},
65+
// x = 0x1.f84p+6, log10f16(x) = 0x1.0ccp+1 (RZ)
66+
{0x57e1U, 0x4033U, 1U, 0U, 0U},
67+
// x = 0x1.f4p+9, log10f16(x) = 0x1.8p+1 (RZ)
68+
{0x63d0U, 0x4200U, 0U, 0U, 0U},
69+
// x = 0x1.388p+13, log10f16(x) = 0x1p+2 (RZ)
70+
{0x70e2U, 0x4400U, 0U, 0U, 0U},
71+
// x = 0x1.674p+13, log10f16(x) = 0x1.03cp+2 (RZ)
72+
{0x719dU, 0x440fU, 1U, 0U, 0U},
73+
}};
74+
75+
LLVM_LIBC_FUNCTION(float16, log10f16, (float16 x)) {
76+
using FPBits = fputil::FPBits<float16>;
77+
FPBits x_bits(x);
78+
79+
uint16_t x_u = x_bits.uintval();
80+
81+
// If x <= 0, or x is 1, or x is +inf, or x is NaN.
82+
if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) {
83+
// log10(NaN) = NaN
84+
if (x_bits.is_nan()) {
85+
if (x_bits.is_signaling_nan()) {
86+
fputil::raise_except_if_required(FE_INVALID);
87+
return FPBits::quiet_nan().get_val();
88+
}
89+
90+
return x;
91+
}
92+
93+
// log10(+/-0) = −inf
94+
if ((x_u & 0x7fffU) == 0U) {
95+
fputil::raise_except_if_required(FE_DIVBYZERO);
96+
return FPBits::inf(Sign::NEG).get_val();
97+
}
98+
99+
if (x_u == 0x3c00U)
100+
return FPBits::zero().get_val();
101+
102+
// When x < 0.
103+
if (x_u > 0x8000U) {
104+
fputil::set_errno_if_required(EDOM);
105+
fputil::raise_except_if_required(FE_INVALID);
106+
return FPBits::quiet_nan().get_val();
107+
}
108+
109+
// log10(+inf) = +inf
110+
return FPBits::inf().get_val();
111+
}
112+
113+
if (auto r = LOG10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
114+
return r.value();
115+
116+
// To compute log10(x), we perform the following range reduction:
117+
// x = 2^m * 1.mant,
118+
// log10(x) = m * log10(2) + log10(1.mant).
119+
// To compute log10(1.mant), let f be the highest 6 bits including the hidden
120+
// bit, and d be the difference (1.mant - f), i.e., the remaining 5 bits of
121+
// the mantissa, then:
122+
// log10(1.mant) = log10(f) + log10(1.mant / f)
123+
// = log10(f) + log10(1 + d/f)
124+
// since d/f is sufficiently small.
125+
// We store log10(f) and 1/f in the lookup tables LOG10F_F and ONE_OVER_F_F
126+
// respectively.
127+
128+
int m = -FPBits::EXP_BIAS;
129+
130+
// When x is subnormal, normalize it.
131+
if ((x_u & FPBits::EXP_MASK) == 0U) {
132+
// Can't pass an integer to fputil::cast directly.
133+
constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
134+
x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP));
135+
x_u = x_bits.uintval();
136+
m -= FPBits::FRACTION_LEN;
137+
}
138+
139+
uint16_t mant = x_bits.get_mantissa();
140+
// Leading 10 - 5 = 5 bits of the mantissa.
141+
int f = mant >> 5;
142+
// Unbiased exponent.
143+
m += x_u >> FPBits::FRACTION_LEN;
144+
145+
// Set bits to 1.mant instead of 2^m * 1.mant.
146+
x_bits.set_biased_exponent(FPBits::EXP_BIAS);
147+
float mant_f = x_bits.get_val();
148+
// v = 1.mant * 1/f - 1 = d/f
149+
float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f);
150+
151+
// Degree-3 minimax polynomial generated by Sollya with the following
152+
// commands:
153+
// > display = hexadecimal;
154+
// > P = fpminimax(log10(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
155+
// > x * P;
156+
float log10p1_d_over_f =
157+
v * fputil::polyeval(v, 0x1.bcb7bp-2f, -0x1.bce168p-3f, 0x1.28acb8p-3f);
158+
// log10(1.mant) = log10(f) + log10(1 + d/f)
159+
float log10_1_mant = LOG10F_F[f] + log10p1_d_over_f;
160+
return fputil::cast<float16>(
161+
fputil::multiply_add(static_cast<float>(m), LOG10F_2, log10_1_mant));
162+
}
163+
164+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/log10f16.h

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

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,17 @@ add_fp_unittest(
18461846
libc.src.__support.FPUtil.fp_bits
18471847
)
18481848

1849+
add_fp_unittest(
1850+
log10f16_test
1851+
NEED_MPFR
1852+
SUITE
1853+
libc-math-unittests
1854+
SRCS
1855+
log10f16_test.cpp
1856+
DEPENDS
1857+
libc.src.math.log10f16
1858+
)
1859+
18491860
add_fp_unittest(
18501861
log1p_test
18511862
NEED_MPFR

libc/test/src/math/log10f16_test.cpp

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

3635+
add_fp_unittest(
3636+
log10f16_test
3637+
SUITE
3638+
libc-math-smoke-tests
3639+
SRCS
3640+
log10f16_test.cpp
3641+
DEPENDS
3642+
libc.hdr.fenv_macros
3643+
libc.src.errno.errno
3644+
libc.src.math.log10f16
3645+
libc.src.__support.FPUtil.cast
3646+
)
3647+
36353648
add_fp_unittest(
36363649
log1p_test
36373650
SUITE

0 commit comments

Comments
 (0)