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 /*********************************************************************
55 * _set_SSE2_enable (MSVCRT.@)
57 int CDECL
MSVCRT__set_SSE2_enable(int flag
)
59 FIXME("(%x) stub\n", flag
);
65 /*********************************************************************
66 * MSVCRT_acosf (MSVCRT.@)
68 float CDECL
MSVCRT_acosf( float x
)
70 if (x
< -1.0 || x
> 1.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
71 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
72 * asin() uses a similar construction. This is bad because as x gets nearer to
73 * 1 the error in the expression "1 - x^2" can get relatively large due to
74 * cancellation. The sqrt() makes things worse. A safer way to calculate
75 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
76 return atan2f(sqrtf((1 - x
) * (1 + x
)), x
);
79 /*********************************************************************
80 * MSVCRT_asinf (MSVCRT.@)
82 float CDECL
MSVCRT_asinf( float x
)
84 if (x
< -1.0 || x
> 1.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
85 return atan2f(x
, sqrtf((1 - x
) * (1 + x
)));
88 /*********************************************************************
89 * MSVCRT_atanf (MSVCRT.@)
91 float CDECL
MSVCRT_atanf( float x
)
93 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
97 /*********************************************************************
98 * MSVCRT_atan2f (MSVCRT.@)
100 float CDECL
MSVCRT_atan2f( float x
, float y
)
102 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
106 /*********************************************************************
107 * MSVCRT_cosf (MSVCRT.@)
109 float CDECL
MSVCRT_cosf( float x
)
111 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
115 /*********************************************************************
116 * MSVCRT_coshf (MSVCRT.@)
118 float CDECL
MSVCRT_coshf( float x
)
120 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
124 /*********************************************************************
125 * MSVCRT_expf (MSVCRT.@)
127 float CDECL
MSVCRT_expf( float x
)
129 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
133 /*********************************************************************
134 * MSVCRT_fmodf (MSVCRT.@)
136 float CDECL
MSVCRT_fmodf( float x
, float y
)
138 if (!finitef(x
) || !finitef(y
)) *MSVCRT__errno() = MSVCRT_EDOM
;
142 /*********************************************************************
143 * MSVCRT_logf (MSVCRT.@)
145 float CDECL
MSVCRT_logf( float x
)
147 if (x
< 0.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
148 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
152 /*********************************************************************
153 * MSVCRT_log10f (MSVCRT.@)
155 float CDECL
MSVCRT_log10f( float x
)
157 if (x
< 0.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
158 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
162 /*********************************************************************
163 * MSVCRT_powf (MSVCRT.@)
165 float CDECL
MSVCRT_powf( float x
, float y
)
167 /* FIXME: If x < 0 and y is not integral, set EDOM */
169 if (!finitef(z
)) *MSVCRT__errno() = MSVCRT_EDOM
;
173 /*********************************************************************
174 * MSVCRT_sinf (MSVCRT.@)
176 float CDECL
MSVCRT_sinf( float x
)
178 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
182 /*********************************************************************
183 * MSVCRT_sinhf (MSVCRT.@)
185 float CDECL
MSVCRT_sinhf( float x
)
187 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
191 /*********************************************************************
192 * MSVCRT_sqrtf (MSVCRT.@)
194 float CDECL
MSVCRT_sqrtf( float x
)
196 if (x
< 0.0 || !finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
200 /*********************************************************************
201 * MSVCRT_tanf (MSVCRT.@)
203 float CDECL
MSVCRT_tanf( float x
)
205 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
209 /*********************************************************************
210 * MSVCRT_tanhf (MSVCRT.@)
212 float CDECL
MSVCRT_tanhf( float x
)
214 if (!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
218 /*********************************************************************
221 float CDECL
MSVCRT_ceilf( float x
)
226 /*********************************************************************
229 float CDECL
MSVCRT_floorf( float x
)
234 /*********************************************************************
237 float CDECL
MSVCRT_frexpf( float x
, int *exp
)
239 return frexpf( x
, exp
);
242 /*********************************************************************
245 float CDECL
MSVCRT__scalbf(float num
, MSVCRT_long power
)
247 if (!finitef(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
248 return ldexpf(num
, power
);
251 /*********************************************************************
254 double CDECL
MSVCRT_modff( float x
, float *iptr
)
256 return modff( x
, iptr
);
261 /*********************************************************************
262 * MSVCRT_acos (MSVCRT.@)
264 double CDECL
MSVCRT_acos( double x
)
266 if (x
< -1.0 || x
> 1.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
267 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
268 * asin() uses a similar construction. This is bad because as x gets nearer to
269 * 1 the error in the expression "1 - x^2" can get relatively large due to
270 * cancellation. The sqrt() makes things worse. A safer way to calculate
271 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
272 return atan2(sqrt((1 - x
) * (1 + x
)), x
);
275 /*********************************************************************
276 * MSVCRT_asin (MSVCRT.@)
278 double CDECL
MSVCRT_asin( double x
)
280 if (x
< -1.0 || x
> 1.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
281 return atan2(x
, sqrt((1 - x
) * (1 + x
)));
284 /*********************************************************************
285 * MSVCRT_atan (MSVCRT.@)
287 double CDECL
MSVCRT_atan( double x
)
289 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
293 /*********************************************************************
294 * MSVCRT_atan2 (MSVCRT.@)
296 double CDECL
MSVCRT_atan2( double x
, double y
)
298 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
302 /*********************************************************************
303 * MSVCRT_cos (MSVCRT.@)
305 double CDECL
MSVCRT_cos( double x
)
307 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
311 /*********************************************************************
312 * MSVCRT_cosh (MSVCRT.@)
314 double CDECL
MSVCRT_cosh( double x
)
316 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
320 /*********************************************************************
321 * MSVCRT_exp (MSVCRT.@)
323 double CDECL
MSVCRT_exp( double x
)
325 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
329 /*********************************************************************
330 * MSVCRT_fmod (MSVCRT.@)
332 double CDECL
MSVCRT_fmod( double x
, double y
)
334 if (!finite(x
) || !finite(y
)) *MSVCRT__errno() = MSVCRT_EDOM
;
338 /*********************************************************************
339 * MSVCRT_log (MSVCRT.@)
341 double CDECL
MSVCRT_log( double x
)
343 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
344 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
348 /*********************************************************************
349 * MSVCRT_log10 (MSVCRT.@)
351 double CDECL
MSVCRT_log10( double x
)
353 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
354 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
358 /*********************************************************************
359 * MSVCRT_pow (MSVCRT.@)
361 double CDECL
MSVCRT_pow( double x
, double y
)
363 /* FIXME: If x < 0 and y is not integral, set EDOM */
365 if (!finite(z
)) *MSVCRT__errno() = MSVCRT_EDOM
;
369 /*********************************************************************
370 * MSVCRT_sin (MSVCRT.@)
372 double CDECL
MSVCRT_sin( double x
)
374 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
378 /*********************************************************************
379 * MSVCRT_sinh (MSVCRT.@)
381 double CDECL
MSVCRT_sinh( double x
)
383 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
387 /*********************************************************************
388 * MSVCRT_sqrt (MSVCRT.@)
390 double CDECL
MSVCRT_sqrt( double x
)
392 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
396 /*********************************************************************
397 * MSVCRT_tan (MSVCRT.@)
399 double CDECL
MSVCRT_tan( double x
)
401 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
405 /*********************************************************************
406 * MSVCRT_tanh (MSVCRT.@)
408 double CDECL
MSVCRT_tanh( double x
)
410 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
415 #if defined(__GNUC__) && defined(__i386__)
417 #define FPU_DOUBLE(var) double var; \
418 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
419 #define FPU_DOUBLES(var1,var2) double var1,var2; \
420 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
421 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
423 /*********************************************************************
426 double CDECL
_CIacos(void)
429 return MSVCRT_acos(x
);
432 /*********************************************************************
435 double CDECL
_CIasin(void)
438 return MSVCRT_asin(x
);
441 /*********************************************************************
444 double CDECL
_CIatan(void)
447 return MSVCRT_atan(x
);
450 /*********************************************************************
451 * _CIatan2 (MSVCRT.@)
453 double CDECL
_CIatan2(void)
456 return MSVCRT_atan2(x
,y
);
459 /*********************************************************************
462 double CDECL
_CIcos(void)
465 return MSVCRT_cos(x
);
468 /*********************************************************************
471 double CDECL
_CIcosh(void)
474 return MSVCRT_cosh(x
);
477 /*********************************************************************
480 double CDECL
_CIexp(void)
483 return MSVCRT_exp(x
);
486 /*********************************************************************
489 double CDECL
_CIfmod(void)
492 return MSVCRT_fmod(x
,y
);
495 /*********************************************************************
498 double CDECL
_CIlog(void)
501 return MSVCRT_log(x
);
504 /*********************************************************************
505 * _CIlog10 (MSVCRT.@)
507 double CDECL
_CIlog10(void)
510 return MSVCRT_log10(x
);
513 /*********************************************************************
516 double CDECL
_CIpow(void)
519 return MSVCRT_pow(x
,y
);
522 /*********************************************************************
525 double CDECL
_CIsin(void)
528 return MSVCRT_sin(x
);
531 /*********************************************************************
534 double CDECL
_CIsinh(void)
537 return MSVCRT_sinh(x
);
540 /*********************************************************************
543 double CDECL
_CIsqrt(void)
546 return MSVCRT_sqrt(x
);
549 /*********************************************************************
552 double CDECL
_CItan(void)
555 return MSVCRT_tan(x
);
558 /*********************************************************************
561 double CDECL
_CItanh(void)
564 return MSVCRT_tanh(x
);
567 #endif /* defined(__GNUC__) && defined(__i386__) */
569 /*********************************************************************
570 * _fpclass (MSVCRT.@)
572 int CDECL
_fpclass(double num
)
574 #if defined(HAVE_FPCLASS) || defined(fpclass)
575 switch (fpclass( num
))
578 case FP_SNAN
: return MSVCRT__FPCLASS_SNAN
;
581 case FP_QNAN
: return MSVCRT__FPCLASS_QNAN
;
584 case FP_NINF
: return MSVCRT__FPCLASS_NINF
;
587 case FP_PINF
: return MSVCRT__FPCLASS_PINF
;
590 case FP_NDENORM
: return MSVCRT__FPCLASS_ND
;
593 case FP_PDENORM
: return MSVCRT__FPCLASS_PD
;
596 case FP_NZERO
: return MSVCRT__FPCLASS_NZ
;
599 case FP_PZERO
: return MSVCRT__FPCLASS_PZ
;
602 case FP_NNORM
: return MSVCRT__FPCLASS_NN
;
605 case FP_PNORM
: return MSVCRT__FPCLASS_PN
;
607 default: return MSVCRT__FPCLASS_PN
;
609 #elif defined (fpclassify)
610 switch (fpclassify( num
))
612 case FP_NAN
: return MSVCRT__FPCLASS_QNAN
;
613 case FP_INFINITE
: return signbit(num
) ? MSVCRT__FPCLASS_NINF
: MSVCRT__FPCLASS_PINF
;
614 case FP_SUBNORMAL
: return signbit(num
) ?MSVCRT__FPCLASS_ND
: MSVCRT__FPCLASS_PD
;
615 case FP_ZERO
: return signbit(num
) ? MSVCRT__FPCLASS_NZ
: MSVCRT__FPCLASS_PZ
;
617 return signbit(num
) ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
;
620 return MSVCRT__FPCLASS_QNAN
;
621 return num
== 0.0 ? MSVCRT__FPCLASS_PZ
: (num
< 0 ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
);
625 /*********************************************************************
628 unsigned int CDECL
_rotl(unsigned int num
, int shift
)
631 return (num
<< shift
) | (num
>> (32-shift
));
634 /*********************************************************************
637 double CDECL
_logb(double num
)
639 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
643 /*********************************************************************
646 MSVCRT_ulong CDECL
MSVCRT__lrotl(MSVCRT_ulong num
, int shift
)
649 return (num
<< shift
) | (num
>> (32-shift
));
652 /*********************************************************************
655 MSVCRT_ulong CDECL
MSVCRT__lrotr(MSVCRT_ulong num
, int shift
)
658 return (num
>> shift
) | (num
<< (32-shift
));
661 /*********************************************************************
664 unsigned int CDECL
_rotr(unsigned int num
, int shift
)
667 return (num
>> shift
) | (num
<< (32-shift
));
670 /*********************************************************************
673 double CDECL
MSVCRT__scalb(double num
, MSVCRT_long power
)
675 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
676 return ldexp(num
, power
);
679 /*********************************************************************
682 double CDECL
_hypot(double x
, double y
)
684 /* FIXME: errno handling */
685 return hypot( x
, y
);
688 /*********************************************************************
691 float CDECL
_hypotf(float x
, float y
)
693 /* FIXME: errno handling */
694 return hypotf( x
, y
);
697 /*********************************************************************
700 double CDECL
MSVCRT_ceil( double x
)
705 /*********************************************************************
708 double CDECL
MSVCRT_floor( double x
)
713 /*********************************************************************
716 double CDECL
MSVCRT_fabs( double x
)
721 /*********************************************************************
724 double CDECL
MSVCRT_frexp( double x
, int *exp
)
726 return frexp( x
, exp
);
729 /*********************************************************************
732 double CDECL
MSVCRT_modf( double x
, double *iptr
)
734 return modf( x
, iptr
);
737 /*********************************************************************
738 * _matherr (MSVCRT.@)
740 int CDECL
MSVCRT__matherr(struct MSVCRT__exception
*e
)
743 TRACE("(%p = %d, %s, %g %g %g)\n",e
, e
->type
, e
->name
, e
->arg1
, e
->arg2
,
747 if (MSVCRT_default_matherr_func
)
748 return MSVCRT_default_matherr_func(e
);
749 ERR(":Unhandled math error!\n");
753 /*********************************************************************
754 * __setusermatherr (MSVCRT.@)
756 void CDECL
MSVCRT___setusermatherr(MSVCRT_matherr_func func
)
758 MSVCRT_default_matherr_func
= func
;
759 TRACE(":new matherr handler %p\n", func
);
762 /**********************************************************************
763 * _statusfp (MSVCRT.@)
765 unsigned int CDECL
_statusfp(void)
767 unsigned int retVal
= 0;
768 #if defined(__GNUC__) && defined(__i386__)
771 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) : );
772 if (fpword
& 0x1) retVal
|= MSVCRT__SW_INVALID
;
773 if (fpword
& 0x2) retVal
|= MSVCRT__SW_DENORMAL
;
774 if (fpword
& 0x4) retVal
|= MSVCRT__SW_ZERODIVIDE
;
775 if (fpword
& 0x8) retVal
|= MSVCRT__SW_OVERFLOW
;
776 if (fpword
& 0x10) retVal
|= MSVCRT__SW_UNDERFLOW
;
777 if (fpword
& 0x20) retVal
|= MSVCRT__SW_INEXACT
;
779 FIXME(":Not implemented!\n");
784 /*********************************************************************
785 * _clearfp (MSVCRT.@)
787 unsigned int CDECL
_clearfp(void)
789 unsigned int retVal
= _statusfp();
790 #if defined(__GNUC__) && defined(__i386__)
791 __asm__
__volatile__( "fnclex" );
793 FIXME(":Not Implemented\n");
798 /*********************************************************************
799 * __fpecode (MSVCRT.@)
801 int * CDECL
__fpecode(void)
803 return &msvcrt_get_thread_data()->fpecode
;
806 /*********************************************************************
809 double CDECL
MSVCRT_ldexp(double num
, MSVCRT_long exp
)
811 double z
= ldexp(num
,exp
);
814 *MSVCRT__errno() = MSVCRT_ERANGE
;
815 else if (z
== 0 && signbit(z
))
816 z
= 0.0; /* Convert -0 -> +0 */
820 /*********************************************************************
823 double CDECL
MSVCRT__cabs(struct MSVCRT__complex num
)
825 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
828 /*********************************************************************
829 * _chgsign (MSVCRT.@)
831 double CDECL
_chgsign(double num
)
833 /* FIXME: +-infinity,Nan not tested */
837 /*********************************************************************
838 * _control87 (MSVCRT.@)
840 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
842 #if defined(__GNUC__) && defined(__i386__)
843 unsigned int fpword
= 0;
844 unsigned int flags
= 0;
846 TRACE("(%08x, %08x): Called\n", newval
, mask
);
848 /* Get fp control word */
849 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) : );
851 TRACE("Control word before : %08x\n", fpword
);
853 /* Convert into mask constants */
854 if (fpword
& 0x1) flags
|= MSVCRT__EM_INVALID
;
855 if (fpword
& 0x2) flags
|= MSVCRT__EM_DENORMAL
;
856 if (fpword
& 0x4) flags
|= MSVCRT__EM_ZERODIVIDE
;
857 if (fpword
& 0x8) flags
|= MSVCRT__EM_OVERFLOW
;
858 if (fpword
& 0x10) flags
|= MSVCRT__EM_UNDERFLOW
;
859 if (fpword
& 0x20) flags
|= MSVCRT__EM_INEXACT
;
860 switch(fpword
& 0xC00) {
861 case 0xC00: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
862 case 0x800: flags
|= MSVCRT__RC_UP
; break;
863 case 0x400: flags
|= MSVCRT__RC_DOWN
; break;
865 switch(fpword
& 0x300) {
866 case 0x0: flags
|= MSVCRT__PC_24
; break;
867 case 0x200: flags
|= MSVCRT__PC_53
; break;
868 case 0x300: flags
|= MSVCRT__PC_64
; break;
870 if (fpword
& 0x1000) flags
|= MSVCRT__IC_AFFINE
;
872 /* Mask with parameters */
873 flags
= (flags
& ~mask
) | (newval
& mask
);
875 /* Convert (masked) value back to fp word */
877 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x1;
878 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x2;
879 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x4;
880 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x8;
881 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x10;
882 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x20;
883 switch(flags
& (MSVCRT__RC_UP
| MSVCRT__RC_DOWN
)) {
884 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0xC00; break;
885 case MSVCRT__RC_UP
: fpword
|= 0x800; break;
886 case MSVCRT__RC_DOWN
: fpword
|= 0x400; break;
888 switch (flags
& (MSVCRT__PC_24
| MSVCRT__PC_53
)) {
889 case MSVCRT__PC_64
: fpword
|= 0x300; break;
890 case MSVCRT__PC_53
: fpword
|= 0x200; break;
891 case MSVCRT__PC_24
: fpword
|= 0x0; break;
893 if (flags
& MSVCRT__IC_AFFINE
) fpword
|= 0x1000;
895 TRACE("Control word after : %08x\n", fpword
);
897 /* Put fp control word */
898 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
902 FIXME(":Not Implemented!\n");
907 /*********************************************************************
908 * _controlfp (MSVCRT.@)
910 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
913 return _control87( newval
, mask
& ~MSVCRT__EM_DENORMAL
);
915 FIXME(":Not Implemented!\n");
920 /*********************************************************************
921 * _controlfp_s (MSVCRT.@)
923 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
928 FIXME("(%p %u %u) semi-stub\n", cur
, newval
, mask
);
930 flags
= _control87( newval
, mask
& ~MSVCRT__EM_DENORMAL
);
937 FIXME(":Not Implemented!\n");
942 /*********************************************************************
943 * _copysign (MSVCRT.@)
945 double CDECL
_copysign(double num
, double sign
)
947 /* FIXME: Behaviour for Nan/Inf? */
949 return num
< 0.0 ? num
: -num
;
950 return num
< 0.0 ? -num
: num
;
953 /*********************************************************************
956 int CDECL
_finite(double num
)
958 return (finite(num
)?1:0); /* See comment for _isnan() */
961 /*********************************************************************
962 * _fpreset (MSVCRT.@)
964 void CDECL
_fpreset(void)
966 #if defined(__GNUC__) && defined(__i386__)
967 __asm__
__volatile__( "fninit" );
969 FIXME(":Not Implemented!\n");
973 /*********************************************************************
976 INT CDECL
_isnan(double num
)
978 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
979 * Do the same, as the result may be used in calculations
981 return isnan(num
) ? 1 : 0;
984 /*********************************************************************
987 double CDECL
_j0(double num
)
989 /* FIXME: errno handling */
993 /*********************************************************************
996 double CDECL
_j1(double num
)
998 /* FIXME: errno handling */
1002 /*********************************************************************
1005 double CDECL
_jn(int n
, double num
)
1007 /* FIXME: errno handling */
1011 /*********************************************************************
1014 double CDECL
_y0(double num
)
1017 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1019 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1021 *MSVCRT__errno() = MSVCRT_EDOM
;
1027 /*********************************************************************
1030 double CDECL
_y1(double num
)
1033 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1035 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1037 *MSVCRT__errno() = MSVCRT_EDOM
;
1043 /*********************************************************************
1046 double CDECL
_yn(int order
, double num
)
1049 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1050 retval
= yn(order
,num
);
1051 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1053 *MSVCRT__errno() = MSVCRT_EDOM
;
1059 /*********************************************************************
1060 * _nextafter (MSVCRT.@)
1062 double CDECL
_nextafter(double num
, double next
)
1065 if (!finite(num
) || !finite(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1066 retval
= nextafter(num
,next
);
1070 /*********************************************************************
1073 char * CDECL
_ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
1076 thread_data_t
*data
= msvcrt_get_thread_data();
1077 /* FIXME: check better for overflow (native supports over 300 chars's) */
1078 ndigits
= min( ndigits
, 80 - 7); /* 7 : space for dec point, 1 for "e",
1079 * 4 for exponent and one for
1080 * terminating '\0' */
1081 if (!data
->efcvt_buffer
)
1082 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1089 /* handle cases with zero ndigits or less */
1091 if( prec
< 1) prec
= 2;
1092 len
= snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
1093 /* take the decimal "point away */
1095 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
1096 /* take the exponential "e" out */
1097 data
->efcvt_buffer
[ prec
] = '\0';
1098 /* read the exponent */
1099 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
1101 /* adjust for some border cases */
1102 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
1104 /* handle cases with zero ndigits or less */
1106 if( data
->efcvt_buffer
[ 0] >= '5')
1108 data
->efcvt_buffer
[ 0] = '\0';
1110 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
1111 return data
->efcvt_buffer
;
1114 /***********************************************************************
1117 char * CDECL
_fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
1119 thread_data_t
*data
= msvcrt_get_thread_data();
1120 int stop
, dec1
, dec2
;
1121 char *ptr1
, *ptr2
, *first
;
1122 char buf
[80]; /* ought to be enough */
1124 if (!data
->efcvt_buffer
)
1125 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1133 snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1135 ptr2
= data
->efcvt_buffer
;
1140 /* For numbers below the requested resolution, work out where
1141 the decimal point will be rather than finding it in the string */
1142 if (number
< 1.0 && number
> 0.0) {
1143 dec2
= log10(number
+ 1e-10);
1144 if (-dec2
<= ndigits
) dec2
= 0;
1147 /* If requested digits is zero or less, we will need to truncate
1148 * the returned string */
1150 stop
= strlen(buf
) + ndigits
;
1155 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1156 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1157 if (!first
) first
= ptr2
;
1158 if ((ptr1
- buf
) < stop
) {
1169 while (*ptr1
== '0') { /* Process leading zeroes */
1174 while (*ptr1
!= '\0') {
1175 if (!first
) first
= ptr2
;
1182 /* We never found a non-zero digit, then our number is either
1183 * smaller than the requested precision, or 0.0 */
1188 first
= data
->efcvt_buffer
;
1193 *decpt
= dec2
? dec2
: dec1
;
1197 /***********************************************************************
1200 char * CDECL
_gcvt( double number
, int ndigit
, char *buff
)
1203 *MSVCRT__errno() = MSVCRT_EINVAL
;
1208 *MSVCRT__errno() = MSVCRT_ERANGE
;
1212 MSVCRT_sprintf(buff
, "%.*g", ndigit
, number
);
1216 /***********************************************************************
1217 * _gcvt_s (MSVCRT.@)
1219 int CDECL
_gcvt_s(char *buff
, MSVCRT_size_t size
, double number
, int digits
)
1224 *MSVCRT__errno() = MSVCRT_EINVAL
;
1225 return MSVCRT_EINVAL
;
1228 if( digits
<0 || digits
>=size
) {
1232 *MSVCRT__errno() = MSVCRT_ERANGE
;
1233 return MSVCRT_ERANGE
;
1236 len
= MSVCRT__scprintf("%.*g", digits
, number
);
1239 *MSVCRT__errno() = MSVCRT_ERANGE
;
1240 return MSVCRT_ERANGE
;
1243 MSVCRT_sprintf(buff
, "%.*g", digits
, number
);
1247 #include <stdlib.h> /* div_t, ldiv_t */
1249 /*********************************************************************
1252 * [i386] Windows binary compatible - returns the struct in eax/edx.
1255 unsigned __int64 CDECL
MSVCRT_div(int num
, int denom
)
1257 div_t dt
= div(num
,denom
);
1258 return ((unsigned __int64
)dt
.rem
<< 32) | (unsigned int)dt
.quot
;
1261 /*********************************************************************
1264 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1266 MSVCRT_div_t CDECL
MSVCRT_div(int num
, int denom
)
1268 div_t dt
= div(num
,denom
);
1276 #endif /* ifdef __i386__ */
1279 /*********************************************************************
1282 * [i386] Windows binary compatible - returns the struct in eax/edx.
1285 unsigned __int64 CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1287 ldiv_t ldt
= ldiv(num
,denom
);
1288 return ((unsigned __int64
)ldt
.rem
<< 32) | (MSVCRT_ulong
)ldt
.quot
;
1291 /*********************************************************************
1294 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1296 MSVCRT_ldiv_t CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1298 ldiv_t result
= ldiv(num
,denom
);
1301 ret
.quot
= result
.quot
;
1302 ret
.rem
= result
.rem
;
1306 #endif /* ifdef __i386__ */
1310 /*********************************************************************
1311 * _adjust_fdiv (MSVCRT.@)
1312 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1314 int MSVCRT__adjust_fdiv
= 0;
1316 /***********************************************************************
1317 * _adj_fdiv_m16i (MSVCRT.@)
1320 * I _think_ this function is intended to work around the Pentium
1323 void __stdcall
_adj_fdiv_m16i( short arg
)
1325 TRACE("(): stub\n");
1328 /***********************************************************************
1329 * _adj_fdiv_m32 (MSVCRT.@)
1332 * I _think_ this function is intended to work around the Pentium
1335 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
1337 TRACE("(): stub\n");
1340 /***********************************************************************
1341 * _adj_fdiv_m32i (MSVCRT.@)
1344 * I _think_ this function is intended to work around the Pentium
1347 void __stdcall
_adj_fdiv_m32i( int arg
)
1349 TRACE("(): stub\n");
1352 /***********************************************************************
1353 * _adj_fdiv_m64 (MSVCRT.@)
1356 * I _think_ this function is intended to work around the Pentium
1359 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
1361 TRACE("(): stub\n");
1364 /***********************************************************************
1365 * _adj_fdiv_r (MSVCRT.@)
1367 * This function is likely to have the wrong number of arguments.
1370 * I _think_ this function is intended to work around the Pentium
1373 void _adj_fdiv_r(void)
1375 TRACE("(): stub\n");
1378 /***********************************************************************
1379 * _adj_fdivr_m16i (MSVCRT.@)
1382 * I _think_ this function is intended to work around the Pentium
1385 void __stdcall
_adj_fdivr_m16i( short arg
)
1387 TRACE("(): stub\n");
1390 /***********************************************************************
1391 * _adj_fdivr_m32 (MSVCRT.@)
1394 * I _think_ this function is intended to work around the Pentium
1397 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
1399 TRACE("(): stub\n");
1402 /***********************************************************************
1403 * _adj_fdivr_m32i (MSVCRT.@)
1406 * I _think_ this function is intended to work around the Pentium
1409 void __stdcall
_adj_fdivr_m32i( int arg
)
1411 TRACE("(): stub\n");
1414 /***********************************************************************
1415 * _adj_fdivr_m64 (MSVCRT.@)
1418 * I _think_ this function is intended to work around the Pentium
1421 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
1423 TRACE("(): stub\n");
1426 /***********************************************************************
1427 * _adj_fpatan (MSVCRT.@)
1429 * This function is likely to have the wrong number of arguments.
1432 * I _think_ this function is intended to work around the Pentium
1435 void _adj_fpatan(void)
1437 TRACE("(): stub\n");
1440 /***********************************************************************
1441 * _adj_fprem (MSVCRT.@)
1443 * This function is likely to have the wrong number of arguments.
1446 * I _think_ this function is intended to work around the Pentium
1449 void _adj_fprem(void)
1451 TRACE("(): stub\n");
1454 /***********************************************************************
1455 * _adj_fprem1 (MSVCRT.@)
1457 * This function is likely to have the wrong number of arguments.
1460 * I _think_ this function is intended to work around the Pentium
1463 void _adj_fprem1(void)
1465 TRACE("(): stub\n");
1468 /***********************************************************************
1469 * _adj_fptan (MSVCRT.@)
1471 * This function is likely to have the wrong number of arguments.
1474 * I _think_ this function is intended to work around the Pentium
1477 void _adj_fptan(void)
1479 TRACE("(): stub\n");
1482 /***********************************************************************
1483 * _safe_fdiv (MSVCRT.@)
1485 * This function is likely to have the wrong number of arguments.
1488 * I _think_ this function is intended to work around the Pentium
1491 void _safe_fdiv(void)
1493 TRACE("(): stub\n");
1496 /***********************************************************************
1497 * _safe_fdivr (MSVCRT.@)
1499 * This function is likely to have the wrong number of arguments.
1502 * I _think_ this function is intended to work around the Pentium
1505 void _safe_fdivr(void)
1507 TRACE("(): stub\n");
1510 /***********************************************************************
1511 * _safe_fprem (MSVCRT.@)
1513 * This function is likely to have the wrong number of arguments.
1516 * I _think_ this function is intended to work around the Pentium
1519 void _safe_fprem(void)
1521 TRACE("(): stub\n");
1524 /***********************************************************************
1525 * _safe_fprem1 (MSVCRT.@)
1528 * This function is likely to have the wrong number of arguments.
1531 * I _think_ this function is intended to work around the Pentium
1534 void _safe_fprem1(void)
1536 TRACE("(): stub\n");
1539 #endif /* __i386__ */