ucrtbase: Add support for natural string widths.
[wine.git] / dlls / msvcrt / math.c
blobcf33f0118e38d3c1346b0057945e5f2eef7c04b6
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 HAVE_FINITEF
38 #define finitef(x) isfinite(x)
39 #endif
41 #ifndef HAVE_ISNANF
42 #ifdef HAVE_ISNAN
43 #define isnanf(x) isnan(x)
44 #else
45 #define isnanf(x) 0
46 #endif
47 #endif
49 /* FIXME: Does not work with -NAN and -0. */
50 #ifndef signbit
51 #define signbit(x) ((x) < 0)
52 #endif
54 typedef int (CDECL *MSVCRT_matherr_func)(struct MSVCRT__exception *);
55 typedef double LDOUBLE; /* long double is just a double */
57 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
59 static BOOL sse2_supported;
60 static BOOL sse2_enabled;
62 void msvcrt_init_math(void)
64 sse2_supported = sse2_enabled = IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );
67 /*********************************************************************
68 * _set_SSE2_enable (MSVCRT.@)
70 int CDECL MSVCRT__set_SSE2_enable(int flag)
72 sse2_enabled = flag && sse2_supported;
73 return sse2_enabled;
76 #ifdef _WIN64
77 /*********************************************************************
78 * _set_FMA3_enable (MSVCR120.@)
80 int CDECL MSVCRT__set_FMA3_enable(int flag)
82 FIXME("(%x) stub\n", flag);
83 return 0;
85 #endif
87 #if defined(__x86_64__) || defined(__arm__) || _MSVCR_VER>=120
89 /*********************************************************************
90 * _chgsignf (MSVCRT.@)
92 float CDECL MSVCRT__chgsignf( float num )
94 /* FIXME: +-infinity,Nan not tested */
95 return -num;
98 /*********************************************************************
99 * _copysignf (MSVCRT.@)
101 float CDECL MSVCRT__copysignf( float num, float sign )
103 if (signbit(sign))
104 return signbit(num) ? num : -num;
105 return signbit(num) ? -num : num;
108 /*********************************************************************
109 * _nextafterf (MSVCRT.@)
111 float CDECL MSVCRT__nextafterf( float num, float next )
113 if (!finitef(num) || !finitef(next)) *MSVCRT__errno() = MSVCRT_EDOM;
114 return nextafterf( num, next );
117 #endif
118 #if defined(__x86_64__) || defined(__arm__)
120 /*********************************************************************
121 * _finitef (MSVCRT.@)
123 int CDECL MSVCRT__finitef( float num )
125 return finitef(num) != 0; /* See comment for _isnan() */
128 /*********************************************************************
129 * _isnanf (MSVCRT.@)
131 INT CDECL MSVCRT__isnanf( float num )
133 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
134 * Do the same, as the result may be used in calculations
136 return isnanf(num) != 0;
139 /*********************************************************************
140 * _logbf (MSVCRT.@)
142 float CDECL MSVCRT__logbf( float num )
144 if (!finitef(num)) *MSVCRT__errno() = MSVCRT_EDOM;
145 return logbf(num);
148 /*********************************************************************
149 * MSVCRT_acosf (MSVCRT.@)
151 float CDECL MSVCRT_acosf( float x )
153 if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
154 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
155 * asin() uses a similar construction. This is bad because as x gets nearer to
156 * 1 the error in the expression "1 - x^2" can get relatively large due to
157 * cancellation. The sqrt() makes things worse. A safer way to calculate
158 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
159 return atan2f(sqrtf((1 - x) * (1 + x)), x);
162 /*********************************************************************
163 * MSVCRT_asinf (MSVCRT.@)
165 float CDECL MSVCRT_asinf( float x )
167 if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
168 return atan2f(x, sqrtf((1 - x) * (1 + x)));
171 /*********************************************************************
172 * MSVCRT_atanf (MSVCRT.@)
174 float CDECL MSVCRT_atanf( float x )
176 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
177 return atanf(x);
180 /*********************************************************************
181 * MSVCRT_atan2f (MSVCRT.@)
183 float CDECL MSVCRT_atan2f( float x, float y )
185 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
186 return atan2f(x,y);
189 /*********************************************************************
190 * MSVCRT_cosf (MSVCRT.@)
192 float CDECL MSVCRT_cosf( float x )
194 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
195 return cosf(x);
198 /*********************************************************************
199 * MSVCRT_coshf (MSVCRT.@)
201 float CDECL MSVCRT_coshf( float x )
203 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
204 return coshf(x);
207 /*********************************************************************
208 * MSVCRT_expf (MSVCRT.@)
210 float CDECL MSVCRT_expf( float x )
212 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
213 return expf(x);
216 /*********************************************************************
217 * MSVCRT_fmodf (MSVCRT.@)
219 float CDECL MSVCRT_fmodf( float x, float y )
221 if (!finitef(x) || !finitef(y)) *MSVCRT__errno() = MSVCRT_EDOM;
222 return fmodf(x,y);
225 /*********************************************************************
226 * MSVCRT_logf (MSVCRT.@)
228 float CDECL MSVCRT_logf( float x)
230 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
231 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
232 return logf(x);
235 /*********************************************************************
236 * MSVCRT_log10f (MSVCRT.@)
238 float CDECL MSVCRT_log10f( float x )
240 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
241 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
242 return log10f(x);
245 /*********************************************************************
246 * MSVCRT_powf (MSVCRT.@)
248 float CDECL MSVCRT_powf( float x, float y )
250 /* FIXME: If x < 0 and y is not integral, set EDOM */
251 float z = powf(x,y);
252 if (!finitef(z)) *MSVCRT__errno() = MSVCRT_EDOM;
253 return z;
256 /*********************************************************************
257 * MSVCRT_sinf (MSVCRT.@)
259 float CDECL MSVCRT_sinf( float x )
261 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
262 return sinf(x);
265 /*********************************************************************
266 * MSVCRT_sinhf (MSVCRT.@)
268 float CDECL MSVCRT_sinhf( float x )
270 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
271 return sinhf(x);
274 /*********************************************************************
275 * MSVCRT_sqrtf (MSVCRT.@)
277 float CDECL MSVCRT_sqrtf( float x )
279 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
280 return sqrtf(x);
283 /*********************************************************************
284 * MSVCRT_tanf (MSVCRT.@)
286 float CDECL MSVCRT_tanf( float x )
288 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
289 return tanf(x);
292 /*********************************************************************
293 * MSVCRT_tanhf (MSVCRT.@)
295 float CDECL MSVCRT_tanhf( float x )
297 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
298 return tanhf(x);
301 /*********************************************************************
302 * ceilf (MSVCRT.@)
304 float CDECL MSVCRT_ceilf( float x )
306 return ceilf(x);
309 /*********************************************************************
310 * fabsf (MSVCRT.@)
312 float CDECL MSVCRT_fabsf( float x )
314 return fabsf(x);
317 /*********************************************************************
318 * floorf (MSVCRT.@)
320 float CDECL MSVCRT_floorf( float x )
322 return floorf(x);
325 /*********************************************************************
326 * frexpf (MSVCRT.@)
328 float CDECL MSVCRT_frexpf( float x, int *exp )
330 return frexpf( x, exp );
333 /*********************************************************************
334 * modff (MSVCRT.@)
336 float CDECL MSVCRT_modff( float x, float *iptr )
338 return modff( x, iptr );
341 #endif
343 /*********************************************************************
344 * MSVCRT_acos (MSVCRT.@)
346 double CDECL MSVCRT_acos( double x )
348 if (x < -1.0 || x > 1.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
349 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
350 * asin() uses a similar construction. This is bad because as x gets nearer to
351 * 1 the error in the expression "1 - x^2" can get relatively large due to
352 * cancellation. The sqrt() makes things worse. A safer way to calculate
353 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
354 return atan2(sqrt((1 - x) * (1 + x)), x);
357 /*********************************************************************
358 * MSVCRT_asin (MSVCRT.@)
360 double CDECL MSVCRT_asin( double x )
362 if (x < -1.0 || x > 1.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
363 return atan2(x, sqrt((1 - x) * (1 + x)));
366 /*********************************************************************
367 * MSVCRT_atan (MSVCRT.@)
369 double CDECL MSVCRT_atan( double x )
371 if (isnan(x)) *MSVCRT__errno() = MSVCRT_EDOM;
372 return atan(x);
375 /*********************************************************************
376 * MSVCRT_atan2 (MSVCRT.@)
378 double CDECL MSVCRT_atan2( double x, double y )
380 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
381 return atan2(x,y);
384 /*********************************************************************
385 * MSVCRT_cos (MSVCRT.@)
387 double CDECL MSVCRT_cos( double x )
389 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
390 return cos(x);
393 /*********************************************************************
394 * MSVCRT_cosh (MSVCRT.@)
396 double CDECL MSVCRT_cosh( double x )
398 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
399 return cosh(x);
402 /*********************************************************************
403 * MSVCRT_exp (MSVCRT.@)
405 double CDECL MSVCRT_exp( double x )
407 if (isnan(x)) *MSVCRT__errno() = MSVCRT_EDOM;
408 return exp(x);
411 /*********************************************************************
412 * MSVCRT_fmod (MSVCRT.@)
414 double CDECL MSVCRT_fmod( double x, double y )
416 if (!isfinite(x) || !isfinite(y)) *MSVCRT__errno() = MSVCRT_EDOM;
417 return fmod(x,y);
420 /*********************************************************************
421 * MSVCRT_log (MSVCRT.@)
423 double CDECL MSVCRT_log( double x)
425 if (x < 0.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
426 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
427 return log(x);
430 /*********************************************************************
431 * MSVCRT_log10 (MSVCRT.@)
433 double CDECL MSVCRT_log10( double x )
435 if (x < 0.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
436 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
437 return log10(x);
440 /*********************************************************************
441 * MSVCRT_pow (MSVCRT.@)
443 double CDECL MSVCRT_pow( double x, double y )
445 /* FIXME: If x < 0 and y is not integral, set EDOM */
446 double z = pow(x,y);
447 if (!isfinite(z)) *MSVCRT__errno() = MSVCRT_EDOM;
448 return z;
451 /*********************************************************************
452 * MSVCRT_sin (MSVCRT.@)
454 double CDECL MSVCRT_sin( double x )
456 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
457 return sin(x);
460 /*********************************************************************
461 * MSVCRT_sinh (MSVCRT.@)
463 double CDECL MSVCRT_sinh( double x )
465 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
466 return sinh(x);
469 /*********************************************************************
470 * MSVCRT_sqrt (MSVCRT.@)
472 double CDECL MSVCRT_sqrt( double x )
474 if (x < 0.0 || !isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
475 return sqrt(x);
478 /*********************************************************************
479 * MSVCRT_tan (MSVCRT.@)
481 double CDECL MSVCRT_tan( double x )
483 if (!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
484 return tan(x);
487 /*********************************************************************
488 * MSVCRT_tanh (MSVCRT.@)
490 double CDECL MSVCRT_tanh( double x )
492 if (isnan(x)) *MSVCRT__errno() = MSVCRT_EDOM;
493 return tanh(x);
497 #if defined(__GNUC__) && defined(__i386__)
499 #define FPU_DOUBLE(var) double var; \
500 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
501 #define FPU_DOUBLES(var1,var2) double var1,var2; \
502 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
503 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
505 /*********************************************************************
506 * _CIacos (MSVCRT.@)
508 double CDECL _CIacos(void)
510 FPU_DOUBLE(x);
511 return MSVCRT_acos(x);
514 /*********************************************************************
515 * _CIasin (MSVCRT.@)
517 double CDECL _CIasin(void)
519 FPU_DOUBLE(x);
520 return MSVCRT_asin(x);
523 /*********************************************************************
524 * _CIatan (MSVCRT.@)
526 double CDECL _CIatan(void)
528 FPU_DOUBLE(x);
529 return MSVCRT_atan(x);
532 /*********************************************************************
533 * _CIatan2 (MSVCRT.@)
535 double CDECL _CIatan2(void)
537 FPU_DOUBLES(x,y);
538 return MSVCRT_atan2(x,y);
541 /*********************************************************************
542 * _CIcos (MSVCRT.@)
544 double CDECL _CIcos(void)
546 FPU_DOUBLE(x);
547 return MSVCRT_cos(x);
550 /*********************************************************************
551 * _CIcosh (MSVCRT.@)
553 double CDECL _CIcosh(void)
555 FPU_DOUBLE(x);
556 return MSVCRT_cosh(x);
559 /*********************************************************************
560 * _CIexp (MSVCRT.@)
562 double CDECL _CIexp(void)
564 FPU_DOUBLE(x);
565 return MSVCRT_exp(x);
568 /*********************************************************************
569 * _CIfmod (MSVCRT.@)
571 double CDECL _CIfmod(void)
573 FPU_DOUBLES(x,y);
574 return MSVCRT_fmod(x,y);
577 /*********************************************************************
578 * _CIlog (MSVCRT.@)
580 double CDECL _CIlog(void)
582 FPU_DOUBLE(x);
583 return MSVCRT_log(x);
586 /*********************************************************************
587 * _CIlog10 (MSVCRT.@)
589 double CDECL _CIlog10(void)
591 FPU_DOUBLE(x);
592 return MSVCRT_log10(x);
595 /*********************************************************************
596 * _CIpow (MSVCRT.@)
598 double CDECL _CIpow(void)
600 FPU_DOUBLES(x,y);
601 return MSVCRT_pow(x,y);
604 /*********************************************************************
605 * _CIsin (MSVCRT.@)
607 double CDECL _CIsin(void)
609 FPU_DOUBLE(x);
610 return MSVCRT_sin(x);
613 /*********************************************************************
614 * _CIsinh (MSVCRT.@)
616 double CDECL _CIsinh(void)
618 FPU_DOUBLE(x);
619 return MSVCRT_sinh(x);
622 /*********************************************************************
623 * _CIsqrt (MSVCRT.@)
625 double CDECL _CIsqrt(void)
627 FPU_DOUBLE(x);
628 return MSVCRT_sqrt(x);
631 /*********************************************************************
632 * _CItan (MSVCRT.@)
634 double CDECL _CItan(void)
636 FPU_DOUBLE(x);
637 return MSVCRT_tan(x);
640 /*********************************************************************
641 * _CItanh (MSVCRT.@)
643 double CDECL _CItanh(void)
645 FPU_DOUBLE(x);
646 return MSVCRT_tanh(x);
649 /*********************************************************************
650 * _ftol (MSVCRT.@)
652 LONGLONG CDECL MSVCRT__ftol(void)
654 FPU_DOUBLE(x);
655 return (LONGLONG)x;
658 #endif /* defined(__GNUC__) && defined(__i386__) */
660 /*********************************************************************
661 * _fpclass (MSVCRT.@)
663 int CDECL MSVCRT__fpclass(double num)
665 #if defined(HAVE_FPCLASS) || defined(fpclass)
666 switch (fpclass( num ))
668 case FP_SNAN: return MSVCRT__FPCLASS_SNAN;
669 case FP_QNAN: return MSVCRT__FPCLASS_QNAN;
670 case FP_NINF: return MSVCRT__FPCLASS_NINF;
671 case FP_PINF: return MSVCRT__FPCLASS_PINF;
672 case FP_NDENORM: return MSVCRT__FPCLASS_ND;
673 case FP_PDENORM: return MSVCRT__FPCLASS_PD;
674 case FP_NZERO: return MSVCRT__FPCLASS_NZ;
675 case FP_PZERO: return MSVCRT__FPCLASS_PZ;
676 case FP_NNORM: return MSVCRT__FPCLASS_NN;
677 case FP_PNORM: return MSVCRT__FPCLASS_PN;
678 default: return MSVCRT__FPCLASS_PN;
680 #elif defined (fpclassify)
681 switch (fpclassify( num ))
683 case FP_NAN: return MSVCRT__FPCLASS_QNAN;
684 case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
685 case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
686 case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
688 return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
689 #else
690 if (!isfinite(num))
691 return MSVCRT__FPCLASS_QNAN;
692 return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
693 #endif
696 /*********************************************************************
697 * _rotl (MSVCRT.@)
699 unsigned int CDECL _rotl(unsigned int num, int shift)
701 shift &= 31;
702 return (num << shift) | (num >> (32-shift));
705 /*********************************************************************
706 * _lrotl (MSVCRT.@)
708 MSVCRT_ulong CDECL MSVCRT__lrotl(MSVCRT_ulong num, int shift)
710 shift &= 0x1f;
711 return (num << shift) | (num >> (32-shift));
714 /*********************************************************************
715 * _lrotr (MSVCRT.@)
717 MSVCRT_ulong CDECL MSVCRT__lrotr(MSVCRT_ulong num, int shift)
719 shift &= 0x1f;
720 return (num >> shift) | (num << (32-shift));
723 /*********************************************************************
724 * _rotr (MSVCRT.@)
726 unsigned int CDECL _rotr(unsigned int num, int shift)
728 shift &= 0x1f;
729 return (num >> shift) | (num << (32-shift));
732 /*********************************************************************
733 * _rotl64 (MSVCRT.@)
735 unsigned __int64 CDECL _rotl64(unsigned __int64 num, int shift)
737 shift &= 63;
738 return (num << shift) | (num >> (64-shift));
741 /*********************************************************************
742 * _rotr64 (MSVCRT.@)
744 unsigned __int64 CDECL _rotr64(unsigned __int64 num, int shift)
746 shift &= 63;
747 return (num >> shift) | (num << (64-shift));
750 /*********************************************************************
751 * abs (MSVCRT.@)
753 int CDECL MSVCRT_abs( int n )
755 return n >= 0 ? n : -n;
758 /*********************************************************************
759 * labs (MSVCRT.@)
761 MSVCRT_long CDECL MSVCRT_labs( MSVCRT_long n )
763 return n >= 0 ? n : -n;
766 /*********************************************************************
767 * llabs (MSVCRT.@)
769 MSVCRT_longlong CDECL MSVCRT_llabs( MSVCRT_longlong n )
771 return n >= 0 ? n : -n;
774 /*********************************************************************
775 * _abs64 (MSVCRT.@)
777 __int64 CDECL _abs64( __int64 n )
779 return n >= 0 ? n : -n;
782 /*********************************************************************
783 * _logb (MSVCRT.@)
785 double CDECL MSVCRT__logb(double num)
787 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
788 return logb(num);
791 /*********************************************************************
792 * _hypot (MSVCRT.@)
794 double CDECL _hypot(double x, double y)
796 /* FIXME: errno handling */
797 return hypot( x, y );
800 /*********************************************************************
801 * _hypotf (MSVCRT.@)
803 float CDECL MSVCRT__hypotf(float x, float y)
805 /* FIXME: errno handling */
806 return hypotf( x, y );
809 /*********************************************************************
810 * ceil (MSVCRT.@)
812 double CDECL MSVCRT_ceil( double x )
814 return ceil(x);
817 /*********************************************************************
818 * floor (MSVCRT.@)
820 double CDECL MSVCRT_floor( double x )
822 return floor(x);
825 /*********************************************************************
826 * fabs (MSVCRT.@)
828 double CDECL MSVCRT_fabs( double x )
830 return fabs(x);
833 /*********************************************************************
834 * frexp (MSVCRT.@)
836 double CDECL MSVCRT_frexp( double x, int *exp )
838 return frexp( x, exp );
841 /*********************************************************************
842 * modf (MSVCRT.@)
844 double CDECL MSVCRT_modf( double x, double *iptr )
846 return modf( x, iptr );
849 /*********************************************************************
850 * _matherr (MSVCRT.@)
852 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
854 if (e)
855 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
856 e->retval);
857 else
858 TRACE("(null)\n");
859 if (MSVCRT_default_matherr_func)
860 return MSVCRT_default_matherr_func(e);
861 ERR(":Unhandled math error!\n");
862 return 0;
865 /*********************************************************************
866 * __setusermatherr (MSVCRT.@)
868 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
870 MSVCRT_default_matherr_func = func;
871 TRACE(":new matherr handler %p\n", func);
874 /**********************************************************************
875 * _statusfp2 (MSVCRT.@)
877 * Not exported by native msvcrt, added in msvcr80.
879 #if defined(__i386__) || defined(__x86_64__)
880 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
882 #ifdef __GNUC__
883 unsigned int flags;
884 unsigned long fpword;
886 if (x86_sw)
888 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
889 flags = 0;
890 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
891 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
892 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
893 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
894 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
895 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
896 *x86_sw = flags;
899 if (!sse2_sw) return;
901 if (sse2_supported)
903 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
904 flags = 0;
905 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
906 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
907 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
908 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
909 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
910 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
911 *sse2_sw = flags;
913 else *sse2_sw = 0;
914 #else
915 FIXME( "not implemented\n" );
916 #endif
918 #endif
920 /**********************************************************************
921 * _statusfp (MSVCRT.@)
923 unsigned int CDECL _statusfp(void)
925 #if defined(__i386__) || defined(__x86_64__)
926 unsigned int x86_sw, sse2_sw;
928 _statusfp2( &x86_sw, &sse2_sw );
929 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
930 return x86_sw | sse2_sw;
931 #else
932 FIXME( "not implemented\n" );
933 return 0;
934 #endif
937 /*********************************************************************
938 * _clearfp (MSVCRT.@)
940 unsigned int CDECL _clearfp(void)
942 unsigned int flags = 0;
943 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
944 unsigned long fpword;
946 __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
947 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
948 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
949 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
950 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
951 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
952 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
954 if (sse2_supported)
956 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
957 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
958 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
959 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
960 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
961 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
962 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
963 fpword &= ~0x3f;
964 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
966 #else
967 FIXME( "not implemented\n" );
968 #endif
969 return flags;
972 /*********************************************************************
973 * __fpecode (MSVCRT.@)
975 int * CDECL __fpecode(void)
977 return &msvcrt_get_thread_data()->fpecode;
980 /*********************************************************************
981 * ldexp (MSVCRT.@)
983 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
985 double z = ldexp(num,exp);
987 if (!isfinite(z))
988 *MSVCRT__errno() = MSVCRT_ERANGE;
989 else if (z == 0 && signbit(z))
990 z = 0.0; /* Convert -0 -> +0 */
991 return z;
994 /*********************************************************************
995 * _cabs (MSVCRT.@)
997 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
999 return sqrt(num.x * num.x + num.y * num.y);
1002 /*********************************************************************
1003 * _chgsign (MSVCRT.@)
1005 double CDECL MSVCRT__chgsign(double num)
1007 /* FIXME: +-infinity,Nan not tested */
1008 return -num;
1011 /*********************************************************************
1012 * __control87_2 (MSVCRT.@)
1014 * Not exported by native msvcrt, added in msvcr80.
1016 #if defined(__i386__) || defined(__x86_64__)
1017 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1018 unsigned int *x86_cw, unsigned int *sse2_cw )
1020 #ifdef __GNUC__
1021 unsigned long fpword;
1022 unsigned int flags;
1024 if (x86_cw)
1026 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1028 /* Convert into mask constants */
1029 flags = 0;
1030 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
1031 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
1032 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
1033 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
1034 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1035 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1036 switch (fpword & 0xc00)
1038 case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1039 case 0x800: flags |= MSVCRT__RC_UP; break;
1040 case 0x400: flags |= MSVCRT__RC_DOWN; break;
1042 switch (fpword & 0x300)
1044 case 0x0: flags |= MSVCRT__PC_24; break;
1045 case 0x200: flags |= MSVCRT__PC_53; break;
1046 case 0x300: flags |= MSVCRT__PC_64; break;
1048 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1050 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1051 if (mask)
1053 flags = (flags & ~mask) | (newval & mask);
1055 /* Convert (masked) value back to fp word */
1056 fpword = 0;
1057 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
1058 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
1059 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1060 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
1061 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
1062 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
1063 switch (flags & MSVCRT__MCW_RC)
1065 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1066 case MSVCRT__RC_UP: fpword |= 0x800; break;
1067 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
1069 switch (flags & MSVCRT__MCW_PC)
1071 case MSVCRT__PC_64: fpword |= 0x300; break;
1072 case MSVCRT__PC_53: fpword |= 0x200; break;
1073 case MSVCRT__PC_24: fpword |= 0x0; break;
1075 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1077 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1079 *x86_cw = flags;
1082 if (!sse2_cw) return 1;
1084 if (sse2_supported)
1086 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1088 /* Convert into mask constants */
1089 flags = 0;
1090 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1091 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1092 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1093 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1094 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1095 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1096 switch (fpword & 0x6000)
1098 case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1099 case 0x4000: flags |= MSVCRT__RC_UP; break;
1100 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1102 switch (fpword & 0x8040)
1104 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1105 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1106 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1109 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1110 if (mask)
1112 flags = (flags & ~mask) | (newval & mask);
1114 /* Convert (masked) value back to fp word */
1115 fpword = 0;
1116 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1117 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1118 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1119 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1120 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1121 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1122 switch (flags & MSVCRT__MCW_RC)
1124 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1125 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1126 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1128 switch (flags & MSVCRT__MCW_DN)
1130 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1131 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1132 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
1134 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1136 *sse2_cw = flags;
1138 else *sse2_cw = 0;
1140 return 1;
1141 #else
1142 FIXME( "not implemented\n" );
1143 return 0;
1144 #endif
1146 #endif
1148 /*********************************************************************
1149 * _control87 (MSVCRT.@)
1151 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1153 #if defined(__i386__) || defined(__x86_64__)
1154 unsigned int x86_cw, sse2_cw;
1156 __control87_2( newval, mask, &x86_cw, &sse2_cw );
1158 if ((x86_cw ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) x86_cw |= MSVCRT__EM_AMBIGUOUS;
1159 return x86_cw;
1160 #else
1161 FIXME( "not implemented\n" );
1162 return 0;
1163 #endif
1166 /*********************************************************************
1167 * _controlfp (MSVCRT.@)
1169 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
1171 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
1174 /*********************************************************************
1175 * _set_controlfp (MSVCRT.@)
1177 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
1179 _controlfp( newval, mask );
1182 /*********************************************************************
1183 * _controlfp_s (MSVCRT.@)
1185 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
1187 static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
1188 MSVCRT__MCW_PC | MSVCRT__MCW_DN);
1189 unsigned int val;
1191 if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
1193 if (cur) *cur = _controlfp( 0, 0 ); /* retrieve it anyway */
1194 return MSVCRT_EINVAL;
1196 val = _controlfp( newval, mask );
1197 if (cur) *cur = val;
1198 return 0;
1201 /*********************************************************************
1202 * fegetenv (MSVCR120.@)
1204 int CDECL MSVCRT_fegetenv(MSVCRT_fenv_t *env)
1206 env->control = _controlfp(0, 0) & (MSVCRT__EM_INEXACT | MSVCRT__EM_UNDERFLOW |
1207 MSVCRT__EM_OVERFLOW | MSVCRT__EM_ZERODIVIDE | MSVCRT__EM_INVALID);
1208 env->status = _statusfp();
1209 return 0;
1212 /*********************************************************************
1213 * __fpe_flt_rounds (UCRTBASE.@)
1215 int CDECL __fpe_flt_rounds(void)
1217 unsigned int fpc = _controlfp(0, 0) & MSVCRT__RC_CHOP;
1219 TRACE("()\n");
1221 switch(fpc) {
1222 case MSVCRT__RC_CHOP: return 0;
1223 case MSVCRT__RC_NEAR: return 1;
1224 #ifdef _WIN64
1225 case MSVCRT__RC_UP: return 3;
1226 default: return 2;
1227 #else
1228 case MSVCRT__RC_UP: return 2;
1229 default: return 3;
1230 #endif
1234 /*********************************************************************
1235 * _copysign (MSVCRT.@)
1237 double CDECL MSVCRT__copysign(double num, double sign)
1239 if (signbit(sign))
1240 return signbit(num) ? num : -num;
1241 return signbit(num) ? -num : num;
1244 /*********************************************************************
1245 * _finite (MSVCRT.@)
1247 int CDECL MSVCRT__finite(double num)
1249 return isfinite(num) != 0; /* See comment for _isnan() */
1252 /*********************************************************************
1253 * _fpreset (MSVCRT.@)
1255 void CDECL _fpreset(void)
1257 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1258 const unsigned int x86_cw = 0x27f;
1259 __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
1260 if (sse2_supported)
1262 const unsigned long sse2_cw = 0x1f80;
1263 __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
1265 #else
1266 FIXME( "not implemented\n" );
1267 #endif
1270 /*********************************************************************
1271 * _isnan (MSVCRT.@)
1273 INT CDECL MSVCRT__isnan(double num)
1275 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1276 * Do the same, as the result may be used in calculations
1278 return isnan(num) != 0;
1281 /*********************************************************************
1282 * _j0 (MSVCRT.@)
1284 double CDECL MSVCRT__j0(double num)
1286 /* FIXME: errno handling */
1287 return j0(num);
1290 /*********************************************************************
1291 * _j1 (MSVCRT.@)
1293 double CDECL MSVCRT__j1(double num)
1295 /* FIXME: errno handling */
1296 return j1(num);
1299 /*********************************************************************
1300 * _jn (MSVCRT.@)
1302 double CDECL MSVCRT__jn(int n, double num)
1304 /* FIXME: errno handling */
1305 return jn(n, num);
1308 /*********************************************************************
1309 * _y0 (MSVCRT.@)
1311 double CDECL MSVCRT__y0(double num)
1313 double retval;
1314 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1315 retval = y0(num);
1316 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1318 *MSVCRT__errno() = MSVCRT_EDOM;
1319 retval = sqrt(-1);
1321 return retval;
1324 /*********************************************************************
1325 * _y1 (MSVCRT.@)
1327 double CDECL MSVCRT__y1(double num)
1329 double retval;
1330 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1331 retval = y1(num);
1332 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1334 *MSVCRT__errno() = MSVCRT_EDOM;
1335 retval = sqrt(-1);
1337 return retval;
1340 /*********************************************************************
1341 * _yn (MSVCRT.@)
1343 double CDECL MSVCRT__yn(int order, double num)
1345 double retval;
1346 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1347 retval = yn(order,num);
1348 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1350 *MSVCRT__errno() = MSVCRT_EDOM;
1351 retval = sqrt(-1);
1353 return retval;
1356 /*********************************************************************
1357 * _nextafter (MSVCRT.@)
1359 double CDECL MSVCRT__nextafter(double num, double next)
1361 double retval;
1362 if (!isfinite(num) || !isfinite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1363 retval = nextafter(num,next);
1364 return retval;
1367 /*********************************************************************
1368 * _ecvt (MSVCRT.@)
1370 char * CDECL MSVCRT__ecvt( double number, int ndigits, int *decpt, int *sign )
1372 int prec, len;
1373 thread_data_t *data = msvcrt_get_thread_data();
1374 /* FIXME: check better for overflow (native supports over 300 chars) */
1375 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1376 * 4 for exponent and one for
1377 * terminating '\0' */
1378 if (!data->efcvt_buffer)
1379 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1381 if( number < 0) {
1382 *sign = TRUE;
1383 number = -number;
1384 } else
1385 *sign = FALSE;
1386 /* handle cases with zero ndigits or less */
1387 prec = ndigits;
1388 if( prec < 1) prec = 2;
1389 len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1390 /* take the decimal "point away */
1391 if( prec != 1)
1392 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1393 /* take the exponential "e" out */
1394 data->efcvt_buffer[ prec] = '\0';
1395 /* read the exponent */
1396 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1397 (*decpt)++;
1398 /* adjust for some border cases */
1399 if( data->efcvt_buffer[0] == '0')/* value is zero */
1400 *decpt = 0;
1401 /* handle cases with zero ndigits or less */
1402 if( ndigits < 1){
1403 if( data->efcvt_buffer[ 0] >= '5')
1404 (*decpt)++;
1405 data->efcvt_buffer[ 0] = '\0';
1407 TRACE("out=\"%s\"\n",data->efcvt_buffer);
1408 return data->efcvt_buffer;
1411 /*********************************************************************
1412 * _ecvt_s (MSVCRT.@)
1414 int CDECL MSVCRT__ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
1416 int prec, len;
1417 char *result;
1418 const char infret[] = "1#INF";
1420 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
1421 if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
1422 if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
1423 if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1424 if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1426 /* special case - inf */
1427 if(number == HUGE_VAL || number == -HUGE_VAL)
1429 memset(buffer, '0', ndigits);
1430 memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
1431 buffer[ndigits] = '\0';
1432 (*decpt) = 1;
1433 if(number == -HUGE_VAL)
1434 (*sign) = 1;
1435 else
1436 (*sign) = 0;
1437 return 0;
1439 /* handle cases with zero ndigits or less */
1440 prec = ndigits;
1441 if( prec < 1) prec = 2;
1442 result = MSVCRT_malloc(prec + 7);
1444 if( number < 0) {
1445 *sign = TRUE;
1446 number = -number;
1447 } else
1448 *sign = FALSE;
1449 len = snprintf(result, prec + 7, "%.*le", prec - 1, number);
1450 /* take the decimal "point away */
1451 if( prec != 1)
1452 memmove( result + 1, result + 2, len - 1 );
1453 /* take the exponential "e" out */
1454 result[ prec] = '\0';
1455 /* read the exponent */
1456 sscanf( result + prec + 1, "%d", decpt);
1457 (*decpt)++;
1458 /* adjust for some border cases */
1459 if( result[0] == '0')/* value is zero */
1460 *decpt = 0;
1461 /* handle cases with zero ndigits or less */
1462 if( ndigits < 1){
1463 if( result[ 0] >= '5')
1464 (*decpt)++;
1465 result[ 0] = '\0';
1467 memcpy( buffer, result, max(ndigits + 1, 1) );
1468 MSVCRT_free( result );
1469 return 0;
1472 /***********************************************************************
1473 * _fcvt (MSVCRT.@)
1475 char * CDECL MSVCRT__fcvt( double number, int ndigits, int *decpt, int *sign )
1477 thread_data_t *data = msvcrt_get_thread_data();
1478 int stop, dec1, dec2;
1479 char *ptr1, *ptr2, *first;
1480 char buf[80]; /* ought to be enough */
1482 if (!data->efcvt_buffer)
1483 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1485 if (number < 0)
1487 *sign = 1;
1488 number = -number;
1489 } else *sign = 0;
1491 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1492 ptr1 = buf;
1493 ptr2 = data->efcvt_buffer;
1494 first = NULL;
1495 dec1 = 0;
1496 dec2 = 0;
1498 /* For numbers below the requested resolution, work out where
1499 the decimal point will be rather than finding it in the string */
1500 if (number < 1.0 && number > 0.0) {
1501 dec2 = log10(number + 1e-10);
1502 if (-dec2 <= ndigits) dec2 = 0;
1505 /* If requested digits is zero or less, we will need to truncate
1506 * the returned string */
1507 if (ndigits < 1) {
1508 stop = strlen(buf) + ndigits;
1509 } else {
1510 stop = strlen(buf);
1513 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1514 while (*ptr1 != '\0' && *ptr1 != '.') {
1515 if (!first) first = ptr2;
1516 if ((ptr1 - buf) < stop) {
1517 *ptr2++ = *ptr1++;
1518 } else {
1519 ptr1++;
1521 dec1++;
1524 if (ndigits > 0) {
1525 ptr1++;
1526 if (!first) {
1527 while (*ptr1 == '0') { /* Process leading zeroes */
1528 *ptr2++ = *ptr1++;
1529 dec1--;
1532 while (*ptr1 != '\0') {
1533 if (!first) first = ptr2;
1534 *ptr2++ = *ptr1++;
1538 *ptr2 = '\0';
1540 /* We never found a non-zero digit, then our number is either
1541 * smaller than the requested precision, or 0.0 */
1542 if (!first) {
1543 if (number > 0.0) {
1544 first = ptr2;
1545 } else {
1546 first = data->efcvt_buffer;
1547 dec1 = 0;
1551 *decpt = dec2 ? dec2 : dec1;
1552 return first;
1555 /***********************************************************************
1556 * _fcvt_s (MSVCRT.@)
1558 int CDECL MSVCRT__fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
1560 int stop, dec1, dec2;
1561 char *ptr1, *ptr2, *first;
1562 char buf[80]; /* ought to be enough */
1564 if (!outbuffer || !decpt || !sign || size == 0)
1566 *MSVCRT__errno() = MSVCRT_EINVAL;
1567 return MSVCRT_EINVAL;
1570 if (number < 0)
1572 *sign = 1;
1573 number = -number;
1574 } else *sign = 0;
1576 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1577 ptr1 = buf;
1578 ptr2 = outbuffer;
1579 first = NULL;
1580 dec1 = 0;
1581 dec2 = 0;
1583 /* For numbers below the requested resolution, work out where
1584 the decimal point will be rather than finding it in the string */
1585 if (number < 1.0 && number > 0.0) {
1586 dec2 = log10(number + 1e-10);
1587 if (-dec2 <= ndigits) dec2 = 0;
1590 /* If requested digits is zero or less, we will need to truncate
1591 * the returned string */
1592 if (ndigits < 1) {
1593 stop = strlen(buf) + ndigits;
1594 } else {
1595 stop = strlen(buf);
1598 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1599 while (*ptr1 != '\0' && *ptr1 != '.') {
1600 if (!first) first = ptr2;
1601 if ((ptr1 - buf) < stop) {
1602 if (size > 1) {
1603 *ptr2++ = *ptr1++;
1604 size--;
1606 } else {
1607 ptr1++;
1609 dec1++;
1612 if (ndigits > 0) {
1613 ptr1++;
1614 if (!first) {
1615 while (*ptr1 == '0') { /* Process leading zeroes */
1616 if (number == 0.0 && size > 1) {
1617 *ptr2++ = '0';
1618 size--;
1620 ptr1++;
1621 dec1--;
1624 while (*ptr1 != '\0') {
1625 if (!first) first = ptr2;
1626 if (size > 1) {
1627 *ptr2++ = *ptr1++;
1628 size--;
1633 *ptr2 = '\0';
1635 /* We never found a non-zero digit, then our number is either
1636 * smaller than the requested precision, or 0.0 */
1637 if (!first && (number <= 0.0))
1638 dec1 = 0;
1640 *decpt = dec2 ? dec2 : dec1;
1641 return 0;
1644 /***********************************************************************
1645 * _gcvt (MSVCRT.@)
1647 char * CDECL MSVCRT__gcvt( double number, int ndigit, char *buff )
1649 if(!buff) {
1650 *MSVCRT__errno() = MSVCRT_EINVAL;
1651 return NULL;
1654 if(ndigit < 0) {
1655 *MSVCRT__errno() = MSVCRT_ERANGE;
1656 return NULL;
1659 MSVCRT_sprintf(buff, "%.*g", ndigit, number);
1660 return buff;
1663 /***********************************************************************
1664 * _gcvt_s (MSVCRT.@)
1666 int CDECL MSVCRT__gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
1668 int len;
1670 if(!buff) {
1671 *MSVCRT__errno() = MSVCRT_EINVAL;
1672 return MSVCRT_EINVAL;
1675 if( digits<0 || digits>=size) {
1676 if(size)
1677 buff[0] = '\0';
1679 *MSVCRT__errno() = MSVCRT_ERANGE;
1680 return MSVCRT_ERANGE;
1683 len = MSVCRT__scprintf("%.*g", digits, number);
1684 if(len > size) {
1685 buff[0] = '\0';
1686 *MSVCRT__errno() = MSVCRT_ERANGE;
1687 return MSVCRT_ERANGE;
1690 MSVCRT_sprintf(buff, "%.*g", digits, number);
1691 return 0;
1694 #include <stdlib.h> /* div_t, ldiv_t */
1696 /*********************************************************************
1697 * div (MSVCRT.@)
1698 * VERSION
1699 * [i386] Windows binary compatible - returns the struct in eax/edx.
1701 #ifdef __i386__
1702 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1704 div_t dt = div(num,denom);
1705 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1707 #else
1708 /*********************************************************************
1709 * div (MSVCRT.@)
1710 * VERSION
1711 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1713 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1715 div_t dt = div(num,denom);
1716 MSVCRT_div_t ret;
1717 ret.quot = dt.quot;
1718 ret.rem = dt.rem;
1720 return ret;
1723 #endif /* ifdef __i386__ */
1726 /*********************************************************************
1727 * ldiv (MSVCRT.@)
1728 * VERSION
1729 * [i386] Windows binary compatible - returns the struct in eax/edx.
1731 #ifdef __i386__
1732 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1734 ldiv_t ldt = ldiv(num,denom);
1735 return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1737 #else
1738 /*********************************************************************
1739 * ldiv (MSVCRT.@)
1740 * VERSION
1741 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1743 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1745 ldiv_t result = ldiv(num,denom);
1747 MSVCRT_ldiv_t ret;
1748 ret.quot = result.quot;
1749 ret.rem = result.rem;
1751 return ret;
1753 #endif /* ifdef __i386__ */
1755 #ifdef __i386__
1757 /*********************************************************************
1758 * _adjust_fdiv (MSVCRT.@)
1759 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1761 int MSVCRT__adjust_fdiv = 0;
1763 /***********************************************************************
1764 * _adj_fdiv_m16i (MSVCRT.@)
1766 * NOTE
1767 * I _think_ this function is intended to work around the Pentium
1768 * fdiv bug.
1770 void __stdcall _adj_fdiv_m16i( short arg )
1772 TRACE("(): stub\n");
1775 /***********************************************************************
1776 * _adj_fdiv_m32 (MSVCRT.@)
1778 * NOTE
1779 * I _think_ this function is intended to work around the Pentium
1780 * fdiv bug.
1782 void __stdcall _adj_fdiv_m32( unsigned int arg )
1784 TRACE("(): stub\n");
1787 /***********************************************************************
1788 * _adj_fdiv_m32i (MSVCRT.@)
1790 * NOTE
1791 * I _think_ this function is intended to work around the Pentium
1792 * fdiv bug.
1794 void __stdcall _adj_fdiv_m32i( int arg )
1796 TRACE("(): stub\n");
1799 /***********************************************************************
1800 * _adj_fdiv_m64 (MSVCRT.@)
1802 * NOTE
1803 * I _think_ this function is intended to work around the Pentium
1804 * fdiv bug.
1806 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1808 TRACE("(): stub\n");
1811 /***********************************************************************
1812 * _adj_fdiv_r (MSVCRT.@)
1813 * FIXME
1814 * This function is likely to have the wrong number of arguments.
1816 * NOTE
1817 * I _think_ this function is intended to work around the Pentium
1818 * fdiv bug.
1820 void _adj_fdiv_r(void)
1822 TRACE("(): stub\n");
1825 /***********************************************************************
1826 * _adj_fdivr_m16i (MSVCRT.@)
1828 * NOTE
1829 * I _think_ this function is intended to work around the Pentium
1830 * fdiv bug.
1832 void __stdcall _adj_fdivr_m16i( short arg )
1834 TRACE("(): stub\n");
1837 /***********************************************************************
1838 * _adj_fdivr_m32 (MSVCRT.@)
1840 * NOTE
1841 * I _think_ this function is intended to work around the Pentium
1842 * fdiv bug.
1844 void __stdcall _adj_fdivr_m32( unsigned int arg )
1846 TRACE("(): stub\n");
1849 /***********************************************************************
1850 * _adj_fdivr_m32i (MSVCRT.@)
1852 * NOTE
1853 * I _think_ this function is intended to work around the Pentium
1854 * fdiv bug.
1856 void __stdcall _adj_fdivr_m32i( int arg )
1858 TRACE("(): stub\n");
1861 /***********************************************************************
1862 * _adj_fdivr_m64 (MSVCRT.@)
1864 * NOTE
1865 * I _think_ this function is intended to work around the Pentium
1866 * fdiv bug.
1868 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1870 TRACE("(): stub\n");
1873 /***********************************************************************
1874 * _adj_fpatan (MSVCRT.@)
1875 * FIXME
1876 * This function is likely to have the wrong number of arguments.
1878 * NOTE
1879 * I _think_ this function is intended to work around the Pentium
1880 * fdiv bug.
1882 void _adj_fpatan(void)
1884 TRACE("(): stub\n");
1887 /***********************************************************************
1888 * _adj_fprem (MSVCRT.@)
1889 * FIXME
1890 * This function is likely to have the wrong number of arguments.
1892 * NOTE
1893 * I _think_ this function is intended to work around the Pentium
1894 * fdiv bug.
1896 void _adj_fprem(void)
1898 TRACE("(): stub\n");
1901 /***********************************************************************
1902 * _adj_fprem1 (MSVCRT.@)
1903 * FIXME
1904 * This function is likely to have the wrong number of arguments.
1906 * NOTE
1907 * I _think_ this function is intended to work around the Pentium
1908 * fdiv bug.
1910 void _adj_fprem1(void)
1912 TRACE("(): stub\n");
1915 /***********************************************************************
1916 * _adj_fptan (MSVCRT.@)
1917 * FIXME
1918 * This function is likely to have the wrong number of arguments.
1920 * NOTE
1921 * I _think_ this function is intended to work around the Pentium
1922 * fdiv bug.
1924 void _adj_fptan(void)
1926 TRACE("(): stub\n");
1929 /***********************************************************************
1930 * _safe_fdiv (MSVCRT.@)
1931 * FIXME
1932 * This function is likely to have the wrong number of arguments.
1934 * NOTE
1935 * I _think_ this function is intended to work around the Pentium
1936 * fdiv bug.
1938 void _safe_fdiv(void)
1940 TRACE("(): stub\n");
1943 /***********************************************************************
1944 * _safe_fdivr (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_fdivr(void)
1954 TRACE("(): stub\n");
1957 /***********************************************************************
1958 * _safe_fprem (MSVCRT.@)
1959 * FIXME
1960 * This function is likely to have the wrong number of arguments.
1962 * NOTE
1963 * I _think_ this function is intended to work around the Pentium
1964 * fdiv bug.
1966 void _safe_fprem(void)
1968 TRACE("(): stub\n");
1971 /***********************************************************************
1972 * _safe_fprem1 (MSVCRT.@)
1974 * FIXME
1975 * This function is likely to have the wrong number of arguments.
1977 * NOTE
1978 * I _think_ this function is intended to work around the Pentium
1979 * fdiv bug.
1981 void _safe_fprem1(void)
1983 TRACE("(): stub\n");
1986 /***********************************************************************
1987 * __libm_sse2_acos (MSVCRT.@)
1989 void __cdecl MSVCRT___libm_sse2_acos(void)
1991 double d;
1992 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
1993 d = acos( d );
1994 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
1997 /***********************************************************************
1998 * __libm_sse2_acosf (MSVCRT.@)
2000 void __cdecl MSVCRT___libm_sse2_acosf(void)
2002 float f;
2003 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2004 f = acosf( f );
2005 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2008 /***********************************************************************
2009 * __libm_sse2_asin (MSVCRT.@)
2011 void __cdecl MSVCRT___libm_sse2_asin(void)
2013 double d;
2014 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2015 d = asin( d );
2016 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2019 /***********************************************************************
2020 * __libm_sse2_asinf (MSVCRT.@)
2022 void __cdecl MSVCRT___libm_sse2_asinf(void)
2024 float f;
2025 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2026 f = asinf( f );
2027 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2030 /***********************************************************************
2031 * __libm_sse2_atan (MSVCRT.@)
2033 void __cdecl MSVCRT___libm_sse2_atan(void)
2035 double d;
2036 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2037 d = atan( d );
2038 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2041 /***********************************************************************
2042 * __libm_sse2_atan2 (MSVCRT.@)
2044 void __cdecl MSVCRT___libm_sse2_atan2(void)
2046 double d1, d2;
2047 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2048 d1 = atan2( d1, d2 );
2049 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2052 /***********************************************************************
2053 * __libm_sse2_atanf (MSVCRT.@)
2055 void __cdecl MSVCRT___libm_sse2_atanf(void)
2057 float f;
2058 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2059 f = atanf( f );
2060 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2063 /***********************************************************************
2064 * __libm_sse2_cos (MSVCRT.@)
2066 void __cdecl MSVCRT___libm_sse2_cos(void)
2068 double d;
2069 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2070 d = cos( d );
2071 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2074 /***********************************************************************
2075 * __libm_sse2_cosf (MSVCRT.@)
2077 void __cdecl MSVCRT___libm_sse2_cosf(void)
2079 float f;
2080 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2081 f = cosf( f );
2082 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2085 /***********************************************************************
2086 * __libm_sse2_exp (MSVCRT.@)
2088 void __cdecl MSVCRT___libm_sse2_exp(void)
2090 double d;
2091 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2092 d = exp( d );
2093 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2096 /***********************************************************************
2097 * __libm_sse2_expf (MSVCRT.@)
2099 void __cdecl MSVCRT___libm_sse2_expf(void)
2101 float f;
2102 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2103 f = expf( f );
2104 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2107 /***********************************************************************
2108 * __libm_sse2_log (MSVCRT.@)
2110 void __cdecl MSVCRT___libm_sse2_log(void)
2112 double d;
2113 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2114 d = log( d );
2115 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2118 /***********************************************************************
2119 * __libm_sse2_log10 (MSVCRT.@)
2121 void __cdecl MSVCRT___libm_sse2_log10(void)
2123 double d;
2124 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2125 d = log10( d );
2126 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2129 /***********************************************************************
2130 * __libm_sse2_log10f (MSVCRT.@)
2132 void __cdecl MSVCRT___libm_sse2_log10f(void)
2134 float f;
2135 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2136 f = log10f( f );
2137 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2140 /***********************************************************************
2141 * __libm_sse2_logf (MSVCRT.@)
2143 void __cdecl MSVCRT___libm_sse2_logf(void)
2145 float f;
2146 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2147 f = logf( f );
2148 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2151 /***********************************************************************
2152 * __libm_sse2_pow (MSVCRT.@)
2154 void __cdecl MSVCRT___libm_sse2_pow(void)
2156 double d1, d2;
2157 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2158 d1 = pow( d1, d2 );
2159 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2162 /***********************************************************************
2163 * __libm_sse2_powf (MSVCRT.@)
2165 void __cdecl MSVCRT___libm_sse2_powf(void)
2167 float f1, f2;
2168 __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
2169 f1 = powf( f1, f2 );
2170 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
2173 /***********************************************************************
2174 * __libm_sse2_sin (MSVCRT.@)
2176 void __cdecl MSVCRT___libm_sse2_sin(void)
2178 double d;
2179 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2180 d = sin( d );
2181 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2184 /***********************************************************************
2185 * __libm_sse2_sinf (MSVCRT.@)
2187 void __cdecl MSVCRT___libm_sse2_sinf(void)
2189 float f;
2190 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2191 f = sinf( f );
2192 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2195 /***********************************************************************
2196 * __libm_sse2_tan (MSVCRT.@)
2198 void __cdecl MSVCRT___libm_sse2_tan(void)
2200 double d;
2201 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2202 d = tan( d );
2203 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2206 /***********************************************************************
2207 * __libm_sse2_tanf (MSVCRT.@)
2209 void __cdecl MSVCRT___libm_sse2_tanf(void)
2211 float f;
2212 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2213 f = tanf( f );
2214 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2217 /***********************************************************************
2218 * __libm_sse2_sqrt_precise (MSVCR110.@)
2220 void __cdecl MSVCRT___libm_sse2_sqrt_precise(void)
2222 double d;
2223 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2224 d = sqrt( d );
2225 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2228 #endif /* __i386__ */
2230 /*********************************************************************
2231 * cbrt (MSVCR120.@)
2233 double CDECL MSVCR120_cbrt(double x)
2235 #ifdef HAVE_CBRT
2236 return cbrt(x);
2237 #else
2238 return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
2239 #endif
2242 /*********************************************************************
2243 * cbrtf (MSVCR120.@)
2245 float CDECL MSVCR120_cbrtf(float x)
2247 #ifdef HAVE_CBRTF
2248 return cbrtf(x);
2249 #else
2250 return MSVCR120_cbrt(x);
2251 #endif
2254 /*********************************************************************
2255 * cbrtl (MSVCR120.@)
2257 LDOUBLE CDECL MSVCR120_cbrtl(LDOUBLE x)
2259 return MSVCR120_cbrt(x);
2262 /*********************************************************************
2263 * exp2 (MSVCR120.@)
2265 double CDECL MSVCR120_exp2(double x)
2267 #ifdef HAVE_EXP2
2268 return exp2(x);
2269 #else
2270 return pow(2, x);
2271 #endif
2274 /*********************************************************************
2275 * exp2f (MSVCR120.@)
2277 float CDECL MSVCR120_exp2f(float x)
2279 #ifdef HAVE_EXP2F
2280 return exp2f(x);
2281 #else
2282 return MSVCR120_exp2(x);
2283 #endif
2286 /*********************************************************************
2287 * exp2l (MSVCR120.@)
2289 LDOUBLE CDECL MSVCR120_exp2l(LDOUBLE x)
2291 return MSVCR120_exp2(x);
2294 /*********************************************************************
2295 * log2 (MSVCR120.@)
2297 double CDECL MSVCR120_log2(double x)
2299 #ifdef HAVE_LOG2
2300 return log2(x);
2301 #else
2302 return log(x) / log(2);
2303 #endif
2306 /*********************************************************************
2307 * log2f (MSVCR120.@)
2309 float CDECL MSVCR120_log2f(float x)
2311 #ifdef HAVE_LOG2F
2312 return log2f(x);
2313 #else
2314 return MSVCR120_log2(x);
2315 #endif
2318 /*********************************************************************
2319 * log2l (MSVCR120.@)
2321 LDOUBLE CDECL MSVCR120_log2l(LDOUBLE x)
2323 return MSVCR120_log2(x);
2326 /*********************************************************************
2327 * rint (MSVCR120.@)
2329 double CDECL MSVCR120_rint(double x)
2331 #ifdef HAVE_RINT
2332 return rint(x);
2333 #else
2334 return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
2335 #endif
2338 /*********************************************************************
2339 * rintf (MSVCR120.@)
2341 float CDECL MSVCR120_rintf(float x)
2343 #ifdef HAVE_RINTF
2344 return rintf(x);
2345 #else
2346 return MSVCR120_rint(x);
2347 #endif
2350 /*********************************************************************
2351 * rintl (MSVCR120.@)
2353 LDOUBLE CDECL MSVCR120_rintl(LDOUBLE x)
2355 return MSVCR120_rint(x);
2358 /*********************************************************************
2359 * lrint (MSVCR120.@)
2361 MSVCRT_long CDECL MSVCR120_lrint(double x)
2363 #ifdef HAVE_LRINT
2364 return lrint(x);
2365 #else
2366 return MSVCR120_rint(x);
2367 #endif
2370 /*********************************************************************
2371 * lrintf (MSVCR120.@)
2373 MSVCRT_long CDECL MSVCR120_lrintf(float x)
2375 #ifdef HAVE_LRINTF
2376 return lrintf(x);
2377 #else
2378 return MSVCR120_lrint(x);
2379 #endif
2382 /*********************************************************************
2383 * lrintl (MSVCR120.@)
2385 MSVCRT_long CDECL MSVCR120_lrintl(LDOUBLE x)
2387 return MSVCR120_lrint(x);
2390 /*********************************************************************
2391 * llrint (MSVCR120.@)
2393 MSVCRT_longlong CDECL MSVCR120_llrint(double x)
2395 #ifdef HAVE_LLRINT
2396 return llrint(x);
2397 #else
2398 return MSVCR120_rint(x);
2399 #endif
2402 /*********************************************************************
2403 * llrintf (MSVCR120.@)
2405 MSVCRT_longlong CDECL MSVCR120_llrintf(float x)
2407 #ifdef HAVE_LLRINTF
2408 return llrintf(x);
2409 #else
2410 return MSVCR120_llrint(x);
2411 #endif
2414 /*********************************************************************
2415 * rintl (MSVCR120.@)
2417 MSVCRT_longlong CDECL MSVCR120_llrintl(LDOUBLE x)
2419 return MSVCR120_llrint(x);
2422 /*********************************************************************
2423 * round (MSVCR120.@)
2425 double CDECL MSVCR120_round(double x)
2427 #ifdef HAVE_ROUND
2428 return round(x);
2429 #else
2430 return MSVCR120_rint(x);
2431 #endif
2434 /*********************************************************************
2435 * roundf (MSVCR120.@)
2437 float CDECL MSVCR120_roundf(float x)
2439 #ifdef HAVE_ROUNDF
2440 return roundf(x);
2441 #else
2442 return MSVCR120_round(x);
2443 #endif
2446 /*********************************************************************
2447 * roundl (MSVCR120.@)
2449 LDOUBLE CDECL MSVCR120_roundl(LDOUBLE x)
2451 return MSVCR120_round(x);
2454 /*********************************************************************
2455 * lround (MSVCR120.@)
2457 MSVCRT_long CDECL MSVCR120_lround(double x)
2459 #ifdef HAVE_LROUND
2460 return lround(x);
2461 #else
2462 return MSVCR120_round(x);
2463 #endif
2466 /*********************************************************************
2467 * lroundf (MSVCR120.@)
2469 MSVCRT_long CDECL MSVCR120_lroundf(float x)
2471 #ifdef HAVE_LROUNDF
2472 return lroundf(x);
2473 #else
2474 return MSVCR120_lround(x);
2475 #endif
2478 /*********************************************************************
2479 * lroundl (MSVCR120.@)
2481 MSVCRT_long CDECL MSVCR120_lroundl(LDOUBLE x)
2483 return MSVCR120_lround(x);
2486 /*********************************************************************
2487 * llround (MSVCR120.@)
2489 MSVCRT_longlong CDECL MSVCR120_llround(double x)
2491 #ifdef HAVE_LLROUND
2492 return llround(x);
2493 #else
2494 return MSVCR120_round(x);
2495 #endif
2498 /*********************************************************************
2499 * llroundf (MSVCR120.@)
2501 MSVCRT_longlong CDECL MSVCR120_llroundf(float x)
2503 #ifdef HAVE_LLROUNDF
2504 return llroundf(x);
2505 #else
2506 return MSVCR120_llround(x);
2507 #endif
2510 /*********************************************************************
2511 * roundl (MSVCR120.@)
2513 MSVCRT_longlong CDECL MSVCR120_llroundl(LDOUBLE x)
2515 return MSVCR120_llround(x);
2518 /*********************************************************************
2519 * trunc (MSVCR120.@)
2521 double CDECL MSVCR120_trunc(double x)
2523 #ifdef HAVE_TRUNC
2524 return trunc(x);
2525 #else
2526 return (x > 0) ? floor(x) : ceil(x);
2527 #endif
2530 /*********************************************************************
2531 * truncf (MSVCR120.@)
2533 float CDECL MSVCR120_truncf(float x)
2535 #ifdef HAVE_TRUNCF
2536 return truncf(x);
2537 #else
2538 return MSVCR120_trunc(x);
2539 #endif
2542 /*********************************************************************
2543 * truncl (MSVCR120.@)
2545 LDOUBLE CDECL MSVCR120_truncl(LDOUBLE x)
2547 return MSVCR120_trunc(x);
2550 /*********************************************************************
2551 * _dclass (MSVCR120.@)
2553 short CDECL MSVCR120__dclass(double x)
2555 switch (MSVCRT__fpclass(x)) {
2556 case MSVCRT__FPCLASS_QNAN:
2557 case MSVCRT__FPCLASS_SNAN:
2558 return MSVCRT_FP_NAN;
2559 case MSVCRT__FPCLASS_NINF:
2560 case MSVCRT__FPCLASS_PINF:
2561 return MSVCRT_FP_INFINITE;
2562 case MSVCRT__FPCLASS_ND:
2563 case MSVCRT__FPCLASS_PD:
2564 return MSVCRT_FP_SUBNORMAL;
2565 case MSVCRT__FPCLASS_NN:
2566 case MSVCRT__FPCLASS_PN:
2567 default:
2568 return MSVCRT_FP_NORMAL;
2569 case MSVCRT__FPCLASS_NZ:
2570 case MSVCRT__FPCLASS_PZ:
2571 return MSVCRT_FP_ZERO;
2575 /*********************************************************************
2576 * _fdclass (MSVCR120.@)
2578 short CDECL MSVCR120__fdclass(float x)
2580 return MSVCR120__dclass(x);
2583 /*********************************************************************
2584 * _ldclass (MSVCR120.@)
2586 short CDECL MSVCR120__ldclass(LDOUBLE x)
2588 return MSVCR120__dclass(x);
2591 /*********************************************************************
2592 * _dtest (MSVCR120.@)
2594 short CDECL MSVCR120__dtest(double *x)
2596 return MSVCR120__dclass(*x);
2599 /*********************************************************************
2600 * _fdtest (MSVCR120.@)
2602 short CDECL MSVCR120__fdtest(float *x)
2604 return MSVCR120__dclass(*x);
2607 /*********************************************************************
2608 * _ldtest (MSVCR120.@)
2610 short CDECL MSVCR120__ldtest(LDOUBLE *x)
2612 return MSVCR120__dclass(*x);
2615 /*********************************************************************
2616 * erff (MSVCR120.@)
2618 float CDECL MSVCR120_erff(float x)
2620 #ifdef HAVE_ERFF
2621 return erff(x);
2622 #else
2623 FIXME( "not implemented\n" );
2624 return 0.0f;
2625 #endif
2628 /*********************************************************************
2629 * erf (MSVCR120.@)
2631 double CDECL MSVCR120_erf(double x)
2633 #ifdef HAVE_ERF
2634 return erf(x);
2635 #else
2636 FIXME( "not implemented\n" );
2637 return 0.0;
2638 #endif
2641 /*********************************************************************
2642 * erfl (MSVCR120.@)
2644 LDOUBLE CDECL MSVCR120_erfl(LDOUBLE x)
2646 return MSVCR120_erf(x);
2649 /*********************************************************************
2650 * fmaxf (MSVCR120.@)
2652 float CDECL MSVCR120_fmaxf(float x, float y)
2654 if(isnanf(x))
2655 return y;
2656 if(isnanf(y))
2657 return x;
2658 if(x==0 && y==0)
2659 return signbit(x) ? y : x;
2660 return x<y ? y : x;
2663 /*********************************************************************
2664 * fmax (MSVCR120.@)
2666 double CDECL MSVCR120_fmax(double x, double y)
2668 if(isnan(x))
2669 return y;
2670 if(isnan(y))
2671 return x;
2672 if(x==0 && y==0)
2673 return signbit(x) ? y : x;
2674 return x<y ? y : x;
2677 /*********************************************************************
2678 * _fdsign (MSVCR120.@)
2680 int CDECL MSVCR120__fdsign(float x)
2682 return signbit(x) ? 0x8000 : 0;
2685 /*********************************************************************
2686 * _dsign (MSVCR120.@)
2688 int CDECL MSVCR120__dsign(double x)
2690 return signbit(x) ? 0x8000 : 0;
2694 /*********************************************************************
2695 * _dpcomp (MSVCR120.@)
2697 int CDECL MSVCR120__dpcomp(double x, double y)
2699 if(isnan(x) || isnan(y))
2700 return 0;
2702 if(x == y) return 2;
2703 return x < y ? 1 : 4;
2706 /*********************************************************************
2707 * _fdpcomp (MSVCR120.@)
2709 int CDECL MSVCR120__fdpcomp(float x, float y)
2711 return MSVCR120__dpcomp(x, y);
2714 /*********************************************************************
2715 * fminf (MSVCR120.@)
2717 float CDECL MSVCR120_fminf(float x, float y)
2719 if(isnanf(x))
2720 return y;
2721 if(isnanf(y))
2722 return x;
2723 if(x==0 && y==0)
2724 return signbit(x) ? x : y;
2725 return x<y ? x : y;
2728 /*********************************************************************
2729 * fmin (MSVCR120.@)
2731 double CDECL MSVCR120_fmin(double x, double y)
2733 if(isnan(x))
2734 return y;
2735 if(isnan(y))
2736 return x;
2737 if(x==0 && y==0)
2738 return signbit(x) ? x : y;
2739 return x<y ? x : y;
2742 /*********************************************************************
2743 * asinh (MSVCR120.@)
2745 double CDECL MSVCR120_asinh(double x)
2747 #ifdef HAVE_ASINH
2748 return asinh(x);
2749 #else
2750 FIXME( "not implemented\n" );
2751 return 0.0;
2752 #endif
2755 /*********************************************************************
2756 * asinhf (MSVCR120.@)
2758 float CDECL MSVCR120_asinhf(float x)
2760 #ifdef HAVE_ASINHF
2761 return asinhf(x);
2762 #else
2763 FIXME( "not implemented\n" );
2764 return 0.0f;
2765 #endif
2768 /*********************************************************************
2769 * asinhl (MSVCR120.@)
2771 LDOUBLE CDECL MSVCR120_asinhl(LDOUBLE x)
2773 return MSVCR120_asinh(x);
2776 /*********************************************************************
2777 * _scalb (MSVCRT.@)
2778 * scalbn (MSVCR120.@)
2779 * scalbln (MSVCR120.@)
2781 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
2783 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
2784 return ldexp(num, power);
2787 /*********************************************************************
2788 * _scalbf (MSVCRT.@)
2789 * scalbnf (MSVCR120.@)
2790 * scalblnf (MSVCR120.@)
2792 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
2794 if (!finitef(num)) *MSVCRT__errno() = MSVCRT_EDOM;
2795 return ldexpf(num, power);
2798 /*********************************************************************
2799 * scalbnl (MSVCR120.@)
2800 * scalblnl (MSVCR120.@)
2802 LDOUBLE CDECL MSVCR120_scalbnl(LDOUBLE num, MSVCRT_long power)
2804 return MSVCRT__scalb(num, power);
2807 /*********************************************************************
2808 * remainder (MSVCR120.@)
2810 double CDECL MSVCR120_remainder(double x, double y)
2812 #ifdef HAVE_REMAINDER
2813 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
2814 if(!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
2815 if(isnan(y) || y==0.0) *MSVCRT__errno() = MSVCRT_EDOM;
2816 return remainder(x, y);
2817 #else
2818 FIXME( "not implemented\n" );
2819 return 0.0;
2820 #endif
2823 /*********************************************************************
2824 * remainderf (MSVCR120.@)
2826 float CDECL MSVCR120_remainderf(float x, float y)
2828 #ifdef HAVE_REMAINDERF
2829 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
2830 if(!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
2831 if(isnanf(y) || y==0.0f) *MSVCRT__errno() = MSVCRT_EDOM;
2832 return remainderf(x, y);
2833 #else
2834 FIXME( "not implemented\n" );
2835 return 0.0f;
2836 #endif
2839 /*********************************************************************
2840 * remainderl (MSVCR120.@)
2842 LDOUBLE CDECL MSVCR120_remainderl(LDOUBLE x, LDOUBLE y)
2844 return MSVCR120_remainder(x, y);