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