1
0

jnf.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_jnf.c */
  2. /*
  3. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  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. #define _GNU_SOURCE
  16. #include "libm.h"
  17. float jnf(int n, float x)
  18. {
  19. int32_t i,hx,ix, sgn;
  20. float a, b, temp, di;
  21. float z, w;
  22. /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
  23. * Thus, J(-n,x) = J(n,-x)
  24. */
  25. GET_FLOAT_WORD(hx, x);
  26. ix = 0x7fffffff & hx;
  27. /* if J(n,NaN) is NaN */
  28. if (ix > 0x7f800000)
  29. return x+x;
  30. if (n < 0) {
  31. n = -n;
  32. x = -x;
  33. hx ^= 0x80000000;
  34. }
  35. if (n == 0) return j0f(x);
  36. if (n == 1) return j1f(x);
  37. sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
  38. x = fabsf(x);
  39. if (ix == 0 || ix >= 0x7f800000) /* if x is 0 or inf */
  40. b = 0.0f;
  41. else if((float)n <= x) {
  42. /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
  43. a = j0f(x);
  44. b = j1f(x);
  45. for (i=1; i<n; i++){
  46. temp = b;
  47. b = b*((float)(i+i)/x) - a; /* avoid underflow */
  48. a = temp;
  49. }
  50. } else {
  51. if (ix < 0x30800000) { /* x < 2**-29 */
  52. /* x is tiny, return the first Taylor expansion of J(n,x)
  53. * J(n,x) = 1/n!*(x/2)^n - ...
  54. */
  55. if (n > 33) /* underflow */
  56. b = 0.0f;
  57. else {
  58. temp = 0.5f * x;
  59. b = temp;
  60. for (a=1.0f,i=2; i<=n; i++) {
  61. a *= (float)i; /* a = n! */
  62. b *= temp; /* b = (x/2)^n */
  63. }
  64. b = b/a;
  65. }
  66. } else {
  67. /* use backward recurrence */
  68. /* x x^2 x^2
  69. * J(n,x)/J(n-1,x) = ---- ------ ------ .....
  70. * 2n - 2(n+1) - 2(n+2)
  71. *
  72. * 1 1 1
  73. * (for large x) = ---- ------ ------ .....
  74. * 2n 2(n+1) 2(n+2)
  75. * -- - ------ - ------ -
  76. * x x x
  77. *
  78. * Let w = 2n/x and h=2/x, then the above quotient
  79. * is equal to the continued fraction:
  80. * 1
  81. * = -----------------------
  82. * 1
  83. * w - -----------------
  84. * 1
  85. * w+h - ---------
  86. * w+2h - ...
  87. *
  88. * To determine how many terms needed, let
  89. * Q(0) = w, Q(1) = w(w+h) - 1,
  90. * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
  91. * When Q(k) > 1e4 good for single
  92. * When Q(k) > 1e9 good for double
  93. * When Q(k) > 1e17 good for quadruple
  94. */
  95. /* determine k */
  96. float t,v;
  97. float q0,q1,h,tmp;
  98. int32_t k,m;
  99. w = (n+n)/x;
  100. h = 2.0f/x;
  101. z = w+h;
  102. q0 = w;
  103. q1 = w*z - 1.0f;
  104. k = 1;
  105. while (q1 < 1.0e9f) {
  106. k += 1;
  107. z += h;
  108. tmp = z*q1 - q0;
  109. q0 = q1;
  110. q1 = tmp;
  111. }
  112. m = n+n;
  113. for (t=0.0f, i = 2*(n+k); i>=m; i -= 2)
  114. t = 1.0f/(i/x-t);
  115. a = t;
  116. b = 1.0f;
  117. /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
  118. * Hence, if n*(log(2n/x)) > ...
  119. * single 8.8722839355e+01
  120. * double 7.09782712893383973096e+02
  121. * long double 1.1356523406294143949491931077970765006170e+04
  122. * then recurrent value may overflow and the result is
  123. * likely underflow to zero
  124. */
  125. tmp = n;
  126. v = 2.0f/x;
  127. tmp = tmp*logf(fabsf(v*tmp));
  128. if (tmp < 88.721679688f) {
  129. for (i=n-1,di=(float)(i+i); i>0; i--) {
  130. temp = b;
  131. b *= di;
  132. b = b/x - a;
  133. a = temp;
  134. di -= 2.0f;
  135. }
  136. } else {
  137. for (i=n-1,di=(float)(i+i); i>0; i--){
  138. temp = b;
  139. b *= di;
  140. b = b/x - a;
  141. a = temp;
  142. di -= 2.0f;
  143. /* scale b to avoid spurious overflow */
  144. if (b > 1e10f) {
  145. a /= b;
  146. t /= b;
  147. b = 1.0f;
  148. }
  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. }
  159. if (sgn == 1) return -b;
  160. return b;
  161. }
  162. float ynf(int n, float x)
  163. {
  164. int32_t i,hx,ix,ib;
  165. int32_t sign;
  166. float a, b, temp;
  167. GET_FLOAT_WORD(hx, x);
  168. ix = 0x7fffffff & hx;
  169. /* if Y(n,NaN) is NaN */
  170. if (ix > 0x7f800000)
  171. return x+x;
  172. if (ix == 0)
  173. return -1.0f/0.0f;
  174. if (hx < 0)
  175. return 0.0f/0.0f;
  176. sign = 1;
  177. if (n < 0) {
  178. n = -n;
  179. sign = 1 - ((n&1)<<1);
  180. }
  181. if (n == 0)
  182. return y0f(x);
  183. if (n == 1)
  184. return sign*y1f(x);
  185. if (ix == 0x7f800000)
  186. return 0.0f;
  187. a = y0f(x);
  188. b = 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)
  198. return b;
  199. return -b;
  200. }