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