e_fmodf.c revision 97407
1204076Spjd/* e_fmodf.c -- float version of e_fmod.c.
2204076Spjd * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3204076Spjd */
4204076Spjd
5204076Spjd/*
6204076Spjd * ====================================================
7204076Spjd * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8204076Spjd *
9219351Spjd * Developed at SunPro, a Sun Microsystems, Inc. business.
10204076Spjd * Permission to use, copy, modify, and distribute this
11219354Spjd * software is freely granted, provided that this notice
12219354Spjd * is preserved.
13204076Spjd * ====================================================
14204076Spjd */
15204076Spjd
16219884Spjd#ifndef lint
17204076Spjdstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/e_fmodf.c 97407 2002-05-28 17:03:12Z alfred $";
18204076Spjd#endif
19204076Spjd
20204076Spjd/*
21204076Spjd * __ieee754_fmodf(x,y)
22219370Spjd * Return x mod y in exact arithmetic
23228712Sdim * Method: shift and subtract
24204076Spjd */
25223586Spjd
26204076Spjd#include "math.h"
27204076Spjd#include "math_private.h"
28204076Spjd
29204076Spjdstatic const float one = 1.0, Zero[] = {0.0, -0.0,};
30204076Spjd
31204076Spjdfloat
32226731Spjd__ieee754_fmodf(float x, float y)
33204076Spjd{
34219370Spjd	int32_t n,hx,hy,hz,ix,iy,sx,i;
35219370Spjd
36207070Spjd	GET_FLOAT_WORD(hx,x);
37207070Spjd	GET_FLOAT_WORD(hy,y);
38207070Spjd	sx = hx&0x80000000;		/* sign of x */
39207070Spjd	hx ^=sx;		/* |x| */
40207070Spjd	hy &= 0x7fffffff;	/* |y| */
41204076Spjd
42204076Spjd    /* purge off exception values */
43204076Spjd	if(hy==0||(hx>=0x7f800000)||		/* y=0,or x not finite */
44204076Spjd	   (hy>0x7f800000))			/* or y is NaN */
45204076Spjd	    return (x*y)/(x*y);
46204076Spjd	if(hx<hy) return x;			/* |x|<|y| return x */
47	if(hx==hy)
48	    return Zero[(u_int32_t)sx>>31];	/* |x|=|y| return x*0*/
49
50    /* determine ix = ilogb(x) */
51	if(hx<0x00800000) {	/* subnormal x */
52	    for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
53	} else ix = (hx>>23)-127;
54
55    /* determine iy = ilogb(y) */
56	if(hy<0x00800000) {	/* subnormal y */
57	    for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
58	} else iy = (hy>>23)-127;
59
60    /* set up {hx,lx}, {hy,ly} and align y to x */
61	if(ix >= -126)
62	    hx = 0x00800000|(0x007fffff&hx);
63	else {		/* subnormal x, shift x to normal */
64	    n = -126-ix;
65	    hx = hx<<n;
66	}
67	if(iy >= -126)
68	    hy = 0x00800000|(0x007fffff&hy);
69	else {		/* subnormal y, shift y to normal */
70	    n = -126-iy;
71	    hy = hy<<n;
72	}
73
74    /* fix point fmod */
75	n = ix - iy;
76	while(n--) {
77	    hz=hx-hy;
78	    if(hz<0){hx = hx+hx;}
79	    else {
80	    	if(hz==0) 		/* return sign(x)*0 */
81		    return Zero[(u_int32_t)sx>>31];
82	    	hx = hz+hz;
83	    }
84	}
85	hz=hx-hy;
86	if(hz>=0) {hx=hz;}
87
88    /* convert back to floating value and restore the sign */
89	if(hx==0) 			/* return sign(x)*0 */
90	    return Zero[(u_int32_t)sx>>31];
91	while(hx<0x00800000) {		/* normalize x */
92	    hx = hx+hx;
93	    iy -= 1;
94	}
95	if(iy>= -126) {		/* normalize output */
96	    hx = ((hx-0x00800000)|((iy+127)<<23));
97	    SET_FLOAT_WORD(x,hx|sx);
98	} else {		/* subnormal output */
99	    n = -126 - iy;
100	    hx >>= n;
101	    SET_FLOAT_WORD(x,hx|sx);
102	    x *= one;		/* create necessary signal */
103	}
104	return x;		/* exact output */
105}
106