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