2 * msvcrt.dll math functions
4 * Copyright 2000 Jon Griffiths
10 #define __USE_ISOC9X 1
11 #define __USE_ISOC99 1
17 DEFAULT_DEBUG_CHANNEL(msvcrt
);
20 #ifndef finite /* Could be a macro */
22 #define finite(x) isfinite(x)
24 #define finite(x) (!isnan(x)) /* At least catch some cases */
33 /* fpclass constants */
34 #define _FPCLASS_SNAN 1
35 #define _FPCLASS_QNAN 2
36 #define _FPCLASS_NINF 4
38 #define _FPCLASS_ND 16
39 #define _FPCLASS_NZ 32
40 #define _FPCLASS_PZ 64
41 #define _FPCLASS_PD 128
42 #define _FPCLASS_PN 256
43 #define _FPCLASS_PINF 512
45 /* _statusfp bit flags */
46 #define _SW_INEXACT 0x1
47 #define _SW_UNDERFLOW 0x2
48 #define _SW_OVERFLOW 0x4
49 #define _SW_ZERODIVIDE 0x8
50 #define _SW_INVALID 0x10
51 #define _SW_DENORMAL 0x80000
53 /* _controlfp masks and bitflags - x86 only so far*/
55 #define _MCW_EM 0x8001f
56 #define _EM_INEXACT 0x1
57 #define _EM_UNDERFLOW 0x2
58 #define _EM_OVERFLOW 0x4
59 #define _EM_ZERODIVIDE 0x8
60 #define _EM_INVALID 0x10
64 #define _RC_DOWN 0x100
66 #define _RC_CHOP 0x300
68 #define _MCW_PC 0x30000
70 #define _PC_53 0x10000
71 #define _PC_24 0x20000
73 #define _MCW_IC 0x40000
74 #define _IC_AFFINE 0x40000
75 #define _IC_PROJECTIVE 0x0
77 #define _EM_DENORMAL 0x80000
80 typedef struct __MSVCRT_complex
86 typedef struct __MSVCRT_exception
96 typedef int (__cdecl
*MSVCRT_matherr_func
)(MSVCRT_exception
*);
98 static MSVCRT_matherr_func MSVCRT_default_matherr_func
= NULL
;
100 #if defined(__GNUC__) && defined(__i386__)
102 #define FPU_DOUBLE(var) double var; \
103 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
104 #define FPU_DOUBLES(var1,var2) double var1,var2; \
105 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
106 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
108 /*********************************************************************
111 double __cdecl
MSVCRT__CIacos(void)
114 if (x
< -1.0 || x
> 1.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
118 /*********************************************************************
121 double __cdecl
MSVCRT__CIasin(void)
124 if (x
< -1.0 || x
> 1.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
128 /*********************************************************************
131 double __cdecl
MSVCRT__CIatan(void)
134 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
138 /*********************************************************************
139 * _CIatan2 (MSVCRT.@)
141 double __cdecl
MSVCRT__CIatan2(void)
144 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
148 /*********************************************************************
151 double __cdecl
MSVCRT__CIcos(void)
154 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
158 /*********************************************************************
161 double __cdecl
MSVCRT__CIcosh(void)
164 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
168 /*********************************************************************
171 double __cdecl
MSVCRT__CIexp(void)
174 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
178 /*********************************************************************
181 double __cdecl
MSVCRT__CIfmod(void)
184 if (!finite(x
) || !finite(y
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
188 /*********************************************************************
191 double __cdecl
MSVCRT__CIlog(void)
194 if (x
< 0.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
195 if (x
== 0.0) SET_THREAD_VAR(errno
,MSVCRT_ERANGE
);
199 /*********************************************************************
200 * _CIlog10 (MSVCRT.@)
202 double __cdecl
MSVCRT__CIlog10(void)
205 if (x
< 0.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
206 if (x
== 0.0) SET_THREAD_VAR(errno
,MSVCRT_ERANGE
);
210 /*********************************************************************
213 double __cdecl
MSVCRT__CIpow(void)
217 /* FIXME: If x < 0 and y is not integral, set EDOM */
219 if (!finite(z
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
223 /*********************************************************************
226 double __cdecl
MSVCRT__CIsin(void)
229 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
233 /*********************************************************************
236 double __cdecl
MSVCRT__CIsinh(void)
239 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
243 /*********************************************************************
246 double __cdecl
MSVCRT__CIsqrt(void)
249 if (x
< 0.0 || !finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
253 /*********************************************************************
256 double __cdecl
MSVCRT__CItan(void)
259 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
263 /*********************************************************************
266 double __cdecl
MSVCRT__CItanh(void)
269 if (!finite(x
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
273 #else /* defined(__GNUC__) && defined(__i386__) */
275 /* The above cannot be called on non x86 platforms, stub them for linking */
277 #define IX86_ONLY(func) double __cdecl MSVCRT_##func(void) { return 0.0; }
296 #endif /* defined(__GNUC__) && defined(__i386__) */
298 /*********************************************************************
299 * _fpclass (MSVCRT.@)
301 int __cdecl
MSVCRT__fpclass(double num
)
303 #if defined(HAVE_FPCLASS) || defined(fpclass)
304 switch (fpclass( num
))
306 case FP_SNAN
: return _FPCLASS_SNAN
;
307 case FP_QNAN
: return _FPCLASS_QNAN
;
308 case FP_NINF
: return _FPCLASS_NINF
;
309 case FP_PINF
: return _FPCLASS_PINF
;
310 case FP_NDENORM
: return _FPCLASS_ND
;
311 case FP_PDENORM
: return _FPCLASS_PD
;
312 case FP_NZERO
: return _FPCLASS_NZ
;
313 case FP_PZERO
: return _FPCLASS_PZ
;
314 case FP_NNORM
: return _FPCLASS_NN
;
317 #elif defined (fpclassify)
318 switch (fpclassify( num
))
320 case FP_NAN
: return _FPCLASS_QNAN
;
321 case FP_INFINITE
: return signbit(num
) ? _FPCLASS_NINF
: _FPCLASS_PINF
;
322 case FP_SUBNORMAL
: return signbit(num
) ?_FPCLASS_ND
: _FPCLASS_PD
;
323 case FP_ZERO
: return signbit(num
) ? _FPCLASS_NZ
: _FPCLASS_PZ
;
325 return signbit(num
) ? _FPCLASS_NN
: _FPCLASS_PN
;
328 return _FPCLASS_QNAN
;
329 return num
== 0.0 ? _FPCLASS_PZ
: (num
< 0 ? _FPCLASS_NN
: _FPCLASS_PN
);
333 /*********************************************************************
336 unsigned int __cdecl
MSVCRT__rotl(unsigned int num
, int shift
)
339 return (num
<< shift
) | (num
>> (32-shift
));
342 /*********************************************************************
345 double __cdecl
MSVCRT__logb(double num
)
347 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
351 /*********************************************************************
354 unsigned long __cdecl
MSVCRT__lrotl(unsigned long num
, int shift
)
357 return (num
<< shift
) | (num
>> (32-shift
));
360 /*********************************************************************
363 unsigned long __cdecl
MSVCRT__lrotr(unsigned long num
, int shift
)
366 return (num
>> shift
) | (num
<< (32-shift
));
369 /*********************************************************************
372 unsigned int __cdecl
MSVCRT__rotr(unsigned int num
, int shift
)
375 return (num
>> shift
) | (num
<< (32-shift
));
378 /*********************************************************************
381 double __cdecl
MSVCRT__scalb(double num
, long power
)
383 /* Note - Can't forward directly as libc expects y as double */
384 double dblpower
= (double)power
;
385 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
386 return scalb(num
, dblpower
);
389 /*********************************************************************
390 * _matherr (MSVCRT.@)
392 int __cdecl
MSVCRT__matherr(MSVCRT_exception
*e
)
395 TRACE("(%p = %d, %s, %g %g %g)\n",e
, e
->type
, e
->name
, e
->arg1
, e
->arg2
,
399 if (MSVCRT_default_matherr_func
)
400 return MSVCRT_default_matherr_func(e
);
401 ERR(":Unhandled math error!\n");
405 /*********************************************************************
406 * __setusermatherr (MSVCRT.@)
408 void __cdecl
MSVCRT___setusermatherr(MSVCRT_matherr_func func
)
410 MSVCRT_default_matherr_func
= func
;
411 TRACE(":new matherr handler %p\n", func
);
414 /**********************************************************************
415 * _statusfp (MSVCRT.@)
417 unsigned int __cdecl
MSVCRT__statusfp(void)
419 unsigned int retVal
= 0;
420 #if defined(__GNUC__) && defined(__i386__)
423 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) : );
424 if (fpword
& 0x1) retVal
|= _SW_INVALID
;
425 if (fpword
& 0x2) retVal
|= _SW_DENORMAL
;
426 if (fpword
& 0x4) retVal
|= _SW_ZERODIVIDE
;
427 if (fpword
& 0x8) retVal
|= _SW_OVERFLOW
;
428 if (fpword
& 0x10) retVal
|= _SW_UNDERFLOW
;
429 if (fpword
& 0x20) retVal
|= _SW_INEXACT
;
431 FIXME(":Not implemented!\n");
436 /*********************************************************************
437 * _clearfp (MSVCRT.@)
439 unsigned int __cdecl
MSVCRT__clearfp(void)
441 unsigned int retVal
= MSVCRT__statusfp();
442 #if defined(__GNUC__) && defined(__i386__)
443 __asm__
__volatile__( "fnclex" );
445 FIXME(":Not Implemented\n");
450 /*********************************************************************
453 double __cdecl
MSVCRT_ldexp(double num
, long exp
)
455 double z
= ldexp(num
,exp
);
458 SET_THREAD_VAR(errno
,MSVCRT_ERANGE
);
459 else if (z
== 0 && signbit(z
))
460 z
= 0.0; /* Convert -0 -> +0 */
464 /*********************************************************************
467 double __cdecl
MSVCRT__cabs(MSVCRT_complex num
)
469 return sqrt(num
.real
* num
.real
+ num
.imaginary
* num
.imaginary
);
472 /*********************************************************************
473 * _chgsign (MSVCRT.@)
475 double __cdecl
MSVCRT__chgsign(double num
)
477 /* FIXME: +-infinity,Nan not tested */
481 /*********************************************************************
482 * _control87 (MSVCRT.@)
484 unsigned int __cdecl
MSVCRT__control87(unsigned int newval
, unsigned int mask
)
486 #if defined(__GNUC__) && defined(__i386__)
487 unsigned int fpword
, flags
= 0;
489 /* Get fp control word */
490 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) : );
492 /* Convert into mask constants */
493 if (fpword
& 0x1) flags
|= _EM_INVALID
;
494 if (fpword
& 0x2) flags
|= _EM_DENORMAL
;
495 if (fpword
& 0x4) flags
|= _EM_ZERODIVIDE
;
496 if (fpword
& 0x8) flags
|= _EM_OVERFLOW
;
497 if (fpword
& 0x10) flags
|= _EM_UNDERFLOW
;
498 if (fpword
& 0x20) flags
|= _EM_INEXACT
;
499 switch(fpword
& 0xC00) {
500 case 0xC00: flags
|= _RC_UP
|_RC_DOWN
; break;
501 case 0x800: flags
|= _RC_UP
; break;
502 case 0x400: flags
|= _RC_DOWN
; break;
504 switch(fpword
& 0x300) {
505 case 0x0: flags
|= _PC_24
; break;
506 case 0x200: flags
|= _PC_53
; break;
507 case 0x300: flags
|= _PC_64
; break;
509 if (fpword
& 0x1000) flags
|= _IC_AFFINE
;
511 /* Mask with parameters */
512 flags
= (flags
& ~mask
) | (newval
& mask
);
514 /* Convert (masked) value back to fp word */
516 if (flags
& _EM_INVALID
) fpword
|= 0x1;
517 if (flags
& _EM_DENORMAL
) fpword
|= 0x2;
518 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x4;
519 if (flags
& _EM_OVERFLOW
) fpword
|= 0x8;
520 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x10;
521 if (flags
& _EM_INEXACT
) fpword
|= 0x20;
522 switch(flags
& (_RC_UP
| _RC_DOWN
)) {
523 case _RC_UP
|_RC_DOWN
: fpword
|= 0xC00; break;
524 case _RC_UP
: fpword
|= 0x800; break;
525 case _RC_DOWN
: fpword
|= 0x400; break;
527 switch (flags
& (_PC_24
| _PC_53
)) {
528 case _PC_64
: fpword
|= 0x300; break;
529 case _PC_53
: fpword
|= 0x200; break;
530 case _PC_24
: fpword
|= 0x0; break;
532 if (!(flags
& _IC_AFFINE
)) fpword
|= 0x1000;
534 /* Put fp control word */
535 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
538 return MSVCRT__controlfp( newval
, mask
);
542 /*********************************************************************
543 * _controlfp (MSVCRT.@)
545 unsigned int __cdecl
MSVCRT__controlfp(unsigned int newval
, unsigned int mask
)
547 #if defined(__GNUC__) && defined(__i386__)
548 return MSVCRT__control87( newval
, mask
);
550 FIXME(":Not Implemented!\n");
555 /*********************************************************************
556 * _copysign (MSVCRT.@)
558 double __cdecl
MSVCRT__copysign(double num
, double sign
)
560 /* FIXME: Behaviour for Nan/Inf? */
562 return num
< 0.0 ? num
: -num
;
563 return num
< 0.0 ? -num
: num
;
566 /*********************************************************************
569 int __cdecl
MSVCRT__finite(double num
)
571 return (finite(num
)?1:0); /* See comment for _isnan() */
574 /*********************************************************************
575 * _fpreset (MSVCRT.@)
577 void __cdecl
MSVCRT__fpreset(void)
579 #if defined(__GNUC__) && defined(__i386__)
580 __asm__
__volatile__( "fninit" );
582 FIXME(":Not Implemented!\n");
586 /*********************************************************************
589 INT __cdecl
MSVCRT__isnan(double num
)
591 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
592 * Do the same, as the result may be used in calculations
594 return isnan(num
) ? 1 : 0;
597 /*********************************************************************
600 double __cdecl
MSVCRT__y0(double num
)
603 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
605 if (MSVCRT__fpclass(retval
) == _FPCLASS_NINF
)
607 SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
613 /*********************************************************************
616 double __cdecl
MSVCRT__y1(double num
)
619 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
621 if (MSVCRT__fpclass(retval
) == _FPCLASS_NINF
)
623 SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
629 /*********************************************************************
632 double __cdecl
MSVCRT__yn(int order
, double num
)
635 if (!finite(num
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
636 retval
= yn(order
,num
);
637 if (MSVCRT__fpclass(retval
) == _FPCLASS_NINF
)
639 SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
645 /*********************************************************************
646 * _nextafter (MSVCRT.@)
648 double __cdecl
MSVCRT__nextafter(double num
, double next
)
651 if (!finite(num
) || !finite(next
)) SET_THREAD_VAR(errno
,MSVCRT_EDOM
);
652 retval
= nextafter(num
,next
);
656 #include <stdlib.h> /* div_t, ldiv_t */
658 /*********************************************************************
661 * [i386] Windows binary compatible - returns the struct in eax/edx.
664 LONGLONG __cdecl
MSVCRT_div(int num
, int denom
)
667 div_t dt
= div(num
,denom
);
668 retval
= ((LONGLONG
)dt
.rem
<< 32) | dt
.quot
;
672 /*********************************************************************
675 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
677 div_t __cdecl
MSVCRT_div(int num
, int denom
)
679 return div(num
,denom
);
681 #endif /* ifdef __i386__ */
684 /*********************************************************************
687 * [i386] Windows binary compatible - returns the struct in eax/edx.
690 ULONGLONG __cdecl
MSVCRT_ldiv(long num
, long denom
)
693 ldiv_t ldt
= ldiv(num
,denom
);
694 retval
= ((ULONGLONG
)ldt
.rem
<< 32) | (ULONG
)ldt
.quot
;
698 /*********************************************************************
701 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
703 ldiv_t __cdecl
MSVCRT_ldiv(long num
, long denom
)
705 return ldiv(num
,denom
);
707 #endif /* ifdef __i386__ */
709 /***********************************************************************
710 * _adj_fdiv_m16i (MSVCRT.@)
712 * This function is likely to have the wrong number of arguments.
715 * I _think_ this function is intended to work around the Pentium
718 void __cdecl
MSVCRT__adj_fdiv_m16i(void)
723 /***********************************************************************
724 * _adj_fdiv_m32 (MSVCRT.@)
726 * This function is likely to have the wrong number of arguments.
729 * I _think_ this function is intended to work around the Pentium
732 void __cdecl
MSVCRT__adj_fdiv_m32(void)
737 /***********************************************************************
738 * _adj_fdiv_m32i (MSVCRT.@)
740 * This function is likely to have the wrong number of arguments.
743 * I _think_ this function is intended to work around the Pentium
746 void __cdecl
MSVCRT__adj_fdiv_m32i(void)
751 /***********************************************************************
752 * _adj_fdiv_m64 (MSVCRT.@)
754 * This function is likely to have the wrong number of arguments.
757 * I _think_ this function is intended to work around the Pentium
760 void __cdecl
MSVCRT__adj_fdiv_m64(void)
765 /***********************************************************************
766 * _adj_fdiv_r (MSVCRT.@)
768 * This function is likely to have the wrong number of arguments.
771 * I _think_ this function is intended to work around the Pentium
774 void __cdecl
MSVCRT__adj_fdiv_r(void)
779 /***********************************************************************
780 * _adj_fdivr_m16i (MSVCRT.@)
782 * This function is likely to have the wrong number of arguments.
785 * I _think_ this function is intended to work around the Pentium
788 void __cdecl
MSVCRT__adj_fdivr_m16i(void)
793 /***********************************************************************
794 * _adj_fdivr_m32 (MSVCRT.@)
796 * This function is likely to have the wrong number of arguments.
799 * I _think_ this function is intended to work around the Pentium
802 void __cdecl
MSVCRT__adj_fdivr_m32(void)
807 /***********************************************************************
808 * _adj_fdivr_m32i (MSVCRT.@)
810 * This function is likely to have the wrong number of arguments.
813 * I _think_ this function is intended to work around the Pentium
816 void __cdecl
MSVCRT__adj_fdivr_m32i(void)
821 /***********************************************************************
822 * _adj_fdivr_m64 (MSVCRT.@)
824 * This function is likely to have the wrong number of arguments.
827 * I _think_ this function is intended to work around the Pentium
830 void __cdecl
MSVCRT__adj_fdivr_m64(void)
835 /***********************************************************************
836 * _adj_fpatan (MSVCRT.@)
838 * This function is likely to have the wrong number of arguments.
841 * I _think_ this function is intended to work around the Pentium
844 void __cdecl
MSVCRT__adj_fpatan(void)
849 /***********************************************************************
850 * _adj_fprem (MSVCRT.@)
852 * This function is likely to have the wrong number of arguments.
855 * I _think_ this function is intended to work around the Pentium
858 void __cdecl
MSVCRT__adj_fprem(void)
863 /***********************************************************************
864 * _adj_fprem1 (MSVCRT.@)
866 * This function is likely to have the wrong number of arguments.
869 * I _think_ this function is intended to work around the Pentium
872 void __cdecl
MSVCRT__adj_fprem1(void)
877 /***********************************************************************
878 * _adj_fptan (MSVCRT.@)
880 * This function is likely to have the wrong number of arguments.
883 * I _think_ this function is intended to work around the Pentium
886 void __cdecl
MSVCRT__adj_fptan(void)
891 /***********************************************************************
892 * _adjust_fdiv (MSVCRT.@)
894 * I _think_ this function should be a variable indicating whether
895 * Pentium fdiv bug safe code should be used.
897 void __cdecl
MSVCRT__adjust_fdiv(void)
902 /***********************************************************************
903 * _safe_fdiv (MSVCRT.@)
905 * This function is likely to have the wrong number of arguments.
908 * I _think_ this function is intended to work around the Pentium
911 void __cdecl
MSVCRT__safe_fdiv(void)
916 /***********************************************************************
917 * _safe_fdivr (MSVCRT.@)
919 * This function is likely to have the wrong number of arguments.
922 * I _think_ this function is intended to work around the Pentium
925 void __cdecl
MSVCRT__safe_fdivr(void)
930 /***********************************************************************
931 * _safe_fprem (MSVCRT.@)
933 * This function is likely to have the wrong number of arguments.
936 * I _think_ this function is intended to work around the Pentium
939 void __cdecl
MSVCRT__safe_fprem(void)
944 /***********************************************************************
945 * _safe_fprem1 (MSVCRT.@)
948 * This function is likely to have the wrong number of arguments.
951 * I _think_ this function is intended to work around the Pentium
954 void __cdecl
MSVCRT__safe_fprem1(void)