George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 1 | /* |
Szabolcs Nagy | 1b94597 | 2018-05-14 14:46:40 +0100 | [diff] [blame] | 2 | * Single-precision e^x function. |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 3 | * |
Szabolcs Nagy | 11253b0 | 2018-11-12 11:10:57 +0000 | [diff] [blame] | 4 | * Copyright (c) 2017-2018, Arm Limited. |
| 5 | * SPDX-License-Identifier: MIT |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 8 | #include <math.h> |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include "math_config.h" |
| 11 | |
| 12 | /* |
| 13 | EXP2F_TABLE_BITS = 5 |
| 14 | EXP2F_POLY_ORDER = 3 |
| 15 | |
| 16 | ULP error: 0.502 (nearest rounding.) |
| 17 | Relative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.) |
| 18 | Wrong count: 170635 (all nearest rounding wrong results with fma.) |
| 19 | Non-nearest ULP error: 1 (rounded ULP error) |
| 20 | */ |
| 21 | |
| 22 | #define N (1 << EXP2F_TABLE_BITS) |
| 23 | #define InvLn2N __exp2f_data.invln2_scaled |
| 24 | #define T __exp2f_data.tab |
| 25 | #define C __exp2f_data.poly_scaled |
| 26 | |
| 27 | static inline uint32_t |
| 28 | top12 (float x) |
| 29 | { |
| 30 | return asuint (x) >> 20; |
| 31 | } |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 32 | |
| 33 | float |
Szabolcs Nagy | 0d51c04 | 2018-04-25 13:26:22 +0100 | [diff] [blame] | 34 | expf (float x) |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 35 | { |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 36 | uint32_t abstop; |
| 37 | uint64_t ki, t; |
| 38 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
| 39 | double_t kd, xd, z, r, r2, y, s; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 40 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 41 | xd = (double_t) x; |
| 42 | abstop = top12 (x) & 0x7ff; |
Szabolcs Nagy | b21378a | 2018-05-11 13:29:44 +0100 | [diff] [blame] | 43 | if (unlikely (abstop >= top12 (88.0f))) |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 44 | { |
| 45 | /* |x| >= 88 or x is nan. */ |
| 46 | if (asuint (x) == asuint (-INFINITY)) |
| 47 | return 0.0f; |
| 48 | if (abstop >= top12 (INFINITY)) |
| 49 | return x + x; |
| 50 | if (x > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */ |
| 51 | return __math_oflowf (0); |
| 52 | if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */ |
| 53 | return __math_uflowf (0); |
| 54 | #if WANT_ERRNO_UFLOW |
| 55 | if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */ |
| 56 | return __math_may_uflowf (0); |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 57 | #endif |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 58 | } |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 59 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 60 | /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */ |
| 61 | z = InvLn2N * xd; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 62 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 63 | /* Round and convert z to int, the result is in [-150*N, 128*N] and |
Szabolcs Nagy | 39b0191 | 2018-05-10 15:35:06 +0100 | [diff] [blame] | 64 | ideally nearest int is used, otherwise the magnitude of r can be |
| 65 | bigger which gives larger approximation error. */ |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 66 | #if TOINT_INTRINSICS |
| 67 | kd = roundtoint (z); |
| 68 | ki = converttoint (z); |
Szabolcs Nagy | 39b0191 | 2018-05-10 15:35:06 +0100 | [diff] [blame] | 69 | #else |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 70 | # define SHIFT __exp2f_data.shift |
Szabolcs Nagy | 5eb9d19 | 2018-05-11 13:20:04 +0100 | [diff] [blame] | 71 | kd = eval_as_double (z + SHIFT); |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 72 | ki = asuint64 (kd); |
| 73 | kd -= SHIFT; |
| 74 | #endif |
| 75 | r = z - kd; |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 76 | |
Szabolcs Nagy | 86067a7 | 2017-08-11 15:35:12 +0100 | [diff] [blame] | 77 | /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ |
| 78 | t = T[ki % N]; |
| 79 | t += ki << (52 - EXP2F_TABLE_BITS); |
| 80 | s = asdouble (t); |
| 81 | z = C[0] * r + C[1]; |
| 82 | r2 = r * r; |
| 83 | y = C[2] * r + 1; |
| 84 | y = z * r2 + y; |
| 85 | y = y * s; |
Szabolcs Nagy | 04884bd | 2018-12-07 14:58:51 +0000 | [diff] [blame] | 86 | return eval_as_float (y); |
George Lander | da55ef9 | 2015-11-19 12:05:06 +0000 | [diff] [blame] | 87 | } |
Szabolcs Nagy | b7d568d | 2018-06-06 12:26:56 +0100 | [diff] [blame] | 88 | #if USE_GLIBC_ABI |
| 89 | strong_alias (expf, __expf_finite) |
| 90 | hidden_alias (expf, __ieee754_expf) |
| 91 | #endif |