Back to index

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