xref: /openbsd-src/lib/libm/src/ld80/s_modfl.c (revision 49393c004c040ee201e6408db68882c3fe4cb110)
1*49393c00Smartynas /* @(#)s_modf.c 5.1 93/09/24 */
2*49393c00Smartynas /*
3*49393c00Smartynas  * ====================================================
4*49393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*49393c00Smartynas  *
6*49393c00Smartynas  * Developed at SunPro, a Sun Microsystems, Inc. business.
7*49393c00Smartynas  * Permission to use, copy, modify, and distribute this
8*49393c00Smartynas  * software is freely granted, provided that this notice
9*49393c00Smartynas  * is preserved.
10*49393c00Smartynas  * ====================================================
11*49393c00Smartynas  */
12*49393c00Smartynas 
13*49393c00Smartynas /*
14*49393c00Smartynas  * modfl(long double x, long double *iptr)
15*49393c00Smartynas  * return fraction part of x, and return x's integral part in *iptr.
16*49393c00Smartynas  * Method:
17*49393c00Smartynas  *	Bit twiddling.
18*49393c00Smartynas  *
19*49393c00Smartynas  * Exception:
20*49393c00Smartynas  *	No exception.
21*49393c00Smartynas  */
22*49393c00Smartynas 
23*49393c00Smartynas #include <math.h>
24*49393c00Smartynas 
25*49393c00Smartynas #include "math_private.h"
26*49393c00Smartynas 
27*49393c00Smartynas static const long double one = 1.0;
28*49393c00Smartynas 
29*49393c00Smartynas long double
modfl(long double x,long double * iptr)30*49393c00Smartynas modfl(long double x, long double *iptr)
31*49393c00Smartynas {
32*49393c00Smartynas 	int32_t i0,i1,jj0;
33*49393c00Smartynas 	u_int32_t i,se;
34*49393c00Smartynas 	GET_LDOUBLE_WORDS(se,i0,i1,x);
35*49393c00Smartynas 	jj0 = (se&0x7fff)-0x3fff;	/* exponent of x */
36*49393c00Smartynas 	if(jj0<32) {			/* integer part in high x */
37*49393c00Smartynas 	    if(jj0<0) {			/* |x|<1 */
38*49393c00Smartynas 		SET_LDOUBLE_WORDS(*iptr,se&0x8000,0,0);	/* *iptr = +-0 */
39*49393c00Smartynas 		return x;
40*49393c00Smartynas 	    } else {
41*49393c00Smartynas 		i = (0x7fffffff)>>jj0;
42*49393c00Smartynas 		if(((i0&i)|i1)==0) {		/* x is integral */
43*49393c00Smartynas 		    *iptr = x;
44*49393c00Smartynas 		    SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
45*49393c00Smartynas 		    return x;
46*49393c00Smartynas 		} else {
47*49393c00Smartynas 		    SET_LDOUBLE_WORDS(*iptr,se,i0&(~i),0);
48*49393c00Smartynas 		    return x - *iptr;
49*49393c00Smartynas 		}
50*49393c00Smartynas 	    }
51*49393c00Smartynas 	} else if (jj0>63) {		/* no fraction part */
52*49393c00Smartynas 	    *iptr = x*one;
53*49393c00Smartynas 	    /* We must handle NaNs separately.  */
54*49393c00Smartynas 	    if (jj0 == 0x4000 && ((i0 & 0x7fffffff) | i1))
55*49393c00Smartynas 	      return x*one;
56*49393c00Smartynas 	    SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
57*49393c00Smartynas 	    return x;
58*49393c00Smartynas 	} else {			/* fraction part in low x */
59*49393c00Smartynas 	    i = ((u_int32_t)(0x7fffffff))>>(jj0-32);
60*49393c00Smartynas 	    if((i1&i)==0) {		/* x is integral */
61*49393c00Smartynas 		*iptr = x;
62*49393c00Smartynas 		SET_LDOUBLE_WORDS(x,se&0x8000,0,0);	/* return +-0 */
63*49393c00Smartynas 		return x;
64*49393c00Smartynas 	    } else {
65*49393c00Smartynas 		SET_LDOUBLE_WORDS(*iptr,se,i0,i1&(~i));
66*49393c00Smartynas 		return x - *iptr;
67*49393c00Smartynas 	    }
68*49393c00Smartynas 	}
69*49393c00Smartynas }
70