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