Skip to content

Commit 2d4c40c

Browse files
committed
[libc][math][c23] Add exp10f16 C23 math function
Part of #95250.
1 parent a3629e4 commit 2d4c40c

File tree

11 files changed

+372
-20
lines changed

11 files changed

+372
-20
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
564564
libc.src.math.canonicalizef16
565565
libc.src.math.ceilf16
566566
libc.src.math.copysignf16
567+
libc.src.math.exp10f16
567568
libc.src.math.exp2f16
568569
libc.src.math.expf16
569570
libc.src.math.f16add

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ Higher Math Functions
286286
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
287287
| exp | |check| | |check| | | |check| | | 7.12.6.1 | F.10.3.1 |
288288
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
289-
| exp10 | |check| | |check| | | | | 7.12.6.2 | F.10.3.2 |
289+
| exp10 | |check| | |check| | | |check| | | 7.12.6.2 | F.10.3.2 |
290290
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
291291
| exp10m1 | | | | | | 7.12.6.3 | F.10.3.3 |
292292
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

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

587587
FunctionSpec<"exp10", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
588588
FunctionSpec<"exp10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
589+
GuardedFunctionSpec<"exp10f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
589590

590591
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
591592
FunctionSpec<"remainderf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ add_math_entrypoint_object(exp2m1f)
107107

108108
add_math_entrypoint_object(exp10)
109109
add_math_entrypoint_object(exp10f)
110+
add_math_entrypoint_object(exp10f16)
110111

111112
add_math_entrypoint_object(expm1)
112113
add_math_entrypoint_object(expm1f)

libc/src/math/exp10f16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,29 @@ add_entrypoint_object(
14071407
-O3
14081408
)
14091409

