• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11 
12 /*
13  * __ieee754_fmod(x,y)
14  * Return x mod y in exact arithmetic
15  * Method: shift and subtract
16  */
17 
18 #include "bsd_private.h"
19 
bsd__ieee754_fmod(double x,double y)20 double bsd__ieee754_fmod(double x, double y)
21 {
22     int32_t n,hx,hy,hz,ix,iy,sx,i;
23     u_int32_t lx,ly,lz;
24 
25     EXTRACT_WORDS(hx,lx,x);
26     EXTRACT_WORDS(hy,ly,y);
27     sx = hx&0x80000000;        /* sign of x */
28     hx ^=sx;                   /* |x| */
29     hy &= 0x7fffffff;          /* |y| */
30 
31     /* purge off exception values */
32     if((hy|ly)==0||(hx>=0x7ff00000)||      /* y=0,or x not finite */
33       ((hy|((ly|-ly)>>31))>0x7ff00000))    /* or y is NaN */
34         return (x*y)/(x*y);
35     if(hx<=hy) {
36         if((hx<hy)||(lx<ly)) return x;     /* |x|<|y| return x */
37         if(lx==ly)
38         return Zero[(u_int32_t)sx>>31];    /* |x|=|y| return x*0*/
39     }
40 
41     /* determine ix = ilogb(x) */
42     if(hx<0x00100000) {    /* subnormal x */
43         if(hx==0) {
44             for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
45         } else {
46             for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
47         }
48     } else ix = (hx>>20)-1023;
49 
50     /* determine iy = ilogb(y) */
51     if(hy<0x00100000) {    /* subnormal y */
52         if(hy==0) {
53             for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
54         } else {
55             for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
56         }
57     } else iy = (hy>>20)-1023;
58 
59     /* set up {hx,lx}, {hy,ly} and align y to x */
60     if(ix >= -1022)
61         hx = 0x00100000|(0x000fffff&hx);
62     else {        /* subnormal x, shift x to normal */
63         n = -1022-ix;
64         if(n<=31) {
65             hx = (hx<<n)|(lx>>(32-n));
66             lx <<= n;
67         } else {
68             hx = lx<<(n-32);
69             lx = 0;
70         }
71     }
72     if(iy >= -1022)
73         hy = 0x00100000|(0x000fffff&hy);
74     else {        /* subnormal y, shift y to normal */
75         n = -1022-iy;
76         if(n<=31) {
77             hy = (hy<<n)|(ly>>(32-n));
78             ly <<= n;
79         } else {
80             hy = ly<<(n-32);
81             ly = 0;
82         }
83     }
84 
85     /* fix point fmod */
86     n = ix - iy;
87     while(n--) {
88         hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
89         if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
90         else {
91             if((hz|lz)==0)    /* return sign(x)*0 */
92                 return Zero[(u_int32_t)sx>>31];
93             hx = hz+hz+(lz>>31); lx = lz+lz;
94         }
95     }
96     hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
97     if(hz>=0) {hx=hz;lx=lz;}
98 
99     /* convert back to floating value and restore the sign */
100     if((hx|lx)==0)                /* return sign(x)*0 */
101         return Zero[(u_int32_t)sx>>31];
102     while(hx<0x00100000) {        /* normalize x */
103         hx = hx+hx+(lx>>31); lx = lx+lx;
104         iy -= 1;
105     }
106     if(iy>= -1022) {     /* normalize output */
107         hx = ((hx-0x00100000)|((iy+1023)<<20));
108         INSERT_WORDS(x,hx|sx,lx);
109     } else {             /* subnormal output */
110         n = -1022 - iy;
111         if(n<=20) {
112             lx = (lx>>n)|((u_int32_t)hx<<(32-n));
113             hx >>= n;
114         } else if (n<=31) {
115             lx = (hx<<(32-n))|(lx>>n); hx = sx;
116         } else {
117             lx = hx>>(n-32); hx = sx;
118         }
119         INSERT_WORDS(x,hx|sx,lx);
120         x *= one;        /* create necessary signal */
121     }
122     return x;            /* exact output */
123 }
124