Skip to content

Commit 95c24cb

Browse files
authored
[libc][math][c23] Add exp10m1f16 C23 math function (llvm#105706)
Part of llvm#95250.
1 parent 7b4c8b3 commit 95c24cb

File tree

16 files changed

+467
-45
lines changed

16 files changed

+467
-45
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
522522
libc.src.math.ceilf16
523523
libc.src.math.copysignf16
524524
libc.src.math.exp10f16
525+
libc.src.math.exp10m1f16
525526
libc.src.math.exp2f16
526527
libc.src.math.expf16
527528
libc.src.math.f16add

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
611611
libc.src.math.ceilf16
612612
libc.src.math.copysignf16
613613
libc.src.math.exp10f16
614+
libc.src.math.exp10m1f16
614615
libc.src.math.exp2f16
615616
libc.src.math.exp2m1f16
616617
libc.src.math.expf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ Higher Math Functions
292292
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
293293
| exp10 | |check| | |check| | | |check| | | 7.12.6.2 | F.10.3.2 |
294294
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
295-
| exp10m1 | | | | | | 7.12.6.3 | F.10.3.3 |
295+
| exp10m1 | | | | |check| | | 7.12.6.3 | F.10.3.3 |
296296
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
297297
| exp2 | |check| | |check| | | |check| | | 7.12.6.4 | F.10.3.4 |
298298
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ def StdC : StandardSpec<"stdc"> {
692692
FunctionSpec<"exp10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
693693
GuardedFunctionSpec<"exp10f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
694694

695+
GuardedFunctionSpec<"exp10m1f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
696+
695697
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
696698
FunctionSpec<"remainderf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
697699
FunctionSpec<"remainderl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ add_math_entrypoint_object(exp10)
127127
add_math_entrypoint_object(exp10f)
128128
add_math_entrypoint_object(exp10f16)
129129

130+
add_math_entrypoint_object(exp10m1f16)
131+
130132
add_math_entrypoint_object(expm1)
131133
add_math_entrypoint_object(expm1f)
132134
add_math_entrypoint_object(expm1f16)

libc/src/math/exp10m1f16.h

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,29 @@ add_entrypoint_object(
16561656
-O3
16571657
)
16581658

1659+
add_entrypoint_object(
1660+
exp10m1f16
1661+
SRCS
1662+
exp10m1f16.cpp
1663+
HDRS
1664+
../exp10m1f16.h
1665+
DEPENDS
1666+
.expxf16
1667+
libc.hdr.errno_macros
1668+
libc.hdr.fenv_macros
1669+
libc.src.__support.FPUtil.cast
1670+
libc.src.__support.FPUtil.except_value_utils
1671+
libc.src.__support.FPUtil.fenv_impl
1672+
libc.src.__support.FPUtil.fp_bits
1673+
libc.src.__support.FPUtil.multiply_add
1674+
libc.src.__support.FPUtil.polyeval
1675+
libc.src.__support.FPUtil.rounding_mode
1676+
libc.src.__support.macros.optimization
1677+
libc.src.__support.macros.properties.cpu_features
1678+
COMPILE_OPTIONS
1679+
-O3
1680+
)
1681+
16591682
add_entrypoint_object(
16601683
expm1
16611684
SRCS

libc/src/math/generic/exp10f16.cpp

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS>
5454
#endif
5555
}};
5656

57-
// Generated by Sollya with the following commands:
58-
// > display = hexadecimal;
59-
// > round(log2(10), SG, RN);
60-
static constexpr float LOG2F_10 = 0x1.a934fp+1f;
61-
62-
// Generated by Sollya with the following commands:
63-
// > display = hexadecimal;
64-
// > round(log10(2), SG, RN);
65-
static constexpr float LOG10F_2 = 0x1.344136p-2f;
66-
6757
LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) {
6858
using FPBits = fputil::FPBits<float16>;
6959
FPBits x_bits(x);
@@ -132,40 +122,9 @@ LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) {
132122
if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
133123
return r.value();
134124

135-
// For -8 < x < 5, to compute 10^x, we perform the following range reduction:
136-
// find hi, mid, lo, such that:
137-
// x = (hi + mid) * log2(10) + lo, in which
138-
// hi is an integer,
139-
// mid * 2^3 is an integer,
140-
// -2^(-4) <= lo < 2^(-4).
141-
// In particular,
142-
// hi + mid = round(x * 2^3) * 2^(-3).
143-
// Then,
144-
// 10^x = 10^(hi + mid + lo) = 2^((hi + mid) * log2(10)) + 10^lo
145-
// We store 2^mid in the lookup table EXP2_MID_BITS, and compute 2^hi * 2^mid
146-
// by adding hi to the exponent field of 2^mid. 10^lo is computed using a
147-
// degree-4 minimax polynomial generated by Sollya.
148-
149-
float xf = x;
150-
float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f));
151-
int x_hi_mid = static_cast<int>(kf);
152-
int x_hi = x_hi_mid >> 3;
153-
int x_mid = x_hi_mid & 0x7;
154-
// lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x
155-
float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf);
156-
157-
uint32_t exp2_hi_mid_bits =
158-
EXP2_MID_BITS[x_mid] +
159-
static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
160-
float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
161-
// Degree-4 minimax polynomial generated by Sollya with the following
162-
// commands:
163-
// > display = hexadecimal;
164-
// > P = fpminimax((10^x - 1)/x, 3, [|SG...|], [-2^-4, 2^-4]);
165-
// > 1 + x * P;
166-
float exp10_lo = fputil::polyeval(lo, 0x1p+0f, 0x1.26bb14p+1f, 0x1.53526p+1f,
167-
0x1.04b434p+1f, 0x1.2bcf9ep+0f);
168-
return fputil::cast<float16>(exp2_hi_mid * exp10_lo);
125+
// 10^x = 2^((hi + mid) * log2(10)) * 10^lo
126+
auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x);
127+
return static_cast<float16>(exp2_hi_mid * exp10_lo);
169128
}
170129

