18346e333Sjtc /* e_j0f.c -- float version of e_j0.c.
28346e333Sjtc * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
38346e333Sjtc */
48346e333Sjtc
58346e333Sjtc /*
68346e333Sjtc * ====================================================
78346e333Sjtc * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
88346e333Sjtc *
98346e333Sjtc * Developed at SunPro, a Sun Microsystems, Inc. business.
108346e333Sjtc * Permission to use, copy, modify, and distribute this
118346e333Sjtc * software is freely granted, provided that this notice
128346e333Sjtc * is preserved.
138346e333Sjtc * ====================================================
148346e333Sjtc */
158346e333Sjtc
16dd7adfbfSlukem #include <sys/cdefs.h>
17d1f06e0bSjtc #if defined(LIBM_SCCS) && !defined(lint)
18*7b2c2e7dSmaya __RCSID("$NetBSD: e_j0f.c,v 1.11 2017/02/09 21:23:11 maya Exp $");
198346e333Sjtc #endif
208346e333Sjtc
219d8b5fa7Sdrochner #include "namespace.h"
228346e333Sjtc #include "math.h"
238346e333Sjtc #include "math_private.h"
248346e333Sjtc
258346e333Sjtc static float pzerof(float), qzerof(float);
268346e333Sjtc
278346e333Sjtc static const float
288346e333Sjtc huge = 1e30,
298346e333Sjtc one = 1.0,
308346e333Sjtc invsqrtpi= 5.6418961287e-01, /* 0x3f106ebb */
318346e333Sjtc tpi = 6.3661974669e-01, /* 0x3f22f983 */
328346e333Sjtc /* R0/S0 on [0, 2.00] */
338346e333Sjtc R02 = 1.5625000000e-02, /* 0x3c800000 */
348346e333Sjtc R03 = -1.8997929874e-04, /* 0xb947352e */
358346e333Sjtc R04 = 1.8295404516e-06, /* 0x35f58e88 */
368346e333Sjtc R05 = -4.6183270541e-09, /* 0xb19eaf3c */
378346e333Sjtc S01 = 1.5619102865e-02, /* 0x3c7fe744 */
388346e333Sjtc S02 = 1.1692678527e-04, /* 0x38f53697 */
398346e333Sjtc S03 = 5.1354652442e-07, /* 0x3509daa6 */
408346e333Sjtc S04 = 1.1661400734e-09; /* 0x30a045e8 */
418346e333Sjtc
428346e333Sjtc static const float zero = 0.0;
438346e333Sjtc
44aa30599eSwiz float
__ieee754_j0f(float x)45aa30599eSwiz __ieee754_j0f(float x)
468346e333Sjtc {
478346e333Sjtc float z, s,c,ss,cc,r,u,v;
48b0c9d092Sjtc int32_t hx,ix;
498346e333Sjtc
508346e333Sjtc GET_FLOAT_WORD(hx,x);
518346e333Sjtc ix = hx&0x7fffffff;
528346e333Sjtc if(ix>=0x7f800000) return one/(x*x);
538346e333Sjtc x = fabsf(x);
548346e333Sjtc if(ix >= 0x40000000) { /* |x| >= 2.0 */
558346e333Sjtc s = sinf(x);
568346e333Sjtc c = cosf(x);
578346e333Sjtc ss = s-c;
588346e333Sjtc cc = s+c;
598346e333Sjtc if(ix<0x7f000000) { /* make sure x+x not overflow */
608346e333Sjtc z = -cosf(x+x);
618346e333Sjtc if ((s*c)<zero) cc = z/ss;
628346e333Sjtc else ss = z/cc;
638346e333Sjtc }
648346e333Sjtc /*
658346e333Sjtc * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x)
668346e333Sjtc * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x)
678346e333Sjtc */
6815da190fSchristos #ifdef DEAD_CODE
698346e333Sjtc if(ix>0x80000000) z = (invsqrtpi*cc)/sqrtf(x);
7015da190fSchristos else
7115da190fSchristos #endif
7215da190fSchristos {
738346e333Sjtc u = pzerof(x); v = qzerof(x);
748346e333Sjtc z = invsqrtpi*(u*cc-v*ss)/sqrtf(x);
758346e333Sjtc }
768346e333Sjtc return z;
778346e333Sjtc }
788346e333Sjtc if(ix<0x39000000) { /* |x| < 2**-13 */
798346e333Sjtc if(huge+x>one) { /* raise inexact if x != 0 */
808346e333Sjtc if(ix<0x32000000) return one; /* |x|<2**-27 */
818346e333Sjtc else return one - (float)0.25*x*x;
828346e333Sjtc }
838346e333Sjtc }
848346e333Sjtc z = x*x;
858346e333Sjtc r = z*(R02+z*(R03+z*(R04+z*R05)));
868346e333Sjtc s = one+z*(S01+z*(S02+z*(S03+z*S04)));
878346e333Sjtc if(ix < 0x3F800000) { /* |x| < 1.00 */
888346e333Sjtc return one + z*((float)-0.25+(r/s));
898346e333Sjtc } else {
908346e333Sjtc u = (float)0.5*x;
918346e333Sjtc return((one+u)*(one-u)+z*(r/s));
928346e333Sjtc }
938346e333Sjtc }
948346e333Sjtc
958346e333Sjtc static const float
968346e333Sjtc u00 = -7.3804296553e-02, /* 0xbd9726b5 */
978346e333Sjtc u01 = 1.7666645348e-01, /* 0x3e34e80d */
988346e333Sjtc u02 = -1.3818567619e-02, /* 0xbc626746 */
998346e333Sjtc u03 = 3.4745343146e-04, /* 0x39b62a69 */
1008346e333Sjtc u04 = -3.8140706238e-06, /* 0xb67ff53c */
1018346e333Sjtc u05 = 1.9559013964e-08, /* 0x32a802ba */
1028346e333Sjtc u06 = -3.9820518410e-11, /* 0xae2f21eb */
1038346e333Sjtc v01 = 1.2730483897e-02, /* 0x3c509385 */
1048346e333Sjtc v02 = 7.6006865129e-05, /* 0x389f65e0 */
1058346e333Sjtc v03 = 2.5915085189e-07, /* 0x348b216c */
1068346e333Sjtc v04 = 4.4111031494e-10; /* 0x2ff280c2 */
1078346e333Sjtc
108aa30599eSwiz float
__ieee754_y0f(float x)109aa30599eSwiz __ieee754_y0f(float x)
1108346e333Sjtc {
1118346e333Sjtc float z, s,c,ss,cc,u,v;
112b0c9d092Sjtc int32_t hx,ix;
1138346e333Sjtc
1148346e333Sjtc GET_FLOAT_WORD(hx,x);
1158346e333Sjtc ix = 0x7fffffff&hx;
1168346e333Sjtc /* Y0(NaN) is NaN, y0(-inf) is Nan, y0(inf) is 0 */
1178346e333Sjtc if(ix>=0x7f800000) return one/(x+x*x);
1188346e333Sjtc if(ix==0) return -one/zero;
1198346e333Sjtc if(hx<0) return zero/zero;
1208346e333Sjtc if(ix >= 0x40000000) { /* |x| >= 2.0 */
1218346e333Sjtc /* y0(x) = sqrt(2/(pi*x))*(p0(x)*sin(x0)+q0(x)*cos(x0))
1228346e333Sjtc * where x0 = x-pi/4
1238346e333Sjtc * Better formula:
1248346e333Sjtc * cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4)
1258346e333Sjtc * = 1/sqrt(2) * (sin(x) + cos(x))
1268346e333Sjtc * sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
1278346e333Sjtc * = 1/sqrt(2) * (sin(x) - cos(x))
1288346e333Sjtc * To avoid cancellation, use
1298346e333Sjtc * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
1308346e333Sjtc * to compute the worse one.
1318346e333Sjtc */
1328346e333Sjtc s = sinf(x);
1338346e333Sjtc c = cosf(x);
1348346e333Sjtc ss = s-c;
1358346e333Sjtc cc = s+c;
1368346e333Sjtc /*
1378346e333Sjtc * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x)
1388346e333Sjtc * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x)
1398346e333Sjtc */
1408346e333Sjtc if(ix<0x7f000000) { /* make sure x+x not overflow */
1418346e333Sjtc z = -cosf(x+x);
1428346e333Sjtc if ((s*c)<zero) cc = z/ss;
1438346e333Sjtc else ss = z/cc;
1448346e333Sjtc }
145fef13708Schristos #ifdef DEAD_CODE
1468346e333Sjtc if(ix>0x80000000) z = (invsqrtpi*ss)/sqrtf(x);
147fef13708Schristos else
148fef13708Schristos #endif
149fef13708Schristos {
1508346e333Sjtc u = pzerof(x); v = qzerof(x);
1518346e333Sjtc z = invsqrtpi*(u*ss+v*cc)/sqrtf(x);
1528346e333Sjtc }
1538346e333Sjtc return z;
1548346e333Sjtc }
1558346e333Sjtc if(ix<=0x32000000) { /* x < 2**-27 */
1568346e333Sjtc return(u00 + tpi*__ieee754_logf(x));
1578346e333Sjtc }
1588346e333Sjtc z = x*x;
1598346e333Sjtc u = u00+z*(u01+z*(u02+z*(u03+z*(u04+z*(u05+z*u06)))));
1608346e333Sjtc v = one+z*(v01+z*(v02+z*(v03+z*v04)));
1618346e333Sjtc return(u/v + tpi*(__ieee754_j0f(x)*__ieee754_logf(x)));
1628346e333Sjtc }
1638346e333Sjtc
1648346e333Sjtc /* The asymptotic expansions of pzero is
1658346e333Sjtc * 1 - 9/128 s^2 + 11025/98304 s^4 - ..., where s = 1/x.
1668346e333Sjtc * For x >= 2, We approximate pzero by
1678346e333Sjtc * pzero(x) = 1 + (R/S)
1688346e333Sjtc * where R = pR0 + pR1*s^2 + pR2*s^4 + ... + pR5*s^10
1698346e333Sjtc * S = 1 + pS0*s^2 + ... + pS4*s^10
1708346e333Sjtc * and
1718346e333Sjtc * | pzero(x)-1-R/S | <= 2 ** ( -60.26)
1728346e333Sjtc */
1738346e333Sjtc static const float pR8[6] = { /* for x in [inf, 8]=1/[0,0.125] */
1748346e333Sjtc 0.0000000000e+00, /* 0x00000000 */
1758346e333Sjtc -7.0312500000e-02, /* 0xbd900000 */
1768346e333Sjtc -8.0816707611e+00, /* 0xc1014e86 */
1778346e333Sjtc -2.5706311035e+02, /* 0xc3808814 */
1788346e333Sjtc -2.4852163086e+03, /* 0xc51b5376 */
1798346e333Sjtc -5.2530439453e+03, /* 0xc5a4285a */
1808346e333Sjtc };
1818346e333Sjtc static const float pS8[5] = {
1828346e333Sjtc 1.1653436279e+02, /* 0x42e91198 */
1838346e333Sjtc 3.8337448730e+03, /* 0x456f9beb */
1848346e333Sjtc 4.0597855469e+04, /* 0x471e95db */
1858346e333Sjtc 1.1675296875e+05, /* 0x47e4087c */
1868346e333Sjtc 4.7627726562e+04, /* 0x473a0bba */
1878346e333Sjtc };
1888346e333Sjtc static const float pR5[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
1898346e333Sjtc -1.1412546255e-11, /* 0xad48c58a */
1908346e333Sjtc -7.0312492549e-02, /* 0xbd8fffff */
1918346e333Sjtc -4.1596107483e+00, /* 0xc0851b88 */
1928346e333Sjtc -6.7674766541e+01, /* 0xc287597b */
1938346e333Sjtc -3.3123129272e+02, /* 0xc3a59d9b */
1948346e333Sjtc -3.4643338013e+02, /* 0xc3ad3779 */
1958346e333Sjtc };
1968346e333Sjtc static const float pS5[5] = {
1978346e333Sjtc 6.0753936768e+01, /* 0x42730408 */
1988346e333Sjtc 1.0512523193e+03, /* 0x44836813 */
1998346e333Sjtc 5.9789707031e+03, /* 0x45bad7c4 */
2008346e333Sjtc 9.6254453125e+03, /* 0x461665c8 */
2018346e333Sjtc 2.4060581055e+03, /* 0x451660ee */
2028346e333Sjtc };
2038346e333Sjtc
2048346e333Sjtc static const float pR3[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
2058346e333Sjtc -2.5470459075e-09, /* 0xb12f081b */
2068346e333Sjtc -7.0311963558e-02, /* 0xbd8fffb8 */
2078346e333Sjtc -2.4090321064e+00, /* 0xc01a2d95 */
2088346e333Sjtc -2.1965976715e+01, /* 0xc1afba52 */
2098346e333Sjtc -5.8079170227e+01, /* 0xc2685112 */
2108346e333Sjtc -3.1447946548e+01, /* 0xc1fb9565 */
2118346e333Sjtc };
2128346e333Sjtc static const float pS3[5] = {
2138346e333Sjtc 3.5856033325e+01, /* 0x420f6c94 */
2148346e333Sjtc 3.6151397705e+02, /* 0x43b4c1ca */
2158346e333Sjtc 1.1936077881e+03, /* 0x44953373 */
2168346e333Sjtc 1.1279968262e+03, /* 0x448cffe6 */
2178346e333Sjtc 1.7358093262e+02, /* 0x432d94b8 */
2188346e333Sjtc };
2198346e333Sjtc
2208346e333Sjtc static const float pR2[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
2218346e333Sjtc -8.8753431271e-08, /* 0xb3be98b7 */
2228346e333Sjtc -7.0303097367e-02, /* 0xbd8ffb12 */
2238346e333Sjtc -1.4507384300e+00, /* 0xbfb9b1cc */
2248346e333Sjtc -7.6356959343e+00, /* 0xc0f4579f */
2258346e333Sjtc -1.1193166733e+01, /* 0xc1331736 */
2268346e333Sjtc -3.2336456776e+00, /* 0xc04ef40d */
2278346e333Sjtc };
2288346e333Sjtc static const float pS2[5] = {
2298346e333Sjtc 2.2220300674e+01, /* 0x41b1c32d */
2308346e333Sjtc 1.3620678711e+02, /* 0x430834f0 */
2318346e333Sjtc 2.7047027588e+02, /* 0x43873c32 */
2328346e333Sjtc 1.5387539673e+02, /* 0x4319e01a */
2338346e333Sjtc 1.4657617569e+01, /* 0x416a859a */
2348346e333Sjtc };
2358346e333Sjtc
236aa30599eSwiz static float
pzerof(float x)237aa30599eSwiz pzerof(float x)
2388346e333Sjtc {
2398346e333Sjtc const float *p,*q;
2408346e333Sjtc float z,r,s;
241b0c9d092Sjtc int32_t ix;
242dd7adfbfSlukem
2438346e333Sjtc GET_FLOAT_WORD(ix,x);
2448346e333Sjtc ix &= 0x7fffffff;
2458346e333Sjtc if(ix>=0x41000000) {p = pR8; q= pS8;}
2468346e333Sjtc else if(ix>=0x40f71c58) {p = pR5; q= pS5;}
2478346e333Sjtc else if(ix>=0x4036db68) {p = pR3; q= pS3;}
248*7b2c2e7dSmaya else /*if(ix>=0x40000000)*/{p = pR2; q= pS2;}
2498346e333Sjtc z = one/(x*x);
2508346e333Sjtc r = p[0]+z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5]))));
2518346e333Sjtc s = one+z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*q[4]))));
2528346e333Sjtc return one+ r/s;
2538346e333Sjtc }
2548346e333Sjtc
2558346e333Sjtc
2568346e333Sjtc /* For x >= 8, the asymptotic expansions of qzero is
2578346e333Sjtc * -1/8 s + 75/1024 s^3 - ..., where s = 1/x.
2588346e333Sjtc * We approximate pzero by
2598346e333Sjtc * qzero(x) = s*(-1.25 + (R/S))
2608346e333Sjtc * where R = qR0 + qR1*s^2 + qR2*s^4 + ... + qR5*s^10
2618346e333Sjtc * S = 1 + qS0*s^2 + ... + qS5*s^12
2628346e333Sjtc * and
2638346e333Sjtc * | qzero(x)/s +1.25-R/S | <= 2 ** ( -61.22)
2648346e333Sjtc */
2658346e333Sjtc static const float qR8[6] = { /* for x in [inf, 8]=1/[0,0.125] */
2668346e333Sjtc 0.0000000000e+00, /* 0x00000000 */
2678346e333Sjtc 7.3242187500e-02, /* 0x3d960000 */
2688346e333Sjtc 1.1768206596e+01, /* 0x413c4a93 */
2698346e333Sjtc 5.5767340088e+02, /* 0x440b6b19 */
2708346e333Sjtc 8.8591972656e+03, /* 0x460a6cca */
2718346e333Sjtc 3.7014625000e+04, /* 0x471096a0 */
2728346e333Sjtc };
2738346e333Sjtc static const float qS8[6] = {
2748346e333Sjtc 1.6377603149e+02, /* 0x4323c6aa */
2758346e333Sjtc 8.0983447266e+03, /* 0x45fd12c2 */
2768346e333Sjtc 1.4253829688e+05, /* 0x480b3293 */
2778346e333Sjtc 8.0330925000e+05, /* 0x49441ed4 */
2788346e333Sjtc 8.4050156250e+05, /* 0x494d3359 */
2798346e333Sjtc -3.4389928125e+05, /* 0xc8a7eb69 */
2808346e333Sjtc };
2818346e333Sjtc
2828346e333Sjtc static const float qR5[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
2838346e333Sjtc 1.8408595828e-11, /* 0x2da1ec79 */
2848346e333Sjtc 7.3242180049e-02, /* 0x3d95ffff */
2858346e333Sjtc 5.8356351852e+00, /* 0x40babd86 */
2868346e333Sjtc 1.3511157227e+02, /* 0x43071c90 */
2878346e333Sjtc 1.0272437744e+03, /* 0x448067cd */
2888346e333Sjtc 1.9899779053e+03, /* 0x44f8bf4b */
2898346e333Sjtc };
2908346e333Sjtc static const float qS5[6] = {
2918346e333Sjtc 8.2776611328e+01, /* 0x42a58da0 */
2928346e333Sjtc 2.0778142090e+03, /* 0x4501dd07 */
2938346e333Sjtc 1.8847289062e+04, /* 0x46933e94 */
2948346e333Sjtc 5.6751113281e+04, /* 0x475daf1d */
2958346e333Sjtc 3.5976753906e+04, /* 0x470c88c1 */
2968346e333Sjtc -5.3543427734e+03, /* 0xc5a752be */
2978346e333Sjtc };
2988346e333Sjtc
2998346e333Sjtc static const float qR3[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
3008346e333Sjtc 4.3774099900e-09, /* 0x3196681b */
3018346e333Sjtc 7.3241114616e-02, /* 0x3d95ff70 */
3028346e333Sjtc 3.3442313671e+00, /* 0x405607e3 */
3038346e333Sjtc 4.2621845245e+01, /* 0x422a7cc5 */
3048346e333Sjtc 1.7080809021e+02, /* 0x432acedf */
3058346e333Sjtc 1.6673394775e+02, /* 0x4326bbe4 */
3068346e333Sjtc };
3078346e333Sjtc static const float qS3[6] = {
3088346e333Sjtc 4.8758872986e+01, /* 0x42430916 */
3098346e333Sjtc 7.0968920898e+02, /* 0x44316c1c */
3108346e333Sjtc 3.7041481934e+03, /* 0x4567825f */
3118346e333Sjtc 6.4604252930e+03, /* 0x45c9e367 */
3128346e333Sjtc 2.5163337402e+03, /* 0x451d4557 */
3138346e333Sjtc -1.4924745178e+02, /* 0xc3153f59 */
3148346e333Sjtc };
3158346e333Sjtc
3168346e333Sjtc static const float qR2[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
3178346e333Sjtc 1.5044444979e-07, /* 0x342189db */
3188346e333Sjtc 7.3223426938e-02, /* 0x3d95f62a */
3198346e333Sjtc 1.9981917143e+00, /* 0x3fffc4bf */
3208346e333Sjtc 1.4495602608e+01, /* 0x4167edfd */
3218346e333Sjtc 3.1666231155e+01, /* 0x41fd5471 */
3228346e333Sjtc 1.6252708435e+01, /* 0x4182058c */
3238346e333Sjtc };
3248346e333Sjtc static const float qS2[6] = {
3258346e333Sjtc 3.0365585327e+01, /* 0x41f2ecb8 */
3268346e333Sjtc 2.6934811401e+02, /* 0x4386ac8f */
3278346e333Sjtc 8.4478375244e+02, /* 0x44533229 */
3288346e333Sjtc 8.8293585205e+02, /* 0x445cbbe5 */
3298346e333Sjtc 2.1266638184e+02, /* 0x4354aa98 */
3308346e333Sjtc -5.3109550476e+00, /* 0xc0a9f358 */
3318346e333Sjtc };
3328346e333Sjtc
333aa30599eSwiz static float
qzerof(float x)334aa30599eSwiz qzerof(float x)
3358346e333Sjtc {
3368346e333Sjtc const float *p,*q;
3378346e333Sjtc float s,r,z;
338b0c9d092Sjtc int32_t ix;
339dd7adfbfSlukem
3408346e333Sjtc GET_FLOAT_WORD(ix,x);
3418346e333Sjtc ix &= 0x7fffffff;
3428346e333Sjtc if(ix>=0x41000000) {p = qR8; q= qS8;}
3438346e333Sjtc else if(ix>=0x40f71c58) {p = qR5; q= qS5;}
3448346e333Sjtc else if(ix>=0x4036db68) {p = qR3; q= qS3;}
345*7b2c2e7dSmaya else /*if(ix>=0x40000000)*/{p = qR2; q= qS2;}
3468346e333Sjtc z = one/(x*x);
3478346e333Sjtc r = p[0]+z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5]))));
3488346e333Sjtc s = one+z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*(q[4]+z*q[5])))));
3498346e333Sjtc return (-(float).125 + r/s)/x;
3508346e333Sjtc }
351