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