1 /* @(#)s_modf.c 5.1 93/09/24 */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
14 * modf(double x, double *iptr)
15 * return fraction part of x, and return x's integral part in *iptr.
23 #include <sys/types.h>
24 #include <machine/endian.h>
27 /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
29 #if BYTE_ORDER == BIG_ENDIAN
39 } ieee_double_shape_type;
43 #if BYTE_ORDER == LITTLE_ENDIAN
53 } ieee_double_shape_type;
57 /* Get two 32 bit ints from a double. */
59 #define EXTRACT_WORDS(ix0,ix1,d) \
61 ieee_double_shape_type ew_u; \
63 (ix0) = ew_u.parts.msw; \
64 (ix1) = ew_u.parts.lsw; \
67 /* Get the more significant 32 bit int from a double. */
69 #define GET_HIGH_WORD(i,d) \
71 ieee_double_shape_type gh_u; \
73 (i) = gh_u.parts.msw; \
76 /* Set a double from two 32 bit ints. */
78 #define INSERT_WORDS(d,ix0,ix1) \
80 ieee_double_shape_type iw_u; \
81 iw_u.parts.msw = (ix0); \
82 iw_u.parts.lsw = (ix1); \
86 static const double one = 1.0;
89 modf(double x, double *iptr)
93 EXTRACT_WORDS(i0,i1,x);
94 j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
95 if(j0<20) { /* integer part in high x */
96 if(j0<0) { /* |x|<1 */
97 INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
100 i = (0x000fffff)>>j0;
101 if(((i0&i)|i1)==0) { /* x is integral */
104 GET_HIGH_WORD(high,x);
105 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
108 INSERT_WORDS(*iptr,i0&(~i),0);
112 } else if (j0>51) { /* no fraction part */
114 if (j0 == 0x400) { /* inf/NaN */
119 GET_HIGH_WORD(high,x);
120 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
122 } else { /* fraction part in low x */
123 i = ((u_int32_t)(0xffffffff))>>(j0-20);
124 if((i1&i)==0) { /* x is integral */
127 GET_HIGH_WORD(high,x);
128 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
131 INSERT_WORDS(*iptr,i0,i1&(~i));