171130
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/generic/exp10m1f16.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//===-- Half-precision 10^x - 1 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/exp10m1f16.h"
10+
#include "expxf16.h"
11+
#include "hdr/errno_macros.h"
12+
#include "hdr/fenv_macros.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/cast.h"
17+
#include "src/__support/FPUtil/except_value_utils.h"
18+
#include "src/__support/FPUtil/multiply_add.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 fputil::ExceptValues<float16, 3> EXP10M1F16_EXCEPTS_LO = {{
28+
// (input, RZ output, RU offset, RD offset, RN offset)
29+
// x = 0x1.5c4p-4, exp10m1f16(x) = 0x1.bacp-3 (RZ)
30+
{0x2d71U, 0x32ebU, 1U, 0U, 0U},
31+
// x = -0x1.5ep-13, exp10m1f16(x) = -0x1.92cp-12 (RZ)
32+
{0x8978U, 0x8e4bU, 0U, 1U, 0U},
33+
// x = -0x1.e2p-10, exp10m1f16(x) = -0x1.14cp-8 (RZ)
34+
{0x9788U, 0x9c53U, 0U, 1U, 0U},
35+
}};
36+
37+
#ifdef LIBC_TARGET_CPU_HAS_FMA
38+
static constexpr size_t N_EXP10M1F16_EXCEPTS_HI = 3;
39+
#else
40+
static constexpr size_t N_EXP10M1F16_EXCEPTS_HI = 6;
41+
#endif
42+
43+
static constexpr fputil::ExceptValues<float16, N_EXP10M1F16_EXCEPTS_HI>
44+
EXP10M1F16_EXCEPTS_HI = {{
45+
// (input, RZ output, RU offset, RD offset, RN offset)
46+
// x = 0x1.8f4p-2, exp10m1f16(x) = 0x1.744p+0 (RZ)
47+
{0x363dU, 0x3dd1U, 1U, 0U, 0U},
48+
// x = 0x1.95cp-2, exp10m1f16(x) = 0x1.7d8p+0 (RZ)
49+
{0x3657U, 0x3df6U, 1U, 0U, 0U},
50+
// x = 0x1.d04p-2, exp10m1f16(x) = 0x1.d7p+0 (RZ)
51+
{0x3741U, 0x3f5cU, 1U, 0U, 1U},
52+
#ifndef LIBC_TARGET_CPU_HAS_FMA
53+
// x = 0x1.0cp+1, exp10m1f16(x) = 0x1.ec4p+6 (RZ)
54+
{0x4030U, 0x57b1U, 1U, 0U, 1U},
55+
// x = 0x1.1b8p+1, exp10m1f16(x) = 0x1.45cp+7 (RZ)
56+
{0x406eU, 0x5917U, 1U, 0U, 1U},
57+
// x = 0x1.2f4p+2, exp10m1f16(x) = 0x1.ab8p+15 (RZ)
58+
{0x44bdU, 0x7aaeU, 1U, 0U, 1U},
59+
#endif
60+
}};
61+
62+
LLVM_LIBC_FUNCTION(float16, exp10m1f16, (float16 x)) {
63+
using FPBits = fputil::FPBits<float16>;
64+
FPBits x_bits(x);
65+
66+
uint16_t x_u = x_bits.uintval();
67+
uint16_t x_abs = x_u & 0x7fffU;
68+
69+
// When |x| <= 2^(-3), or |x| >= 11 * log10(2), or x is NaN.
70+
if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x429fU)) {
71+
// exp10m1(NaN) = NaN
72+
if (x_bits.is_nan()) {
73+
if (x_bits.is_signaling_nan()) {
74+
fputil::raise_except_if_required(FE_INVALID);
75+
return FPBits::quiet_nan().get_val();
76+
}
77+
78+
return x;
79+
}
80+
81+
// When x >= 16 * log10(2).
82+
if (x_u >= 0x44d1U && x_bits.is_pos()) {
83+
// exp10m1(+inf) = +inf
84+
if (x_bits.is_inf())
85+
return FPBits::inf().get_val();
86+
87+
switch (fputil::quick_get_round()) {
88+
case FE_TONEAREST:
89+
case FE_UPWARD:
90+
fputil::set_errno_if_required(ERANGE);
91+
fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
92+
return FPBits::inf().get_val();
93+
default:
94+
return FPBits::max_normal().get_val();
95+
}
96+
}
97+
98+
// When x < -11 * log10(2).
99+
if (x_u > 0xc29fU) {
100+
// exp10m1(-inf) = -1
101+
if (x_bits.is_inf())
102+
return FPBits::one(Sign::NEG).get_val();
103+
104+
// When x >= -0x1.ce4p+1, round(10^x - 1, HP, RN) = -0x1.ffcp-1.
105+
if (x_u <= 0xc339U) {
106+
return fputil::round_result_slightly_down(
107+
fputil::cast<float16>(-0x1.ffcp-1));
108+
}
109+
110+
// When x < -0x1.ce4p+1, round(10^x - 1, HP, RN) = -1.
111+
switch (fputil::quick_get_round()) {
112+
case FE_TONEAREST:
113+
case FE_DOWNWARD:
114+
return FPBits::one(Sign::NEG).get_val();
115+
default:
116+
return fputil::cast<float16>(-0x1.ffcp-1);
117+
}
118+
}
119+
120+
// When |x| <= 2^(-3).
121+
if (x_abs <= 0x3000U) {
122+
if (auto r = EXP10M1F16_EXCEPTS_LO.lookup(x_u);
123+
LIBC_UNLIKELY(r.has_value()))
124+
return r.value();
125+
126+
float xf = x;
127+
// Degree-5 minimax polynomial generated by Sollya with the following
128+
// commands:
129+
// > display = hexadecimal;
130+
// > P = fpminimax((10^x - 1)/x, 4, [|SG...|], [-2^-3, 2^-3]);
131+
// > x * P;
132+
return fputil::cast<float16>(
133+
xf * fputil::polyeval(xf, 0x1.26bb1cp+1f, 0x1.5351c8p+1f,
134+
0x1.04704p+1f, 0x1.2ce084p+0f, 0x1.14a6bep-1f));
135+
}
136+
}
137+
138+
// When x is 1, 2, or 3. These are hard-to-round cases with exact results.
139+
// 10^4 - 1 = 9'999 is not exactly representable as a float16, but luckily the
140+
// polynomial approximation gives the correct result for x = 4 in all
141+
// rounding modes.
142+
if (LIBC_UNLIKELY((x_u & ~(0x3c00U | 0x4000U | 0x4200U | 0x4400U)) == 0)) {
143+
switch (x_u) {
144+
case 0x3c00U: // x = 1.0f16
145+
return fputil::cast<float16>(9.0);
146+
case 0x4000U: // x = 2.0f16
147+
return fputil::cast<float16>(99.0);
148+
case 0x4200U: // x = 3.0f16
149+
return fputil::cast<float16>(999.0);
150+
}
151+
}
152+
153+
if (auto r = EXP10M1F16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
154+
return r.value();
155+
156+
// exp10(x) = exp2((hi + mid) * log2(10)) * exp10(lo)
157+
auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x);
158+
// exp10m1(x) = exp2((hi + mid) * log2(lo)) * exp10(lo) - 1
159+
return fputil::cast<float16>(
160+
fputil::multiply_add(exp2_hi_mid, exp10_lo, -1.0f));
161+
}
162+
163+
} // namespace LIBC_NAMESPACE_DECL

