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