xref: /freebsd/lib/libc/gen/modf.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1*6232589aSDavid Schultz /*
2*6232589aSDavid Schultz  * ====================================================
3*6232589aSDavid Schultz  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4*6232589aSDavid Schultz  *
5*6232589aSDavid Schultz  * Developed at SunPro, a Sun Microsystems, Inc. business.
6*6232589aSDavid Schultz  * Permission to use, copy, modify, and distribute this
7*6232589aSDavid Schultz  * software is freely granted, provided that this notice
8*6232589aSDavid Schultz  * is preserved.
9*6232589aSDavid Schultz  * ====================================================
10*6232589aSDavid Schultz  */
11*6232589aSDavid Schultz 
12*6232589aSDavid Schultz /*
13*6232589aSDavid Schultz  * modf(double x, double *iptr)
14*6232589aSDavid Schultz  * return fraction part of x, and return x's integral part in *iptr.
15*6232589aSDavid Schultz  * Method:
16*6232589aSDavid Schultz  *	Bit twiddling.
17*6232589aSDavid Schultz  *
18*6232589aSDavid Schultz  * Exception:
19*6232589aSDavid Schultz  *	No exception.
20*6232589aSDavid Schultz  */
21*6232589aSDavid Schultz 
22*6232589aSDavid Schultz #include <sys/types.h>
23*6232589aSDavid Schultz #include <machine/endian.h>
24*6232589aSDavid Schultz #include <math.h>
25*6232589aSDavid Schultz 
26*6232589aSDavid Schultz /* Bit fiddling routines copied from msun/src/math_private.h,v 1.15 */
27*6232589aSDavid Schultz 
28*6232589aSDavid Schultz #if BYTE_ORDER == BIG_ENDIAN
29*6232589aSDavid Schultz 
30*6232589aSDavid Schultz typedef union
31*6232589aSDavid Schultz {
32*6232589aSDavid Schultz   double value;
33*6232589aSDavid Schultz   struct
34*6232589aSDavid Schultz   {
35*6232589aSDavid Schultz     u_int32_t msw;
36*6232589aSDavid Schultz     u_int32_t lsw;
37*6232589aSDavid Schultz   } parts;
38*6232589aSDavid Schultz } ieee_double_shape_type;
39*6232589aSDavid Schultz 
40*6232589aSDavid Schultz #endif
41*6232589aSDavid Schultz 
42*6232589aSDavid Schultz #if BYTE_ORDER == LITTLE_ENDIAN
43*6232589aSDavid Schultz 
44*6232589aSDavid Schultz typedef union
45*6232589aSDavid Schultz {
46*6232589aSDavid Schultz   double value;
47*6232589aSDavid Schultz   struct
48*6232589aSDavid Schultz   {
49*6232589aSDavid Schultz     u_int32_t lsw;
50*6232589aSDavid Schultz     u_int32_t msw;
51*6232589aSDavid Schultz   } parts;
52*6232589aSDavid Schultz } ieee_double_shape_type;
53*6232589aSDavid Schultz 
54*6232589aSDavid Schultz #endif
55*6232589aSDavid Schultz 
56*6232589aSDavid Schultz /* Get two 32 bit ints from a double.  */
57*6232589aSDavid Schultz 
58*6232589aSDavid Schultz #define EXTRACT_WORDS(ix0,ix1,d)				\
59*6232589aSDavid Schultz do {								\
60*6232589aSDavid Schultz   ieee_double_shape_type ew_u;					\
61*6232589aSDavid Schultz   ew_u.value = (d);						\
62*6232589aSDavid Schultz   (ix0) = ew_u.parts.msw;					\
63*6232589aSDavid Schultz   (ix1) = ew_u.parts.lsw;					\
64*6232589aSDavid Schultz } while (0)
65*6232589aSDavid Schultz 
66*6232589aSDavid Schultz /* Get the more significant 32 bit int from a double.  */
67*6232589aSDavid Schultz 
68*6232589aSDavid Schultz #define GET_HIGH_WORD(i,d)					\
69*6232589aSDavid Schultz do {								\
70*6232589aSDavid Schultz   ieee_double_shape_type gh_u;					\
71*6232589aSDavid Schultz   gh_u.value = (d);						\
72*6232589aSDavid Schultz   (i) = gh_u.parts.msw;						\
73*6232589aSDavid Schultz } while (0)
74*6232589aSDavid Schultz 
75*6232589aSDavid Schultz /* Set a double from two 32 bit ints.  */
76*6232589aSDavid Schultz 
77*6232589aSDavid Schultz #define INSERT_WORDS(d,ix0,ix1)					\
78*6232589aSDavid Schultz do {								\
79*6232589aSDavid Schultz   ieee_double_shape_type iw_u;					\
80*6232589aSDavid Schultz   iw_u.parts.msw = (ix0);					\
81*6232589aSDavid Schultz   iw_u.parts.lsw = (ix1);					\
82*6232589aSDavid Schultz   (d) = iw_u.value;						\
83*6232589aSDavid Schultz } while (0)
84*6232589aSDavid Schultz 
85*6232589aSDavid Schultz static const double one = 1.0;
86*6232589aSDavid Schultz 
87*6232589aSDavid Schultz double
modf(double x,double * iptr)88*6232589aSDavid Schultz modf(double x, double *iptr)
89*6232589aSDavid Schultz {
90*6232589aSDavid Schultz 	int32_t i0,i1,j0;
91*6232589aSDavid Schultz 	u_int32_t i;
92*6232589aSDavid Schultz 	EXTRACT_WORDS(i0,i1,x);
93*6232589aSDavid Schultz 	j0 = ((i0>>20)&0x7ff)-0x3ff;	/* exponent of x */
94*6232589aSDavid Schultz 	if(j0<20) {			/* integer part in high x */
95*6232589aSDavid Schultz 	    if(j0<0) {			/* |x|<1 */
96*6232589aSDavid Schultz 	        INSERT_WORDS(*iptr,i0&0x80000000,0);	/* *iptr = +-0 */
97*6232589aSDavid Schultz 		return x;
98*6232589aSDavid Schultz 	    } else {
99*6232589aSDavid Schultz 		i = (0x000fffff)>>j0;
100*6232589aSDavid Schultz 		if(((i0&i)|i1)==0) {		/* x is integral */
101*6232589aSDavid Schultz 		    u_int32_t high;
102*6232589aSDavid Schultz 		    *iptr = x;
103*6232589aSDavid Schultz 		    GET_HIGH_WORD(high,x);
104*6232589aSDavid Schultz 		    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
105*6232589aSDavid Schultz 		    return x;
106*6232589aSDavid Schultz 		} else {
107*6232589aSDavid Schultz 		    INSERT_WORDS(*iptr,i0&(~i),0);
108*6232589aSDavid Schultz 		    return x - *iptr;
109*6232589aSDavid Schultz 		}
110*6232589aSDavid Schultz 	    }
111*6232589aSDavid Schultz 	} else if (j0>51) {		/* no fraction part */
112*6232589aSDavid Schultz 	    u_int32_t high;
113*6232589aSDavid Schultz 	    if (j0 == 0x400) {		/* inf/NaN */
114*6232589aSDavid Schultz 		*iptr = x;
115*6232589aSDavid Schultz 		return 0.0 / x;
116*6232589aSDavid Schultz 	    }
117*6232589aSDavid Schultz 	    *iptr = x*one;
118*6232589aSDavid Schultz 	    GET_HIGH_WORD(high,x);
119*6232589aSDavid Schultz 	    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
120*6232589aSDavid Schultz 	    return x;
121*6232589aSDavid Schultz 	} else {			/* fraction part in low x */
122*6232589aSDavid Schultz 	    i = ((u_int32_t)(0xffffffff))>>(j0-20);
123*6232589aSDavid Schultz 	    if((i1&i)==0) { 		/* x is integral */
124*6232589aSDavid Schultz 	        u_int32_t high;
125*6232589aSDavid Schultz 		*iptr = x;
126*6232589aSDavid Schultz 		GET_HIGH_WORD(high,x);
127*6232589aSDavid Schultz 		INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
128*6232589aSDavid Schultz 		return x;
129*6232589aSDavid Schultz 	    } else {
130*6232589aSDavid Schultz 	        INSERT_WORDS(*iptr,i0,i1&(~i));
131*6232589aSDavid Schultz 		return x - *iptr;
132*6232589aSDavid Schultz 	    }
133*6232589aSDavid Schultz 	}
134*6232589aSDavid Schultz }
135