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
23 #define __USE_ISOC9X 1
24 #define __USE_ISOC99 1
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
37 #ifndef finite /* Could be a macro */
39 #define finite(x) isfinite(x)
41 #define finite(x) (!isnan(x)) /* At least catch some cases */
50 typedef int (CDECL
*MSVCRT_matherr_func
)(struct MSVCRT__exception
*);
52 static MSVCRT_matherr_func MSVCRT_default_matherr_func
= NULL
;
54 static BOOL sse2_supported
;
55 static BOOL sse2_enabled
;
57 void msvcrt_init_math(void)
59 sse2_supported
= sse2_enabled
= IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE
);
62 /*********************************************************************
63 * _set_SSE2_enable (MSVCRT.@)
65 int CDECL
MSVCRT__set_SSE2_enable(int flag
)
67 sse2_enabled
= flag
&& sse2_supported
;
73 /*********************************************************************
74 * MSVCRT_acosf (MSVCRT.@)
76 float CDECL
MSVCRT_acosf( float x
)
78 if (x
< -1.0 || x
> 1.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
79 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
80 * asin() uses a similar construction. This is bad because as x gets nearer to
81 * 1 the error in the expression "1 - x^2" can get relatively large due to
82 * cancellation. The sqrt() makes things worse. A safer way to calculate
83 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
84 return atan2f(sqrtf((1 - x
) * (1 + x
)), x
);
87 /*********************************************************************
88 * MSVCRT_asinf (MSVCRT.@)
90 float CDECL
MSVCRT_asinf( float x
)
92 if (x
< -1.0 || x
> 1.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
93 return atan2f(x
, sqrtf((1 - x
) * (1 + x
)));
96 /*********************************************************************
97 * MSVCRT_atanf (MSVCRT.@)
99 float CDECL
MSVCRT_atanf( float x
)
101 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
105 /*********************************************************************
106 * MSVCRT_atan2f (MSVCRT.@)
108 float CDECL
MSVCRT_atan2f( float x
, float y
)
110 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
114 /*********************************************************************
115 * MSVCRT_cosf (MSVCRT.@)
117 float CDECL
MSVCRT_cosf( float x
)
119 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
123 /*********************************************************************
124 * MSVCRT_coshf (MSVCRT.@)
126 float CDECL
MSVCRT_coshf( float x
)
128 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
132 /*********************************************************************
133 * MSVCRT_expf (MSVCRT.@)
135 float CDECL
MSVCRT_expf( float x
)
137 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
141 /*********************************************************************
142 * MSVCRT_fmodf (MSVCRT.@)
144 float CDECL
MSVCRT_fmodf( float x
, float y
)
146 if (!finitef(x
) || !finitef(y
)) *MSVCRT__errno() = MSVCRT_EDOM
;
150 /*********************************************************************
151 * MSVCRT_logf (MSVCRT.@)
153 float CDECL
MSVCRT_logf( float x
)
155 if (x
< 0.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
156 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
160 /*********************************************************************
161 * MSVCRT_log10f (MSVCRT.@)
163 float CDECL
MSVCRT_log10f( float x
)
165 if (x
< 0.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
166 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
170 /*********************************************************************
171 * MSVCRT_powf (MSVCRT.@)
173 float CDECL
MSVCRT_powf( float x
, float y
)
175 /* FIXME: If x < 0 and y is not integral, set EDOM */
177 if (!finitef(z
)) *MSVCRT__errno() = MSVCRT_EDOM
;
181 /*********************************************************************
182 * MSVCRT_sinf (MSVCRT.@)
184 float CDECL
MSVCRT_sinf( float x
)
186 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
190 /*********************************************************************
191 * MSVCRT_sinhf (MSVCRT.@)
193 float CDECL
MSVCRT_sinhf( float x
)
195 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
199 /*********************************************************************
200 * MSVCRT_sqrtf (MSVCRT.@)
202 float CDECL
MSVCRT_sqrtf( float x
)
204 if (x
< 0.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
208 /*********************************************************************
209 * MSVCRT_tanf (MSVCRT.@)
211 float CDECL
MSVCRT_tanf( float x
)
213 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
217 /*********************************************************************
218 * MSVCRT_tanhf (MSVCRT.@)
220 float CDECL
MSVCRT_tanhf( float x
)
222 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
226 /*********************************************************************
229 float CDECL
MSVCRT_ceilf( float x
)
234 /*********************************************************************
237 float CDECL
MSVCRT_floorf( float x
)
242 /*********************************************************************
245 float CDECL
MSVCRT_frexpf( float x
, int *exp
)
247 return frexpf( x
, exp
);
250 /*********************************************************************
253 float CDECL
MSVCRT__scalbf(float num
, MSVCRT_long power
)
255 if (!finitef(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
256 return ldexpf(num
, power
);
259 /*********************************************************************
262 double CDECL
MSVCRT_modff( float x
, float *iptr
)
264 return modff( x
, iptr
);
269 /*********************************************************************
270 * MSVCRT_acos (MSVCRT.@)
272 double CDECL
MSVCRT_acos( double x
)
274 if (x
< -1.0 || x
> 1.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
275 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
276 * asin() uses a similar construction. This is bad because as x gets nearer to
277 * 1 the error in the expression "1 - x^2" can get relatively large due to
278 * cancellation. The sqrt() makes things worse. A safer way to calculate
279 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
280 return atan2(sqrt((1 - x
) * (1 + x
)), x
);
283 /*********************************************************************
284 * MSVCRT_asin (MSVCRT.@)
286 double CDECL
MSVCRT_asin( double x
)
288 if (x
< -1.0 || x
> 1.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
289 return atan2(x
, sqrt((1 - x
) * (1 + x
)));
292 /*********************************************************************
293 * MSVCRT_atan (MSVCRT.@)
295 double CDECL
MSVCRT_atan( double x
)
297 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
301 /*********************************************************************
302 * MSVCRT_atan2 (MSVCRT.@)
304 double CDECL
MSVCRT_atan2( double x
, double y
)
306 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
310 /*********************************************************************
311 * MSVCRT_cos (MSVCRT.@)
313 double CDECL
MSVCRT_cos( double x
)
315 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
319 /*********************************************************************
320 * MSVCRT_cosh (MSVCRT.@)
322 double CDECL
MSVCRT_cosh( double x
)
324 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
328 /*********************************************************************
329 * MSVCRT_exp (MSVCRT.@)
331 double CDECL
MSVCRT_exp( double x
)
333 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
337 /*********************************************************************
338 * MSVCRT_fmod (MSVCRT.@)
340 double CDECL
MSVCRT_fmod( double x
, double y
)
342 if (!finite(x
) || !finite(y
)) *MSVCRT__errno() = MSVCRT_EDOM
;
346 /*********************************************************************
347 * MSVCRT_log (MSVCRT.@)
349 double CDECL
MSVCRT_log( double x
)
351 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
352 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
356 /*********************************************************************
357 * MSVCRT_log10 (MSVCRT.@)
359 double CDECL
MSVCRT_log10( double x
)
361 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
362 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
366 /*********************************************************************
367 * MSVCRT_pow (MSVCRT.@)
369 double CDECL
MSVCRT_pow( double x
, double y
)
371 /* FIXME: If x < 0 and y is not integral, set EDOM */
373 if (!finite(z
)) *MSVCRT__errno() = MSVCRT_EDOM
;
377 /*********************************************************************
378 * MSVCRT_sin (MSVCRT.@)
380 double CDECL
MSVCRT_sin( double x
)
382 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
386 /*********************************************************************
387 * MSVCRT_sinh (MSVCRT.@)
389 double CDECL
MSVCRT_sinh( double x
)
391 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
395 /*********************************************************************
396 * MSVCRT_sqrt (MSVCRT.@)
398 double CDECL
MSVCRT_sqrt( double x
)
400 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
404 /*********************************************************************
405 * MSVCRT_tan (MSVCRT.@)
407 double CDECL
MSVCRT_tan( double x
)
409 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
413 /*********************************************************************
414 * MSVCRT_tanh (MSVCRT.@)
416 double CDECL
MSVCRT_tanh( double x
)
418 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
423 #if defined(__GNUC__) && defined(__i386__)
425 #define FPU_DOUBLE(var) double var; \
426 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
427 #define FPU_DOUBLES(var1,var2) double var1,var2; \
428 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
429 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
431 /*********************************************************************
434 double CDECL
_CIacos(void)
437 return MSVCRT_acos(x
);
440 /*********************************************************************
443 double CDECL
_CIasin(void)
446 return MSVCRT_asin(x
);
449 /*********************************************************************
452 double CDECL
_CIatan(void)
455 return MSVCRT_atan(x
);
458 /*********************************************************************
459 * _CIatan2 (MSVCRT.@)
461 double CDECL
_CIatan2(void)
464 return MSVCRT_atan2(x
,y
);
467 /*********************************************************************
470 double CDECL
_CIcos(void)
473 return MSVCRT_cos(x
);
476 /*********************************************************************
479 double CDECL
_CIcosh(void)
482 return MSVCRT_cosh(x
);
485 /*********************************************************************
488 double CDECL
_CIexp(void)
491 return MSVCRT_exp(x
);
494 /*********************************************************************
497 double CDECL
_CIfmod(void)
500 return MSVCRT_fmod(x
,y
);
503 /*********************************************************************
506 double CDECL
_CIlog(void)
509 return MSVCRT_log(x
);
512 /*********************************************************************
513 * _CIlog10 (MSVCRT.@)
515 double CDECL
_CIlog10(void)
518 return MSVCRT_log10(x
);
521 /*********************************************************************
524 double CDECL
_CIpow(void)
527 return MSVCRT_pow(x
,y
);
530 /*********************************************************************
533 double CDECL
_CIsin(void)
536 return MSVCRT_sin(x
);
539 /*********************************************************************
542 double CDECL
_CIsinh(void)
545 return MSVCRT_sinh(x
);
548 /*********************************************************************
551 double CDECL
_CIsqrt(void)
554 return MSVCRT_sqrt(x
);
557 /*********************************************************************
560 double CDECL
_CItan(void)
563 return MSVCRT_tan(x
);
566 /*********************************************************************
569 double CDECL
_CItanh(void)
572 return MSVCRT_tanh(x
);
575 #endif /* defined(__GNUC__) && defined(__i386__) */
577 /*********************************************************************
578 * _fpclass (MSVCRT.@)
580 int CDECL
_fpclass(double num
)
582 #if defined(HAVE_FPCLASS) || defined(fpclass)
583 switch (fpclass( num
))
586 case FP_SNAN
: return MSVCRT__FPCLASS_SNAN
;
589 case FP_QNAN
: return MSVCRT__FPCLASS_QNAN
;
592 case FP_NINF
: return MSVCRT__FPCLASS_NINF
;
595 case FP_PINF
: return MSVCRT__FPCLASS_PINF
;
598 case FP_NDENORM
: return MSVCRT__FPCLASS_ND
;
601 case FP_PDENORM
: return MSVCRT__FPCLASS_PD
;
604 case FP_NZERO
: return MSVCRT__FPCLASS_NZ
;
607 case FP_PZERO
: return MSVCRT__FPCLASS_PZ
;
610 case FP_NNORM
: return MSVCRT__FPCLASS_NN
;
613 case FP_PNORM
: return MSVCRT__FPCLASS_PN
;
615 default: return MSVCRT__FPCLASS_PN
;
617 #elif defined (fpclassify)
618 switch (fpclassify( num
))
620 case FP_NAN
: return MSVCRT__FPCLASS_QNAN
;
621 case FP_INFINITE
: return signbit(num
) ? MSVCRT__FPCLASS_NINF
: MSVCRT__FPCLASS_PINF
;
622 case FP_SUBNORMAL
: return signbit(num
) ?MSVCRT__FPCLASS_ND
: MSVCRT__FPCLASS_PD
;
623 case FP_ZERO
: return signbit(num
) ? MSVCRT__FPCLASS_NZ
: MSVCRT__FPCLASS_PZ
;
625 return signbit(num
) ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
;
628 return MSVCRT__FPCLASS_QNAN
;
629 return num
== 0.0 ? MSVCRT__FPCLASS_PZ
: (num
< 0 ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
);
633 /*********************************************************************
636 unsigned int CDECL
_rotl(unsigned int num
, int shift
)
639 return (num
<< shift
) | (num
>> (32-shift
));
642 /*********************************************************************
645 double CDECL
_logb(double num
)
647 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
651 /*********************************************************************
654 MSVCRT_ulong CDECL
MSVCRT__lrotl(MSVCRT_ulong num
, int shift
)
657 return (num
<< shift
) | (num
>> (32-shift
));
660 /*********************************************************************
663 MSVCRT_ulong CDECL
MSVCRT__lrotr(MSVCRT_ulong num
, int shift
)
666 return (num
>> shift
) | (num
<< (32-shift
));
669 /*********************************************************************
672 unsigned int CDECL
_rotr(unsigned int num
, int shift
)
675 return (num
>> shift
) | (num
<< (32-shift
));
678 /*********************************************************************
681 double CDECL
MSVCRT__scalb(double num
, MSVCRT_long power
)
683 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
684 return ldexp(num
, power
);
687 /*********************************************************************
690 double CDECL
_hypot(double x
, double y
)
692 /* FIXME: errno handling */
693 return hypot( x
, y
);
696 /*********************************************************************
699 float CDECL
_hypotf(float x
, float y
)
701 /* FIXME: errno handling */
702 return hypotf( x
, y
);
705 /*********************************************************************
708 double CDECL
MSVCRT_ceil( double x
)
713 /*********************************************************************
716 double CDECL
MSVCRT_floor( double x
)
721 /*********************************************************************
724 double CDECL
MSVCRT_fabs( double x
)
729 /*********************************************************************
732 double CDECL
MSVCRT_frexp( double x
, int *exp
)
734 return frexp( x
, exp
);
737 /*********************************************************************
740 double CDECL
MSVCRT_modf( double x
, double *iptr
)
742 return modf( x
, iptr
);
745 /*********************************************************************
746 * _matherr (MSVCRT.@)
748 int CDECL
MSVCRT__matherr(struct MSVCRT__exception
*e
)
751 TRACE("(%p = %d, %s, %g %g %g)\n",e
, e
->type
, e
->name
, e
->arg1
, e
->arg2
,
755 if (MSVCRT_default_matherr_func
)
756 return MSVCRT_default_matherr_func(e
);
757 ERR(":Unhandled math error!\n");
761 /*********************************************************************
762 * __setusermatherr (MSVCRT.@)
764 void CDECL
MSVCRT___setusermatherr(MSVCRT_matherr_func func
)
766 MSVCRT_default_matherr_func
= func
;
767 TRACE(":new matherr handler %p\n", func
);
770 /**********************************************************************
771 * _statusfp2 (MSVCRT.@)
773 * Not exported by native msvcrt, added in msvcr80.
775 #if defined(__i386__) || defined(__x86_64__)
776 void CDECL
_statusfp2( unsigned int *x86_sw
, unsigned int *sse2_sw
)
780 unsigned long fpword
;
784 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) );
786 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
787 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
788 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
789 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
790 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
791 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
795 if (!sse2_sw
) return;
799 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
801 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
802 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
803 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
804 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
805 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
806 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
811 FIXME( "not implemented\n" );
816 /**********************************************************************
817 * _statusfp (MSVCRT.@)
819 unsigned int CDECL
_statusfp(void)
821 #if defined(__i386__) || defined(__x86_64__)
822 unsigned int x86_sw
, sse2_sw
;
824 _statusfp2( &x86_sw
, &sse2_sw
);
825 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
826 return x86_sw
| sse2_sw
;
828 FIXME( "not implemented\n" );
833 /*********************************************************************
834 * _clearfp (MSVCRT.@)
836 unsigned int CDECL
_clearfp(void)
838 unsigned int flags
= 0;
839 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
840 unsigned long fpword
;
842 __asm__
__volatile__( "fnstsw %0; fnclex" : "=m" (fpword
) );
843 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
844 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
845 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
846 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
847 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
848 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
852 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
853 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
854 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
855 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
856 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
857 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
858 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
860 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
863 FIXME( "not implemented\n" );
868 /*********************************************************************
869 * __fpecode (MSVCRT.@)
871 int * CDECL
__fpecode(void)
873 return &msvcrt_get_thread_data()->fpecode
;
876 /*********************************************************************
879 double CDECL
MSVCRT_ldexp(double num
, MSVCRT_long exp
)
881 double z
= ldexp(num
,exp
);
884 *MSVCRT__errno() = MSVCRT_ERANGE
;
885 else if (z
== 0 && signbit(z
))
886 z
= 0.0; /* Convert -0 -> +0 */
890 /*********************************************************************
893 double CDECL
MSVCRT__cabs(struct MSVCRT__complex num
)
895 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
898 /*********************************************************************
899 * _chgsign (MSVCRT.@)
901 double CDECL
_chgsign(double num
)
903 /* FIXME: +-infinity,Nan not tested */
907 /*********************************************************************
908 * __control87_2 (MSVCRT.@)
910 * Not exported by native msvcrt, added in msvcr80.
912 #if defined(__i386__) || defined(__x86_64__)
913 int CDECL
__control87_2( unsigned int newval
, unsigned int mask
,
914 unsigned int *x86_cw
, unsigned int *sse2_cw
)
917 unsigned long fpword
;
922 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
924 /* Convert into mask constants */
926 if (fpword
& 0x1) flags
|= MSVCRT__EM_INVALID
;
927 if (fpword
& 0x2) flags
|= MSVCRT__EM_DENORMAL
;
928 if (fpword
& 0x4) flags
|= MSVCRT__EM_ZERODIVIDE
;
929 if (fpword
& 0x8) flags
|= MSVCRT__EM_OVERFLOW
;
930 if (fpword
& 0x10) flags
|= MSVCRT__EM_UNDERFLOW
;
931 if (fpword
& 0x20) flags
|= MSVCRT__EM_INEXACT
;
932 switch (fpword
& 0xc00)
934 case 0xc00: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
935 case 0x800: flags
|= MSVCRT__RC_UP
; break;
936 case 0x400: flags
|= MSVCRT__RC_DOWN
; break;
938 switch (fpword
& 0x300)
940 case 0x0: flags
|= MSVCRT__PC_24
; break;
941 case 0x200: flags
|= MSVCRT__PC_53
; break;
942 case 0x300: flags
|= MSVCRT__PC_64
; break;
944 if (fpword
& 0x1000) flags
|= MSVCRT__IC_AFFINE
;
946 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
949 flags
= (flags
& ~mask
) | (newval
& mask
);
951 /* Convert (masked) value back to fp word */
953 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x1;
954 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x2;
955 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x4;
956 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x8;
957 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x10;
958 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x20;
959 switch (flags
& MSVCRT__MCW_RC
)
961 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0xc00; break;
962 case MSVCRT__RC_UP
: fpword
|= 0x800; break;
963 case MSVCRT__RC_DOWN
: fpword
|= 0x400; break;
965 switch (flags
& MSVCRT__MCW_PC
)
967 case MSVCRT__PC_64
: fpword
|= 0x300; break;
968 case MSVCRT__PC_53
: fpword
|= 0x200; break;
969 case MSVCRT__PC_24
: fpword
|= 0x0; break;
971 if (flags
& MSVCRT__IC_AFFINE
) fpword
|= 0x1000;
973 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
978 if (!sse2_cw
) return 1;
982 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
984 /* Convert into mask constants */
986 if (fpword
& 0x80) flags
|= MSVCRT__EM_INVALID
;
987 if (fpword
& 0x100) flags
|= MSVCRT__EM_DENORMAL
;
988 if (fpword
& 0x200) flags
|= MSVCRT__EM_ZERODIVIDE
;
989 if (fpword
& 0x400) flags
|= MSVCRT__EM_OVERFLOW
;
990 if (fpword
& 0x800) flags
|= MSVCRT__EM_UNDERFLOW
;
991 if (fpword
& 0x1000) flags
|= MSVCRT__EM_INEXACT
;
992 switch (fpword
& 0x6000)
994 case 0x6000: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
995 case 0x4000: flags
|= MSVCRT__RC_UP
; break;
996 case 0x2000: flags
|= MSVCRT__RC_DOWN
; break;
998 switch (fpword
& 0x8040)
1000 case 0x0040: flags
|= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
1001 case 0x8000: flags
|= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
1002 case 0x8040: flags
|= MSVCRT__DN_FLUSH
; break;
1005 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1008 flags
= (flags
& ~mask
) | (newval
& mask
);
1010 /* Convert (masked) value back to fp word */
1012 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x80;
1013 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x100;
1014 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x200;
1015 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x400;
1016 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x800;
1017 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x1000;
1018 switch (flags
& MSVCRT__MCW_RC
)
1020 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0x6000; break;
1021 case MSVCRT__RC_UP
: fpword
|= 0x4000; break;
1022 case MSVCRT__RC_DOWN
: fpword
|= 0x2000; break;
1024 switch (flags
& MSVCRT__MCW_DN
)
1026 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
1027 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
1028 case MSVCRT__DN_FLUSH
: fpword
|= 0x8040; break;
1030 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1038 FIXME( "not implemented\n" );
1044 /*********************************************************************
1045 * _control87 (MSVCRT.@)
1047 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
1049 #if defined(__i386__) || defined(__x86_64__)
1050 unsigned int x86_cw
, sse2_cw
;
1052 __control87_2( newval
, mask
, &x86_cw
, &sse2_cw
);
1054 if ((x86_cw
^ sse2_cw
) & (MSVCRT__MCW_EM
| MSVCRT__MCW_RC
)) x86_cw
|= MSVCRT__EM_AMBIGUOUS
;
1057 FIXME( "not implemented\n" );
1062 /*********************************************************************
1063 * _controlfp (MSVCRT.@)
1065 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
1067 return _control87( newval
, mask
& ~MSVCRT__EM_DENORMAL
);
1070 /*********************************************************************
1071 * _controlfp_s (MSVCRT.@)
1073 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
1075 static const unsigned int all_flags
= (MSVCRT__MCW_EM
| MSVCRT__MCW_IC
| MSVCRT__MCW_RC
|
1076 MSVCRT__MCW_PC
| MSVCRT__MCW_DN
);
1079 if (!MSVCRT_CHECK_PMT( !(newval
& mask
& ~all_flags
) ))
1081 if (cur
) *cur
= _controlfp( 0, 0 ); /* retrieve it anyway */
1082 *MSVCRT__errno() = MSVCRT_EINVAL
;
1083 return MSVCRT_EINVAL
;
1085 val
= _controlfp( newval
, mask
);
1086 if (cur
) *cur
= val
;
1090 /*********************************************************************
1091 * _copysign (MSVCRT.@)
1093 double CDECL
_copysign(double num
, double sign
)
1095 /* FIXME: Behaviour for Nan/Inf? */
1097 return num
< 0.0 ? num
: -num
;
1098 return num
< 0.0 ? -num
: num
;
1101 /*********************************************************************
1102 * _finite (MSVCRT.@)
1104 int CDECL
_finite(double num
)
1106 return (finite(num
)?1:0); /* See comment for _isnan() */
1109 /*********************************************************************
1110 * _fpreset (MSVCRT.@)
1112 void CDECL
_fpreset(void)
1114 #if defined(__GNUC__) && defined(__i386__)
1115 __asm__
__volatile__( "fninit" );
1117 FIXME(":Not Implemented!\n");
1121 /*********************************************************************
1124 INT CDECL
_isnan(double num
)
1126 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1127 * Do the same, as the result may be used in calculations
1129 return isnan(num
) ? 1 : 0;
1132 /*********************************************************************
1135 double CDECL
_j0(double num
)
1137 /* FIXME: errno handling */
1141 /*********************************************************************
1144 double CDECL
_j1(double num
)
1146 /* FIXME: errno handling */
1150 /*********************************************************************
1153 double CDECL
_jn(int n
, double num
)
1155 /* FIXME: errno handling */
1159 /*********************************************************************
1162 double CDECL
_y0(double num
)
1165 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1167 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1169 *MSVCRT__errno() = MSVCRT_EDOM
;
1175 /*********************************************************************
1178 double CDECL
_y1(double num
)
1181 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1183 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1185 *MSVCRT__errno() = MSVCRT_EDOM
;
1191 /*********************************************************************
1194 double CDECL
_yn(int order
, double num
)
1197 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1198 retval
= yn(order
,num
);
1199 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1201 *MSVCRT__errno() = MSVCRT_EDOM
;
1207 /*********************************************************************
1208 * _nextafter (MSVCRT.@)
1210 double CDECL
_nextafter(double num
, double next
)
1213 if (!finite(num
) || !finite(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1214 retval
= nextafter(num
,next
);
1218 /*********************************************************************
1221 char * CDECL
_ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
1224 thread_data_t
*data
= msvcrt_get_thread_data();
1225 /* FIXME: check better for overflow (native supports over 300 chars's) */
1226 ndigits
= min( ndigits
, 80 - 7); /* 7 : space for dec point, 1 for "e",
1227 * 4 for exponent and one for
1228 * terminating '\0' */
1229 if (!data
->efcvt_buffer
)
1230 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1237 /* handle cases with zero ndigits or less */
1239 if( prec
< 1) prec
= 2;
1240 len
= snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
1241 /* take the decimal "point away */
1243 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
1244 /* take the exponential "e" out */
1245 data
->efcvt_buffer
[ prec
] = '\0';
1246 /* read the exponent */
1247 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
1249 /* adjust for some border cases */
1250 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
1252 /* handle cases with zero ndigits or less */
1254 if( data
->efcvt_buffer
[ 0] >= '5')
1256 data
->efcvt_buffer
[ 0] = '\0';
1258 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
1259 return data
->efcvt_buffer
;
1262 /*********************************************************************
1263 * _ecvt_s (MSVCRT.@)
1265 int CDECL
_ecvt_s( char *buffer
, MSVCRT_size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
1269 const char infret
[] = "1#INF";
1271 if(!MSVCRT_CHECK_PMT(buffer
!= NULL
) || !MSVCRT_CHECK_PMT(decpt
!= NULL
) || !MSVCRT_CHECK_PMT(sign
!= NULL
))
1273 *MSVCRT__errno() = MSVCRT_EINVAL
;
1274 return MSVCRT_EINVAL
;
1276 if(!MSVCRT_CHECK_PMT(length
> 2) || !MSVCRT_CHECK_PMT(ndigits
< (int)length
- 1))
1278 *MSVCRT__errno() = MSVCRT_ERANGE
;
1279 return MSVCRT_ERANGE
;
1282 /* special case - inf */
1283 if(number
== HUGE_VAL
|| number
== -HUGE_VAL
)
1285 memset(buffer
, '0', ndigits
);
1286 memcpy(buffer
, infret
, min(ndigits
, sizeof(infret
) - 1 ) );
1287 buffer
[ndigits
] = '\0';
1289 if(number
== -HUGE_VAL
)
1295 result
= (char*)MSVCRT_malloc(max(ndigits
+ 7, 7));
1302 /* handle cases with zero ndigits or less */
1304 if( prec
< 1) prec
= 2;
1305 len
= snprintf(result
, 80, "%.*le", prec
- 1, number
);
1306 /* take the decimal "point away */
1308 memmove( result
+ 1, result
+ 2, len
- 1 );
1309 /* take the exponential "e" out */
1310 result
[ prec
] = '\0';
1311 /* read the exponent */
1312 sscanf( result
+ prec
+ 1, "%d", decpt
);
1314 /* adjust for some border cases */
1315 if( result
[0] == '0')/* value is zero */
1317 /* handle cases with zero ndigits or less */
1319 if( result
[ 0] >= '5')
1323 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
1324 MSVCRT_free( result
);
1328 /***********************************************************************
1331 char * CDECL
_fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
1333 thread_data_t
*data
= msvcrt_get_thread_data();
1334 int stop
, dec1
, dec2
;
1335 char *ptr1
, *ptr2
, *first
;
1336 char buf
[80]; /* ought to be enough */
1338 if (!data
->efcvt_buffer
)
1339 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1347 snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1349 ptr2
= data
->efcvt_buffer
;
1354 /* For numbers below the requested resolution, work out where
1355 the decimal point will be rather than finding it in the string */
1356 if (number
< 1.0 && number
> 0.0) {
1357 dec2
= log10(number
+ 1e-10);
1358 if (-dec2
<= ndigits
) dec2
= 0;
1361 /* If requested digits is zero or less, we will need to truncate
1362 * the returned string */
1364 stop
= strlen(buf
) + ndigits
;
1369 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1370 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1371 if (!first
) first
= ptr2
;
1372 if ((ptr1
- buf
) < stop
) {
1383 while (*ptr1
== '0') { /* Process leading zeroes */
1388 while (*ptr1
!= '\0') {
1389 if (!first
) first
= ptr2
;
1396 /* We never found a non-zero digit, then our number is either
1397 * smaller than the requested precision, or 0.0 */
1402 first
= data
->efcvt_buffer
;
1407 *decpt
= dec2
? dec2
: dec1
;
1411 /***********************************************************************
1414 char * CDECL
_gcvt( double number
, int ndigit
, char *buff
)
1417 *MSVCRT__errno() = MSVCRT_EINVAL
;
1422 *MSVCRT__errno() = MSVCRT_ERANGE
;
1426 MSVCRT_sprintf(buff
, "%.*g", ndigit
, number
);
1430 /***********************************************************************
1431 * _gcvt_s (MSVCRT.@)
1433 int CDECL
_gcvt_s(char *buff
, MSVCRT_size_t size
, double number
, int digits
)
1438 *MSVCRT__errno() = MSVCRT_EINVAL
;
1439 return MSVCRT_EINVAL
;
1442 if( digits
<0 || digits
>=size
) {
1446 *MSVCRT__errno() = MSVCRT_ERANGE
;
1447 return MSVCRT_ERANGE
;
1450 len
= MSVCRT__scprintf("%.*g", digits
, number
);
1453 *MSVCRT__errno() = MSVCRT_ERANGE
;
1454 return MSVCRT_ERANGE
;
1457 MSVCRT_sprintf(buff
, "%.*g", digits
, number
);
1461 #include <stdlib.h> /* div_t, ldiv_t */
1463 /*********************************************************************
1466 * [i386] Windows binary compatible - returns the struct in eax/edx.
1469 unsigned __int64 CDECL
MSVCRT_div(int num
, int denom
)
1471 div_t dt
= div(num
,denom
);
1472 return ((unsigned __int64
)dt
.rem
<< 32) | (unsigned int)dt
.quot
;
1475 /*********************************************************************
1478 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1480 MSVCRT_div_t CDECL
MSVCRT_div(int num
, int denom
)
1482 div_t dt
= div(num
,denom
);
1490 #endif /* ifdef __i386__ */
1493 /*********************************************************************
1496 * [i386] Windows binary compatible - returns the struct in eax/edx.
1499 unsigned __int64 CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1501 ldiv_t ldt
= ldiv(num
,denom
);
1502 return ((unsigned __int64
)ldt
.rem
<< 32) | (MSVCRT_ulong
)ldt
.quot
;
1505 /*********************************************************************
1508 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1510 MSVCRT_ldiv_t CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1512 ldiv_t result
= ldiv(num
,denom
);
1515 ret
.quot
= result
.quot
;
1516 ret
.rem
= result
.rem
;
1520 #endif /* ifdef __i386__ */
1524 /*********************************************************************
1525 * _adjust_fdiv (MSVCRT.@)
1526 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1528 int MSVCRT__adjust_fdiv
= 0;
1530 /***********************************************************************
1531 * _adj_fdiv_m16i (MSVCRT.@)
1534 * I _think_ this function is intended to work around the Pentium
1537 void __stdcall
_adj_fdiv_m16i( short arg
)
1539 TRACE("(): stub\n");
1542 /***********************************************************************
1543 * _adj_fdiv_m32 (MSVCRT.@)
1546 * I _think_ this function is intended to work around the Pentium
1549 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
1551 TRACE("(): stub\n");
1554 /***********************************************************************
1555 * _adj_fdiv_m32i (MSVCRT.@)
1558 * I _think_ this function is intended to work around the Pentium
1561 void __stdcall
_adj_fdiv_m32i( int arg
)
1563 TRACE("(): stub\n");
1566 /***********************************************************************
1567 * _adj_fdiv_m64 (MSVCRT.@)
1570 * I _think_ this function is intended to work around the Pentium
1573 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
1575 TRACE("(): stub\n");
1578 /***********************************************************************
1579 * _adj_fdiv_r (MSVCRT.@)
1581 * This function is likely to have the wrong number of arguments.
1584 * I _think_ this function is intended to work around the Pentium
1587 void _adj_fdiv_r(void)
1589 TRACE("(): stub\n");
1592 /***********************************************************************
1593 * _adj_fdivr_m16i (MSVCRT.@)
1596 * I _think_ this function is intended to work around the Pentium
1599 void __stdcall
_adj_fdivr_m16i( short arg
)
1601 TRACE("(): stub\n");
1604 /***********************************************************************
1605 * _adj_fdivr_m32 (MSVCRT.@)
1608 * I _think_ this function is intended to work around the Pentium
1611 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
1613 TRACE("(): stub\n");
1616 /***********************************************************************
1617 * _adj_fdivr_m32i (MSVCRT.@)
1620 * I _think_ this function is intended to work around the Pentium
1623 void __stdcall
_adj_fdivr_m32i( int arg
)
1625 TRACE("(): stub\n");
1628 /***********************************************************************
1629 * _adj_fdivr_m64 (MSVCRT.@)
1632 * I _think_ this function is intended to work around the Pentium
1635 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
1637 TRACE("(): stub\n");
1640 /***********************************************************************
1641 * _adj_fpatan (MSVCRT.@)
1643 * This function is likely to have the wrong number of arguments.
1646 * I _think_ this function is intended to work around the Pentium
1649 void _adj_fpatan(void)
1651 TRACE("(): stub\n");
1654 /***********************************************************************
1655 * _adj_fprem (MSVCRT.@)
1657 * This function is likely to have the wrong number of arguments.
1660 * I _think_ this function is intended to work around the Pentium
1663 void _adj_fprem(void)
1665 TRACE("(): stub\n");
1668 /***********************************************************************
1669 * _adj_fprem1 (MSVCRT.@)
1671 * This function is likely to have the wrong number of arguments.
1674 * I _think_ this function is intended to work around the Pentium
1677 void _adj_fprem1(void)
1679 TRACE("(): stub\n");
1682 /***********************************************************************
1683 * _adj_fptan (MSVCRT.@)
1685 * This function is likely to have the wrong number of arguments.
1688 * I _think_ this function is intended to work around the Pentium
1691 void _adj_fptan(void)
1693 TRACE("(): stub\n");
1696 /***********************************************************************
1697 * _safe_fdiv (MSVCRT.@)
1699 * This function is likely to have the wrong number of arguments.
1702 * I _think_ this function is intended to work around the Pentium
1705 void _safe_fdiv(void)
1707 TRACE("(): stub\n");
1710 /***********************************************************************
1711 * _safe_fdivr (MSVCRT.@)
1713 * This function is likely to have the wrong number of arguments.
1716 * I _think_ this function is intended to work around the Pentium
1719 void _safe_fdivr(void)
1721 TRACE("(): stub\n");
1724 /***********************************************************************
1725 * _safe_fprem (MSVCRT.@)
1727 * This function is likely to have the wrong number of arguments.
1730 * I _think_ this function is intended to work around the Pentium
1733 void _safe_fprem(void)
1735 TRACE("(): stub\n");
1738 /***********************************************************************
1739 * _safe_fprem1 (MSVCRT.@)
1742 * This function is likely to have the wrong number of arguments.
1745 * I _think_ this function is intended to work around the Pentium
1748 void _safe_fprem1(void)
1750 TRACE("(): stub\n");
1753 #endif /* __i386__ */