libc/src/math/generic/expxf16.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,53 @@ LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) {
127127
return {exp2_hi_mid, exp2_lo};
128128
}
129129

130+
// Generated by Sollya with the following commands:
131+
// > display = hexadecimal;
132+
// > round(log2(10), SG, RN);
133+
static constexpr float LOG2F_10 = 0x1.a934fp+1f;
134+
135+
// Generated by Sollya with the following commands:
136+
// > display = hexadecimal;
137+
// > round(log10(2), SG, RN);
138+
static constexpr float LOG10F_2 = 0x1.344136p-2f;
139+
140+
LIBC_INLINE ExpRangeReduction exp10_range_reduction(float16 x) {
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 {exp2_hi_mid, exp10_lo};
175+
}
176+
130177
} // namespace LIBC_NAMESPACE_DECL
131178

132179
#endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H

libc/test/src/math/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,17 @@ add_fp_unittest(
10621062
libc.src.math.exp10f16
10631063
)
10641064

1065+
add_fp_unittest(
1066+
exp10m1f16_test
1067+
NEED_MPFR
1068+
SUITE
1069+
libc-math-unittests
1070+
SRCS
1071+
exp10m1f16_test.cpp
1072+
DEPENDS
1073+
libc.src.math.exp10m1f16
1074+
)
1075+
10651076
add_fp_unittest(
10661077
copysign_test
10671078
SUITE

0 commit comments

Comments
 (0)