1*05a0b428SJohn Marino /* e_fmodf.c -- float version of e_fmod.c.
2*05a0b428SJohn Marino * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3*05a0b428SJohn Marino */
4*05a0b428SJohn Marino
5*05a0b428SJohn Marino /*
6*05a0b428SJohn Marino * ====================================================
7*05a0b428SJohn Marino * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8*05a0b428SJohn Marino *
9*05a0b428SJohn Marino * Developed at SunPro, a Sun Microsystems, Inc. business.
10*05a0b428SJohn Marino * Permission to use, copy, modify, and distribute this
11*05a0b428SJohn Marino * software is freely granted, provided that this notice
12*05a0b428SJohn Marino * is preserved.
13*05a0b428SJohn Marino * ====================================================
14*05a0b428SJohn Marino */
15*05a0b428SJohn Marino
16*05a0b428SJohn Marino /*
17*05a0b428SJohn Marino * fmodf(x,y)
18*05a0b428SJohn Marino * Return x mod y in exact arithmetic
19*05a0b428SJohn Marino * Method: shift and subtract
20*05a0b428SJohn Marino */
21*05a0b428SJohn Marino
22*05a0b428SJohn Marino #include "math.h"
23*05a0b428SJohn Marino #include "math_private.h"
24*05a0b428SJohn Marino
25*05a0b428SJohn Marino static const float one = 1.0, Zero[] = {0.0, -0.0,};
26*05a0b428SJohn Marino
27*05a0b428SJohn Marino float
fmodf(float x,float y)28*05a0b428SJohn Marino fmodf(float x, float y)
29*05a0b428SJohn Marino {
30*05a0b428SJohn Marino int32_t n,hx,hy,hz,ix,iy,sx,i;
31*05a0b428SJohn Marino
32*05a0b428SJohn Marino GET_FLOAT_WORD(hx,x);
33*05a0b428SJohn Marino GET_FLOAT_WORD(hy,y);
34*05a0b428SJohn Marino sx = hx&0x80000000; /* sign of x */
35*05a0b428SJohn Marino hx ^=sx; /* |x| */
36*05a0b428SJohn Marino hy &= 0x7fffffff; /* |y| */
37*05a0b428SJohn Marino
38*05a0b428SJohn Marino /* purge off exception values */
39*05a0b428SJohn Marino if(hy==0||(hx>=0x7f800000)|| /* y=0,or x not finite */
40*05a0b428SJohn Marino (hy>0x7f800000)) /* or y is NaN */
41*05a0b428SJohn Marino return (x*y)/(x*y);
42*05a0b428SJohn Marino if(hx<hy) return x; /* |x|<|y| return x */
43*05a0b428SJohn Marino if(hx==hy)
44*05a0b428SJohn Marino return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
45*05a0b428SJohn Marino
46*05a0b428SJohn Marino /* determine ix = ilogb(x) */
47*05a0b428SJohn Marino if(hx<0x00800000) { /* subnormal x */
48*05a0b428SJohn Marino for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
49*05a0b428SJohn Marino } else ix = (hx>>23)-127;
50*05a0b428SJohn Marino
51*05a0b428SJohn Marino /* determine iy = ilogb(y) */
52*05a0b428SJohn Marino if(hy<0x00800000) { /* subnormal y */
53*05a0b428SJohn Marino for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
54*05a0b428SJohn Marino } else iy = (hy>>23)-127;
55*05a0b428SJohn Marino
56*05a0b428SJohn Marino /* set up {hx,lx}, {hy,ly} and align y to x */
57*05a0b428SJohn Marino if(ix >= -126)
58*05a0b428SJohn Marino hx = 0x00800000|(0x007fffff&hx);
59*05a0b428SJohn Marino else { /* subnormal x, shift x to normal */
60*05a0b428SJohn Marino n = -126-ix;
61*05a0b428SJohn Marino hx = hx<<n;
62*05a0b428SJohn Marino }
63*05a0b428SJohn Marino if(iy >= -126)
64*05a0b428SJohn Marino hy = 0x00800000|(0x007fffff&hy);
65*05a0b428SJohn Marino else { /* subnormal y, shift y to normal */
66*05a0b428SJohn Marino n = -126-iy;
67*05a0b428SJohn Marino hy = hy<<n;
68*05a0b428SJohn Marino }
69*05a0b428SJohn Marino
70*05a0b428SJohn Marino /* fix point fmod */
71*05a0b428SJohn Marino n = ix - iy;
72*05a0b428SJohn Marino while(n--) {
73*05a0b428SJohn Marino hz=hx-hy;
74*05a0b428SJohn Marino if(hz<0){hx = hx+hx;}
75*05a0b428SJohn Marino else {
76*05a0b428SJohn Marino if(hz==0) /* return sign(x)*0 */
77*05a0b428SJohn Marino return Zero[(u_int32_t)sx>>31];
78*05a0b428SJohn Marino hx = hz+hz;
79*05a0b428SJohn Marino }
80*05a0b428SJohn Marino }
81*05a0b428SJohn Marino hz=hx-hy;
82*05a0b428SJohn Marino if(hz>=0) {hx=hz;}
83*05a0b428SJohn Marino
84*05a0b428SJohn Marino /* convert back to floating value and restore the sign */
85*05a0b428SJohn Marino if(hx==0) /* return sign(x)*0 */
86*05a0b428SJohn Marino return Zero[(u_int32_t)sx>>31];
87*05a0b428SJohn Marino while(hx<0x00800000) { /* normalize x */
88*05a0b428SJohn Marino hx = hx+hx;
89*05a0b428SJohn Marino iy -= 1;
90*05a0b428SJohn Marino }
91*05a0b428SJohn Marino if(iy>= -126) { /* normalize output */
92*05a0b428SJohn Marino hx = ((hx-0x00800000)|((iy+127)<<23));
93*05a0b428SJohn Marino SET_FLOAT_WORD(x,hx|sx);
94*05a0b428SJohn Marino } else { /* subnormal output */
95*05a0b428SJohn Marino n = -126 - iy;
96*05a0b428SJohn Marino hx >>= n;
97*05a0b428SJohn Marino SET_FLOAT_WORD(x,hx|sx);
98*05a0b428SJohn Marino x *= one; /* create necessary signal */
99*05a0b428SJohn Marino }
100*05a0b428SJohn Marino return x; /* exact output */
101*05a0b428SJohn Marino }
102