Skip to content

Commit 2295725

Browse files
committed
[libc][math][c23] Add exp10f16 C23 math function
Part of #95250.
1 parent 936515c commit 2295725

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
@@ -576,6 +576,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
576576
libc.src.math.canonicalizef16
577577
libc.src.math.ceilf16
578578
libc.src.math.copysignf16
579+
libc.src.math.exp10f16
579580
libc.src.math.exp2f16
580581
libc.src.math.expf16
581582
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
@@ -591,6 +591,7 @@ def StdC : StandardSpec<"stdc"> {
591591

592592
FunctionSpec<"exp10", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
593593
FunctionSpec<"exp10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
594+
GuardedFunctionSpec<"exp10f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
594595

595596
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
596597
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
@@ -116,6 +116,7 @@ add_math_entrypoint_object(exp2m1f)
116116

117117
add_math_entrypoint_object(exp10)
118118
add_math_entrypoint_object(exp10f)
119+
add_math_entrypoint_object(exp10f16)
119120

120121
add_math_entrypoint_object(expm1)
121122
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
@@ -1497,6 +1497,29 @@ add_entrypoint_object(
14971497
-O3
14981498
)
14991499

1500+
add_entrypoint_object(
1501+
exp10f16
1502+
SRCS
1503+
exp10f16.cpp
1504+
HDRS
1505+
../exp10f16.h
1506+
DEPENDS
1507+
libc.hdr.errno_macros
1508+
libc.hdr.fenv_macros
1509+
libc.src.__support.CPP.array
1510+
libc.src.__support.FPUtil.except_value_utils
1511+
libc.src.__support.FPUtil.fenv_impl
1512+
libc.src.__support.FPUtil.fp_bits
1513+
libc.src.__support.FPUtil.multiply_add
1514+
libc.src.__support.FPUtil.nearest_integer
1515+
libc.src.__support.FPUtil.polyeval
1516+
libc.src.__support.FPUtil.rounding_mode
1517+
libc.src.__support.macros.optimization
1518+
libc.src.__support.macros.properties.cpu_features
1519+
COMPILE_OPTIONS
1520+
-O3
1521+
)
1522+
15001523
add_entrypoint_object(
15011524
expm1
15021525
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
@@ -990,6 +990,19 @@ add_fp_unittest(
990990
libc.src.__support.FPUtil.fp_bits
991991
)
992992

993+
add_fp_unittest(
994+
exp10_test
995+
NEED_MPFR
996+
SUITE
997+
libc-math-unittests
998+
SRCS
999+
exp10_test.cpp
1000+
DEPENDS
1001+
libc.src.errno.errno
1002+
libc.src.math.exp10
1003+
libc.src.__support.FPUtil.fp_bits
1004+
)
1005+
9931006
add_fp_unittest(
9941007
exp10f_test
9951008
NEED_MPFR
@@ -1004,16 +1017,14 @@ add_fp_unittest(
10041017
)
10051018

10061019
add_fp_unittest(
1007-
exp10_test
1008-
NEED_MPFR
1009-
SUITE
1010-
libc-math-unittests
1011-
SRCS
1012-
exp10_test.cpp
1013-
DEPENDS
1014-
libc.src.errno.errno
1015-
libc.src.math.exp10
1016-
libc.src.__support.FPUtil.fp_bits
1020+
exp10f16_test
1021+
NEED_MPFR
1022+
SUITE
1023+
libc-math-unittests
1024+
SRCS
1025+
exp10f16_test.cpp
1026+
DEPENDS
1027+
libc.src.math.exp10f16
10171028
)
10181029

10191030
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
@@ -1076,6 +1076,18 @@ add_fp_unittest(
10761076
libc.src.math.exp2m1f
10771077
)
10781078

1079+
add_fp_unittest(
1080+
exp10_test
1081+
SUITE
1082+
libc-math-smoke-tests
1083+
SRCS
1084+
exp10_test.cpp
1085+
DEPENDS
1086+
libc.src.errno.errno
1087+
libc.src.math.exp10
1088+
libc.src.__support.FPUtil.fp_bits
1089+
)
1090+
10791091
add_fp_unittest(
10801092
exp10f_test
10811093
SUITE
@@ -1089,15 +1101,15 @@ add_fp_unittest(
10891101
)
10901102

10911103
add_fp_unittest(
1092-
exp10_test
1093-
SUITE
1094-
libc-math-smoke-tests
1095-
SRCS
1096-
exp10_test.cpp
1097-
DEPENDS
1098-
libc.src.errno.errno
1099-
libc.src.math.exp10
1100-
libc.src.__support.FPUtil.fp_bits
1104+
exp10f16_test
1105+
SUITE
1106+
libc-math-smoke-tests
1107+
SRCS
1108+
exp10f16_test.cpp
1109+
DEPENDS
1110+
libc.hdr.fenv_macros
1111+
libc.src.errno.errno
1112+
libc.src.math.exp10f16
11011113
)
11021114

11031115
add_fp_unittest(

0 commit comments

Comments
 (0)