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