Skip to content

Commit a3629e4

Browse files
committed
[libc][math][c23] Add exp2f16 C23 math function
1 parent cc7aef9 commit a3629e4

File tree

13 files changed

+361
-20
lines changed

13 files changed

+361
-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.exp2f16
567568
libc.src.math.expf16
568569
libc.src.math.f16add
569570
libc.src.math.f16addf

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
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

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

578578
FunctionSpec<"exp2", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
579579
FunctionSpec<"exp2f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
580+
GuardedFunctionSpec<"exp2f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
580581

581582
FunctionSpec<"exp2m1f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
582583

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ add_math_entrypoint_object(expf16)
101101

102102
add_math_entrypoint_object(exp2)
103103
add_math_entrypoint_object(exp2f)
104+
add_math_entrypoint_object(exp2f16)
104105

105106
add_math_entrypoint_object(exp2m1f)
106107

libc/src/math/exp2f16.h

Lines changed: 21 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,28 @@ add_entrypoint_object(
13051305
-O3
13061306
)
13071307

1308+
add_entrypoint_object(
1309+
exp2f16
1310+
SRCS
1311+
exp2f16.cpp
1312+
HDRS
1313+
../exp2f16.h
1314+
DEPENDS
1315+
libc.hdr.errno_macros
1316+
libc.hdr.fenv_macros
1317+
libc.src.__support.CPP.array
1318+
libc.src.__support.FPUtil.except_value_utils
1319+
libc.src.__support.FPUtil.fenv_impl
1320+
libc.src.__support.FPUtil.fp_bits
1321+
libc.src.__support.FPUtil.multiply_add
1322+
libc.src.__support.FPUtil.nearest_integer
1323+
libc.src.__support.FPUtil.polyeval
1324+
libc.src.__support.FPUtil.rounding_mode
1325+
libc.src.__support.macros.optimization
1326+
COMPILE_OPTIONS
1327+
-O3
1328+
)
1329+
13081330
add_entrypoint_object(
13091331
exp2m1f
13101332
SRCS

libc/src/math/generic/exp2f16.cpp

Lines changed: 134 additions & 0 deletions
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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,19 @@ add_fp_unittest(
925925
libc.src.math.expf16
926926
)
927927

928+
add_fp_unittest(
929+
exp2_test
930+
NEED_MPFR
931+
SUITE
932+
libc-math-unittests
933+
SRCS
934+
exp2_test.cpp
935+
DEPENDS
936+
libc.src.errno.errno
937+
libc.src.math.exp2
938+
libc.src.__support.FPUtil.fp_bits
939+
)
940+
928941
add_fp_unittest(
929942
exp2f_test
930943
NEED_MPFR
@@ -939,16 +952,14 @@ add_fp_unittest(
939952
)
940953

941954
add_fp_unittest(
942-
exp2_test
943-
NEED_MPFR
944-
SUITE
945-
libc-math-unittests
946-
SRCS
947-
exp2_test.cpp
948-
DEPENDS
949-
libc.src.errno.errno
950-
libc.src.math.exp2
951-
libc.src.__support.FPUtil.fp_bits
955+
exp2f16_test
956+
NEED_MPFR
957+
SUITE
958+
libc-math-unittests
959+
SRCS
960+
exp2f16_test.cpp
961+
DEPENDS
962+
libc.src.math.exp2f16
952963
)
953964

954965
add_fp_unittest(

libc/test/src/math/exp2f16_test.cpp

Lines changed: 40 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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
Lines changed: 22 additions & 0 deletions
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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,18 @@ add_fp_unittest(
977977
libc.src.math.expf16
978978
)
979979

980+
add_fp_unittest(
981+
exp2_test
982+
SUITE
983+
libc-math-smoke-tests
984+
SRCS
985+
exp2_test.cpp
986+
DEPENDS
987+
libc.src.errno.errno
988+
libc.src.math.exp2
989+
libc.src.__support.FPUtil.fp_bits
990+
)
991+
980992
add_fp_unittest(
981993
exp2f_test
982994
SUITE
@@ -990,15 +1002,15 @@ add_fp_unittest(
9901002
)
9911003

9921004
add_fp_unittest(
993-
exp2_test
994-
SUITE
995-
libc-math-smoke-tests
996-
SRCS
997-
exp2_test.cpp
998-
DEPENDS
999-
libc.src.errno.errno
1000-
libc.src.math.exp2
1001-
libc.src.__support.FPUtil.fp_bits
1005+
exp2f16_test
1006+
SUITE
1007+
libc-math-smoke-tests
1008+
SRCS
1009+
exp2f16_test.cpp
1010+
DEPENDS
1011+
libc.hdr.fenv_macros
1012+
libc.src.errno.errno
1013+
libc.src.math.exp2f16
10021014
)
10031015

10041016
add_fp_unittest(

0 commit comments

Comments
 (0)