home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / hobby / palmoon / palmoon.EXE / k_rem_pio2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-16  |  9.5 KB  |  337 lines

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