Increased the search buffer for InternalFindFirstFile/InternalFindNextFile.
[reactos.git] / reactos / subsys / win32k / misc / math.c
1 /* Math functions for i387.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by John C. Bowman <bowman@ipp-garching.mpg.de>, 1995.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <windows.h>
22
23 double atan (double __x);
24 double atan2 (double __y, double __x);
25 double ceil (double __x);
26 double cos (double __x);
27 double fabs (double __x);
28 double floor (double __x);
29 long _ftol (double fl);
30 double log (double __x);
31 double __log2 (double __x);
32 double pow (double __x, double __y);
33 double sin (double __x);
34 double sqrt (double __x);
35 double tan (double __x);
36
37
38 double atan (double __x)
39 {
40 register double __value;
41 __asm __volatile__
42 ("fld1\n\t"
43 "fpatan"
44 : "=t" (__value) : "0" (__x));
45
46 return __value;
47 }
48
49 double atan2 (double __y, double __x)
50 {
51 register double __value;
52 __asm __volatile__
53 ("fpatan\n\t"
54 "fld %%st(0)"
55 : "=t" (__value) : "0" (__x), "u" (__y));
56
57 return __value;
58 }
59
60 double ceil (double __x)
61 {
62 register double __value;
63 __volatile unsigned short int __cw, __cwtmp;
64
65 __asm __volatile ("fnstcw %0" : "=m" (__cw));
66 __cwtmp = (__cw & 0xf3ff) | 0x0800; /* rounding up */
67 __asm __volatile ("fldcw %0" : : "m" (__cwtmp));
68 __asm __volatile ("frndint" : "=t" (__value) : "0" (__x));
69 __asm __volatile ("fldcw %0" : : "m" (__cw));
70
71 return __value;
72 }
73
74 double cos (double __x)
75 {
76 register double __value;
77 __asm __volatile__
78 ("fcos"
79 : "=t" (__value): "0" (__x));
80
81 return __value;
82 }
83
84 double fabs (double __x)
85 {
86 register double __value;
87 __asm __volatile__
88 ("fabs"
89 : "=t" (__value) : "0" (__x));
90
91 return __value;
92 }
93
94 double floor (double __x)
95 {
96 register double __value;
97 __volatile unsigned short int __cw, __cwtmp;
98
99 __asm __volatile ("fnstcw %0" : "=m" (__cw));
100 __cwtmp = (__cw & 0xf3ff) | 0x0400; /* rounding down */
101 __asm __volatile ("fldcw %0" : : "m" (__cwtmp));
102 __asm __volatile ("frndint" : "=t" (__value) : "0" (__x));
103 __asm __volatile ("fldcw %0" : : "m" (__cw));
104
105 return __value;
106 }
107
108 long _ftol (double fl)
109 {
110 return (long)fl;
111 }
112
113 double log (double __x)
114 {
115 register double __value;
116 __asm __volatile__
117 ("fldln2\n\t"
118 "fxch\n\t"
119 "fyl2x"
120 : "=t" (__value) : "0" (__x));
121
122 return __value;
123 }
124
125 double __log2 (double __x)
126 {
127 register double __value;
128 __asm __volatile__
129 ("fld1\n\t"
130 "fxch\n\t"
131 "fyl2x"
132 : "=t" (__value) : "0" (__x));
133
134 return __value;
135 }
136
137 double pow (double __x, double __y)
138 {
139 register double __value, __exponent;
140 long __p = (long) __y;
141
142 if (__x == 0.0 && __y > 0.0)
143 return 0.0;
144 if (__y == (double) __p)
145 {
146 double __r = 1.0;
147 if (__p == 0)
148 return 1.0;
149 if (__p < 0)
150 {
151 __p = -__p;
152 __x = 1.0 / __x;
153 }
154 while (1)
155 {
156 if (__p & 1)
157 __r *= __x;
158 __p >>= 1;
159 if (__p == 0)
160 return __r;
161 __x *= __x;
162 }
163 /* NOTREACHED */
164 }
165 __asm __volatile__
166 ("fmul %%st(1) # y * log2(x)\n\t"
167 "fst %%st(1)\n\t"
168 "frndint # int(y * log2(x))\n\t"
169 "fxch\n\t"
170 "fsub %%st(1) # fract(y * log2(x))\n\t"
171 "f2xm1 # 2^(fract(y * log2(x))) - 1\n\t"
172 : "=t" (__value), "=u" (__exponent) : "0" (__log2 (__x)), "1" (__y));
173 __value += 1.0;
174 __asm __volatile__
175 ("fscale"
176 : "=t" (__value) : "0" (__value), "u" (__exponent));
177
178 return __value;
179 }
180
181 double sin (double __x)
182 {
183 register double __value;
184 __asm __volatile__
185 ("fsin"
186 : "=t" (__value) : "0" (__x));
187
188 return __value;
189 }
190
191 double sqrt (double __x)
192 {
193 register double __value;
194 __asm __volatile__
195 ("fsqrt"
196 : "=t" (__value) : "0" (__x));
197
198 return __value;
199 }
200
201 double tan (double __x)
202 {
203 register double __value;
204 register double __value2 __attribute__ ((unused));
205 __asm __volatile__
206 ("fptan"
207 : "=t" (__value2), "=u" (__value) : "0" (__x));
208
209 return __value;
210 }
211
212 //FIXME! Is there a better algorithm. like FT_MulDiv
213 INT STDCALL EngMulDiv(
214 INT nMultiplicand,
215 INT nMultiplier,
216 INT nDivisor)
217 {
218 #if SIZEOF_LONG_LONG >= 8
219 long long ret;
220
221 if (!nDivisor) return -1;
222
223 /* We want to deal with a positive divisor to simplify the logic. */
224 if (nDivisor < 0)
225 {
226 nMultiplicand = - nMultiplicand;
227 nDivisor = -nDivisor;
228 }
229
230 /* If the result is positive, we "add" to round. else, we subtract to round. */
231 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
232 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
233 ret = (((long long)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
234 else
235 ret = (((long long)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
236
237 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
238 return ret;
239 #else
240 if (!nDivisor) return -1;
241
242 /* We want to deal with a positive divisor to simplify the logic. */
243 if (nDivisor < 0)
244 {
245 nMultiplicand = - nMultiplicand;
246 nDivisor = -nDivisor;
247 }
248
249 /* If the result is positive, we "add" to round. else, we subtract to round. */
250 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
251 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
252 return ((nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
253
254 return ((nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
255
256 #endif
257 }