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