Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/exp_inline.h
48378 views
1
/*
2
* Double-precision e^x function.
3
*
4
* Copyright (c) 2018-2024, Arm Limited.
5
* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6
*/
7
8
#ifndef PL_MATH_EXP_INLINE_H
9
#define PL_MATH_EXP_INLINE_H
10
11
#include <float.h>
12
#include <math.h>
13
#include <stdint.h>
14
#include "math_config.h"
15
16
#define N (1 << EXP_TABLE_BITS)
17
#define InvLn2N __exp_data.invln2N
18
#define NegLn2hiN __exp_data.negln2hiN
19
#define NegLn2loN __exp_data.negln2loN
20
#define Shift __exp_data.shift
21
#define T __exp_data.tab
22
#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
23
#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
24
#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
25
#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
26
#define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
27
28
/* Handle cases that may overflow or underflow when computing the result that
29
is scale*(1+TMP) without intermediate rounding. The bit representation of
30
scale is in SBITS, however it has a computed exponent that may have
31
overflown into the sign bit so that needs to be adjusted before using it as
32
a double. (int32_t)KI is the k used in the argument reduction and exponent
33
adjustment of scale, positive k here means the result may overflow and
34
negative k means the result may underflow. */
35
static inline double
36
exp_inline_special_case (double_t tmp, uint64_t sbits, uint64_t ki)
37
{
38
double_t scale, y;
39
40
if ((ki & 0x80000000) == 0)
41
{
42
/* k > 0, the exponent of scale might have overflowed by <= 460. */
43
sbits -= 1009ull << 52;
44
scale = asdouble (sbits);
45
y = 0x1p1009 * (scale + scale * tmp);
46
return check_oflow (eval_as_double (y));
47
}
48
/* k < 0, need special care in the subnormal range. */
49
sbits += 1022ull << 52;
50
scale = asdouble (sbits);
51
y = scale + scale * tmp;
52
if (y < 1.0)
53
{
54
/* Round y to the right precision before scaling it into the subnormal
55
range to avoid double rounding that can cause 0.5+E/2 ulp error where
56
E is the worst-case ulp error outside the subnormal range. So this
57
is only useful if the goal is better than 1 ulp worst-case error. */
58
double_t hi, lo;
59
lo = scale - y + scale * tmp;
60
hi = 1.0 + y;
61
lo = 1.0 - hi + y + lo;
62
y = eval_as_double (hi + lo) - 1.0;
63
/* Avoid -0.0 with downward rounding. */
64
if (WANT_ROUNDING && y == 0.0)
65
y = 0.0;
66
/* The underflow exception needs to be signaled explicitly. */
67
force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
68
}
69
y = 0x1p-1022 * y;
70
return check_uflow (eval_as_double (y));
71
}
72
73
/* Top 12 bits of a double (sign and exponent bits). */
74
static inline uint32_t
75
top12 (double x)
76
{
77
return asuint64 (x) >> 52;
78
}
79
80
/* Computes exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
81
If hastail is 0 then xtail is assumed to be 0 too. */
82
static inline double
83
exp_inline (double x, double xtail)
84
{
85
uint32_t abstop;
86
uint64_t ki, idx, top, sbits;
87
/* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
88
double_t kd, z, r, r2, scale, tail, tmp;
89
90
abstop = top12 (x) & 0x7ff;
91
if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
92
{
93
if (abstop - top12 (0x1p-54) >= 0x80000000)
94
/* Avoid spurious underflow for tiny x. */
95
/* Note: 0 is common input. */
96
return WANT_ROUNDING ? 1.0 + x : 1.0;
97
if (abstop >= top12 (1024.0))
98
{
99
if (asuint64 (x) == asuint64 (-INFINITY))
100
return 0.0;
101
if (abstop >= top12 (INFINITY))
102
return 1.0 + x;
103
if (asuint64 (x) >> 63)
104
return __math_uflow (0);
105
else
106
return __math_oflow (0);
107
}
108
/* Large x is special cased below. */
109
abstop = 0;
110
}
111
112
/* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */
113
/* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */
114
z = InvLn2N * x;
115
#if TOINT_INTRINSICS
116
kd = roundtoint (z);
117
ki = converttoint (z);
118
#elif EXP_USE_TOINT_NARROW
119
/* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */
120
kd = eval_as_double (z + Shift);
121
ki = asuint64 (kd) >> 16;
122
kd = (double_t) (int32_t) ki;
123
#else
124
/* z - kd is in [-1, 1] in non-nearest rounding modes. */
125
kd = eval_as_double (z + Shift);
126
ki = asuint64 (kd);
127
kd -= Shift;
128
#endif
129
r = x + kd * NegLn2hiN + kd * NegLn2loN;
130
/* The code assumes 2^-200 < |xtail| < 2^-8/N. */
131
if (!__builtin_constant_p (xtail) || xtail != 0.0)
132
r += xtail;
133
/* 2^(k/N) ~= scale * (1 + tail). */
134
idx = 2 * (ki % N);
135
top = ki << (52 - EXP_TABLE_BITS);
136
tail = asdouble (T[idx]);
137
/* This is only a valid scale when -1023*N < k < 1024*N. */
138
sbits = T[idx + 1] + top;
139
/* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */
140
/* Evaluation is optimized assuming superscalar pipelined execution. */
141
r2 = r * r;
142
/* Without fma the worst case error is 0.25/N ulp larger. */
143
/* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */
144
#if EXP_POLY_ORDER == 4
145
tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
146
#elif EXP_POLY_ORDER == 5
147
tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
148
#elif EXP_POLY_ORDER == 6
149
tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
150
#endif
151
if (unlikely (abstop == 0))
152
return exp_inline_special_case (tmp, sbits, ki);
153
scale = asdouble (sbits);
154
/* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
155
is no spurious underflow here even without fma. */
156
return eval_as_double (scale + scale * tmp);
157
}
158
159
#endif
160
161