blob: f63c062f32caf74b724f11e0ac97ece8da4d75b9 [file] [log] [blame]
Rich Felkerb69f6952012-03-13 01:17:53 -04001/* origin: FreeBSD /usr/src/lib/msun/src/e_jnf.c */
2/*
3 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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
Rich Felkerde7db6e2012-03-16 21:16:32 -040016#define _GNU_SOURCE
Rich Felkerb69f6952012-03-13 01:17:53 -040017#include "libm.h"
18
Rich Felkerb69f6952012-03-13 01:17:53 -040019float jnf(int n, float x)
20{
Szabolcs Nagy5652d702013-01-01 22:20:45 +010021 uint32_t ix;
22 int nm1, sign, i;
23 float a, b, temp;
Rich Felkerb69f6952012-03-13 01:17:53 -040024
Szabolcs Nagy5652d702013-01-01 22:20:45 +010025 GET_FLOAT_WORD(ix, x);
26 sign = ix>>31;
27 ix &= 0x7fffffff;
28 if (ix > 0x7f800000) /* nan */
29 return x;
30
31 /* J(-n,x) = J(n,-x), use |n|-1 to avoid overflow in -n */
32 if (n == 0)
33 return j0f(x);
Rich Felkerb69f6952012-03-13 01:17:53 -040034 if (n < 0) {
Szabolcs Nagy5652d702013-01-01 22:20:45 +010035 nm1 = -(n+1);
Rich Felkerb69f6952012-03-13 01:17:53 -040036 x = -x;
Szabolcs Nagy5652d702013-01-01 22:20:45 +010037 sign ^= 1;
38 } else
39 nm1 = n-1;
40 if (nm1 == 0)
41 return j1f(x);
Rich Felkerb69f6952012-03-13 01:17:53 -040042
Szabolcs Nagy5652d702013-01-01 22:20:45 +010043 sign &= n; /* even n: 0, odd n: signbit(x) */
Rich Felkerb69f6952012-03-13 01:17:53 -040044 x = fabsf(x);
Szabolcs Nagy5652d702013-01-01 22:20:45 +010045 if (ix == 0 || ix == 0x7f800000) /* if x is 0 or inf */
nsz0cbb6542012-03-19 23:41:19 +010046 b = 0.0f;
Szabolcs Nagy5652d702013-01-01 22:20:45 +010047 else if (nm1 < x) {
Rich Felkerb69f6952012-03-13 01:17:53 -040048 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
49 a = j0f(x);
50 b = j1f(x);
Szabolcs Nagy5652d702013-01-01 22:20:45 +010051 for (i=0; i<nm1; ){
52 i++;
Rich Felkerb69f6952012-03-13 01:17:53 -040053 temp = b;
Szabolcs Nagy5652d702013-01-01 22:20:45 +010054 b = b*(2.0f*i/x) - a;
Rich Felkerb69f6952012-03-13 01:17:53 -040055 a = temp;
56 }
57 } else {
Szabolcs Nagy5652d702013-01-01 22:20:45 +010058 if (ix < 0x35800000) { /* x < 2**-20 */
Rich Felkerb69f6952012-03-13 01:17:53 -040059 /* x is tiny, return the first Taylor expansion of J(n,x)
60 * J(n,x) = 1/n!*(x/2)^n - ...
61 */
Szabolcs Nagy5652d702013-01-01 22:20:45 +010062 if (nm1 > 8) /* underflow */
63 nm1 = 8;
64 temp = 0.5f * x;
65 b = temp;
66 a = 1.0f;
67 for (i=2; i<=nm1+1; i++) {
68 a *= (float)i; /* a = n! */
69 b *= temp; /* b = (x/2)^n */
Rich Felkerb69f6952012-03-13 01:17:53 -040070 }
Szabolcs Nagy5652d702013-01-01 22:20:45 +010071 b = b/a;
Rich Felkerb69f6952012-03-13 01:17:53 -040072 } else {
73 /* use backward recurrence */
74 /* x x^2 x^2
75 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
76 * 2n - 2(n+1) - 2(n+2)
77 *
78 * 1 1 1
79 * (for large x) = ---- ------ ------ .....
80 * 2n 2(n+1) 2(n+2)
81 * -- - ------ - ------ -
82 * x x x
83 *
84 * Let w = 2n/x and h=2/x, then the above quotient
85 * is equal to the continued fraction:
86 * 1
87 * = -----------------------
88 * 1
89 * w - -----------------
90 * 1
91 * w+h - ---------
92 * w+2h - ...
93 *
94 * To determine how many terms needed, let
95 * Q(0) = w, Q(1) = w(w+h) - 1,
96 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
97 * When Q(k) > 1e4 good for single
98 * When Q(k) > 1e9 good for double
99 * When Q(k) > 1e17 good for quadruple
100 */
101 /* determine k */
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100102 float t,q0,q1,w,h,z,tmp,nf;
103 int k;
Rich Felkerb69f6952012-03-13 01:17:53 -0400104
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100105 nf = nm1+1.0f;
106 w = 2*nf/x;
107 h = 2/x;
Rich Felkerb69f6952012-03-13 01:17:53 -0400108 z = w+h;
109 q0 = w;
nsz8d0a6f72012-03-13 20:24:23 +0100110 q1 = w*z - 1.0f;
Rich Felkerb69f6952012-03-13 01:17:53 -0400111 k = 1;
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100112 while (q1 < 1.0e4f) {
Rich Felkerb69f6952012-03-13 01:17:53 -0400113 k += 1;
114 z += h;
115 tmp = z*q1 - q0;
116 q0 = q1;
117 q1 = tmp;
118 }
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100119 for (t=0.0f, i=k; i>=0; i--)
120 t = 1.0f/(2*(i+nf)/x-t);
Rich Felkerb69f6952012-03-13 01:17:53 -0400121 a = t;
nsz0cbb6542012-03-19 23:41:19 +0100122 b = 1.0f;
Rich Felkerb69f6952012-03-13 01:17:53 -0400123 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
124 * Hence, if n*(log(2n/x)) > ...
125 * single 8.8722839355e+01
126 * double 7.09782712893383973096e+02
127 * long double 1.1356523406294143949491931077970765006170e+04
128 * then recurrent value may overflow and the result is
129 * likely underflow to zero
130 */
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100131 tmp = nf*logf(fabsf(w));
nsz8d0a6f72012-03-13 20:24:23 +0100132 if (tmp < 88.721679688f) {
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100133 for (i=nm1; i>0; i--) {
Rich Felkerb69f6952012-03-13 01:17:53 -0400134 temp = b;
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100135 b = 2.0f*i*b/x - a;
Rich Felkerb69f6952012-03-13 01:17:53 -0400136 a = temp;
Rich Felkerb69f6952012-03-13 01:17:53 -0400137 }
138 } else {
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100139 for (i=nm1; i>0; i--){
Rich Felkerb69f6952012-03-13 01:17:53 -0400140 temp = b;
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100141 b = 2.0f*i*b/x - a;
Rich Felkerb69f6952012-03-13 01:17:53 -0400142 a = temp;
Rich Felkerb69f6952012-03-13 01:17:53 -0400143 /* scale b to avoid spurious overflow */
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100144 if (b > 0x1p60f) {
Rich Felkerb69f6952012-03-13 01:17:53 -0400145 a /= b;
146 t /= b;
nsz0cbb6542012-03-19 23:41:19 +0100147 b = 1.0f;
Rich Felkerb69f6952012-03-13 01:17:53 -0400148 }
149 }
150 }
151 z = j0f(x);
152 w = j1f(x);
153 if (fabsf(z) >= fabsf(w))
154 b = t*z/b;
155 else
156 b = t*w/a;
157 }
158 }
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100159 return sign ? -b : b;
Rich Felkerb69f6952012-03-13 01:17:53 -0400160}
161
162float ynf(int n, float x)
163{
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100164 uint32_t ix, ib;
165 int nm1, sign, i;
Rich Felkerb69f6952012-03-13 01:17:53 -0400166 float a, b, temp;
167
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100168 GET_FLOAT_WORD(ix, x);
169 sign = ix>>31;
170 ix &= 0x7fffffff;
171 if (ix > 0x7f800000) /* nan */
172 return x;
173 if (sign && ix != 0) /* x < 0 */
174 return 0/0.0f;
Rich Felkerb69f6952012-03-13 01:17:53 -0400175 if (ix == 0x7f800000)
nsz0cbb6542012-03-19 23:41:19 +0100176 return 0.0f;
Rich Felkerb69f6952012-03-13 01:17:53 -0400177
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100178 if (n == 0)
179 return y0f(x);
180 if (n < 0) {
181 nm1 = -(n+1);
182 sign = n&1;
183 } else {
184 nm1 = n-1;
185 sign = 0;
186 }
187 if (nm1 == 0)
188 return sign ? -y1f(x) : y1f(x);
189
Rich Felkerb69f6952012-03-13 01:17:53 -0400190 a = y0f(x);
191 b = y1f(x);
192 /* quit if b is -inf */
193 GET_FLOAT_WORD(ib,b);
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100194 for (i = 0; i < nm1 && ib != 0xff800000; ) {
195 i++;
Rich Felkerb69f6952012-03-13 01:17:53 -0400196 temp = b;
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100197 b = (2.0f*i/x)*b - a;
Rich Felkerb69f6952012-03-13 01:17:53 -0400198 GET_FLOAT_WORD(ib, b);
199 a = temp;
200 }
Szabolcs Nagy5652d702013-01-01 22:20:45 +0100201 return sign ? -b : b;
Rich Felkerb69f6952012-03-13 01:17:53 -0400202}