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