atl100: Added AtlWaitWithMessageLoop implementation.
[wine] / dlls / msvcrt / math.c
1 /*
2  * msvcrt.dll math functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 #include "config.h"
21
22 #include <stdio.h>
23 #define __USE_ISOC9X 1
24 #define __USE_ISOC99 1
25 #include <math.h>
26 #ifdef HAVE_IEEEFP_H
27 #include <ieeefp.h>
28 #endif
29
30 #include "msvcrt.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35
36 #ifndef HAVE_FINITE
37 #ifndef finite /* Could be a macro */
38 #ifdef isfinite
39 #define finite(x) isfinite(x)
40 #else
41 #define finite(x) (!isnan(x)) /* At least catch some cases */
42 #endif
43 #endif
44 #endif
45
46 #ifndef signbit
47 #define signbit(x) 0
48 #endif
49
50 typedef int (CDECL *MSVCRT_matherr_func)(struct MSVCRT__exception *);
51
52 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
53
54 static BOOL sse2_supported;
55 static BOOL sse2_enabled;
56
57 void msvcrt_init_math(void)
58 {
59     sse2_supported = sse2_enabled = IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );
60 }
61
62 /*********************************************************************
63  *      _set_SSE2_enable (MSVCRT.@)
64  */
65 int CDECL MSVCRT__set_SSE2_enable(int flag)
66 {
67     sse2_enabled = flag && sse2_supported;
68     return sse2_enabled;
69 }
70
71 #ifdef __x86_64__
72
73 /*********************************************************************
74  *      _chgsignf (MSVCRT.@)
75  */
76 float CDECL MSVCRT__chgsignf( float num )
77 {
78     /* FIXME: +-infinity,Nan not tested */
79     return -num;
80 }
81
82 /*********************************************************************
83  *      _copysignf (MSVCRT.@)
84  */
85 float CDECL MSVCRT__copysignf( float num, float sign )
86 {
87     /* FIXME: Behaviour for Nan/Inf? */
88     if (sign < 0.0)
89         return num < 0.0 ? num : -num;
90     return num < 0.0 ? -num : num;
91 }
92
93 /*********************************************************************
94  *      _finitef (MSVCRT.@)
95  */
96 int CDECL MSVCRT__finitef( float num )
97 {
98     return finitef(num) != 0; /* See comment for _isnan() */
99 }
100
101 /*********************************************************************
102  *      _isnanf (MSVCRT.@)
103  */
104 INT CDECL MSVCRT__isnanf( float num )
105 {
106     /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
107      * Do the same, as the result may be used in calculations
108      */
109     return isnanf(num) != 0;
110 }
111
112 /*********************************************************************
113  *      _logbf (MSVCRT.@)
114  */
115 float CDECL MSVCRT__logbf( float num )
116 {
117     if (!finitef(num)) *MSVCRT__errno() = MSVCRT_EDOM;
118     return logbf(num);
119 }
120
121 /*********************************************************************
122  *      _nextafterf (MSVCRT.@)
123  */
124 float CDECL MSVCRT__nextafterf( float num, float next )
125 {
126     if (!finitef(num) || !finitef(next)) *MSVCRT__errno() = MSVCRT_EDOM;
127     return nextafterf( num, next );
128 }
129
130 /*********************************************************************
131  *      MSVCRT_acosf (MSVCRT.@)
132  */
133 float CDECL MSVCRT_acosf( float x )
134 {
135   if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
136   /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
137    * asin() uses a similar construction. This is bad because as x gets nearer to
138    * 1 the error in the expression "1 - x^2" can get relatively large due to
139    * cancellation. The sqrt() makes things worse. A safer way to calculate
140    * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
141   return atan2f(sqrtf((1 - x) * (1 + x)), x);
142 }
143
144 /*********************************************************************
145  *      MSVCRT_asinf (MSVCRT.@)
146  */
147 float CDECL MSVCRT_asinf( float x )
148 {
149   if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
150   return atan2f(x, sqrtf((1 - x) * (1 + x)));
151 }
152
153 /*********************************************************************
154  *      MSVCRT_atanf (MSVCRT.@)
155  */
156 float CDECL MSVCRT_atanf( float x )
157 {
158   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
159   return atanf(x);
160 }
161
162 /*********************************************************************
163  *              MSVCRT_atan2f (MSVCRT.@)
164  */
165 float CDECL MSVCRT_atan2f( float x, float y )
166 {
167   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
168   return atan2f(x,y);
169 }
170
171 /*********************************************************************
172  *      MSVCRT_cosf (MSVCRT.@)
173  */
174 float CDECL MSVCRT_cosf( float x )
175 {
176   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
177   return cosf(x);
178 }
179
180 /*********************************************************************
181  *      MSVCRT_coshf (MSVCRT.@)
182  */
183 float CDECL MSVCRT_coshf( float x )
184 {
185   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
186   return coshf(x);
187 }
188
189 /*********************************************************************
190  *      MSVCRT_expf (MSVCRT.@)
191  */
192 float CDECL MSVCRT_expf( float x )
193 {
194   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
195   return expf(x);
196 }
197
198 /*********************************************************************
199  *      MSVCRT_fmodf (MSVCRT.@)
200  */
201 float CDECL MSVCRT_fmodf( float x, float y )
202 {
203   if (!finitef(x) || !finitef(y)) *MSVCRT__errno() = MSVCRT_EDOM;
204   return fmodf(x,y);
205 }
206
207 /*********************************************************************
208  *      MSVCRT_logf (MSVCRT.@)
209  */
210 float CDECL MSVCRT_logf( float x)
211 {
212   if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
213   if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
214   return logf(x);
215 }
216
217 /*********************************************************************
218  *      MSVCRT_log10f (MSVCRT.@)
219  */
220 float CDECL MSVCRT_log10f( float x )
221 {
222   if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
223   if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
224   return log10f(x);
225 }
226
227 /*********************************************************************
228  *      MSVCRT_powf (MSVCRT.@)
229  */
230 float CDECL MSVCRT_powf( float x, float y )
231 {
232   /* FIXME: If x < 0 and y is not integral, set EDOM */
233   float z = powf(x,y);
234   if (!finitef(z)) *MSVCRT__errno() = MSVCRT_EDOM;
235   return z;
236 }
237
238 /*********************************************************************
239  *      MSVCRT_sinf (MSVCRT.@)
240  */
241 float CDECL MSVCRT_sinf( float x )
242 {
243   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
244   return sinf(x);
245 }
246
247 /*********************************************************************
248  *      MSVCRT_sinhf (MSVCRT.@)
249  */
250 float CDECL MSVCRT_sinhf( float x )
251 {
252   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
253   return sinhf(x);
254 }
255
256 /*********************************************************************
257  *      MSVCRT_sqrtf (MSVCRT.@)
258  */
259 float CDECL MSVCRT_sqrtf( float x )
260 {
261   if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
262   return sqrtf(x);
263 }
264
265 /*********************************************************************
266  *      MSVCRT_tanf (MSVCRT.@)
267  */
268 float CDECL MSVCRT_tanf( float x )
269 {
270   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
271   return tanf(x);
272 }
273
274 /*********************************************************************
275  *      MSVCRT_tanhf (MSVCRT.@)
276  */
277 float CDECL MSVCRT_tanhf( float x )
278 {
279   if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
280   return tanhf(x);
281 }
282
283 /*********************************************************************
284  *      ceilf (MSVCRT.@)
285  */
286 float CDECL MSVCRT_ceilf( float x )
287 {
288   return ceilf(x);
289 }
290
291 /*********************************************************************
292  *      floorf (MSVCRT.@)
293  */
294 float CDECL MSVCRT_floorf( float x )
295 {
296   return floorf(x);
297 }
298
299 /*********************************************************************
300  *      frexpf (MSVCRT.@)
301  */
302 float CDECL MSVCRT_frexpf( float x, int *exp )
303 {
304   return frexpf( x, exp );
305 }
306
307 /*********************************************************************
308  *      _scalbf (MSVCRT.@)
309  */
310 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
311 {
312   if (!finitef(num)) *MSVCRT__errno() = MSVCRT_EDOM;
313   return ldexpf(num, power);
314 }
315
316 /*********************************************************************
317  *      modff (MSVCRT.@)
318  */
319 double CDECL MSVCRT_modff( float x, float *iptr )
320 {
321   return modff( x, iptr );
322 }
323
324 #endif
325
326 /*********************************************************************
327  *              MSVCRT_acos (MSVCRT.@)
328  */
329 double CDECL MSVCRT_acos( double x )
330 {
331   if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
332   /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
333    * asin() uses a similar construction. This is bad because as x gets nearer to
334    * 1 the error in the expression "1 - x^2" can get relatively large due to
335    * cancellation. The sqrt() makes things worse. A safer way to calculate
336    * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
337   return atan2(sqrt((1 - x) * (1 + x)), x);
338 }
339
340 /*********************************************************************
341  *              MSVCRT_asin (MSVCRT.@)
342  */
343 double CDECL MSVCRT_asin( double x )
344 {
345   if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
346   return atan2(x, sqrt((1 - x) * (1 + x)));
347 }
348
349 /*********************************************************************
350  *              MSVCRT_atan (MSVCRT.@)
351  */
352 double CDECL MSVCRT_atan( double x )
353 {
354   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
355   return atan(x);
356 }
357
358 /*********************************************************************
359  *              MSVCRT_atan2 (MSVCRT.@)
360  */
361 double CDECL MSVCRT_atan2( double x, double y )
362 {
363   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
364   return atan2(x,y);
365 }
366
367 /*********************************************************************
368  *              MSVCRT_cos (MSVCRT.@)
369  */
370 double CDECL MSVCRT_cos( double x )
371 {
372   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
373   return cos(x);
374 }
375
376 /*********************************************************************
377  *              MSVCRT_cosh (MSVCRT.@)
378  */
379 double CDECL MSVCRT_cosh( double x )
380 {
381   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
382   return cosh(x);
383 }
384
385 /*********************************************************************
386  *              MSVCRT_exp (MSVCRT.@)
387  */
388 double CDECL MSVCRT_exp( double x )
389 {
390   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
391   return exp(x);
392 }
393
394 /*********************************************************************
395  *              MSVCRT_fmod (MSVCRT.@)
396  */
397 double CDECL MSVCRT_fmod( double x, double y )
398 {
399   if (!finite(x) || !finite(y)) *MSVCRT__errno() = MSVCRT_EDOM;
400   return fmod(x,y);
401 }
402
403 /*********************************************************************
404  *              MSVCRT_log (MSVCRT.@)
405  */
406 double CDECL MSVCRT_log( double x)
407 {
408   if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
409   if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
410   return log(x);
411 }
412
413 /*********************************************************************
414  *              MSVCRT_log10 (MSVCRT.@)
415  */
416 double CDECL MSVCRT_log10( double x )
417 {
418   if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
419   if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
420   return log10(x);
421 }
422
423 /*********************************************************************
424  *              MSVCRT_pow (MSVCRT.@)
425  */
426 double CDECL MSVCRT_pow( double x, double y )
427 {
428   /* FIXME: If x < 0 and y is not integral, set EDOM */
429   double z = pow(x,y);
430   if (!finite(z)) *MSVCRT__errno() = MSVCRT_EDOM;
431   return z;
432 }
433
434 /*********************************************************************
435  *              MSVCRT_sin (MSVCRT.@)
436  */
437 double CDECL MSVCRT_sin( double x )
438 {
439   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
440   return sin(x);
441 }
442
443 /*********************************************************************
444  *              MSVCRT_sinh (MSVCRT.@)
445  */
446 double CDECL MSVCRT_sinh( double x )
447 {
448   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
449   return sinh(x);
450 }
451
452 /*********************************************************************
453  *              MSVCRT_sqrt (MSVCRT.@)
454  */
455 double CDECL MSVCRT_sqrt( double x )
456 {
457   if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
458   return sqrt(x);
459 }
460
461 /*********************************************************************
462  *              MSVCRT_tan (MSVCRT.@)
463  */
464 double CDECL MSVCRT_tan( double x )
465 {
466   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
467   return tan(x);
468 }
469
470 /*********************************************************************
471  *              MSVCRT_tanh (MSVCRT.@)
472  */
473 double CDECL MSVCRT_tanh( double x )
474 {
475   if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
476   return tanh(x);
477 }
478
479
480 #if defined(__GNUC__) && defined(__i386__)
481
482 #define FPU_DOUBLE(var) double var; \
483   __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
484 #define FPU_DOUBLES(var1,var2) double var1,var2; \
485   __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
486   __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
487
488 /*********************************************************************
489  *              _CIacos (MSVCRT.@)
490  */
491 double CDECL _CIacos(void)
492 {
493   FPU_DOUBLE(x);
494   return MSVCRT_acos(x);
495 }
496
497 /*********************************************************************
498  *              _CIasin (MSVCRT.@)
499  */
500 double CDECL _CIasin(void)
501 {
502   FPU_DOUBLE(x);
503   return MSVCRT_asin(x);
504 }
505
506 /*********************************************************************
507  *              _CIatan (MSVCRT.@)
508  */
509 double CDECL _CIatan(void)
510 {
511   FPU_DOUBLE(x);
512   return MSVCRT_atan(x);
513 }
514
515 /*********************************************************************
516  *              _CIatan2 (MSVCRT.@)
517  */
518 double CDECL _CIatan2(void)
519 {
520   FPU_DOUBLES(x,y);
521   return MSVCRT_atan2(x,y);
522 }
523
524 /*********************************************************************
525  *              _CIcos (MSVCRT.@)
526  */
527 double CDECL _CIcos(void)
528 {
529   FPU_DOUBLE(x);
530   return MSVCRT_cos(x);
531 }
532
533 /*********************************************************************
534  *              _CIcosh (MSVCRT.@)
535  */
536 double CDECL _CIcosh(void)
537 {
538   FPU_DOUBLE(x);
539   return MSVCRT_cosh(x);
540 }
541
542 /*********************************************************************
543  *              _CIexp (MSVCRT.@)
544  */
545 double CDECL _CIexp(void)
546 {
547   FPU_DOUBLE(x);
548   return MSVCRT_exp(x);
549 }
550
551 /*********************************************************************
552  *              _CIfmod (MSVCRT.@)
553  */
554 double CDECL _CIfmod(void)
555 {
556   FPU_DOUBLES(x,y);
557   return MSVCRT_fmod(x,y);
558 }
559
560 /*********************************************************************
561  *              _CIlog (MSVCRT.@)
562  */
563 double CDECL _CIlog(void)
564 {
565   FPU_DOUBLE(x);
566   return MSVCRT_log(x);
567 }
568
569 /*********************************************************************
570  *              _CIlog10 (MSVCRT.@)
571  */
572 double CDECL _CIlog10(void)
573 {
574   FPU_DOUBLE(x);
575   return MSVCRT_log10(x);
576 }
577
578 /*********************************************************************
579  *              _CIpow (MSVCRT.@)
580  */
581 double CDECL _CIpow(void)
582 {
583   FPU_DOUBLES(x,y);
584   return MSVCRT_pow(x,y);
585 }
586
587 /*********************************************************************
588  *              _CIsin (MSVCRT.@)
589  */
590 double CDECL _CIsin(void)
591 {
592   FPU_DOUBLE(x);
593   return MSVCRT_sin(x);
594 }
595
596 /*********************************************************************
597  *              _CIsinh (MSVCRT.@)
598  */
599 double CDECL _CIsinh(void)
600 {
601   FPU_DOUBLE(x);
602   return MSVCRT_sinh(x);
603 }
604
605 /*********************************************************************
606  *              _CIsqrt (MSVCRT.@)
607  */
608 double CDECL _CIsqrt(void)
609 {
610   FPU_DOUBLE(x);
611   return MSVCRT_sqrt(x);
612 }
613
614 /*********************************************************************
615  *              _CItan (MSVCRT.@)
616  */
617 double CDECL _CItan(void)
618 {
619   FPU_DOUBLE(x);
620   return MSVCRT_tan(x);
621 }
622
623 /*********************************************************************
624  *              _CItanh (MSVCRT.@)
625  */
626 double CDECL _CItanh(void)
627 {
628   FPU_DOUBLE(x);
629   return MSVCRT_tanh(x);
630 }
631
632 /*********************************************************************
633  *                  _ftol   (MSVCRT.@)
634  */
635 LONGLONG CDECL MSVCRT__ftol(void)
636 {
637     FPU_DOUBLE(x);
638     return (LONGLONG)x;
639 }
640
641 #endif /* defined(__GNUC__) && defined(__i386__) */
642
643 /*********************************************************************
644  *              _fpclass (MSVCRT.@)
645  */
646 int CDECL MSVCRT__fpclass(double num)
647 {
648 #if defined(HAVE_FPCLASS) || defined(fpclass)
649   switch (fpclass( num ))
650   {
651 #ifdef FP_SNAN
652   case FP_SNAN:  return MSVCRT__FPCLASS_SNAN;
653 #endif
654 #ifdef FP_QNAN
655   case FP_QNAN:  return MSVCRT__FPCLASS_QNAN;
656 #endif
657 #ifdef FP_NINF
658   case FP_NINF:  return MSVCRT__FPCLASS_NINF;
659 #endif
660 #ifdef FP_PINF
661   case FP_PINF:  return MSVCRT__FPCLASS_PINF;
662 #endif
663 #ifdef FP_NDENORM
664   case FP_NDENORM: return MSVCRT__FPCLASS_ND;
665 #endif
666 #ifdef FP_PDENORM
667   case FP_PDENORM: return MSVCRT__FPCLASS_PD;
668 #endif
669 #ifdef FP_NZERO
670   case FP_NZERO: return MSVCRT__FPCLASS_NZ;
671 #endif
672 #ifdef FP_PZERO
673   case FP_PZERO: return MSVCRT__FPCLASS_PZ;
674 #endif
675 #ifdef FP_NNORM
676   case FP_NNORM: return MSVCRT__FPCLASS_NN;
677 #endif
678 #ifdef FP_PNORM
679   case FP_PNORM: return MSVCRT__FPCLASS_PN;
680 #endif
681   default: return MSVCRT__FPCLASS_PN;
682   }
683 #elif defined (fpclassify)
684   switch (fpclassify( num ))
685   {
686   case FP_NAN: return MSVCRT__FPCLASS_QNAN;
687   case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
688   case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
689   case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
690   }
691   return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
692 #else
693   if (!finite(num))
694     return MSVCRT__FPCLASS_QNAN;
695   return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
696 #endif
697 }
698
699 /*********************************************************************
700  *              _rotl (MSVCRT.@)
701  */
702 unsigned int CDECL _rotl(unsigned int num, int shift)
703 {
704   shift &= 31;
705   return (num << shift) | (num >> (32-shift));
706 }
707
708 /*********************************************************************
709  *              _lrotl (MSVCRT.@)
710  */
711 MSVCRT_ulong CDECL MSVCRT__lrotl(MSVCRT_ulong num, int shift)
712 {
713   shift &= 0x1f;
714   return (num << shift) | (num >> (32-shift));
715 }
716
717 /*********************************************************************
718  *              _lrotr (MSVCRT.@)
719  */
720 MSVCRT_ulong CDECL MSVCRT__lrotr(MSVCRT_ulong num, int shift)
721 {
722   shift &= 0x1f;
723   return (num >> shift) | (num << (32-shift));
724 }
725
726 /*********************************************************************
727  *              _rotr (MSVCRT.@)
728  */
729 unsigned int CDECL _rotr(unsigned int num, int shift)
730 {
731     shift &= 0x1f;
732     return (num >> shift) | (num << (32-shift));
733 }
734
735 /*********************************************************************
736  *              _rotl64 (MSVCRT.@)
737  */
738 unsigned __int64 CDECL _rotl64(unsigned __int64 num, int shift)
739 {
740   shift &= 63;
741   return (num << shift) | (num >> (64-shift));
742 }
743
744 /*********************************************************************
745  *              _rotr64 (MSVCRT.@)
746  */
747 unsigned __int64 CDECL _rotr64(unsigned __int64 num, int shift)
748 {
749     shift &= 63;
750     return (num >> shift) | (num << (64-shift));
751 }
752
753 /*********************************************************************
754  *              abs (MSVCRT.@)
755  */
756 int CDECL MSVCRT_abs( int n )
757 {
758     return n >= 0 ? n : -n;
759 }
760
761 /*********************************************************************
762  *              labs (MSVCRT.@)
763  */
764 MSVCRT_long CDECL MSVCRT_labs( MSVCRT_long n )
765 {
766     return n >= 0 ? n : -n;
767 }
768
769 /*********************************************************************
770  *              _abs64 (MSVCRT.@)
771  */
772 __int64 CDECL _abs64( __int64 n )
773 {
774     return n >= 0 ? n : -n;
775 }
776
777 /*********************************************************************
778  *              _logb (MSVCRT.@)
779  */
780 double CDECL MSVCRT__logb(double num)
781 {
782   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
783   return logb(num);
784 }
785
786 /*********************************************************************
787  *              _scalb (MSVCRT.@)
788  */
789 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
790 {
791   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
792   return ldexp(num, power);
793 }
794
795 /*********************************************************************
796  *              _hypot (MSVCRT.@)
797  */
798 double CDECL _hypot(double x, double y)
799 {
800   /* FIXME: errno handling */
801   return hypot( x, y );
802 }
803
804 /*********************************************************************
805  *      _hypotf (MSVCRT.@)
806  */
807 float CDECL MSVCRT__hypotf(float x, float y)
808 {
809   /* FIXME: errno handling */
810   return hypotf( x, y );
811 }
812
813 /*********************************************************************
814  *              ceil (MSVCRT.@)
815  */
816 double CDECL MSVCRT_ceil( double x )
817 {
818   return ceil(x);
819 }
820
821 /*********************************************************************
822  *              floor (MSVCRT.@)
823  */
824 double CDECL MSVCRT_floor( double x )
825 {
826   return floor(x);
827 }
828
829 /*********************************************************************
830  *              fabs (MSVCRT.@)
831  */
832 double CDECL MSVCRT_fabs( double x )
833 {
834   return fabs(x);
835 }
836
837 /*********************************************************************
838  *              frexp (MSVCRT.@)
839  */
840 double CDECL MSVCRT_frexp( double x, int *exp )
841 {
842   return frexp( x, exp );
843 }
844
845 /*********************************************************************
846  *              modf (MSVCRT.@)
847  */
848 double CDECL MSVCRT_modf( double x, double *iptr )
849 {
850   return modf( x, iptr );
851 }
852
853 /*********************************************************************
854  *              _matherr (MSVCRT.@)
855  */
856 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
857 {
858   if (e)
859     TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
860           e->retval);
861   else
862     TRACE("(null)\n");
863   if (MSVCRT_default_matherr_func)
864     return MSVCRT_default_matherr_func(e);
865   ERR(":Unhandled math error!\n");
866   return 0;
867 }
868
869 /*********************************************************************
870  *              __setusermatherr (MSVCRT.@)
871  */
872 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
873 {
874   MSVCRT_default_matherr_func = func;
875   TRACE(":new matherr handler %p\n", func);
876 }
877
878 /**********************************************************************
879  *              _statusfp2 (MSVCRT.@)
880  *
881  * Not exported by native msvcrt, added in msvcr80.
882  */
883 #if defined(__i386__) || defined(__x86_64__)
884 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
885 {
886 #ifdef __GNUC__
887     unsigned int flags;
888     unsigned long fpword;
889
890     if (x86_sw)
891     {
892         __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
893         flags = 0;
894         if (fpword & 0x1)  flags |= MSVCRT__SW_INVALID;
895         if (fpword & 0x2)  flags |= MSVCRT__SW_DENORMAL;
896         if (fpword & 0x4)  flags |= MSVCRT__SW_ZERODIVIDE;
897         if (fpword & 0x8)  flags |= MSVCRT__SW_OVERFLOW;
898         if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
899         if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
900         *x86_sw = flags;
901     }
902
903     if (!sse2_sw) return;
904
905     if (sse2_supported)
906     {
907         __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
908         flags = 0;
909         if (fpword & 0x1)  flags |= MSVCRT__SW_INVALID;
910         if (fpword & 0x2)  flags |= MSVCRT__SW_DENORMAL;
911         if (fpword & 0x4)  flags |= MSVCRT__SW_ZERODIVIDE;
912         if (fpword & 0x8)  flags |= MSVCRT__SW_OVERFLOW;
913         if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
914         if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
915         *sse2_sw = flags;
916     }
917     else *sse2_sw = 0;
918 #else
919     FIXME( "not implemented\n" );
920 #endif
921 }
922 #endif
923
924 /**********************************************************************
925  *              _statusfp (MSVCRT.@)
926  */
927 unsigned int CDECL _statusfp(void)
928 {
929 #if defined(__i386__) || defined(__x86_64__)
930     unsigned int x86_sw, sse2_sw;
931
932     _statusfp2( &x86_sw, &sse2_sw );
933     /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
934     return x86_sw | sse2_sw;
935 #else
936     FIXME( "not implemented\n" );
937     return 0;
938 #endif
939 }
940
941 /*********************************************************************
942  *              _clearfp (MSVCRT.@)
943  */
944 unsigned int CDECL _clearfp(void)
945 {
946     unsigned int flags = 0;
947 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
948     unsigned long fpword;
949
950     __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
951     if (fpword & 0x1)  flags |= MSVCRT__SW_INVALID;
952     if (fpword & 0x2)  flags |= MSVCRT__SW_DENORMAL;
953     if (fpword & 0x4)  flags |= MSVCRT__SW_ZERODIVIDE;
954     if (fpword & 0x8)  flags |= MSVCRT__SW_OVERFLOW;
955     if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
956     if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
957
958     if (sse2_supported)
959     {
960         __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
961         if (fpword & 0x1)  flags |= MSVCRT__SW_INVALID;
962         if (fpword & 0x2)  flags |= MSVCRT__SW_DENORMAL;
963         if (fpword & 0x4)  flags |= MSVCRT__SW_ZERODIVIDE;
964         if (fpword & 0x8)  flags |= MSVCRT__SW_OVERFLOW;
965         if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
966         if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
967         fpword &= ~0x3f;
968         __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
969     }
970 #else
971     FIXME( "not implemented\n" );
972 #endif
973     return flags;
974 }
975
976 /*********************************************************************
977  *              __fpecode (MSVCRT.@)
978  */
979 int * CDECL __fpecode(void)
980 {
981     return &msvcrt_get_thread_data()->fpecode;
982 }
983
984 /*********************************************************************
985  *              ldexp (MSVCRT.@)
986  */
987 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
988 {
989   double z = ldexp(num,exp);
990
991   if (!finite(z))
992     *MSVCRT__errno() = MSVCRT_ERANGE;
993   else if (z == 0 && signbit(z))
994     z = 0.0; /* Convert -0 -> +0 */
995   return z;
996 }
997
998 /*********************************************************************
999  *              _cabs (MSVCRT.@)
1000  */
1001 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
1002 {
1003   return sqrt(num.x * num.x + num.y * num.y);
1004 }
1005
1006 /*********************************************************************
1007  *              _chgsign (MSVCRT.@)
1008  */
1009 double CDECL MSVCRT__chgsign(double num)
1010 {
1011   /* FIXME: +-infinity,Nan not tested */
1012   return -num;
1013 }
1014
1015 /*********************************************************************
1016  *              __control87_2 (MSVCRT.@)
1017  *
1018  * Not exported by native msvcrt, added in msvcr80.
1019  */
1020 #if defined(__i386__) || defined(__x86_64__)
1021 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1022                          unsigned int *x86_cw, unsigned int *sse2_cw )
1023 {
1024 #ifdef __GNUC__
1025     unsigned long fpword;
1026     unsigned int flags;
1027
1028     if (x86_cw)
1029     {
1030         __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1031
1032         /* Convert into mask constants */
1033         flags = 0;
1034         if (fpword & 0x1)  flags |= MSVCRT__EM_INVALID;
1035         if (fpword & 0x2)  flags |= MSVCRT__EM_DENORMAL;
1036         if (fpword & 0x4)  flags |= MSVCRT__EM_ZERODIVIDE;
1037         if (fpword & 0x8)  flags |= MSVCRT__EM_OVERFLOW;
1038         if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1039         if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1040         switch (fpword & 0xc00)
1041         {
1042         case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1043         case 0x800: flags |= MSVCRT__RC_UP; break;
1044         case 0x400: flags |= MSVCRT__RC_DOWN; break;
1045         }
1046         switch (fpword & 0x300)
1047         {
1048         case 0x0:   flags |= MSVCRT__PC_24; break;
1049         case 0x200: flags |= MSVCRT__PC_53; break;
1050         case 0x300: flags |= MSVCRT__PC_64; break;
1051         }
1052         if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1053
1054         TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1055         if (mask)
1056         {
1057             flags = (flags & ~mask) | (newval & mask);
1058
1059             /* Convert (masked) value back to fp word */
1060             fpword = 0;
1061             if (flags & MSVCRT__EM_INVALID)    fpword |= 0x1;
1062             if (flags & MSVCRT__EM_DENORMAL)   fpword |= 0x2;
1063             if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1064             if (flags & MSVCRT__EM_OVERFLOW)   fpword |= 0x8;
1065             if (flags & MSVCRT__EM_UNDERFLOW)  fpword |= 0x10;
1066             if (flags & MSVCRT__EM_INEXACT)    fpword |= 0x20;
1067             switch (flags & MSVCRT__MCW_RC)
1068             {
1069             case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1070             case MSVCRT__RC_UP:                 fpword |= 0x800; break;
1071             case MSVCRT__RC_DOWN:               fpword |= 0x400; break;
1072             }
1073             switch (flags & MSVCRT__MCW_PC)
1074             {
1075             case MSVCRT__PC_64: fpword |= 0x300; break;
1076             case MSVCRT__PC_53: fpword |= 0x200; break;
1077             case MSVCRT__PC_24: fpword |= 0x0; break;
1078             }
1079             if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1080
1081             __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1082         }
1083         *x86_cw = flags;
1084     }
1085
1086     if (!sse2_cw) return 1;
1087
1088     if (sse2_supported)
1089     {
1090         __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1091
1092         /* Convert into mask constants */
1093         flags = 0;
1094         if (fpword & 0x80)   flags |= MSVCRT__EM_INVALID;
1095         if (fpword & 0x100)  flags |= MSVCRT__EM_DENORMAL;
1096         if (fpword & 0x200)  flags |= MSVCRT__EM_ZERODIVIDE;
1097         if (fpword & 0x400)  flags |= MSVCRT__EM_OVERFLOW;
1098         if (fpword & 0x800)  flags |= MSVCRT__EM_UNDERFLOW;
1099         if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1100         switch (fpword & 0x6000)
1101         {
1102         case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1103         case 0x4000: flags |= MSVCRT__RC_UP; break;
1104         case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1105         }
1106         switch (fpword & 0x8040)
1107         {
1108         case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1109         case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1110         case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1111         }
1112
1113         TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1114         if (mask)
1115         {
1116             flags = (flags & ~mask) | (newval & mask);
1117
1118             /* Convert (masked) value back to fp word */
1119             fpword = 0;
1120             if (flags & MSVCRT__EM_INVALID)    fpword |= 0x80;
1121             if (flags & MSVCRT__EM_DENORMAL)   fpword |= 0x100;
1122             if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1123             if (flags & MSVCRT__EM_OVERFLOW)   fpword |= 0x400;
1124             if (flags & MSVCRT__EM_UNDERFLOW)  fpword |= 0x800;
1125             if (flags & MSVCRT__EM_INEXACT)    fpword |= 0x1000;
1126             switch (flags & MSVCRT__MCW_RC)
1127             {
1128             case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1129             case MSVCRT__RC_UP:                 fpword |= 0x4000; break;
1130             case MSVCRT__RC_DOWN:               fpword |= 0x2000; break;
1131             }
1132             switch (flags & MSVCRT__MCW_DN)
1133             {
1134             case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1135             case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1136             case MSVCRT__DN_FLUSH:                       fpword |= 0x8040; break;
1137             }
1138             __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1139         }
1140         *sse2_cw = flags;
1141     }
1142     else *sse2_cw = 0;
1143
1144     return 1;
1145 #else
1146     FIXME( "not implemented\n" );
1147     return 0;
1148 #endif
1149 }
1150 #endif
1151
1152 /*********************************************************************
1153  *              _control87 (MSVCRT.@)
1154  */
1155 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1156 {
1157 #if defined(__i386__) || defined(__x86_64__)
1158     unsigned int x86_cw, sse2_cw;
1159
1160     __control87_2( newval, mask, &x86_cw, &sse2_cw );
1161
1162     if ((x86_cw ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) x86_cw |= MSVCRT__EM_AMBIGUOUS;
1163     return x86_cw;
1164 #else
1165     FIXME( "not implemented\n" );
1166     return 0;
1167 #endif
1168 }
1169
1170 /*********************************************************************
1171  *              _controlfp (MSVCRT.@)
1172  */
1173 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
1174 {
1175   return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
1176 }
1177
1178 /*********************************************************************
1179  *              _set_controlfp (MSVCRT.@)
1180  */
1181 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
1182 {
1183     _controlfp( newval, mask );
1184 }
1185
1186 /*********************************************************************
1187  *              _controlfp_s (MSVCRT.@)
1188  */
1189 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
1190 {
1191     static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
1192                                            MSVCRT__MCW_PC | MSVCRT__MCW_DN);
1193     unsigned int val;
1194
1195     if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
1196     {
1197         if (cur) *cur = _controlfp( 0, 0 );  /* retrieve it anyway */
1198         return MSVCRT_EINVAL;
1199     }
1200     val = _controlfp( newval, mask );
1201     if (cur) *cur = val;
1202     return 0;
1203 }
1204
1205 /*********************************************************************
1206  *              _copysign (MSVCRT.@)
1207  */
1208 double CDECL MSVCRT__copysign(double num, double sign)
1209 {
1210   /* FIXME: Behaviour for Nan/Inf? */
1211   if (sign < 0.0)
1212     return num < 0.0 ? num : -num;
1213   return num < 0.0 ? -num : num;
1214 }
1215
1216 /*********************************************************************
1217  *              _finite (MSVCRT.@)
1218  */
1219 int CDECL MSVCRT__finite(double num)
1220 {
1221   return (finite(num)?1:0); /* See comment for _isnan() */
1222 }
1223
1224 /*********************************************************************
1225  *              _fpreset (MSVCRT.@)
1226  */
1227 void CDECL _fpreset(void)
1228 {
1229 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1230     const unsigned int x86_cw = 0x27f;
1231     __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
1232     if (sse2_supported)
1233     {
1234         const unsigned long sse2_cw = 0x1f80;
1235         __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
1236     }
1237 #else
1238     FIXME( "not implemented\n" );
1239 #endif
1240 }
1241
1242 /*********************************************************************
1243  *              _isnan (MSVCRT.@)
1244  */
1245 INT CDECL MSVCRT__isnan(double num)
1246 {
1247   /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1248    * Do the same, as the result may be used in calculations
1249    */
1250   return isnan(num) ? 1 : 0;
1251 }
1252
1253 /*********************************************************************
1254  *              _j0 (MSVCRT.@)
1255  */
1256 double CDECL MSVCRT__j0(double num)
1257 {
1258   /* FIXME: errno handling */
1259   return j0(num);
1260 }
1261
1262 /*********************************************************************
1263  *              _j1 (MSVCRT.@)
1264  */
1265 double CDECL MSVCRT__j1(double num)
1266 {
1267   /* FIXME: errno handling */
1268   return j1(num);
1269 }
1270
1271 /*********************************************************************
1272  *              _jn (MSVCRT.@)
1273  */
1274 double CDECL MSVCRT__jn(int n, double num)
1275 {
1276   /* FIXME: errno handling */
1277   return jn(n, num);
1278 }
1279
1280 /*********************************************************************
1281  *              _y0 (MSVCRT.@)
1282  */
1283 double CDECL MSVCRT__y0(double num)
1284 {
1285   double retval;
1286   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1287   retval  = y0(num);
1288   if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1289   {
1290     *MSVCRT__errno() = MSVCRT_EDOM;
1291     retval = sqrt(-1);
1292   }
1293   return retval;
1294 }
1295
1296 /*********************************************************************
1297  *              _y1 (MSVCRT.@)
1298  */
1299 double CDECL MSVCRT__y1(double num)
1300 {
1301   double retval;
1302   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1303   retval  = y1(num);
1304   if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1305   {
1306     *MSVCRT__errno() = MSVCRT_EDOM;
1307     retval = sqrt(-1);
1308   }
1309   return retval;
1310 }
1311
1312 /*********************************************************************
1313  *              _yn (MSVCRT.@)
1314  */
1315 double CDECL MSVCRT__yn(int order, double num)
1316 {
1317   double retval;
1318   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1319   retval  = yn(order,num);
1320   if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1321   {
1322     *MSVCRT__errno() = MSVCRT_EDOM;
1323     retval = sqrt(-1);
1324   }
1325   return retval;
1326 }
1327
1328 /*********************************************************************
1329  *              _nextafter (MSVCRT.@)
1330  */
1331 double CDECL MSVCRT__nextafter(double num, double next)
1332 {
1333   double retval;
1334   if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1335   retval = nextafter(num,next);
1336   return retval;
1337 }
1338
1339 /*********************************************************************
1340  *              _ecvt (MSVCRT.@)
1341  */
1342 char * CDECL _ecvt( double number, int ndigits, int *decpt, int *sign )
1343 {
1344     int prec, len;
1345     thread_data_t *data = msvcrt_get_thread_data();
1346     /* FIXME: check better for overflow (native supports over 300 chars) */
1347     ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1348                                       * 4 for exponent and one for
1349                                       * terminating '\0' */
1350     if (!data->efcvt_buffer)
1351         data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1352
1353     if( number < 0) {
1354         *sign = TRUE;
1355         number = -number;
1356     } else
1357         *sign = FALSE;
1358     /* handle cases with zero ndigits or less */
1359     prec = ndigits;
1360     if( prec < 1) prec = 2;
1361     len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1362     /* take the decimal "point away */
1363     if( prec != 1)
1364         memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1365     /* take the exponential "e" out */
1366     data->efcvt_buffer[ prec] = '\0';
1367     /* read the exponent */
1368     sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1369     (*decpt)++;
1370     /* adjust for some border cases */
1371     if( data->efcvt_buffer[0] == '0')/* value is zero */
1372         *decpt = 0;
1373     /* handle cases with zero ndigits or less */
1374     if( ndigits < 1){
1375         if( data->efcvt_buffer[ 0] >= '5')
1376             (*decpt)++;
1377         data->efcvt_buffer[ 0] = '\0';
1378     }
1379     TRACE("out=\"%s\"\n",data->efcvt_buffer);
1380     return data->efcvt_buffer;
1381 }
1382
1383 /*********************************************************************
1384  *              _ecvt_s (MSVCRT.@)
1385  */
1386 int CDECL _ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
1387 {
1388     int prec, len;
1389     char *result;
1390     const char infret[] = "1#INF";
1391
1392     if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
1393     if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
1394     if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
1395     if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1396     if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1397
1398     /* special case - inf */
1399     if(number == HUGE_VAL || number == -HUGE_VAL)
1400     {
1401         memset(buffer, '0', ndigits);
1402         memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
1403         buffer[ndigits] = '\0';
1404         (*decpt) = 1;
1405         if(number == -HUGE_VAL)
1406             (*sign) = 1;
1407         else
1408             (*sign) = 0;
1409         return 0;
1410     }
1411     /* handle cases with zero ndigits or less */
1412     prec = ndigits;
1413     if( prec < 1) prec = 2;
1414     result = MSVCRT_malloc(prec + 7);
1415
1416     if( number < 0) {
1417         *sign = TRUE;
1418         number = -number;
1419     } else
1420         *sign = FALSE;
1421     len = snprintf(result, prec + 7, "%.*le", prec - 1, number);
1422     /* take the decimal "point away */
1423     if( prec != 1)
1424         memmove( result + 1, result + 2, len - 1 );
1425     /* take the exponential "e" out */
1426     result[ prec] = '\0';
1427     /* read the exponent */
1428     sscanf( result + prec + 1, "%d", decpt);
1429     (*decpt)++;
1430     /* adjust for some border cases */
1431     if( result[0] == '0')/* value is zero */
1432         *decpt = 0;
1433     /* handle cases with zero ndigits or less */
1434     if( ndigits < 1){
1435         if( result[ 0] >= '5')
1436             (*decpt)++;
1437         result[ 0] = '\0';
1438     }
1439     memcpy( buffer, result, max(ndigits + 1, 1) );
1440     MSVCRT_free( result );
1441     return 0;
1442 }
1443
1444 /***********************************************************************
1445  *              _fcvt  (MSVCRT.@)
1446  */
1447 char * CDECL _fcvt( double number, int ndigits, int *decpt, int *sign )
1448 {
1449     thread_data_t *data = msvcrt_get_thread_data();
1450     int stop, dec1, dec2;
1451     char *ptr1, *ptr2, *first;
1452     char buf[80]; /* ought to be enough */
1453
1454     if (!data->efcvt_buffer)
1455         data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1456
1457     if (number < 0)
1458     {
1459         *sign = 1;
1460         number = -number;
1461     } else *sign = 0;
1462
1463     snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1464     ptr1 = buf;
1465     ptr2 = data->efcvt_buffer;
1466     first = NULL;
1467     dec1 = 0;
1468     dec2 = 0;
1469
1470     /* For numbers below the requested resolution, work out where
1471        the decimal point will be rather than finding it in the string */
1472     if (number < 1.0 && number > 0.0) {
1473         dec2 = log10(number + 1e-10);
1474         if (-dec2 <= ndigits) dec2 = 0;
1475     }
1476
1477     /* If requested digits is zero or less, we will need to truncate
1478      * the returned string */
1479     if (ndigits < 1) {
1480         stop = strlen(buf) + ndigits;
1481     } else {
1482         stop = strlen(buf);
1483     }
1484
1485     while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1486     while (*ptr1 != '\0' && *ptr1 != '.') {
1487         if (!first) first = ptr2;
1488         if ((ptr1 - buf) < stop) {
1489             *ptr2++ = *ptr1++;
1490         } else {
1491             ptr1++;
1492         }
1493         dec1++;
1494     }
1495
1496     if (ndigits > 0) {
1497         ptr1++;
1498         if (!first) {
1499             while (*ptr1 == '0') { /* Process leading zeroes */
1500                 *ptr2++ = *ptr1++;
1501                 dec1--;
1502             }
1503         }
1504         while (*ptr1 != '\0') {
1505             if (!first) first = ptr2;
1506             *ptr2++ = *ptr1++;
1507         }
1508     }
1509
1510     *ptr2 = '\0';
1511
1512     /* We never found a non-zero digit, then our number is either
1513      * smaller than the requested precision, or 0.0 */
1514     if (!first) {
1515         if (number > 0.0) {
1516             first = ptr2;
1517         } else {
1518             first = data->efcvt_buffer;
1519             dec1 = 0;
1520         }
1521     }
1522
1523     *decpt = dec2 ? dec2 : dec1;
1524     return first;
1525 }
1526
1527 /***********************************************************************
1528  *              _fcvt_s  (MSVCRT.@)
1529  */
1530 int CDECL _fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
1531 {
1532     int stop, dec1, dec2;
1533     char *ptr1, *ptr2, *first;
1534     char buf[80]; /* ought to be enough */
1535
1536     if (!outbuffer || !decpt || !sign || size == 0)
1537     {
1538         *MSVCRT__errno() = MSVCRT_EINVAL;
1539         return MSVCRT_EINVAL;
1540     }
1541
1542     if (number < 0)
1543     {
1544         *sign = 1;
1545         number = -number;
1546     } else *sign = 0;
1547
1548     snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1549     ptr1 = buf;
1550     ptr2 = outbuffer;
1551     first = NULL;
1552     dec1 = 0;
1553     dec2 = 0;
1554
1555     /* For numbers below the requested resolution, work out where
1556        the decimal point will be rather than finding it in the string */
1557     if (number < 1.0 && number > 0.0) {
1558         dec2 = log10(number + 1e-10);
1559         if (-dec2 <= ndigits) dec2 = 0;
1560     }
1561
1562     /* If requested digits is zero or less, we will need to truncate
1563      * the returned string */
1564     if (ndigits < 1) {
1565         stop = strlen(buf) + ndigits;
1566     } else {
1567         stop = strlen(buf);
1568     }
1569
1570     while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1571     while (*ptr1 != '\0' && *ptr1 != '.') {
1572         if (!first) first = ptr2;
1573         if ((ptr1 - buf) < stop) {
1574             if (size > 1) {
1575                 *ptr2++ = *ptr1++;
1576                 size--;
1577             }
1578         } else {
1579             ptr1++;
1580         }
1581         dec1++;
1582     }
1583
1584     if (ndigits > 0) {
1585         ptr1++;
1586         if (!first) {
1587             while (*ptr1 == '0') { /* Process leading zeroes */
1588                 if (number == 0.0 && size > 1) {
1589                     *ptr2++ = '0';
1590                     size--;
1591                 }
1592                 ptr1++;
1593                 dec1--;
1594             }
1595         }
1596         while (*ptr1 != '\0') {
1597             if (!first) first = ptr2;
1598             if (size > 1) {
1599                 *ptr2++ = *ptr1++;
1600                 size--;
1601             }
1602         }
1603     }
1604
1605     *ptr2 = '\0';
1606
1607     /* We never found a non-zero digit, then our number is either
1608      * smaller than the requested precision, or 0.0 */
1609     if (!first && (number <= 0.0))
1610         dec1 = 0;
1611
1612     *decpt = dec2 ? dec2 : dec1;
1613     return 0;
1614 }
1615
1616 /***********************************************************************
1617  *              _gcvt  (MSVCRT.@)
1618  */
1619 char * CDECL _gcvt( double number, int ndigit, char *buff )
1620 {
1621     if(!buff) {
1622         *MSVCRT__errno() = MSVCRT_EINVAL;
1623         return NULL;
1624     }
1625
1626     if(ndigit < 0) {
1627         *MSVCRT__errno() = MSVCRT_ERANGE;
1628         return NULL;
1629     }
1630
1631     MSVCRT_sprintf(buff, "%.*g", ndigit, number);
1632     return buff;
1633 }
1634
1635 /***********************************************************************
1636  *              _gcvt_s  (MSVCRT.@)
1637  */
1638 int CDECL _gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
1639 {
1640     int len;
1641
1642     if(!buff) {
1643         *MSVCRT__errno() = MSVCRT_EINVAL;
1644         return MSVCRT_EINVAL;
1645     }
1646
1647     if( digits<0 || digits>=size) {
1648         if(size)
1649             buff[0] = '\0';
1650
1651         *MSVCRT__errno() = MSVCRT_ERANGE;
1652         return MSVCRT_ERANGE;
1653     }
1654
1655     len = MSVCRT__scprintf("%.*g", digits, number);
1656     if(len > size) {
1657         buff[0] = '\0';
1658         *MSVCRT__errno() = MSVCRT_ERANGE;
1659         return MSVCRT_ERANGE;
1660     }
1661
1662     MSVCRT_sprintf(buff, "%.*g", digits, number);
1663     return 0;
1664 }
1665
1666 #include <stdlib.h> /* div_t, ldiv_t */
1667
1668 /*********************************************************************
1669  *              div (MSVCRT.@)
1670  * VERSION
1671  *      [i386] Windows binary compatible - returns the struct in eax/edx.
1672  */
1673 #ifdef __i386__
1674 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1675 {
1676   div_t dt = div(num,denom);
1677   return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1678 }
1679 #else
1680 /*********************************************************************
1681  *              div (MSVCRT.@)
1682  * VERSION
1683  *      [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1684  */
1685 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1686 {
1687   div_t dt = div(num,denom);
1688   MSVCRT_div_t     ret;
1689   ret.quot = dt.quot;
1690   ret.rem = dt.rem;
1691
1692   return ret;
1693
1694 }
1695 #endif /* ifdef __i386__ */
1696
1697
1698 /*********************************************************************
1699  *              ldiv (MSVCRT.@)
1700  * VERSION
1701  *      [i386] Windows binary compatible - returns the struct in eax/edx.
1702  */
1703 #ifdef __i386__
1704 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1705 {
1706   ldiv_t ldt = ldiv(num,denom);
1707   return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1708 }
1709 #else
1710 /*********************************************************************
1711  *              ldiv (MSVCRT.@)
1712  * VERSION
1713  *      [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1714  */
1715 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1716 {
1717   ldiv_t result = ldiv(num,denom);
1718
1719   MSVCRT_ldiv_t ret;
1720   ret.quot = result.quot;
1721   ret.rem = result.rem;
1722
1723   return ret;
1724 }
1725 #endif /* ifdef __i386__ */
1726
1727 #ifdef __i386__
1728
1729 /*********************************************************************
1730  *              _adjust_fdiv (MSVCRT.@)
1731  * Used by the MSVC compiler to work around the Pentium FDIV bug.
1732  */
1733 int MSVCRT__adjust_fdiv = 0;
1734
1735 /***********************************************************************
1736  *              _adj_fdiv_m16i (MSVCRT.@)
1737  *
1738  * NOTE
1739  *    I _think_ this function is intended to work around the Pentium
1740  *    fdiv bug.
1741  */
1742 void __stdcall _adj_fdiv_m16i( short arg )
1743 {
1744   TRACE("(): stub\n");
1745 }
1746
1747 /***********************************************************************
1748  *              _adj_fdiv_m32 (MSVCRT.@)
1749  *
1750  * NOTE
1751  *    I _think_ this function is intended to work around the Pentium
1752  *    fdiv bug.
1753  */
1754 void __stdcall _adj_fdiv_m32( unsigned int arg )
1755 {
1756   TRACE("(): stub\n");
1757 }
1758
1759 /***********************************************************************
1760  *              _adj_fdiv_m32i (MSVCRT.@)
1761  *
1762  * NOTE
1763  *    I _think_ this function is intended to work around the Pentium
1764  *    fdiv bug.
1765  */
1766 void __stdcall _adj_fdiv_m32i( int arg )
1767 {
1768   TRACE("(): stub\n");
1769 }
1770
1771 /***********************************************************************
1772  *              _adj_fdiv_m64 (MSVCRT.@)
1773  *
1774  * NOTE
1775  *    I _think_ this function is intended to work around the Pentium
1776  *    fdiv bug.
1777  */
1778 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1779 {
1780   TRACE("(): stub\n");
1781 }
1782
1783 /***********************************************************************
1784  *              _adj_fdiv_r (MSVCRT.@)
1785  * FIXME
1786  *    This function is likely to have the wrong number of arguments.
1787  *
1788  * NOTE
1789  *    I _think_ this function is intended to work around the Pentium
1790  *    fdiv bug.
1791  */
1792 void _adj_fdiv_r(void)
1793 {
1794   TRACE("(): stub\n");
1795 }
1796
1797 /***********************************************************************
1798  *              _adj_fdivr_m16i (MSVCRT.@)
1799  *
1800  * NOTE
1801  *    I _think_ this function is intended to work around the Pentium
1802  *    fdiv bug.
1803  */
1804 void __stdcall _adj_fdivr_m16i( short arg )
1805 {
1806   TRACE("(): stub\n");
1807 }
1808
1809 /***********************************************************************
1810  *              _adj_fdivr_m32 (MSVCRT.@)
1811  *
1812  * NOTE
1813  *    I _think_ this function is intended to work around the Pentium
1814  *    fdiv bug.
1815  */
1816 void __stdcall _adj_fdivr_m32( unsigned int arg )
1817 {
1818   TRACE("(): stub\n");
1819 }
1820
1821 /***********************************************************************
1822  *              _adj_fdivr_m32i (MSVCRT.@)
1823  *
1824  * NOTE
1825  *    I _think_ this function is intended to work around the Pentium
1826  *    fdiv bug.
1827  */
1828 void __stdcall _adj_fdivr_m32i( int arg )
1829 {
1830   TRACE("(): stub\n");
1831 }
1832
1833 /***********************************************************************
1834  *              _adj_fdivr_m64 (MSVCRT.@)
1835  *
1836  * NOTE
1837  *    I _think_ this function is intended to work around the Pentium
1838  *    fdiv bug.
1839  */
1840 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1841 {
1842   TRACE("(): stub\n");
1843 }
1844
1845 /***********************************************************************
1846  *              _adj_fpatan (MSVCRT.@)
1847  * FIXME
1848  *    This function is likely to have the wrong number of arguments.
1849  *
1850  * NOTE
1851  *    I _think_ this function is intended to work around the Pentium
1852  *    fdiv bug.
1853  */
1854 void _adj_fpatan(void)
1855 {
1856   TRACE("(): stub\n");
1857 }
1858
1859 /***********************************************************************
1860  *              _adj_fprem (MSVCRT.@)
1861  * FIXME
1862  *    This function is likely to have the wrong number of arguments.
1863  *
1864  * NOTE
1865  *    I _think_ this function is intended to work around the Pentium
1866  *    fdiv bug.
1867  */
1868 void _adj_fprem(void)
1869 {
1870   TRACE("(): stub\n");
1871 }
1872
1873 /***********************************************************************
1874  *              _adj_fprem1 (MSVCRT.@)
1875  * FIXME
1876  *    This function is likely to have the wrong number of arguments.
1877  *
1878  * NOTE
1879  *    I _think_ this function is intended to work around the Pentium
1880  *    fdiv bug.
1881  */
1882 void _adj_fprem1(void)
1883 {
1884   TRACE("(): stub\n");
1885 }
1886
1887 /***********************************************************************
1888  *              _adj_fptan (MSVCRT.@)
1889  * FIXME
1890  *    This function is likely to have the wrong number of arguments.
1891  *
1892  * NOTE
1893  *    I _think_ this function is intended to work around the Pentium
1894  *    fdiv bug.
1895  */
1896 void _adj_fptan(void)
1897 {
1898   TRACE("(): stub\n");
1899 }
1900
1901 /***********************************************************************
1902  *              _safe_fdiv (MSVCRT.@)
1903  * FIXME
1904  *    This function is likely to have the wrong number of arguments.
1905  *
1906  * NOTE
1907  *    I _think_ this function is intended to work around the Pentium
1908  *    fdiv bug.
1909  */
1910 void _safe_fdiv(void)
1911 {
1912   TRACE("(): stub\n");
1913 }
1914
1915 /***********************************************************************
1916  *              _safe_fdivr (MSVCRT.@)
1917  * FIXME
1918  *    This function is likely to have the wrong number of arguments.
1919  *
1920  * NOTE
1921  *    I _think_ this function is intended to work around the Pentium
1922  *    fdiv bug.
1923  */
1924 void _safe_fdivr(void)
1925 {
1926   TRACE("(): stub\n");
1927 }
1928
1929 /***********************************************************************
1930  *              _safe_fprem (MSVCRT.@)
1931  * FIXME
1932  *    This function is likely to have the wrong number of arguments.
1933  *
1934  * NOTE
1935  *    I _think_ this function is intended to work around the Pentium
1936  *    fdiv bug.
1937  */
1938 void _safe_fprem(void)
1939 {
1940   TRACE("(): stub\n");
1941 }
1942
1943 /***********************************************************************
1944  *              _safe_fprem1 (MSVCRT.@)
1945  *
1946  * FIXME
1947  *    This function is likely to have the wrong number of arguments.
1948  *
1949  * NOTE
1950  *    I _think_ this function is intended to work around the Pentium
1951  *    fdiv bug.
1952  */
1953 void _safe_fprem1(void)
1954 {
1955   TRACE("(): stub\n");
1956 }
1957
1958 /***********************************************************************
1959  *              __libm_sse2_acos   (MSVCRT.@)
1960  */
1961 void __cdecl __libm_sse2_acos(void)
1962 {
1963     double d;
1964     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1965     d = acos( d );
1966     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1967 }
1968
1969 /***********************************************************************
1970  *              __libm_sse2_acosf   (MSVCRT.@)
1971  */
1972 void __cdecl __libm_sse2_acosf(void)
1973 {
1974     float f;
1975     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
1976     f = acosf( f );
1977     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
1978 }
1979
1980 /***********************************************************************
1981  *              __libm_sse2_asin   (MSVCRT.@)
1982  */
1983 void __cdecl __libm_sse2_asin(void)
1984 {
1985     double d;
1986     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1987     d = asin( d );
1988     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1989 }
1990
1991 /***********************************************************************
1992  *              __libm_sse2_asinf   (MSVCRT.@)
1993  */
1994 void __cdecl __libm_sse2_asinf(void)
1995 {
1996     float f;
1997     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
1998     f = asinf( f );
1999     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2000 }
2001
2002 /***********************************************************************
2003  *              __libm_sse2_atan   (MSVCRT.@)
2004  */
2005 void __cdecl __libm_sse2_atan(void)
2006 {
2007     double d;
2008     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2009     d = atan( d );
2010     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2011 }
2012
2013 /***********************************************************************
2014  *              __libm_sse2_atan2   (MSVCRT.@)
2015  */
2016 void __cdecl __libm_sse2_atan2(void)
2017 {
2018     double d1, d2;
2019     __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2020     d1 = atan2( d1, d2 );
2021     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2022 }
2023
2024 /***********************************************************************
2025  *              __libm_sse2_atanf   (MSVCRT.@)
2026  */
2027 void __cdecl __libm_sse2_atanf(void)
2028 {
2029     float f;
2030     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2031     f = atanf( f );
2032     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2033 }
2034
2035 /***********************************************************************
2036  *              __libm_sse2_cos   (MSVCRT.@)
2037  */
2038 void __cdecl __libm_sse2_cos(void)
2039 {
2040     double d;
2041     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2042     d = cos( d );
2043     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2044 }
2045
2046 /***********************************************************************
2047  *              __libm_sse2_cosf   (MSVCRT.@)
2048  */
2049 void __cdecl __libm_sse2_cosf(void)
2050 {
2051     float f;
2052     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2053     f = cosf( f );
2054     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2055 }
2056
2057 /***********************************************************************
2058  *              __libm_sse2_exp   (MSVCRT.@)
2059  */
2060 void __cdecl __libm_sse2_exp(void)
2061 {
2062     double d;
2063     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2064     d = exp( d );
2065     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2066 }
2067
2068 /***********************************************************************
2069  *              __libm_sse2_expf   (MSVCRT.@)
2070  */
2071 void __cdecl __libm_sse2_expf(void)
2072 {
2073     float f;
2074     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2075     f = expf( f );
2076     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2077 }
2078
2079 /***********************************************************************
2080  *              __libm_sse2_log   (MSVCRT.@)
2081  */
2082 void __cdecl __libm_sse2_log(void)
2083 {
2084     double d;
2085     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2086     d = log( d );
2087     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2088 }
2089
2090 /***********************************************************************
2091  *              __libm_sse2_log10   (MSVCRT.@)
2092  */
2093 void __cdecl __libm_sse2_log10(void)
2094 {
2095     double d;
2096     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2097     d = log10( d );
2098     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2099 }
2100
2101 /***********************************************************************
2102  *              __libm_sse2_log10f   (MSVCRT.@)
2103  */
2104 void __cdecl __libm_sse2_log10f(void)
2105 {
2106     float f;
2107     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2108     f = log10f( f );
2109     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2110 }
2111
2112 /***********************************************************************
2113  *              __libm_sse2_logf   (MSVCRT.@)
2114  */
2115 void __cdecl __libm_sse2_logf(void)
2116 {
2117     float f;
2118     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2119     f = logf( f );
2120     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2121 }
2122
2123 /***********************************************************************
2124  *              __libm_sse2_pow   (MSVCRT.@)
2125  */
2126 void __cdecl __libm_sse2_pow(void)
2127 {
2128     double d1, d2;
2129     __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2130     d1 = pow( d1, d2 );
2131     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2132 }
2133
2134 /***********************************************************************
2135  *              __libm_sse2_powf   (MSVCRT.@)
2136  */
2137 void __cdecl __libm_sse2_powf(void)
2138 {
2139     float f1, f2;
2140     __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
2141     f1 = powf( f1, f2 );
2142     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
2143 }
2144
2145 /***********************************************************************
2146  *              __libm_sse2_sin   (MSVCRT.@)
2147  */
2148 void __cdecl __libm_sse2_sin(void)
2149 {
2150     double d;
2151     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2152     d = sin( d );
2153     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2154 }
2155
2156 /***********************************************************************
2157  *              __libm_sse2_sinf   (MSVCRT.@)
2158  */
2159 void __cdecl __libm_sse2_sinf(void)
2160 {
2161     float f;
2162     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2163     f = sinf( f );
2164     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2165 }
2166
2167 /***********************************************************************
2168  *              __libm_sse2_tan   (MSVCRT.@)
2169  */
2170 void __cdecl __libm_sse2_tan(void)
2171 {
2172     double d;
2173     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2174     d = tan( d );
2175     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2176 }
2177
2178 /***********************************************************************
2179  *              __libm_sse2_tanf   (MSVCRT.@)
2180  */
2181 void __cdecl __libm_sse2_tanf(void)
2182 {
2183     float f;
2184     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2185     f = tanf( f );
2186     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2187 }
2188
2189 #endif  /* __i386__ */