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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 (*MSVCRT_matherr_func
)(struct MSVCRT__exception
*);
52 static MSVCRT_matherr_func MSVCRT_default_matherr_func
= NULL
;
54 #if defined(__GNUC__) && defined(__i386__)
56 #define FPU_DOUBLE(var) double var; \
57 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
58 #define FPU_DOUBLES(var1,var2) double var1,var2; \
59 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
60 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
62 /*********************************************************************
68 if (x
< -1.0 || x
> 1.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
72 /*********************************************************************
78 if (x
< -1.0 || x
> 1.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
82 /*********************************************************************
88 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
92 /*********************************************************************
98 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
102 /*********************************************************************
108 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
112 /*********************************************************************
118 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
122 /*********************************************************************
128 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
132 /*********************************************************************
138 if (!finite(x
) || !finite(y
)) *MSVCRT__errno() = MSVCRT_EDOM
;
142 /*********************************************************************
148 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
149 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
153 /*********************************************************************
154 * _CIlog10 (MSVCRT.@)
156 double _CIlog10(void)
159 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
160 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
164 /*********************************************************************
171 /* FIXME: If x < 0 and y is not integral, set EDOM */
173 if (!finite(z
)) *MSVCRT__errno() = MSVCRT_EDOM
;
177 /*********************************************************************
183 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
187 /*********************************************************************
193 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
197 /*********************************************************************
203 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
207 /*********************************************************************
213 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
217 /*********************************************************************
223 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
227 #else /* defined(__GNUC__) && defined(__i386__) */
229 /* The above cannot be called on non x86 platforms, stub them for linking */
231 #define IX86_ONLY(func) double func(void) { return 0.0; }
250 #endif /* defined(__GNUC__) && defined(__i386__) */
252 /*********************************************************************
253 * _fpclass (MSVCRT.@)
255 int _fpclass(double num
)
257 #if defined(HAVE_FPCLASS) || defined(fpclass)
258 switch (fpclass( num
))
261 case FP_SNAN
: return MSVCRT__FPCLASS_SNAN
;
264 case FP_QNAN
: return MSVCRT__FPCLASS_QNAN
;
267 case FP_NINF
: return MSVCRT__FPCLASS_NINF
;
270 case FP_PINF
: return MSVCRT__FPCLASS_PINF
;
273 case FP_NDENORM
: return MSVCRT__FPCLASS_ND
;
276 case FP_PDENORM
: return MSVCRT__FPCLASS_PD
;
279 case FP_NZERO
: return MSVCRT__FPCLASS_NZ
;
282 case FP_PZERO
: return MSVCRT__FPCLASS_PZ
;
285 case FP_NNORM
: return MSVCRT__FPCLASS_NN
;
288 case FP_PNORM
: return MSVCRT__FPCLASS_PN
;
291 return MSVCRT__FPCLASS_PN
;
292 #elif defined (fpclassify)
293 switch (fpclassify( num
))
295 case FP_NAN
: return MSVCRT__FPCLASS_QNAN
;
296 case FP_INFINITE
: return signbit(num
) ? MSVCRT__FPCLASS_NINF
: MSVCRT__FPCLASS_PINF
;
297 case FP_SUBNORMAL
: return signbit(num
) ?MSVCRT__FPCLASS_ND
: MSVCRT__FPCLASS_PD
;
298 case FP_ZERO
: return signbit(num
) ? MSVCRT__FPCLASS_NZ
: MSVCRT__FPCLASS_PZ
;
300 return signbit(num
) ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
;
303 return MSVCRT__FPCLASS_QNAN
;
304 return num
== 0.0 ? MSVCRT__FPCLASS_PZ
: (num
< 0 ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
);
308 /*********************************************************************
311 unsigned int _rotl(unsigned int num
, int shift
)
314 return (num
<< shift
) | (num
>> (32-shift
));
317 /*********************************************************************
320 double _logb(double num
)
322 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
326 /*********************************************************************
329 unsigned long _lrotl(unsigned long num
, int shift
)
332 return (num
<< shift
) | (num
>> (32-shift
));
335 /*********************************************************************
338 unsigned long _lrotr(unsigned long num
, int shift
)
341 return (num
>> shift
) | (num
<< (32-shift
));
344 /*********************************************************************
347 unsigned int _rotr(unsigned int num
, int shift
)
350 return (num
>> shift
) | (num
<< (32-shift
));
353 /*********************************************************************
356 double _scalb(double num
, long power
)
358 /* Note - Can't forward directly as libc expects y as double */
359 double dblpower
= (double)power
;
360 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
361 return scalb(num
, dblpower
);
364 /*********************************************************************
365 * _matherr (MSVCRT.@)
367 int MSVCRT__matherr(struct MSVCRT__exception
*e
)
370 TRACE("(%p = %d, %s, %g %g %g)\n",e
, e
->type
, e
->name
, e
->arg1
, e
->arg2
,
374 if (MSVCRT_default_matherr_func
)
375 return MSVCRT_default_matherr_func(e
);
376 ERR(":Unhandled math error!\n");
380 /*********************************************************************
381 * __setusermatherr (MSVCRT.@)
383 void MSVCRT___setusermatherr(MSVCRT_matherr_func func
)
385 MSVCRT_default_matherr_func
= func
;
386 TRACE(":new matherr handler %p\n", func
);
389 /**********************************************************************
390 * _statusfp (MSVCRT.@)
392 unsigned int _statusfp(void)
394 unsigned int retVal
= 0;
395 #if defined(__GNUC__) && defined(__i386__)
398 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) : );
399 if (fpword
& 0x1) retVal
|= MSVCRT__SW_INVALID
;
400 if (fpword
& 0x2) retVal
|= MSVCRT__SW_DENORMAL
;
401 if (fpword
& 0x4) retVal
|= MSVCRT__SW_ZERODIVIDE
;
402 if (fpword
& 0x8) retVal
|= MSVCRT__SW_OVERFLOW
;
403 if (fpword
& 0x10) retVal
|= MSVCRT__SW_UNDERFLOW
;
404 if (fpword
& 0x20) retVal
|= MSVCRT__SW_INEXACT
;
406 FIXME(":Not implemented!\n");
411 /*********************************************************************
412 * _clearfp (MSVCRT.@)
414 unsigned int _clearfp(void)
416 unsigned int retVal
= _statusfp();
417 #if defined(__GNUC__) && defined(__i386__)
418 __asm__
__volatile__( "fnclex" );
420 FIXME(":Not Implemented\n");
425 /*********************************************************************
426 * __fpecode (MSVCRT.@)
430 return &msvcrt_get_thread_data()->fpecode
;
433 /*********************************************************************
436 double MSVCRT_ldexp(double num
, long exp
)
438 double z
= ldexp(num
,exp
);
441 *MSVCRT__errno() = MSVCRT_ERANGE
;
442 else if (z
== 0 && signbit(z
))
443 z
= 0.0; /* Convert -0 -> +0 */
447 /*********************************************************************
450 double MSVCRT__cabs(struct MSVCRT__complex num
)
452 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
455 /*********************************************************************
456 * _chgsign (MSVCRT.@)
458 double _chgsign(double num
)
460 /* FIXME: +-infinity,Nan not tested */
464 /*********************************************************************
465 * _control87 (MSVCRT.@)
467 unsigned int _control87(unsigned int newval
, unsigned int mask
)
469 #if defined(__GNUC__) && defined(__i386__)
470 unsigned int fpword
= 0;
471 unsigned int flags
= 0;
473 TRACE("(%08x, %08x): Called\n", newval
, mask
);
475 /* Get fp control word */
476 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) : );
478 TRACE("Control word before : %08x\n", fpword
);
480 /* Convert into mask constants */
481 if (fpword
& 0x1) flags
|= MSVCRT__EM_INVALID
;
482 if (fpword
& 0x2) flags
|= MSVCRT__EM_DENORMAL
;
483 if (fpword
& 0x4) flags
|= MSVCRT__EM_ZERODIVIDE
;
484 if (fpword
& 0x8) flags
|= MSVCRT__EM_OVERFLOW
;
485 if (fpword
& 0x10) flags
|= MSVCRT__EM_UNDERFLOW
;
486 if (fpword
& 0x20) flags
|= MSVCRT__EM_INEXACT
;
487 switch(fpword
& 0xC00) {
488 case 0xC00: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
489 case 0x800: flags
|= MSVCRT__RC_UP
; break;
490 case 0x400: flags
|= MSVCRT__RC_DOWN
; break;
492 switch(fpword
& 0x300) {
493 case 0x0: flags
|= MSVCRT__PC_24
; break;
494 case 0x200: flags
|= MSVCRT__PC_53
; break;
495 case 0x300: flags
|= MSVCRT__PC_64
; break;
497 if (fpword
& 0x1000) flags
|= MSVCRT__IC_AFFINE
;
499 /* Mask with parameters */
500 flags
= (flags
& ~mask
) | (newval
& mask
);
502 /* Convert (masked) value back to fp word */
504 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x1;
505 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x2;
506 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x4;
507 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x8;
508 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x10;
509 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x20;
510 switch(flags
& (MSVCRT__RC_UP
| MSVCRT__RC_DOWN
)) {
511 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0xC00; break;
512 case MSVCRT__RC_UP
: fpword
|= 0x800; break;
513 case MSVCRT__RC_DOWN
: fpword
|= 0x400; break;
515 switch (flags
& (MSVCRT__PC_24
| MSVCRT__PC_53
)) {
516 case MSVCRT__PC_64
: fpword
|= 0x300; break;
517 case MSVCRT__PC_53
: fpword
|= 0x200; break;
518 case MSVCRT__PC_24
: fpword
|= 0x0; break;
520 if (flags
& MSVCRT__IC_AFFINE
) fpword
|= 0x1000;
522 TRACE("Control word after : %08x\n", fpword
);
524 /* Put fp control word */
525 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
529 FIXME(":Not Implemented!\n");
534 /*********************************************************************
535 * _controlfp (MSVCRT.@)
537 unsigned int _controlfp(unsigned int newval
, unsigned int mask
)
540 return _control87( newval
, mask
& ~MSVCRT__EM_DENORMAL
);
542 FIXME(":Not Implemented!\n");
547 /*********************************************************************
548 * _copysign (MSVCRT.@)
550 double _copysign(double num
, double sign
)
552 /* FIXME: Behaviour for Nan/Inf? */
554 return num
< 0.0 ? num
: -num
;
555 return num
< 0.0 ? -num
: num
;
558 /*********************************************************************
561 int _finite(double num
)
563 return (finite(num
)?1:0); /* See comment for _isnan() */
566 /*********************************************************************
567 * _fpreset (MSVCRT.@)
571 #if defined(__GNUC__) && defined(__i386__)
572 __asm__
__volatile__( "fninit" );
574 FIXME(":Not Implemented!\n");
578 /*********************************************************************
581 INT
_isnan(double num
)
583 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
584 * Do the same, as the result may be used in calculations
586 return isnan(num
) ? 1 : 0;
589 /*********************************************************************
592 double _y0(double num
)
595 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
597 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
599 *MSVCRT__errno() = MSVCRT_EDOM
;
605 /*********************************************************************
608 double _y1(double num
)
611 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
613 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
615 *MSVCRT__errno() = MSVCRT_EDOM
;
621 /*********************************************************************
624 double _yn(int order
, double num
)
627 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
628 retval
= yn(order
,num
);
629 if (_fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
631 *MSVCRT__errno() = MSVCRT_EDOM
;
637 /*********************************************************************
638 * _nextafter (MSVCRT.@)
640 double _nextafter(double num
, double next
)
643 if (!finite(num
) || !finite(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
644 retval
= nextafter(num
,next
);
648 /*********************************************************************
651 char *_ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
653 thread_data_t
*data
= msvcrt_get_thread_data();
656 if (!data
->efcvt_buffer
)
657 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
659 snprintf(data
->efcvt_buffer
, 80, "%.*e", ndigits
/* FIXME wrong */, number
);
660 *sign
= (number
< 0);
661 dec
= strchr(data
->efcvt_buffer
, '.');
662 *decpt
= (dec
) ? dec
- data
->efcvt_buffer
: -1;
663 return data
->efcvt_buffer
;
666 /***********************************************************************
669 char *_fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
671 thread_data_t
*data
= msvcrt_get_thread_data();
674 if (!data
->efcvt_buffer
)
675 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
677 snprintf(data
->efcvt_buffer
, 80, "%.*e", ndigits
, number
);
678 *sign
= (number
< 0);
679 dec
= strchr(data
->efcvt_buffer
, '.');
680 *decpt
= (dec
) ? dec
- data
->efcvt_buffer
: -1;
681 return data
->efcvt_buffer
;
684 /***********************************************************************
687 * FIXME: uses both E and F.
689 char *_gcvt( double number
, int ndigit
, char *buff
)
691 sprintf(buff
, "%.*E", ndigit
, number
);
695 #include <stdlib.h> /* div_t, ldiv_t */
697 /*********************************************************************
700 * [i386] Windows binary compatible - returns the struct in eax/edx.
703 unsigned __int64
MSVCRT_div(int num
, int denom
)
705 div_t dt
= div(num
,denom
);
706 return ((unsigned __int64
)dt
.rem
<< 32) | (unsigned int)dt
.quot
;
709 /*********************************************************************
712 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
714 MSVCRT_div_t
MSVCRT_div(int num
, int denom
)
716 div_t dt
= div(num
,denom
);
724 #endif /* ifdef __i386__ */
727 /*********************************************************************
730 * [i386] Windows binary compatible - returns the struct in eax/edx.
733 unsigned __int64
MSVCRT_ldiv(long num
, long denom
)
735 ldiv_t ldt
= ldiv(num
,denom
);
736 return ((unsigned __int64
)ldt
.rem
<< 32) | (unsigned long)ldt
.quot
;
739 /*********************************************************************
742 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
744 MSVCRT_ldiv_t
MSVCRT_ldiv(long num
, long denom
)
746 ldiv_t result
= ldiv(num
,denom
);
749 ret
.quot
= result
.quot
;
750 ret
.rem
= result
.rem
;
754 #endif /* ifdef __i386__ */
756 /***********************************************************************
757 * _adj_fdiv_m16i (MSVCRT.@)
760 * I _think_ this function is intended to work around the Pentium
763 void __stdcall
_adj_fdiv_m16i( short arg
)
768 /***********************************************************************
769 * _adj_fdiv_m32 (MSVCRT.@)
772 * I _think_ this function is intended to work around the Pentium
775 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
780 /***********************************************************************
781 * _adj_fdiv_m32i (MSVCRT.@)
784 * I _think_ this function is intended to work around the Pentium
787 void __stdcall
_adj_fdiv_m32i( int arg
)
792 /***********************************************************************
793 * _adj_fdiv_m64 (MSVCRT.@)
796 * I _think_ this function is intended to work around the Pentium
799 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
804 /***********************************************************************
805 * _adj_fdiv_r (MSVCRT.@)
807 * This function is likely to have the wrong number of arguments.
810 * I _think_ this function is intended to work around the Pentium
813 void _adj_fdiv_r(void)
818 /***********************************************************************
819 * _adj_fdivr_m16i (MSVCRT.@)
822 * I _think_ this function is intended to work around the Pentium
825 void __stdcall
_adj_fdivr_m16i( short arg
)
830 /***********************************************************************
831 * _adj_fdivr_m32 (MSVCRT.@)
834 * I _think_ this function is intended to work around the Pentium
837 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
842 /***********************************************************************
843 * _adj_fdivr_m32i (MSVCRT.@)
846 * I _think_ this function is intended to work around the Pentium
849 void __stdcall
_adj_fdivr_m32i( int arg
)
854 /***********************************************************************
855 * _adj_fdivr_m64 (MSVCRT.@)
858 * I _think_ this function is intended to work around the Pentium
861 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
866 /***********************************************************************
867 * _adj_fpatan (MSVCRT.@)
869 * This function is likely to have the wrong number of arguments.
872 * I _think_ this function is intended to work around the Pentium
875 void _adj_fpatan(void)
880 /***********************************************************************
881 * _adj_fprem (MSVCRT.@)
883 * This function is likely to have the wrong number of arguments.
886 * I _think_ this function is intended to work around the Pentium
889 void _adj_fprem(void)
894 /***********************************************************************
895 * _adj_fprem1 (MSVCRT.@)
897 * This function is likely to have the wrong number of arguments.
900 * I _think_ this function is intended to work around the Pentium
903 void _adj_fprem1(void)
908 /***********************************************************************
909 * _adj_fptan (MSVCRT.@)
911 * This function is likely to have the wrong number of arguments.
914 * I _think_ this function is intended to work around the Pentium
917 void _adj_fptan(void)
922 /***********************************************************************
923 * _adjust_fdiv (MSVCRT.@)
925 * I _think_ this function should be a variable indicating whether
926 * Pentium fdiv bug safe code should be used.
928 void _adjust_fdiv(void)
933 /***********************************************************************
934 * _safe_fdiv (MSVCRT.@)
936 * This function is likely to have the wrong number of arguments.
939 * I _think_ this function is intended to work around the Pentium
942 void _safe_fdiv(void)
947 /***********************************************************************
948 * _safe_fdivr (MSVCRT.@)
950 * This function is likely to have the wrong number of arguments.
953 * I _think_ this function is intended to work around the Pentium
956 void _safe_fdivr(void)
961 /***********************************************************************
962 * _safe_fprem (MSVCRT.@)
964 * This function is likely to have the wrong number of arguments.
967 * I _think_ this function is intended to work around the Pentium
970 void _safe_fprem(void)
975 /***********************************************************************
976 * _safe_fprem1 (MSVCRT.@)
979 * This function is likely to have the wrong number of arguments.
982 * I _think_ this function is intended to work around the Pentium
985 void _safe_fprem1(void)