Skip to content

Commit 59338ad

Browse files
authored
[libc][math][c23] Add exp10f16 C23 math function (#101588)
Part of #95250.
1 parent fc73736 commit 59338ad

File tree

15 files changed

+437
-28
lines changed

15 files changed

+437
-28
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
581581
libc.src.math.canonicalizef16
582582
libc.src.math.ceilf16
583583
libc.src.math.copysignf16
584+
libc.src.math.exp10f16
584585
libc.src.math.exp2f16
585586
libc.src.math.expf16
586587
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
@@ -597,6 +597,7 @@ def StdC : StandardSpec<"stdc"> {
597597

598598
FunctionSpec<"exp10", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
599599
FunctionSpec<"exp10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
600+
GuardedFunctionSpec<"exp10f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
600601

601602
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
602603
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ add_entrypoint_object(
14021402
HDRS
14031403
../exp2f16.h
14041404
DEPENDS
1405+
.expxf16
14051406
libc.hdr.errno_macros
14061407
libc.hdr.fenv_macros
14071408
libc.src.__support.CPP.array
@@ -1497,6 +1498,30 @@ add_entrypoint_object(
14971498
-O3
14981499
)
14991500

1501+
add_entrypoint_object(
1502+
exp10f16
1503+
SRCS
1504+
exp10f16.cpp
1505+
HDRS
1506+
../exp10f16.h
1507+
DEPENDS
1508+
.expxf16
1509+
libc.hdr.errno_macros
1510+
libc.hdr.fenv_macros
1511+
libc.src.__support.CPP.array
1512+
libc.src.__support.FPUtil.except_value_utils
1513+
libc.src.__support.FPUtil.fenv_impl
1514+
libc.src.__support.FPUtil.fp_bits
1515+
libc.src.__support.FPUtil.multiply_add
1516+
libc.src.__support.FPUtil.nearest_integer
1517+
libc.src.__support.FPUtil.polyeval
1518+
libc.src.__support.FPUtil.rounding_mode
1519+
libc.src.__support.macros.optimization
1520+
libc.src.__support.macros.properties.cpu_features
1521+
COMPILE_OPTIONS
1522+
-O3
1523+
)
1524+
15001525
add_entrypoint_object(
15011526
expm1
15021527
SRCS
@@ -4846,3 +4871,11 @@ add_entrypoint_object(
48464871
COMPILE_OPTIONS
48474872
-O3
48484873
)
4874+
4875+
add_header_library(
4876+
expxf16
4877+
HDRS
4878+
expxf16.h
4879+
DEPENDS
4880+
libc.src.__support.CPP.array
4881+
)

libc/src/math/generic/exp10f16.cpp

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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 "expxf16.h"
11+
#include "hdr/errno_macros.h"
12+
#include "hdr/fenv_macros.h"
13+
#include "src/__support/CPP/array.h"
14+
#include "src/__support/FPUtil/FEnvImpl.h"
15+
#include "src/__support/FPUtil/FPBits.h"
16+
#include "src/__support/FPUtil/PolyEval.h"
17+
#include "src/__support/FPUtil/except_value_utils.h"
18+
#include "src/__support/FPUtil/multiply_add.h"
19+
#include "src/__support/FPUtil/nearest_integer.h"
20+
#include "src/__support/FPUtil/rounding_mode.h"
21+
#include "src/__support/common.h"
22+
#include "src/__support/macros/config.h"
23+
#include "src/__support/macros/optimization.h"
24+
#include "src/__support/macros/properties/cpu_features.h"
25+
26+
namespace LIBC_NAMESPACE_DECL {
27+
28+
#ifdef LIBC_TARGET_CPU_HAS_FMA
29+
static constexpr size_t N_EXP10F16_EXCEPTS = 5;
30+
#else
31+
static constexpr size_t N_EXP10F16_EXCEPTS = 8;
32+
#endif
33+
34+
static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS>
35+
EXP10F16_EXCEPTS = {{
36+
// x = 0x1.8f4p-2, exp10f16(x) = 0x1.3ap+1 (RZ)
37+
{0x363dU, 0x40e8U, 1U, 0U, 1U},
38+
// x = 0x1.95cp-2, exp10f16(x) = 0x1.3ecp+1 (RZ)
39+
{0x3657U, 0x40fbU, 1U, 0U, 0U},
40+
// x = -0x1.018p-4, exp10f16(x) = 0x1.bbp-1 (RZ)
41+
{0xac06U, 0x3aecU, 1U, 0U, 0U},
42+
// x = -0x1.c28p+0, exp10f16(x) = 0x1.1ccp-6 (RZ)
43+
{0xbf0aU, 0x2473U, 1U, 0U, 0U},
44+
// x = -0x1.e1cp+1, exp10f16(x) = 0x1.694p-13 (RZ)
45+
{0xc387U, 0x09a5U, 1U, 0U, 0U},
46+
#ifndef LIBC_TARGET_CPU_HAS_FMA
47+
// x = 0x1.0cp+1, exp10f16(x) = 0x1.f04p+6 (RZ)
48+
{0x4030U, 0x57c1U, 1U, 0U, 1U},
49+
// x = 0x1.1b8p+1, exp10f16(x) = 0x1.47cp+7 (RZ)
50+
{0x406eU, 0x591fU, 1U, 0U, 1U},
51+
// x = 0x1.1b8p+2, exp10f16(x) = 0x1.a4p+14 (RZ)
52+
{0x446eU, 0x7690U, 1U, 0U, 1U},
53+
#endif
54+
}};
55+
56+
// Generated by Sollya with the following commands:
57+
// > display = hexadecimal;
58+
// > round(log2(10), SG, RN);
59+
static constexpr float LOG2F_10 = 0x1.a934fp+1f;
60+
61+
// Generated by Sollya with the following commands:
62+
// > display = hexadecimal;
63+
// > round(log10(2), SG, RN);
64+
static constexpr float LOG10F_2 = 0x1.344136p-2f;
65+
66+
LLVM_LIBC_FUNCTION(float16, exp10f16, (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 |x| >= 5, or x is NaN.
74+
if (LIBC_UNLIKELY(x_abs >= 0x4500U)) {
75+
// exp10(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 >= 5.
86+
if (x_bits.is_pos()) {
87+
// exp10(+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 <= -8.
103+
if (x_u >= 0xc800U) {
104+
// exp10(-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+
if (fputil::fenv_is_round_up())
112+
return FPBits::min_subnormal().get_val();
113+
return FPBits::zero().get_val();
114+
}
115+
}
116+
117+
// When x is 1, 2, 3, or 4. These are hard-to-round cases with exact results.
118+
if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) {
119+
switch (x_u) {
120+
case 0x3c00U: // x = 1.0f16
121+
return static_cast<float16>(10.0);
122+
case 0x4000U: // x = 2.0f16
123+
return static_cast<float16>(100.0);
124+
case 0x4200U: // x = 3.0f16
125+
return static_cast<float16>(1'000.0);
126+
case 0x4400U: // x = 4.0f16
127+
return static_cast<float16>(10'000.0);
128+
}
129+
}
130+
131+
if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
132+
return r.value();
133+
134+
// For -8 < x < 5, to compute 10^x, we perform the following range reduction:
135+
// find hi, mid, lo, such that:
136+
// x = (hi + mid) * log2(10) + lo, in which
137+
// hi is an integer,
138+
// mid * 2^3 is an integer,
139+
// -2^(-4) <= lo < 2^(-4).
140+
// In particular,
141+
// hi + mid = round(x * 2^3) * 2^(-3).
142+
// Then,
143+
// 10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo
144+
// We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid
145+
// by adding hi to the exponent field of 2^mid. 10^lo is computed using a
146+
// degree-4 minimax polynomial generated by Sollya.
147+
148+
float xf = x;
149+
float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f));
150+
int x_hi_mid = static_cast<int>(kf);
151+
int x_hi = x_hi_mid >> 3;
152+
int x_mid = x_hi_mid & 0x7;
153+
// lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x
154+
float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf);
155+
156+
uint32_t exp2_hi_mid_bits =
157+
EXP2_MID_BITS[x_mid] +
158+
static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
159+
float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
160+
// Degree-4 minimax polynomial generated by Sollya with the following
161+
// commands:
162+
// > display = hexadecimal;
163+
// > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]);
164+
// > 1 + x * P;
165+
float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f,
166+
0x1.04b434p+1f, 0x1.2bcf9ep+0f);
167+
return static_cast<float16>(exp2_hi_mid * exp10_lo);
168+
}
169+
170+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/generic/exp2f16.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/math/exp2f16.h"
10+
#include "expxf16.h"
1011
#include "hdr/errno_macros.h"
1112
#include "hdr/fenv_macros.h"
1213
#include "src/__support/CPP/array.h"
@@ -33,14 +34,6 @@ static constexpr fputil::ExceptValues<float16, 3> EXP2F16_EXCEPTS = {{
3334
{0xaf57U, 0x3b63U, 1U, 0U, 0U},
3435
}};
3536

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-
4437
LLVM_LIBC_FUNCTION(float16, exp2f16, (float16 x)) {
4538
using FPBits = fputil::FPBits<float16>;
4639
FPBits x_bits(x);

libc/src/math/generic/expxf16.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- Common utilities for half-precision exponential functions ---------===//
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_GENERIC_EXPXF16_H
10+
#define LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H
11+
12+
#include "src/__support/CPP/array.h"
13+
#include "src/__support/macros/config.h"
14+
#include <stdint.h>
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
// Generated by Sollya with the following commands:
19+
// > display = hexadecimal;
20+
// > for i from 0 to 7 do printsingle(round(2^(i * 2^-3), SG, RN));
21+
constexpr cpp::array<uint32_t, 8> EXP2_MID_BITS = {
22+
0x3f80'0000U, 0x3f8b'95c2U, 0x3f98'37f0U, 0x3fa5'fed7U,
23+
0x3fb5'04f3U, 0x3fc5'672aU, 0x3fd7'44fdU, 0x3fea'c0c7U,
24+
};
25+
26+
} // namespace LIBC_NAMESPACE_DECL
27+
28+
#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H

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(

0 commit comments

Comments
 (0)