Skip to content

Commit 971a1ac

Browse files
authored
[libc][math][c23] Add expf16 C23 math function (#100632)
Part of #95250.
1 parent d01c051 commit 971a1ac

File tree

15 files changed

+404
-21
lines changed

15 files changed

+404
-21
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
540540
libc.src.math.canonicalizef16
541541
libc.src.math.ceilf16
542542
libc.src.math.copysignf16
543+
libc.src.math.expf16
543544
libc.src.math.f16add
544545
libc.src.math.f16addf
545546
libc.src.math.f16div

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
562562
libc.src.math.canonicalizef16
563563
libc.src.math.ceilf16
564564
libc.src.math.copysignf16
565+
libc.src.math.expf16
565566
libc.src.math.f16add
566567
libc.src.math.f16addf
567568
libc.src.math.f16addl

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Higher Math Functions
284284
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
285285
| erfc | | | | | | 7.12.8.2 | F.10.5.2 |
286286
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
287-
| exp | |check| | |check| | | | | 7.12.6.1 | F.10.3.1 |
287+
| exp | |check| | |check| | | |check| | | 7.12.6.1 | F.10.3.1 |
288288
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
289289
| exp10 | |check| | |check| | | | | 7.12.6.2 | F.10.3.2 |
290290
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

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

574574
FunctionSpec<"exp", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
575575
FunctionSpec<"expf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
576+
GuardedFunctionSpec<"expf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
576577

577578
FunctionSpec<"exp2", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
578579
FunctionSpec<"exp2f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ add_math_entrypoint_object(erff)
9797

9898
add_math_entrypoint_object(exp)
9999
add_math_entrypoint_object(expf)
100+
add_math_entrypoint_object(expf16)
100101

101102
add_math_entrypoint_object(exp2)
102103
add_math_entrypoint_object(exp2f)

libc/src/math/expf16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,27 @@ add_entrypoint_object(
12261226
-O3
12271227
)
12281228

1229+
add_entrypoint_object(
1230+
expf16
1231+
SRCS
1232+
expf16.cpp
1233+
HDRS
1234+
../expf16.h
1235+
DEPENDS
1236+
libc.hdr.errno_macros
1237+
libc.hdr.fenv_macros
1238+
libc.src.__support.CPP.array
1239+
libc.src.__support.FPUtil.fenv_impl
1240+
libc.src.__support.FPUtil.fp_bits
1241+
libc.src.__support.FPUtil.polyeval
1242+
libc.src.__support.FPUtil.multiply_add
1243+
libc.src.__support.FPUtil.nearest_integer
1244+
libc.src.__support.FPUtil.rounding_mode
1245+
libc.src.__support.macros.optimization
1246+
COMPILE_OPTIONS
1247+
-O3
1248+
)
1249+
12291250
add_entrypoint_object(
12301251
exp2
12311252
SRCS

libc/src/math/generic/expf16.cpp

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
//===-- Half-precision e^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/expf16.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+
24+
namespace LIBC_NAMESPACE_DECL {
25+
26+
static constexpr fputil::ExceptValues<float16, 2> EXPF16_EXCEPTS_LO = {{
27+
// (input, RZ output, RU offset, RD offset, RN offset)
28+
// x = 0x1.de4p-8, expf16(x) = 0x1.01cp+0 (RZ)
29+
{0x1f79U, 0x3c07U, 1U, 0U, 0U},
30+
// x = 0x1.73cp-6, expf16(x) = 0x1.05cp+0 (RZ)
31+
{0x25cfU, 0x3c17U, 1U, 0U, 0U},
32+
}};
33+
34+
static constexpr fputil::ExceptValues<float16, 3> EXPF16_EXCEPTS_HI = {{
35+
// (input, RZ output, RU offset, RD offset, RN offset)
36+
// x = 0x1.c34p+0, expf16(x) = 0x1.74cp+2 (RZ)
37+
{0x3f0dU, 0x45d3U, 1U, 0U, 1U},
38+
// x = -0x1.488p-5, expf16(x) = 0x1.ebcp-1 (RZ)
39+
{0xa922U, 0x3bafU, 1U, 0U, 0U},
40+
// x = -0x1.55p-5, expf16(x) = 0x1.ebp-1 (RZ)
41+
{0xa954U, 0x3bacU, 1U, 0U, 0U},
42+
}};
43+
44+
// Generated by Sollya with the following commands:
45+
// > display = hexadecimal;
46+
// > for i from -18 to 12 do print(round(exp(i), SG, RN));
47+
static constexpr cpp::array<float, 31> EXP_HI = {
48+
0x1.05a628p-26f, 0x1.639e32p-25f, 0x1.e355bcp-24f, 0x1.4875cap-22f,
49+
0x1.be6c7p-21f, 0x1.2f6054p-19f, 0x1.9c54c4p-18f, 0x1.183542p-16f,
50+
0x1.7cd79cp-15f, 0x1.02cf22p-13f, 0x1.5fc21p-12f, 0x1.de16bap-11f,
51+
0x1.44e52p-9f, 0x1.b993fep-8f, 0x1.2c155cp-6f, 0x1.97db0cp-5f,
52+
0x1.152aaap-3f, 0x1.78b564p-2f, 0x1p+0f, 0x1.5bf0a8p+1f,
53+
0x1.d8e64cp+2f, 0x1.415e5cp+4f, 0x1.b4c902p+5f, 0x1.28d38ap+7f,
54+
0x1.936dc6p+8f, 0x1.122886p+10f, 0x1.749ea8p+11f, 0x1.fa7158p+12f,
55+
0x1.5829dcp+14f, 0x1.d3c448p+15f, 0x1.3de166p+17f,
56+
};
57+
58+
// Generated by Sollya with the following commands:
59+
// > display = hexadecimal;
60+
// > for i from 0 to 7 do print(round(exp(i * 2^-3), SG, RN));
61+
static constexpr cpp::array<float, 8> EXP_MID = {
62+
0x1p+0f, 0x1.221604p+0f, 0x1.48b5e4p+0f, 0x1.747a52p+0f,
63+
0x1.a61298p+0f, 0x1.de455ep+0f, 0x1.0ef9dcp+1f, 0x1.330e58p+1f,
64+
};
65+
66+
LLVM_LIBC_FUNCTION(float16, expf16, (float16 x)) {
67+
using FPBits = fputil::FPBits<float16>;
68+
FPBits x_bits(x);
69+
70+
uint16_t x_u = x_bits.uintval();
71+
uint16_t x_abs = x_u & 0x7fffU;
72+
73+
// When 0 < |x| <= 2^(-5), or |x| >= 12, or x is NaN.
74+
if (LIBC_UNLIKELY(x_abs <= 0x2800U || x_abs >= 0x4a00U)) {
75+
// exp(NaN) = NaN
76+
if (x_bits.is_nan()) {
77+
if (x_bits.is_signaling_nan()) {
78+
fputil::raise_except_if_required(FE_INVALID);
79+
return FPBits::quiet_nan().get_val();
80+
}
81+
82+
return x;
83+
}
84+
85+
// When x >= 12.
86+
if (x_bits.is_pos() && x_abs >= 0x4a00U) {
87+
// exp(+inf) = +inf
88+
if (x_bits.is_inf())
89+
return FPBits::inf().get_val();
90+
91+
switch (fputil::quick_get_round()) {
92+
case FE_TONEAREST:
93+
case FE_UPWARD:
94+
fputil::set_errno_if_required(ERANGE);
95+
fputil::raise_except_if_required(FE_OVERFLOW);
96+
return FPBits::inf().get_val();
97+
default:
98+
return FPBits::max_normal().get_val();
99+
}
100+
}
101+
102+
// When x <= -18.
103+
if (x_u >= 0xcc80U) {
104+
// exp(-inf) = +0
105+
if (x_bits.is_inf())
106+
return FPBits::zero().get_val();
107+
108+
fputil::set_errno_if_required(ERANGE);
109+
fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
110+
111+
switch (fputil::quick_get_round()) {
112+
case FE_UPWARD:
113+
return FPBits::min_subnormal().get_val();
114+
default:
115+
return FPBits::zero().get_val();
116+
}
117+
}
118+
119+
// When 0 < |x| <= 2^(-5).
120+
if (x_abs <= 0x2800U && !x_bits.is_zero()) {
121+
if (auto r = EXPF16_EXCEPTS_LO.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
122+
return r.value();
123+
124+
float xf = x;
125+
// Degree-3 minimax polynomial generated by Sollya with the following
126+
// commands:
127+
// > display = hexadecimal;
128+
// > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
129+
// > 1 + x * P;
130+
float r =
131+
fputil::polyeval(xf, 0x1p+0f, 0x1p+0f, 0x1.0004p-1f, 0x1.555778p-3f);
132+
return static_cast<float16>(r);
133+
}
134+
}
135+
136+
if (auto r = EXPF16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
137+
return r.value();
138+
139+
// For -18 < x < 12, to compute exp(x), we perform the following range
140+
// reduction: find hi, mid, lo, such that:
141+
// x = hi + mid + lo, in which
142+
// hi is an integer,
143+
// mid * 2^3 is an integer,
144+
// -2^(-4) <= lo < 2^(-4).
145+
// In particular,
146+
// hi + mid = round(x * 2^3) * 2^(-3).
147+
// Then,
148+
// exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo).
149+
// We store exp(hi) and exp(mid) in the lookup tables EXP_HI and EXP_MID
150+
// respectively. exp(lo) is computed using a degree-3 minimax polynomial
151+
// generated by Sollya.
152+
153+
float xf = static_cast<float>(x);
154+
float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
155+
int x_hi_mid = static_cast<int>(kf);
156+
int x_hi = x_hi_mid >> 3;
157+
int x_mid = x_hi_mid & 0x7;
158+
// lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x
159+
float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf);
160+
161+
float exp_hi = EXP_HI[x_hi + 18];
162+
float exp_mid = EXP_MID[x_mid];
163+
// Degree-3 minimax polynomial generated by Sollya with the following
164+
// commands:
165+
// > display = hexadecimal;
166+
// > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-4, 2^-4]);
167+
// > 1 + x * P;
168+
float exp_lo =
169+
fputil::polyeval(lo, 0x1p+0f, 0x1p+0f, 0x1.001p-1f, 0x1.555ddep-3f);
170+
return static_cast<float16>(exp_hi * exp_mid * exp_lo);
171+
}
172+
173+
} // namespace LIBC_NAMESPACE_DECL

libc/test/UnitTest/FPMatcher.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ template <typename T> struct FPTest : public Test {
7474
static constexpr T inf = FPBits::inf(Sign::POS).get_val();
7575
static constexpr T neg_inf = FPBits::inf(Sign::NEG).get_val();
7676
static constexpr T min_normal = FPBits::min_normal().get_val();
77-
static constexpr T max_normal = FPBits::max_normal().get_val();
77+
static constexpr T max_normal = FPBits::max_normal(Sign::POS).get_val();
78+
static constexpr T neg_max_normal = FPBits::max_normal(Sign::NEG).get_val();
7879
static constexpr T min_denormal = FPBits::min_subnormal().get_val();
7980
static constexpr T max_denormal = FPBits::max_subnormal().get_val();
8081

libc/test/src/math/CMakeLists.txt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,19 @@ add_fp_unittest(
888888
libc.src.__support.FPUtil.fp_bits
889889
)
890890

891+
add_fp_unittest(
892+
exp_test
893+
NEED_MPFR
894+
SUITE
895+
libc-math-unittests
896+
SRCS
897+
exp_test.cpp
898+
DEPENDS
899+
libc.src.errno.errno
900+
libc.src.math.exp
901+
libc.src.__support.FPUtil.fp_bits
902+
)
903+
891904
add_fp_unittest(
892905
expf_test
893906
NEED_MPFR
@@ -902,16 +915,14 @@ add_fp_unittest(
902915
)
903916

904917
add_fp_unittest(
905-
exp_test
906-
NEED_MPFR
907-
SUITE
908-
libc-math-unittests
909-
SRCS
910-
exp_test.cpp
911-
DEPENDS
912-
libc.src.errno.errno
913-
libc.src.math.exp
914-
libc.src.__support.FPUtil.fp_bits
918+
expf16_test
919+
NEED_MPFR
920+
SUITE
921+
libc-math-unittests
922+
SRCS
923+
expf16_test.cpp
924+
DEPENDS
925+
libc.src.math.expf16
915926
)
916927

917928
add_fp_unittest(

libc/test/src/math/expf16_test.cpp

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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ add_perf_binary(
175175
-fno-builtin
176176
)
177177

178+
add_perf_binary(
179+
expf16_perf
180+
SRCS
181+
expf16_perf.cpp
182+
DEPENDS
183+
.single_input_single_output_diff
184+
libc.src.math.expf16
185+
COMPILE_OPTIONS
186+
-fno-builtin
187+
)
188+
178189
add_perf_binary(
179190
fabsf_perf
180191
SRCS

0 commit comments

Comments
 (0)