1 /* origin: FreeBSD /usr/src/lib/msun/src/s_remquof.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
13 * Return the IEEE remainder and set *quo to the last n bits of the
14 * quotient, rounded to the nearest integer. We choose n=31 because
15 * we wind up computing all the integer bits of the quotient anyway as
16 * a side-effect of computing the remainder by the shift and subtract
17 * method. In practice, this is far more bits than are needed to use
18 * remquo in reduction algorithms.
23 static const float Zero[] = {0.0, -0.0,};
25 float remquof(float x, float y, int *quo)
27 int32_t n,hx,hy,hz,ix,iy,sx,i;
30 GET_FLOAT_WORD(hx, x);
31 GET_FLOAT_WORD(hy, y);
32 sxy = (hx ^ hy) & 0x80000000;
33 sx = hx & 0x80000000; /* sign of x */
35 hy &= 0x7fffffff; /* |y| */
37 /* purge off exception values */
38 if (hy == 0 || hx >= 0x7f800000 || hy > 0x7f800000) /* y=0,NaN;or x not finite */
40 if (hx < hy) { /* |x| < |y| return x or x-y */
43 } else if(hx==hy) { /* |x| = |y| return x*0*/
45 return Zero[(uint32_t)sx>>31];
48 /* determine ix = ilogb(x) */
49 if (hx < 0x00800000) { /* subnormal x */
50 for (ix = -126, i=hx<<8; i>0; i<<=1) ix--;
54 /* determine iy = ilogb(y) */
55 if (hy < 0x00800000) { /* subnormal y */
56 for (iy = -126, i=hy<<8; i>0; i<<=1) iy--;
60 /* set up {hx,lx}, {hy,ly} and align y to x */
62 hx = 0x00800000|(0x007fffff&hx);
63 else { /* subnormal x, shift x to normal */
68 hy = 0x00800000|(0x007fffff&hy);
69 else { /* subnormal y, shift y to normal */
93 /* convert back to floating value and restore the sign */
94 if (hx == 0) { /* return sign(x)*0 */
97 return Zero[(uint32_t)sx>>31];
99 while (hx < 0x00800000) { /* normalize x */
103 if (iy >= -126) { /* normalize output */
104 hx = (hx-0x00800000)|((iy+127)<<23);
105 } else { /* subnormal output */
110 SET_FLOAT_WORD(x,hx);
113 if (x + x > y || (x + x == y && (q & 1))) {
117 } else if (x > 0.5f*y || (x == 0.5f*y && (q & 1))) {
121 GET_FLOAT_WORD(hx, x);
122 SET_FLOAT_WORD(x, hx ^ sx);