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