xref: /openbsd-src/lib/libm/src/ld128/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 	int64_t i0,i1,jj0;
33*49393c00Smartynas 	u_int64_t i;
34*49393c00Smartynas 	GET_LDOUBLE_WORDS64(i0,i1,x);
35*49393c00Smartynas 	jj0 = ((i0>>48)&0x7fff)-0x3fff;	/* exponent of x */
36*49393c00Smartynas 	if(jj0<48) {			/* integer part in high x */
37*49393c00Smartynas 	    if(jj0<0) {			/* |x|<1 */
38*49393c00Smartynas 		/* *iptr = +-0 */
39*49393c00Smartynas 		SET_LDOUBLE_WORDS64(*iptr,i0&0x8000000000000000ULL,0);
40*49393c00Smartynas 		return x;
41*49393c00Smartynas 	    } else {
42*49393c00Smartynas 		i = (0x0000ffffffffffffLL)>>jj0;
43*49393c00Smartynas 		if(((i0&i)|i1)==0) {		/* x is integral */
44*49393c00Smartynas 		    *iptr = x;
45*49393c00Smartynas 		    /* return +-0 */
46*49393c00Smartynas 		    SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0);
47*49393c00Smartynas 		    return x;
48*49393c00Smartynas 		} else {
49*49393c00Smartynas 		    SET_LDOUBLE_WORDS64(*iptr,i0&(~i),0);
50*49393c00Smartynas 		    return x - *iptr;
51*49393c00Smartynas 		}
52*49393c00Smartynas 	    }
53*49393c00Smartynas 	} else if (jj0>111) {		/* no fraction part */
54*49393c00Smartynas 	    *iptr = x*one;
55*49393c00Smartynas 	    /* We must handle NaNs separately.  */
56*49393c00Smartynas 	    if (jj0 == 0x4000 && ((i0 & 0x0000ffffffffffffLL) | i1))
57*49393c00Smartynas 	      return x*one;
58*49393c00Smartynas 	    /* return +-0 */
59*49393c00Smartynas 	    SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0);
60*49393c00Smartynas 	    return x;
61*49393c00Smartynas 	} else {			/* fraction part in low x */
62*49393c00Smartynas 	    i = -1ULL>>(jj0-48);
63*49393c00Smartynas 	    if((i1&i)==0) {		/* x is integral */
64*49393c00Smartynas 		*iptr = x;
65*49393c00Smartynas 		/* return +-0 */
66*49393c00Smartynas 		SET_LDOUBLE_WORDS64(x,i0&0x8000000000000000ULL,0);
67*49393c00Smartynas 		return x;
68*49393c00Smartynas 	    } else {
69*49393c00Smartynas 		SET_LDOUBLE_WORDS64(*iptr,i0,i1&(~i));
70*49393c00Smartynas 		return x - *iptr;
71*49393c00Smartynas 	    }
72*49393c00Smartynas 	}
73*49393c00Smartynas }
74