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
22 #include "msvcrt/errno.h"
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
)) *MSVCRT__errno() = MSVCRT_EDOM
;
136 /*********************************************************************
142 if (x
< -1.0 || x
> 1.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
146 /*********************************************************************
152 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
156 /*********************************************************************
157 * _CIatan2 (MSVCRT.@)
159 double _CIatan2(void)
162 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
166 /*********************************************************************
172 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
176 /*********************************************************************
182 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
186 /*********************************************************************
192 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
196 /*********************************************************************
202 if (!finite(x
) || !finite(y
)) *MSVCRT__errno() = MSVCRT_EDOM
;
206 /*********************************************************************
212 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
213 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
217 /*********************************************************************
218 * _CIlog10 (MSVCRT.@)
220 double _CIlog10(void)
223 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
224 if (x
== 0.0) *MSVCRT__errno() = MSVCRT_ERANGE
;
228 /*********************************************************************
235 /* FIXME: If x < 0 and y is not integral, set EDOM */
237 if (!finite(z
)) *MSVCRT__errno() = MSVCRT_EDOM
;
241 /*********************************************************************
247 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
251 /*********************************************************************
257 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
261 /*********************************************************************
267 if (x
< 0.0 || !finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
271 /*********************************************************************
277 if (!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
281 /*********************************************************************
287 if (!finite(x
)) *MSVCRT__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 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
;
333 case FP_PNORM
: return _FPCLASS_PN
;
336 #elif defined (fpclassify)
337 switch (fpclassify( num
))
339 case FP_NAN
: return _FPCLASS_QNAN
;
340 case FP_INFINITE
: return signbit(num
) ? _FPCLASS_NINF
: _FPCLASS_PINF
;
341 case FP_SUBNORMAL
: return signbit(num
) ?_FPCLASS_ND
: _FPCLASS_PD
;
342 case FP_ZERO
: return signbit(num
) ? _FPCLASS_NZ
: _FPCLASS_PZ
;
344 return signbit(num
) ? _FPCLASS_NN
: _FPCLASS_PN
;
347 return _FPCLASS_QNAN
;
348 return num
== 0.0 ? _FPCLASS_PZ
: (num
< 0 ? _FPCLASS_NN
: _FPCLASS_PN
);
352 /*********************************************************************
355 unsigned int _rotl(unsigned int num
, int shift
)
358 return (num
<< shift
) | (num
>> (32-shift
));
361 /*********************************************************************
364 double _logb(double num
)
366 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
370 /*********************************************************************
373 unsigned long _lrotl(unsigned long num
, int shift
)
376 return (num
<< shift
) | (num
>> (32-shift
));
379 /*********************************************************************
382 unsigned long _lrotr(unsigned long num
, int shift
)
385 return (num
>> shift
) | (num
<< (32-shift
));
388 /*********************************************************************
391 unsigned int _rotr(unsigned int num
, int shift
)
394 return (num
>> shift
) | (num
<< (32-shift
));
397 /*********************************************************************
400 double _scalb(double num
, long power
)
402 /* Note - Can't forward directly as libc expects y as double */
403 double dblpower
= (double)power
;
404 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
405 return scalb(num
, dblpower
);
408 /*********************************************************************
409 * _matherr (MSVCRT.@)
411 int _matherr(MSVCRT_exception
*e
)
414 TRACE("(%p = %d, %s, %g %g %g)\n",e
, e
->type
, e
->name
, e
->arg1
, e
->arg2
,
418 if (MSVCRT_default_matherr_func
)
419 return MSVCRT_default_matherr_func(e
);
420 ERR(":Unhandled math error!\n");
424 /*********************************************************************
425 * __setusermatherr (MSVCRT.@)
427 void MSVCRT___setusermatherr(MSVCRT_matherr_func func
)
429 MSVCRT_default_matherr_func
= func
;
430 TRACE(":new matherr handler %p\n", func
);
433 /**********************************************************************
434 * _statusfp (MSVCRT.@)
436 unsigned int _statusfp(void)
438 unsigned int retVal
= 0;
439 #if defined(__GNUC__) && defined(__i386__)
442 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) : );
443 if (fpword
& 0x1) retVal
|= _SW_INVALID
;
444 if (fpword
& 0x2) retVal
|= _SW_DENORMAL
;
445 if (fpword
& 0x4) retVal
|= _SW_ZERODIVIDE
;
446 if (fpword
& 0x8) retVal
|= _SW_OVERFLOW
;
447 if (fpword
& 0x10) retVal
|= _SW_UNDERFLOW
;
448 if (fpword
& 0x20) retVal
|= _SW_INEXACT
;
450 FIXME(":Not implemented!\n");
455 /*********************************************************************
456 * _clearfp (MSVCRT.@)
458 unsigned int _clearfp(void)
460 unsigned int retVal
= _statusfp();
461 #if defined(__GNUC__) && defined(__i386__)
462 __asm__
__volatile__( "fnclex" );
464 FIXME(":Not Implemented\n");
469 /*********************************************************************
472 double MSVCRT_ldexp(double num
, long exp
)
474 double z
= ldexp(num
,exp
);
477 *MSVCRT__errno() = MSVCRT_ERANGE
;
478 else if (z
== 0 && signbit(z
))
479 z
= 0.0; /* Convert -0 -> +0 */
483 /*********************************************************************
486 double _cabs(MSVCRT_complex num
)
488 return sqrt(num
.real
* num
.real
+ num
.imaginary
* num
.imaginary
);
491 /*********************************************************************
492 * _chgsign (MSVCRT.@)
494 double _chgsign(double num
)
496 /* FIXME: +-infinity,Nan not tested */
500 /*********************************************************************
501 * _control87 (MSVCRT.@)
503 unsigned int _control87(unsigned int newval
, unsigned int mask
)
505 #if defined(__GNUC__) && defined(__i386__)
506 unsigned int fpword
= 0;
507 unsigned int flags
= 0;
509 TRACE("(%08x, %08x): Called\n", newval
, mask
);
511 /* Get fp control word */
512 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) : );
514 TRACE("Control word before : %08x\n", fpword
);
516 /* Convert into mask constants */
517 if (fpword
& 0x1) flags
|= _EM_INVALID
;
518 if (fpword
& 0x2) flags
|= _EM_DENORMAL
;
519 if (fpword
& 0x4) flags
|= _EM_ZERODIVIDE
;
520 if (fpword
& 0x8) flags
|= _EM_OVERFLOW
;
521 if (fpword
& 0x10) flags
|= _EM_UNDERFLOW
;
522 if (fpword
& 0x20) flags
|= _EM_INEXACT
;
523 switch(fpword
& 0xC00) {
524 case 0xC00: flags
|= _RC_UP
|_RC_DOWN
; break;
525 case 0x800: flags
|= _RC_UP
; break;
526 case 0x400: flags
|= _RC_DOWN
; break;
528 switch(fpword
& 0x300) {
529 case 0x0: flags
|= _PC_24
; break;
530 case 0x200: flags
|= _PC_53
; break;
531 case 0x300: flags
|= _PC_64
; break;
533 if (fpword
& 0x1000) flags
|= _IC_AFFINE
;
535 /* Mask with parameters */
536 flags
= (flags
& ~mask
) | (newval
& mask
);
538 /* Convert (masked) value back to fp word */
540 if (flags
& _EM_INVALID
) fpword
|= 0x1;
541 if (flags
& _EM_DENORMAL
) fpword
|= 0x2;
542 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x4;
543 if (flags
& _EM_OVERFLOW
) fpword
|= 0x8;
544 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x10;
545 if (flags
& _EM_INEXACT
) fpword
|= 0x20;
546 switch(flags
& (_RC_UP
| _RC_DOWN
)) {
547 case _RC_UP
|_RC_DOWN
: fpword
|= 0xC00; break;
548 case _RC_UP
: fpword
|= 0x800; break;
549 case _RC_DOWN
: fpword
|= 0x400; break;
551 switch (flags
& (_PC_24
| _PC_53
)) {
552 case _PC_64
: fpword
|= 0x300; break;
553 case _PC_53
: fpword
|= 0x200; break;
554 case _PC_24
: fpword
|= 0x0; break;
556 if (flags
& _IC_AFFINE
) fpword
|= 0x1000;
558 TRACE("Control word after : %08x\n", fpword
);
560 /* Put fp control word */
561 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
565 FIXME(":Not Implemented!\n");
570 /*********************************************************************
571 * _controlfp (MSVCRT.@)
573 unsigned int _controlfp(unsigned int newval
, unsigned int mask
)
576 return _control87( newval
, mask
& ~_EM_DENORMAL
);
578 FIXME(":Not Implemented!\n");
583 /*********************************************************************
584 * _copysign (MSVCRT.@)
586 double _copysign(double num
, double sign
)
588 /* FIXME: Behaviour for Nan/Inf? */
590 return num
< 0.0 ? num
: -num
;
591 return num
< 0.0 ? -num
: num
;
594 /*********************************************************************
597 int _finite(double num
)
599 return (finite(num
)?1:0); /* See comment for _isnan() */
602 /*********************************************************************
603 * _fpreset (MSVCRT.@)
607 #if defined(__GNUC__) && defined(__i386__)
608 __asm__
__volatile__( "fninit" );
610 FIXME(":Not Implemented!\n");
614 /*********************************************************************
617 INT
_isnan(double num
)
619 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
620 * Do the same, as the result may be used in calculations
622 return isnan(num
) ? 1 : 0;
625 /*********************************************************************
628 double _y0(double num
)
631 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
633 if (_fpclass(retval
) == _FPCLASS_NINF
)
635 *MSVCRT__errno() = MSVCRT_EDOM
;
641 /*********************************************************************
644 double _y1(double num
)
647 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
649 if (_fpclass(retval
) == _FPCLASS_NINF
)
651 *MSVCRT__errno() = MSVCRT_EDOM
;
657 /*********************************************************************
660 double _yn(int order
, double num
)
663 if (!finite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
664 retval
= yn(order
,num
);
665 if (_fpclass(retval
) == _FPCLASS_NINF
)
667 *MSVCRT__errno() = MSVCRT_EDOM
;
673 /*********************************************************************
674 * _nextafter (MSVCRT.@)
676 double _nextafter(double num
, double next
)
679 if (!finite(num
) || !finite(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
680 retval
= nextafter(num
,next
);
684 #include <stdlib.h> /* div_t, ldiv_t */
686 /*********************************************************************
689 * [i386] Windows binary compatible - returns the struct in eax/edx.
692 LONGLONG
MSVCRT_div(int num
, int denom
)
695 div_t dt
= div(num
,denom
);
696 retval
= ((LONGLONG
)dt
.rem
<< 32) | dt
.quot
;
700 /*********************************************************************
703 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
705 MSVCRT_div_t
MSVCRT_div(int num
, int denom
)
707 div_t dt
= div(num
,denom
);
715 #endif /* ifdef __i386__ */
718 /*********************************************************************
721 * [i386] Windows binary compatible - returns the struct in eax/edx.
724 ULONGLONG
MSVCRT_ldiv(long num
, long denom
)
727 ldiv_t ldt
= ldiv(num
,denom
);
728 retval
= ((ULONGLONG
)ldt
.rem
<< 32) | (ULONG
)ldt
.quot
;
732 /*********************************************************************
735 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
737 MSVCRT_ldiv_t
MSVCRT_ldiv(long num
, long denom
)
739 ldiv_t result
= ldiv(num
,denom
);
742 ret
.quot
= result
.quot
;
743 ret
.rem
= result
.rem
;
747 #endif /* ifdef __i386__ */
749 /***********************************************************************
750 * _adj_fdiv_m16i (MSVCRT.@)
752 * This function is likely to have the wrong number of arguments.
755 * I _think_ this function is intended to work around the Pentium
758 void _adj_fdiv_m16i(void)
763 /***********************************************************************
764 * _adj_fdiv_m32 (MSVCRT.@)
766 * This function is likely to have the wrong number of arguments.
769 * I _think_ this function is intended to work around the Pentium
772 void _adj_fdiv_m32(void)
777 /***********************************************************************
778 * _adj_fdiv_m32i (MSVCRT.@)
780 * This function is likely to have the wrong number of arguments.
783 * I _think_ this function is intended to work around the Pentium
786 void _adj_fdiv_m32i(void)
791 /***********************************************************************
792 * _adj_fdiv_m64 (MSVCRT.@)
794 * This function is likely to have the wrong number of arguments.
797 * I _think_ this function is intended to work around the Pentium
800 void _adj_fdiv_m64(void)
805 /***********************************************************************
806 * _adj_fdiv_r (MSVCRT.@)
808 * This function is likely to have the wrong number of arguments.
811 * I _think_ this function is intended to work around the Pentium
814 void _adj_fdiv_r(void)
819 /***********************************************************************
820 * _adj_fdivr_m16i (MSVCRT.@)
822 * This function is likely to have the wrong number of arguments.
825 * I _think_ this function is intended to work around the Pentium
828 void _adj_fdivr_m16i(void)
833 /***********************************************************************
834 * _adj_fdivr_m32 (MSVCRT.@)
836 * This function is likely to have the wrong number of arguments.
839 * I _think_ this function is intended to work around the Pentium
842 void _adj_fdivr_m32(void)
847 /***********************************************************************
848 * _adj_fdivr_m32i (MSVCRT.@)
850 * This function is likely to have the wrong number of arguments.
853 * I _think_ this function is intended to work around the Pentium
856 void _adj_fdivr_m32i(void)
861 /***********************************************************************
862 * _adj_fdivr_m64 (MSVCRT.@)
864 * This function is likely to have the wrong number of arguments.
867 * I _think_ this function is intended to work around the Pentium
870 void _adj_fdivr_m64(void)
875 /***********************************************************************
876 * _adj_fpatan (MSVCRT.@)
878 * This function is likely to have the wrong number of arguments.
881 * I _think_ this function is intended to work around the Pentium
884 void _adj_fpatan(void)
889 /***********************************************************************
890 * _adj_fprem (MSVCRT.@)
892 * This function is likely to have the wrong number of arguments.
895 * I _think_ this function is intended to work around the Pentium
898 void _adj_fprem(void)
903 /***********************************************************************
904 * _adj_fprem1 (MSVCRT.@)
906 * This function is likely to have the wrong number of arguments.
909 * I _think_ this function is intended to work around the Pentium
912 void _adj_fprem1(void)
917 /***********************************************************************
918 * _adj_fptan (MSVCRT.@)
920 * This function is likely to have the wrong number of arguments.
923 * I _think_ this function is intended to work around the Pentium
926 void _adj_fptan(void)
931 /***********************************************************************
932 * _adjust_fdiv (MSVCRT.@)
934 * I _think_ this function should be a variable indicating whether
935 * Pentium fdiv bug safe code should be used.
937 void _adjust_fdiv(void)
942 /***********************************************************************
943 * _safe_fdiv (MSVCRT.@)
945 * This function is likely to have the wrong number of arguments.
948 * I _think_ this function is intended to work around the Pentium
951 void _safe_fdiv(void)
956 /***********************************************************************
957 * _safe_fdivr (MSVCRT.@)
959 * This function is likely to have the wrong number of arguments.
962 * I _think_ this function is intended to work around the Pentium
965 void _safe_fdivr(void)
970 /***********************************************************************
971 * _safe_fprem (MSVCRT.@)
973 * This function is likely to have the wrong number of arguments.
976 * I _think_ this function is intended to work around the Pentium
979 void _safe_fprem(void)
984 /***********************************************************************
985 * _safe_fprem1 (MSVCRT.@)
988 * This function is likely to have the wrong number of arguments.
991 * I _think_ this function is intended to work around the Pentium
994 void _safe_fprem1(void)