base/math/s_modf.c

Go to the documentation of this file.
00001 #if !defined(__ppc__)
00002 /* @(#)s_modf.c 5.1 93/09/24 */
00003 /*
00004  * ====================================================
00005  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00006  *
00007  * Developed at SunPro, a Sun Microsystems, Inc. business.
00008  * Permission to use, copy, modify, and distribute this
00009  * software is freely granted, provided that this notice 
00010  * is preserved.
00011  * ====================================================
00012  */
00013 
00014 #if defined(LIBM_SCCS) && !defined(lint)
00015 static char rcsid[] = "$NetBSD: s_modf.c,v 1.8 1995/05/10 20:47:55 jtc Exp $";
00016 #endif
00017 
00018 /*
00019  * modf(double x, double *iptr) 
00020  * return fraction part of x, and return x's integral part in *iptr.
00021  * Method:
00022  *  Bit twiddling.
00023  *
00024  * Exception:
00025  *  No exception.
00026  */
00027 
00028 #include "math.h"
00029 #include "mathP.h"
00030 
00031 #ifdef __STDC__
00032 static const double one = 1.0;
00033 #else
00034 static double one = 1.0;
00035 #endif
00036 
00037 #ifdef __STDC__
00038     double modf(double x, double *iptr)
00039 #else
00040     double modf(x, iptr)
00041     double x,*iptr;
00042 #endif
00043 {
00044     int32_t i0,i1,j0;
00045     u_int32_t i;
00046     EXTRACT_WORDS(i0,i1,x);
00047     j0 = ((i0>>20)&0x7ff)-0x3ff;    /* exponent of x */
00048     if(j0<20) {         /* integer part in high x */
00049         if(j0<0) {          /* |x|<1 */
00050             INSERT_WORDS(*iptr,i0&0x80000000,0);    /* *iptr = +-0 */
00051         return x;
00052         } else {
00053         i = (0x000fffff)>>j0;
00054         if(((i0&i)|i1)==0) {        /* x is integral */
00055             u_int32_t high;
00056             *iptr = x;
00057             GET_HIGH_WORD(high,x);
00058             INSERT_WORDS(x,high&0x80000000,0);  /* return +-0 */
00059             return x;
00060         } else {
00061             INSERT_WORDS(*iptr,i0&(~i),0);
00062             return x - *iptr;
00063         }
00064         }
00065     } else if (j0>51) {     /* no fraction part */
00066         u_int32_t high;
00067         *iptr = x*one;
00068         GET_HIGH_WORD(high,x);
00069         INSERT_WORDS(x,high&0x80000000,0);  /* return +-0 */
00070         return x;
00071     } else {            /* fraction part in low x */
00072         i = ((u_int32_t)(0xffffffff))>>(j0-20);
00073         if((i1&i)==0) {         /* x is integral */
00074             u_int32_t high;
00075         *iptr = x;
00076         GET_HIGH_WORD(high,x);
00077         INSERT_WORDS(x,high&0x80000000,0);  /* return +-0 */
00078         return x;
00079         } else {
00080             INSERT_WORDS(*iptr,i0,i1&(~i));
00081         return x - *iptr;
00082         }
00083     }
00084 }
00085 #endif /* !__ppc__ */

Generated on Tue Feb 2 17:46:05 2010 for RTAI API by  doxygen 1.4.7