1 | /* Single-precision e^x function. |
---|
2 | Copyright (c) 2017 ARM Ltd. All rights reserved. |
---|
3 | |
---|
4 | Redistribution and use in source and binary forms, with or without |
---|
5 | modification, are permitted provided that the following conditions |
---|
6 | are met: |
---|
7 | 1. Redistributions of source code must retain the above copyright |
---|
8 | notice, this list of conditions and the following disclaimer. |
---|
9 | 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | notice, this list of conditions and the following disclaimer in the |
---|
11 | documentation and/or other materials provided with the distribution. |
---|
12 | 3. The name of the company may not be used to endorse or promote |
---|
13 | products derived from this software without specific prior written |
---|
14 | permission. |
---|
15 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS AND ANY EXPRESS OR IMPLIED |
---|
17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
---|
18 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
---|
21 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
---|
26 | |
---|
27 | #include "fdlibm.h" |
---|
28 | #if !__OBSOLETE_MATH |
---|
29 | |
---|
30 | #include <math.h> |
---|
31 | #include <stdint.h> |
---|
32 | #include "math_config.h" |
---|
33 | |
---|
34 | /* |
---|
35 | EXP2F_TABLE_BITS = 5 |
---|
36 | EXP2F_POLY_ORDER = 3 |
---|
37 | |
---|
38 | ULP error: 0.502 (nearest rounding.) |
---|
39 | Relative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.) |
---|
40 | Wrong count: 170635 (all nearest rounding wrong results with fma.) |
---|
41 | Non-nearest ULP error: 1 (rounded ULP error) |
---|
42 | */ |
---|
43 | |
---|
44 | #define N (1 << EXP2F_TABLE_BITS) |
---|
45 | #define InvLn2N __exp2f_data.invln2_scaled |
---|
46 | #define T __exp2f_data.tab |
---|
47 | #define C __exp2f_data.poly_scaled |
---|
48 | |
---|
49 | static inline uint32_t |
---|
50 | top12 (float x) |
---|
51 | { |
---|
52 | return asuint (x) >> 20; |
---|
53 | } |
---|
54 | |
---|
55 | float |
---|
56 | expf (float x) |
---|
57 | { |
---|
58 | uint32_t abstop; |
---|
59 | uint64_t ki, t; |
---|
60 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
---|
61 | double_t kd, xd, z, r, r2, y, s; |
---|
62 | |
---|
63 | xd = (double_t) x; |
---|
64 | abstop = top12 (x) & 0x7ff; |
---|
65 | if (__builtin_expect (abstop >= top12 (88.0f), 0)) |
---|
66 | { |
---|
67 | /* |x| >= 88 or x is nan. */ |
---|
68 | if (asuint (x) == asuint (-INFINITY)) |
---|
69 | return 0.0f; |
---|
70 | if (abstop >= top12 (INFINITY)) |
---|
71 | return x + x; |
---|
72 | if (x > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */ |
---|
73 | return __math_oflowf (0); |
---|
74 | if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */ |
---|
75 | return __math_uflowf (0); |
---|
76 | #if WANT_ERRNO_UFLOW |
---|
77 | if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */ |
---|
78 | return __math_may_uflowf (0); |
---|
79 | #endif |
---|
80 | } |
---|
81 | |
---|
82 | /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */ |
---|
83 | z = InvLn2N * xd; |
---|
84 | |
---|
85 | /* Round and convert z to int, the result is in [-150*N, 128*N] and |
---|
86 | ideally ties-to-even rule is used, otherwise the magnitude of r |
---|
87 | can be bigger which gives larger approximation error. */ |
---|
88 | #if TOINT_INTRINSICS |
---|
89 | kd = roundtoint (z); |
---|
90 | ki = converttoint (z); |
---|
91 | #elif TOINT_RINT |
---|
92 | kd = rint (z); |
---|
93 | ki = (long) kd; |
---|
94 | #elif TOINT_SHIFT |
---|
95 | # define SHIFT __exp2f_data.shift |
---|
96 | kd = (double) (z + SHIFT); /* Rounding to double precision is required. */ |
---|
97 | ki = asuint64 (kd); |
---|
98 | kd -= SHIFT; |
---|
99 | #endif |
---|
100 | r = z - kd; |
---|
101 | |
---|
102 | /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ |
---|
103 | t = T[ki % N]; |
---|
104 | t += ki << (52 - EXP2F_TABLE_BITS); |
---|
105 | s = asdouble (t); |
---|
106 | z = C[0] * r + C[1]; |
---|
107 | r2 = r * r; |
---|
108 | y = C[2] * r + 1; |
---|
109 | y = z * r2 + y; |
---|
110 | y = y * s; |
---|
111 | return (float) y; |
---|
112 | } |
---|
113 | #endif /* !__OBSOLETE_MATH */ |
---|