Skip to content

Commit 936515c

Browse files
authored
[libc][math][c23] Add exp2f16 C23 math function (#101217)
Part of #95250.
1 parent 4c6a897 commit 936515c

File tree

13 files changed

+361
-20
lines changed

13 files changed

+361
-20
lines changed

libc/config/linux/x86_64/entrypoints.txt

+1
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.exp2f16
579580
libc.src.math.expf16
580581
libc.src.math.f16add
581582
libc.src.math.f16addf

libc/docs/math/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ Higher Math Functions
290290
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
291291
| exp10m1 | | | | | | 7.12.6.3 | F.10.3.3 |
292292
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
293-
| exp2 | |check| | |check| | | | | 7.12.6.4 | F.10.3.4 |
293+
| exp2 | |check| | |check| | | |check| | | 7.12.6.4 | F.10.3.4 |
294294
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
295295
| exp2m1 | |check| | | | | | 7.12.6.5 | F.10.3.5 |
296296
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

+1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ def StdC : StandardSpec<"stdc"> {
582582

583583
FunctionSpec<"exp2", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
584584
FunctionSpec<"exp2f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
585+
GuardedFunctionSpec<"exp2f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
585586

586587
FunctionSpec<"exp2m1f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
587588

libc/src/math/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ add_math_entrypoint_object(expf16)
110110

111111
add_math_entrypoint_object(exp2)
112112
add_math_entrypoint_object(exp2f)
113+
add_math_entrypoint_object(exp2f16)
113114

114115
add_math_entrypoint_object(exp2m1f)
115116

libc/src/math/exp2f16.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for exp2f16 -----------------------*- 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_EXP2F16_H
10+
#define LLVM_LIBC_SRC_MATH_EXP2F16_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 exp2f16(float16 x);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_MATH_EXP2F16_H

libc/src/math/generic/CMakeLists.txt

+22
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,28 @@ add_entrypoint_object(
13951395
-O3
13961396
)
13971397

1398+
add_entrypoint_object(
1399+
exp2f16
1400+
SRCS
1401+
exp2f16.cpp
1402+
HDRS
1403+
../exp2f16.h
1404+
DEPENDS
1405+
libc.hdr.errno_macros
1406+
libc.hdr.fenv_macros
1407+
libc.src.__support.CPP.array
1408+
libc.src.__support.FPUtil.except_value_utils
1409+
libc.src.__support.FPUtil.fenv_impl
1410+
libc.src.__support.FPUtil.fp_bits
1411+
libc.src.__support.FPUtil.multiply_add
1412+
libc.src.__support.FPUtil.nearest_integer
1413+
libc.src.__support.FPUtil.polyeval
1414+
libc.src.__support.FPUtil.rounding_mode
1415+
libc.src.__support.macros.optimization
1416+
COMPILE_OPTIONS
1417+
-O3
1418+
)
1419+
13981420
add_entrypoint_object(
13991421
exp2m1f
14001422
SRCS

libc/src/math/generic/exp2f16.cpp

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//===-- Half-precision 2^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/exp2f16.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, 3> EXP2F16_EXCEPTS = {{
27+
// (input, RZ output, RU offset, RD offset, RN offset)
28+
// x = 0x1.714p-11, exp2f16(x) = 0x1p+0 (RZ)
29+
{0x11c5U, 0x3c00U, 1U, 0U, 1U},
30+
// x = -0x1.558p-4, exp2f16(x) = 0x1.e34p-1 (RZ)
31+
{0xad56U, 0x3b8dU, 1U, 0U, 0U},
32+
// x = -0x1.d5cp-4, exp2f16(x) = 0x1.d8cp-1 (RZ)
33+
{0xaf57U, 0x3b63U, 1U, 0U, 0U},
34+
}};
35+
36+
// Generated by Sollya with the following commands:
37+
// > display = hexadecimal;
38+
// > for i from 0 to 7 do printsingle(round(2^(i * 2^-3), SG, RN));
39+
static constexpr cpp::array<uint32_t, 8> EXP2_MID_BITS = {
40+
0x3f80'0000U, 0x3f8b'95c2U, 0x3f98'37f0U, 0x3fa5'fed7U,
41+
0x3fb5'04f3U, 0x3fc5'672aU, 0x3fd7'44fdU, 0x3fea'c0c7U,
42+
};
43+
44+
LLVM_LIBC_FUNCTION(float16, exp2f16, (float16 x)) {
45+
using FPBits = fputil::FPBits<float16>;
46+
FPBits x_bits(x);
47+
48+
uint16_t x_u = x_bits.uintval();
49+
uint16_t x_abs = x_u & 0x7fffU;
50+
51+
// When |x| >= 16, or x is NaN.
52+
if (LIBC_UNLIKELY(x_abs >= 0x4c00U)) {
53+
// exp2(NaN) = NaN
54+
if (x_bits.is_nan()) {
55+
if (x_bits.is_signaling_nan()) {
56+
fputil::raise_except_if_required(FE_INVALID);
57+
return FPBits::quiet_nan().get_val();
58+
}
59+
60+
return x;
61+
}
62+
63+
// When x >= 16.
64+
if (x_bits.is_pos()) {
65+
// exp2(+inf) = +inf
66+
if (x_bits.is_inf())
67+
return FPBits::inf().get_val();
68+
69+
switch (fputil::quick_get_round()) {
70+
case FE_TONEAREST:
71+
case FE_UPWARD:
72+
fputil::set_errno_if_required(ERANGE);
73+
fputil::raise_except_if_required(FE_OVERFLOW);
74+
return FPBits::inf().get_val();
75+
default:
76+
return FPBits::max_normal().get_val();
77+
}
78+
}
79+
80+
// When x <= -25.
81+
if (x_u >= 0xce40U) {
82+
// exp2(-inf) = +0
83+
if (x_bits.is_inf())
84+
return FPBits::zero().get_val();
85+
86+
fputil::set_errno_if_required(ERANGE);
87+
fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
88+
89+
if (fputil::fenv_is_round_up())
90+
return FPBits::min_subnormal().get_val();
91+
return FPBits::zero().get_val();
92+
}
93+
}
94+
95+
if (auto r = EXP2F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
96+
return r.value();
97+
98+
// For -25 < x < 16, to compute 2^x, we perform the following range reduction:
99+
// find hi, mid, lo, such that:
100+
// x = hi + mid + lo, in which
101+
// hi is an integer,
102+
// mid * 2^3 is an integer,
103+
// -2^(-4) <= lo < 2^(-4).
104+
// In particular,
105+
// hi + mid = round(x * 2^3) * 2^(-3).
106+
// Then,
107+
// 2^x = 2^(hi + mid + lo) = 2^hi * 2^mid * 2^lo.
108+
// We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid
109+
// by adding hi to the exponent field of 2^mid. 2^lo is computed using a
110+
// degree-3 minimax polynomial generated by Sollya.
111+
112+
float xf = x;
113+
float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
114+
int x_hi_mid = static_cast<int>(kf);
115+
int x_hi = x_hi_mid >> 3;
116+
int x_mid = x_hi_mid & 0x7;
117+
// lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x
118+
float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf);
119+
120+
uint32_t exp2_hi_mid_bits =
121+
EXP2_MID_BITS[x_mid] +
122+
static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
123+
float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
124+
// Degree-3 minimax polynomial generated by Sollya with the following
125+
// commands:
126+
// > display = hexadecimal;
127+
// > P = fpminimax((2^x - 1)/x, 2, [|SG...|], [-2^-4, 2^-4]);
128+
// > 1 + x * P;
129+
float exp2_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.62e43p-1f, 0x1.ec0aa6p-3f,
130+
0x1.c6b4a6p-5f);
131+
return static_cast<float16>(exp2_hi_mid * exp2_lo);
132+
}
133+
134+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/math/CMakeLists.txt

