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 /*
14  * modfl(long double x, long double *iptr)
15  * return fraction part of x, and return x's integral part in *iptr.
16  * Method:
17  *	Bit twiddling.
18  *
19  * Exception:
20  *	No exception.
21  */
22 
23 #include <math.h>
24 
25 #include "math_private.h"
26 
27 static const long double one = 1.0;
28 
29 long double
30 modfl(long double x, long double *iptr)
31 {
32 	int32_t i0,i1,jj0;
33 	u_int32_t i,se;
34 	GET_LDOUBLE_WORDS(se,i0,i1,x);
35 	jj0 = (se&0x7fff)-0x3fff;	/* exponent of x */
36 	if(jj0<32) {			/* integer part in high x */
37 	    if(jj0<0) {			/* |x|<1 */
38 		SET_LDOUBLE_WORDS(*iptr,se&0x8000,0,0);	/* *iptr = +-0 */
39 		return x;
40 	    } else {
41 		i = (0x7fffffff)>>jj0;
42 		if(((i0&i)|i1)==0) {		/* x is integral */
43 		    *iptr = x;
44 		    SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
45 		    return x;
46 		} else {
47 		    SET_LDOUBLE_WORDS(*iptr,se,i0&(~i),0);
48 		    return x - *iptr;
49 		}
50 	    }
51 	} else if (jj0>63) {		/* no fraction part */
52 	    *iptr = x*one;
53 	    /* We must handle NaNs separately.  */
54 	    if (jj0 == 0x4000 && ((i0 & 0x7fffffff) | i1))
55 	      return x*one;
56 	    SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
57 	    return x;
58 	} else {			/* fraction part in low x */
59 	    i = ((u_int32_t)(0x7fffffff))>>(jj0-32);
60 	    if((i1&i)==0) {		/* x is integral */
61 		*iptr = x;
62 		SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
63 		return x;
64 	    } else {
65 		SET_LDOUBLE_WORDS(*iptr,se,i0,i1&(~i));
66 		return x - *iptr;
67 	    }
68 	}
69 }
70