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.7 wiz __RCSID("$NetBSD: e_fmodf.c,v 1.7 2002/05/26 22:01:49 wiz 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 static const float one = 1.0, Zero[] = {0.0, -0.0,}; 31 1.1 jtc 32 1.7 wiz float 33 1.7 wiz __ieee754_fmodf(float x, float y) 34 1.1 jtc { 35 1.2 jtc int32_t n,hx,hy,hz,ix,iy,sx,i; 36 1.1 jtc 37 1.1 jtc GET_FLOAT_WORD(hx,x); 38 1.1 jtc GET_FLOAT_WORD(hy,y); 39 1.1 jtc sx = hx&0x80000000; /* sign of x */ 40 1.1 jtc hx ^=sx; /* |x| */ 41 1.1 jtc hy &= 0x7fffffff; /* |y| */ 42 1.1 jtc 43 1.1 jtc /* purge off exception values */ 44 1.1 jtc if(hy==0||(hx>=0x7f800000)|| /* y=0,or x not finite */ 45 1.1 jtc (hy>0x7f800000)) /* or y is NaN */ 46 1.1 jtc return (x*y)/(x*y); 47 1.1 jtc if(hx<hy) return x; /* |x|<|y| return x */ 48 1.1 jtc if(hx==hy) 49 1.2 jtc return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/ 50 1.1 jtc 51 1.1 jtc /* determine ix = ilogb(x) */ 52 1.1 jtc if(hx<0x00800000) { /* subnormal x */ 53 1.1 jtc for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1; 54 1.1 jtc } else ix = (hx>>23)-127; 55 1.1 jtc 56 1.1 jtc /* determine iy = ilogb(y) */ 57 1.1 jtc if(hy<0x00800000) { /* subnormal y */ 58 1.1 jtc for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1; 59 1.1 jtc } else iy = (hy>>23)-127; 60 1.1 jtc 61 1.1 jtc /* set up {hx,lx}, {hy,ly} and align y to x */ 62 1.6 simonb if(ix >= -126) 63 1.1 jtc hx = 0x00800000|(0x007fffff&hx); 64 1.1 jtc else { /* subnormal x, shift x to normal */ 65 1.1 jtc n = -126-ix; 66 1.1 jtc hx = hx<<n; 67 1.1 jtc } 68 1.6 simonb if(iy >= -126) 69 1.1 jtc hy = 0x00800000|(0x007fffff&hy); 70 1.1 jtc else { /* subnormal y, shift y to normal */ 71 1.1 jtc n = -126-iy; 72 1.1 jtc hy = hy<<n; 73 1.1 jtc } 74 1.1 jtc 75 1.1 jtc /* fix point fmod */ 76 1.1 jtc n = ix - iy; 77 1.1 jtc while(n--) { 78 1.1 jtc hz=hx-hy; 79 1.1 jtc if(hz<0){hx = hx+hx;} 80 1.1 jtc else { 81 1.1 jtc if(hz==0) /* return sign(x)*0 */ 82 1.2 jtc return Zero[(u_int32_t)sx>>31]; 83 1.1 jtc hx = hz+hz; 84 1.1 jtc } 85 1.1 jtc } 86 1.1 jtc hz=hx-hy; 87 1.1 jtc if(hz>=0) {hx=hz;} 88 1.1 jtc 89 1.1 jtc /* convert back to floating value and restore the sign */ 90 1.1 jtc if(hx==0) /* return sign(x)*0 */ 91 1.6 simonb return Zero[(u_int32_t)sx>>31]; 92 1.1 jtc while(hx<0x00800000) { /* normalize x */ 93 1.1 jtc hx = hx+hx; 94 1.1 jtc iy -= 1; 95 1.1 jtc } 96 1.1 jtc if(iy>= -126) { /* normalize output */ 97 1.1 jtc hx = ((hx-0x00800000)|((iy+127)<<23)); 98 1.1 jtc SET_FLOAT_WORD(x,hx|sx); 99 1.1 jtc } else { /* subnormal output */ 100 1.1 jtc n = -126 - iy; 101 1.1 jtc hx >>= n; 102 1.1 jtc SET_FLOAT_WORD(x,hx|sx); 103 1.1 jtc x *= one; /* create necessary signal */ 104 1.1 jtc } 105 1.1 jtc return x; /* exact output */ 106 1.1 jtc } 107