1 1.1 christos /*- 2 1.1 christos * ==================================================== 3 1.1 christos * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 4 1.1 christos * 5 1.1 christos * Developed at SunPro, a Sun Microsystems, Inc. business. 6 1.1 christos * Permission to use, copy, modify, and distribute this 7 1.1 christos * software is freely granted, provided that this notice 8 1.1 christos * is preserved. 9 1.1 christos * ==================================================== 10 1.1 christos */ 11 1.1 christos 12 1.1 christos /* 13 1.1 christos * Copyright (c) 2008 Stephen L. Moshier <steve (at) moshier.net> 14 1.1 christos * 15 1.1 christos * Permission to use, copy, modify, and distribute this software for any 16 1.1 christos * purpose with or without fee is hereby granted, provided that the above 17 1.1 christos * copyright notice and this permission notice appear in all copies. 18 1.1 christos * 19 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 20 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 21 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 22 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 23 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 24 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 25 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 26 1.1 christos */ 27 1.1 christos 28 1.1 christos /* powl(x,y) return x**y 29 1.1 christos * 30 1.1 christos * n 31 1.1 christos * Method: Let x = 2 * (1+f) 32 1.1 christos * 1. Compute and return log2(x) in two pieces: 33 1.1 christos * log2(x) = w1 + w2, 34 1.1 christos * where w1 has 113-53 = 60 bit trailing zeros. 35 1.1 christos * 2. Perform y*log2(x) = n+y' by simulating multi-precision 36 1.1 christos * arithmetic, where |y'|<=0.5. 37 1.1 christos * 3. Return x**y = 2**n*exp(y'*log2) 38 1.1 christos * 39 1.1 christos * Special cases: 40 1.1 christos * 1. (anything) ** 0 is 1 41 1.1 christos * 2. (anything) ** 1 is itself 42 1.1 christos * 3. (anything) ** NAN is NAN 43 1.1 christos * 4. NAN ** (anything except 0) is NAN 44 1.1 christos * 5. +-(|x| > 1) ** +INF is +INF 45 1.1 christos * 6. +-(|x| > 1) ** -INF is +0 46 1.1 christos * 7. +-(|x| < 1) ** +INF is +0 47 1.1 christos * 8. +-(|x| < 1) ** -INF is +INF 48 1.1 christos * 9. +-1 ** +-INF is NAN 49 1.1 christos * 10. +0 ** (+anything except 0, NAN) is +0 50 1.1 christos * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 51 1.1 christos * 12. +0 ** (-anything except 0, NAN) is +INF 52 1.1 christos * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF 53 1.1 christos * 14. -0 ** (odd integer) = -( +0 ** (odd integer) ) 54 1.1 christos * 15. +INF ** (+anything except 0,NAN) is +INF 55 1.1 christos * 16. +INF ** (-anything except 0,NAN) is +0 56 1.1 christos * 17. -INF ** (anything) = -0 ** (-anything) 57 1.1 christos * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) 58 1.1 christos * 19. (-anything except 0 and inf) ** (non-integer) is NAN 59 1.1 christos * 60 1.1 christos */ 61 1.1 christos 62 1.1 christos #include <sys/cdefs.h> 63 1.1 christos #include <float.h> 64 1.1 christos #include <math.h> 65 1.1 christos 66 1.1 christos #include "math_private.h" 67 1.1 christos 68 1.1 christos static const long double bp[] = { 69 1.1 christos 1.0L, 70 1.1 christos 1.5L, 71 1.1 christos }; 72 1.1 christos 73 1.1 christos /* log_2(1.5) */ 74 1.1 christos static const long double dp_h[] = { 75 1.1 christos 0.0, 76 1.1 christos 5.8496250072115607565592654282227158546448E-1L 77 1.1 christos }; 78 1.1 christos 79 1.1 christos /* Low part of log_2(1.5) */ 80 1.1 christos static const long double dp_l[] = { 81 1.1 christos 0.0, 82 1.1 christos 1.0579781240112554492329533686862998106046E-16L 83 1.1 christos }; 84 1.1 christos 85 1.1 christos static const long double zero = 0.0L, 86 1.1 christos one = 1.0L, 87 1.1 christos two = 2.0L, 88 1.1 christos two113 = 1.0384593717069655257060992658440192E34L, 89 1.1 christos huge = 1.0e3000L, 90 1.1 christos tiny = 1.0e-3000L; 91 1.1 christos 92 1.1 christos /* 3/2 log x = 3 z + z^3 + z^3 (z^2 R(z^2)) 93 1.1 christos z = (x-1)/(x+1) 94 1.1 christos 1 <= x <= 1.25 95 1.1 christos Peak relative error 2.3e-37 */ 96 1.1 christos static const long double LN[] = 97 1.1 christos { 98 1.1 christos -3.0779177200290054398792536829702930623200E1L, 99 1.1 christos 6.5135778082209159921251824580292116201640E1L, 100 1.1 christos -4.6312921812152436921591152809994014413540E1L, 101 1.1 christos 1.2510208195629420304615674658258363295208E1L, 102 1.1 christos -9.9266909031921425609179910128531667336670E-1L 103 1.1 christos }; 104 1.1 christos static const long double LD[] = 105 1.1 christos { 106 1.1 christos -5.129862866715009066465422805058933131960E1L, 107 1.1 christos 1.452015077564081884387441590064272782044E2L, 108 1.1 christos -1.524043275549860505277434040464085593165E2L, 109 1.1 christos 7.236063513651544224319663428634139768808E1L, 110 1.1 christos -1.494198912340228235853027849917095580053E1L 111 1.1 christos /* 1.0E0 */ 112 1.1 christos }; 113 1.1 christos 114 1.1 christos /* exp(x) = 1 + x - x / (1 - 2 / (x - x^2 R(x^2))) 115 1.1 christos 0 <= x <= 0.5 116 1.1 christos Peak relative error 5.7e-38 */ 117 1.1 christos static const long double PN[] = 118 1.1 christos { 119 1.1 christos 5.081801691915377692446852383385968225675E8L, 120 1.1 christos 9.360895299872484512023336636427675327355E6L, 121 1.1 christos 4.213701282274196030811629773097579432957E4L, 122 1.1 christos 5.201006511142748908655720086041570288182E1L, 123 1.1 christos 9.088368420359444263703202925095675982530E-3L, 124 1.1 christos }; 125 1.1 christos static const long double PD[] = 126 1.1 christos { 127 1.1 christos 3.049081015149226615468111430031590411682E9L, 128 1.1 christos 1.069833887183886839966085436512368982758E8L, 129 1.1 christos 8.259257717868875207333991924545445705394E5L, 130 1.1 christos 1.872583833284143212651746812884298360922E3L, 131 1.1 christos /* 1.0E0 */ 132 1.1 christos }; 133 1.1 christos 134 1.1 christos static const long double 135 1.1 christos /* ln 2 */ 136 1.1 christos lg2 = 6.9314718055994530941723212145817656807550E-1L, 137 1.1 christos lg2_h = 6.9314718055994528622676398299518041312695E-1L, 138 1.1 christos lg2_l = 2.3190468138462996154948554638754786504121E-17L, 139 1.1 christos ovt = 8.0085662595372944372e-0017L, 140 1.1 christos /* 2/(3*log(2)) */ 141 1.1 christos cp = 9.6179669392597560490661645400126142495110E-1L, 142 1.1 christos cp_h = 9.6179669392597555432899980587535537779331E-1L, 143 1.1 christos cp_l = 5.0577616648125906047157785230014751039424E-17L; 144 1.1 christos 145 1.1 christos long double 146 1.1 christos powl(long double x, long double y) 147 1.1 christos { 148 1.1 christos long double z, ax, z_h, z_l, p_h, p_l; 149 1.1 christos long double yy1, t1, t2, r, s, t, u, v, w; 150 1.1 christos long double s2, s_h, s_l, t_h, t_l; 151 1.1 christos int32_t i, j, k, yisint, n; 152 1.1 christos u_int32_t ix, iy; 153 1.1 christos int32_t hx, hy; 154 1.1 christos ieee_quad_shape_type o, p, q; 155 1.1 christos 156 1.1 christos p.value = x; 157 1.1 christos hx = p.parts32.mswhi; 158 1.1 christos ix = hx & 0x7fffffff; 159 1.1 christos 160 1.1 christos q.value = y; 161 1.1 christos hy = q.parts32.mswhi; 162 1.1 christos iy = hy & 0x7fffffff; 163 1.1 christos 164 1.1 christos 165 1.1 christos /* y==zero: x**0 = 1 */ 166 1.1 christos if ((iy | q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0) 167 1.1 christos return one; 168 1.1 christos 169 1.1 christos /* 1.0**y = 1; -1.0**+-Inf = 1 */ 170 1.1 christos if (x == one) 171 1.1 christos return one; 172 1.1 christos if (x == -1.0L && iy == 0x7fff0000 173 1.1 christos && (q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0) 174 1.1 christos return one; 175 1.1 christos 176 1.1 christos /* +-NaN return x+y */ 177 1.1 christos if ((ix > 0x7fff0000) 178 1.1 christos || ((ix == 0x7fff0000) 179 1.1 christos && ((p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) != 0)) 180 1.1 christos || (iy > 0x7fff0000) 181 1.1 christos || ((iy == 0x7fff0000) 182 1.1 christos && ((q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) != 0))) 183 1.1 christos return nan_mix(x, y); 184 1.1 christos 185 1.1 christos /* determine if y is an odd int when x < 0 186 1.1 christos * yisint = 0 ... y is not an integer 187 1.1 christos * yisint = 1 ... y is an odd int 188 1.1 christos * yisint = 2 ... y is an even int 189 1.1 christos */ 190 1.1 christos yisint = 0; 191 1.1 christos if (hx < 0) 192 1.1 christos { 193 1.1 christos if (iy >= 0x40700000) /* 2^113 */ 194 1.1 christos yisint = 2; /* even integer y */ 195 1.1 christos else if (iy >= 0x3fff0000) /* 1.0 */ 196 1.1 christos { 197 1.1 christos if (floorl (y) == y) 198 1.1 christos { 199 1.1 christos z = 0.5 * y; 200 1.1 christos if (floorl (z) == z) 201 1.1 christos yisint = 2; 202 1.1 christos else 203 1.1 christos yisint = 1; 204 1.1 christos } 205 1.1 christos } 206 1.1 christos } 207 1.1 christos 208 1.1 christos /* special value of y */ 209 1.1 christos if ((q.parts32.mswlo | q.parts32.lswhi | q.parts32.lswlo) == 0) 210 1.1 christos { 211 1.1 christos if (iy == 0x7fff0000) /* y is +-inf */ 212 1.1 christos { 213 1.1 christos if (((ix - 0x3fff0000) | p.parts32.mswlo | p.parts32.lswhi | 214 1.1 christos p.parts32.lswlo) == 0) 215 1.1 christos return y - y; /* +-1**inf is NaN */ 216 1.1 christos else if (ix >= 0x3fff0000) /* (|x|>1)**+-inf = inf,0 */ 217 1.1 christos return (hy >= 0) ? y : zero; 218 1.1 christos else /* (|x|<1)**-,+inf = inf,0 */ 219 1.1 christos return (hy < 0) ? -y : zero; 220 1.1 christos } 221 1.1 christos if (iy == 0x3fff0000) 222 1.1 christos { /* y is +-1 */ 223 1.1 christos if (hy < 0) 224 1.1 christos return one / x; 225 1.1 christos else 226 1.1 christos return x; 227 1.1 christos } 228 1.1 christos if (hy == 0x40000000) 229 1.1 christos return x * x; /* y is 2 */ 230 1.1 christos if (hy == 0x3ffe0000) 231 1.1 christos { /* y is 0.5 */ 232 1.1 christos if (hx >= 0) /* x >= +0 */ 233 1.1 christos return sqrtl (x); 234 1.1 christos } 235 1.1 christos } 236 1.1 christos 237 1.1 christos ax = fabsl (x); 238 1.1 christos /* special value of x */ 239 1.1 christos if ((p.parts32.mswlo | p.parts32.lswhi | p.parts32.lswlo) == 0) 240 1.1 christos { 241 1.1 christos if (ix == 0x7fff0000 || ix == 0 || ix == 0x3fff0000) 242 1.1 christos { 243 1.1 christos z = ax; /*x is +-0,+-inf,+-1 */ 244 1.1 christos if (hy < 0) 245 1.1 christos z = one / z; /* z = (1/|x|) */ 246 1.1 christos if (hx < 0) 247 1.1 christos { 248 1.1 christos if (((ix - 0x3fff0000) | yisint) == 0) 249 1.1 christos { 250 1.1 christos z = (z - z) / (z - z); /* (-1)**non-int is NaN */ 251 1.1 christos } 252 1.1 christos else if (yisint == 1) 253 1.1 christos z = -z; /* (x<0)**odd = -(|x|**odd) */ 254 1.1 christos } 255 1.1 christos return z; 256 1.1 christos } 257 1.1 christos } 258 1.1 christos 259 1.1 christos /* (x<0)**(non-int) is NaN */ 260 1.1 christos if (((((u_int32_t) hx >> 31) - 1) | yisint) == 0) 261 1.1 christos return (x - x) / (x - x); 262 1.1 christos 263 1.1 christos /* |y| is huge. 264 1.1 christos 2^-16495 = 1/2 of smallest representable value. 265 1.1 christos If (1 - 1/131072)^y underflows, y > 1.4986e9 */ 266 1.1 christos if (iy > 0x401d654b) 267 1.1 christos { 268 1.1 christos /* if (1 - 2^-113)^y underflows, y > 1.1873e38 */ 269 1.1 christos if (iy > 0x407d654b) 270 1.1 christos { 271 1.1 christos if (ix <= 0x3ffeffff) 272 1.1 christos return (hy < 0) ? huge * huge : tiny * tiny; 273 1.1 christos if (ix >= 0x3fff0000) 274 1.1 christos return (hy > 0) ? huge * huge : tiny * tiny; 275 1.1 christos } 276 1.1 christos /* over/underflow if x is not close to one */ 277 1.1 christos if (ix < 0x3ffeffff) 278 1.1 christos return (hy < 0) ? huge * huge : tiny * tiny; 279 1.1 christos if (ix > 0x3fff0000) 280 1.1 christos return (hy > 0) ? huge * huge : tiny * tiny; 281 1.1 christos } 282 1.1 christos 283 1.1 christos n = 0; 284 1.1 christos /* take care subnormal number */ 285 1.1 christos if (ix < 0x00010000) 286 1.1 christos { 287 1.1 christos ax *= two113; 288 1.1 christos n -= 113; 289 1.1 christos o.value = ax; 290 1.1 christos ix = o.parts32.mswhi; 291 1.1 christos } 292 1.1 christos n += ((ix) >> 16) - 0x3fff; 293 1.1 christos j = ix & 0x0000ffff; 294 1.1 christos /* determine interval */ 295 1.1 christos ix = j | 0x3fff0000; /* normalize ix */ 296 1.1 christos if (j <= 0x3988) 297 1.1 christos k = 0; /* |x|<sqrt(3/2) */ 298 1.1 christos else if (j < 0xbb67) 299 1.1 christos k = 1; /* |x|<sqrt(3) */ 300 1.1 christos else 301 1.1 christos { 302 1.1 christos k = 0; 303 1.1 christos n += 1; 304 1.1 christos ix -= 0x00010000; 305 1.1 christos } 306 1.1 christos 307 1.1 christos o.value = ax; 308 1.1 christos o.parts32.mswhi = ix; 309 1.1 christos ax = o.value; 310 1.1 christos 311 1.1 christos /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ 312 1.1 christos u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */ 313 1.1 christos v = one / (ax + bp[k]); 314 1.1 christos s = u * v; 315 1.1 christos s_h = s; 316 1.1 christos 317 1.1 christos o.value = s_h; 318 1.1 christos o.parts32.lswlo = 0; 319 1.1 christos o.parts32.lswhi &= 0xf8000000; 320 1.1 christos s_h = o.value; 321 1.1 christos /* t_h=ax+bp[k] High */ 322 1.1 christos t_h = ax + bp[k]; 323 1.1 christos o.value = t_h; 324 1.1 christos o.parts32.lswlo = 0; 325 1.1 christos o.parts32.lswhi &= 0xf8000000; 326 1.1 christos t_h = o.value; 327 1.1 christos t_l = ax - (t_h - bp[k]); 328 1.1 christos s_l = v * ((u - s_h * t_h) - s_h * t_l); 329 1.1 christos /* compute log(ax) */ 330 1.1 christos s2 = s * s; 331 1.1 christos u = LN[0] + s2 * (LN[1] + s2 * (LN[2] + s2 * (LN[3] + s2 * LN[4]))); 332 1.1 christos v = LD[0] + s2 * (LD[1] + s2 * (LD[2] + s2 * (LD[3] + s2 * (LD[4] + s2)))); 333 1.1 christos r = s2 * s2 * u / v; 334 1.1 christos r += s_l * (s_h + s); 335 1.1 christos s2 = s_h * s_h; 336 1.1 christos t_h = 3.0 + s2 + r; 337 1.1 christos o.value = t_h; 338 1.1 christos o.parts32.lswlo = 0; 339 1.1 christos o.parts32.lswhi &= 0xf8000000; 340 1.1 christos t_h = o.value; 341 1.1 christos t_l = r - ((t_h - 3.0) - s2); 342 1.1 christos /* u+v = s*(1+...) */ 343 1.1 christos u = s_h * t_h; 344 1.1 christos v = s_l * t_h + t_l * s; 345 1.1 christos /* 2/(3log2)*(s+...) */ 346 1.1 christos p_h = u + v; 347 1.1 christos o.value = p_h; 348 1.1 christos o.parts32.lswlo = 0; 349 1.1 christos o.parts32.lswhi &= 0xf8000000; 350 1.1 christos p_h = o.value; 351 1.1 christos p_l = v - (p_h - u); 352 1.1 christos z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */ 353 1.1 christos z_l = cp_l * p_h + p_l * cp + dp_l[k]; 354 1.1 christos /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */ 355 1.1 christos t = (long double) n; 356 1.1 christos t1 = (((z_h + z_l) + dp_h[k]) + t); 357 1.1 christos o.value = t1; 358 1.1 christos o.parts32.lswlo = 0; 359 1.1 christos o.parts32.lswhi &= 0xf8000000; 360 1.1 christos t1 = o.value; 361 1.1 christos t2 = z_l - (((t1 - t) - dp_h[k]) - z_h); 362 1.1 christos 363 1.1 christos /* s (sign of result -ve**odd) = -1 else = 1 */ 364 1.1 christos s = one; 365 1.1 christos if (((((u_int32_t) hx >> 31) - 1) | (yisint - 1)) == 0) 366 1.1 christos s = -one; /* (-ve)**(odd int) */ 367 1.1 christos 368 1.1 christos /* split up y into yy1+y2 and compute (yy1+y2)*(t1+t2) */ 369 1.1 christos yy1 = y; 370 1.1 christos o.value = yy1; 371 1.1 christos o.parts32.lswlo = 0; 372 1.1 christos o.parts32.lswhi &= 0xf8000000; 373 1.1 christos yy1 = o.value; 374 1.1 christos p_l = (y - yy1) * t1 + y * t2; 375 1.1 christos p_h = yy1 * t1; 376 1.1 christos z = p_l + p_h; 377 1.1 christos o.value = z; 378 1.1 christos j = o.parts32.mswhi; 379 1.1 christos if (j >= 0x400d0000) /* z >= 16384 */ 380 1.1 christos { 381 1.1 christos /* if z > 16384 */ 382 1.1 christos if (((j - 0x400d0000) | o.parts32.mswlo | o.parts32.lswhi | 383 1.1 christos o.parts32.lswlo) != 0) 384 1.1 christos return s * huge * huge; /* overflow */ 385 1.1 christos else 386 1.1 christos { 387 1.1 christos if (p_l + ovt > z - p_h) 388 1.1 christos return s * huge * huge; /* overflow */ 389 1.1 christos } 390 1.1 christos } 391 1.1 christos else if ((j & 0x7fffffff) >= 0x400d01b9) /* z <= -16495 */ 392 1.1 christos { 393 1.1 christos /* z < -16495 */ 394 1.1 christos if (((j - 0xc00d01bc) | o.parts32.mswlo | o.parts32.lswhi | 395 1.1 christos o.parts32.lswlo) 396 1.1 christos != 0) 397 1.1 christos return s * tiny * tiny; /* underflow */ 398 1.1 christos else 399 1.1 christos { 400 1.1 christos if (p_l <= z - p_h) 401 1.1 christos return s * tiny * tiny; /* underflow */ 402 1.1 christos } 403 1.1 christos } 404 1.1 christos /* compute 2**(p_h+p_l) */ 405 1.1 christos i = j & 0x7fffffff; 406 1.1 christos k = (i >> 16) - 0x3fff; 407 1.1 christos n = 0; 408 1.1 christos if (i > 0x3ffe0000) 409 1.1 christos { /* if |z| > 0.5, set n = [z+0.5] */ 410 1.1 christos n = floorl (z + 0.5L); 411 1.1 christos t = n; 412 1.1 christos p_h -= t; 413 1.1 christos } 414 1.1 christos t = p_l + p_h; 415 1.1 christos o.value = t; 416 1.1 christos o.parts32.lswlo = 0; 417 1.1 christos o.parts32.lswhi &= 0xf8000000; 418 1.1 christos t = o.value; 419 1.1 christos u = t * lg2_h; 420 1.1 christos v = (p_l - (t - p_h)) * lg2 + t * lg2_l; 421 1.1 christos z = u + v; 422 1.1 christos w = v - (z - u); 423 1.1 christos /* exp(z) */ 424 1.1 christos t = z * z; 425 1.1 christos u = PN[0] + t * (PN[1] + t * (PN[2] + t * (PN[3] + t * PN[4]))); 426 1.1 christos v = PD[0] + t * (PD[1] + t * (PD[2] + t * (PD[3] + t))); 427 1.1 christos t1 = z - t * u / v; 428 1.1 christos r = (z * t1) / (t1 - two) - (w + z * w); 429 1.1 christos z = one - (r - z); 430 1.1 christos o.value = z; 431 1.1 christos j = o.parts32.mswhi; 432 1.1 christos j += (n << 16); 433 1.1 christos if ((j >> 16) <= 0) 434 1.1 christos z = scalbnl (z, n); /* subnormal output */ 435 1.1 christos else 436 1.1 christos { 437 1.1 christos o.parts32.mswhi = j; 438 1.1 christos z = o.value; 439 1.1 christos } 440 1.1 christos return s * z; 441 1.1 christos } 442