Skip to content

Commit 5797271

Browse files
committed
[libc][math][c23] Add log10f16 C23 math function
Part of #95250.
1 parent 2f0d352 commit 5797271

File tree

13 files changed

+334
-1
lines changed

13 files changed

+334
-1
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
531531
libc.src.math.llogbf16
532532
libc.src.math.llrintf16
533533
libc.src.math.llroundf16
534+
libc.src.math.log10f16
534535
libc.src.math.log2f16
535536
libc.src.math.logbf16
536537
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
@@ -642,6 +642,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
642642
libc.src.math.llogbf16
643643
libc.src.math.llrintf16
644644
libc.src.math.llroundf16
645+
libc.src.math.log10f16
645646
libc.src.math.log2f16
646647
libc.src.math.logbf16
647648
libc.src.math.logf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ Higher Math Functions
308308
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
309309
| log | |check| | |check| | | |check| | | 7.12.6.11 | F.10.3.11 |
310310
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
311-
| log10 | |check| | |check| | | | | 7.12.6.12 | F.10.3.12 |
311+
| log10 | |check| | |check| | | |check| | | 7.12.6.12 | F.10.3.12 |
312312
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
313313
| log10p1 | | | | | | 7.12.6.13 | F.10.3.13 |
314314
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

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

557557
FunctionSpec<"log10", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
558558
FunctionSpec<"log10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
559+
GuardedFunctionSpec<"log10f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
559560

560561
FunctionSpec<"log1p", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
561562
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
@@ -322,6 +322,7 @@ add_math_entrypoint_object(ldexpf128)
322322

323323
add_math_entrypoint_object(log10)
324324
add_math_entrypoint_object(log10f)
325+
add_math_entrypoint_object(log10f16)
325326

326327
add_math_entrypoint_object(log1p)
327328
add_math_entrypoint_object(log1pf)

libc/src/math/generic/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,6 +2094,27 @@ add_entrypoint_object(
20942094
-O3
20952095
)
20962096

2097+
add_entrypoint_object(
2098+
log10f16
2099+
SRCS
2100+
log10f16.cpp
2101+
HDRS
2102+
../log10f16.h
2103+
DEPENDS
2104+
.expxf16
2105+
libc.hdr.errno_macros
2106+
libc.hdr.fenv_macros
2107+
libc.src.__support.FPUtil.except_value_utils
2108+
libc.src.__support.FPUtil.fenv_impl
2109+
libc.src.__support.FPUtil.fp_bits
2110+
libc.src.__support.FPUtil.multiply_add
2111+
libc.src.__support.FPUtil.polyeval
2112+
libc.src.__support.macros.optimization
2113+
libc.src.__support.macros.properties.cpu_features
2114+
COMPILE_OPTIONS
2115+
-O3
2116+
)
2117+
20972118
add_entrypoint_object(
20982119
log1p
20992120
SRCS

libc/src/math/generic/expxf16.h

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

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

libc/src/math/generic/log10f16.cpp

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

1838+
add_fp_unittest(
1839+
log10f16_test
1840+
NEED_MPFR
1841+
SUITE
1842+
libc-math-unittests
1843+
SRCS
1844+
log10f16_test.cpp
1845+
DEPENDS
1846+
libc.src.math.log10f16
1847+
)
1848+
18381849
add_fp_unittest(
18391850
log1p_test
18401851
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,18 @@ add_fp_unittest(
34803480
libc.src.__support.FPUtil.fp_bits
34813481
)
34823482

3483+
add_fp_unittest(
3484+
log10f16_test
3485+
SUITE
3486+
libc-math-smoke-tests
3487+
SRCS
3488+
log10f16_test.cpp
3489+
DEPENDS
3490+
libc.hdr.fenv_macros
3491+
libc.src.errno.errno
3492+
libc.src.math.log10f16
3493+
)
3494+
34833495
add_fp_unittest(
34843496
log1p_test
34853497
SUITE

0 commit comments

Comments
 (0)