rtld: increase TLS storage space (bug 2566)
[dragonfly.git] / lib / libm / src / e_jnf.c
1 /* e_jnf.c -- float version of e_jn.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  *
15  * $FreeBSD: head/lib/msun/src/e_jnf.c 215237 2010-11-13 10:54:10Z uqs $
16  */
17
18 #include "math.h"
19 #include "math_private.h"
20
21 static const float
22 two   =  2.0000000000e+00, /* 0x40000000 */
23 one   =  1.0000000000e+00; /* 0x3F800000 */
24
25 static const float zero  =  0.0000000000e+00;
26
27 float
28 __ieee754_jnf(int n, float x)
29 {
30         int32_t i,hx,ix, sgn;
31         float a, b, temp, di;
32         float z, w;
33
34     /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
35      * Thus, J(-n,x) = J(n,-x)
36      */
37         GET_FLOAT_WORD(hx,x);
38         ix = 0x7fffffff&hx;
39     /* if J(n,NaN) is NaN */
40         if(ix>0x7f800000) return x+x;
41         if(n<0){
42                 n = -n;
43                 x = -x;
44                 hx ^= 0x80000000;
45         }
46         if(n==0) return(__ieee754_j0f(x));
47         if(n==1) return(__ieee754_j1f(x));
48         sgn = (n&1)&(hx>>31);   /* even n -- 0, odd n -- sign(x) */
49         x = fabsf(x);
50         if(ix==0||ix>=0x7f800000)       /* if x is 0 or inf */
51             b = zero;
52         else if((float)n<=x) {
53                 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
54             a = __ieee754_j0f(x);
55             b = __ieee754_j1f(x);
56             for(i=1;i<n;i++){
57                 temp = b;
58                 b = b*((float)(i+i)/x) - a; /* avoid underflow */
59                 a = temp;
60             }
61         } else {
62             if(ix<0x30800000) { /* x < 2**-29 */
63     /* x is tiny, return the first Taylor expansion of J(n,x)
64      * J(n,x) = 1/n!*(x/2)^n  - ...
65      */
66                 if(n>33)        /* underflow */
67                     b = zero;
68                 else {
69                     temp = x*(float)0.5; b = temp;
70                     for (a=one,i=2;i<=n;i++) {
71                         a *= (float)i;          /* a = n! */
72                         b *= temp;              /* b = (x/2)^n */
73                     }
74                     b = b/a;
75                 }
76             } else {
77                 /* use backward recurrence */
78                 /*                      x      x^2      x^2
79                  *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
80                  *                      2n  - 2(n+1) - 2(n+2)
81                  *
82                  *                      1      1        1
83                  *  (for large x)   =  ----  ------   ------   .....
84                  *                      2n   2(n+1)   2(n+2)
85                  *                      -- - ------ - ------ -
86                  *                       x     x         x
87                  *
88                  * Let w = 2n/x and h=2/x, then the above quotient
89                  * is equal to the continued fraction:
90                  *                  1
91                  *      = -----------------------
92                  *                     1
93                  *         w - -----------------
94                  *                        1
95                  *              w+h - ---------
96                  *                     w+2h - ...
97                  *
98                  * To determine how many terms needed, let
99                  * Q(0) = w, Q(1) = w(w+h) - 1,
100                  * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
101                  * When Q(k) > 1e4      good for single
102                  * When Q(k) > 1e9      good for double
103                  * When Q(k) > 1e17     good for quadruple
104                  */
105             /* determine k */
106                 float t,v;
107                 float q0,q1,h,tmp; int32_t k,m;
108                 w  = (n+n)/(float)x; h = (float)2.0/(float)x;
109                 q0 = w;  z = w+h; q1 = w*z - (float)1.0; k=1;
110                 while(q1<(float)1.0e9) {
111                         k += 1; z += h;
112                         tmp = z*q1 - q0;
113                         q0 = q1;
114                         q1 = tmp;
115                 }
116                 m = n+n;
117                 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
118                 a = t;
119                 b = one;
120                 /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
121                  *  Hence, if n*(log(2n/x)) > ...
122                  *  single 8.8722839355e+01
123                  *  double 7.09782712893383973096e+02
124                  *  long double 1.1356523406294143949491931077970765006170e+04
125                  *  then recurrent value may overflow and the result is
126                  *  likely underflow to zero
127                  */
128                 tmp = n;
129                 v = two/x;
130                 tmp = tmp*__ieee754_logf(fabsf(v*tmp));
131                 if(tmp<(float)8.8721679688e+01) {
132                     for(i=n-1,di=(float)(i+i);i>0;i--){
133                         temp = b;
134                         b *= di;
135                         b  = b/x - a;
136                         a = temp;
137                         di -= two;
138                     }
139                 } else {
140                     for(i=n-1,di=(float)(i+i);i>0;i--){
141                         temp = b;
142                         b *= di;
143                         b  = b/x - a;
144                         a = temp;
145                         di -= two;
146                     /* scale b to avoid spurious overflow */
147                         if(b>(float)1e10) {
148                             a /= b;
149                             t /= b;
150                             b  = one;
151                         }
152                     }
153                 }
154                 z = __ieee754_j0f(x);
155                 w = __ieee754_j1f(x);
156                 if (fabsf(z) >= fabsf(w))
157                     b = (t*z/b);
158                 else
159                     b = (t*w/a);
160             }
161         }
162         if(sgn==1) return -b; else return b;
163 }
164
165 float
166 __ieee754_ynf(int n, float x)
167 {
168         int32_t i,hx,ix,ib;
169         int32_t sign;
170         float a, b, temp;
171
172         GET_FLOAT_WORD(hx,x);
173         ix = 0x7fffffff&hx;
174     /* if Y(n,NaN) is NaN */
175         if(ix>0x7f800000) return x+x;
176         if(ix==0) return -one/zero;
177         if(hx<0) return zero/zero;
178         sign = 1;
179         if(n<0){
180                 n = -n;
181                 sign = 1 - ((n&1)<<1);
182         }
183         if(n==0) return(__ieee754_y0f(x));
184         if(n==1) return(sign*__ieee754_y1f(x));
185         if(ix==0x7f800000) return zero;
186
187         a = __ieee754_y0f(x);
188         b = __ieee754_y1f(x);
189         /* quit if b is -inf */
190         GET_FLOAT_WORD(ib,b);
191         for(i=1;i<n&&ib!=0xff800000;i++){
192             temp = b;
193             b = ((float)(i+i)/x)*b - a;
194             GET_FLOAT_WORD(ib,b);
195             a = temp;
196         }
197         if(sign>0) return b; else return -b;
198 }