1410+
add_entrypoint_object(
1411+
exp10f16
1412+
SRCS
1413+
exp10f16.cpp
1414+
HDRS
1415+
../exp10f16.h
1416+
DEPENDS
1417+
libc.hdr.errno_macros
1418+
libc.hdr.fenv_macros
1419+
libc.src.__support.CPP.array
1420+
libc.src.__support.FPUtil.except_value_utils
1421+
libc.src.__support.FPUtil.fenv_impl
1422+
libc.src.__support.FPUtil.fp_bits
1423+
libc.src.__support.FPUtil.multiply_add
1424+
libc.src.__support.FPUtil.nearest_integer
1425+
libc.src.__support.FPUtil.polyeval
1426+
libc.src.__support.FPUtil.rounding_mode
1427+
libc.src.__support.macros.optimization
1428+
libc.src.__support.macros.properties.cpu_features
1429+
COMPILE_OPTIONS
1430+
-O3
1431+
)
1432+
14101433
add_entrypoint_object(
14111434
expm1
14121435
SRCS

libc/src/math/generic/exp10f16.cpp

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
//===-- Half-precision 10^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/exp10f16.h"
10+
#include "hdr/errno_macros.h"
11+
#include "hdr/fenv_macros.h"
12+
#include "src/__support/CPP/array.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/FPUtil/nearest_integer.h"
19+
#include "src/__support/FPUtil/rounding_mode.h"
20+
#include "src/__support/common.h"
21+
#include "src/__support/macros/config.h"
22+
#include "src/__support/macros/optimization.h"
23+
#include "src/__support/macros/properties/cpu_features.h"
24+
25+
namespace LIBC_NAMESPACE_DECL {
26+
27+
static constexpr size_t N_EXP10F16_EXCEPTS = 5
28+
#ifndef LIBC_TARGET_CPU_HAS_FMA
29+
+ 3
30+
#endif
31+
;
32+
33+
static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS>
34+
EXP10F16_EXCEPTS = {{
35+
// x = 0x1.8f4p-2, exp10f16(x) = 0x1.3ap+1 (RZ)
36+
{0x363dU, 0x40e8U, 1U, 0U, 1U},
37+
// x = 0x1.95cp-2, exp10f16(x) = 0x1.3ecp+1 (RZ)
38+
{0x3657U, 0x40fbU, 1U, 0U, 0U},
39+
// x = -0x1.018p-4, exp10f16(x) = 0x1.bbp-1 (RZ)
40+
{0xac06U, 0x3aecU, 1U, 0U, 0U},
41+
// x = -0x1.c28p+0, exp10f16(x) = 0x1.1ccp-6 (RZ)
42+
{0xbf0aU, 0x2473U, 1U, 0U, 0U},
43+
// x = -0x1.e1cp+1, exp10f16(x) = 0x1.694p-13 (RZ)
44+
{0xc387U, 0x09a5U, 1U, 0U, 0U},
45+
#ifndef LIBC_TARGET_CPU_HAS_FMA
46+
// x = 0x1.0cp+1, exp10f16(x) = 0x1.f04p+6 (RZ)
47+
{0x4030U, 0x57c1U, 1U, 0U, 1U},
48+
// x = 0x1.1b8p+1, exp10f16(x) = 0x1.47cp+7 (RZ)
49+
{0x406eU, 0x591fU, 1U, 0U, 1U},
50+
// x = 0x1.1b8p+2, exp10f16(x) = 0x1.a4p+14 (RZ)
51+
{0x446eU, 0x7690U, 1U, 0U, 1U},
52+
#endif
53+
}};
54+
55+
// Generated by Sollya with the following commands:
56+
// > display = hexadecimal;
57+
// > round(log2(10), SG, RN);
58+
static constexpr float LOG2F_10 = 0x1.a934fp+1f;
59+
60+
// Generated by Sollya with the following commands:
61+
// > display = hexadecimal;
62+
// > round(log10(2), SG, RN);
63+
static constexpr float LOG10F_2 = 0x1.344136p-2f;
64+
65+
// Generated by Sollya with the following commands:
66+
// > display = hexadecimal;
67+
// > for i from 0 to 7 do printsingle(round(2^(i * 2^-3), SG, RN));
68+
static constexpr cpp::array<uint32_t, 8> EXP2_MID_BITS = {
69+
0x3f80'0000U, 0x3f8b'95c2U, 0x3f98'37f0U, 0x3fa5'fed7U,
70+
0x3fb5'04f3U, 0x3fc5'672aU, 0x3fd7'44fdU, 0x3fea'c0c7U,
71+
};
72+
73+
LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) {
74+
using FPBits = fputil::FPBits<float16>;
75+
FPBits x_bits(x);
76+
77+
uint16_t x_u = x_bits.uintval();
78+
uint16_t x_abs = x_u & 0x7fffU;
79+
80+
// When |x| >= 5, or x is NaN.
81+
if (LIBC_UNLIKELY(x_abs >= 0x4500U)) {
82+
// exp10(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+
// When x >= 5.
93+
if (x_bits.is_pos()) {
94+
// exp10(+inf) = +inf
95+
if (x_bits.is_inf())
96+
return FPBits::inf().get_val();
97+
98+
switch (fputil::quick_get_round()) {
99+
case FE_TONEAREST:
100+
case FE_UPWARD:
101+
fputil::set_errno_if_required(ERANGE);
102+
fputil::raise_except_if_required(FE_OVERFLOW);
103+
return FPBits::inf().get_val();
104+
default:
105+
return FPBits::max_normal().get_val();
106+
}
107+
}
108+
109+
// When x <= -8.
110+
if (x_u >= 0xc800U) {
111+
// exp10(-inf) = +0
112+
if (x_bits.is_inf())
113+
return FPBits::zero().get_val();
114+
115+
fputil::set_errno_if_required(ERANGE);
116+
fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
117+
118+
if (fputil::fenv_is_round_up())
119+
return FPBits::min_subnormal().get_val();
120+
return FPBits::zero().get_val();
121+
}
122+
}
123+
124+
// When x is 1, 2, 3, or 4. These are hard-to-round cases with exact results.
125+
if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) {
126+
switch (x_u) {
127+
case 0x3c00U: // x = 1.0f16
128+
return static_cast<float16>(10.0);
129+
case 0x4000U: // x = 2.0f16
130+
return static_cast<float16>(100.0);
131+
case 0x4200U: // x = 3.0f16
132+
return static_cast<float16>(1'000.0);
133+
case 0x4400U: // x = 4.0f16
134+
return static_cast<float16>(10'000.0);
135+
}
136+
}
137+
138+
if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
139+
return r.value();
140+
141+
// For -8 < x < 5, to compute 10^x, we perform the following range reduction:
142+
// find hi, mid, lo, such that:
143+
// x = (hi + mid) * log2(10) + lo, in which
144+
// hi is an integer,
145+
// mid * 2^3 is an integer,
146+
// -2^(-4) <= lo < 2^(-4).
147+
// In particular,
148+
// hi + mid = round(x * 2^3) * 2^(-3).
149+
// Then,
150+
// 10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo
151+
// We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid
152+
// by adding hi to the exponent field of 2^mid. 10^lo is computed using a
153+
// degree-4 minimax polynomial generated by Sollya.
154+
155+
float xf = x;
156+
float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f));
157+
int x_hi_mid = static_cast<int>(kf);
158+
int x_hi = x_hi_mid >> 3;
159+
int x_mid = x_hi_mid & 0x7;
160+
// lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x
161+
float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf);
162+
163+
uint32_t exp2_hi_mid_bits =
164+
EXP2_MID_BITS[x_mid] +
165+
static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
166+
float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
167+
// Degree-4 minimax polynomial generated by Sollya with the following
168+
// commands:
169+
// > display = hexadecimal;
170+
// > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]);
171+
// > 1 + x * P;
172+
float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f,
173+
0x1.04b434p+1f, 0x1.2bcf9ep+0f);
174+
return static_cast<float16>(exp2_hi_mid * exp10_lo);
175+
}
176+
177+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/math/CMakeLists.txt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,19 @@ add_fp_unittest(
976976
libc.src.__support.FPUtil.fp_bits
977977
)
978978

