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