update for HEAD-2003021201
[reactos.git] / lib / crtdll / math / modf.c
1 /* @(#)s_modf.c 1.3 95/01/18 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunSoft, 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 #include <msvcrt/float.h>
14 #include <msvcrt/math.h>
15 #include <msvcrt/internal/ieee.h>
16
17
18
19 //static const double one = 1.0;
20
21 double modf(double __x, double *__i)
22 {
23         double_t * x = (double_t *)&__x;
24         double_t * iptr = ( double_t *)__i;
25
26         int j0;
27         unsigned int i;
28         j0 = x->exponent - 0x3ff;  /* exponent of x */  
29         if(j0<20) {                     /* integer part in high x */
30                 if(j0<0) {                  /* |x|<1 */
31                         *__i = 0.0;
32                         iptr->sign = x->sign;
33                         return __x;
34                 } else {
35
36                         if ( x->mantissah == 0 && x->mantissal == 0 ) {
37                                 *__i = __x;
38                                 return 0.0;
39                         }
40
41                         i = (0x000fffff)>>j0;
42                         iptr->sign = x->sign;
43                         iptr->exponent = x->exponent;
44                         iptr->mantissah = x->mantissah&(~i);
45                         iptr->mantissal = 0;
46                         if ( __x == *__i ) {
47                                 __x = 0.0;
48                                 x->sign = iptr->sign;
49                                 return __x;
50                         }
51                         return __x - *__i;
52                 }
53         } else if (j0>51) {             /* no fraction part */
54                 *__i = __x;
55                 if ( _isnan(__x) || _isinf(__x) )
56                         return __x;
57
58                 __x = 0.0;
59                 x->sign = iptr->sign;
60                 return __x;
61         } else {                        /* fraction part in low x */
62
63                 i = ((unsigned)(0xffffffff))>>(j0-20);
64                 iptr->sign = x->sign;
65                 iptr->exponent = x->exponent;
66                 iptr->mantissah = x->mantissah;
67                 iptr->mantissal = x->mantissal&(~i);
68                 if ( __x == *__i ) {
69                         __x = 0.0;
70                         x->sign = iptr->sign;
71                         return __x;
72                 }
73                 return __x - *__i;
74         }
75 }
76
77
78 long double modfl(long double __x, long double *__i)
79 {
80         long_double_t * x = (long_double_t *)&__x;
81         long_double_t * iptr = (long_double_t *)__i;
82
83         int j0;
84         unsigned int i;
85         j0 = x->exponent - 0x3fff;  /* exponent of x */
86
87         if(j0<32) {                     /* integer part in high x */
88                 if(j0<0) {                  /* |x|<1 */
89                         *__i = 0.0L;
90                         iptr->sign = x->sign;
91                         return __x;
92                 } else {
93
94                         i = ((unsigned int)(0xffffffff))>>(j0+1);
95                         if ( x->mantissal == 0 && (x->mantissal & i) == 0 ) {
96                                 *__i =  __x;
97                                 __x = 0.0L;
98                                 x->sign = iptr->sign;
99                                 return __x;
100                         }
101                         iptr->sign = x->sign;
102                         iptr->exponent = x->exponent;
103                         iptr->mantissah = x->mantissah&((~i));
104                         iptr->mantissal = 0;
105                 
106                         return __x - *__i;
107                 }
108         } else if (j0>63) {             /* no fraction part */
109                 *__i = __x;
110                 if (  _isnanl(__x) ||  _isinfl(__x)  )
111                         return __x;
112
113                 __x = 0.0L;
114                 x->sign = iptr->sign;
115                 return __x;
116         } else {                        /* fraction part in low x */
117
118                 i = ((unsigned int)(0xffffffff))>>(j0-32);
119                 if ( x->mantissal == 0 ) {
120                         *__i =  __x;
121                         __x = 0.0L;
122                         x->sign = iptr->sign;
123                         return __x;
124                 }
125                 iptr->sign = x->sign;
126                 iptr->exponent = x->exponent;
127                 iptr->mantissah = x->mantissah;
128                 iptr->mantissal = x->mantissal&(~i);
129         
130                 return __x - *__i;
131         }
132 }