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