POK
|
00001 /* 00002 * POK header 00003 * 00004 * The following file is a part of the POK project. Any modification should 00005 * made according to the POK licence. You CANNOT use this file or a part of 00006 * this file is this part of a file for your own project 00007 * 00008 * For more information on the POK licence, please see our LICENCE FILE 00009 * 00010 * Please follow the coding guidelines described in doc/CODING_GUIDELINES 00011 * 00012 * Copyright (c) 2007-2009 POK team 00013 * 00014 * Created by julien on Fri Jan 30 14:41:34 2009 00015 */ 00016 00017 /* @(#)e_fmod.c 5.1 93/09/24 */ 00018 /* 00019 * ==================================================== 00020 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 00021 * 00022 * Developed at SunPro, a Sun Microsystems, Inc. business. 00023 * Permission to use, copy, modify, and distribute this 00024 * software is freely granted, provided that this notice 00025 * is preserved. 00026 * ==================================================== 00027 */ 00028 00029 /* 00030 * __ieee754_fmod(x,y) 00031 * Return x mod y in exact arithmetic 00032 * Method: shift and subtract 00033 */ 00034 00035 #ifdef POK_NEEDS_LIBMATH 00036 00037 #include "math_private.h" 00038 00039 static const double one = 1.0, Zero[] = {0.0, -0.0,}; 00040 00041 double 00042 __ieee754_fmod(double x, double y) 00043 { 00044 int32_t n,hx,hy,hz,ix,iy,sx,i; 00045 uint32_t lx,ly,lz; 00046 00047 EXTRACT_WORDS(hx,lx,x); 00048 EXTRACT_WORDS(hy,ly,y); 00049 sx = hx&0x80000000; /* sign of x */ 00050 hx ^=sx; /* |x| */ 00051 hy &= 0x7fffffff; /* |y| */ 00052 00053 /* purge off exception values */ 00054 if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ 00055 ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */ 00056 return (x*y)/(x*y); 00057 if(hx<=hy) { 00058 if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */ 00059 if(lx==ly) 00060 return Zero[(uint32_t)sx>>31]; /* |x|=|y| return x*0*/ 00061 } 00062 00063 /* determine ix = ilogb(x) */ 00064 if(hx<0x00100000) { /* subnormal x */ 00065 if(hx==0) { 00066 for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; 00067 } else { 00068 for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; 00069 } 00070 } else ix = (hx>>20)-1023; 00071 00072 /* determine iy = ilogb(y) */ 00073 if(hy<0x00100000) { /* subnormal y */ 00074 if(hy==0) { 00075 for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; 00076 } else { 00077 for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; 00078 } 00079 } else iy = (hy>>20)-1023; 00080 00081 /* set up {hx,lx}, {hy,ly} and align y to x */ 00082 if(ix >= -1022) 00083 hx = 0x00100000|(0x000fffff&hx); 00084 else { /* subnormal x, shift x to normal */ 00085 n = -1022-ix; 00086 if(n<=31) { 00087 hx = (hx<<n)|(lx>>(32-n)); 00088 lx <<= n; 00089 } else { 00090 hx = lx<<(n-32); 00091 lx = 0; 00092 } 00093 } 00094 if(iy >= -1022) 00095 hy = 0x00100000|(0x000fffff&hy); 00096 else { /* subnormal y, shift y to normal */ 00097 n = -1022-iy; 00098 if(n<=31) { 00099 hy = (hy<<n)|(ly>>(32-n)); 00100 ly <<= n; 00101 } else { 00102 hy = ly<<(n-32); 00103 ly = 0; 00104 } 00105 } 00106 00107 /* fix point fmod */ 00108 n = ix - iy; 00109 while(n--) { 00110 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 00111 if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;} 00112 else { 00113 if((hz|lz)==0) /* return sign(x)*0 */ 00114 return Zero[(uint32_t)sx>>31]; 00115 hx = hz+hz+(lz>>31); lx = lz+lz; 00116 } 00117 } 00118 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 00119 if(hz>=0) {hx=hz;lx=lz;} 00120 00121 /* convert back to floating value and restore the sign */ 00122 if((hx|lx)==0) /* return sign(x)*0 */ 00123 return Zero[(uint32_t)sx>>31]; 00124 while(hx<0x00100000) { /* normalize x */ 00125 hx = hx+hx+(lx>>31); lx = lx+lx; 00126 iy -= 1; 00127 } 00128 if(iy>= -1022) { /* normalize output */ 00129 hx = ((hx-0x00100000)|((iy+1023)<<20)); 00130 INSERT_WORDS(x,hx|sx,lx); 00131 } else { /* subnormal output */ 00132 n = -1022 - iy; 00133 if(n<=20) { 00134 lx = (lx>>n)|((uint32_t)hx<<(32-n)); 00135 hx >>= n; 00136 } else if (n<=31) { 00137 lx = (hx<<(32-n))|(lx>>n); hx = sx; 00138 } else { 00139 lx = hx>>(n-32); hx = sx; 00140 } 00141 INSERT_WORDS(x,hx|sx,lx); 00142 x *= one; /* create necessary signal */ 00143 } 00144 return x; /* exact output */ 00145 } 00146 #endif 00147