k_rem_pio2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* @(#)k_rem_pio2.c 1.3 95/01/18 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. */
  12. /*
  13. * __kernel_rem_pio2(x,y,e0,nx,prec,ipio2)
  14. * double x[],y[]; int e0,nx,prec; int ipio2[];
  15. *
  16. * __kernel_rem_pio2 return the last three digits of N with
  17. * y = x - N*pi/2
  18. * so that |y| < pi/2.
  19. *
  20. * The method is to compute the integer (mod 8) and fraction parts of
  21. * (2/pi)*x without doing the full multiplication. In general we
  22. * skip the part of the product that are known to be a huge integer (
  23. * more accurately, = 0 mod 8 ). Thus the number of operations are
  24. * independent of the exponent of the input.
  25. *
  26. * (2/pi) is represented by an array of 24-bit integers in ipio2[].
  27. *
  28. * Input parameters:
  29. * x[] The input value (must be positive) is broken into nx
  30. * pieces of 24-bit integers in double precision format.
  31. * x[i] will be the i-th 24 bit of x. The scaled exponent
  32. * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
  33. * match x's up to 24 bits.
  34. *
  35. * Example of breaking a double positive z into x[0]+x[1]+x[2]:
  36. * e0 = ilogb(z)-23
  37. * z = scalbn(z,-e0)
  38. * for i = 0,1,2
  39. * x[i] = floor(z)
  40. * z = (z-x[i])*2**24
  41. *
  42. *
  43. * y[] ouput result in an array of double precision numbers.
  44. * The dimension of y[] is:
  45. * 24-bit precision 1
  46. * 53-bit precision 2
  47. * 64-bit precision 2
  48. * 113-bit precision 3
  49. * The actual value is the sum of them. Thus for 113-bit
  50. * precison, one may have to do something like:
  51. *
  52. * long double t,w,r_head, r_tail;
  53. * t = (long double)y[2] + (long double)y[1];
  54. * w = (long double)y[0];
  55. * r_head = t+w;
  56. * r_tail = w - (r_head - t);
  57. *
  58. * e0 The exponent of x[0]
  59. *
  60. * nx dimension of x[]
  61. *
  62. * prec an integer indicating the precision:
  63. * 0 24 bits (single)
  64. * 1 53 bits (double)
  65. * 2 64 bits (extended)
  66. * 3 113 bits (quad)
  67. *
  68. * ipio2[]
  69. * integer array, contains the (24*i)-th to (24*i+23)-th
  70. * bit of 2/pi after binary point. The corresponding
  71. * floating value is
  72. *
  73. * ipio2[i] * 2^(-24(i+1)).
  74. *
  75. * External function:
  76. * double scalbn(), floor();
  77. *
  78. *
  79. * Here is the description of some local variables:
  80. *
  81. * jk jk+1 is the initial number of terms of ipio2[] needed
  82. * in the computation. The recommended value is 2,3,4,
  83. * 6 for single, double, extended,and quad.
  84. *
  85. * jz local integer variable indicating the number of
  86. * terms of ipio2[] used.
  87. *
  88. * jx nx - 1
  89. *
  90. * jv index for pointing to the suitable ipio2[] for the
  91. * computation. In general, we want
  92. * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8
  93. * is an integer. Thus
  94. * e0-3-24*jv >= 0 or (e0-3)/24 >= jv
  95. * Hence jv = max(0,(e0-3)/24).
  96. *
  97. * jp jp+1 is the number of terms in PIo2[] needed, jp = jk.
  98. *
  99. * q[] double array with integral value, representing the
  100. * 24-bits chunk of the product of x and 2/pi.
  101. *
  102. * q0 the corresponding exponent of q[0]. Note that the
  103. * exponent for q[i] would be q0-24*i.
  104. *
  105. * PIo2[] double precision array, obtained by cutting pi/2
  106. * into 24 bits chunks.
  107. *
  108. * f[] ipio2[] in floating point
  109. *
  110. * iq[] integer array by breaking up q[] in 24-bits chunk.
  111. *
  112. * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
  113. *
  114. * ih integer. If >0 it indicates q[] is >= 0.5, hence
  115. * it also indicates the *sign* of the result.
  116. *
  117. */
  118. /*
  119. * Constants:
  120. * The hexadecimal values are the intended ones for the following
  121. * constants. The decimal values may be used, provided that the
  122. * compiler will convert from decimal to binary accurately enough
  123. * to produce the hexadecimal values shown.
  124. */
  125. #include "fdlibm.h"
  126. #ifdef __STDC__
  127. static const int init_jk[] = {2,3,4,6}; /* initial value for jk */
  128. #else
  129. static int init_jk[] = {2,3,4,6};
  130. #endif
  131. #ifdef __STDC__
  132. static const double PIo2[] = {
  133. #else
  134. static double PIo2[] = {
  135. #endif
  136. 1.57079625129699707031e+00, /* 0x3FF921FB, 0x40000000 */
  137. 7.54978941586159635335e-08, /* 0x3E74442D, 0x00000000 */
  138. 5.39030252995776476554e-15, /* 0x3CF84698, 0x80000000 */
  139. 3.28200341580791294123e-22, /* 0x3B78CC51, 0x60000000 */
  140. 1.27065575308067607349e-29, /* 0x39F01B83, 0x80000000 */
  141. 1.22933308981111328932e-36, /* 0x387A2520, 0x40000000 */
  142. 2.73370053816464559624e-44, /* 0x36E38222, 0x80000000 */
  143. 2.16741683877804819444e-51, /* 0x3569F31D, 0x00000000 */
  144. };
  145. #ifdef __STDC__
  146. static const double
  147. #else
  148. static double
  149. #endif
  150. zero = 0.0,
  151. one = 1.0,
  152. two24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */
  153. twon24 = 5.96046447753906250000e-08; /* 0x3E700000, 0x00000000 */
  154. #ifdef __STDC__
  155. int __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec, const int *ipio2)
  156. #else
  157. int __kernel_rem_pio2(x,y,e0,nx,prec,ipio2)
  158. double x[], y[]; int e0,nx,prec; int ipio2[];
  159. #endif
  160. {
  161. int jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih;
  162. double z,fw,f[20],fq[20],q[20];
  163. /* initialize jk*/
  164. jk = init_jk[prec];
  165. jp = jk;
  166. /* determine jx,jv,q0, note that 3>q0 */
  167. jx = nx-1;
  168. jv = (e0-3)/24; if(jv<0) jv=0;
  169. q0 = e0-24*(jv+1);
  170. /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
  171. j = jv-jx; m = jx+jk;
  172. for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (double) ipio2[j];
  173. /* compute q[0],q[1],...q[jk] */
  174. for (i=0;i<=jk;i++) {
  175. for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw;
  176. }
  177. jz = jk;
  178. recompute:
  179. /* distill q[] into iq[] reversingly */
  180. for(i=0,j=jz,z=q[jz];j>0;i++,j--) {
  181. fw = (double)((int)(twon24* z));
  182. iq[i] = (int)(z-two24*fw);
  183. z = q[j-1]+fw;
  184. }
  185. /* compute n */
  186. z = scalbn(z,q0); /* actual value of z */
  187. z -= 8.0*floor(z*0.125); /* trim off integer >= 8 */
  188. n = (int) z;
  189. z -= (double)n;
  190. ih = 0;
  191. if(q0>0) { /* need iq[jz-1] to determine n */
  192. i = (iq[jz-1]>>(24-q0)); n += i;
  193. iq[jz-1] -= i<<(24-q0);
  194. ih = iq[jz-1]>>(23-q0);
  195. }
  196. else if(q0==0) ih = iq[jz-1]>>23;
  197. else if(z>=0.5) ih=2;
  198. if(ih>0) { /* q > 0.5 */
  199. n += 1; carry = 0;
  200. for(i=0;i<jz ;i++) { /* compute 1-q */
  201. j = iq[i];
  202. if(carry==0) {
  203. if(j!=0) {
  204. carry = 1; iq[i] = 0x1000000- j;
  205. }
  206. } else iq[i] = 0xffffff - j;
  207. }
  208. if(q0>0) { /* rare case: chance is 1 in 12 */
  209. switch(q0) {
  210. case 1:
  211. iq[jz-1] &= 0x7fffff; break;
  212. case 2:
  213. iq[jz-1] &= 0x3fffff; break;
  214. }
  215. }
  216. if(ih==2) {
  217. z = one - z;
  218. if(carry!=0) z -= scalbn(one,q0);
  219. }
  220. }
  221. /* check if recomputation is needed */
  222. if(z==zero) {
  223. j = 0;
  224. for (i=jz-1;i>=jk;i--) j |= iq[i];
  225. if(j==0) { /* need recomputation */
  226. for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */
  227. for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */
  228. f[jx+i] = (double) ipio2[jv+i];
  229. for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j];
  230. q[i] = fw;
  231. }
  232. jz += k;
  233. goto recompute;
  234. }
  235. }
  236. /* chop off zero terms */
  237. if(z==0.0) {
  238. jz -= 1; q0 -= 24;
  239. while(iq[jz]==0) { jz--; q0-=24;}
  240. } else { /* break z into 24-bit if necessary */
  241. z = scalbn(z,-q0);
  242. if(z>=two24) {
  243. fw = (double)((int)(twon24*z));
  244. iq[jz] = (int)(z-two24*fw);
  245. jz += 1; q0 += 24;
  246. iq[jz] = (int) fw;
  247. } else iq[jz] = (int) z ;
  248. }
  249. /* convert integer "bit" chunk to floating-point value */
  250. fw = scalbn(one,q0);
  251. for(i=jz;i>=0;i--) {
  252. q[i] = fw*(double)iq[i]; fw*=twon24;
  253. }
  254. /* compute PIo2[0,...,jp]*q[jz,...,0] */
  255. for(i=jz;i>=0;i--) {
  256. for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k];
  257. fq[jz-i] = fw;
  258. }
  259. /* compress fq[] into y[] */
  260. switch(prec) {
  261. case 0:
  262. fw = 0.0;
  263. for (i=jz;i>=0;i--) fw += fq[i];
  264. y[0] = (ih==0)? fw: -fw;
  265. break;
  266. case 1:
  267. case 2:
  268. fw = 0.0;
  269. for (i=jz;i>=0;i--) fw += fq[i];
  270. y[0] = (ih==0)? fw: -fw;
  271. fw = fq[0]-fw;
  272. for (i=1;i<=jz;i++) fw += fq[i];
  273. y[1] = (ih==0)? fw: -fw;
  274. break;
  275. case 3: /* painful */
  276. for (i=jz;i>0;i--) {
  277. fw = fq[i-1]+fq[i];
  278. fq[i] += fq[i-1]-fw;
  279. fq[i-1] = fw;
  280. }
  281. for (i=jz;i>1;i--) {
  282. fw = fq[i-1]+fq[i];
  283. fq[i] += fq[i-1]-fw;
  284. fq[i-1] = fw;
  285. }
  286. for (fw=0.0,i=jz;i>=2;i--) fw += fq[i];
  287. if(ih==0) {
  288. y[0] = fq[0]; y[1] = fq[1]; y[2] = fw;
  289. } else {
  290. y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw;
  291. }
  292. }
  293. return n&7;
  294. }