xref: /freebsd/lib/msun/src/s_modf.c (revision 2dcc228679cb39677331f1064bedb9eac825a52f)
1 /* @(#)s_modf.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
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
9  * is preserved.
10  * ====================================================
11  */
12 
13 #ifndef lint
14 static char rcsid[] = "$FreeBSD$";
15 #endif
16 
17 /*
18  * modf(double x, double *iptr)
19  * return fraction part of x, and return x's integral part in *iptr.
20  * Method:
21  *	Bit twiddling.
22  *
23  * Exception:
24  *	No exception.
25  */
26 
27 #include "math.h"
28 #include "math_private.h"
29 
30 static const double one = 1.0;
31 
32 	double modf(double x, double *iptr)
33 {
34 	int32_t i0,i1,j0;
35 	u_int32_t i;
36 	EXTRACT_WORDS(i0,i1,x);
37 	j0 = ((i0>>20)&0x7ff)-0x3ff;	/* exponent of x */
38 	if(j0<20) {			/* integer part in high x */
39 	    if(j0<0) {			/* |x|<1 */
40 	        INSERT_WORDS(*iptr,i0&0x80000000,0);	/* *iptr = +-0 */
41 		return x;
42 	    } else {
43 		i = (0x000fffff)>>j0;
44 		if(((i0&i)|i1)==0) {		/* x is integral */
45 		    u_int32_t high;
46 		    *iptr = x;
47 		    GET_HIGH_WORD(high,x);
48 		    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
49 		    return x;
50 		} else {
51 		    INSERT_WORDS(*iptr,i0&(~i),0);
52 		    return x - *iptr;
53 		}
54 	    }
55 	} else if (j0>51) {		/* no fraction part */
56 	    u_int32_t high;
57 	    *iptr = x*one;
58 	    GET_HIGH_WORD(high,x);
59 	    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
60 	    return x;
61 	} else {			/* fraction part in low x */
62 	    i = ((u_int32_t)(0xffffffff))>>(j0-20);
63 	    if((i1&i)==0) { 		/* x is integral */
64 	        u_int32_t high;
65 		*iptr = x;
66 		GET_HIGH_WORD(high,x);
67 		INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
68 		return x;
69 	    } else {
70 	        INSERT_WORDS(*iptr,i0,i1&(~i));
71 		return x - *iptr;
72 	    }
73 	}
74 }
75