+21-10
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,19 @@ add_fp_unittest(
939939
libc.src.math.expf16
940940
)
941941

942+
add_fp_unittest(
943+
exp2_test
944+
NEED_MPFR
945+
SUITE
946+
libc-math-unittests
947+
SRCS
948+
exp2_test.cpp
949+
DEPENDS
950+
libc.src.errno.errno
951+
libc.src.math.exp2
952+
libc.src.__support.FPUtil.fp_bits
953+
)
954+
942955
add_fp_unittest(
943956
exp2f_test
944957
NEED_MPFR
@@ -953,16 +966,14 @@ add_fp_unittest(
953966
)
954967

955968
add_fp_unittest(
956-
exp2_test
957-
NEED_MPFR
958-
SUITE
959-
libc-math-unittests
960-
SRCS
961-
exp2_test.cpp
962-
DEPENDS
963-
libc.src.errno.errno
964-
libc.src.math.exp2
965-
libc.src.__support.FPUtil.fp_bits
969+
exp2f16_test
970+
NEED_MPFR
971+
SUITE
972+
libc-math-unittests
973+
SRCS
974+
exp2f16_test.cpp
975+
DEPENDS
976+
libc.src.math.exp2f16
966977
)
967978

968979
add_fp_unittest(

libc/test/src/math/exp2f16_test.cpp

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

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

+11
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ add_perf_binary(
164164
-fno-builtin
165165
)
166166

167+
add_perf_binary(
168+
exp2f16_perf
169+
SRCS
170+
exp2f16_perf.cpp
171+
DEPENDS
172+
.single_input_single_output_diff
173+
libc.src.math.exp2f16
174+
COMPILE_OPTIONS
175+
-fno-builtin
176+
)
177+
167178
add_perf_binary(
168179
expf_perf
169180
SRCS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Performance test for exp2f16 --------------------------------------===//
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 "SingleInputSingleOutputPerf.h"
10+
11+
#include "src/math/exp2f16.h"
12+
13+
// LLVM libc might be the only libc implementation with support for float16 math
14+
// functions currently. We can't compare our float16 functions against the
15+
// system libc, so we compare them against this placeholder function.
16+
static float16 placeholderf16(float16 x) { return x; }
17+
18+
int main() {
19+
SINGLE_INPUT_SINGLE_OUTPUT_PERF_EX(float16, LIBC_NAMESPACE::exp2f16,
20+
::placeholderf16, 20'000,
21+
"exp2f16_perf.log")
22+
}

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

+21-9
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,18 @@ add_fp_unittest(
10291029
libc.src.math.expf16
10301030
)
10311031

1032+
add_fp_unittest(
1033+
exp2_test
1034+
SUITE
1035+
libc-math-smoke-tests
1036+
SRCS
1037+
exp2_test.cpp
1038+
DEPENDS
1039+
libc.src.errno.errno
1040+
libc.src.math.exp2
1041+
libc.src.__support.FPUtil.fp_bits
1042+
)
1043+
10321044
add_fp_unittest(
10331045
exp2f_test
10341046
SUITE
@@ -1042,15 +1054,15 @@ add_fp_unittest(
10421054
)
10431055

10441056
add_fp_unittest(
1045-
exp2_test
1046-
SUITE
1047-
libc-math-smoke-tests
1048-
SRCS
1049-
exp2_test.cpp
1050-
DEPENDS
1051-
libc.src.errno.errno
1052-
libc.src.math.exp2
1053-
libc.src.__support.FPUtil.fp_bits
1057+
exp2f16_test
1058+
SUITE
1059+
libc-math-smoke-tests
1060+
SRCS
1061+
exp2f16_test.cpp
1062+
DEPENDS
1063+
libc.hdr.fenv_macros
1064+
libc.src.errno.errno
1065+
libc.src.math.exp2f16
10541066
)
10551067

10561068
add_fp_unittest(

0 commit comments

Comments
 (0)