Skip to content

Commit 92629d8

Browse files
committed
[libc][math][c23] Add exp10m1f16 C23 math function
Part of #95250.
1 parent 22d56a5 commit 92629d8

File tree

16 files changed

+459
-44
lines changed

16 files changed

+459
-44
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
486486
libc.src.math.ceilf16
487487
libc.src.math.copysignf16
488488
libc.src.math.exp10f16
489+
libc.src.math.exp10m1f16
489490
libc.src.math.exp2f16
490491
libc.src.math.expf16
491492
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
@@ -595,6 +595,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
595595
libc.src.math.ceilf16
596596
libc.src.math.copysignf16
597597
libc.src.math.exp10f16
598+
libc.src.math.exp10m1f16
598599
libc.src.math.exp2f16
599600
libc.src.math.exp2m1f16
600601
libc.src.math.expf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Higher Math Functions
288288
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
289289
| exp10 | |check| | |check| | | |check| | | 7.12.6.2 | F.10.3.2 |
290290
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
291-
| exp10m1 | | | | | | 7.12.6.3 | F.10.3.3 |
291+
| exp10m1 | | | | |check| | | 7.12.6.3 | F.10.3.3 |
292292
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
293293
| exp2 | |check| | |check| | | |check| | | 7.12.6.4 | F.10.3.4 |
294294
+-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

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

609+
GuardedFunctionSpec<"exp10m1f16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
610+
609611
FunctionSpec<"remainder", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
610612
FunctionSpec<"remainderf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
611613
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
@@ -121,6 +121,8 @@ add_math_entrypoint_object(exp10)
121121
add_math_entrypoint_object(exp10f)
122122
add_math_entrypoint_object(exp10f16)
123123

124+
add_math_entrypoint_object(exp10m1f16)
125+
124126
add_math_entrypoint_object(expm1)
125127
add_math_entrypoint_object(expm1f)
126128
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,28 @@ add_entrypoint_object(
15761576
-O3
15771577
)
15781578

1579+
add_entrypoint_object(
1580+
exp10m1f16
1581+
SRCS
1582+
exp10m1f16.cpp
1583+
HDRS
1584+
../exp10m1f16.h
1585+
DEPENDS
1586+
.expxf16
1587+
libc.hdr.errno_macros
1588+
libc.hdr.fenv_macros
1589+
libc.src.__support.FPUtil.except_value_utils
1590+
libc.src.__support.FPUtil.fenv_impl
1591+
libc.src.__support.FPUtil.fp_bits
1592+
libc.src.__support.FPUtil.multiply_add
1593+
libc.src.__support.FPUtil.polyeval
1594+
libc.src.__support.FPUtil.rounding_mode
1595+
libc.src.__support.macros.optimization
1596+
libc.src.__support.macros.properties.cpu_features
1597+
COMPILE_OPTIONS
1598+
-O3
1599+
)
1600+
15791601
add_entrypoint_object(
15801602
expm1
15811603
SRCS

libc/src/math/generic/exp10f16.cpp

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ static constexpr fputil::ExceptValues<float16, N_EXP10F16_EXCEPTS>
5353
#endif
5454
}};
5555

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-
6656
LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) {
6757
using FPBits = fputil::FPBits<float16>;
6858
FPBits x_bits(x);
@@ -131,39 +121,8 @@ LLVM_LIBC_FUNCTION(float16, exp10f16, (float16 x)) {
131121
if (auto r = EXP10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
132122
return r.value();
133123

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);
124+
// 10^x = 2^((hi + mid) * log2(10)) * 10^lo
125+
auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x);
167126
return static_cast<float16>(exp2_hi_mid * exp10_lo);
168127
}
169128

libc/src/math/generic/exp10m1f16.cpp

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

1054+
add_fp_unittest(
1055+
exp10m1f16_test
1056+
NEED_MPFR
1057+
SUITE
1058+
libc-math-unittests
1059+
SRCS
1060+
exp10m1f16_test.cpp
1061+
DEPENDS
1062+
libc.src.math.exp10m1f16
1063+
)
1064+
10541065
add_fp_unittest(
10551066
copysign_test
10561067
SUITE

0 commit comments

Comments
 (0)