msvcrt: Update comments for non-exported functions.
[wine/multimedia.git] / dlls / msvcrt / math.c
blob2c5a8f3da67a928b08bf4655d08f553cd0187618
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 * _abs64 (MSVCRT.@)
771 __int64 CDECL _abs64( __int64 n )
773 return n >= 0 ? n : -n;
776 /*********************************************************************
777 * _logb (MSVCRT.@)
779 double CDECL MSVCRT__logb(double num)
781 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
782 return logb(num);
785 /*********************************************************************
786 * _scalb (MSVCRT.@)
788 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
790 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
791 return ldexp(num, power);
794 /*********************************************************************
795 * _hypot (MSVCRT.@)
797 double CDECL _hypot(double x, double y)
799 /* FIXME: errno handling */
800 return hypot( x, y );
803 /*********************************************************************
804 * _hypotf (MSVCRT.@)
806 float CDECL MSVCRT__hypotf(float x, float y)
808 /* FIXME: errno handling */
809 return hypotf( x, y );
812 /*********************************************************************
813 * ceil (MSVCRT.@)
815 double CDECL MSVCRT_ceil( double x )
817 return ceil(x);
820 /*********************************************************************
821 * floor (MSVCRT.@)
823 double CDECL MSVCRT_floor( double x )
825 return floor(x);
828 /*********************************************************************
829 * fabs (MSVCRT.@)
831 double CDECL MSVCRT_fabs( double x )
833 return fabs(x);
836 /*********************************************************************
837 * frexp (MSVCRT.@)
839 double CDECL MSVCRT_frexp( double x, int *exp )
841 return frexp( x, exp );
844 /*********************************************************************
845 * modf (MSVCRT.@)
847 double CDECL MSVCRT_modf( double x, double *iptr )
849 return modf( x, iptr );
852 /*********************************************************************
853 * _matherr (MSVCRT.@)
855 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
857 if (e)
858 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
859 e->retval);
860 else
861 TRACE("(null)\n");
862 if (MSVCRT_default_matherr_func)
863 return MSVCRT_default_matherr_func(e);
864 ERR(":Unhandled math error!\n");
865 return 0;
868 /*********************************************************************
869 * __setusermatherr (MSVCRT.@)
871 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
873 MSVCRT_default_matherr_func = func;
874 TRACE(":new matherr handler %p\n", func);
877 /**********************************************************************
878 * _statusfp2 (MSVCRT.@)
880 * Not exported by native msvcrt, added in msvcr80.
882 #if defined(__i386__) || defined(__x86_64__)
883 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
885 #ifdef __GNUC__
886 unsigned int flags;
887 unsigned long fpword;
889 if (x86_sw)
891 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
892 flags = 0;
893 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
894 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
895 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
896 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
897 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
898 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
899 *x86_sw = flags;
902 if (!sse2_sw) return;
904 if (sse2_supported)
906 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
907 flags = 0;
908 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
909 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
910 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
911 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
912 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
913 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
914 *sse2_sw = flags;
916 else *sse2_sw = 0;
917 #else
918 FIXME( "not implemented\n" );
919 #endif
921 #endif
923 /**********************************************************************
924 * _statusfp (MSVCRT.@)
926 unsigned int CDECL _statusfp(void)
928 #if defined(__i386__) || defined(__x86_64__)
929 unsigned int x86_sw, sse2_sw;
931 _statusfp2( &x86_sw, &sse2_sw );
932 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
933 return x86_sw | sse2_sw;
934 #else
935 FIXME( "not implemented\n" );
936 return 0;
937 #endif
940 /*********************************************************************
941 * _clearfp (MSVCRT.@)
943 unsigned int CDECL _clearfp(void)
945 unsigned int flags = 0;
946 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
947 unsigned long fpword;
949 __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
950 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
951 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
952 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
953 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
954 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
955 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
957 if (sse2_supported)
959 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
960 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
961 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
962 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
963 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
964 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
965 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
966 fpword &= ~0x3f;
967 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
969 #else
970 FIXME( "not implemented\n" );
971 #endif
972 return flags;
975 /*********************************************************************
976 * __fpecode (MSVCRT.@)
978 int * CDECL __fpecode(void)
980 return &msvcrt_get_thread_data()->fpecode;
983 /*********************************************************************
984 * ldexp (MSVCRT.@)
986 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
988 double z = ldexp(num,exp);
990 if (!isfinite(z))
991 *MSVCRT__errno() = MSVCRT_ERANGE;
992 else if (z == 0 && signbit(z))
993 z = 0.0; /* Convert -0 -> +0 */
994 return z;
997 /*********************************************************************
998 * _cabs (MSVCRT.@)
1000 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
1002 return sqrt(num.x * num.x + num.y * num.y);
1005 /*********************************************************************
1006 * _chgsign (MSVCRT.@)
1008 double CDECL MSVCRT__chgsign(double num)
1010 /* FIXME: +-infinity,Nan not tested */
1011 return -num;
1014 /*********************************************************************
1015 * __control87_2 (MSVCRT.@)
1017 * Not exported by native msvcrt, added in msvcr80.
1019 #if defined(__i386__) || defined(__x86_64__)
1020 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1021 unsigned int *x86_cw, unsigned int *sse2_cw )
1023 #ifdef __GNUC__
1024 unsigned long fpword;
1025 unsigned int flags;
1027 if (x86_cw)
1029 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1031 /* Convert into mask constants */
1032 flags = 0;
1033 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
1034 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
1035 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
1036 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
1037 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1038 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1039 switch (fpword & 0xc00)
1041 case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1042 case 0x800: flags |= MSVCRT__RC_UP; break;
1043 case 0x400: flags |= MSVCRT__RC_DOWN; break;
1045 switch (fpword & 0x300)
1047 case 0x0: flags |= MSVCRT__PC_24; break;
1048 case 0x200: flags |= MSVCRT__PC_53; break;
1049 case 0x300: flags |= MSVCRT__PC_64; break;
1051 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1053 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1054 if (mask)
1056 flags = (flags & ~mask) | (newval & mask);
1058 /* Convert (masked) value back to fp word */
1059 fpword = 0;
1060 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
1061 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
1062 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1063 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
1064 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
1065 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
1066 switch (flags & MSVCRT__MCW_RC)
1068 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1069 case MSVCRT__RC_UP: fpword |= 0x800; break;
1070 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
1072 switch (flags & MSVCRT__MCW_PC)
1074 case MSVCRT__PC_64: fpword |= 0x300; break;
1075 case MSVCRT__PC_53: fpword |= 0x200; break;
1076 case MSVCRT__PC_24: fpword |= 0x0; break;
1078 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1080 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1082 *x86_cw = flags;
1085 if (!sse2_cw) return 1;
1087 if (sse2_supported)
1089 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1091 /* Convert into mask constants */
1092 flags = 0;
1093 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1094 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1095 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1096 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1097 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1098 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1099 switch (fpword & 0x6000)
1101 case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1102 case 0x4000: flags |= MSVCRT__RC_UP; break;
1103 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1105 switch (fpword & 0x8040)
1107 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1108 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1109 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1112 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1113 if (mask)
1115 flags = (flags & ~mask) | (newval & mask);
1117 /* Convert (masked) value back to fp word */
1118 fpword = 0;
1119 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1120 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1121 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1122 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1123 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1124 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1125 switch (flags & MSVCRT__MCW_RC)
1127 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1128 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1129 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1131 switch (flags & MSVCRT__MCW_DN)
1133 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1134 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1135 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
1137 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1139 *sse2_cw = flags;
1141 else *sse2_cw = 0;
1143 return 1;
1144 #else
1145 FIXME( "not implemented\n" );
1146 return 0;
1147 #endif
1149 #endif
1151 /*********************************************************************
1152 * _control87 (MSVCRT.@)
1154 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1156 #if defined(__i386__) || defined(__x86_64__)
1157 unsigned int x86_cw, sse2_cw;
1159 __control87_2( newval, mask, &x86_cw, &sse2_cw );
1161 if ((x86_cw ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) x86_cw |= MSVCRT__EM_AMBIGUOUS;
1162 return x86_cw;
1163 #else
1164 FIXME( "not implemented\n" );
1165 return 0;
1166 #endif
1169 /*********************************************************************
1170 * _controlfp (MSVCRT.@)
1172 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
1174 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
1177 /*********************************************************************
1178 * _set_controlfp (MSVCRT.@)
1180 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
1182 _controlfp( newval, mask );
1185 /*********************************************************************
1186 * _controlfp_s (MSVCRT.@)
1188 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
1190 static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
1191 MSVCRT__MCW_PC | MSVCRT__MCW_DN);
1192 unsigned int val;
1194 if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
1196 if (cur) *cur = _controlfp( 0, 0 ); /* retrieve it anyway */
1197 return MSVCRT_EINVAL;
1199 val = _controlfp( newval, mask );
1200 if (cur) *cur = val;
1201 return 0;
1204 /*********************************************************************
1205 * _copysign (MSVCRT.@)
1207 double CDECL MSVCRT__copysign(double num, double sign)
1209 /* FIXME: Behaviour for Nan/Inf? */
1210 if (sign < 0.0)
1211 return num < 0.0 ? num : -num;
1212 return num < 0.0 ? -num : num;
1215 /*********************************************************************
1216 * _finite (MSVCRT.@)
1218 int CDECL MSVCRT__finite(double num)
1220 return isfinite(num) != 0; /* See comment for _isnan() */
1223 /*********************************************************************
1224 * _fpreset (MSVCRT.@)
1226 void CDECL _fpreset(void)
1228 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1229 const unsigned int x86_cw = 0x27f;
1230 __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
1231 if (sse2_supported)
1233 const unsigned long sse2_cw = 0x1f80;
1234 __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
1236 #else
1237 FIXME( "not implemented\n" );
1238 #endif
1241 /*********************************************************************
1242 * _isnan (MSVCRT.@)
1244 INT CDECL MSVCRT__isnan(double num)
1246 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1247 * Do the same, as the result may be used in calculations
1249 return isnan(num) != 0;
1252 /*********************************************************************
1253 * _j0 (MSVCRT.@)
1255 double CDECL MSVCRT__j0(double num)
1257 /* FIXME: errno handling */
1258 return j0(num);
1261 /*********************************************************************
1262 * _j1 (MSVCRT.@)
1264 double CDECL MSVCRT__j1(double num)
1266 /* FIXME: errno handling */
1267 return j1(num);
1270 /*********************************************************************
1271 * _jn (MSVCRT.@)
1273 double CDECL MSVCRT__jn(int n, double num)
1275 /* FIXME: errno handling */
1276 return jn(n, num);
1279 /*********************************************************************
1280 * _y0 (MSVCRT.@)
1282 double CDECL MSVCRT__y0(double num)
1284 double retval;
1285 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1286 retval = y0(num);
1287 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1289 *MSVCRT__errno() = MSVCRT_EDOM;
1290 retval = sqrt(-1);
1292 return retval;
1295 /*********************************************************************
1296 * _y1 (MSVCRT.@)
1298 double CDECL MSVCRT__y1(double num)
1300 double retval;
1301 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1302 retval = y1(num);
1303 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1305 *MSVCRT__errno() = MSVCRT_EDOM;
1306 retval = sqrt(-1);
1308 return retval;
1311 /*********************************************************************
1312 * _yn (MSVCRT.@)
1314 double CDECL MSVCRT__yn(int order, double num)
1316 double retval;
1317 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1318 retval = yn(order,num);
1319 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1321 *MSVCRT__errno() = MSVCRT_EDOM;
1322 retval = sqrt(-1);
1324 return retval;
1327 /*********************************************************************
1328 * _nextafter (MSVCRT.@)
1330 double CDECL MSVCRT__nextafter(double num, double next)
1332 double retval;
1333 if (!isfinite(num) || !isfinite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1334 retval = nextafter(num,next);
1335 return retval;
1338 /*********************************************************************
1339 * _ecvt (MSVCRT.@)
1341 char * CDECL MSVCRT__ecvt( double number, int ndigits, int *decpt, int *sign )
1343 int prec, len;
1344 thread_data_t *data = msvcrt_get_thread_data();
1345 /* FIXME: check better for overflow (native supports over 300 chars) */
1346 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1347 * 4 for exponent and one for
1348 * terminating '\0' */
1349 if (!data->efcvt_buffer)
1350 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1352 if( number < 0) {
1353 *sign = TRUE;
1354 number = -number;
1355 } else
1356 *sign = FALSE;
1357 /* handle cases with zero ndigits or less */
1358 prec = ndigits;
1359 if( prec < 1) prec = 2;
1360 len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1361 /* take the decimal "point away */
1362 if( prec != 1)
1363 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1364 /* take the exponential "e" out */
1365 data->efcvt_buffer[ prec] = '\0';
1366 /* read the exponent */
1367 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1368 (*decpt)++;
1369 /* adjust for some border cases */
1370 if( data->efcvt_buffer[0] == '0')/* value is zero */
1371 *decpt = 0;
1372 /* handle cases with zero ndigits or less */
1373 if( ndigits < 1){
1374 if( data->efcvt_buffer[ 0] >= '5')
1375 (*decpt)++;
1376 data->efcvt_buffer[ 0] = '\0';
1378 TRACE("out=\"%s\"\n",data->efcvt_buffer);
1379 return data->efcvt_buffer;
1382 /*********************************************************************
1383 * _ecvt_s (MSVCRT.@)
1385 int CDECL MSVCRT__ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
1387 int prec, len;
1388 char *result;
1389 const char infret[] = "1#INF";
1391 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
1392 if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
1393 if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
1394 if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1395 if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1397 /* special case - inf */
1398 if(number == HUGE_VAL || number == -HUGE_VAL)
1400 memset(buffer, '0', ndigits);
1401 memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
1402 buffer[ndigits] = '\0';
1403 (*decpt) = 1;
1404 if(number == -HUGE_VAL)
1405 (*sign) = 1;
1406 else
1407 (*sign) = 0;
1408 return 0;
1410 /* handle cases with zero ndigits or less */
1411 prec = ndigits;
1412 if( prec < 1) prec = 2;
1413 result = MSVCRT_malloc(prec + 7);
1415 if( number < 0) {
1416 *sign = TRUE;
1417 number = -number;
1418 } else
1419 *sign = FALSE;
1420 len = snprintf(result, prec + 7, "%.*le", prec - 1, number);
1421 /* take the decimal "point away */
1422 if( prec != 1)
1423 memmove( result + 1, result + 2, len - 1 );
1424 /* take the exponential "e" out */
1425 result[ prec] = '\0';
1426 /* read the exponent */
1427 sscanf( result + prec + 1, "%d", decpt);
1428 (*decpt)++;
1429 /* adjust for some border cases */
1430 if( result[0] == '0')/* value is zero */
1431 *decpt = 0;
1432 /* handle cases with zero ndigits or less */
1433 if( ndigits < 1){
1434 if( result[ 0] >= '5')
1435 (*decpt)++;
1436 result[ 0] = '\0';
1438 memcpy( buffer, result, max(ndigits + 1, 1) );
1439 MSVCRT_free( result );
1440 return 0;
1443 /***********************************************************************
1444 * _fcvt (MSVCRT.@)
1446 char * CDECL MSVCRT__fcvt( double number, int ndigits, int *decpt, int *sign )
1448 thread_data_t *data = msvcrt_get_thread_data();
1449 int stop, dec1, dec2;
1450 char *ptr1, *ptr2, *first;
1451 char buf[80]; /* ought to be enough */
1453 if (!data->efcvt_buffer)
1454 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1456 if (number < 0)
1458 *sign = 1;
1459 number = -number;
1460 } else *sign = 0;
1462 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1463 ptr1 = buf;
1464 ptr2 = data->efcvt_buffer;
1465 first = NULL;
1466 dec1 = 0;
1467 dec2 = 0;
1469 /* For numbers below the requested resolution, work out where
1470 the decimal point will be rather than finding it in the string */
1471 if (number < 1.0 && number > 0.0) {
1472 dec2 = log10(number + 1e-10);
1473 if (-dec2 <= ndigits) dec2 = 0;
1476 /* If requested digits is zero or less, we will need to truncate
1477 * the returned string */
1478 if (ndigits < 1) {
1479 stop = strlen(buf) + ndigits;
1480 } else {
1481 stop = strlen(buf);
1484 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1485 while (*ptr1 != '\0' && *ptr1 != '.') {
1486 if (!first) first = ptr2;
1487 if ((ptr1 - buf) < stop) {
1488 *ptr2++ = *ptr1++;
1489 } else {
1490 ptr1++;
1492 dec1++;
1495 if (ndigits > 0) {
1496 ptr1++;
1497 if (!first) {
1498 while (*ptr1 == '0') { /* Process leading zeroes */
1499 *ptr2++ = *ptr1++;
1500 dec1--;
1503 while (*ptr1 != '\0') {
1504 if (!first) first = ptr2;
1505 *ptr2++ = *ptr1++;
1509 *ptr2 = '\0';
1511 /* We never found a non-zero digit, then our number is either
1512 * smaller than the requested precision, or 0.0 */
1513 if (!first) {
1514 if (number > 0.0) {
1515 first = ptr2;
1516 } else {
1517 first = data->efcvt_buffer;
1518 dec1 = 0;
1522 *decpt = dec2 ? dec2 : dec1;
1523 return first;
1526 /***********************************************************************
1527 * _fcvt_s (MSVCRT.@)
1529 int CDECL MSVCRT__fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
1531 int stop, dec1, dec2;
1532 char *ptr1, *ptr2, *first;
1533 char buf[80]; /* ought to be enough */
1535 if (!outbuffer || !decpt || !sign || size == 0)
1537 *MSVCRT__errno() = MSVCRT_EINVAL;
1538 return MSVCRT_EINVAL;
1541 if (number < 0)
1543 *sign = 1;
1544 number = -number;
1545 } else *sign = 0;
1547 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1548 ptr1 = buf;
1549 ptr2 = outbuffer;
1550 first = NULL;
1551 dec1 = 0;
1552 dec2 = 0;
1554 /* For numbers below the requested resolution, work out where
1555 the decimal point will be rather than finding it in the string */
1556 if (number < 1.0 && number > 0.0) {
1557 dec2 = log10(number + 1e-10);
1558 if (-dec2 <= ndigits) dec2 = 0;
1561 /* If requested digits is zero or less, we will need to truncate
1562 * the returned string */
1563 if (ndigits < 1) {
1564 stop = strlen(buf) + ndigits;
1565 } else {
1566 stop = strlen(buf);
1569 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1570 while (*ptr1 != '\0' && *ptr1 != '.') {
1571 if (!first) first = ptr2;
1572 if ((ptr1 - buf) < stop) {
1573 if (size > 1) {
1574 *ptr2++ = *ptr1++;
1575 size--;
1577 } else {
1578 ptr1++;
1580 dec1++;
1583 if (ndigits > 0) {
1584 ptr1++;
1585 if (!first) {
1586 while (*ptr1 == '0') { /* Process leading zeroes */
1587 if (number == 0.0 && size > 1) {
1588 *ptr2++ = '0';
1589 size--;
1591 ptr1++;
1592 dec1--;
1595 while (*ptr1 != '\0') {
1596 if (!first) first = ptr2;
1597 if (size > 1) {
1598 *ptr2++ = *ptr1++;
1599 size--;
1604 *ptr2 = '\0';
1606 /* We never found a non-zero digit, then our number is either
1607 * smaller than the requested precision, or 0.0 */
1608 if (!first && (number <= 0.0))
1609 dec1 = 0;
1611 *decpt = dec2 ? dec2 : dec1;
1612 return 0;
1615 /***********************************************************************
1616 * _gcvt (MSVCRT.@)
1618 char * CDECL MSVCRT__gcvt( double number, int ndigit, char *buff )
1620 if(!buff) {
1621 *MSVCRT__errno() = MSVCRT_EINVAL;
1622 return NULL;
1625 if(ndigit < 0) {
1626 *MSVCRT__errno() = MSVCRT_ERANGE;
1627 return NULL;
1630 MSVCRT_sprintf(buff, "%.*g", ndigit, number);
1631 return buff;
1634 /***********************************************************************
1635 * _gcvt_s (MSVCRT.@)
1637 int CDECL MSVCRT__gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
1639 int len;
1641 if(!buff) {
1642 *MSVCRT__errno() = MSVCRT_EINVAL;
1643 return MSVCRT_EINVAL;
1646 if( digits<0 || digits>=size) {
1647 if(size)
1648 buff[0] = '\0';
1650 *MSVCRT__errno() = MSVCRT_ERANGE;
1651 return MSVCRT_ERANGE;
1654 len = MSVCRT__scprintf("%.*g", digits, number);
1655 if(len > size) {
1656 buff[0] = '\0';
1657 *MSVCRT__errno() = MSVCRT_ERANGE;
1658 return MSVCRT_ERANGE;
1661 MSVCRT_sprintf(buff, "%.*g", digits, number);
1662 return 0;
1665 #include <stdlib.h> /* div_t, ldiv_t */
1667 /*********************************************************************
1668 * div (MSVCRT.@)
1669 * VERSION
1670 * [i386] Windows binary compatible - returns the struct in eax/edx.
1672 #ifdef __i386__
1673 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1675 div_t dt = div(num,denom);
1676 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1678 #else
1679 /*********************************************************************
1680 * div (MSVCRT.@)
1681 * VERSION
1682 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1684 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1686 div_t dt = div(num,denom);
1687 MSVCRT_div_t ret;
1688 ret.quot = dt.quot;
1689 ret.rem = dt.rem;
1691 return ret;
1694 #endif /* ifdef __i386__ */
1697 /*********************************************************************
1698 * ldiv (MSVCRT.@)
1699 * VERSION
1700 * [i386] Windows binary compatible - returns the struct in eax/edx.
1702 #ifdef __i386__
1703 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1705 ldiv_t ldt = ldiv(num,denom);
1706 return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1708 #else
1709 /*********************************************************************
1710 * ldiv (MSVCRT.@)
1711 * VERSION
1712 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1714 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1716 ldiv_t result = ldiv(num,denom);
1718 MSVCRT_ldiv_t ret;
1719 ret.quot = result.quot;
1720 ret.rem = result.rem;
1722 return ret;
1724 #endif /* ifdef __i386__ */
1726 #ifdef __i386__
1728 /*********************************************************************
1729 * _adjust_fdiv (MSVCRT.@)
1730 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1732 int MSVCRT__adjust_fdiv = 0;
1734 /***********************************************************************
1735 * _adj_fdiv_m16i (MSVCRT.@)
1737 * NOTE
1738 * I _think_ this function is intended to work around the Pentium
1739 * fdiv bug.
1741 void __stdcall _adj_fdiv_m16i( short arg )
1743 TRACE("(): stub\n");
1746 /***********************************************************************
1747 * _adj_fdiv_m32 (MSVCRT.@)
1749 * NOTE
1750 * I _think_ this function is intended to work around the Pentium
1751 * fdiv bug.
1753 void __stdcall _adj_fdiv_m32( unsigned int arg )
1755 TRACE("(): stub\n");
1758 /***********************************************************************
1759 * _adj_fdiv_m32i (MSVCRT.@)
1761 * NOTE
1762 * I _think_ this function is intended to work around the Pentium
1763 * fdiv bug.
1765 void __stdcall _adj_fdiv_m32i( int arg )
1767 TRACE("(): stub\n");
1770 /***********************************************************************
1771 * _adj_fdiv_m64 (MSVCRT.@)
1773 * NOTE
1774 * I _think_ this function is intended to work around the Pentium
1775 * fdiv bug.
1777 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1779 TRACE("(): stub\n");
1782 /***********************************************************************
1783 * _adj_fdiv_r (MSVCRT.@)
1784 * FIXME
1785 * This function is likely to have the wrong number of arguments.
1787 * NOTE
1788 * I _think_ this function is intended to work around the Pentium
1789 * fdiv bug.
1791 void _adj_fdiv_r(void)
1793 TRACE("(): stub\n");
1796 /***********************************************************************
1797 * _adj_fdivr_m16i (MSVCRT.@)
1799 * NOTE
1800 * I _think_ this function is intended to work around the Pentium
1801 * fdiv bug.
1803 void __stdcall _adj_fdivr_m16i( short arg )
1805 TRACE("(): stub\n");
1808 /***********************************************************************
1809 * _adj_fdivr_m32 (MSVCRT.@)
1811 * NOTE
1812 * I _think_ this function is intended to work around the Pentium
1813 * fdiv bug.
1815 void __stdcall _adj_fdivr_m32( unsigned int arg )
1817 TRACE("(): stub\n");
1820 /***********************************************************************
1821 * _adj_fdivr_m32i (MSVCRT.@)
1823 * NOTE
1824 * I _think_ this function is intended to work around the Pentium
1825 * fdiv bug.
1827 void __stdcall _adj_fdivr_m32i( int arg )
1829 TRACE("(): stub\n");
1832 /***********************************************************************
1833 * _adj_fdivr_m64 (MSVCRT.@)
1835 * NOTE
1836 * I _think_ this function is intended to work around the Pentium
1837 * fdiv bug.
1839 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1841 TRACE("(): stub\n");
1844 /***********************************************************************
1845 * _adj_fpatan (MSVCRT.@)
1846 * FIXME
1847 * This function is likely to have the wrong number of arguments.
1849 * NOTE
1850 * I _think_ this function is intended to work around the Pentium
1851 * fdiv bug.
1853 void _adj_fpatan(void)
1855 TRACE("(): stub\n");
1858 /***********************************************************************
1859 * _adj_fprem (MSVCRT.@)
1860 * FIXME
1861 * This function is likely to have the wrong number of arguments.
1863 * NOTE
1864 * I _think_ this function is intended to work around the Pentium
1865 * fdiv bug.
1867 void _adj_fprem(void)
1869 TRACE("(): stub\n");
1872 /***********************************************************************
1873 * _adj_fprem1 (MSVCRT.@)
1874 * FIXME
1875 * This function is likely to have the wrong number of arguments.
1877 * NOTE
1878 * I _think_ this function is intended to work around the Pentium
1879 * fdiv bug.
1881 void _adj_fprem1(void)
1883 TRACE("(): stub\n");
1886 /***********************************************************************
1887 * _adj_fptan (MSVCRT.@)
1888 * FIXME
1889 * This function is likely to have the wrong number of arguments.
1891 * NOTE
1892 * I _think_ this function is intended to work around the Pentium
1893 * fdiv bug.
1895 void _adj_fptan(void)
1897 TRACE("(): stub\n");
1900 /***********************************************************************
1901 * _safe_fdiv (MSVCRT.@)
1902 * FIXME
1903 * This function is likely to have the wrong number of arguments.
1905 * NOTE
1906 * I _think_ this function is intended to work around the Pentium
1907 * fdiv bug.
1909 void _safe_fdiv(void)
1911 TRACE("(): stub\n");
1914 /***********************************************************************
1915 * _safe_fdivr (MSVCRT.@)
1916 * FIXME
1917 * This function is likely to have the wrong number of arguments.
1919 * NOTE
1920 * I _think_ this function is intended to work around the Pentium
1921 * fdiv bug.
1923 void _safe_fdivr(void)
1925 TRACE("(): stub\n");
1928 /***********************************************************************
1929 * _safe_fprem (MSVCRT.@)
1930 * FIXME
1931 * This function is likely to have the wrong number of arguments.
1933 * NOTE
1934 * I _think_ this function is intended to work around the Pentium
1935 * fdiv bug.
1937 void _safe_fprem(void)
1939 TRACE("(): stub\n");
1942 /***********************************************************************
1943 * _safe_fprem1 (MSVCRT.@)
1945 * FIXME
1946 * This function is likely to have the wrong number of arguments.
1948 * NOTE
1949 * I _think_ this function is intended to work around the Pentium
1950 * fdiv bug.
1952 void _safe_fprem1(void)
1954 TRACE("(): stub\n");
1957 /***********************************************************************
1958 * __libm_sse2_acos (MSVCRT.@)
1960 void __cdecl __libm_sse2_acos(void)
1962 double d;
1963 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1964 d = acos( d );
1965 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1968 /***********************************************************************
1969 * __libm_sse2_acosf (MSVCRT.@)
1971 void __cdecl __libm_sse2_acosf(void)
1973 float f;
1974 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
1975 f = acosf( f );
1976 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
1979 /***********************************************************************
1980 * __libm_sse2_asin (MSVCRT.@)
1982 void __cdecl __libm_sse2_asin(void)
1984 double d;
1985 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1986 d = asin( d );
1987 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1990 /***********************************************************************
1991 * __libm_sse2_asinf (MSVCRT.@)
1993 void __cdecl __libm_sse2_asinf(void)
1995 float f;
1996 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
1997 f = asinf( f );
1998 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2001 /***********************************************************************
2002 * __libm_sse2_atan (MSVCRT.@)
2004 void __cdecl __libm_sse2_atan(void)
2006 double d;
2007 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2008 d = atan( d );
2009 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2012 /***********************************************************************
2013 * __libm_sse2_atan2 (MSVCRT.@)
2015 void __cdecl __libm_sse2_atan2(void)
2017 double d1, d2;
2018 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2019 d1 = atan2( d1, d2 );
2020 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2023 /***********************************************************************
2024 * __libm_sse2_atanf (MSVCRT.@)
2026 void __cdecl __libm_sse2_atanf(void)
2028 float f;
2029 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2030 f = atanf( f );
2031 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2034 /***********************************************************************
2035 * __libm_sse2_cos (MSVCRT.@)
2037 void __cdecl __libm_sse2_cos(void)
2039 double d;
2040 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2041 d = cos( d );
2042 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2045 /***********************************************************************
2046 * __libm_sse2_cosf (MSVCRT.@)
2048 void __cdecl __libm_sse2_cosf(void)
2050 float f;
2051 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2052 f = cosf( f );
2053 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2056 /***********************************************************************
2057 * __libm_sse2_exp (MSVCRT.@)
2059 void __cdecl __libm_sse2_exp(void)
2061 double d;
2062 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2063 d = exp( d );
2064 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2067 /***********************************************************************
2068 * __libm_sse2_expf (MSVCRT.@)
2070 void __cdecl __libm_sse2_expf(void)
2072 float f;
2073 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2074 f = expf( f );
2075 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2078 /***********************************************************************
2079 * __libm_sse2_log (MSVCRT.@)
2081 void __cdecl __libm_sse2_log(void)
2083 double d;
2084 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2085 d = log( d );
2086 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2089 /***********************************************************************
2090 * __libm_sse2_log10 (MSVCRT.@)
2092 void __cdecl __libm_sse2_log10(void)
2094 double d;
2095 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2096 d = log10( d );
2097 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2100 /***********************************************************************
2101 * __libm_sse2_log10f (MSVCRT.@)
2103 void __cdecl __libm_sse2_log10f(void)
2105 float f;
2106 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2107 f = log10f( f );
2108 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2111 /***********************************************************************
2112 * __libm_sse2_logf (MSVCRT.@)
2114 void __cdecl __libm_sse2_logf(void)
2116 float f;
2117 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2118 f = logf( f );
2119 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2122 /***********************************************************************
2123 * __libm_sse2_pow (MSVCRT.@)
2125 void __cdecl __libm_sse2_pow(void)
2127 double d1, d2;
2128 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2129 d1 = pow( d1, d2 );
2130 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2133 /***********************************************************************
2134 * __libm_sse2_powf (MSVCRT.@)
2136 void __cdecl __libm_sse2_powf(void)
2138 float f1, f2;
2139 __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
2140 f1 = powf( f1, f2 );
2141 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
2144 /***********************************************************************
2145 * __libm_sse2_sin (MSVCRT.@)
2147 void __cdecl __libm_sse2_sin(void)
2149 double d;
2150 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2151 d = sin( d );
2152 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2155 /***********************************************************************
2156 * __libm_sse2_sinf (MSVCRT.@)
2158 void __cdecl __libm_sse2_sinf(void)
2160 float f;
2161 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2162 f = sinf( f );
2163 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2166 /***********************************************************************
2167 * __libm_sse2_tan (MSVCRT.@)
2169 void __cdecl __libm_sse2_tan(void)
2171 double d;
2172 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2173 d = tan( d );
2174 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2177 /***********************************************************************
2178 * __libm_sse2_tanf (MSVCRT.@)
2180 void __cdecl __libm_sse2_tanf(void)
2182 float f;
2183 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2184 f = tanf( f );
2185 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2188 /***********************************************************************
2189 * __libm_sse2_sqrt_precise (MSVCR110.@)
2191 void __cdecl __libm_sse2_sqrt_precise(void)
2193 double d;
2194 __asm__ __volatile__( "movd %%xmm0,%0" : "=m" (d) );
2195 d = sqrt( d );
2196 __asm__ __volatile__( "movd %0,%%xmm0" : : "m" (d) );
2199 #endif /* __i386__ */