Skip to content

Commit 8b3537f

Browse files
committed
[libc][math][c23] Add exp10m1f C23 math function
Fixes #86503.
1 parent 359ab3a commit 8b3537f

File tree

17 files changed

+517
-18
lines changed

17 files changed

+517
-18
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ set(TARGET_LIBM_ENTRYPOINTS
374374
libc.src.math.expf
375375
libc.src.math.exp10
376376
libc.src.math.exp10f
377+
libc.src.math.exp10m1f
377378
libc.src.math.exp2
378379
libc.src.math.exp2f
379380
libc.src.math.exp2m1f

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ Higher Math Functions
266266
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
267267
| exp10 | |check| | |check| | | | | 7.12.6.2 | F.10.3.2 |
268268
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
269-
| exp10m1 | | | | | | 7.12.6.3 | F.10.3.3 |
269+
| exp10m1 | |check| | | | | | 7.12.6.3 | F.10.3.3 |
270270
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
271271
| exp2 | |check| | |check| | | | | 7.12.6.4 | F.10.3.4 |
272272
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,8 @@ def StdC : StandardSpec<"stdc"> {
553553
FunctionSpec<"exp10", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
554554
FunctionSpec<"exp10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
555555

556+
FunctionSpec<"exp10m1f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
557+
556558
FunctionSpec<"remainderf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
557559
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
558560
FunctionSpec<"remainderl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ add_math_entrypoint_object(exp2m1f)
9393
add_math_entrypoint_object(exp10)
9494
add_math_entrypoint_object(exp10f)
9595

96+
add_math_entrypoint_object(exp10m1f)
97+
9698
add_math_entrypoint_object(expm1)
9799
add_math_entrypoint_object(expm1f)
98100

libc/src/math/exp10m1f.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for exp10m1f ----------------------*- 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_EXP10M1F_H
10+
#define LLVM_LIBC_SRC_MATH_EXP10M1F_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
float exp10m1f(float x);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_MATH_EXP10M1F_H

libc/src/math/generic/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ add_entrypoint_object(
873873
.explogxf
874874
libc.src.errno.errno
875875
libc.src.__support.common
876+
libc.src.__support.FPUtil.except_value_utils
876877
libc.src.__support.FPUtil.fenv_impl
877878
libc.src.__support.FPUtil.fp_bits
878879
libc.src.__support.FPUtil.multiply_add
@@ -943,6 +944,27 @@ add_entrypoint_object(
943944
-O3
944945
)
945946

947+
add_entrypoint_object(
948+
exp10m1f
949+
SRCS
950+
exp10m1f.cpp
951+
HDRS
952+
../exp10m1f.h
953+
DEPENDS
954+
.explogxf
955+
libc.src.errno.errno
956+
libc.src.__support.common
957+
libc.src.__support.FPUtil.except_value_utils
958+
libc.src.__support.FPUtil.fenv_impl
959+
libc.src.__support.FPUtil.fp_bits
960+
libc.src.__support.FPUtil.multiply_add
961+
libc.src.__support.FPUtil.polyeval
962+
libc.src.__support.FPUtil.rounding_mode
963+
libc.src.__support.macros.optimization
964+
COMPILE_OPTIONS
965+
-O3
966+
)
967+
946968
add_entrypoint_object(
947969
expm1
948970
SRCS

libc/src/math/generic/exp10m1f.cpp

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
//===-- Implementation of exp10m1f 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/exp10m1f.h"
10+
#include "src/__support/FPUtil/FEnvImpl.h"
11+
#include "src/__support/FPUtil/FPBits.h"
12+
#include "src/__support/FPUtil/PolyEval.h"
13+
#include "src/__support/FPUtil/except_value_utils.h"
14+
#include "src/__support/FPUtil/multiply_add.h"
15+
#include "src/__support/FPUtil/rounding_mode.h"
16+
#include "src/__support/common.h"
17+
#include "src/__support/macros/optimization.h"
18+
#include "src/errno/libc_errno.h"
19+
20+
#include "explogxf.h"
21+
22+
namespace LIBC_NAMESPACE {
23+
24+
static constexpr size_t N_EXCEPTS_LO = 11;
25+
26+
static constexpr fputil::ExceptValues<float, N_EXCEPTS_LO> EXP10M1F_EXCEPTS_LO =
27+
{{
28+
// x = 0x1.0fe54ep-11, exp10m1f(x) = 0x1.3937eep-10 (RZ)
29+
{0x3a07'f2a7U, 0x3a9c'9bf7U, 1U, 0U, 1U},
30+
// x = 0x1.80e6eap-11, exp10m1f(x) = 0x1.bb8272p-10 (RZ)
31+
{0x3a40'7375U, 0x3add'c139U, 1U, 0U, 1U},
32+
// x = -0x1.2a33bcp-51, exp10m1f(x) = -0x1.57515ep-50 (RZ)
33+
{0xa615'19deU, 0xa6ab'a8afU, 0U, 1U, 0U},
34+
// x = -0x0p+0, exp10m1f(x) = -0x0p+0 (RZ)
35+
{0x8000'0000U, 0x8000'0000U, 0U, 0U, 0U},
36+
// x = -0x1.b59e08p-31, exp10m1f(x) = -0x1.f7d356p-30 (RZ)
37+
{0xb05a'cf04U, 0xb0fb'e9abU, 0U, 1U, 1U},
38+
// x = -0x1.bf342p-12, exp10m1f(x) = -0x1.014e02p-10 (RZ)
39+
{0xb9df'9a10U, 0xba80'a701U, 0U, 1U, 0U},
40+
// x = -0x1.6207fp-11, exp10m1f(x) = -0x1.9746cap-10 (RZ)
41+
{0xba31'03f8U, 0xbacb'a365U, 0U, 1U, 1U},
42+
// x = -0x1.bd0c66p-11, exp10m1f(x) = -0x1.ffe168p-10 (RZ)
43+
{0xba5e'8633U, 0xbaff'f0b4U, 0U, 1U, 1U},
44+
// x = -0x1.ffd84cp-10, exp10m1f(x) = -0x1.25faf2p-8 (RZ)
45+
{0xbaff'ec26U, 0xbb92'fd79U, 0U, 1U, 0U},
46+
// x = -0x1.a74172p-9, exp10m1f(x) = -0x1.e57be2p-8 (RZ)
47+
{0xbb53'a0b9U, 0xbbf2'bdf1U, 0U, 1U, 1U},
48+
// x = -0x1.cb694cp-9, exp10m1f(x) = -0x1.0764e4p-7 (RZ)
49+
{0xbb65'b4a6U, 0xbc03'b272U, 0U, 1U, 0U},
50+
}};
51+
52+
static constexpr size_t N_EXCEPTS_HI = 19;
53+
54+
static constexpr fputil::ExceptValues<float, N_EXCEPTS_HI> EXP10M1F_EXCEPTS_HI =
55+
{{
56+
// (input, RZ output, RU offset, RD offset, RN offset)
57+
// x = 0x1.8d31eep-8, exp10m1f(x) = 0x1.cc7e4cp-7 (RZ)
58+
{0x3bc6'98f7U, 0x3c66'3f26U, 1U, 0U, 1U},
59+
// x = 0x1.915fcep-8, exp10m1f(x) = 0x1.d15f72p-7 (RZ)
60+
{0x3bc8'afe7U, 0x3c68'afb9U, 1U, 0U, 0U},
61+
// x = 0x1.bcf982p-8, exp10m1f(x) = 0x1.022928p-6 (RZ)
62+
{0x3bde'7cc1U, 0x3c81'1494U, 1U, 0U, 1U},
63+
// x = 0x1.99ff0ap-7, exp10m1f(x) = 0x1.dee416p-6 (RZ)
64+
{0x3c4c'ff85U, 0x3cef'720bU, 1U, 0U, 0U},
65+
// x = 0x1.75ea14p-6, exp10m1f(x) = 0x1.b9ff16p-5 (RZ)
66+
{0x3cba'f50aU, 0x3d5c'ff8bU, 1U, 0U, 0U},
67+
// x = 0x1.f81b64p-6, exp10m1f(x) = 0x1.2cb6bcp-4 (RZ)
68+
{0x3cfc'0db2U, 0x3d96'5b5eU, 1U, 0U, 0U},
69+
// x = 0x1.fafecp+3, exp10m1f(x) = 0x1.8c880ap+52 (RZ)
70+
{0x417d'7f60U, 0x59c6'4405U, 1U, 0U, 0U},
71+
// x = -0x1.3bf094p-8, exp10m1f(x) = -0x1.69ba4ap-7 (RZ)
72+
{0xbb9d'f84aU, 0xbc34'dd25U, 0U, 1U, 0U},
73+
// x = -0x1.4558bcp-8, exp10m1f(x) = -0x1.746fb8p-7 (RZ)
74+
{0xbba2'ac5eU, 0xbc3a'37dcU, 0U, 1U, 1U},
75+
// x = -0x1.4bb43p-8, exp10m1f(x) = -0x1.7babe4p-7 (RZ)
76+
{0xbba5'da18U, 0xbc3d'd5f2U, 0U, 1U, 1U},
77+
// x = -0x1.776cc8p-8, exp10m1f(x) = -0x1.ad62c4p-7 (RZ)
78+
{0xbbbb'b664U, 0xbc56'b162U, 0U, 1U, 0U},
79+
// x = -0x1.f024cp-8, exp10m1f(x) = -0x1.1b20d6p-6 (RZ)
80+
{0xbbf8'1260U, 0xbc8d'906bU, 0U, 1U, 1U},
81+
// x = -0x1.f510eep-8, exp10m1f(x) = -0x1.1de9aap-6 (RZ)
82+
{0xbbfa'8877U, 0xbc8e'f4d5U, 0U, 1U, 0U},
83+
// x = -0x1.0b43c4p-7, exp10m1f(x) = -0x1.30d418p-6 (RZ)
84+
{0xbc05'a1e2U, 0xbc98'6a0cU, 0U, 1U, 0U},
85+
// x = -0x1.245ee4p-7, exp10m1f(x) = -0x1.4d2b86p-6 (RZ)
86+
{0xbc12'2f72U, 0xbca6'95c3U, 0U, 1U, 0U},
87+
// x = -0x1.f9f2dap-7, exp10m1f(x) = -0x1.1e2186p-5 (RZ)
88+
{0xbc7c'f96dU, 0xbd0f'10c3U, 0U, 1U, 0U},
89+
// x = -0x1.08e42p-6, exp10m1f(x) = -0x1.2b5c4p-5 (RZ)
90+
{0xbc84'7210U, 0xbd15'ae20U, 0U, 1U, 1U},
91+
// x = -0x1.0cdc44p-5, exp10m1f(x) = -0x1.2a2152p-4 (RZ)
92+
{0xbd06'6e22U, 0xbd95'10a9U, 0U, 1U, 1U},
93+
// x = -0x1.ca4322p-5, exp10m1f(x) = -0x1.ef073p-4 (RZ)
94+
{0xbd65'2191U, 0xbdf7'8398U, 0U, 1U, 1U},
95+
}};
96+
97+
LLVM_LIBC_FUNCTION(float, exp10m1f, (float x)) {
98+
using FPBits = fputil::FPBits<float>;
99+
FPBits xbits(x);
100+
101+
uint32_t x_u = xbits.uintval();
102+
uint32_t x_abs = x_u & 0x7fff'ffffU;
103+
104+
// When x >= log10(2^128), or x is nan
105+
if (LIBC_UNLIKELY(xbits.is_pos() && x_u >= 0x421a'209bU)) {
106+
if (xbits.is_finite()) {
107+
int rounding = fputil::quick_get_round();
108+
if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
109+
return FPBits::max_normal().get_val();
110+
111+
fputil::set_errno_if_required(ERANGE);
112+
fputil::raise_except_if_required(FE_OVERFLOW);
113+
}
114+
115+
// x >= log10(2^128) and 10^x - 1 rounds to +inf, or x is +inf or nan
116+
return x + FPBits::inf().get_val();
117+
}
118+
119+
// When |x| <= log10(2) * 2^(-6)
120+
if (LIBC_UNLIKELY(x_abs <= 0x3b9a'209bU)) {
121+
if (auto r = EXP10M1F_EXCEPTS_LO.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
122+
return r.value();
123+
124+
double dx = x;
125+
double dx_sq = dx * dx;
126+
double c0 = dx * Exp10Base::COEFFS[0];
127+
double c1 =
128+
fputil::multiply_add(dx, Exp10Base::COEFFS[2], Exp10Base::COEFFS[1]);
129+
double c2 =
130+
fputil::multiply_add(dx, Exp10Base::COEFFS[4], Exp10Base::COEFFS[3]);
131+
// 10^dx - 1 ~ (1 + COEFFS[0] * dx + ... + COEFFS[4] * dx^5) - 1
132+
// = COEFFS[0] * dx + ... + COEFFS[4] * dx^5
133+
return static_cast<float>(fputil::polyeval(dx_sq, c0, c1, c2));
134+
}
135+
136+
// When x <= log10(2^-25), or x is nan
137+
if (LIBC_UNLIKELY(x <= -0x1.e1a5e2p+2f)) {
138+
// exp10m1(-inf) = -1
139+
if (xbits.is_inf())
140+
return -1.0f;
141+
// exp10m1(nan) = nan
142+
if (xbits.is_nan())
143+
return x;
144+
145+
int rounding = fputil::quick_get_round();
146+
if (rounding == FE_UPWARD || rounding == FE_TOWARDZERO ||
147+
(rounding == FE_TONEAREST && x == -0x1.e1a5e2p+2f))
148+
return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f
149+
150+
fputil::set_errno_if_required(ERANGE);
151+
fputil::raise_except_if_required(FE_UNDERFLOW);
152+
return -1.0f;
153+
}
154+
155+
// Exact outputs when x = 1, 2, ..., 10.
156+
// Quick check mask: 0x800f'ffffU = ~(bits of 1.0f | ... | bits of 10.0f)
157+
if (LIBC_UNLIKELY((x_u & 0x800f'ffffU) == 0)) {
158+
switch (x_u) {
159+
case 0x3f800000U: // x = 1.0f
160+
return 9.0f;
161+
case 0x40000000U: // x = 2.0f
162+
return 99.0f;
163+
case 0x40400000U: // x = 3.0f
164+
return 999.0f;
165+
case 0x40800000U: // x = 4.0f
166+
return 9'999.0f;
167+
case 0x40a00000U: // x = 5.0f
168+
return 99'999.0f;
169+
case 0x40c00000U: // x = 6.0f
170+
return 999'999.0f;
171+
case 0x40e00000U: // x = 7.0f
172+
return 9'999'999.0f;
173+
case 0x41000000U: { // x = 8.0f
174+
int rounding = fputil::quick_get_round();
175+
if (rounding == FE_UPWARD || rounding == FE_TONEAREST)
176+
return 100'000'000.0f;
177+
return 99'999'992.0f;
178+
}
179+
case 0x41100000U: { // x = 9.0f
180+
int rounding = fputil::quick_get_round();
181+
if (rounding == FE_UPWARD || rounding == FE_TONEAREST)
182+
return 1'000'000'000.0f;
183+
return 999'999'936.0f;
184+
}
185+
case 0x41200000U: { // x = 10.0f
186+
int rounding = fputil::quick_get_round();
187+
if (rounding == FE_UPWARD || rounding == FE_TONEAREST)
188+
return 10'000'000'000.0f;
189+
return 9'999'998'976.0f;
190+
}
191+
}
192+
}
193+
194+
if (auto r = EXP10M1F_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
195+
return r.value();
196+
197+
// Range reduction: 10^x = 2^(mid + hi) * 10^lo
198+
// rr = (2^(mid + hi), lo)
199+
auto rr = exp_b_range_reduc<Exp10Base>(x);
200+
201+
// The low part is approximated by a degree-5 minimax polynomial.
202+
// 10^lo ~ 1 + COEFFS[0] * lo + ... + COEFFS[4] * lo^5
203+
double lo_sq = rr.lo * rr.lo;
204+
double c0 = fputil::multiply_add(rr.lo, Exp10Base::COEFFS[0], 1.0);
205+
double c1 =
206+
fputil::multiply_add(rr.lo, Exp10Base::COEFFS[2], Exp10Base::COEFFS[1]);
207+
double c2 =
208+
fputil::multiply_add(rr.lo, Exp10Base::COEFFS[4], Exp10Base::COEFFS[3]);
209+
double exp10_lo = fputil::polyeval(lo_sq, c0, c1, c2);
210+
// 10^x - 1 = 2^(mid + hi) * 10^lo - 1
211+
// ~ mh * exp10_lo - 1
212+
return static_cast<float>(fputil::multiply_add(exp10_lo, rr.mh, -1.0));
213+
}
214+
215+
} // namespace LIBC_NAMESPACE

libc/test/src/math/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ add_fp_unittest(
643643
SRCS
644644
exp2m1f_test.cpp
645645
DEPENDS
646+
libc.hdr.math_macros
646647
libc.src.errno.errno
647648
libc.src.math.exp2m1f
648649
libc.src.__support.CPP.array
@@ -675,6 +676,21 @@ add_fp_unittest(
675676
libc.src.__support.FPUtil.fp_bits
676677
)
677678

679+
add_fp_unittest(
680+
exp10m1f_test
681+
NEED_MPFR
682+
SUITE
683+
libc-math-unittests
684+
SRCS
685+
exp10m1f_test.cpp
686+
DEPENDS
687+
libc.hdr.math_macros
688+
libc.src.errno.errno
689+
libc.src.math.exp10m1f
690+
libc.src.__support.CPP.array
691+
libc.src.__support.FPUtil.fp_bits
692+
)
693+
678694
add_fp_unittest(
679695
copysign_test
680696
SUITE

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ add_fp_unittest(
165165
-lpthread
166166
)
167167

168+
add_fp_unittest(
169+
exp10m1f_test
170+
NO_RUN_POSTBUILD
171+
NEED_MPFR
172+
SUITE
173+
libc_math_exhaustive_tests
174+
SRCS
175+
exp10m1f_test.cpp
176+
DEPENDS
177+
.exhaustive_test
178+
libc.src.math.exp10m1f
179+
LINK_LIBRARIES
180+
-lpthread
181+
)
182+
168183
add_fp_unittest(
169184
expm1f_test
170185
NO_RUN_POSTBUILD
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- Exhaustive test for exp10m1f --------------------------------------===//
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 "exhaustive_test.h"
10+
#include "src/math/exp10m1f.h"
11+
#include "utils/MPFRWrapper/MPFRUtils.h"
12+
13+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
14+
15+
using LlvmLibcExp10m1fExhaustiveTest =
16+
LlvmLibcUnaryOpExhaustiveMathTest<float, mpfr::Operation::Exp10m1,
17+
LIBC_NAMESPACE::exp10m1f>;
18+
19+
// Range: [0, Inf];
20+
static constexpr uint32_t POS_START = 0x0000'0000U;
21+
static constexpr uint32_t POS_STOP = 0x7f80'0000U;
22+
23+
TEST_F(LlvmLibcExp10m1fExhaustiveTest, PostiveRange) {
24+
test_full_range_all_roundings(POS_START, POS_STOP);
25+
}
26+
27+
// Range: [-Inf, 0];
28+
static constexpr uint32_t NEG_START = 0x8000'0000U;
29+
static constexpr uint32_t NEG_STOP = 0xff80'0000U;
30+
31+
TEST_F(LlvmLibcExp10m1fExhaustiveTest, NegativeRange) {
32+
test_full_range_all_roundings(NEG_START, NEG_STOP);
33+
}

0 commit comments

Comments
 (0)