xref: /freebsd/lib/msun/src/s_remquof.c (revision c58c77246f88da87f309e8c449e98195d43e2c76)
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
remquof(float x,float y,int * quo)26 remquof(float x, float y, int *quo)
27 {
28 	int32_t hx, hy, hz, ix, iy, n, sx;
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)
51 	    ix = subnormal_ilogbf(hx);
52 	else
53 	    ix = (hx>>23)-127;
54 
55     /* determine iy = ilogb(y) */
56 	if(hy<0x00800000)
57 	    iy = subnormal_ilogbf(hy);
58 	else
59 	    iy = (hy>>23)-127;
60 
61     /* set up {hx,lx}, {hy,ly} and align y to x */
62 	if(ix >= -126)
63 	    hx = 0x00800000|(0x007fffff&hx);
64 	else {		/* subnormal x, shift x to normal */
65 	    n = -126-ix;
66 	    hx <<= n;
67 	}
68 	if(iy >= -126)
69 	    hy = 0x00800000|(0x007fffff&hy);
70 	else {		/* subnormal y, shift y to normal */
71 	    n = -126-iy;
72 	    hy <<= n;
73 	}
74 
75     /* fix point fmod */
76 	n = ix - iy;
77 	q = 0;
78 	while(n--) {
79 	    hz=hx-hy;
80 	    if(hz<0) hx = hx << 1;
81 	    else {hx = hz << 1; q++;}
82 	    q <<= 1;
83 	}
84 	hz=hx-hy;
85 	if(hz>=0) {hx=hz;q++;}
86 
87     /* convert back to floating value and restore the sign */
88 	if(hx==0) {				/* return sign(x)*0 */
89 	    q &= 0x7fffffff;
90 	    *quo = (sxy ? -q : q);
91 	    return Zero[(u_int32_t)sx>>31];
92 	}
93 	while(hx<0x00800000) {		/* normalize x */
94 	    hx <<= 1;
95 	    iy -= 1;
96 	}
97 	if(iy>= -126) {		/* normalize output */
98 	    hx = ((hx-0x00800000)|((iy+127)<<23));
99 	} else {		/* subnormal output */
100 	    n = -126 - iy;
101 	    hx >>= n;
102 	}
103 fixup:
104 	SET_FLOAT_WORD(x,hx);
105 	y = fabsf(y);
106 	if (y < 0x1p-125f) {
107 	    if (x+x>y || (x+x==y && (q & 1))) {
108 		q++;
109 		x-=y;
110 	    }
111 	} else if (x>0.5f*y || (x==0.5f*y && (q & 1))) {
112 	    q++;
113 	    x-=y;
114 	}
115 	GET_FLOAT_WORD(hx,x);
116 	SET_FLOAT_WORD(x,hx^sx);
117 	q &= 0x7fffffff;
118 	*quo = (sxy ? -q : q);
119 	return x;
120 }
121