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
24 #define __USE_ISOC9X 1
25 #define __USE_ISOC99 1
31 #include "msvcrt/stdlib.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
38 #ifndef finite /* Could be a macro */
40 #define finite(x) isfinite(x)
42 #define finite(x) (!isnan(x)) /* At least catch some cases */
51 /* fpclass constants */
52 #define _FPCLASS_SNAN 1
53 #define _FPCLASS_QNAN 2
54 #define _FPCLASS_NINF 4
56 #define _FPCLASS_ND 16
57 #define _FPCLASS_NZ 32
58 #define _FPCLASS_PZ 64
59 #define _FPCLASS_PD 128
60 #define _FPCLASS_PN 256
61 #define _FPCLASS_PINF 512
63 /* _statusfp bit flags */
64 #define _SW_INEXACT 0x1
65 #define _SW_UNDERFLOW 0x2
66 #define _SW_OVERFLOW 0x4
67 #define _SW_ZERODIVIDE 0x8
68 #define _SW_INVALID 0x10
69 #define _SW_DENORMAL 0x80000
71 /* _controlfp masks and bitflags - x86 only so far*/
73 #define _MCW_EM 0x8001f
74 #define _EM_INEXACT 0x1
75 #define _EM_UNDERFLOW 0x2
76 #define _EM_OVERFLOW 0x4
77 #define _EM_ZERODIVIDE 0x8
78 #define _EM_INVALID 0x10
82 #define _RC_DOWN 0x100
84 #define _RC_CHOP 0x300
86 #define _MCW_PC 0x30000
88 #define _PC_53 0x10000
89 #define _PC_24 0x20000
91 #define _MCW_IC 0x40000
92 #define _IC_AFFINE 0x40000
93 #define _IC_PROJECTIVE 0x0
95 #define _EM_DENORMAL 0x80000
98 typedef struct __MSVCRT_complex
104 typedef struct __MSVCRT_exception
114 typedef int (*MSVCRT_matherr_func
)(MSVCRT_exception
*);
116 static MSVCRT_matherr_func MSVCRT_default_matherr_func
= NULL
;
118 #if defined(__GNUC__) && defined(__i386__)
120 #define FPU_DOUBLE(var) double var; \
121 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
122 #define FPU_DOUBLES(var1,var2) double var1,var2; \
123 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
124 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
126 /*********************************************************************
132 if (x
< -1.0 || x
> 1.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
136 /*********************************************************************
142 if (x
< -1.0 || x
> 1.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
146 /*********************************************************************
152 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
156 /*********************************************************************
157 * _CIatan2 (MSVCRT.@)
159 double _CIatan2(void)
162 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
166 /*********************************************************************
172 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
176 /*********************************************************************
182 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
186 /*********************************************************************
192 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
196 /*********************************************************************
202 if (!finite(x
) || !finite(y
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
206 /*********************************************************************
212 if (x
< 0.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
213 if (x
== 0.0) SET_THREAD_VAR(errno
,MSVCRT_ERANGE
);
217 /*********************************************************************
218 * _CIlog10 (MSVCRT.@)
220 double _CIlog10(void)
223 if (x
< 0.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
224 if (x
== 0.0) SET_THREAD_VAR(errno
,MSVCRT_ERANGE
);
228 /*********************************************************************
235 /* FIXME: If x < 0 and y is not integral, set EDOM */
237 if (!finite(z
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
241 /*********************************************************************
247 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
251 /*********************************************************************
257 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
261 /*********************************************************************
267 if (x
< 0.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
271 /*********************************************************************
277 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
281 /*********************************************************************
287 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
291 #else /* defined(__GNUC__) && defined(__i386__) */
293 /* The above cannot be called on non x86 platforms, stub them for linking */
295 #define IX86_ONLY(func) double MSVCRT_##func(void) { return 0.0; }
314 #endif /* defined(__GNUC__) && defined(__i386__) */
316 /*********************************************************************
317 * _fpclass (MSVCRT.@)
319 int _fpclass(double num
)
321 #if defined(HAVE_FPCLASS) || defined(fpclass)
322 switch (fpclass( num
))
324 case FP_SNAN
: return _FPCLASS_SNAN
;
325 case FP_QNAN
: return _FPCLASS_QNAN
;
326 case FP_NINF
: return _FPCLASS_NINF
;
327 case FP_PINF
: return _FPCLASS_PINF
;
328 case FP_NDENORM
: return _FPCLASS_ND
;
329 case FP_PDENORM
: return _FPCLASS_PD
;
330 case FP_NZERO
: return _FPCLASS_NZ
;
331 case FP_PZERO
: return _FPCLASS_PZ
;
332 case FP_NNORM
: return _FPCLASS_NN
;
335 #elif defined (fpclassify)
336 switch (fpclassify( num
))
338 case FP_NAN
: return _FPCLASS_QNAN
;
339 case FP_INFINITE
: return signbit(num
) ? _FPCLASS_NINF
: _FPCLASS_PINF
;
340 case FP_SUBNORMAL
: return signbit(num
) ?_FPCLASS_ND
: _FPCLASS_PD
;
341 case FP_ZERO
: return signbit(num
) ? _FPCLASS_NZ
: _FPCLASS_PZ
;
343 return signbit(num
) ? _FPCLASS_NN
: _FPCLASS_PN
;
346 return _FPCLASS_QNAN
;
347 return num
== 0.0 ? _FPCLASS_PZ
: (num
< 0 ? _FPCLASS_NN
: _FPCLASS_PN
);
351 /*********************************************************************
354 unsigned int _rotl(unsigned int num
, int shift
)
357 return (num
<< shift
) | (num
>> (32-shift
));
360 /*********************************************************************
363 double _logb(double num
)
365 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
369 /*********************************************************************
372 unsigned long _lrotl(unsigned long num
, int shift
)
375 return (num
<< shift
) | (num
>> (32-shift
));
378 /*********************************************************************
381 unsigned long _lrotr(unsigned long num
, int shift
)
384 return (num
>> shift
) | (num
<< (32-shift
));
387 /*********************************************************************
390 unsigned int _rotr(unsigned int num
, int shift
)
393 return (num
>> shift
) | (num
<< (32-shift
));
396 /*********************************************************************
399 double _scalb(double num
, long power
)
401 /* Note - Can't forward directly as libc expects y as double */
402 double dblpower
= (double)power
;
403 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
404 return scalb(num
, dblpower
);
407 /*********************************************************************
408 * _matherr (MSVCRT.@)
410 int _matherr(MSVCRT_exception
*e
)
413 TRACE("(%p = %d, %s, %g %g %g)\n",e
, e
->type
, e
->name
, e
->arg1
, e
->arg2
,
417 if (MSVCRT_default_matherr_func
)
418 return MSVCRT_default_matherr_func(e
);
419 ERR(":Unhandled math error!\n");
423 /*********************************************************************
424 * __setusermatherr (MSVCRT.@)
426 void MSVCRT___setusermatherr(MSVCRT_matherr_func func
)
428 MSVCRT_default_matherr_func
= func
;
429 TRACE(":new matherr handler %p\n", func
);
432 /**********************************************************************
433 * _statusfp (MSVCRT.@)
435 unsigned int _statusfp(void)
437 unsigned int retVal
= 0;
438 #if defined(__GNUC__) && defined(__i386__)
441 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) : );
442 if (fpword
& 0x1) retVal
|= _SW_INVALID
;
443 if (fpword
& 0x2) retVal
|= _SW_DENORMAL
;
444 if (fpword
& 0x4) retVal
|= _SW_ZERODIVIDE
;
445 if (fpword
& 0x8) retVal
|= _SW_OVERFLOW
;
446 if (fpword
& 0x10) retVal
|= _SW_UNDERFLOW
;
447 if (fpword
& 0x20) retVal
|= _SW_INEXACT
;
449 FIXME(":Not implemented!\n");
454 /*********************************************************************
455 * _clearfp (MSVCRT.@)
457 unsigned int _clearfp(void)
459 unsigned int retVal
= _statusfp();
460 #if defined(__GNUC__) && defined(__i386__)
461 __asm__
__volatile__( "fnclex" );
463 FIXME(":Not Implemented\n");
468 /*********************************************************************
471 double MSVCRT_ldexp(double num
, long exp
)
473 double z
= ldexp(num
,exp
);
476 SET_THREAD_VAR(errno
,MSVCRT_ERANGE
);
477 else if (z
== 0 && signbit(z
))
478 z
= 0.0; /* Convert -0 -> +0 */
482 /*********************************************************************
485 double _cabs(MSVCRT_complex num
)
487 return sqrt(num
.real
* num
.real
+ num
.imaginary
* num
.imaginary
);
490 /*********************************************************************
491 * _chgsign (MSVCRT.@)
493 double _chgsign(double num
)
495 /* FIXME: +-infinity,Nan not tested */
499 /*********************************************************************
500 * _control87 (MSVCRT.@)
502 unsigned int _control87(unsigned int newval
, unsigned int mask
)
504 #if defined(__GNUC__) && defined(__i386__)
505 unsigned int fpword
= 0;
506 unsigned int flags
= 0;
508 TRACE("(%08x, %08x): Called\n", newval
, mask
);
510 /* Get fp control word */
511 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) : );
513 TRACE("Control word before : %08x\n", fpword
);
515 /* Convert into mask constants */
516 if (fpword
& 0x1) flags
|= _EM_INVALID
;
517 if (fpword
& 0x2) flags
|= _EM_DENORMAL
;
518 if (fpword
& 0x4) flags
|= _EM_ZERODIVIDE
;
519 if (fpword
& 0x8) flags
|= _EM_OVERFLOW
;
520 if (fpword
& 0x10) flags
|= _EM_UNDERFLOW
;
521 if (fpword
& 0x20) flags
|= _EM_INEXACT
;
522 switch(fpword
& 0xC00) {
523 case 0xC00: flags
|= _RC_UP
|_RC_DOWN
; break;
524 case 0x800: flags
|= _RC_UP
; break;
525 case 0x400: flags
|= _RC_DOWN
; break;
527 switch(fpword
& 0x300) {
528 case 0x0: flags
|= _PC_24
; break;
529 case 0x200: flags
|= _PC_53
; break;
530 case 0x300: flags
|= _PC_64
; break;
532 if (fpword
& 0x1000) flags
|= _IC_AFFINE
;
534 /* Mask with parameters */
535 flags
= (flags
& ~mask
) | (newval
& mask
);
537 /* Convert (masked) value back to fp word */
539 if (flags
& _EM_INVALID
) fpword
|= 0x1;
540 if (flags
& _EM_DENORMAL
) fpword
|= 0x2;
541 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x4;
542 if (flags
& _EM_OVERFLOW
) fpword
|= 0x8;
543 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x10;
544 if (flags
& _EM_INEXACT
) fpword
|= 0x20;
545 switch(flags
& (_RC_UP
| _RC_DOWN
)) {
546 case _RC_UP
|_RC_DOWN
: fpword
|= 0xC00; break;
547 case _RC_UP
: fpword
|= 0x800; break;
548 case _RC_DOWN
: fpword
|= 0x400; break;
550 switch (flags
& (_PC_24
| _PC_53
)) {
551 case _PC_64
: fpword
|= 0x300; break;
552 case _PC_53
: fpword
|= 0x200; break;
553 case _PC_24
: fpword
|= 0x0; break;
555 if (flags
& _IC_AFFINE
) fpword
|= 0x1000;
557 TRACE("Control word after : %08x\n", fpword
);
559 /* Put fp control word */
560 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
564 FIXME(":Not Implemented!\n");
569 /*********************************************************************
570 * _controlfp (MSVCRT.@)
572 unsigned int _controlfp(unsigned int newval
, unsigned int mask
)
574 return _control87( newval
, mask
& ~_EM_DENORMAL
);
577 /*********************************************************************
578 * _copysign (MSVCRT.@)
580 double _copysign(double num
, double sign
)
582 /* FIXME: Behaviour for Nan/Inf? */
584 return num
< 0.0 ? num
: -num
;
585 return num
< 0.0 ? -num
: num
;
588 /*********************************************************************
591 int _finite(double num
)
593 return (finite(num
)?1:0); /* See comment for _isnan() */
596 /*********************************************************************
597 * _fpreset (MSVCRT.@)
601 #if defined(__GNUC__) && defined(__i386__)
602 __asm__
__volatile__( "fninit" );
604 FIXME(":Not Implemented!\n");
608 /*********************************************************************
611 INT
_isnan(double num
)
613 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
614 * Do the same, as the result may be used in calculations
616 return isnan(num
) ? 1 : 0;
619 /*********************************************************************
622 double _y0(double num
)
625 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
627 if (_fpclass(retval
) == _FPCLASS_NINF
)
629 SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
635 /*********************************************************************
638 double _y1(double num
)
641 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
643 if (_fpclass(retval
) == _FPCLASS_NINF
)
645 SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
651 /*********************************************************************
654 double _yn(int order
, double num
)
657 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
658 retval
= yn(order
,num
);
659 if (_fpclass(retval
) == _FPCLASS_NINF
)
661 SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
667 /*********************************************************************
668 * _nextafter (MSVCRT.@)
670 double _nextafter(double num
, double next
)
673 if (!finite(num
) || !finite(next
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
674 retval
= nextafter(num
,next
);
678 #include <stdlib.h> /* div_t, ldiv_t */
680 /*********************************************************************
683 * [i386] Windows binary compatible - returns the struct in eax/edx.
686 LONGLONG
MSVCRT_div(int num
, int denom
)
689 div_t dt
= div(num
,denom
);
690 retval
= ((LONGLONG
)dt
.rem
<< 32) | dt
.quot
;
694 /*********************************************************************
697 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
699 MSVCRT_div_t
MSVCRT_div(int num
, int denom
)
701 return div(num
,denom
);
703 #endif /* ifdef __i386__ */
706 /*********************************************************************
709 * [i386] Windows binary compatible - returns the struct in eax/edx.
712 ULONGLONG
MSVCRT_ldiv(long num
, long denom
)
715 ldiv_t ldt
= ldiv(num
,denom
);
716 retval
= ((ULONGLONG
)ldt
.rem
<< 32) | (ULONG
)ldt
.quot
;
720 /*********************************************************************
723 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
725 MSVCRT_ldiv_t
MSVCRT_ldiv(long num
, long denom
)
727 return ldiv(num
,denom
);
729 #endif /* ifdef __i386__ */
731 /***********************************************************************
732 * _adj_fdiv_m16i (MSVCRT.@)
734 * This function is likely to have the wrong number of arguments.
737 * I _think_ this function is intended to work around the Pentium
740 void _adj_fdiv_m16i(void)
745 /***********************************************************************
746 * _adj_fdiv_m32 (MSVCRT.@)
748 * This function is likely to have the wrong number of arguments.
751 * I _think_ this function is intended to work around the Pentium
754 void _adj_fdiv_m32(void)
759 /***********************************************************************
760 * _adj_fdiv_m32i (MSVCRT.@)
762 * This function is likely to have the wrong number of arguments.
765 * I _think_ this function is intended to work around the Pentium
768 void _adj_fdiv_m32i(void)
773 /***********************************************************************
774 * _adj_fdiv_m64 (MSVCRT.@)
776 * This function is likely to have the wrong number of arguments.
779 * I _think_ this function is intended to work around the Pentium
782 void _adj_fdiv_m64(void)
787 /***********************************************************************
788 * _adj_fdiv_r (MSVCRT.@)
790 * This function is likely to have the wrong number of arguments.
793 * I _think_ this function is intended to work around the Pentium
796 void _adj_fdiv_r(void)
801 /***********************************************************************
802 * _adj_fdivr_m16i (MSVCRT.@)
804 * This function is likely to have the wrong number of arguments.
807 * I _think_ this function is intended to work around the Pentium
810 void _adj_fdivr_m16i(void)
815 /***********************************************************************
816 * _adj_fdivr_m32 (MSVCRT.@)
818 * This function is likely to have the wrong number of arguments.
821 * I _think_ this function is intended to work around the Pentium
824 void _adj_fdivr_m32(void)
829 /***********************************************************************
830 * _adj_fdivr_m32i (MSVCRT.@)
832 * This function is likely to have the wrong number of arguments.
835 * I _think_ this function is intended to work around the Pentium
838 void _adj_fdivr_m32i(void)
843 /***********************************************************************
844 * _adj_fdivr_m64 (MSVCRT.@)
846 * This function is likely to have the wrong number of arguments.
849 * I _think_ this function is intended to work around the Pentium
852 void _adj_fdivr_m64(void)
857 /***********************************************************************
858 * _adj_fpatan (MSVCRT.@)
860 * This function is likely to have the wrong number of arguments.
863 * I _think_ this function is intended to work around the Pentium
866 void _adj_fpatan(void)
871 /***********************************************************************
872 * _adj_fprem (MSVCRT.@)
874 * This function is likely to have the wrong number of arguments.
877 * I _think_ this function is intended to work around the Pentium
880 void _adj_fprem(void)
885 /***********************************************************************
886 * _adj_fprem1 (MSVCRT.@)
888 * This function is likely to have the wrong number of arguments.
891 * I _think_ this function is intended to work around the Pentium
894 void _adj_fprem1(void)
899 /***********************************************************************
900 * _adj_fptan (MSVCRT.@)
902 * This function is likely to have the wrong number of arguments.
905 * I _think_ this function is intended to work around the Pentium
908 void _adj_fptan(void)
913 /***********************************************************************
914 * _adjust_fdiv (MSVCRT.@)
916 * I _think_ this function should be a variable indicating whether
917 * Pentium fdiv bug safe code should be used.
919 void _adjust_fdiv(void)
924 /***********************************************************************
925 * _safe_fdiv (MSVCRT.@)
927 * This function is likely to have the wrong number of arguments.
930 * I _think_ this function is intended to work around the Pentium
933 void _safe_fdiv(void)
938 /***********************************************************************
939 * _safe_fdivr (MSVCRT.@)
941 * This function is likely to have the wrong number of arguments.
944 * I _think_ this function is intended to work around the Pentium
947 void _safe_fdivr(void)
952 /***********************************************************************
953 * _safe_fprem (MSVCRT.@)
955 * This function is likely to have the wrong number of arguments.
958 * I _think_ this function is intended to work around the Pentium
961 void _safe_fprem(void)
966 /***********************************************************************
967 * _safe_fprem1 (MSVCRT.@)
970 * This function is likely to have the wrong number of arguments.
973 * I _think_ this function is intended to work around the Pentium
976 void _safe_fprem1(void)