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