msi: Check transform validation flags.
[wine/multimedia.git] / dlls / msvcrt / math.c
blobd67b2e8a34ad5fb40193450a17d77005bd218d90
1 /*
2 * msvcrt.dll math functions
4 * Copyright 2000 Jon Griffiths
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.
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.
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
20 #include "config.h"
21 #include "wine/port.h"
23 #include <stdio.h>
24 #define __USE_ISOC9X 1
25 #define __USE_ISOC99 1
26 #include <math.h>
27 #ifdef HAVE_IEEEFP_H
28 #include <ieeefp.h>
29 #endif
31 #include "msvcrt.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 #ifndef signbit
38 #define signbit(x) 0
39 #endif
41 typedef int (CDECL *MSVCRT_matherr_func)(struct MSVCRT__exception *);
43 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
45 static BOOL sse2_supported;
46 static BOOL sse2_enabled;
48 void msvcrt_init_math(void)
50 sse2_supported = sse2_enabled = IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );
53 /*********************************************************************
54 * _set_SSE2_enable (MSVCRT.@)
56 int CDECL MSVCRT__set_SSE2_enable(int flag)
58 sse2_enabled = flag && sse2_supported;
59 return sse2_enabled;
62 #if defined(__x86_64__) || defined(__arm__)
64 /*********************************************************************
65 * _chgsignf (MSVCRT.@)
67 float CDECL MSVCRT__chgsignf( float num )
69 /* FIXME: +-infinity,Nan not tested */
70 return -num;
73 /*********************************************************************
74 * _copysignf (MSVCRT.@)
76 float CDECL MSVCRT__copysignf( float num, float sign )
78 /* FIXME: Behaviour for Nan/Inf? */
79 if (sign < 0.0)
80 return num < 0.0 ? num : -num;
81 return num < 0.0 ? -num : num;
84 /*********************************************************************
85 * _finitef (MSVCRT.@)
87 int CDECL MSVCRT__finitef( float num )
89 return finitef(num) != 0; /* See comment for _isnan() */
92 /*********************************************************************
93 * _isnanf (MSVCRT.@)
95 INT CDECL MSVCRT__isnanf( float num )
97 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
98 * Do the same, as the result may be used in calculations
100 return isnanf(num) != 0;
103 /*********************************************************************
104 * _logbf (MSVCRT.@)
106 float CDECL MSVCRT__logbf( float num )
108 if (!finitef(num)) *MSVCRT__errno() = MSVCRT_EDOM;
109 return logbf(num);
112 /*********************************************************************
113 * _nextafterf (MSVCRT.@)
115 float CDECL MSVCRT__nextafterf( float num, float next )
117 if (!finitef(num) || !finitef(next)) *MSVCRT__errno() = MSVCRT_EDOM;
118 return nextafterf( num, next );
121 /*********************************************************************
122 * MSVCRT_acosf (MSVCRT.@)
124 float CDECL MSVCRT_acosf( float x )
126 if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
127 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
128 * asin() uses a similar construction. This is bad because as x gets nearer to
129 * 1 the error in the expression "1 - x^2" can get relatively large due to
130 * cancellation. The sqrt() makes things worse. A safer way to calculate
131 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
132 return atan2f(sqrtf((1 - x) * (1 + x)), x);
135 /*********************************************************************
136 * MSVCRT_asinf (MSVCRT.@)
138 float CDECL MSVCRT_asinf( float x )
140 if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
141 return atan2f(x, sqrtf((1 - x) * (1 + x)));
144 /*********************************************************************
145 * MSVCRT_atanf (MSVCRT.@)
147 float CDECL MSVCRT_atanf( float x )
149 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
150 return atanf(x);
153 /*********************************************************************
154 * MSVCRT_atan2f (MSVCRT.@)
156 float CDECL MSVCRT_atan2f( float x, float y )
158 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
159 return atan2f(x,y);
162 /*********************************************************************
163 * MSVCRT_cosf (MSVCRT.@)
165 float CDECL MSVCRT_cosf( float x )
167 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
168 return cosf(x);
171 /*********************************************************************
172 * MSVCRT_coshf (MSVCRT.@)
174 float CDECL MSVCRT_coshf( float x )
176 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
177 return coshf(x);
180 /*********************************************************************
181 * MSVCRT_expf (MSVCRT.@)
183 float CDECL MSVCRT_expf( float x )
185 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
186 return expf(x);
189 /*********************************************************************
190 * MSVCRT_fmodf (MSVCRT.@)
192 float CDECL MSVCRT_fmodf( float x, float y )
194 if (!finitef(x) || !finitef(y)) *MSVCRT__errno() = MSVCRT_EDOM;
195 return fmodf(x,y);
198 /*********************************************************************
199 * MSVCRT_logf (MSVCRT.@)
201 float CDECL MSVCRT_logf( float x)
203 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
204 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
205 return logf(x);
208 /*********************************************************************
209 * MSVCRT_log10f (MSVCRT.@)
211 float CDECL MSVCRT_log10f( float x )
213 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
214 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
215 return log10f(x);
218 /*********************************************************************
219 * MSVCRT_powf (MSVCRT.@)
221 float CDECL MSVCRT_powf( float x, float y )
223 /* FIXME: If x < 0 and y is not integral, set EDOM */
224 float z = powf(x,y);
225 if (!finitef(z)) *MSVCRT__errno() = MSVCRT_EDOM;
226 return z;
229 /*********************************************************************
230 * MSVCRT_sinf (MSVCRT.@)
232 float CDECL MSVCRT_sinf( float x )
234 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
235 return sinf(x);
238 /*********************************************************************
239 * MSVCRT_sinhf (MSVCRT.@)
241 float CDECL MSVCRT_sinhf( float x )
243 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
244 return sinhf(x);
247 /*********************************************************************
248 * MSVCRT_sqrtf (MSVCRT.@)
250 float CDECL MSVCRT_sqrtf( float x )
252 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
253 return sqrtf(x);
256 /*********************************************************************
257 * MSVCRT_tanf (MSVCRT.@)
259 float CDECL MSVCRT_tanf( float x )
261 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
262 return tanf(x);
265 /*********************************************************************
266 * MSVCRT_tanhf (MSVCRT.@)
268 float CDECL MSVCRT_tanhf( float x )
270 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
271 return tanhf(x);
274 /*********************************************************************
275 * ceilf (MSVCRT.@)
277 float CDECL MSVCRT_ceilf( float x )
279 return ceilf(x);
282 /*********************************************************************
283 * fabsf (MSVCRT.@)
285 float CDECL MSVCRT_fabsf( float x )
287 return fabsf(x);
290 /*********************************************************************
291 * floorf (MSVCRT.@)
293 float CDECL MSVCRT_floorf( float x )
295 return floorf(x);
298 /*********************************************************************
299 * frexpf (MSVCRT.@)
301 float CDECL MSVCRT_frexpf( float x, int *exp )
303 return frexpf( x, exp );
306 /*********************************************************************
307 * _scalbf (MSVCRT.@)
309 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
311 if (!finitef(num)) *MSVCRT__errno() = MSVCRT_EDOM;
312 return ldexpf(num, power);
315 /*********************************************************************
316 * modff (MSVCRT.@)
318 double CDECL MSVCRT_modff( float x, float *iptr )
320 return modff( x, iptr );
323 #endif
325 /*********************************************************************
326 * MSVCRT_acos (MSVCRT.@)
328 double CDECL MSVCRT_acos( double x )
330 if (x < -1.0 || x > 1.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
331 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
332 * asin() uses a similar construction. This is bad because as x gets nearer to
333 * 1 the error in the expression "1 - x^2" can get relatively large due to
334 * cancellation. The sqrt() makes things worse. A safer way to calculate
335 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
336 return atan2(sqrt((1 - x) * (1 + x)), x);
339 /*********************************************************************
340 * MSVCRT_asin (MSVCRT.@)
342 double CDECL MSVCRT_asin( double x )
344 if (x < -1.0 || x > 1.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
345 return atan2(x, sqrt((1 - x) * (1 + x)));
348 /*********************************************************************
349 * MSVCRT_atan (MSVCRT.@)
351 double CDECL MSVCRT_atan( double x )
353 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
354 return atan(x);
357 /*********************************************************************
358 * MSVCRT_atan2 (MSVCRT.@)
360 double CDECL MSVCRT_atan2( double x, double y )
362 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
363 return atan2(x,y);
366 /*********************************************************************
367 * MSVCRT_cos (MSVCRT.@)
369 double CDECL MSVCRT_cos( double x )
371 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
372 return cos(x);
375 /*********************************************************************
376 * MSVCRT_cosh (MSVCRT.@)
378 double CDECL MSVCRT_cosh( double x )
380 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
381 return cosh(x);
384 /*********************************************************************
385 * MSVCRT_exp (MSVCRT.@)
387 double CDECL MSVCRT_exp( double x )
389 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
390 return exp(x);
393 /*********************************************************************
394 * MSVCRT_fmod (MSVCRT.@)
396 double CDECL MSVCRT_fmod( double x, double y )
398 if (!isfinite(x) || !isfinite(y)) *MSVCRT__errno() = MSVCRT_EDOM;
399 return fmod(x,y);
402 /*********************************************************************
403 * MSVCRT_log (MSVCRT.@)
405 double CDECL MSVCRT_log( double x)
407 if (x < 0.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
408 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
409 return log(x);
412 /*********************************************************************
413 * MSVCRT_log10 (MSVCRT.@)
415 double CDECL MSVCRT_log10( double x )
417 if (x < 0.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
418 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
419 return log10(x);
422 /*********************************************************************
423 * MSVCRT_pow (MSVCRT.@)
425 double CDECL MSVCRT_pow( double x, double y )
427 /* FIXME: If x < 0 and y is not integral, set EDOM */
428 double z = pow(x,y);
429 if (!isfinite(z)) *MSVCRT__errno() = MSVCRT_EDOM;
430 return z;
433 /*********************************************************************
434 * MSVCRT_sin (MSVCRT.@)
436 double CDECL MSVCRT_sin( double x )
438 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
439 return sin(x);
442 /*********************************************************************
443 * MSVCRT_sinh (MSVCRT.@)
445 double CDECL MSVCRT_sinh( double x )
447 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
448 return sinh(x);
451 /*********************************************************************
452 * MSVCRT_sqrt (MSVCRT.@)
454 double CDECL MSVCRT_sqrt( double x )
456 if (x < 0.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
457 return sqrt(x);
460 /*********************************************************************
461 * MSVCRT_tan (MSVCRT.@)
463 double CDECL MSVCRT_tan( double x )
465 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
466 return tan(x);
469 /*********************************************************************
470 * MSVCRT_tanh (MSVCRT.@)
472 double CDECL MSVCRT_tanh( double x )
474 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
475 return tanh(x);
479 #if defined(__GNUC__) && defined(__i386__)
481 #define FPU_DOUBLE(var) double var; \
482 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
483 #define FPU_DOUBLES(var1,var2) double var1,var2; \
484 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
485 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
487 /*********************************************************************
488 * _CIacos (MSVCRT.@)
490 double CDECL _CIacos(void)
492 FPU_DOUBLE(x);
493 return MSVCRT_acos(x);
496 /*********************************************************************
497 * _CIasin (MSVCRT.@)
499 double CDECL _CIasin(void)
501 FPU_DOUBLE(x);
502 return MSVCRT_asin(x);
505 /*********************************************************************
506 * _CIatan (MSVCRT.@)
508 double CDECL _CIatan(void)
510 FPU_DOUBLE(x);
511 return MSVCRT_atan(x);
514 /*********************************************************************
515 * _CIatan2 (MSVCRT.@)
517 double CDECL _CIatan2(void)
519 FPU_DOUBLES(x,y);
520 return MSVCRT_atan2(x,y);
523 /*********************************************************************
524 * _CIcos (MSVCRT.@)
526 double CDECL _CIcos(void)
528 FPU_DOUBLE(x);
529 return MSVCRT_cos(x);
532 /*********************************************************************
533 * _CIcosh (MSVCRT.@)
535 double CDECL _CIcosh(void)
537 FPU_DOUBLE(x);
538 return MSVCRT_cosh(x);
541 /*********************************************************************
542 * _CIexp (MSVCRT.@)
544 double CDECL _CIexp(void)
546 FPU_DOUBLE(x);
547 return MSVCRT_exp(x);
550 /*********************************************************************
551 * _CIfmod (MSVCRT.@)
553 double CDECL _CIfmod(void)
555 FPU_DOUBLES(x,y);
556 return MSVCRT_fmod(x,y);
559 /*********************************************************************
560 * _CIlog (MSVCRT.@)
562 double CDECL _CIlog(void)
564 FPU_DOUBLE(x);
565 return MSVCRT_log(x);
568 /*********************************************************************
569 * _CIlog10 (MSVCRT.@)
571 double CDECL _CIlog10(void)
573 FPU_DOUBLE(x);
574 return MSVCRT_log10(x);
577 /*********************************************************************
578 * _CIpow (MSVCRT.@)
580 double CDECL _CIpow(void)
582 FPU_DOUBLES(x,y);
583 return MSVCRT_pow(x,y);
586 /*********************************************************************
587 * _CIsin (MSVCRT.@)
589 double CDECL _CIsin(void)
591 FPU_DOUBLE(x);
592 return MSVCRT_sin(x);
595 /*********************************************************************
596 * _CIsinh (MSVCRT.@)
598 double CDECL _CIsinh(void)
600 FPU_DOUBLE(x);
601 return MSVCRT_sinh(x);
604 /*********************************************************************
605 * _CIsqrt (MSVCRT.@)
607 double CDECL _CIsqrt(void)
609 FPU_DOUBLE(x);
610 return MSVCRT_sqrt(x);
613 /*********************************************************************
614 * _CItan (MSVCRT.@)
616 double CDECL _CItan(void)
618 FPU_DOUBLE(x);
619 return MSVCRT_tan(x);
622 /*********************************************************************
623 * _CItanh (MSVCRT.@)
625 double CDECL _CItanh(void)
627 FPU_DOUBLE(x);
628 return MSVCRT_tanh(x);
631 /*********************************************************************
632 * _ftol (MSVCRT.@)
634 LONGLONG CDECL MSVCRT__ftol(void)
636 FPU_DOUBLE(x);
637 return (LONGLONG)x;
640 #endif /* defined(__GNUC__) && defined(__i386__) */
642 /*********************************************************************
643 * _fpclass (MSVCRT.@)
645 int CDECL MSVCRT__fpclass(double num)
647 #if defined(HAVE_FPCLASS) || defined(fpclass)
648 switch (fpclass( num ))
650 #ifdef FP_SNAN
651 case FP_SNAN: return MSVCRT__FPCLASS_SNAN;
652 #endif
653 #ifdef FP_QNAN
654 case FP_QNAN: return MSVCRT__FPCLASS_QNAN;
655 #endif
656 #ifdef FP_NINF
657 case FP_NINF: return MSVCRT__FPCLASS_NINF;
658 #endif
659 #ifdef FP_PINF
660 case FP_PINF: return MSVCRT__FPCLASS_PINF;
661 #endif
662 #ifdef FP_NDENORM
663 case FP_NDENORM: return MSVCRT__FPCLASS_ND;
664 #endif
665 #ifdef FP_PDENORM
666 case FP_PDENORM: return MSVCRT__FPCLASS_PD;
667 #endif
668 #ifdef FP_NZERO
669 case FP_NZERO: return MSVCRT__FPCLASS_NZ;
670 #endif
671 #ifdef FP_PZERO
672 case FP_PZERO: return MSVCRT__FPCLASS_PZ;
673 #endif
674 #ifdef FP_NNORM
675 case FP_NNORM: return MSVCRT__FPCLASS_NN;
676 #endif
677 #ifdef FP_PNORM
678 case FP_PNORM: return MSVCRT__FPCLASS_PN;
679 #endif
680 default: return MSVCRT__FPCLASS_PN;
682 #elif defined (fpclassify)
683 switch (fpclassify( num ))
685 case FP_NAN: return MSVCRT__FPCLASS_QNAN;
686 case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
687 case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
688 case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
690 return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
691 #else
692 if (!isfinite(num))
693 return MSVCRT__FPCLASS_QNAN;
694 return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
695 #endif
698 /*********************************************************************
699 * _rotl (MSVCRT.@)
701 unsigned int CDECL _rotl(unsigned int num, int shift)
703 shift &= 31;
704 return (num << shift) | (num >> (32-shift));
707 /*********************************************************************
708 * _lrotl (MSVCRT.@)
710 MSVCRT_ulong CDECL MSVCRT__lrotl(MSVCRT_ulong num, int shift)
712 shift &= 0x1f;
713 return (num << shift) | (num >> (32-shift));
716 /*********************************************************************
717 * _lrotr (MSVCRT.@)
719 MSVCRT_ulong CDECL MSVCRT__lrotr(MSVCRT_ulong num, int shift)
721 shift &= 0x1f;
722 return (num >> shift) | (num << (32-shift));
725 /*********************************************************************
726 * _rotr (MSVCRT.@)
728 unsigned int CDECL _rotr(unsigned int num, int shift)
730 shift &= 0x1f;
731 return (num >> shift) | (num << (32-shift));
734 /*********************************************************************
735 * _rotl64 (MSVCRT.@)
737 unsigned __int64 CDECL _rotl64(unsigned __int64 num, int shift)
739 shift &= 63;
740 return (num << shift) | (num >> (64-shift));
743 /*********************************************************************
744 * _rotr64 (MSVCRT.@)
746 unsigned __int64 CDECL _rotr64(unsigned __int64 num, int shift)
748 shift &= 63;
749 return (num >> shift) | (num << (64-shift));
752 /*********************************************************************
753 * abs (MSVCRT.@)
755 int CDECL MSVCRT_abs( int n )
757 return n >= 0 ? n : -n;
760 /*********************************************************************
761 * labs (MSVCRT.@)
763 MSVCRT_long CDECL MSVCRT_labs( MSVCRT_long n )
765 return n >= 0 ? n : -n;
768 /*********************************************************************
769 * llabs (MSVCRT.@)
771 MSVCRT_longlong CDECL MSVCRT_llabs( MSVCRT_longlong n )
773 return n >= 0 ? n : -n;
776 /*********************************************************************
777 * _abs64 (MSVCRT.@)
779 __int64 CDECL _abs64( __int64 n )
781 return n >= 0 ? n : -n;
784 /*********************************************************************
785 * _logb (MSVCRT.@)
787 double CDECL MSVCRT__logb(double num)
789 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
790 return logb(num);
793 /*********************************************************************
794 * _scalb (MSVCRT.@)
796 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
798 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
799 return ldexp(num, power);
802 /*********************************************************************
803 * _hypot (MSVCRT.@)
805 double CDECL _hypot(double x, double y)
807 /* FIXME: errno handling */
808 return hypot( x, y );
811 /*********************************************************************
812 * _hypotf (MSVCRT.@)
814 float CDECL MSVCRT__hypotf(float x, float y)
816 /* FIXME: errno handling */
817 return hypotf( x, y );
820 /*********************************************************************
821 * ceil (MSVCRT.@)
823 double CDECL MSVCRT_ceil( double x )
825 return ceil(x);
828 /*********************************************************************
829 * floor (MSVCRT.@)
831 double CDECL MSVCRT_floor( double x )
833 return floor(x);
836 /*********************************************************************
837 * fabs (MSVCRT.@)
839 double CDECL MSVCRT_fabs( double x )
841 return fabs(x);
844 /*********************************************************************
845 * frexp (MSVCRT.@)
847 double CDECL MSVCRT_frexp( double x, int *exp )
849 return frexp( x, exp );
852 /*********************************************************************
853 * modf (MSVCRT.@)
855 double CDECL MSVCRT_modf( double x, double *iptr )
857 return modf( x, iptr );
860 /*********************************************************************
861 * _matherr (MSVCRT.@)
863 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
865 if (e)
866 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
867 e->retval);
868 else
869 TRACE("(null)\n");
870 if (MSVCRT_default_matherr_func)
871 return MSVCRT_default_matherr_func(e);
872 ERR(":Unhandled math error!\n");
873 return 0;
876 /*********************************************************************
877 * __setusermatherr (MSVCRT.@)
879 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
881 MSVCRT_default_matherr_func = func;
882 TRACE(":new matherr handler %p\n", func);
885 /**********************************************************************
886 * _statusfp2 (MSVCRT.@)
888 * Not exported by native msvcrt, added in msvcr80.
890 #if defined(__i386__) || defined(__x86_64__)
891 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
893 #ifdef __GNUC__
894 unsigned int flags;
895 unsigned long fpword;
897 if (x86_sw)
899 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
900 flags = 0;
901 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
902 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
903 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
904 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
905 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
906 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
907 *x86_sw = flags;
910 if (!sse2_sw) return;
912 if (sse2_supported)
914 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
915 flags = 0;
916 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
917 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
918 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
919 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
920 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
921 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
922 *sse2_sw = flags;
924 else *sse2_sw = 0;
925 #else
926 FIXME( "not implemented\n" );
927 #endif
929 #endif
931 /**********************************************************************
932 * _statusfp (MSVCRT.@)
934 unsigned int CDECL _statusfp(void)
936 #if defined(__i386__) || defined(__x86_64__)
937 unsigned int x86_sw, sse2_sw;
939 _statusfp2( &x86_sw, &sse2_sw );
940 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
941 return x86_sw | sse2_sw;
942 #else
943 FIXME( "not implemented\n" );
944 return 0;
945 #endif
948 /*********************************************************************
949 * _clearfp (MSVCRT.@)
951 unsigned int CDECL _clearfp(void)
953 unsigned int flags = 0;
954 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
955 unsigned long fpword;
957 __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
958 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
959 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
960 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
961 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
962 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
963 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
965 if (sse2_supported)
967 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
968 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
969 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
970 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
971 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
972 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
973 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
974 fpword &= ~0x3f;
975 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
977 #else
978 FIXME( "not implemented\n" );
979 #endif
980 return flags;
983 /*********************************************************************
984 * __fpecode (MSVCRT.@)
986 int * CDECL __fpecode(void)
988 return &msvcrt_get_thread_data()->fpecode;
991 /*********************************************************************
992 * ldexp (MSVCRT.@)
994 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
996 double z = ldexp(num,exp);
998 if (!isfinite(z))
999 *MSVCRT__errno() = MSVCRT_ERANGE;
1000 else if (z == 0 && signbit(z))
1001 z = 0.0; /* Convert -0 -> +0 */
1002 return z;
1005 /*********************************************************************
1006 * _cabs (MSVCRT.@)
1008 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
1010 return sqrt(num.x * num.x + num.y * num.y);
1013 /*********************************************************************
1014 * _chgsign (MSVCRT.@)
1016 double CDECL MSVCRT__chgsign(double num)
1018 /* FIXME: +-infinity,Nan not tested */
1019 return -num;
1022 /*********************************************************************
1023 * __control87_2 (MSVCRT.@)
1025 * Not exported by native msvcrt, added in msvcr80.
1027 #if defined(__i386__) || defined(__x86_64__)
1028 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1029 unsigned int *x86_cw, unsigned int *sse2_cw )
1031 #ifdef __GNUC__
1032 unsigned long fpword;
1033 unsigned int flags;
1035 if (x86_cw)
1037 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1039 /* Convert into mask constants */
1040 flags = 0;
1041 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
1042 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
1043 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
1044 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
1045 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1046 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1047 switch (fpword & 0xc00)
1049 case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1050 case 0x800: flags |= MSVCRT__RC_UP; break;
1051 case 0x400: flags |= MSVCRT__RC_DOWN; break;
1053 switch (fpword & 0x300)
1055 case 0x0: flags |= MSVCRT__PC_24; break;
1056 case 0x200: flags |= MSVCRT__PC_53; break;
1057 case 0x300: flags |= MSVCRT__PC_64; break;
1059 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1061 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1062 if (mask)
1064 flags = (flags & ~mask) | (newval & mask);
1066 /* Convert (masked) value back to fp word */
1067 fpword = 0;
1068 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
1069 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
1070 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1071 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
1072 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
1073 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
1074 switch (flags & MSVCRT__MCW_RC)
1076 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1077 case MSVCRT__RC_UP: fpword |= 0x800; break;
1078 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
1080 switch (flags & MSVCRT__MCW_PC)
1082 case MSVCRT__PC_64: fpword |= 0x300; break;
1083 case MSVCRT__PC_53: fpword |= 0x200; break;
1084 case MSVCRT__PC_24: fpword |= 0x0; break;
1086 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1088 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1090 *x86_cw = flags;
1093 if (!sse2_cw) return 1;
1095 if (sse2_supported)
1097 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1099 /* Convert into mask constants */
1100 flags = 0;
1101 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1102 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1103 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1104 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1105 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1106 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1107 switch (fpword & 0x6000)
1109 case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1110 case 0x4000: flags |= MSVCRT__RC_UP; break;
1111 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1113 switch (fpword & 0x8040)
1115 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1116 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1117 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1120 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1121 if (mask)
1123 flags = (flags & ~mask) | (newval & mask);
1125 /* Convert (masked) value back to fp word */
1126 fpword = 0;
1127 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1128 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1129 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1130 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1131 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1132 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1133 switch (flags & MSVCRT__MCW_RC)
1135 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1136 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1137 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1139 switch (flags & MSVCRT__MCW_DN)
1141 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1142 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1143 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
1145 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1147 *sse2_cw = flags;
1149 else *sse2_cw = 0;
1151 return 1;
1152 #else
1153 FIXME( "not implemented\n" );
1154 return 0;
1155 #endif
1157 #endif
1159 /*********************************************************************
1160 * _control87 (MSVCRT.@)
1162 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1164 #if defined(__i386__) || defined(__x86_64__)
1165 unsigned int x86_cw, sse2_cw;
1167 __control87_2( newval, mask, &x86_cw, &sse2_cw );
1169 if ((x86_cw ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) x86_cw |= MSVCRT__EM_AMBIGUOUS;
1170 return x86_cw;
1171 #else
1172 FIXME( "not implemented\n" );
1173 return 0;
1174 #endif
1177 /*********************************************************************
1178 * _controlfp (MSVCRT.@)
1180 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
1182 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
1185 /*********************************************************************
1186 * _set_controlfp (MSVCRT.@)
1188 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
1190 _controlfp( newval, mask );
1193 /*********************************************************************
1194 * _controlfp_s (MSVCRT.@)
1196 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
1198 static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
1199 MSVCRT__MCW_PC | MSVCRT__MCW_DN);
1200 unsigned int val;
1202 if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
1204 if (cur) *cur = _controlfp( 0, 0 ); /* retrieve it anyway */
1205 return MSVCRT_EINVAL;
1207 val = _controlfp( newval, mask );
1208 if (cur) *cur = val;
1209 return 0;
1212 /*********************************************************************
1213 * _copysign (MSVCRT.@)
1215 double CDECL MSVCRT__copysign(double num, double sign)
1217 /* FIXME: Behaviour for Nan/Inf? */
1218 if (sign < 0.0)
1219 return num < 0.0 ? num : -num;
1220 return num < 0.0 ? -num : num;
1223 /*********************************************************************
1224 * _finite (MSVCRT.@)
1226 int CDECL MSVCRT__finite(double num)
1228 return isfinite(num) != 0; /* See comment for _isnan() */
1231 /*********************************************************************
1232 * _fpreset (MSVCRT.@)
1234 void CDECL _fpreset(void)
1236 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1237 const unsigned int x86_cw = 0x27f;
1238 __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
1239 if (sse2_supported)
1241 const unsigned long sse2_cw = 0x1f80;
1242 __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
1244 #else
1245 FIXME( "not implemented\n" );
1246 #endif
1249 /*********************************************************************
1250 * _isnan (MSVCRT.@)
1252 INT CDECL MSVCRT__isnan(double num)
1254 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1255 * Do the same, as the result may be used in calculations
1257 return isnan(num) != 0;
1260 /*********************************************************************
1261 * _j0 (MSVCRT.@)
1263 double CDECL MSVCRT__j0(double num)
1265 /* FIXME: errno handling */
1266 return j0(num);
1269 /*********************************************************************
1270 * _j1 (MSVCRT.@)
1272 double CDECL MSVCRT__j1(double num)
1274 /* FIXME: errno handling */
1275 return j1(num);
1278 /*********************************************************************
1279 * _jn (MSVCRT.@)
1281 double CDECL MSVCRT__jn(int n, double num)
1283 /* FIXME: errno handling */
1284 return jn(n, num);
1287 /*********************************************************************
1288 * _y0 (MSVCRT.@)
1290 double CDECL MSVCRT__y0(double num)
1292 double retval;
1293 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1294 retval = y0(num);
1295 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1297 *MSVCRT__errno() = MSVCRT_EDOM;
1298 retval = sqrt(-1);
1300 return retval;
1303 /*********************************************************************
1304 * _y1 (MSVCRT.@)
1306 double CDECL MSVCRT__y1(double num)
1308 double retval;
1309 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1310 retval = y1(num);
1311 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1313 *MSVCRT__errno() = MSVCRT_EDOM;
1314 retval = sqrt(-1);
1316 return retval;
1319 /*********************************************************************
1320 * _yn (MSVCRT.@)
1322 double CDECL MSVCRT__yn(int order, double num)
1324 double retval;
1325 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1326 retval = yn(order,num);
1327 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1329 *MSVCRT__errno() = MSVCRT_EDOM;
1330 retval = sqrt(-1);
1332 return retval;
1335 /*********************************************************************
1336 * _nextafter (MSVCRT.@)
1338 double CDECL MSVCRT__nextafter(double num, double next)
1340 double retval;
1341 if (!isfinite(num) || !isfinite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1342 retval = nextafter(num,next);
1343 return retval;
1346 /*********************************************************************
1347 * _ecvt (MSVCRT.@)
1349 char * CDECL MSVCRT__ecvt( double number, int ndigits, int *decpt, int *sign )
1351 int prec, len;
1352 thread_data_t *data = msvcrt_get_thread_data();
1353 /* FIXME: check better for overflow (native supports over 300 chars) */
1354 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1355 * 4 for exponent and one for
1356 * terminating '\0' */
1357 if (!data->efcvt_buffer)
1358 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1360 if( number < 0) {
1361 *sign = TRUE;
1362 number = -number;
1363 } else
1364 *sign = FALSE;
1365 /* handle cases with zero ndigits or less */
1366 prec = ndigits;
1367 if( prec < 1) prec = 2;
1368 len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1369 /* take the decimal "point away */
1370 if( prec != 1)
1371 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1372 /* take the exponential "e" out */
1373 data->efcvt_buffer[ prec] = '\0';
1374 /* read the exponent */
1375 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1376 (*decpt)++;
1377 /* adjust for some border cases */
1378 if( data->efcvt_buffer[0] == '0')/* value is zero */
1379 *decpt = 0;
1380 /* handle cases with zero ndigits or less */
1381 if( ndigits < 1){
1382 if( data->efcvt_buffer[ 0] >= '5')
1383 (*decpt)++;
1384 data->efcvt_buffer[ 0] = '\0';
1386 TRACE("out=\"%s\"\n",data->efcvt_buffer);
1387 return data->efcvt_buffer;
1390 /*********************************************************************
1391 * _ecvt_s (MSVCRT.@)
1393 int CDECL MSVCRT__ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
1395 int prec, len;
1396 char *result;
1397 const char infret[] = "1#INF";
1399 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
1400 if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
1401 if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
1402 if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1403 if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1405 /* special case - inf */
1406 if(number == HUGE_VAL || number == -HUGE_VAL)
1408 memset(buffer, '0', ndigits);
1409 memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
1410 buffer[ndigits] = '\0';
1411 (*decpt) = 1;
1412 if(number == -HUGE_VAL)
1413 (*sign) = 1;
1414 else
1415 (*sign) = 0;
1416 return 0;
1418 /* handle cases with zero ndigits or less */
1419 prec = ndigits;
1420 if( prec < 1) prec = 2;
1421 result = MSVCRT_malloc(prec + 7);
1423 if( number < 0) {
1424 *sign = TRUE;
1425 number = -number;
1426 } else
1427 *sign = FALSE;
1428 len = snprintf(result, prec + 7, "%.*le", prec - 1, number);
1429 /* take the decimal "point away */
1430 if( prec != 1)
1431 memmove( result + 1, result + 2, len - 1 );
1432 /* take the exponential "e" out */
1433 result[ prec] = '\0';
1434 /* read the exponent */
1435 sscanf( result + prec + 1, "%d", decpt);
1436 (*decpt)++;
1437 /* adjust for some border cases */
1438 if( result[0] == '0')/* value is zero */
1439 *decpt = 0;
1440 /* handle cases with zero ndigits or less */
1441 if( ndigits < 1){
1442 if( result[ 0] >= '5')
1443 (*decpt)++;
1444 result[ 0] = '\0';
1446 memcpy( buffer, result, max(ndigits + 1, 1) );
1447 MSVCRT_free( result );
1448 return 0;
1451 /***********************************************************************
1452 * _fcvt (MSVCRT.@)
1454 char * CDECL MSVCRT__fcvt( double number, int ndigits, int *decpt, int *sign )
1456 thread_data_t *data = msvcrt_get_thread_data();
1457 int stop, dec1, dec2;
1458 char *ptr1, *ptr2, *first;
1459 char buf[80]; /* ought to be enough */
1461 if (!data->efcvt_buffer)
1462 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1464 if (number < 0)
1466 *sign = 1;
1467 number = -number;
1468 } else *sign = 0;
1470 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1471 ptr1 = buf;
1472 ptr2 = data->efcvt_buffer;
1473 first = NULL;
1474 dec1 = 0;
1475 dec2 = 0;
1477 /* For numbers below the requested resolution, work out where
1478 the decimal point will be rather than finding it in the string */
1479 if (number < 1.0 && number > 0.0) {
1480 dec2 = log10(number + 1e-10);
1481 if (-dec2 <= ndigits) dec2 = 0;
1484 /* If requested digits is zero or less, we will need to truncate
1485 * the returned string */
1486 if (ndigits < 1) {
1487 stop = strlen(buf) + ndigits;
1488 } else {
1489 stop = strlen(buf);
1492 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1493 while (*ptr1 != '\0' && *ptr1 != '.') {
1494 if (!first) first = ptr2;
1495 if ((ptr1 - buf) < stop) {
1496 *ptr2++ = *ptr1++;
1497 } else {
1498 ptr1++;
1500 dec1++;
1503 if (ndigits > 0) {
1504 ptr1++;
1505 if (!first) {
1506 while (*ptr1 == '0') { /* Process leading zeroes */
1507 *ptr2++ = *ptr1++;
1508 dec1--;
1511 while (*ptr1 != '\0') {
1512 if (!first) first = ptr2;
1513 *ptr2++ = *ptr1++;
1517 *ptr2 = '\0';
1519 /* We never found a non-zero digit, then our number is either
1520 * smaller than the requested precision, or 0.0 */
1521 if (!first) {
1522 if (number > 0.0) {
1523 first = ptr2;
1524 } else {
1525 first = data->efcvt_buffer;
1526 dec1 = 0;
1530 *decpt = dec2 ? dec2 : dec1;
1531 return first;
1534 /***********************************************************************
1535 * _fcvt_s (MSVCRT.@)
1537 int CDECL MSVCRT__fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
1539 int stop, dec1, dec2;
1540 char *ptr1, *ptr2, *first;
1541 char buf[80]; /* ought to be enough */
1543 if (!outbuffer || !decpt || !sign || size == 0)
1545 *MSVCRT__errno() = MSVCRT_EINVAL;
1546 return MSVCRT_EINVAL;
1549 if (number < 0)
1551 *sign = 1;
1552 number = -number;
1553 } else *sign = 0;
1555 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1556 ptr1 = buf;
1557 ptr2 = outbuffer;
1558 first = NULL;
1559 dec1 = 0;
1560 dec2 = 0;
1562 /* For numbers below the requested resolution, work out where
1563 the decimal point will be rather than finding it in the string */
1564 if (number < 1.0 && number > 0.0) {
1565 dec2 = log10(number + 1e-10);
1566 if (-dec2 <= ndigits) dec2 = 0;
1569 /* If requested digits is zero or less, we will need to truncate
1570 * the returned string */
1571 if (ndigits < 1) {
1572 stop = strlen(buf) + ndigits;
1573 } else {
1574 stop = strlen(buf);
1577 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1578 while (*ptr1 != '\0' && *ptr1 != '.') {
1579 if (!first) first = ptr2;
1580 if ((ptr1 - buf) < stop) {
1581 if (size > 1) {
1582 *ptr2++ = *ptr1++;
1583 size--;
1585 } else {
1586 ptr1++;
1588 dec1++;
1591 if (ndigits > 0) {
1592 ptr1++;
1593 if (!first) {
1594 while (*ptr1 == '0') { /* Process leading zeroes */
1595 if (number == 0.0 && size > 1) {
1596 *ptr2++ = '0';
1597 size--;
1599 ptr1++;
1600 dec1--;
1603 while (*ptr1 != '\0') {
1604 if (!first) first = ptr2;
1605 if (size > 1) {
1606 *ptr2++ = *ptr1++;
1607 size--;
1612 *ptr2 = '\0';
1614 /* We never found a non-zero digit, then our number is either
1615 * smaller than the requested precision, or 0.0 */
1616 if (!first && (number <= 0.0))
1617 dec1 = 0;
1619 *decpt = dec2 ? dec2 : dec1;
1620 return 0;
1623 /***********************************************************************
1624 * _gcvt (MSVCRT.@)
1626 char * CDECL MSVCRT__gcvt( double number, int ndigit, char *buff )
1628 if(!buff) {
1629 *MSVCRT__errno() = MSVCRT_EINVAL;
1630 return NULL;
1633 if(ndigit < 0) {
1634 *MSVCRT__errno() = MSVCRT_ERANGE;
1635 return NULL;
1638 MSVCRT_sprintf(buff, "%.*g", ndigit, number);
1639 return buff;
1642 /***********************************************************************
1643 * _gcvt_s (MSVCRT.@)
1645 int CDECL MSVCRT__gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
1647 int len;
1649 if(!buff) {
1650 *MSVCRT__errno() = MSVCRT_EINVAL;
1651 return MSVCRT_EINVAL;
1654 if( digits<0 || digits>=size) {
1655 if(size)
1656 buff[0] = '\0';
1658 *MSVCRT__errno() = MSVCRT_ERANGE;
1659 return MSVCRT_ERANGE;
1662 len = MSVCRT__scprintf("%.*g", digits, number);
1663 if(len > size) {
1664 buff[0] = '\0';
1665 *MSVCRT__errno() = MSVCRT_ERANGE;
1666 return MSVCRT_ERANGE;
1669 MSVCRT_sprintf(buff, "%.*g", digits, number);
1670 return 0;
1673 #include <stdlib.h> /* div_t, ldiv_t */
1675 /*********************************************************************
1676 * div (MSVCRT.@)
1677 * VERSION
1678 * [i386] Windows binary compatible - returns the struct in eax/edx.
1680 #ifdef __i386__
1681 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1683 div_t dt = div(num,denom);
1684 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1686 #else
1687 /*********************************************************************
1688 * div (MSVCRT.@)
1689 * VERSION
1690 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1692 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1694 div_t dt = div(num,denom);
1695 MSVCRT_div_t ret;
1696 ret.quot = dt.quot;
1697 ret.rem = dt.rem;
1699 return ret;
1702 #endif /* ifdef __i386__ */
1705 /*********************************************************************
1706 * ldiv (MSVCRT.@)
1707 * VERSION
1708 * [i386] Windows binary compatible - returns the struct in eax/edx.
1710 #ifdef __i386__
1711 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1713 ldiv_t ldt = ldiv(num,denom);
1714 return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1716 #else
1717 /*********************************************************************
1718 * ldiv (MSVCRT.@)
1719 * VERSION
1720 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1722 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1724 ldiv_t result = ldiv(num,denom);
1726 MSVCRT_ldiv_t ret;
1727 ret.quot = result.quot;
1728 ret.rem = result.rem;
1730 return ret;
1732 #endif /* ifdef __i386__ */
1734 #ifdef __i386__
1736 /*********************************************************************
1737 * _adjust_fdiv (MSVCRT.@)
1738 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1740 int MSVCRT__adjust_fdiv = 0;
1742 /***********************************************************************
1743 * _adj_fdiv_m16i (MSVCRT.@)
1745 * NOTE
1746 * I _think_ this function is intended to work around the Pentium
1747 * fdiv bug.
1749 void __stdcall _adj_fdiv_m16i( short arg )
1751 TRACE("(): stub\n");
1754 /***********************************************************************
1755 * _adj_fdiv_m32 (MSVCRT.@)
1757 * NOTE
1758 * I _think_ this function is intended to work around the Pentium
1759 * fdiv bug.
1761 void __stdcall _adj_fdiv_m32( unsigned int arg )
1763 TRACE("(): stub\n");
1766 /***********************************************************************
1767 * _adj_fdiv_m32i (MSVCRT.@)
1769 * NOTE
1770 * I _think_ this function is intended to work around the Pentium
1771 * fdiv bug.
1773 void __stdcall _adj_fdiv_m32i( int arg )
1775 TRACE("(): stub\n");
1778 /***********************************************************************
1779 * _adj_fdiv_m64 (MSVCRT.@)
1781 * NOTE
1782 * I _think_ this function is intended to work around the Pentium
1783 * fdiv bug.
1785 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1787 TRACE("(): stub\n");
1790 /***********************************************************************
1791 * _adj_fdiv_r (MSVCRT.@)
1792 * FIXME
1793 * This function is likely to have the wrong number of arguments.
1795 * NOTE
1796 * I _think_ this function is intended to work around the Pentium
1797 * fdiv bug.
1799 void _adj_fdiv_r(void)
1801 TRACE("(): stub\n");
1804 /***********************************************************************
1805 * _adj_fdivr_m16i (MSVCRT.@)
1807 * NOTE
1808 * I _think_ this function is intended to work around the Pentium
1809 * fdiv bug.
1811 void __stdcall _adj_fdivr_m16i( short arg )
1813 TRACE("(): stub\n");
1816 /***********************************************************************
1817 * _adj_fdivr_m32 (MSVCRT.@)
1819 * NOTE
1820 * I _think_ this function is intended to work around the Pentium
1821 * fdiv bug.
1823 void __stdcall _adj_fdivr_m32( unsigned int arg )
1825 TRACE("(): stub\n");
1828 /***********************************************************************
1829 * _adj_fdivr_m32i (MSVCRT.@)
1831 * NOTE
1832 * I _think_ this function is intended to work around the Pentium
1833 * fdiv bug.
1835 void __stdcall _adj_fdivr_m32i( int arg )
1837 TRACE("(): stub\n");
1840 /***********************************************************************
1841 * _adj_fdivr_m64 (MSVCRT.@)
1843 * NOTE
1844 * I _think_ this function is intended to work around the Pentium
1845 * fdiv bug.
1847 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1849 TRACE("(): stub\n");
1852 /***********************************************************************
1853 * _adj_fpatan (MSVCRT.@)
1854 * FIXME
1855 * This function is likely to have the wrong number of arguments.
1857 * NOTE
1858 * I _think_ this function is intended to work around the Pentium
1859 * fdiv bug.
1861 void _adj_fpatan(void)
1863 TRACE("(): stub\n");
1866 /***********************************************************************
1867 * _adj_fprem (MSVCRT.@)
1868 * FIXME
1869 * This function is likely to have the wrong number of arguments.
1871 * NOTE
1872 * I _think_ this function is intended to work around the Pentium
1873 * fdiv bug.
1875 void _adj_fprem(void)
1877 TRACE("(): stub\n");
1880 /***********************************************************************
1881 * _adj_fprem1 (MSVCRT.@)
1882 * FIXME
1883 * This function is likely to have the wrong number of arguments.
1885 * NOTE
1886 * I _think_ this function is intended to work around the Pentium
1887 * fdiv bug.
1889 void _adj_fprem1(void)
1891 TRACE("(): stub\n");
1894 /***********************************************************************
1895 * _adj_fptan (MSVCRT.@)
1896 * FIXME
1897 * This function is likely to have the wrong number of arguments.
1899 * NOTE
1900 * I _think_ this function is intended to work around the Pentium
1901 * fdiv bug.
1903 void _adj_fptan(void)
1905 TRACE("(): stub\n");
1908 /***********************************************************************
1909 * _safe_fdiv (MSVCRT.@)
1910 * FIXME
1911 * This function is likely to have the wrong number of arguments.
1913 * NOTE
1914 * I _think_ this function is intended to work around the Pentium
1915 * fdiv bug.
1917 void _safe_fdiv(void)
1919 TRACE("(): stub\n");
1922 /***********************************************************************
1923 * _safe_fdivr (MSVCRT.@)
1924 * FIXME
1925 * This function is likely to have the wrong number of arguments.
1927 * NOTE
1928 * I _think_ this function is intended to work around the Pentium
1929 * fdiv bug.
1931 void _safe_fdivr(void)
1933 TRACE("(): stub\n");
1936 /***********************************************************************
1937 * _safe_fprem (MSVCRT.@)
1938 * FIXME
1939 * This function is likely to have the wrong number of arguments.
1941 * NOTE
1942 * I _think_ this function is intended to work around the Pentium
1943 * fdiv bug.
1945 void _safe_fprem(void)
1947 TRACE("(): stub\n");
1950 /***********************************************************************
1951 * _safe_fprem1 (MSVCRT.@)
1953 * FIXME
1954 * This function is likely to have the wrong number of arguments.
1956 * NOTE
1957 * I _think_ this function is intended to work around the Pentium
1958 * fdiv bug.
1960 void _safe_fprem1(void)
1962 TRACE("(): stub\n");
1965 /***********************************************************************
1966 * __libm_sse2_acos (MSVCRT.@)
1968 void __cdecl __libm_sse2_acos(void)
1970 double d;
1971 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1972 d = acos( d );
1973 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1976 /***********************************************************************
1977 * __libm_sse2_acosf (MSVCRT.@)
1979 void __cdecl __libm_sse2_acosf(void)
1981 float f;
1982 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
1983 f = acosf( f );
1984 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
1987 /***********************************************************************
1988 * __libm_sse2_asin (MSVCRT.@)
1990 void __cdecl __libm_sse2_asin(void)
1992 double d;
1993 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1994 d = asin( d );
1995 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1998 /***********************************************************************
1999 * __libm_sse2_asinf (MSVCRT.@)
2001 void __cdecl __libm_sse2_asinf(void)
2003 float f;
2004 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2005 f = asinf( f );
2006 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2009 /***********************************************************************
2010 * __libm_sse2_atan (MSVCRT.@)
2012 void __cdecl __libm_sse2_atan(void)
2014 double d;
2015 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2016 d = atan( d );
2017 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2020 /***********************************************************************
2021 * __libm_sse2_atan2 (MSVCRT.@)
2023 void __cdecl __libm_sse2_atan2(void)
2025 double d1, d2;
2026 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2027 d1 = atan2( d1, d2 );
2028 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2031 /***********************************************************************
2032 * __libm_sse2_atanf (MSVCRT.@)
2034 void __cdecl __libm_sse2_atanf(void)
2036 float f;
2037 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2038 f = atanf( f );
2039 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2042 /***********************************************************************
2043 * __libm_sse2_cos (MSVCRT.@)
2045 void __cdecl __libm_sse2_cos(void)
2047 double d;
2048 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2049 d = cos( d );
2050 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2053 /***********************************************************************
2054 * __libm_sse2_cosf (MSVCRT.@)
2056 void __cdecl __libm_sse2_cosf(void)
2058 float f;
2059 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2060 f = cosf( f );
2061 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2064 /***********************************************************************
2065 * __libm_sse2_exp (MSVCRT.@)
2067 void __cdecl __libm_sse2_exp(void)
2069 double d;
2070 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2071 d = exp( d );
2072 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2075 /***********************************************************************
2076 * __libm_sse2_expf (MSVCRT.@)
2078 void __cdecl __libm_sse2_expf(void)
2080 float f;
2081 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2082 f = expf( f );
2083 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2086 /***********************************************************************
2087 * __libm_sse2_log (MSVCRT.@)
2089 void __cdecl __libm_sse2_log(void)
2091 double d;
2092 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2093 d = log( d );
2094 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2097 /***********************************************************************
2098 * __libm_sse2_log10 (MSVCRT.@)
2100 void __cdecl __libm_sse2_log10(void)
2102 double d;
2103 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2104 d = log10( d );
2105 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2108 /***********************************************************************
2109 * __libm_sse2_log10f (MSVCRT.@)
2111 void __cdecl __libm_sse2_log10f(void)
2113 float f;
2114 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2115 f = log10f( f );
2116 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2119 /***********************************************************************
2120 * __libm_sse2_logf (MSVCRT.@)
2122 void __cdecl __libm_sse2_logf(void)
2124 float f;
2125 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2126 f = logf( f );
2127 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2130 /***********************************************************************
2131 * __libm_sse2_pow (MSVCRT.@)
2133 void __cdecl __libm_sse2_pow(void)
2135 double d1, d2;
2136 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2137 d1 = pow( d1, d2 );
2138 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2141 /***********************************************************************
2142 * __libm_sse2_powf (MSVCRT.@)
2144 void __cdecl __libm_sse2_powf(void)
2146 float f1, f2;
2147 __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
2148 f1 = powf( f1, f2 );
2149 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
2152 /***********************************************************************
2153 * __libm_sse2_sin (MSVCRT.@)
2155 void __cdecl __libm_sse2_sin(void)
2157 double d;
2158 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2159 d = sin( d );
2160 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2163 /***********************************************************************
2164 * __libm_sse2_sinf (MSVCRT.@)
2166 void __cdecl __libm_sse2_sinf(void)
2168 float f;
2169 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2170 f = sinf( f );
2171 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2174 /***********************************************************************
2175 * __libm_sse2_tan (MSVCRT.@)
2177 void __cdecl __libm_sse2_tan(void)
2179 double d;
2180 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2181 d = tan( d );
2182 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2185 /***********************************************************************
2186 * __libm_sse2_tanf (MSVCRT.@)
2188 void __cdecl __libm_sse2_tanf(void)
2190 float f;
2191 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2192 f = tanf( f );
2193 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2196 /***********************************************************************
2197 * __libm_sse2_sqrt_precise (MSVCR110.@)
2199 void __cdecl __libm_sse2_sqrt_precise(void)
2201 double d;
2202 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2203 d = sqrt( d );
2204 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2207 #endif /* __i386__ */