besself.c revision 25c28e83beb90e7c80452a7c818c5e6f73a07dc8
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "libm.h"
#include <float.h>
extern int __swapRP(int);
#endif
static const float
zerof = 0.0f,
onef = 1.0f;
static const double C[] = {
0.0,
-0.125,
0.25,
0.375,
0.5,
1.0,
2.0,
8.0,
0.5641895835477562869480794515607725858441, /* 1/sqrt(pi) */
0.636619772367581343075535053490057448, /* 2/pi */
1.0e9,
};
#define zero C[0]
#define neighth C[1]
#define quarter C[2]
#define three8 C[3]
#define half C[4]
#define one C[5]
#define two C[6]
#define eight C[7]
#define isqrtpi C[8]
#define tpi C[9]
#define big C[10]
static const double Cj0y0[] = {
0.4861344183386052721391238447e5, /* pr */
0.1377662549407112278133438945e6,
0.1222466364088289731869114004e6,
0.4107070084315176135583353374e5,
0.5026073801860637125889039915e4,
0.1783193659125479654541542419e3,
0.88010344055383421691677564e0,
0.4861344183386052721414037058e5, /* ps */
0.1378196632630384670477582699e6,
0.1223967185341006542748936787e6,
0.4120150243795353639995862617e5,
0.5068271181053546392490184353e4,
0.1829817905472769960535671664e3,
1.0,
-0.1731210995701068539185611951e3, /* qr */
-0.5522559165936166961235240613e3,
-0.5604935606637346590614529613e3,
-0.2200430300226009379477365011e3,
-0.323869355375648849771296746e2,
-0.14294979207907956223499258e1,
-0.834690374102384988158918e-2,
0.1107975037248683865326709645e5, /* qs */
0.3544581680627082674651471873e5,
0.3619118937918394132179019059e5,
0.1439895563565398007471485822e5,
0.2190277023344363955930226234e4,
0.106695157020407986137501682e3,
1.0,
};
static const double Cj0[] = {
-2.500000000000003622131880894830476755537e-0001, /* r0 */
1.095597547334830263234433855932375353303e-0002,
-1.819734750463320921799187258987098087697e-0004,
9.977001946806131657544212501069893930846e-0007,
1.0, /* s0 */
1.867609810662950169966782360588199673741e-0002,
1.590389206181565490878430827706972074208e-0004,
6.520867386742583632375520147714499522721e-0007,
9.999999999999999942156495584397047660949e-0001, /* r1 */
-2.389887722731319130476839836908143731281e-0001,
1.293359476138939027791270393439493640570e-0002,
-2.770985642343140122168852400228563364082e-0004,
2.905241575772067678086738389169625218912e-0006,
-1.636846356264052597969042009265043251279e-0008,
5.072306160724884775085431059052611737827e-0011,
-8.187060730684066824228914775146536139112e-0014,
5.422219326959949863954297860723723423842e-0017,
1.0, /* s1 */
1.101122772686807702762104741932076228349e-0002,
6.140169310641649223411427764669143978228e-0005,
2.292035877515152097976946119293215705250e-0007,
6.356910426504644334558832036362219583789e-0010,
1.366626326900219555045096999553948891401e-0012,
2.280399586866739522891837985560481180088e-0015,
2.801559820648939665270492520004836611187e-0018,
2.073101088320349159764410261466350732968e-0021,
};
static const double Cy0[] = {
-7.380429510868722526754723020704317641941e-0002, /* u0 */
1.772607102684869924301459663049874294814e-0001,
-1.524370666542713828604078090970799356306e-0002,
4.650819100693891757143771557629924591915e-0004,
-7.125768872339528975036316108718239946022e-0006,
6.411017001656104598327565004771515257146e-0008,
-3.694275157433032553021246812379258781665e-0010,
1.434364544206266624252820889648445263842e-0012,
-3.852064731859936455895036286874139896861e-0015,
7.182052899726138381739945881914874579696e-0018,
-9.060556574619677567323741194079797987200e-0021,
7.124435467408860515265552217131230511455e-0024,
-2.709726774636397615328813121715432044771e-0027,
1.0, /* v0 */
4.678678931512549002587702477349214886475e-0003,
9.486828955529948534822800829497565178985e-0006,
1.001495929158861646659010844136682454906e-0008,
4.725338116256021660204443235685358593611e-0012,
};
static const double Cj1y1[] = {
-0.4435757816794127857114720794e7, /* pr0 */
-0.9942246505077641195658377899e7,
-0.6603373248364939109255245434e7,
-0.1523529351181137383255105722e7,
-0.1098240554345934672737413139e6,
-0.1611616644324610116477412898e4,
-0.4435757816794127856828016962e7, /* ps0 */
-0.9934124389934585658967556309e7,
-0.6585339479723087072826915069e7,
-0.1511809506634160881644546358e7,
-0.1072638599110382011903063867e6,
-0.1455009440190496182453565068e4,
0.3322091340985722351859704442e5, /* qr0 */
0.8514516067533570196555001171e5,
0.6617883658127083517939992166e5,
0.1849426287322386679652009819e5,
0.1706375429020768002061283546e4,
0.3526513384663603218592175580e2,
0.7087128194102874357377502472e6, /* qs0 */
0.1819458042243997298924553839e7,
0.1419460669603720892855755253e7,
0.4002944358226697511708610813e6,
0.3789022974577220264142952256e5,
0.8638367769604990967475517183e3,
};
static const double Cj1[] = {
-6.250000000000002203053200981413218949548e-0002, /* a0 */
1.600998455640072901321605101981501263762e-0003,
-1.963888815948313758552511884390162864930e-0005,
8.263917341093549759781339713418201620998e-0008,
1.0e0, /* b0 */
1.605069137643004242395356851797873766927e-0002,
1.149454623251299996428500249509098499383e-0004,
3.849701673735260970379681807910852327825e-0007,
4.999999999999999995517408894340485471724e-0001,
-6.003825028120475684835384519945468075423e-0002,
2.301719899263321828388344461995355419832e-0003,
-4.208494869238892934859525221654040304068e-0005,
4.377745135188837783031540029700282443388e-0007,
-2.854106755678624335145364226735677754179e-0009,
1.234002865443952024332943901323798413689e-0011,
-3.645498437039791058951273508838177134310e-0014,
7.404320596071797459925377103787837414422e-0017,
-1.009457448277522275262808398517024439084e-0019,
8.520158355824819796968771418801019930585e-0023,
-3.458159926081163274483854614601091361424e-0026,
1.0e0, /* b1 */
4.923499437590484879081138588998986303306e-0003,
1.054389489212184156499666953501976688452e-0005,
1.180768373106166527048240364872043816050e-0008,
5.942665743476099355323245707680648588540e-0012,
};
static const double Cy1[] = {
-1.960570906462389461018983259589655961560e-0001, /* c0 */
4.931824118350661953459180060007970291139e-0002,
-1.626975871565393656845930125424683008677e-0003,
1.359657517926394132692884168082224258360e-0005,
1.0e0, /* d0 */
2.565807214838390835108224713630901653793e-0002,
3.374175208978404268650522752520906231508e-0004,
2.840368571306070719539936935220728843177e-0006,
1.396387402048998277638900944415752207592e-0008,
-1.960570906462389473336339614647555351626e-0001, /* c1 */
5.336268030335074494231369159933012844735e-0002,
-2.684137504382748094149184541866332033280e-0003,
5.737671618979185736981543498580051903060e-0005,
-6.642696350686335339171171785557663224892e-0007,
4.692417922568160354012347591960362101664e-0009,
-2.161728635907789319335231338621412258355e-0011,
6.727353419738316107197644431844194668702e-0014,
-1.427502986803861372125234355906790573422e-0016,
2.020392498726806769468143219616642940371e-0019,
-1.761371948595104156753045457888272716340e-0022,
7.352828391941157905175042420249225115816e-0026,
1.0e0, /* d1 */
5.029187436727947764916247076102283399442e-0003,
1.102693095808242775074856548927801750627e-0005,
1.268035774543174837829534603830227216291e-0008,
6.579416271766610825192542295821308730206e-0012,
};
/* core of j0f computation; assumes fx is finite */
static double
{
int ix, i;
if (ix > 0x41000000) {
/* x > 8; see comments in j0.c */
s = sin(x);
c = cos(x);
ss = s - c;
} else {
cc = s + c;
}
if (ix > 0x501502f9) {
/* x > 1.0e10 */
} else {
t = eight / x;
z = t * t;
}
}
if (ix <= 0x3727c5ac) {
/* x <= 1.0e-5 */
return (one - x);
}
z = x * x;
if (ix <= 0x3fa3d70a) {
/* x <= 1.28 */
return (one + z * (r / s));
}
r = r1[8];
s = s1[8];
for (i = 7; i >= 0; i--) {
r = r * z + r1[i];
s = s * z + s1[i];
}
return (r / s);
}
float
{
float f;
int ix;
int rp;
#endif
if (ix > 0x7f800000)
return (zerof);
}
#endif
if (rp != fp_extended)
#endif
return (f);
}
/* core of y0f computation; assumes fx is finite and positive */
static double
{
int ix, i;
x = (double)fx;
if (ix > 0x41000000) {
/* x > 8; see comments in j0.c */
s = sin(x);
c = cos(x);
ss = s - c;
} else {
cc = s + c;
}
if (ix > 0x501502f9) {
/* x > 1.0e10 */
} else {
t = eight / x;
z = t * t;
}
}
z = x * x;
u = u0[12];
for (i = 11; i >= 0; i--)
u = u * z + u0[i];
}
float
{
float f;
int ix;
int rp;
#endif
if (ix <= 0) { /* zero or negative */
if ((ix << 1) == 0)
}
return (zerof);
#endif
if (rp != fp_extended)
#endif
return (f);
}
/* core of j1f computation; assumes fx is finite */
static double
{
ix &= ~0x80000000;
if (ix > 0x41000000) {
/* x > 8; see comments in j1.c */
s = sin(x);
c = cos(x);
cc = s - c;
} else {
ss = -s - c;
}
if (ix > 0x501502f9) {
/* x > 1.0e10 */
} else {
t = eight / x;
z = t * t;
}
return ((sgn)? -t : t);
}
if (ix <= 0x3727c5ac) {
/* x <= 1.0e-5 */
t = half * x;
else
return ((sgn)? -t : t);
}
z = x * x;
if (ix < 0x3fa3d70a) {
/* x < 1.28 */
t = x * half + x * (z * (r / s));
} else {
r = a1[11];
for (i = 10; i >= 0; i--)
r = r * z + a1[i];
t = x * (r / s);
}
return ((sgn)? -t : t);
}
float
{
float f;
int ix;
int rp;
#endif
#endif
if (rp != fp_extended)
#endif
return (f);
}
/* core of y1f computation; assumes fx is finite and positive */
static double
{
int i, ix;
x = (double)fx;
if (ix > 0x41000000) {
/* x > 8; see comments in j1.c */
s = sin(x);
c = cos(x);
cc = s - c;
} else {
ss = -s - c;
}
if (ix > 0x501502f9) {
/* x > 1.0e10 */
} else {
t = eight / x;
z = t * t;
}
}
return (-tpi / x);
z = x * x;
if (ix < 0x3fa3d70a) {
/* x < 1.28 */
} else {
u = c1[11];
for (i = 10; i >= 0; i--)
u = u * z + c1[i];
}
}
float
{
float f;
int ix;
int rp;
#endif
if (ix <= 0) { /* zero or negative */
if ((ix << 1) == 0)
}
return (zerof);
#endif
if (rp != fp_extended)
#endif
return (f);
}
float
{
float f;
int rp;
#endif
if (n < 0) {
n = -n;
}
if (n == 0)
if (n == 1)
ix &= ~0x80000000;
if (ix > 0x7f800000)
}
if ((ix << 1) == 0)
#endif
x = (double)fx;
if ((double)n <= x) {
/* safe to use J(n+1,x) = 2n/x * J(n,x) - J(n-1,x) */
for (i = 1; i < n; i++) {
temp = b;
b = b * ((double)(i + i) / x) - a;
a = temp;
}
f = (float)b;
if (rp != fp_extended)
#endif
return ((sgn)? -f : f);
}
if (ix < 0x3089705f) {
/* x < 1.0e-9; use J(n,x) = 1/n! * (x / 2)^n */
if (n > 6)
n = 6; /* result underflows to zero for n >= 6 */
b = t = half * x;
a = one;
for (i = 2; i <= n; i++) {
b *= t;
a *= (double)i;
}
b /= a;
} else {
/*
* Use the backward recurrence:
*
* x x^2 x^2
* J(n,x)/J(n-1,x) = ---- - ------ - ------ .....
* 2n 2(n+1) 2(n+2)
*
* Let w = 2n/x and h = 2/x. Then the above quotient
* is equal to the continued fraction:
* 1
* = -----------------------
* 1
* w - -----------------
* 1
* w+h - ---------
* w+2h - ...
*
* To determine how many terms are needed, run the
* recurrence
*
* Q(0) = w,
* Q(1) = w(w+h) - 1,
* Q(k) = (w+k*h)*Q(k-1) - Q(k-2).
*
* Then when Q(k) > 1e4, k is large enough for single
* precision.
*/
/* XXX NOT DONE - rework this */
w = (n + n) / x;
h = two / x;
q0 = w;
z = w + h;
k = 1;
k++;
z += h;
}
m = n + n;
t = zero;
for (i = (n + k) << 1; i >= m; i -= 2)
t = one / ((double)i / x - t);
a = t;
b = one;
/*
* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
* hence, if n*(log(2n/x)) > ...
* single 8.8722839355e+01
* double 7.09782712893383973096e+02
* then recurrent value may overflow and the result is
* likely underflow to zero
*/
temp = (double)n;
if (temp < 7.09782712893383973096e+02) {
for (i = n - 1; i > 0; i--) {
temp = b;
b = b * ((double)(i + i) / x) - a;
a = temp;
}
} else {
for (i = n - 1; i > 0; i--) {
temp = b;
b = b * ((double)(i + i) / x) - a;
a = temp;
if (b > 1.0e100) {
a /= b;
t /= b;
b = one;
}
}
}
}
f = (float)b;
if (rp != fp_extended)
#endif
return ((sgn)? -f : f);
}
float
{
double a, b, temp, x;
float f;
int rp;
#endif
sign = 0;
if (n < 0) {
n = -n;
if (n & 1)
sign = 1;
}
if (n == 0)
if (n == 1)
if (ix <= 0) { /* zero or negative */
if ((ix << 1) == 0)
}
return (zerof);
#endif
x = (double)fx;
for (i = 1; i < n; i++) {
temp = b;
b *= (double)(i + i) / x;
if (b <= -DBL_MAX)
break;
b -= a;
a = temp;
}
f = (float)b;
if (rp != fp_extended)
#endif
return ((sign)? -f : f);
}