msvcrt/tests: Execute c++ related tests on 64-bit systems.
[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 #endif /* defined(__GNUC__) && defined(__i386__) */
633
634 /*********************************************************************
635  *              _fpclass (MSVCRT.@)
636  */
637 int CDECL MSVCRT__fpclass(double num)
638 {
639 #if defined(HAVE_FPCLASS) || defined(fpclass)
640   switch (fpclass( num ))
641   {
642 #ifdef FP_SNAN
643   case FP_SNAN:  return MSVCRT__FPCLASS_SNAN;
644 #endif
645 #ifdef FP_QNAN
646   case FP_QNAN:  return MSVCRT__FPCLASS_QNAN;
647 #endif
648 #ifdef FP_NINF
649   case FP_NINF:  return MSVCRT__FPCLASS_NINF;
650 #endif
651 #ifdef FP_PINF
652   case FP_PINF:  return MSVCRT__FPCLASS_PINF;
653 #endif
654 #ifdef FP_NDENORM
655   case FP_NDENORM: return MSVCRT__FPCLASS_ND;
656 #endif
657 #ifdef FP_PDENORM
658   case FP_PDENORM: return MSVCRT__FPCLASS_PD;
659 #endif
660 #ifdef FP_NZERO
661   case FP_NZERO: return MSVCRT__FPCLASS_NZ;
662 #endif
663 #ifdef FP_PZERO
664   case FP_PZERO: return MSVCRT__FPCLASS_PZ;
665 #endif
666 #ifdef FP_NNORM
667   case FP_NNORM: return MSVCRT__FPCLASS_NN;
668 #endif
669 #ifdef FP_PNORM
670   case FP_PNORM: return MSVCRT__FPCLASS_PN;
671 #endif
672   default: return MSVCRT__FPCLASS_PN;
673   }
674 #elif defined (fpclassify)
675   switch (fpclassify( num ))
676   {
677   case FP_NAN: return MSVCRT__FPCLASS_QNAN;
678   case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
679   case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
680   case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
681   }
682   return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
683 #else
684   if (!finite(num))
685     return MSVCRT__FPCLASS_QNAN;
686   return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
687 #endif
688 }
689
690 /*********************************************************************
691  *              _rotl (MSVCRT.@)
692  */
693 unsigned int CDECL _rotl(unsigned int num, int shift)
694 {
695   shift &= 31;
696   return (num << shift) | (num >> (32-shift));
697 }
698
699 /*********************************************************************
700  *              _lrotl (MSVCRT.@)
701  */
702 MSVCRT_ulong CDECL MSVCRT__lrotl(MSVCRT_ulong num, int shift)
703 {
704   shift &= 0x1f;
705   return (num << shift) | (num >> (32-shift));
706 }
707
708 /*********************************************************************
709  *              _lrotr (MSVCRT.@)
710  */
711 MSVCRT_ulong CDECL MSVCRT__lrotr(MSVCRT_ulong num, int shift)
712 {
713   shift &= 0x1f;
714   return (num >> shift) | (num << (32-shift));
715 }
716
717 /*********************************************************************
718  *              _rotr (MSVCRT.@)
719  */
720 unsigned int CDECL _rotr(unsigned int num, int shift)
721 {
722     shift &= 0x1f;
723     return (num >> shift) | (num << (32-shift));
724 }
725
726 /*********************************************************************
727  *              _rotl64 (MSVCRT.@)
728  */
729 unsigned __int64 CDECL _rotl64(unsigned __int64 num, int shift)
730 {
731   shift &= 63;
732   return (num << shift) | (num >> (64-shift));
733 }
734
735 /*********************************************************************
736  *              _rotr64 (MSVCRT.@)
737  */
738 unsigned __int64 CDECL _rotr64(unsigned __int64 num, int shift)
739 {
740     shift &= 63;
741     return (num >> shift) | (num << (64-shift));
742 }
743
744 /*********************************************************************
745  *              abs (MSVCRT.@)
746  */
747 int CDECL MSVCRT_abs( int n )
748 {
749     return n >= 0 ? n : -n;
750 }
751
752 /*********************************************************************
753  *              labs (MSVCRT.@)
754  */
755 MSVCRT_long CDECL MSVCRT_labs( MSVCRT_long n )
756 {
757     return n >= 0 ? n : -n;
758 }
759
760 /*********************************************************************
761  *              _abs64 (MSVCRT.@)
762  */
763 __int64 CDECL _abs64( __int64 n )
764 {
765     return n >= 0 ? n : -n;
766 }
767
768 /*********************************************************************
769  *              _logb (MSVCRT.@)
770  */
771 double CDECL MSVCRT__logb(double num)
772 {
773   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
774   return logb(num);
775 }
776
777 /*********************************************************************
778  *              _scalb (MSVCRT.@)
779  */
780 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
781 {
782   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
783   return ldexp(num, power);
784 }
785
786 /*********************************************************************
787  *              _hypot (MSVCRT.@)
788  */
789 double CDECL _hypot(double x, double y)
790 {
791   /* FIXME: errno handling */
792   return hypot( x, y );
793 }
794
795 /*********************************************************************
796  *      _hypotf (MSVCRT.@)
797  */
798 float CDECL MSVCRT__hypotf(float x, float y)
799 {
800   /* FIXME: errno handling */
801   return hypotf( x, y );
802 }
803
804 /*********************************************************************
805  *              ceil (MSVCRT.@)
806  */
807 double CDECL MSVCRT_ceil( double x )
808 {
809   return ceil(x);
810 }
811
812 /*********************************************************************
813  *              floor (MSVCRT.@)
814  */
815 double CDECL MSVCRT_floor( double x )
816 {
817   return floor(x);
818 }
819
820 /*********************************************************************
821  *              fabs (MSVCRT.@)
822  */
823 double CDECL MSVCRT_fabs( double x )
824 {
825   return fabs(x);
826 }
827
828 /*********************************************************************
829  *              frexp (MSVCRT.@)
830  */
831 double CDECL MSVCRT_frexp( double x, int *exp )
832 {
833   return frexp( x, exp );
834 }
835
836 /*********************************************************************
837  *              modf (MSVCRT.@)
838  */
839 double CDECL MSVCRT_modf( double x, double *iptr )
840 {
841   return modf( x, iptr );
842 }
843
844 /*********************************************************************
845  *              _matherr (MSVCRT.@)
846  */
847 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
848 {
849   if (e)
850     TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
851           e->retval);
852   else
853     TRACE("(null)\n");
854   if (MSVCRT_default_matherr_func)
855     return MSVCRT_default_matherr_func(e);
856   ERR(":Unhandled math error!\n");
857   return 0;
858 }
859
860 /*********************************************************************
861  *              __setusermatherr (MSVCRT.@)
862  */
863 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
864 {
865   MSVCRT_default_matherr_func = func;
866   TRACE(":new matherr handler %p\n", func);
867 }
868
869 /**********************************************************************
870  *              _statusfp2 (MSVCRT.@)
871  *
872  * Not exported by native msvcrt, added in msvcr80.
873  */
874 #if defined(__i386__) || defined(__x86_64__)
875 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
876 {
877 #ifdef __GNUC__
878     unsigned int flags;
879     unsigned long fpword;
880
881     if (x86_sw)
882     {
883         __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
884         flags = 0;
885         if (fpword & 0x1)  flags |= MSVCRT__SW_INVALID;
886         if (fpword & 0x2)  flags |= MSVCRT__SW_DENORMAL;
887         if (fpword & 0x4)  flags |= MSVCRT__SW_ZERODIVIDE;
888         if (fpword & 0x8)  flags |= MSVCRT__SW_OVERFLOW;
889         if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
890         if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
891         *x86_sw = flags;
892     }
893
894     if (!sse2_sw) return;
895
896     if (sse2_supported)
897     {
898         __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
899         flags = 0;
900         if (fpword & 0x1)  flags |= MSVCRT__SW_INVALID;
901         if (fpword & 0x2)  flags |= MSVCRT__SW_DENORMAL;
902         if (fpword & 0x4)  flags |= MSVCRT__SW_ZERODIVIDE;
903         if (fpword & 0x8)  flags |= MSVCRT__SW_OVERFLOW;
904         if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
905         if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
906         *sse2_sw = flags;
907     }
908     else *sse2_sw = 0;
909 #else
910     FIXME( "not implemented\n" );
911 #endif
912 }
913 #endif
914
915 /**********************************************************************
916  *              _statusfp (MSVCRT.@)
917  */
918 unsigned int CDECL _statusfp(void)
919 {
920 #if defined(__i386__) || defined(__x86_64__)
921     unsigned int x86_sw, sse2_sw;
922
923     _statusfp2( &x86_sw, &sse2_sw );
924     /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
925     return x86_sw | sse2_sw;
926 #else
927     FIXME( "not implemented\n" );
928     return 0;
929 #endif
930 }
931
932 /*********************************************************************
933  *              _clearfp (MSVCRT.@)
934  */
935 unsigned int CDECL _clearfp(void)
936 {
937     unsigned int flags = 0;
938 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
939     unsigned long fpword;
940
941     __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
942     if (fpword & 0x1)  flags |= MSVCRT__SW_INVALID;
943     if (fpword & 0x2)  flags |= MSVCRT__SW_DENORMAL;
944     if (fpword & 0x4)  flags |= MSVCRT__SW_ZERODIVIDE;
945     if (fpword & 0x8)  flags |= MSVCRT__SW_OVERFLOW;
946     if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
947     if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
948
949     if (sse2_supported)
950     {
951         __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
952         if (fpword & 0x1)  flags |= MSVCRT__SW_INVALID;
953         if (fpword & 0x2)  flags |= MSVCRT__SW_DENORMAL;
954         if (fpword & 0x4)  flags |= MSVCRT__SW_ZERODIVIDE;
955         if (fpword & 0x8)  flags |= MSVCRT__SW_OVERFLOW;
956         if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
957         if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
958         fpword &= ~0x3f;
959         __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
960     }
961 #else
962     FIXME( "not implemented\n" );
963 #endif
964     return flags;
965 }
966
967 /*********************************************************************
968  *              __fpecode (MSVCRT.@)
969  */
970 int * CDECL __fpecode(void)
971 {
972     return &msvcrt_get_thread_data()->fpecode;
973 }
974
975 /*********************************************************************
976  *              ldexp (MSVCRT.@)
977  */
978 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
979 {
980   double z = ldexp(num,exp);
981
982   if (!finite(z))
983     *MSVCRT__errno() = MSVCRT_ERANGE;
984   else if (z == 0 && signbit(z))
985     z = 0.0; /* Convert -0 -> +0 */
986   return z;
987 }
988
989 /*********************************************************************
990  *              _cabs (MSVCRT.@)
991  */
992 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
993 {
994   return sqrt(num.x * num.x + num.y * num.y);
995 }
996
997 /*********************************************************************
998  *              _chgsign (MSVCRT.@)
999  */
1000 double CDECL MSVCRT__chgsign(double num)
1001 {
1002   /* FIXME: +-infinity,Nan not tested */
1003   return -num;
1004 }
1005
1006 /*********************************************************************
1007  *              __control87_2 (MSVCRT.@)
1008  *
1009  * Not exported by native msvcrt, added in msvcr80.
1010  */
1011 #if defined(__i386__) || defined(__x86_64__)
1012 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1013                          unsigned int *x86_cw, unsigned int *sse2_cw )
1014 {
1015 #ifdef __GNUC__
1016     unsigned long fpword;
1017     unsigned int flags;
1018
1019     if (x86_cw)
1020     {
1021         __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1022
1023         /* Convert into mask constants */
1024         flags = 0;
1025         if (fpword & 0x1)  flags |= MSVCRT__EM_INVALID;
1026         if (fpword & 0x2)  flags |= MSVCRT__EM_DENORMAL;
1027         if (fpword & 0x4)  flags |= MSVCRT__EM_ZERODIVIDE;
1028         if (fpword & 0x8)  flags |= MSVCRT__EM_OVERFLOW;
1029         if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1030         if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1031         switch (fpword & 0xc00)
1032         {
1033         case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1034         case 0x800: flags |= MSVCRT__RC_UP; break;
1035         case 0x400: flags |= MSVCRT__RC_DOWN; break;
1036         }
1037         switch (fpword & 0x300)
1038         {
1039         case 0x0:   flags |= MSVCRT__PC_24; break;
1040         case 0x200: flags |= MSVCRT__PC_53; break;
1041         case 0x300: flags |= MSVCRT__PC_64; break;
1042         }
1043         if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1044
1045         TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1046         if (mask)
1047         {
1048             flags = (flags & ~mask) | (newval & mask);
1049
1050             /* Convert (masked) value back to fp word */
1051             fpword = 0;
1052             if (flags & MSVCRT__EM_INVALID)    fpword |= 0x1;
1053             if (flags & MSVCRT__EM_DENORMAL)   fpword |= 0x2;
1054             if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1055             if (flags & MSVCRT__EM_OVERFLOW)   fpword |= 0x8;
1056             if (flags & MSVCRT__EM_UNDERFLOW)  fpword |= 0x10;
1057             if (flags & MSVCRT__EM_INEXACT)    fpword |= 0x20;
1058             switch (flags & MSVCRT__MCW_RC)
1059             {
1060             case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1061             case MSVCRT__RC_UP:                 fpword |= 0x800; break;
1062             case MSVCRT__RC_DOWN:               fpword |= 0x400; break;
1063             }
1064             switch (flags & MSVCRT__MCW_PC)
1065             {
1066             case MSVCRT__PC_64: fpword |= 0x300; break;
1067             case MSVCRT__PC_53: fpword |= 0x200; break;
1068             case MSVCRT__PC_24: fpword |= 0x0; break;
1069             }
1070             if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1071
1072             __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1073         }
1074         *x86_cw = flags;
1075     }
1076
1077     if (!sse2_cw) return 1;
1078
1079     if (sse2_supported)
1080     {
1081         __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1082
1083         /* Convert into mask constants */
1084         flags = 0;
1085         if (fpword & 0x80)   flags |= MSVCRT__EM_INVALID;
1086         if (fpword & 0x100)  flags |= MSVCRT__EM_DENORMAL;
1087         if (fpword & 0x200)  flags |= MSVCRT__EM_ZERODIVIDE;
1088         if (fpword & 0x400)  flags |= MSVCRT__EM_OVERFLOW;
1089         if (fpword & 0x800)  flags |= MSVCRT__EM_UNDERFLOW;
1090         if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1091         switch (fpword & 0x6000)
1092         {
1093         case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1094         case 0x4000: flags |= MSVCRT__RC_UP; break;
1095         case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1096         }
1097         switch (fpword & 0x8040)
1098         {
1099         case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1100         case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1101         case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1102         }
1103
1104         TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1105         if (mask)
1106         {
1107             flags = (flags & ~mask) | (newval & mask);
1108
1109             /* Convert (masked) value back to fp word */
1110             fpword = 0;
1111             if (flags & MSVCRT__EM_INVALID)    fpword |= 0x80;
1112             if (flags & MSVCRT__EM_DENORMAL)   fpword |= 0x100;
1113             if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1114             if (flags & MSVCRT__EM_OVERFLOW)   fpword |= 0x400;
1115             if (flags & MSVCRT__EM_UNDERFLOW)  fpword |= 0x800;
1116             if (flags & MSVCRT__EM_INEXACT)    fpword |= 0x1000;
1117             switch (flags & MSVCRT__MCW_RC)
1118             {
1119             case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1120             case MSVCRT__RC_UP:                 fpword |= 0x4000; break;
1121             case MSVCRT__RC_DOWN:               fpword |= 0x2000; break;
1122             }
1123             switch (flags & MSVCRT__MCW_DN)
1124             {
1125             case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1126             case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1127             case MSVCRT__DN_FLUSH:                       fpword |= 0x8040; break;
1128             }
1129             __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1130         }
1131         *sse2_cw = flags;
1132     }
1133     else *sse2_cw = 0;
1134
1135     return 1;
1136 #else
1137     FIXME( "not implemented\n" );
1138     return 0;
1139 #endif
1140 }
1141 #endif
1142
1143 /*********************************************************************
1144  *              _control87 (MSVCRT.@)
1145  */
1146 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1147 {
1148 #if defined(__i386__) || defined(__x86_64__)
1149     unsigned int x86_cw, sse2_cw;
1150
1151     __control87_2( newval, mask, &x86_cw, &sse2_cw );
1152
1153     if ((x86_cw ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) x86_cw |= MSVCRT__EM_AMBIGUOUS;
1154     return x86_cw;
1155 #else
1156     FIXME( "not implemented\n" );
1157     return 0;
1158 #endif
1159 }
1160
1161 /*********************************************************************
1162  *              _controlfp (MSVCRT.@)
1163  */
1164 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
1165 {
1166   return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
1167 }
1168
1169 /*********************************************************************
1170  *              _set_controlfp (MSVCRT.@)
1171  */
1172 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
1173 {
1174     _controlfp( newval, mask );
1175 }
1176
1177 /*********************************************************************
1178  *              _controlfp_s (MSVCRT.@)
1179  */
1180 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
1181 {
1182     static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
1183                                            MSVCRT__MCW_PC | MSVCRT__MCW_DN);
1184     unsigned int val;
1185
1186     if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
1187     {
1188         if (cur) *cur = _controlfp( 0, 0 );  /* retrieve it anyway */
1189         return MSVCRT_EINVAL;
1190     }
1191     val = _controlfp( newval, mask );
1192     if (cur) *cur = val;
1193     return 0;
1194 }
1195
1196 /*********************************************************************
1197  *              _copysign (MSVCRT.@)
1198  */
1199 double CDECL MSVCRT__copysign(double num, double sign)
1200 {
1201   /* FIXME: Behaviour for Nan/Inf? */
1202   if (sign < 0.0)
1203     return num < 0.0 ? num : -num;
1204   return num < 0.0 ? -num : num;
1205 }
1206
1207 /*********************************************************************
1208  *              _finite (MSVCRT.@)
1209  */
1210 int CDECL MSVCRT__finite(double num)
1211 {
1212   return (finite(num)?1:0); /* See comment for _isnan() */
1213 }
1214
1215 /*********************************************************************
1216  *              _fpreset (MSVCRT.@)
1217  */
1218 void CDECL _fpreset(void)
1219 {
1220 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1221     const unsigned int x86_cw = 0x27f;
1222     __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
1223     if (sse2_supported)
1224     {
1225         const unsigned long sse2_cw = 0x1f80;
1226         __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
1227     }
1228 #else
1229     FIXME( "not implemented\n" );
1230 #endif
1231 }
1232
1233 /*********************************************************************
1234  *              _isnan (MSVCRT.@)
1235  */
1236 INT CDECL MSVCRT__isnan(double num)
1237 {
1238   /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1239    * Do the same, as the result may be used in calculations
1240    */
1241   return isnan(num) ? 1 : 0;
1242 }
1243
1244 /*********************************************************************
1245  *              _j0 (MSVCRT.@)
1246  */
1247 double CDECL MSVCRT__j0(double num)
1248 {
1249   /* FIXME: errno handling */
1250   return j0(num);
1251 }
1252
1253 /*********************************************************************
1254  *              _j1 (MSVCRT.@)
1255  */
1256 double CDECL MSVCRT__j1(double num)
1257 {
1258   /* FIXME: errno handling */
1259   return j1(num);
1260 }
1261
1262 /*********************************************************************
1263  *              _jn (MSVCRT.@)
1264  */
1265 double CDECL MSVCRT__jn(int n, double num)
1266 {
1267   /* FIXME: errno handling */
1268   return jn(n, num);
1269 }
1270
1271 /*********************************************************************
1272  *              _y0 (MSVCRT.@)
1273  */
1274 double CDECL MSVCRT__y0(double num)
1275 {
1276   double retval;
1277   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1278   retval  = y0(num);
1279   if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1280   {
1281     *MSVCRT__errno() = MSVCRT_EDOM;
1282     retval = sqrt(-1);
1283   }
1284   return retval;
1285 }
1286
1287 /*********************************************************************
1288  *              _y1 (MSVCRT.@)
1289  */
1290 double CDECL MSVCRT__y1(double num)
1291 {
1292   double retval;
1293   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1294   retval  = y1(num);
1295   if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1296   {
1297     *MSVCRT__errno() = MSVCRT_EDOM;
1298     retval = sqrt(-1);
1299   }
1300   return retval;
1301 }
1302
1303 /*********************************************************************
1304  *              _yn (MSVCRT.@)
1305  */
1306 double CDECL MSVCRT__yn(int order, double num)
1307 {
1308   double retval;
1309   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1310   retval  = yn(order,num);
1311   if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1312   {
1313     *MSVCRT__errno() = MSVCRT_EDOM;
1314     retval = sqrt(-1);
1315   }
1316   return retval;
1317 }
1318
1319 /*********************************************************************
1320  *              _nextafter (MSVCRT.@)
1321  */
1322 double CDECL MSVCRT__nextafter(double num, double next)
1323 {
1324   double retval;
1325   if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1326   retval = nextafter(num,next);
1327   return retval;
1328 }
1329
1330 /*********************************************************************
1331  *              _ecvt (MSVCRT.@)
1332  */
1333 char * CDECL _ecvt( double number, int ndigits, int *decpt, int *sign )
1334 {
1335     int prec, len;
1336     thread_data_t *data = msvcrt_get_thread_data();
1337     /* FIXME: check better for overflow (native supports over 300 chars) */
1338     ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1339                                       * 4 for exponent and one for
1340                                       * terminating '\0' */
1341     if (!data->efcvt_buffer)
1342         data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1343
1344     if( number < 0) {
1345         *sign = TRUE;
1346         number = -number;
1347     } else
1348         *sign = FALSE;
1349     /* handle cases with zero ndigits or less */
1350     prec = ndigits;
1351     if( prec < 1) prec = 2;
1352     len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1353     /* take the decimal "point away */
1354     if( prec != 1)
1355         memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1356     /* take the exponential "e" out */
1357     data->efcvt_buffer[ prec] = '\0';
1358     /* read the exponent */
1359     sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1360     (*decpt)++;
1361     /* adjust for some border cases */
1362     if( data->efcvt_buffer[0] == '0')/* value is zero */
1363         *decpt = 0;
1364     /* handle cases with zero ndigits or less */
1365     if( ndigits < 1){
1366         if( data->efcvt_buffer[ 0] >= '5')
1367             (*decpt)++;
1368         data->efcvt_buffer[ 0] = '\0';
1369     }
1370     TRACE("out=\"%s\"\n",data->efcvt_buffer);
1371     return data->efcvt_buffer;
1372 }
1373
1374 /*********************************************************************
1375  *              _ecvt_s (MSVCRT.@)
1376  */
1377 int CDECL _ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
1378 {
1379     int prec, len;
1380     char *result;
1381     const char infret[] = "1#INF";
1382
1383     if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
1384     if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
1385     if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
1386     if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1387     if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1388
1389     /* special case - inf */
1390     if(number == HUGE_VAL || number == -HUGE_VAL)
1391     {
1392         memset(buffer, '0', ndigits);
1393         memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
1394         buffer[ndigits] = '\0';
1395         (*decpt) = 1;
1396         if(number == -HUGE_VAL)
1397             (*sign) = 1;
1398         else
1399             (*sign) = 0;
1400         return 0;
1401     }
1402     /* handle cases with zero ndigits or less */
1403     prec = ndigits;
1404     if( prec < 1) prec = 2;
1405     result = MSVCRT_malloc(prec + 7);
1406
1407     if( number < 0) {
1408         *sign = TRUE;
1409         number = -number;
1410     } else
1411         *sign = FALSE;
1412     len = snprintf(result, prec + 7, "%.*le", prec - 1, number);
1413     /* take the decimal "point away */
1414     if( prec != 1)
1415         memmove( result + 1, result + 2, len - 1 );
1416     /* take the exponential "e" out */
1417     result[ prec] = '\0';
1418     /* read the exponent */
1419     sscanf( result + prec + 1, "%d", decpt);
1420     (*decpt)++;
1421     /* adjust for some border cases */
1422     if( result[0] == '0')/* value is zero */
1423         *decpt = 0;
1424     /* handle cases with zero ndigits or less */
1425     if( ndigits < 1){
1426         if( result[ 0] >= '5')
1427             (*decpt)++;
1428         result[ 0] = '\0';
1429     }
1430     memcpy( buffer, result, max(ndigits + 1, 1) );
1431     MSVCRT_free( result );
1432     return 0;
1433 }
1434
1435 /***********************************************************************
1436  *              _fcvt  (MSVCRT.@)
1437  */
1438 char * CDECL _fcvt( double number, int ndigits, int *decpt, int *sign )
1439 {
1440     thread_data_t *data = msvcrt_get_thread_data();
1441     int stop, dec1, dec2;
1442     char *ptr1, *ptr2, *first;
1443     char buf[80]; /* ought to be enough */
1444
1445     if (!data->efcvt_buffer)
1446         data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1447
1448     if (number < 0)
1449     {
1450         *sign = 1;
1451         number = -number;
1452     } else *sign = 0;
1453
1454     snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1455     ptr1 = buf;
1456     ptr2 = data->efcvt_buffer;
1457     first = NULL;
1458     dec1 = 0;
1459     dec2 = 0;
1460
1461     /* For numbers below the requested resolution, work out where
1462        the decimal point will be rather than finding it in the string */
1463     if (number < 1.0 && number > 0.0) {
1464         dec2 = log10(number + 1e-10);
1465         if (-dec2 <= ndigits) dec2 = 0;
1466     }
1467
1468     /* If requested digits is zero or less, we will need to truncate
1469      * the returned string */
1470     if (ndigits < 1) {
1471         stop = strlen(buf) + ndigits;
1472     } else {
1473         stop = strlen(buf);
1474     }
1475
1476     while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1477     while (*ptr1 != '\0' && *ptr1 != '.') {
1478         if (!first) first = ptr2;
1479         if ((ptr1 - buf) < stop) {
1480             *ptr2++ = *ptr1++;
1481         } else {
1482             ptr1++;
1483         }
1484         dec1++;
1485     }
1486
1487     if (ndigits > 0) {
1488         ptr1++;
1489         if (!first) {
1490             while (*ptr1 == '0') { /* Process leading zeroes */
1491                 *ptr2++ = *ptr1++;
1492                 dec1--;
1493             }
1494         }
1495         while (*ptr1 != '\0') {
1496             if (!first) first = ptr2;
1497             *ptr2++ = *ptr1++;
1498         }
1499     }
1500
1501     *ptr2 = '\0';
1502
1503     /* We never found a non-zero digit, then our number is either
1504      * smaller than the requested precision, or 0.0 */
1505     if (!first) {
1506         if (number > 0.0) {
1507             first = ptr2;
1508         } else {
1509             first = data->efcvt_buffer;
1510             dec1 = 0;
1511         }
1512     }
1513
1514     *decpt = dec2 ? dec2 : dec1;
1515     return first;
1516 }
1517
1518 /***********************************************************************
1519  *              _fcvt_s  (MSVCRT.@)
1520  */
1521 int CDECL _fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
1522 {
1523     int stop, dec1, dec2;
1524     char *ptr1, *ptr2, *first;
1525     char buf[80]; /* ought to be enough */
1526
1527     if (!outbuffer || !decpt || !sign || size == 0)
1528     {
1529         *MSVCRT__errno() = MSVCRT_EINVAL;
1530         return MSVCRT_EINVAL;
1531     }
1532
1533     if (number < 0)
1534     {
1535         *sign = 1;
1536         number = -number;
1537     } else *sign = 0;
1538
1539     snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1540     ptr1 = buf;
1541     ptr2 = outbuffer;
1542     first = NULL;
1543     dec1 = 0;
1544     dec2 = 0;
1545
1546     /* For numbers below the requested resolution, work out where
1547        the decimal point will be rather than finding it in the string */
1548     if (number < 1.0 && number > 0.0) {
1549         dec2 = log10(number + 1e-10);
1550         if (-dec2 <= ndigits) dec2 = 0;
1551     }
1552
1553     /* If requested digits is zero or less, we will need to truncate
1554      * the returned string */
1555     if (ndigits < 1) {
1556         stop = strlen(buf) + ndigits;
1557     } else {
1558         stop = strlen(buf);
1559     }
1560
1561     while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1562     while (*ptr1 != '\0' && *ptr1 != '.') {
1563         if (!first) first = ptr2;
1564         if ((ptr1 - buf) < stop) {
1565             if (size > 1) {
1566                 *ptr2++ = *ptr1++;
1567                 size--;
1568             }
1569         } else {
1570             ptr1++;
1571         }
1572         dec1++;
1573     }
1574
1575     if (ndigits > 0) {
1576         ptr1++;
1577         if (!first) {
1578             while (*ptr1 == '0') { /* Process leading zeroes */
1579                 if (number == 0.0 && size > 1) {
1580                     *ptr2++ = '0';
1581                     size--;
1582                 }
1583                 ptr1++;
1584                 dec1--;
1585             }
1586         }
1587         while (*ptr1 != '\0') {
1588             if (!first) first = ptr2;
1589             if (size > 1) {
1590                 *ptr2++ = *ptr1++;
1591                 size--;
1592             }
1593         }
1594     }
1595
1596     *ptr2 = '\0';
1597
1598     /* We never found a non-zero digit, then our number is either
1599      * smaller than the requested precision, or 0.0 */
1600     if (!first && (number <= 0.0))
1601         dec1 = 0;
1602
1603     *decpt = dec2 ? dec2 : dec1;
1604     return 0;
1605 }
1606
1607 /***********************************************************************
1608  *              _gcvt  (MSVCRT.@)
1609  */
1610 char * CDECL _gcvt( double number, int ndigit, char *buff )
1611 {
1612     if(!buff) {
1613         *MSVCRT__errno() = MSVCRT_EINVAL;
1614         return NULL;
1615     }
1616
1617     if(ndigit < 0) {
1618         *MSVCRT__errno() = MSVCRT_ERANGE;
1619         return NULL;
1620     }
1621
1622     MSVCRT_sprintf(buff, "%.*g", ndigit, number);
1623     return buff;
1624 }
1625
1626 /***********************************************************************
1627  *              _gcvt_s  (MSVCRT.@)
1628  */
1629 int CDECL _gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
1630 {
1631     int len;
1632
1633     if(!buff) {
1634         *MSVCRT__errno() = MSVCRT_EINVAL;
1635         return MSVCRT_EINVAL;
1636     }
1637
1638     if( digits<0 || digits>=size) {
1639         if(size)
1640             buff[0] = '\0';
1641
1642         *MSVCRT__errno() = MSVCRT_ERANGE;
1643         return MSVCRT_ERANGE;
1644     }
1645
1646     len = MSVCRT__scprintf("%.*g", digits, number);
1647     if(len > size) {
1648         buff[0] = '\0';
1649         *MSVCRT__errno() = MSVCRT_ERANGE;
1650         return MSVCRT_ERANGE;
1651     }
1652
1653     MSVCRT_sprintf(buff, "%.*g", digits, number);
1654     return 0;
1655 }
1656
1657 #include <stdlib.h> /* div_t, ldiv_t */
1658
1659 /*********************************************************************
1660  *              div (MSVCRT.@)
1661  * VERSION
1662  *      [i386] Windows binary compatible - returns the struct in eax/edx.
1663  */
1664 #ifdef __i386__
1665 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1666 {
1667   div_t dt = div(num,denom);
1668   return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1669 }
1670 #else
1671 /*********************************************************************
1672  *              div (MSVCRT.@)
1673  * VERSION
1674  *      [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1675  */
1676 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1677 {
1678   div_t dt = div(num,denom);
1679   MSVCRT_div_t     ret;
1680   ret.quot = dt.quot;
1681   ret.rem = dt.rem;
1682
1683   return ret;
1684
1685 }
1686 #endif /* ifdef __i386__ */
1687
1688
1689 /*********************************************************************
1690  *              ldiv (MSVCRT.@)
1691  * VERSION
1692  *      [i386] Windows binary compatible - returns the struct in eax/edx.
1693  */
1694 #ifdef __i386__
1695 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1696 {
1697   ldiv_t ldt = ldiv(num,denom);
1698   return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1699 }
1700 #else
1701 /*********************************************************************
1702  *              ldiv (MSVCRT.@)
1703  * VERSION
1704  *      [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1705  */
1706 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1707 {
1708   ldiv_t result = ldiv(num,denom);
1709
1710   MSVCRT_ldiv_t ret;
1711   ret.quot = result.quot;
1712   ret.rem = result.rem;
1713
1714   return ret;
1715 }
1716 #endif /* ifdef __i386__ */
1717
1718 #ifdef __i386__
1719
1720 /*********************************************************************
1721  *              _adjust_fdiv (MSVCRT.@)
1722  * Used by the MSVC compiler to work around the Pentium FDIV bug.
1723  */
1724 int MSVCRT__adjust_fdiv = 0;
1725
1726 /***********************************************************************
1727  *              _adj_fdiv_m16i (MSVCRT.@)
1728  *
1729  * NOTE
1730  *    I _think_ this function is intended to work around the Pentium
1731  *    fdiv bug.
1732  */
1733 void __stdcall _adj_fdiv_m16i( short arg )
1734 {
1735   TRACE("(): stub\n");
1736 }
1737
1738 /***********************************************************************
1739  *              _adj_fdiv_m32 (MSVCRT.@)
1740  *
1741  * NOTE
1742  *    I _think_ this function is intended to work around the Pentium
1743  *    fdiv bug.
1744  */
1745 void __stdcall _adj_fdiv_m32( unsigned int arg )
1746 {
1747   TRACE("(): stub\n");
1748 }
1749
1750 /***********************************************************************
1751  *              _adj_fdiv_m32i (MSVCRT.@)
1752  *
1753  * NOTE
1754  *    I _think_ this function is intended to work around the Pentium
1755  *    fdiv bug.
1756  */
1757 void __stdcall _adj_fdiv_m32i( int arg )
1758 {
1759   TRACE("(): stub\n");
1760 }
1761
1762 /***********************************************************************
1763  *              _adj_fdiv_m64 (MSVCRT.@)
1764  *
1765  * NOTE
1766  *    I _think_ this function is intended to work around the Pentium
1767  *    fdiv bug.
1768  */
1769 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1770 {
1771   TRACE("(): stub\n");
1772 }
1773
1774 /***********************************************************************
1775  *              _adj_fdiv_r (MSVCRT.@)
1776  * FIXME
1777  *    This function is likely to have the wrong number of arguments.
1778  *
1779  * NOTE
1780  *    I _think_ this function is intended to work around the Pentium
1781  *    fdiv bug.
1782  */
1783 void _adj_fdiv_r(void)
1784 {
1785   TRACE("(): stub\n");
1786 }
1787
1788 /***********************************************************************
1789  *              _adj_fdivr_m16i (MSVCRT.@)
1790  *
1791  * NOTE
1792  *    I _think_ this function is intended to work around the Pentium
1793  *    fdiv bug.
1794  */
1795 void __stdcall _adj_fdivr_m16i( short arg )
1796 {
1797   TRACE("(): stub\n");
1798 }
1799
1800 /***********************************************************************
1801  *              _adj_fdivr_m32 (MSVCRT.@)
1802  *
1803  * NOTE
1804  *    I _think_ this function is intended to work around the Pentium
1805  *    fdiv bug.
1806  */
1807 void __stdcall _adj_fdivr_m32( unsigned int arg )
1808 {
1809   TRACE("(): stub\n");
1810 }
1811
1812 /***********************************************************************
1813  *              _adj_fdivr_m32i (MSVCRT.@)
1814  *
1815  * NOTE
1816  *    I _think_ this function is intended to work around the Pentium
1817  *    fdiv bug.
1818  */
1819 void __stdcall _adj_fdivr_m32i( int arg )
1820 {
1821   TRACE("(): stub\n");
1822 }
1823
1824 /***********************************************************************
1825  *              _adj_fdivr_m64 (MSVCRT.@)
1826  *
1827  * NOTE
1828  *    I _think_ this function is intended to work around the Pentium
1829  *    fdiv bug.
1830  */
1831 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1832 {
1833   TRACE("(): stub\n");
1834 }
1835
1836 /***********************************************************************
1837  *              _adj_fpatan (MSVCRT.@)
1838  * FIXME
1839  *    This function is likely to have the wrong number of arguments.
1840  *
1841  * NOTE
1842  *    I _think_ this function is intended to work around the Pentium
1843  *    fdiv bug.
1844  */
1845 void _adj_fpatan(void)
1846 {
1847   TRACE("(): stub\n");
1848 }
1849
1850 /***********************************************************************
1851  *              _adj_fprem (MSVCRT.@)
1852  * FIXME
1853  *    This function is likely to have the wrong number of arguments.
1854  *
1855  * NOTE
1856  *    I _think_ this function is intended to work around the Pentium
1857  *    fdiv bug.
1858  */
1859 void _adj_fprem(void)
1860 {
1861   TRACE("(): stub\n");
1862 }
1863
1864 /***********************************************************************
1865  *              _adj_fprem1 (MSVCRT.@)
1866  * FIXME
1867  *    This function is likely to have the wrong number of arguments.
1868  *
1869  * NOTE
1870  *    I _think_ this function is intended to work around the Pentium
1871  *    fdiv bug.
1872  */
1873 void _adj_fprem1(void)
1874 {
1875   TRACE("(): stub\n");
1876 }
1877
1878 /***********************************************************************
1879  *              _adj_fptan (MSVCRT.@)
1880  * FIXME
1881  *    This function is likely to have the wrong number of arguments.
1882  *
1883  * NOTE
1884  *    I _think_ this function is intended to work around the Pentium
1885  *    fdiv bug.
1886  */
1887 void _adj_fptan(void)
1888 {
1889   TRACE("(): stub\n");
1890 }
1891
1892 /***********************************************************************
1893  *              _safe_fdiv (MSVCRT.@)
1894  * FIXME
1895  *    This function is likely to have the wrong number of arguments.
1896  *
1897  * NOTE
1898  *    I _think_ this function is intended to work around the Pentium
1899  *    fdiv bug.
1900  */
1901 void _safe_fdiv(void)
1902 {
1903   TRACE("(): stub\n");
1904 }
1905
1906 /***********************************************************************
1907  *              _safe_fdivr (MSVCRT.@)
1908  * FIXME
1909  *    This function is likely to have the wrong number of arguments.
1910  *
1911  * NOTE
1912  *    I _think_ this function is intended to work around the Pentium
1913  *    fdiv bug.
1914  */
1915 void _safe_fdivr(void)
1916 {
1917   TRACE("(): stub\n");
1918 }
1919
1920 /***********************************************************************
1921  *              _safe_fprem (MSVCRT.@)
1922  * FIXME
1923  *    This function is likely to have the wrong number of arguments.
1924  *
1925  * NOTE
1926  *    I _think_ this function is intended to work around the Pentium
1927  *    fdiv bug.
1928  */
1929 void _safe_fprem(void)
1930 {
1931   TRACE("(): stub\n");
1932 }
1933
1934 /***********************************************************************
1935  *              _safe_fprem1 (MSVCRT.@)
1936  *
1937  * FIXME
1938  *    This function is likely to have the wrong number of arguments.
1939  *
1940  * NOTE
1941  *    I _think_ this function is intended to work around the Pentium
1942  *    fdiv bug.
1943  */
1944 void _safe_fprem1(void)
1945 {
1946   TRACE("(): stub\n");
1947 }
1948
1949 /***********************************************************************
1950  *              __libm_sse2_acos   (MSVCRT.@)
1951  */
1952 void __cdecl __libm_sse2_acos(void)
1953 {
1954     double d;
1955     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1956     d = acos( d );
1957     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1958 }
1959
1960 /***********************************************************************
1961  *              __libm_sse2_acosf   (MSVCRT.@)
1962  */
1963 void __cdecl __libm_sse2_acosf(void)
1964 {
1965     float f;
1966     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
1967     f = acosf( f );
1968     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
1969 }
1970
1971 /***********************************************************************
1972  *              __libm_sse2_asin   (MSVCRT.@)
1973  */
1974 void __cdecl __libm_sse2_asin(void)
1975 {
1976     double d;
1977     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1978     d = asin( d );
1979     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1980 }
1981
1982 /***********************************************************************
1983  *              __libm_sse2_asinf   (MSVCRT.@)
1984  */
1985 void __cdecl __libm_sse2_asinf(void)
1986 {
1987     float f;
1988     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
1989     f = asinf( f );
1990     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
1991 }
1992
1993 /***********************************************************************
1994  *              __libm_sse2_atan   (MSVCRT.@)
1995  */
1996 void __cdecl __libm_sse2_atan(void)
1997 {
1998     double d;
1999     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2000     d = atan( d );
2001     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2002 }
2003
2004 /***********************************************************************
2005  *              __libm_sse2_atan2   (MSVCRT.@)
2006  */
2007 void __cdecl __libm_sse2_atan2(void)
2008 {
2009     double d1, d2;
2010     __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2011     d1 = atan2( d1, d2 );
2012     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2013 }
2014
2015 /***********************************************************************
2016  *              __libm_sse2_atanf   (MSVCRT.@)
2017  */
2018 void __cdecl __libm_sse2_atanf(void)
2019 {
2020     float f;
2021     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2022     f = atanf( f );
2023     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2024 }
2025
2026 /***********************************************************************
2027  *              __libm_sse2_cos   (MSVCRT.@)
2028  */
2029 void __cdecl __libm_sse2_cos(void)
2030 {
2031     double d;
2032     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2033     d = cos( d );
2034     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2035 }
2036
2037 /***********************************************************************
2038  *              __libm_sse2_cosf   (MSVCRT.@)
2039  */
2040 void __cdecl __libm_sse2_cosf(void)
2041 {
2042     float f;
2043     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2044     f = cosf( f );
2045     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2046 }
2047
2048 /***********************************************************************
2049  *              __libm_sse2_exp   (MSVCRT.@)
2050  */
2051 void __cdecl __libm_sse2_exp(void)
2052 {
2053     double d;
2054     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2055     d = exp( d );
2056     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2057 }
2058
2059 /***********************************************************************
2060  *              __libm_sse2_expf   (MSVCRT.@)
2061  */
2062 void __cdecl __libm_sse2_expf(void)
2063 {
2064     float f;
2065     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2066     f = expf( f );
2067     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2068 }
2069
2070 /***********************************************************************
2071  *              __libm_sse2_log   (MSVCRT.@)
2072  */
2073 void __cdecl __libm_sse2_log(void)
2074 {
2075     double d;
2076     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2077     d = log( d );
2078     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2079 }
2080
2081 /***********************************************************************
2082  *              __libm_sse2_log10   (MSVCRT.@)
2083  */
2084 void __cdecl __libm_sse2_log10(void)
2085 {
2086     double d;
2087     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2088     d = log10( d );
2089     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2090 }
2091
2092 /***********************************************************************
2093  *              __libm_sse2_log10f   (MSVCRT.@)
2094  */
2095 void __cdecl __libm_sse2_log10f(void)
2096 {
2097     float f;
2098     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2099     f = log10f( f );
2100     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2101 }
2102
2103 /***********************************************************************
2104  *              __libm_sse2_logf   (MSVCRT.@)
2105  */
2106 void __cdecl __libm_sse2_logf(void)
2107 {
2108     float f;
2109     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2110     f = logf( f );
2111     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2112 }
2113
2114 /***********************************************************************
2115  *              __libm_sse2_pow   (MSVCRT.@)
2116  */
2117 void __cdecl __libm_sse2_pow(void)
2118 {
2119     double d1, d2;
2120     __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2121     d1 = pow( d1, d2 );
2122     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2123 }
2124
2125 /***********************************************************************
2126  *              __libm_sse2_powf   (MSVCRT.@)
2127  */
2128 void __cdecl __libm_sse2_powf(void)
2129 {
2130     float f1, f2;
2131     __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
2132     f1 = powf( f1, f2 );
2133     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
2134 }
2135
2136 /***********************************************************************
2137  *              __libm_sse2_sin   (MSVCRT.@)
2138  */
2139 void __cdecl __libm_sse2_sin(void)
2140 {
2141     double d;
2142     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2143     d = sin( d );
2144     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2145 }
2146
2147 /***********************************************************************
2148  *              __libm_sse2_sinf   (MSVCRT.@)
2149  */
2150 void __cdecl __libm_sse2_sinf(void)
2151 {
2152     float f;
2153     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2154     f = sinf( f );
2155     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2156 }
2157
2158 /***********************************************************************
2159  *              __libm_sse2_tan   (MSVCRT.@)
2160  */
2161 void __cdecl __libm_sse2_tan(void)
2162 {
2163     double d;
2164     __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2165     d = tan( d );
2166     __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2167 }
2168
2169 /***********************************************************************
2170  *              __libm_sse2_tanf   (MSVCRT.@)
2171  */
2172 void __cdecl __libm_sse2_tanf(void)
2173 {
2174     float f;
2175     __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2176     f = tanf( f );
2177     __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2178 }
2179
2180 #endif  /* __i386__ */