Home | History | Annotate | Line # | Download | only in src
e_fmodf.c revision 1.6
      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.6  simonb  * 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.5   lukem #include <sys/cdefs.h>
     17  1.3     jtc #if defined(LIBM_SCCS) && !defined(lint)
     18  1.6  simonb __RCSID("$NetBSD: e_fmodf.c,v 1.6 1999/07/02 15:37:39 simonb Exp $");
     19  1.1     jtc #endif
     20  1.1     jtc 
     21  1.6  simonb /*
     22  1.1     jtc  * __ieee754_fmodf(x,y)
     23  1.1     jtc  * Return x mod y in exact arithmetic
     24  1.1     jtc  * Method: shift and subtract
     25  1.1     jtc  */
     26  1.1     jtc 
     27  1.1     jtc #include "math.h"
     28  1.1     jtc #include "math_private.h"
     29  1.1     jtc 
     30  1.1     jtc #ifdef __STDC__
     31  1.1     jtc static const float one = 1.0, Zero[] = {0.0, -0.0,};
     32  1.1     jtc #else
     33  1.1     jtc static float one = 1.0, Zero[] = {0.0, -0.0,};
     34  1.1     jtc #endif
     35  1.1     jtc 
     36  1.1     jtc #ifdef __STDC__
     37  1.1     jtc 	float __ieee754_fmodf(float x, float y)
     38  1.1     jtc #else
     39  1.1     jtc 	float __ieee754_fmodf(x,y)
     40  1.1     jtc 	float x,y ;
     41  1.1     jtc #endif
     42  1.1     jtc {
     43  1.2     jtc 	int32_t n,hx,hy,hz,ix,iy,sx,i;
     44  1.1     jtc 
     45  1.1     jtc 	GET_FLOAT_WORD(hx,x);
     46  1.1     jtc 	GET_FLOAT_WORD(hy,y);
     47  1.1     jtc 	sx = hx&0x80000000;		/* sign of x */
     48  1.1     jtc 	hx ^=sx;		/* |x| */
     49  1.1     jtc 	hy &= 0x7fffffff;	/* |y| */
     50  1.1     jtc 
     51  1.1     jtc     /* purge off exception values */
     52  1.1     jtc 	if(hy==0||(hx>=0x7f800000)||		/* y=0,or x not finite */
     53  1.1     jtc 	   (hy>0x7f800000))			/* or y is NaN */
     54  1.1     jtc 	    return (x*y)/(x*y);
     55  1.1     jtc 	if(hx<hy) return x;			/* |x|<|y| return x */
     56  1.1     jtc 	if(hx==hy)
     57  1.2     jtc 	    return Zero[(u_int32_t)sx>>31];	/* |x|=|y| return x*0*/
     58  1.1     jtc 
     59  1.1     jtc     /* determine ix = ilogb(x) */
     60  1.1     jtc 	if(hx<0x00800000) {	/* subnormal x */
     61  1.1     jtc 	    for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
     62  1.1     jtc 	} else ix = (hx>>23)-127;
     63  1.1     jtc 
     64  1.1     jtc     /* determine iy = ilogb(y) */
     65  1.1     jtc 	if(hy<0x00800000) {	/* subnormal y */
     66  1.1     jtc 	    for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
     67  1.1     jtc 	} else iy = (hy>>23)-127;
     68  1.1     jtc 
     69  1.1     jtc     /* set up {hx,lx}, {hy,ly} and align y to x */
     70  1.6  simonb 	if(ix >= -126)
     71  1.1     jtc 	    hx = 0x00800000|(0x007fffff&hx);
     72  1.1     jtc 	else {		/* subnormal x, shift x to normal */
     73  1.1     jtc 	    n = -126-ix;
     74  1.1     jtc 	    hx = hx<<n;
     75  1.1     jtc 	}
     76  1.6  simonb 	if(iy >= -126)
     77  1.1     jtc 	    hy = 0x00800000|(0x007fffff&hy);
     78  1.1     jtc 	else {		/* subnormal y, shift y to normal */
     79  1.1     jtc 	    n = -126-iy;
     80  1.1     jtc 	    hy = hy<<n;
     81  1.1     jtc 	}
     82  1.1     jtc 
     83  1.1     jtc     /* fix point fmod */
     84  1.1     jtc 	n = ix - iy;
     85  1.1     jtc 	while(n--) {
     86  1.1     jtc 	    hz=hx-hy;
     87  1.1     jtc 	    if(hz<0){hx = hx+hx;}
     88  1.1     jtc 	    else {
     89  1.1     jtc 	    	if(hz==0) 		/* return sign(x)*0 */
     90  1.2     jtc 		    return Zero[(u_int32_t)sx>>31];
     91  1.1     jtc 	    	hx = hz+hz;
     92  1.1     jtc 	    }
     93  1.1     jtc 	}
     94  1.1     jtc 	hz=hx-hy;
     95  1.1     jtc 	if(hz>=0) {hx=hz;}
     96  1.1     jtc 
     97  1.1     jtc     /* convert back to floating value and restore the sign */
     98  1.1     jtc 	if(hx==0) 			/* return sign(x)*0 */
     99  1.6  simonb 	    return Zero[(u_int32_t)sx>>31];
    100  1.1     jtc 	while(hx<0x00800000) {		/* normalize x */
    101  1.1     jtc 	    hx = hx+hx;
    102  1.1     jtc 	    iy -= 1;
    103  1.1     jtc 	}
    104  1.1     jtc 	if(iy>= -126) {		/* normalize output */
    105  1.1     jtc 	    hx = ((hx-0x00800000)|((iy+127)<<23));
    106  1.1     jtc 	    SET_FLOAT_WORD(x,hx|sx);
    107  1.1     jtc 	} else {		/* subnormal output */
    108  1.1     jtc 	    n = -126 - iy;
    109  1.1     jtc 	    hx >>= n;
    110  1.1     jtc 	    SET_FLOAT_WORD(x,hx|sx);
    111  1.1     jtc 	    x *= one;		/* create necessary signal */
    112  1.1     jtc 	}
    113  1.1     jtc 	return x;		/* exact output */
    114  1.1     jtc }
    115