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