Skip to content

Commit 14171b8

Browse files
authored
[libc][stdfix] Add exp function for short _Accum and _Accum types. (#84391)
1 parent 3a56b5a commit 14171b8

File tree

15 files changed

+423
-13
lines changed

15 files changed

+423
-13
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
288288
libc.src.stdfix.absr
289289
libc.src.stdfix.abslk
290290
libc.src.stdfix.abslr
291+
libc.src.stdfix.exphk
292+
libc.src.stdfix.expk
291293
libc.src.stdfix.roundhk
292294
libc.src.stdfix.roundhr
293295
libc.src.stdfix.roundk

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
288288
libc.src.stdfix.absr
289289
libc.src.stdfix.abslk
290290
libc.src.stdfix.abslr
291+
libc.src.stdfix.exphk
292+
libc.src.stdfix.expk
291293
libc.src.stdfix.roundhk
292294
libc.src.stdfix.roundhr
293295
libc.src.stdfix.roundk

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
483483
libc.src.stdfix.absr
484484
libc.src.stdfix.abslk
485485
libc.src.stdfix.abslr
486+
libc.src.stdfix.exphk
487+
libc.src.stdfix.expk
486488
libc.src.stdfix.roundhk
487489
libc.src.stdfix.roundhr
488490
libc.src.stdfix.roundk

libc/docs/math/stdfix.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ floating point types, but are not part of the ISO/IEC TR 18037:2008 spec.
110110
+===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
111111
| cos | | | | | | | | | | | | |
112112
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
113-
| exp | | | | | | | | | | | | |
113+
| exp | | | | | | | | |check| | | |check| | | |
114114
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
115115
| log | | | | | | | | | | | | |
116116
+---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+

libc/spec/llvm_libc_stdfix_ext.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ def LLVMLibcStdfixExt : StandardSpec<"llvm_libc_stdfix_ext"> {
55
[], // types
66
[], // enums
77
[ // functions
8+
GuardedFunctionSpec<"exphk", RetValSpec<ShortAccumType>, [ArgSpec<ShortAccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
9+
GuardedFunctionSpec<"expk", RetValSpec<AccumType>, [ArgSpec<AccumType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
10+
811
GuardedFunctionSpec<"sqrtuhr", RetValSpec<UnsignedShortFractType>, [ArgSpec<UnsignedShortFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
912
GuardedFunctionSpec<"sqrtur", RetValSpec<UnsignedFractType>, [ArgSpec<UnsignedFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,
1013
GuardedFunctionSpec<"sqrtulr", RetValSpec<UnsignedLongFractType>, [ArgSpec<UnsignedLongFractType>], "LIBC_COMPILER_HAS_FIXED_POINT">,

libc/src/__support/fixed_point/fx_rep.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ template <> struct FXRep<short fract> {
4545
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
4646

4747
LIBC_INLINE static constexpr Type MIN() { return SFRACT_MIN; }
48-
LIBC_INLINE static constexpr Type MAX() { return SFRACT_MIN; }
48+
LIBC_INLINE static constexpr Type MAX() { return SFRACT_MAX; }
4949
LIBC_INLINE static constexpr Type ZERO() { return 0.0HR; }
5050
LIBC_INLINE static constexpr Type EPS() { return SFRACT_EPSILON; }
5151
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5HR; }
@@ -65,7 +65,7 @@ template <> struct FXRep<unsigned short fract> {
6565
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
6666

6767
LIBC_INLINE static constexpr Type MIN() { return USFRACT_MIN; }
68-
LIBC_INLINE static constexpr Type MAX() { return USFRACT_MIN; }
68+
LIBC_INLINE static constexpr Type MAX() { return USFRACT_MAX; }
6969
LIBC_INLINE static constexpr Type ZERO() { return 0.0UHR; }
7070
LIBC_INLINE static constexpr Type EPS() { return USFRACT_EPSILON; }
7171
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5UHR; }
@@ -85,7 +85,7 @@ template <> struct FXRep<fract> {
8585
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
8686

8787
LIBC_INLINE static constexpr Type MIN() { return FRACT_MIN; }
88-
LIBC_INLINE static constexpr Type MAX() { return FRACT_MIN; }
88+
LIBC_INLINE static constexpr Type MAX() { return FRACT_MAX; }
8989
LIBC_INLINE static constexpr Type ZERO() { return 0.0R; }
9090
LIBC_INLINE static constexpr Type EPS() { return FRACT_EPSILON; }
9191
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5R; }
@@ -105,7 +105,7 @@ template <> struct FXRep<unsigned fract> {
105105
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
106106

107107
LIBC_INLINE static constexpr Type MIN() { return UFRACT_MIN; }
108-
LIBC_INLINE static constexpr Type MAX() { return UFRACT_MIN; }
108+
LIBC_INLINE static constexpr Type MAX() { return UFRACT_MAX; }
109109
LIBC_INLINE static constexpr Type ZERO() { return 0.0UR; }
110110
LIBC_INLINE static constexpr Type EPS() { return UFRACT_EPSILON; }
111111
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5UR; }
@@ -125,7 +125,7 @@ template <> struct FXRep<long fract> {
125125
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
126126

127127
LIBC_INLINE static constexpr Type MIN() { return LFRACT_MIN; }
128-
LIBC_INLINE static constexpr Type MAX() { return LFRACT_MIN; }
128+
LIBC_INLINE static constexpr Type MAX() { return LFRACT_MAX; }
129129
LIBC_INLINE static constexpr Type ZERO() { return 0.0LR; }
130130
LIBC_INLINE static constexpr Type EPS() { return LFRACT_EPSILON; }
131131
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5LR; }
@@ -145,7 +145,7 @@ template <> struct FXRep<unsigned long fract> {
145145
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
146146

147147
LIBC_INLINE static constexpr Type MIN() { return ULFRACT_MIN; }
148-
LIBC_INLINE static constexpr Type MAX() { return ULFRACT_MIN; }
148+
LIBC_INLINE static constexpr Type MAX() { return ULFRACT_MAX; }
149149
LIBC_INLINE static constexpr Type ZERO() { return 0.0ULR; }
150150
LIBC_INLINE static constexpr Type EPS() { return ULFRACT_EPSILON; }
151151
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5ULR; }
@@ -165,7 +165,7 @@ template <> struct FXRep<short accum> {
165165
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
166166

167167
LIBC_INLINE static constexpr Type MIN() { return SACCUM_MIN; }
168-
LIBC_INLINE static constexpr Type MAX() { return SACCUM_MIN; }
168+
LIBC_INLINE static constexpr Type MAX() { return SACCUM_MAX; }
169169
LIBC_INLINE static constexpr Type ZERO() { return 0.0HK; }
170170
LIBC_INLINE static constexpr Type EPS() { return SACCUM_EPSILON; }
171171
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5HK; }
@@ -185,7 +185,7 @@ template <> struct FXRep<unsigned short accum> {
185185
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
186186

187187
LIBC_INLINE static constexpr Type MIN() { return USACCUM_MIN; }
188-
LIBC_INLINE static constexpr Type MAX() { return USACCUM_MIN; }
188+
LIBC_INLINE static constexpr Type MAX() { return USACCUM_MAX; }
189189
LIBC_INLINE static constexpr Type ZERO() { return 0.0UHK; }
190190
LIBC_INLINE static constexpr Type EPS() { return USACCUM_EPSILON; }
191191
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5UHK; }
@@ -205,7 +205,7 @@ template <> struct FXRep<accum> {
205205
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
206206

207207
LIBC_INLINE static constexpr Type MIN() { return ACCUM_MIN; }
208-
LIBC_INLINE static constexpr Type MAX() { return ACCUM_MIN; }
208+
LIBC_INLINE static constexpr Type MAX() { return ACCUM_MAX; }
209209
LIBC_INLINE static constexpr Type ZERO() { return 0.0K; }
210210
LIBC_INLINE static constexpr Type EPS() { return ACCUM_EPSILON; }
211211
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5K; }
@@ -225,7 +225,7 @@ template <> struct FXRep<unsigned accum> {
225225
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
226226

227227
LIBC_INLINE static constexpr Type MIN() { return UACCUM_MIN; }
228-
LIBC_INLINE static constexpr Type MAX() { return UACCUM_MIN; }
228+
LIBC_INLINE static constexpr Type MAX() { return UACCUM_MAX; }
229229
LIBC_INLINE static constexpr Type ZERO() { return 0.0UK; }
230230
LIBC_INLINE static constexpr Type EPS() { return UACCUM_EPSILON; }
231231
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5UK; }
@@ -245,7 +245,7 @@ template <> struct FXRep<long accum> {
245245
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
246246

247247
LIBC_INLINE static constexpr Type MIN() { return LACCUM_MIN; }
248-
LIBC_INLINE static constexpr Type MAX() { return LACCUM_MIN; }
248+
LIBC_INLINE static constexpr Type MAX() { return LACCUM_MAX; }
249249
LIBC_INLINE static constexpr Type ZERO() { return 0.0LK; }
250250
LIBC_INLINE static constexpr Type EPS() { return LACCUM_EPSILON; }
251251
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5LK; }
@@ -265,7 +265,7 @@ template <> struct FXRep<unsigned long accum> {
265265
SIGN_LEN + INTEGRAL_LEN + FRACTION_LEN;
266266

267267
LIBC_INLINE static constexpr Type MIN() { return ULACCUM_MIN; }
268-
LIBC_INLINE static constexpr Type MAX() { return ULACCUM_MIN; }
268+
LIBC_INLINE static constexpr Type MAX() { return ULACCUM_MAX; }
269269
LIBC_INLINE static constexpr Type ZERO() { return 0.0ULK; }
270270
LIBC_INLINE static constexpr Type EPS() { return ULACCUM_EPSILON; }
271271
LIBC_INLINE static constexpr Type ONE_HALF() { return 0.5ULK; }

libc/src/stdfix/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,29 @@ add_entrypoint_object(
6767
DEPENDS
6868
libc.src.__support.fixed_point.sqrt
6969
)
70+
71+
add_entrypoint_object(
72+
exphk
73+
HDRS
74+
exphk.h
75+
SRCS
76+
exphk.cpp
77+
COMPILE_OPTIONS
78+
-O3
79+
DEPENDS
80+
libc.src.__support.fixed_point.fx_rep
81+
libc.src.__support.CPP.bit
82+
)
83+
84+
add_entrypoint_object(
85+
expk
86+
HDRS
87+
expk.h
88+
SRCS
89+
expk.cpp
90+
COMPILE_OPTIONS
91+
-O3
92+
DEPENDS
93+
libc.src.__support.fixed_point.fx_rep
94+
libc.src.__support.CPP.bit
95+
)

libc/src/stdfix/exphk.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//===-- Implementation of exphk 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 "exphk.h"
10+
#include "src/__support/CPP/bit.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/fixed_point/fx_bits.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
namespace {
17+
18+
// Look up tables for exp(hi) and exp(mid).
19+
// Generated with Sollya:
20+
// > for i from 0 to 89 do {
21+
// hi = floor(i/8) - 5;
22+
// m = i/8 - floor(i/8) - 0.5;
23+
// e_hi = nearestint(exp(hi) * 2^7) * 2^-7;
24+
// e_mid = nearestint(exp(m) * 2^7) * 2^-7;
25+
// print(hi, e_hi, m, e_mid);
26+
// };
27+
// Notice that when i = 88 and 89, e_hi will overflow short accum range.
28+
static constexpr short accum EXP_HI[12] = {
29+
0x1.0p-7hk, 0x1.0p-6hk, 0x1.8p-5hk, 0x1.1p-3hk, 0x1.78p-2hk, 0x1.0p0hk,
30+
0x1.5cp1hk, 0x1.d9p2hk, 0x1.416p4hk, 0x1.b4dp5hk, 0x1.28d4p7hk, SACCUM_MAX,
31+
};
32+
33+
static constexpr short accum EXP_MID[8] = {
34+
0x1.38p-1hk, 0x1.6p-1hk, 0x1.9p-1hk, 0x1.c4p-1hk,
35+
0x1.0p0hk, 0x1.22p0hk, 0x1.48p0hk, 0x1.74p0hk,
36+
};
37+
38+
} // anonymous namespace
39+
40+
LLVM_LIBC_FUNCTION(short accum, exphk, (short accum x)) {
41+
using FXRep = fixed_point::FXRep<short accum>;
42+
using StorageType = typename FXRep::StorageType;
43+
// Output overflow
44+
if (LIBC_UNLIKELY(x >= 0x1.64p2hk))
45+
return FXRep::MAX();
46+
// Lower bound where exp(x) -> 0:
47+
// floor(log(2^-8) * 2^7) * 2^-7
48+
if (LIBC_UNLIKELY(x <= -0x1.63p2hk))
49+
return FXRep::ZERO();
50+
51+
// Current range of x:
52+
// -0x1.628p2 <= x <= 0x1.638p2
53+
// Range reduction:
54+
// x = hi + mid + lo,
55+
// where:
56+
// hi is an integer
57+
// mid * 2^3 is an integer
58+
// |lo| <= 2^-4.
59+
// Then exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo)
60+
// ~ exp(hi) * exp(mid) * (1 + lo)
61+
// with relative errors < |lo|^2 <= 2^-8.
62+
// exp(hi) and exp(mid) are extracted from small lookup tables.
63+
64+
// Round-to-nearest 1/8, tie-to-(+Int):
65+
constexpr short accum ONE_SIXTEENTH = 0x1.0p-4hk;
66+
// x_rounded = floor(x + 1/16).
67+
short accum x_rounded = ((x + ONE_SIXTEENTH) >> (FXRep::FRACTION_LEN - 3))
68+
<< (FXRep::FRACTION_LEN - 3);
69+
short accum lo = x - x_rounded;
70+
71+
// Range of x_rounded:
72+
// x_rounded >= floor((-0x1.628p2 + 0x1.0p-4) * 2^3) * 2^-3
73+
// = -0x1.6p2 = -5.5
74+
// To get the indices, we shift the values so that it start with 0.
75+
// Range of indices: 0 <= indices <= 89
76+
StorageType indices = cpp::bit_cast<StorageType>((x_rounded + 0x1.6p2hk) >>
77+
(FXRep::FRACTION_LEN - 3));
78+
// So we have the following relation:
79+
// indices = (hi + mid + 44/8) * 8
80+
// That implies:
81+
// hi + mid = indices/8 - 5.5
82+
// So for lookup tables, we can use the upper 4 bits to get:
83+
// exp( floor(indices / 8) - 5 )
84+
// and lower 3 bits for:
85+
// exp( (indices - floor(indices)) - 0.5 )
86+
short accum exp_hi = EXP_HI[indices >> 3];
87+
short accum exp_mid = EXP_MID[indices & 0x7];
88+
// exp(x) ~ exp(hi) * exp(mid) * (1 + lo);
89+
return (exp_hi * (exp_mid * (0x1.0p0hk + lo)));
90+
}
91+
92+
} // namespace LIBC_NAMESPACE

libc/src/stdfix/exphk.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for exphk -------------------------*- 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_STDFIX_EXPHK_H
10+
#define LLVM_LIBC_SRC_STDFIX_EXPHK_H
11+
12+
#include "include/llvm-libc-macros/stdfix-macros.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
short accum exphk(short accum x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_STDFIX_EXPHK_H

0 commit comments

Comments
 (0)