2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Developed at SunPro, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
9 * ====================================================
13 * modf(double x, double *iptr)
14 * return fraction part of x, and return x's integral part in *iptr.
22 #include <sys/types.h>
23 #include <machine/endian.h>
26 /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
28 #if BYTE_ORDER == BIG_ENDIAN
38 } ieee_double_shape_type;
42 #if BYTE_ORDER == LITTLE_ENDIAN
52 } ieee_double_shape_type;
56 /* Get two 32 bit ints from a double. */
58 #define EXTRACT_WORDS(ix0,ix1,d) \
60 ieee_double_shape_type ew_u; \
62 (ix0) = ew_u.parts.msw; \
63 (ix1) = ew_u.parts.lsw; \
66 /* Get the more significant 32 bit int from a double. */
68 #define GET_HIGH_WORD(i,d) \
70 ieee_double_shape_type gh_u; \
72 (i) = gh_u.parts.msw; \
75 /* Set a double from two 32 bit ints. */
77 #define INSERT_WORDS(d,ix0,ix1) \
79 ieee_double_shape_type iw_u; \
80 iw_u.parts.msw = (ix0); \
81 iw_u.parts.lsw = (ix1); \
85 static const double one = 1.0;
88 modf(double x, double *iptr)
92 EXTRACT_WORDS(i0,i1,x);
93 j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
94 if(j0<20) { /* integer part in high x */
95 if(j0<0) { /* |x|<1 */
96 INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
100 if(((i0&i)|i1)==0) { /* x is integral */
103 GET_HIGH_WORD(high,x);
104 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
107 INSERT_WORDS(*iptr,i0&(~i),0);
111 } else if (j0>51) { /* no fraction part */
113 if (j0 == 0x400) { /* inf/NaN */
118 GET_HIGH_WORD(high,x);
119 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
121 } else { /* fraction part in low x */
122 i = ((u_int32_t)(0xffffffff))>>(j0-20);
123 if((i1&i)==0) { /* x is integral */
126 GET_HIGH_WORD(high,x);
127 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
130 INSERT_WORDS(*iptr,i0,i1&(~i));