ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

s_modf.c
Go to the documentation of this file.
00001 
00002 
00003 /* @(#)s_modf.c 5.1 93/09/24 */
00004 /*
00005  * ====================================================
00006  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00007  *
00008  * Developed at SunPro, a Sun Microsystems, Inc. business.
00009  * Permission to use, copy, modify, and distribute this
00010  * software is freely granted, provided that this notice
00011  * is preserved.
00012  * ====================================================
00013  */
00014 
00015 /*
00016 FUNCTION
00017        <<modf>>, <<modff>>---split fractional and integer parts
00018 
00019 INDEX
00020     modf
00021 INDEX
00022     modff
00023 
00024 ANSI_SYNOPSIS
00025     #include <math.h>
00026     double modf(double <[val]>, double *<[ipart]>);
00027         float modff(float <[val]>, float *<[ipart]>);
00028 
00029 TRAD_SYNOPSIS
00030     #include <math.h>
00031     double modf(<[val]>, <[ipart]>)
00032         double <[val]>;
00033         double *<[ipart]>;
00034 
00035     float modff(<[val]>, <[ipart]>)
00036     float <[val]>;
00037         float *<[ipart]>;
00038 
00039 DESCRIPTION
00040     <<modf>> splits the double <[val]> apart into an integer part
00041     and a fractional part, returning the fractional part and
00042     storing the integer part in <<*<[ipart]>>>.  No rounding
00043     whatsoever is done; the sum of the integer and fractional
00044     parts is guaranteed to be exactly  equal to <[val]>.   That
00045     is, if . <[realpart]> = modf(<[val]>, &<[intpart]>); then
00046     `<<<[realpart]>+<[intpart]>>>' is the same as <[val]>.
00047     <<modff>> is identical, save that it takes and returns
00048     <<float>> rather than <<double>> values.
00049 
00050 RETURNS
00051     The fractional part is returned.  Each result has the same
00052     sign as the supplied argument <[val]>.
00053 
00054 PORTABILITY
00055     <<modf>> is ANSI C. <<modff>> is an extension.
00056 
00057 QUICKREF
00058     modf  ansi pure
00059     modff - pure
00060 
00061 */
00062 
00063 /*
00064  * modf(double x, double *iptr)
00065  * return fraction part of x, and return x's integral part in *iptr.
00066  * Method:
00067  *  Bit twiddling.
00068  *
00069  * Exception:
00070  *  No exception.
00071  */
00072 
00073 
00074 static const double one = 1.0;
00075 
00076 #define __int32_t long
00077 #define __uint32_t unsigned long
00078 #define __IEEE_LITTLE_ENDIAN
00079 
00080 #ifdef __IEEE_BIG_ENDIAN
00081 
00082 typedef union
00083 {
00084   struct
00085   {
00086     __uint32_t msw;
00087     __uint32_t lsw;
00088   } parts;
00089   double value;
00090 } ieee_double_shape_type;
00091 
00092 #endif
00093 
00094 #ifdef __IEEE_LITTLE_ENDIAN
00095 
00096 typedef union
00097 {
00098   struct
00099   {
00100     __uint32_t lsw;
00101     __uint32_t msw;
00102   } parts;
00103   double value;
00104 } ieee_double_shape_type;
00105 
00106 #endif
00107 
00108 
00109 /* Get two 32 bit ints from a double.  */
00110 
00111 #define EXTRACT_WORDS(ix0,ix1,d)                \
00112 do {                                \
00113   ieee_double_shape_type ew_u;                  \
00114   ew_u.value = (d);                     \
00115   (ix0) = ew_u.parts.msw;                   \
00116   (ix1) = ew_u.parts.lsw;                   \
00117 } while (0)
00118 
00119 /* Get the more significant 32 bit int from a double.  */
00120 
00121 #define GET_HIGH_WORD(i,d)                  \
00122 do {                                \
00123   ieee_double_shape_type gh_u;                  \
00124   gh_u.value = (d);                     \
00125   (i) = gh_u.parts.msw;                     \
00126 } while (0)
00127 
00128 /* Get the less significant 32 bit int from a double.  */
00129 
00130 #define GET_LOW_WORD(i,d)                   \
00131 do {                                \
00132   ieee_double_shape_type gl_u;                  \
00133   gl_u.value = (d);                     \
00134   (i) = gl_u.parts.lsw;                     \
00135 } while (0)
00136 
00137 /* Set a double from two 32 bit ints.  */
00138 
00139 #define INSERT_WORDS(d,ix0,ix1)                 \
00140 do {                                \
00141   ieee_double_shape_type iw_u;                  \
00142   iw_u.parts.msw = (ix0);                   \
00143   iw_u.parts.lsw = (ix1);                   \
00144   (d) = iw_u.value;                     \
00145 } while (0)
00146 
00147 
00148 
00149 
00150 double modf(double x, double *iptr)
00151 {
00152     __int32_t i0,i1,j_0;
00153     __uint32_t i;
00154     EXTRACT_WORDS(i0,i1,x);
00155     j_0 = ((i0>>20)&0x7ff)-0x3ff;   /* exponent of x */
00156     if(j_0<20) {            /* integer part in high x */
00157         if(j_0<0) {         /* |x|<1 */
00158             INSERT_WORDS(*iptr,i0&0x80000000U,0);   /* *iptr = +-0 */
00159         return x;
00160         } else {
00161         i = (0x000fffff)>>j_0;
00162         if(((i0&i)|i1)==0) {        /* x is integral */
00163             __uint32_t high;
00164             *iptr = x;
00165             GET_HIGH_WORD(high,x);
00166             INSERT_WORDS(x,high&0x80000000U,0); /* return +-0 */
00167             return x;
00168         } else {
00169             INSERT_WORDS(*iptr,i0&(~i),0);
00170             return x - *iptr;
00171         }
00172         }
00173     } else if (j_0>51) {        /* no fraction part */
00174         __uint32_t high;
00175         *iptr = x*one;
00176         GET_HIGH_WORD(high,x);
00177         INSERT_WORDS(x,high&0x80000000U,0); /* return +-0 */
00178         return x;
00179     } else {            /* fraction part in low x */
00180         i = ((__uint32_t)(0xffffffffU))>>(j_0-20);
00181         if((i1&i)==0) {         /* x is integral */
00182             __uint32_t high;
00183         *iptr = x;
00184         GET_HIGH_WORD(high,x);
00185         INSERT_WORDS(x,high&0x80000000U,0); /* return +-0 */
00186         return x;
00187         } else {
00188             INSERT_WORDS(*iptr,i0,i1&(~i));
00189         return x - *iptr;
00190         }
00191     }
00192 }
00193 
00194 //#endif /* _DOUBLE_IS_32BITS */

Generated on Fri May 25 2012 04:34:55 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.