<iso646.h>: Avoid conflicts w/ C++ keywords.
[dragonfly.git] / lib / libm / src / s_modf.c
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  * FreeBSD SVN: 165838 (2007-01-06)
13  */
14
15 /*
16  * modf(double x, double *iptr)
17  * return fraction part of x, and return x's integral part in *iptr.
18  * Method:
19  *      Bit twiddling.
20  *
21  * Exception:
22  *      No exception.
23  */
24
25 #include <math.h>
26 #include "math_private.h"
27
28 static const double one = 1.0;
29
30 double
31 modf(double x, double *iptr)
32 {
33         int32_t i0,i1,j0;
34         u_int32_t i;
35         EXTRACT_WORDS(i0,i1,x);
36         j0 = ((i0>>20)&0x7ff)-0x3ff;    /* exponent of x */
37         if(j0<20) {                     /* integer part in high x */
38             if(j0<0) {                  /* |x|<1 */
39                 INSERT_WORDS(*iptr,i0&0x80000000,0);    /* *iptr = +-0 */
40                 return x;
41             } else {
42                 i = (0x000fffff)>>j0;
43                 if(((i0&i)|i1)==0) {            /* x is integral */
44                     u_int32_t high;
45                     *iptr = x;
46                     GET_HIGH_WORD(high,x);
47                     INSERT_WORDS(x,high&0x80000000,0);  /* return +-0 */
48                     return x;
49                 } else {
50                     INSERT_WORDS(*iptr,i0&(~i),0);
51                     return x - *iptr;
52                 }
53             }
54         } else if (j0>51) {             /* no fraction part */
55             u_int32_t high;
56             if (j0 == 0x400) {          /* inf/NaN */
57                 *iptr = x;
58                 return 0.0 / x;
59             }
60             *iptr = x*one;
61             GET_HIGH_WORD(high,x);
62             INSERT_WORDS(x,high&0x80000000,0);  /* return +-0 */
63             return x;
64         } else {                        /* fraction part in low x */
65             i = ((u_int32_t)(0xffffffff))>>(j0-20);
66             if((i1&i)==0) {             /* x is integral */
67                 u_int32_t high;
68                 *iptr = x;
69                 GET_HIGH_WORD(high,x);
70                 INSERT_WORDS(x,high&0x80000000,0);      /* return +-0 */
71                 return x;
72             } else {
73                 INSERT_WORDS(*iptr,i0,i1&(~i));
74                 return x - *iptr;
75             }
76         }
77 }