xref: /freebsd/lib/msun/src/s_remquof.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 "math.h"
13 #include "math_private.h"
14 
15 static const float Zero[] = {0.0, -0.0,};
16 
17 /*
18  * Return the IEEE remainder and set *quo to the last n bits of the
19  * quotient, rounded to the nearest integer.  We choose n=31 because
20  * we wind up computing all the integer bits of the quotient anyway as
21  * a side-effect of computing the remainder by the shift and subtract
22  * method.  In practice, this is far more bits than are needed to use
23  * remquo in reduction algorithms.
24  */
25 float
26 remquof(float x, float y, int *quo)
27 {
28 	int32_t n,hx,hy,hz,ix,iy,sx,i;
29 	u_int32_t q,sxy;
30 
31 	GET_FLOAT_WORD(hx,x);
32 	GET_FLOAT_WORD(hy,y);
33 	sxy = (hx ^ hy) & 0x80000000;
34 	sx = hx&0x80000000;		/* sign of x */
35 	hx ^=sx;		/* |x| */
36 	hy &= 0x7fffffff;	/* |y| */
37 
38     /* purge off exception values */
39 	if(hy==0||hx>=0x7f800000||hy>0x7f800000) /* y=0,NaN;or x not finite */
40 	    return nan_mix_op(x, y, *)/nan_mix_op(x, y, *);
41 	if(hx<hy) {
42 	    q = 0;
43 	    goto fixup;	/* |x|<|y| return x or x-y */
44 	} else if(hx==hy) {
45 	    *quo = (sxy ? -1 : 1);
46 	    return Zero[(u_int32_t)sx>>31];	/* |x|=|y| return x*0*/
47 	}
48 
49     /* determine ix = ilogb(x) */
50 	if(hx<0x00800000) {	/* subnormal x */
51 	    for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
52 	} else ix = (hx>>23)-127;
53 
54     /* determine iy = ilogb(y) */
55 	if(hy<0x00800000) {	/* subnormal y */
56 	    for (iy = -126,i=(hy<<8); i>0; i<<=1) iy -=1;
57 	} else iy = (hy>>23)-127;
58 
59     /* set up {hx,lx}, {hy,ly} and align y to x */
60 	if(ix >= -126)
61 	    hx = 0x00800000|(0x007fffff&hx);
62 	else {		/* subnormal x, shift x to normal */
63 	    n = -126-ix;
64 	    hx <<= n;
65 	}
66 	if(iy >= -126)
67 	    hy = 0x00800000|(0x007fffff&hy);
68 	else {		/* subnormal y, shift y to normal */
69 	    n = -126-iy;
70 	    hy <<= n;
71 	}
72 
73     /* fix point fmod */
74 	n = ix - iy;
75 	q = 0;
76 	while(n--) {
77 	    hz=hx-hy;
78 	    if(hz<0) hx = hx << 1;
79 	    else {hx = hz << 1; q++;}
80 	    q <<= 1;
81 	}
82 	hz=hx-hy;
83 	if(hz>=0) {hx=hz;q++;}
84 
85     /* convert back to floating value and restore the sign */
86 	if(hx==0) {				/* return sign(x)*0 */
87 	    q &= 0x7fffffff;
88 	    *quo = (sxy ? -q : q);
89 	    return Zero[(u_int32_t)sx>>31];
90 	}
91 	while(hx<0x00800000) {		/* normalize x */
92 	    hx <<= 1;
93 	    iy -= 1;
94 	}
95 	if(iy>= -126) {		/* normalize output */
96 	    hx = ((hx-0x00800000)|((iy+127)<<23));
97 	} else {		/* subnormal output */
98 	    n = -126 - iy;
99 	    hx >>= n;
100 	}
101 fixup:
102 	SET_FLOAT_WORD(x,hx);
103 	y = fabsf(y);
104 	if (y < 0x1p-125f) {
105 	    if (x+x>y || (x+x==y && (q & 1))) {
106 		q++;
107 		x-=y;
108 	    }
109 	} else if (x>0.5f*y || (x==0.5f*y && (q & 1))) {
110 	    q++;
111 	    x-=y;
112 	}
113 	GET_FLOAT_WORD(hx,x);
114 	SET_FLOAT_WORD(x,hx^sx);
115 	q &= 0x7fffffff;
116 	*quo = (sxy ? -q : q);
117 	return x;
118 }
119