1 /* Double-precision 2^x function.
2    Copyright (C) 2018-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <math.h>
20 #include <stdint.h>
21 #include <math-barriers.h>
22 #include <math-narrow-eval.h>
23 #include <math-svid-compat.h>
24 #include <libm-alias-finite.h>
25 #include <libm-alias-double.h>
26 #include "math_config.h"
27 
28 #define N (1 << EXP_TABLE_BITS)
29 #define Shift __exp_data.exp2_shift
30 #define T __exp_data.tab
31 #define C1 __exp_data.exp2_poly[0]
32 #define C2 __exp_data.exp2_poly[1]
33 #define C3 __exp_data.exp2_poly[2]
34 #define C4 __exp_data.exp2_poly[3]
35 #define C5 __exp_data.exp2_poly[4]
36 
37 /* Handle cases that may overflow or underflow when computing the result that
38    is scale*(1+TMP) without intermediate rounding.  The bit representation of
39    scale is in SBITS, however it has a computed exponent that may have
40    overflown into the sign bit so that needs to be adjusted before using it as
41    a double.  (int32_t)KI is the k used in the argument reduction and exponent
42    adjustment of scale, positive k here means the result may overflow and
43    negative k means the result may underflow.  */
44 static inline double
specialcase(double_t tmp,uint64_t sbits,uint64_t ki)45 specialcase (double_t tmp, uint64_t sbits, uint64_t ki)
46 {
47   double_t scale, y;
48 
49   if ((ki & 0x80000000) == 0)
50     {
51       /* k > 0, the exponent of scale might have overflowed by 1.  */
52       sbits -= 1ull << 52;
53       scale = asdouble (sbits);
54       y = 2 * (scale + scale * tmp);
55       return check_oflow (y);
56     }
57   /* k < 0, need special care in the subnormal range.  */
58   sbits += 1022ull << 52;
59   scale = asdouble (sbits);
60   y = scale + scale * tmp;
61   if (y < 1.0)
62     {
63       /* Round y to the right precision before scaling it into the subnormal
64 	 range to avoid double rounding that can cause 0.5+E/2 ulp error where
65 	 E is the worst-case ulp error outside the subnormal range.  So this
66 	 is only useful if the goal is better than 1 ulp worst-case error.  */
67       double_t hi, lo;
68       lo = scale - y + scale * tmp;
69       hi = 1.0 + y;
70       lo = 1.0 - hi + y + lo;
71       y = math_narrow_eval (hi + lo) - 1.0;
72       /* Avoid -0.0 with downward rounding.  */
73       if (WANT_ROUNDING && y == 0.0)
74 	y = 0.0;
75       /* The underflow exception needs to be signaled explicitly.  */
76       math_force_eval (math_opt_barrier (0x1p-1022) * 0x1p-1022);
77     }
78   y = 0x1p-1022 * y;
79   return check_uflow (y);
80 }
81 
82 /* Top 12 bits of a double (sign and exponent bits).  */
83 static inline uint32_t
top12(double x)84 top12 (double x)
85 {
86   return asuint64 (x) >> 52;
87 }
88 
89 double
__exp2(double x)90 __exp2 (double x)
91 {
92   uint32_t abstop;
93   uint64_t ki, idx, top, sbits;
94   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
95   double_t kd, r, r2, scale, tail, tmp;
96 
97   abstop = top12 (x) & 0x7ff;
98   if (__glibc_unlikely (abstop - top12 (0x1p-54)
99 			>= top12 (512.0) - top12 (0x1p-54)))
100     {
101       if (abstop - top12 (0x1p-54) >= 0x80000000)
102 	/* Avoid spurious underflow for tiny x.  */
103 	/* Note: 0 is common input.  */
104 	return WANT_ROUNDING ? 1.0 + x : 1.0;
105       if (abstop >= top12 (1024.0))
106 	{
107 	  if (asuint64 (x) == asuint64 (-INFINITY))
108 	    return 0.0;
109 	  if (abstop >= top12 (INFINITY))
110 	    return 1.0 + x;
111 	  if (!(asuint64 (x) >> 63))
112 	    return __math_oflow (0);
113 	  else if (asuint64 (x) >= asuint64 (-1075.0))
114 	    return __math_uflow (0);
115 	}
116       if (2 * asuint64 (x) > 2 * asuint64 (928.0))
117 	/* Large x is special cased below.  */
118 	abstop = 0;
119     }
120 
121   /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)].  */
122   /* x = k/N + r, with int k and r in [-1/2N, 1/2N].  */
123   kd = math_narrow_eval (x + Shift);
124   ki = asuint64 (kd); /* k.  */
125   kd -= Shift; /* k/N for int k.  */
126   r = x - kd;
127   /* 2^(k/N) ~= scale * (1 + tail).  */
128   idx = 2 * (ki % N);
129   top = ki << (52 - EXP_TABLE_BITS);
130   tail = asdouble (T[idx]);
131   /* This is only a valid scale when -1023*N < k < 1024*N.  */
132   sbits = T[idx + 1] + top;
133   /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1).  */
134   /* Evaluation is optimized assuming superscalar pipelined execution.  */
135   r2 = r * r;
136   /* Without fma the worst case error is 0.5/N ulp larger.  */
137   /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp.  */
138   tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
139   if (__glibc_unlikely (abstop == 0))
140     return specialcase (tmp, sbits, ki);
141   scale = asdouble (sbits);
142   /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there
143      is no spurious underflow here even without fma.  */
144   return scale + scale * tmp;
145 }
146 #ifndef __exp2
147 strong_alias (__exp2, __ieee754_exp2)
148 libm_alias_finite (__ieee754_exp2, __exp2)
149 # if LIBM_SVID_COMPAT
150 versioned_symbol (libm, __exp2, exp2, GLIBC_2_29);
151 libm_alias_double_other (__exp2, exp2)
152 # else
153 libm_alias_double (__exp2, exp2)
154 # endif
155 #endif
156