s_remquo.c revision 1.1 1 1.1 christos /* @(#)e_fmod.c 1.3 95/01/18 */
2 1.1 christos /*-
3 1.1 christos * ====================================================
4 1.1 christos * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 1.1 christos *
6 1.1 christos * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 1.1 christos * Permission to use, copy, modify, and distribute this
8 1.1 christos * software is freely granted, provided that this notice
9 1.1 christos * is preserved.
10 1.1 christos * ====================================================
11 1.1 christos */
12 1.1 christos
13 1.1 christos #include <sys/cdefs.h>
14 1.1 christos
15 1.1 christos #include <float.h>
16 1.1 christos
17 1.1 christos #include "math.h"
18 1.1 christos #include "math_private.h"
19 1.1 christos
20 1.1 christos static const double Zero[] = {0.0, -0.0,};
21 1.1 christos
22 1.1 christos /*
23 1.1 christos * Return the IEEE remainder and set *quo to the last n bits of the
24 1.1 christos * quotient, rounded to the nearest integer. We choose n=31 because
25 1.1 christos * we wind up computing all the integer bits of the quotient anyway as
26 1.1 christos * a side-effect of computing the remainder by the shift and subtract
27 1.1 christos * method. In practice, this is far more bits than are needed to use
28 1.1 christos * remquo in reduction algorithms.
29 1.1 christos */
30 1.1 christos double
31 1.1 christos remquo(double x, double y, int *quo)
32 1.1 christos {
33 1.1 christos int32_t n,hx,hy,hz,ix,iy,sx,i;
34 1.1 christos u_int32_t lx,ly,lz,q,sxy;
35 1.1 christos
36 1.1 christos EXTRACT_WORDS(hx,lx,x);
37 1.1 christos EXTRACT_WORDS(hy,ly,y);
38 1.1 christos sxy = (hx ^ hy) & 0x80000000;
39 1.1 christos sx = hx&0x80000000; /* sign of x */
40 1.1 christos hx ^=sx; /* |x| */
41 1.1 christos hy &= 0x7fffffff; /* |y| */
42 1.1 christos
43 1.1 christos /* purge off exception values */
44 1.1 christos if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */
45 1.1 christos ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */
46 1.1 christos return (x*y)/(x*y);
47 1.1 christos if(hx<=hy) {
48 1.1 christos if((hx<hy)||(lx<ly)) {
49 1.1 christos q = 0;
50 1.1 christos goto fixup; /* |x|<|y| return x or x-y */
51 1.1 christos }
52 1.1 christos if(lx==ly) {
53 1.1 christos *quo = 1;
54 1.1 christos return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
55 1.1 christos }
56 1.1 christos }
57 1.1 christos
58 1.1 christos /* determine ix = ilogb(x) */
59 1.1 christos if(hx<0x00100000) { /* subnormal x */
60 1.1 christos if(hx==0) {
61 1.1 christos for (ix = -1043, i=lx; i>0; i<<=1) ix -=1;
62 1.1 christos } else {
63 1.1 christos for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1;
64 1.1 christos }
65 1.1 christos } else ix = (hx>>20)-1023;
66 1.1 christos
67 1.1 christos /* determine iy = ilogb(y) */
68 1.1 christos if(hy<0x00100000) { /* subnormal y */
69 1.1 christos if(hy==0) {
70 1.1 christos for (iy = -1043, i=ly; i>0; i<<=1) iy -=1;
71 1.1 christos } else {
72 1.1 christos for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1;
73 1.1 christos }
74 1.1 christos } else iy = (hy>>20)-1023;
75 1.1 christos
76 1.1 christos /* set up {hx,lx}, {hy,ly} and align y to x */
77 1.1 christos if(ix >= -1022)
78 1.1 christos hx = 0x00100000|(0x000fffff&hx);
79 1.1 christos else { /* subnormal x, shift x to normal */
80 1.1 christos n = -1022-ix;
81 1.1 christos if(n<=31) {
82 1.1 christos hx = (hx<<n)|(lx>>(32-n));
83 1.1 christos lx <<= n;
84 1.1 christos } else {
85 1.1 christos hx = lx<<(n-32);
86 1.1 christos lx = 0;
87 1.1 christos }
88 1.1 christos }
89 1.1 christos if(iy >= -1022)
90 1.1 christos hy = 0x00100000|(0x000fffff&hy);
91 1.1 christos else { /* subnormal y, shift y to normal */
92 1.1 christos n = -1022-iy;
93 1.1 christos if(n<=31) {
94 1.1 christos hy = (hy<<n)|(ly>>(32-n));
95 1.1 christos ly <<= n;
96 1.1 christos } else {
97 1.1 christos hy = ly<<(n-32);
98 1.1 christos ly = 0;
99 1.1 christos }
100 1.1 christos }
101 1.1 christos
102 1.1 christos /* fix point fmod */
103 1.1 christos n = ix - iy;
104 1.1 christos q = 0;
105 1.1 christos while(n--) {
106 1.1 christos hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
107 1.1 christos if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;}
108 1.1 christos else {hx = hz+hz+(lz>>31); lx = lz+lz; q++;}
109 1.1 christos q <<= 1;
110 1.1 christos }
111 1.1 christos hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
112 1.1 christos if(hz>=0) {hx=hz;lx=lz;q++;}
113 1.1 christos
114 1.1 christos /* convert back to floating value and restore the sign */
115 1.1 christos if((hx|lx)==0) { /* return sign(x)*0 */
116 1.1 christos *quo = (sxy ? -q : q);
117 1.1 christos return Zero[(u_int32_t)sx>>31];
118 1.1 christos }
119 1.1 christos while(hx<0x00100000) { /* normalize x */
120 1.1 christos hx = hx+hx+(lx>>31); lx = lx+lx;
121 1.1 christos iy -= 1;
122 1.1 christos }
123 1.1 christos if(iy>= -1022) { /* normalize output */
124 1.1 christos hx = ((hx-0x00100000)|((iy+1023)<<20));
125 1.1 christos } else { /* subnormal output */
126 1.1 christos n = -1022 - iy;
127 1.1 christos if(n<=20) {
128 1.1 christos lx = (lx>>n)|((u_int32_t)hx<<(32-n));
129 1.1 christos hx >>= n;
130 1.1 christos } else if (n<=31) {
131 1.1 christos lx = (hx<<(32-n))|(lx>>n); hx = sx;
132 1.1 christos } else {
133 1.1 christos lx = hx>>(n-32); hx = sx;
134 1.1 christos }
135 1.1 christos }
136 1.1 christos fixup:
137 1.1 christos INSERT_WORDS(x,hx,lx);
138 1.1 christos y = fabs(y);
139 1.1 christos if (y < 0x1p-1021) {
140 1.1 christos if (x+x>y || (x+x==y && (q & 1))) {
141 1.1 christos q++;
142 1.1 christos x-=y;
143 1.1 christos }
144 1.1 christos } else if (x>0.5*y || (x==0.5*y && (q & 1))) {
145 1.1 christos q++;
146 1.1 christos x-=y;
147 1.1 christos }
148 1.1 christos GET_HIGH_WORD(hx,x);
149 1.1 christos SET_HIGH_WORD(x,hx^sx);
150 1.1 christos q &= 0x7fffffff;
151 1.1 christos *quo = (sxy ? -q : q);
152 1.1 christos return x;
153 1.1 christos }
154