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