979+
add_fp_unittest(
980+
exp10_test
981+
NEED_MPFR
982+
SUITE
983+
libc-math-unittests
984+
SRCS
985+
exp10_test.cpp
986+
DEPENDS
987+
libc.src.errno.errno
988+
libc.src.math.exp10
989+
libc.src.__support.FPUtil.fp_bits
990+
)
991+
979992
add_fp_unittest(
980993
exp10f_test
981994
NEED_MPFR
@@ -990,16 +1003,14 @@ add_fp_unittest(
9901003
)
9911004

9921005
add_fp_unittest(
993-
exp10_test
994-
NEED_MPFR
995-
SUITE
996-
libc-math-unittests
997-
SRCS
998-
exp10_test.cpp
999-
DEPENDS
1000-
libc.src.errno.errno
1001-
libc.src.math.exp10
1002-
libc.src.__support.FPUtil.fp_bits
1006+
exp10f16_test
1007+
NEED_MPFR
1008+
SUITE
1009+
libc-math-unittests
1010+
SRCS
1011+
exp10f16_test.cpp
1012+
DEPENDS
1013+
libc.src.math.exp10f16
10031014
)
10041015

10051016
add_fp_unittest(

libc/test/src/math/exp10f16_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- Exhaustive test for exp10f16 --------------------------------------===//
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/exp10f16.h"
10+
#include "test/UnitTest/FPMatcher.h"
11+
#include "test/UnitTest/Test.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
14+
using LlvmLibcExp10f16Test = 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(LlvmLibcExp10f16Test, 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::Exp10, x,
30+
LIBC_NAMESPACE::exp10f16(x), 0.5);
31+
}
32+
}
33+
34+
TEST_F(LlvmLibcExp10f16Test, 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::Exp10, x,
38+
LIBC_NAMESPACE::exp10f16(x), 0.5);
39+
}
40+
}

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,18 @@ add_fp_unittest(
10241024
libc.src.math.exp2m1f
10251025
)
10261026

1027+
add_fp_unittest(
1028+
exp10_test
1029+
SUITE
1030+
libc-math-smoke-tests
1031+
SRCS
1032+
exp10_test.cpp
1033+
DEPENDS
1034+
libc.src.errno.errno
1035+
libc.src.math.exp10
1036+
libc.src.__support.FPUtil.fp_bits
1037+
)
1038+
10271039
add_fp_unittest(
10281040
exp10f_test
10291041
SUITE
@@ -1037,15 +1049,15 @@ add_fp_unittest(
10371049
)
10381050

10391051
add_fp_unittest(
1040-
exp10_test
1041-
SUITE
1042-
libc-math-smoke-tests
1043-
SRCS
1044-
exp10_test.cpp
1045-
DEPENDS
1046-
libc.src.errno.errno
1047-
libc.src.math.exp10
1048-
libc.src.__support.FPUtil.fp_bits
1052+
exp10f16_test
1053+
SUITE
1054+
libc-math-smoke-tests
1055+
SRCS
1056+
exp10f16_test.cpp
1057+
DEPENDS
1058+
libc.hdr.fenv_macros
1059+
libc.src.errno.errno
1060+
libc.src.math.exp10f16
10491061
)
10501062

10511063
add_fp_unittest(

0 commit comments

Comments
 (0)