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