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