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