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 * ====================================================
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
17 * modf(double x, double *iptr)
18 * return fraction part of x, and return x's integral part in *iptr.
26 #include <sys/types.h>
27 #include <machine/endian.h>
30 /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
32 #if BYTE_ORDER == BIG_ENDIAN
42 } ieee_double_shape_type;
46 #if BYTE_ORDER == LITTLE_ENDIAN
56 } ieee_double_shape_type;
60 /* Get two 32 bit ints from a double. */
62 #define EXTRACT_WORDS(ix0,ix1,d) \
64 ieee_double_shape_type ew_u; \
66 (ix0) = ew_u.parts.msw; \
67 (ix1) = ew_u.parts.lsw; \
70 /* Get the more significant 32 bit int from a double. */
72 #define GET_HIGH_WORD(i,d) \
74 ieee_double_shape_type gh_u; \
76 (i) = gh_u.parts.msw; \
79 /* Set a double from two 32 bit ints. */
81 #define INSERT_WORDS(d,ix0,ix1) \
83 ieee_double_shape_type iw_u; \
84 iw_u.parts.msw = (ix0); \
85 iw_u.parts.lsw = (ix1); \
89 static const double one = 1.0;
92 modf(double x, double *iptr)
96 EXTRACT_WORDS(i0,i1,x);
97 j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */
98 if(j0<20) { /* integer part in high x */
99 if(j0<0) { /* |x|<1 */
100 INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */
103 i = (0x000fffff)>>j0;
104 if(((i0&i)|i1)==0) { /* x is integral */
107 GET_HIGH_WORD(high,x);
108 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
111 INSERT_WORDS(*iptr,i0&(~i),0);
115 } else if (j0>51) { /* no fraction part */
117 if (j0 == 0x400) { /* inf/NaN */
122 GET_HIGH_WORD(high,x);
123 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
125 } else { /* fraction part in low x */
126 i = ((u_int32_t)(0xffffffff))>>(j0-20);
127 if((i1&i)==0) { /* x is integral */
130 GET_HIGH_WORD(high,x);
131 INSERT_WORDS(x,high&0x80000000,0); /* return +-0 */
134 INSERT_WORDS(*iptr,i0,i1&(~i));