xref: /aosp_15_r20/external/arm-optimized-routines/math/exp2.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision 2^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 Shift __exp_data.exp2_shift
15*412f47f9SXin Li #define T __exp_data.tab
16*412f47f9SXin Li #define C1 __exp_data.exp2_poly[0]
17*412f47f9SXin Li #define C2 __exp_data.exp2_poly[1]
18*412f47f9SXin Li #define C3 __exp_data.exp2_poly[2]
19*412f47f9SXin Li #define C4 __exp_data.exp2_poly[3]
20*412f47f9SXin Li #define C5 __exp_data.exp2_poly[4]
21*412f47f9SXin Li #define C6 __exp_data.exp2_poly[5]
22*412f47f9SXin Li 
23*412f47f9SXin Li /* Handle cases that may overflow or underflow when computing the result that
24*412f47f9SXin Li    is scale*(1+TMP) without intermediate rounding.  The bit representation of
25*412f47f9SXin Li    scale is in SBITS, however it has a computed exponent that may have
26*412f47f9SXin Li    overflown into the sign bit so that needs to be adjusted before using it as
27*412f47f9SXin Li    a double.  (int32_t)KI is the k used in the argument reduction and exponent
28*412f47f9SXin Li    adjustment of scale, positive k here means the result may overflow and
29*412f47f9SXin Li    negative k means the result may underflow.  */
30*412f47f9SXin Li static inline double
specialcase(double_t tmp,uint64_t sbits,uint64_t ki)31*412f47f9SXin Li specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
32*412f47f9SXin Li {
33*412f47f9SXin Li   double_t scale, y;
34*412f47f9SXin Li 
35*412f47f9SXin Li   if ((ki & 0x80000000) == 0)
36*412f47f9SXin Li     {
37*412f47f9SXin Li       /* k > 0, the exponent of scale might have overflowed by 1.  */
38*412f47f9SXin Li       sbits -= 1ull << 52;
39*412f47f9SXin Li       scale = asdouble (sbits);
40*412f47f9SXin Li       y = 2 * (scale + scale * tmp);
41*412f47f9SXin Li       return check_oflow (eval_as_double (y));
42*412f47f9SXin Li     }
43*412f47f9SXin Li   /* k < 0, need special care in the subnormal range.  */
44*412f47f9SXin Li   sbits += 1022ull << 52;
45*412f47f9SXin Li   scale = asdouble (sbits);
46*412f47f9SXin Li   y = scale + scale * tmp;
47*412f47f9SXin Li   if (y < 1.0)
48*412f47f9SXin Li     {
49*412f47f9SXin Li       /* Round y to the right precision before scaling it into the subnormal
50*412f47f9SXin Li 	 range to avoid double rounding that can cause 0.5+E/2 ulp error where
51*412f47f9SXin Li 	 E is the worst-case ulp error outside the subnormal range.  So this
52*412f47f9SXin Li 	 is only useful if the goal is better than 1 ulp worst-case error.  */
53*412f47f9SXin Li       double_t hi, lo;
54*412f47f9SXin Li       lo = scale - y + scale * tmp;
55*412f47f9SXin Li       hi = 1.0 + y;
56*412f47f9SXin Li       lo = 1.0 - hi + y + lo;
57*412f47f9SXin Li       y = eval_as_double (hi + lo) - 1.0;
58*412f47f9SXin Li       /* Avoid -0.0 with downward rounding.  */
59*412f47f9SXin Li       if (WANT_ROUNDING && y == 0.0)
60*412f47f9SXin Li 	y = 0.0;
61*412f47f9SXin Li       /* The underflow exception needs to be signaled explicitly.  */
62*412f47f9SXin Li       force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
63*412f47f9SXin Li     }
64*412f47f9SXin Li   y = 0x1p-1022 * y;
65*412f47f9SXin Li   return check_uflow (eval_as_double (y));
66*412f47f9SXin Li }
67*412f47f9SXin Li 
68*412f47f9SXin Li /* Top 12 bits of a double (sign and exponent bits).  */
69*412f47f9SXin Li static inline uint32_t
top12(double x)70*412f47f9SXin Li top12 (double x)
71*412f47f9SXin Li {
72*412f47f9SXin Li   return asuint64 (x) >> 52;
73*412f47f9SXin Li }
74*412f47f9SXin Li 
75*412f47f9SXin Li double
exp2(double x)76*412f47f9SXin Li exp2 (double x)
77*412f47f9SXin Li {
78*412f47f9SXin Li   uint32_t abstop;
79*412f47f9SXin Li   uint64_t ki, idx, top, sbits;
80*412f47f9SXin Li   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
81*412f47f9SXin Li   double_t kd, r, r2, scale, tail, tmp;
82*412f47f9SXin Li 
83*412f47f9SXin Li   abstop = top12 (x) & 0x7ff;
84*412f47f9SXin Li   if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
85*412f47f9SXin Li     {
86*412f47f9SXin Li       if (abstop - top12 (0x1p-54) >= 0x80000000)
87*412f47f9SXin Li 	/* Avoid spurious underflow for tiny x.  */
88*412f47f9SXin Li 	/* Note: 0 is common input.  */
89*412f47f9SXin Li 	return WANT_ROUNDING ? 1.0 + x : 1.0;
90*412f47f9SXin Li       if (abstop >= top12 (1024.0))
91*412f47f9SXin Li 	{
92*412f47f9SXin Li 	  if (asuint64 (x) == asuint64 (-INFINITY))
93*412f47f9SXin Li 	    return 0.0;
94*412f47f9SXin Li 	  if (abstop >= top12 (INFINITY))
95*412f47f9SXin Li 	    return 1.0 + x;
96*412f47f9SXin Li 	  if (!(asuint64 (x) >> 63))
97*412f47f9SXin Li 	    return __math_oflow (0);
98*412f47f9SXin Li 	  else if (asuint64 (x) >= asuint64 (-1075.0))
99*412f47f9SXin Li 	    return __math_uflow (0);
100*412f47f9SXin Li 	}
101*412f47f9SXin Li       if (2 * asuint64 (x) > 2 * asuint64 (928.0))
102*412f47f9SXin Li 	/* Large x is special cased below.  */
103*412f47f9SXin Li 	abstop = 0;
104*412f47f9SXin Li     }
105*412f47f9SXin Li 
106*412f47f9SXin Li   /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)].  */
107*412f47f9SXin Li   /* x = k/N + r, with int k and r in [-1/2N, 1/2N].  */
108*412f47f9SXin Li   kd = eval_as_double (x + Shift);
109*412f47f9SXin Li   ki = asuint64 (kd); /* k.  */
110*412f47f9SXin Li   kd -= Shift; /* k/N for int k.  */
111*412f47f9SXin Li   r = x - kd;
112*412f47f9SXin Li   /* 2^(k/N) ~= scale * (1 + tail).  */
113*412f47f9SXin Li   idx = 2 * (ki % N);
114*412f47f9SXin Li   top = ki << (52 - EXP_TABLE_BITS);
115*412f47f9SXin Li   tail = asdouble (T[idx]);
116*412f47f9SXin Li   /* This is only a valid scale when -1023*N < k < 1024*N.  */
117*412f47f9SXin Li   sbits = T[idx + 1] + top;
118*412f47f9SXin Li   /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1).  */
119*412f47f9SXin Li   /* Evaluation is optimized assuming superscalar pipelined execution.  */
120*412f47f9SXin Li   r2 = r * r;
121*412f47f9SXin Li   /* Without fma the worst case error is 0.5/N ulp larger.  */
122*412f47f9SXin Li   /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp.  */
123*412f47f9SXin Li #if EXP2_POLY_ORDER == 4
124*412f47f9SXin Li   tmp = tail + r * C1 + r2 * C2 + r * r2 * (C3 + r * C4);
125*412f47f9SXin Li #elif EXP2_POLY_ORDER == 5
126*412f47f9SXin Li   tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
127*412f47f9SXin Li #elif EXP2_POLY_ORDER == 6
128*412f47f9SXin Li   tmp = tail + r * C1 + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
129*412f47f9SXin Li #endif
130*412f47f9SXin Li   if (unlikely (abstop == 0))
131*412f47f9SXin Li     return specialcase (tmp, sbits, ki);
132*412f47f9SXin Li   scale = asdouble (sbits);
133*412f47f9SXin Li   /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there
134*412f47f9SXin Li      is no spurious underflow here even without fma.  */
135*412f47f9SXin Li   return eval_as_double (scale + scale * tmp);
136*412f47f9SXin Li }
137*412f47f9SXin Li #if USE_GLIBC_ABI
strong_alias(exp2,__exp2_finite)138*412f47f9SXin Li strong_alias (exp2, __exp2_finite)
139*412f47f9SXin Li hidden_alias (exp2, __ieee754_exp2)
140*412f47f9SXin Li # if LDBL_MANT_DIG == 53
141*412f47f9SXin Li long double exp2l (long double x) { return exp2 (x); }
142*412f47f9SXin Li # endif
143*412f47f9SXin Li #endif
144