e_jnf.c 4.8 KB

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