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 
24 
25 static const long double one = 1.0L;
26 
27 long double
modfl(long double x,long double * iptr)28 modfl(long double x, long double *iptr)
29 {
30 	int32_t i0,i1,jj0;
31 	u_int32_t i,se;
32 	GET_LDOUBLE_WORDS(se,i0,i1,x);
33 	jj0 = (se&0x7fff)-0x3fff;	/* exponent of x */
34 	if(jj0<32) {			/* integer part in high x */
35 	    if(jj0<0) {			/* |x|<1 */
36 		SET_LDOUBLE_WORDS(*iptr,se&0x8000,0,0);	/* *iptr = +-0 */
37 		return x;
38 	    } else {
39 		i = (0x7fffffff)>>jj0;
40 		if(((i0&i)|i1)==0) {		/* x is integral */
41 		    *iptr = x;
42 		    SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
43 		    return x;
44 		} else {
45 		    SET_LDOUBLE_WORDS(*iptr,se,i0&(~i),0);
46 		    return x - *iptr;
47 		}
48 	    }
49 	} else if (jj0>63) {		/* no fraction part */
50 	    *iptr = x*one;
51 	    /* We must handle NaNs separately.  */
52 	    if (jj0 == 0x4000 && ((i0 & 0x7fffffff) | i1))
53 	      return x*one;
54 	    SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
55 	    return x;
56 	} else {			/* fraction part in low x */
57 	    i = ((u_int32_t)(0x7fffffff))>>(jj0-32);
58 	    if((i1&i)==0) {		/* x is integral */
59 		*iptr = x;
60 		SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
61 		return x;
62 	    } else {
63 		SET_LDOUBLE_WORDS(*iptr,se,i0,i1&(~i));
64 		return x - *iptr;
65 	    }
66 	}
67 }
68