1 /*
2  * Single-precision pow function.
3  *
4  * Copyright (c) 2017-2018, Arm Limited.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 #if WANT_SINGLEPREC
9 #include "single/e_powf.c"
10 #else
11 
12 #include <math.h>
13 #include <stdint.h>
14 #include "math_config.h"
15 
16 /*
17 POWF_LOG2_POLY_ORDER = 5
18 EXP2F_TABLE_BITS = 5
19 
20 ULP error: 0.82 (~ 0.5 + relerr*2^24)
21 relerr: 1.27 * 2^-26 (Relative error ~= 128*Ln2*relerr_log2 + relerr_exp2)
22 relerr_log2: 1.83 * 2^-33 (Relative error of logx.)
23 relerr_exp2: 1.69 * 2^-34 (Relative error of exp2(ylogx).)
24 */
25 
26 #define N (1 << POWF_LOG2_TABLE_BITS)
27 #define T __powf_log2_data.tab
28 #define A __powf_log2_data.poly
29 #define OFF 0x3f330000
30 
31 /* Subnormal input is normalized so ix has negative biased exponent.
32    Output is multiplied by N (POWF_SCALE) if TOINT_INTRINICS is set.  */
33 static inline double_t
log2_inline(uint32_t ix)34 log2_inline (uint32_t ix)
35 {
36   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
37   double_t z, r, r2, r4, p, q, y, y0, invc, logc;
38   uint32_t iz, top, tmp;
39   int k, i;
40 
41   /* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
42      The range is split into N subintervals.
43      The ith subinterval contains z and c is near its center.  */
44   tmp = ix - OFF;
45   i = (tmp >> (23 - POWF_LOG2_TABLE_BITS)) % N;
46   top = tmp & 0xff800000;
47   iz = ix - top;
48   k = (int32_t) top >> (23 - POWF_SCALE_BITS); /* arithmetic shift */
49   invc = T[i].invc;
50   logc = T[i].logc;
51   z = (double_t) asfloat (iz);
52 
53   /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
54   r = z * invc - 1;
55   y0 = logc + (double_t) k;
56 
57   /* Pipelined polynomial evaluation to approximate log1p(r)/ln2.  */
58   r2 = r * r;
59   y = A[0] * r + A[1];
60   p = A[2] * r + A[3];
61   r4 = r2 * r2;
62   q = A[4] * r + y0;
63   q = p * r2 + q;
64   y = y * r4 + q;
65   return y;
66 }
67 
68 #undef N
69 #undef T
70 #define N (1 << EXP2F_TABLE_BITS)
71 #define T __exp2f_data.tab
72 #define SIGN_BIAS (1 << (EXP2F_TABLE_BITS + 11))
73 
74 /* The output of log2 and thus the input of exp2 is either scaled by N
75    (in case of fast toint intrinsics) or not.  The unscaled xd must be
76    in [-1021,1023], sign_bias sets the sign of the result.  */
77 static inline float
exp2_inline(double_t xd,uint32_t sign_bias)78 exp2_inline (double_t xd, uint32_t sign_bias)
79 {
80   uint64_t ki, ski, t;
81   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
82   double_t kd, z, r, r2, y, s;
83 
84 #if TOINT_INTRINSICS
85 # define C __exp2f_data.poly_scaled
86   /* N*x = k + r with r in [-1/2, 1/2] */
87   kd = roundtoint (xd); /* k */
88   ki = converttoint (xd);
89 #else
90 # define C __exp2f_data.poly
91 # define SHIFT __exp2f_data.shift_scaled
92   /* x = k/N + r with r in [-1/(2N), 1/(2N)] */
93   kd = eval_as_double (xd + SHIFT);
94   ki = asuint64 (kd);
95   kd -= SHIFT; /* k/N */
96 #endif
97   r = xd - kd;
98 
99   /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
100   t = T[ki % N];
101   ski = ki + sign_bias;
102   t += ski << (52 - EXP2F_TABLE_BITS);
103   s = asdouble (t);
104   z = C[0] * r + C[1];
105   r2 = r * r;
106   y = C[2] * r + 1;
107   y = z * r2 + y;
108   y = y * s;
109   return eval_as_float (y);
110 }
111 
112 /* Returns 0 if not int, 1 if odd int, 2 if even int.  The argument is
113    the bit representation of a non-zero finite floating-point value.  */
114 static inline int
checkint(uint32_t iy)115 checkint (uint32_t iy)
116 {
117   int e = iy >> 23 & 0xff;
118   if (e < 0x7f)
119     return 0;
120   if (e > 0x7f + 23)
121     return 2;
122   if (iy & ((1 << (0x7f + 23 - e)) - 1))
123     return 0;
124   if (iy & (1 << (0x7f + 23 - e)))
125     return 1;
126   return 2;
127 }
128 
129 static inline int
zeroinfnan(uint32_t ix)130 zeroinfnan (uint32_t ix)
131 {
132   return 2 * ix - 1 >= 2u * 0x7f800000 - 1;
133 }
134 
135 float
powf(float x,float y)136 powf (float x, float y)
137 {
138   uint32_t sign_bias = 0;
139   uint32_t ix, iy;
140 
141   ix = asuint (x);
142   iy = asuint (y);
143   if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000 || zeroinfnan (iy)))
144     {
145       /* Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan).  */
146       if (unlikely (zeroinfnan (iy)))
147 	{
148 	  if (2 * iy == 0)
149 	    return issignalingf_inline (x) ? x + y : 1.0f;
150 	  if (ix == 0x3f800000)
151 	    return issignalingf_inline (y) ? x + y : 1.0f;
152 	  if (2 * ix > 2u * 0x7f800000 || 2 * iy > 2u * 0x7f800000)
153 	    return x + y;
154 	  if (2 * ix == 2 * 0x3f800000)
155 	    return 1.0f;
156 	  if ((2 * ix < 2 * 0x3f800000) == !(iy & 0x80000000))
157 	    return 0.0f; /* |x|<1 && y==inf or |x|>1 && y==-inf.  */
158 	  return y * y;
159 	}
160       if (unlikely (zeroinfnan (ix)))
161 	{
162 	  float_t x2 = x * x;
163 	  if (ix & 0x80000000 && checkint (iy) == 1)
164 	    {
165 	      x2 = -x2;
166 	      sign_bias = 1;
167 	    }
168 #if WANT_ERRNO
169 	  if (2 * ix == 0 && iy & 0x80000000)
170 	    return __math_divzerof (sign_bias);
171 #endif
172 	  /* Without the barrier some versions of clang hoist the 1/x2 and
173 	     thus division by zero exception can be signaled spuriously.  */
174 	  return iy & 0x80000000 ? opt_barrier_float (1 / x2) : x2;
175 	}
176       /* x and y are non-zero finite.  */
177       if (ix & 0x80000000)
178 	{
179 	  /* Finite x < 0.  */
180 	  int yint = checkint (iy);
181 	  if (yint == 0)
182 	    return __math_invalidf (x);
183 	  if (yint == 1)
184 	    sign_bias = SIGN_BIAS;
185 	  ix &= 0x7fffffff;
186 	}
187       if (ix < 0x00800000)
188 	{
189 	  /* Normalize subnormal x so exponent becomes negative.  */
190 	  ix = asuint (x * 0x1p23f);
191 	  ix &= 0x7fffffff;
192 	  ix -= 23 << 23;
193 	}
194     }
195   double_t logx = log2_inline (ix);
196   double_t ylogx = y * logx; /* Note: cannot overflow, y is single prec.  */
197   if (unlikely ((asuint64 (ylogx) >> 47 & 0xffff)
198 		 >= asuint64 (126.0 * POWF_SCALE) >> 47))
199     {
200       /* |y*log(x)| >= 126.  */
201       if (ylogx > 0x1.fffffffd1d571p+6 * POWF_SCALE)
202 	/* |x^y| > 0x1.ffffffp127.  */
203 	return __math_oflowf (sign_bias);
204       if (WANT_ROUNDING && WANT_ERRNO
205 	  && ylogx > 0x1.fffffffa3aae2p+6 * POWF_SCALE)
206 	/* |x^y| > 0x1.fffffep127, check if we round away from 0.  */
207 	if ((!sign_bias
208 	     && eval_as_float (1.0f + opt_barrier_float (0x1p-25f)) != 1.0f)
209 	    || (sign_bias
210 		&& eval_as_float (-1.0f - opt_barrier_float (0x1p-25f))
211 		     != -1.0f))
212 	  return __math_oflowf (sign_bias);
213       if (ylogx <= -150.0 * POWF_SCALE)
214 	return __math_uflowf (sign_bias);
215 #if WANT_ERRNO_UFLOW
216       if (ylogx < -149.0 * POWF_SCALE)
217 	return __math_may_uflowf (sign_bias);
218 #endif
219     }
220   return exp2_inline (ylogx, sign_bias);
221 }
222 #if USE_GLIBC_ABI
223 strong_alias (powf, __powf_finite)
224 hidden_alias (powf, __ieee754_powf)
225 #endif
226 #endif
227