home *** CD-ROM | disk | FTP | other *** search
/ Game Zone - 1,000+ Games / GAMEZONE.BIN / Programs / PALM / Oh-One / src / e_fmod.c < prev    next >
C/C++ Source or Header  |  1997-08-16  |  4KB  |  147 lines

  1. // 15 August 1997, Rick Huebner:  Small changes made to adapt for MathLib
  2.  
  3. /* @(#)e_fmod.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: e_fmod.c,v 1.8 1995/05/10 20:45:07 jtc Exp $";
  17. #endif
  18.  
  19. /* 
  20.  * __ieee754_fmod(x,y)
  21.  * Return x mod y in exact arithmetic
  22.  * Method: shift and subtract
  23.  */
  24.  
  25. #include "math.h"
  26. #include "math_private.h"
  27.  
  28. #ifdef __STDC__
  29. static const double one = 1.0;    // Zero[] = {0.0, -0.0,};
  30. #else
  31. static double one = 1.0;    // Zero[] = {0.0, -0.0,};
  32. #endif
  33.  
  34. #ifdef __STDC__
  35.     double __ieee754_fmod(double x, double y)
  36. #else
  37.     double __ieee754_fmod(x,y)
  38.     double x,y ;
  39. #endif
  40. {
  41.     double Zero[2];
  42.     int32_t n,hx,hy,hz,ix,iy,sx,i;
  43.     u_int32_t lx,ly,lz;
  44.  
  45.     Zero[0] =  0.0;
  46.     Zero[1] = -0.0;
  47.     
  48.     EXTRACT_WORDS(hx,lx,x);
  49.     EXTRACT_WORDS(hy,ly,y);
  50.     sx = hx&0x80000000;        /* sign of x */
  51.     hx ^=sx;        /* |x| */
  52.     hy &= 0x7fffffff;    /* |y| */
  53.  
  54.     /* purge off exception values */
  55.     if((hy|ly)==0||(hx>=0x7ff00000)||    /* y=0,or x not finite */
  56.       ((hy|((ly|-ly)>>31))>0x7ff00000))    /* or y is NaN */
  57.         return (x*y)/(x*y);
  58.     if(hx<=hy) {
  59.         if((hx<hy)||(lx<ly)) return x;    /* |x|<|y| return x */
  60.         if(lx==ly) 
  61.         return Zero[(u_int32_t)sx>>31];    /* |x|=|y| return x*0*/
  62.     }
  63.  
  64.     /* determine ix = ilogb(x) */
  65.     if(hx<0x00100000) {    /* subnormal x */
  66.         if(hx==0) {
  67.         for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
  68.         } else {
  69.         for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
  70.         }
  71.     } else ix = (hx>>20)-1023;
  72.  
  73.     /* determine iy = ilogb(y) */
  74.     if(hy<0x00100000) {    /* subnormal y */
  75.         if(hy==0) {
  76.         for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
  77.         } else {
  78.         for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
  79.         }
  80.     } else iy = (hy>>20)-1023;
  81.  
  82.     /* set up {hx,lx}, {hy,ly} and align y to x */
  83.     if(ix >= -1022) 
  84.         hx = 0x00100000|(0x000fffff&hx);
  85.     else {        /* subnormal x, shift x to normal */
  86.         n = -1022-ix;
  87.         if(n<=31) {
  88.             hx = (hx<<n)|(lx>>(32-n));
  89.             lx <<= n;
  90.         } else {
  91.         hx = lx<<(n-32);
  92.         lx = 0;
  93.         }
  94.     }
  95.     if(iy >= -1022) 
  96.         hy = 0x00100000|(0x000fffff&hy);
  97.     else {        /* subnormal y, shift y to normal */
  98.         n = -1022-iy;
  99.         if(n<=31) {
  100.             hy = (hy<<n)|(ly>>(32-n));
  101.             ly <<= n;
  102.         } else {
  103.         hy = ly<<(n-32);
  104.         ly = 0;
  105.         }
  106.     }
  107.  
  108.     /* fix point fmod */
  109.     n = ix - iy;
  110.     while(n--) {
  111.         hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  112.         if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
  113.         else {
  114.             if((hz|lz)==0)         /* return sign(x)*0 */
  115.             return Zero[(u_int32_t)sx>>31];
  116.             hx = hz+hz+(lz>>31); lx = lz+lz;
  117.         }
  118.     }
  119.     hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
  120.     if(hz>=0) {hx=hz;lx=lz;}
  121.  
  122.     /* convert back to floating value and restore the sign */
  123.     if((hx|lx)==0)             /* return sign(x)*0 */
  124.         return Zero[(u_int32_t)sx>>31];    
  125.     while(hx<0x00100000) {        /* normalize x */
  126.         hx = hx+hx+(lx>>31); lx = lx+lx;
  127.         iy -= 1;
  128.     }
  129.     if(iy>= -1022) {    /* normalize output */
  130.         hx = ((hx-0x00100000)|((iy+1023)<<20));
  131.         INSERT_WORDS(x,hx|sx,lx);
  132.     } else {        /* subnormal output */
  133.         n = -1022 - iy;
  134.         if(n<=20) {
  135.         lx = (lx>>n)|((u_int32_t)hx<<(32-n));
  136.         hx >>= n;
  137.         } else if (n<=31) {
  138.         lx = (hx<<(32-n))|(lx>>n); hx = sx;
  139.         } else {
  140.         lx = hx>>(n-32); hx = sx;
  141.         }
  142.         INSERT_WORDS(x,hx|sx,lx);
  143.         x *= one;        /* create necessary signal */
  144.     }
  145.     return x;        /* exact output */
  146. }
  147.