1 /* @(#)s_modf.c 5.1 93/09/24 */
2 /* $FreeBSD: head/lib/libc/gen/modf.c 226606 2011-10-21 06:40:36Z das $ */
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
11 * ====================================================
15 * modf(double x, double *iptr)
16 * return fraction part of x, and return x's integral part in *iptr.
24 #include <sys/types.h>
25 #include <machine/endian.h>
28 /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
30 #if BYTE_ORDER == BIG_ENDIAN
40 } ieee_double_shape_type
;
44 #if BYTE_ORDER == LITTLE_ENDIAN
54 } ieee_double_shape_type
;
58 /* Get two 32 bit ints from a double. */
60 #define EXTRACT_WORDS(ix0,ix1,d) \
62 ieee_double_shape_type ew_u; \
64 (ix0) = ew_u.parts.msw; \
65 (ix1) = ew_u.parts.lsw; \
68 /* Get the more significant 32 bit int from a double. */
70 #define GET_HIGH_WORD(i,d) \
72 ieee_double_shape_type gh_u; \
74 (i) = gh_u.parts.msw; \
77 /* Set a double from two 32 bit ints. */
79 #define INSERT_WORDS(d,ix0,ix1) \
81 ieee_double_shape_type iw_u; \
82 iw_u.parts.msw = (ix0); \
83 iw_u.parts.lsw = (ix1); \
87 static const double one
= 1.0;
90 modf(double x
, double *iptr
)
94 EXTRACT_WORDS(i0
,i1
,x
);
95 j0
= ((i0
>>20)&0x7ff)-0x3ff; /* exponent of x */
96 if(j0
<20) { /* integer part in high x */
97 if(j0
<0) { /* |x|<1 */
98 INSERT_WORDS(*iptr
,i0
&0x80000000,0); /* *iptr = +-0 */
101 i
= (0x000fffff)>>j0
;
102 if(((i0
&i
)|i1
)==0) { /* x is integral */
105 GET_HIGH_WORD(high
,x
);
106 INSERT_WORDS(x
,high
&0x80000000,0); /* return +-0 */
109 INSERT_WORDS(*iptr
,i0
&(~i
),0);
113 } else if (j0
>51) { /* no fraction part */
115 if (j0
== 0x400) { /* inf/NaN */
120 GET_HIGH_WORD(high
,x
);
121 INSERT_WORDS(x
,high
&0x80000000,0); /* return +-0 */
123 } else { /* fraction part in low x */
124 i
= ((u_int32_t
)(0xffffffff))>>(j0
-20);
125 if((i1
&i
)==0) { /* x is integral */
128 GET_HIGH_WORD(high
,x
);
129 INSERT_WORDS(x
,high
&0x80000000,0); /* return +-0 */
132 INSERT_WORDS(*iptr
,i0
,i1
&(~i
));