2 * msvcrt.dll math functions
4 * Copyright 2000 Jon Griffiths
10 #define __USE_ISOC9X 1
11 #define __USE_ISOC99 1
17 #include "msvcrt/stdlib.h"
19 #include "wine/debug.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
24 #ifndef finite /* Could be a macro */
26 #define finite(x) isfinite(x)
28 #define finite(x) (!isnan(x)) /* At least catch some cases */
37 /* fpclass constants */
38 #define _FPCLASS_SNAN 1
39 #define _FPCLASS_QNAN 2
40 #define _FPCLASS_NINF 4
42 #define _FPCLASS_ND 16
43 #define _FPCLASS_NZ 32
44 #define _FPCLASS_PZ 64
45 #define _FPCLASS_PD 128
46 #define _FPCLASS_PN 256
47 #define _FPCLASS_PINF 512
49 /* _statusfp bit flags */
50 #define _SW_INEXACT 0x1
51 #define _SW_UNDERFLOW 0x2
52 #define _SW_OVERFLOW 0x4
53 #define _SW_ZERODIVIDE 0x8
54 #define _SW_INVALID 0x10
55 #define _SW_DENORMAL 0x80000
57 /* _controlfp masks and bitflags - x86 only so far*/
59 #define _MCW_EM 0x8001f
60 #define _EM_INEXACT 0x1
61 #define _EM_UNDERFLOW 0x2
62 #define _EM_OVERFLOW 0x4
63 #define _EM_ZERODIVIDE 0x8
64 #define _EM_INVALID 0x10
68 #define _RC_DOWN 0x100
70 #define _RC_CHOP 0x300
72 #define _MCW_PC 0x30000
74 #define _PC_53 0x10000
75 #define _PC_24 0x20000
77 #define _MCW_IC 0x40000
78 #define _IC_AFFINE 0x40000
79 #define _IC_PROJECTIVE 0x0
81 #define _EM_DENORMAL 0x80000
84 typedef struct __MSVCRT_complex
90 typedef struct __MSVCRT_exception
100 typedef int (*MSVCRT_matherr_func)(MSVCRT_exception *);
102 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
104 #if defined(__GNUC__) && defined(__i386__)
106 #define FPU_DOUBLE(var) double var; \
107 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
108 #define FPU_DOUBLES(var1,var2) double var1,var2; \
109 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
110 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
112 /*********************************************************************
118 if (x < -1.0 || x > 1.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
122 /*********************************************************************
128 if (x < -1.0 || x > 1.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
132 /*********************************************************************
138 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
142 /*********************************************************************
143 * _CIatan2 (MSVCRT.@)
145 double _CIatan2(void)
148 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
152 /*********************************************************************
158 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
162 /*********************************************************************
168 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
172 /*********************************************************************
178 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
182 /*********************************************************************
188 if (!finite(x) || !finite(y)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
192 /*********************************************************************
198 if (x < 0.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
199 if (x == 0.0) SET_THREAD_VAR(errno,MSVCRT_ERANGE);
203 /*********************************************************************
204 * _CIlog10 (MSVCRT.@)
206 double _CIlog10(void)
209 if (x < 0.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
210 if (x == 0.0) SET_THREAD_VAR(errno,MSVCRT_ERANGE);
214 /*********************************************************************
221 /* FIXME: If x < 0 and y is not integral, set EDOM */
223 if (!finite(z)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
227 /*********************************************************************
233 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
237 /*********************************************************************
243 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
247 /*********************************************************************
253 if (x < 0.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
257 /*********************************************************************
263 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
267 /*********************************************************************
273 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
277 #else /* defined(__GNUC__) && defined(__i386__) */
279 /* The above cannot be called on non x86 platforms, stub them for linking */
281 #define IX86_ONLY(func) double MSVCRT_##func(void) { return 0.0; }
300 #endif /* defined(__GNUC__) && defined(__i386__) */
302 /*********************************************************************
303 * _fpclass (MSVCRT.@)
305 int _fpclass(double num)
307 #if defined(HAVE_FPCLASS) || defined(fpclass)
308 switch (fpclass( num ))
310 case FP_SNAN: return _FPCLASS_SNAN;
311 case FP_QNAN: return _FPCLASS_QNAN;
312 case FP_NINF: return _FPCLASS_NINF;
313 case FP_PINF: return _FPCLASS_PINF;
314 case FP_NDENORM: return _FPCLASS_ND;
315 case FP_PDENORM: return _FPCLASS_PD;
316 case FP_NZERO: return _FPCLASS_NZ;
317 case FP_PZERO: return _FPCLASS_PZ;
318 case FP_NNORM: return _FPCLASS_NN;
321 #elif defined (fpclassify)
322 switch (fpclassify( num ))
324 case FP_NAN: return _FPCLASS_QNAN;
325 case FP_INFINITE: return signbit(num) ? _FPCLASS_NINF : _FPCLASS_PINF;
326 case FP_SUBNORMAL: return signbit(num) ?_FPCLASS_ND : _FPCLASS_PD;
327 case FP_ZERO: return signbit(num) ? _FPCLASS_NZ : _FPCLASS_PZ;
329 return signbit(num) ? _FPCLASS_NN : _FPCLASS_PN;
332 return _FPCLASS_QNAN;
333 return num == 0.0 ? _FPCLASS_PZ : (num < 0 ? _FPCLASS_NN : _FPCLASS_PN);
337 /*********************************************************************
340 unsigned int _rotl(unsigned int num, int shift)
343 return (num << shift) | (num >> (32-shift));
346 /*********************************************************************
349 double _logb(double num)
351 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
355 /*********************************************************************
358 unsigned long _lrotl(unsigned long num, int shift)
361 return (num << shift) | (num >> (32-shift));
364 /*********************************************************************
367 unsigned long _lrotr(unsigned long num, int shift)
370 return (num >> shift) | (num << (32-shift));
373 /*********************************************************************
376 unsigned int _rotr(unsigned int num, int shift)
379 return (num >> shift) | (num << (32-shift));
382 /*********************************************************************
385 double _scalb(double num, long power)
387 /* Note - Can't forward directly as libc expects y as double */
388 double dblpower = (double)power;
389 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
390 return scalb(num, dblpower);
393 /*********************************************************************
394 * _matherr (MSVCRT.@)
396 int _matherr(MSVCRT_exception *e)
399 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
403 if (MSVCRT_default_matherr_func)
404 return MSVCRT_default_matherr_func(e);
405 ERR(":Unhandled math error!\n");
409 /*********************************************************************
410 * __setusermatherr (MSVCRT.@)
412 void MSVCRT___setusermatherr(MSVCRT_matherr_func func)
414 MSVCRT_default_matherr_func = func;
415 TRACE(":new matherr handler %p\n", func);
418 /**********************************************************************
419 * _statusfp (MSVCRT.@)
421 unsigned int _statusfp(void)
423 unsigned int retVal = 0;
424 #if defined(__GNUC__) && defined(__i386__)
427 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
428 if (fpword & 0x1) retVal |= _SW_INVALID;
429 if (fpword & 0x2) retVal |= _SW_DENORMAL;
430 if (fpword & 0x4) retVal |= _SW_ZERODIVIDE;
431 if (fpword & 0x8) retVal |= _SW_OVERFLOW;
432 if (fpword & 0x10) retVal |= _SW_UNDERFLOW;
433 if (fpword & 0x20) retVal |= _SW_INEXACT;
435 FIXME(":Not implemented!\n");
440 /*********************************************************************
441 * _clearfp (MSVCRT.@)
443 unsigned int _clearfp(void)
445 unsigned int retVal = _statusfp();
446 #if defined(__GNUC__) && defined(__i386__)
447 __asm__ __volatile__( "fnclex" );
449 FIXME(":Not Implemented\n");
454 /*********************************************************************
457 double MSVCRT_ldexp(double num, long exp)
459 double z = ldexp(num,exp);
462 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
463 else if (z == 0 && signbit(z))
464 z = 0.0; /* Convert -0 -> +0 */
468 /*********************************************************************
471 double _cabs(MSVCRT_complex num)
473 return sqrt(num.real * num.real + num.imaginary * num.imaginary);
476 /*********************************************************************
477 * _chgsign (MSVCRT.@)
479 double _chgsign(double num)
481 /* FIXME: +-infinity,Nan not tested */
485 /*********************************************************************
486 * _control87 (MSVCRT.@)
488 unsigned int _control87(unsigned int newval, unsigned int mask)
490 #if defined(__GNUC__) && defined(__i386__)
491 unsigned int fpword = 0;
492 unsigned int flags = 0;
494 TRACE("(%08x, %08x): Called\n", newval, mask);
496 /* Get fp control word */
497 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) : );
499 TRACE("Control word before : %08x\n", fpword);
501 /* Convert into mask constants */
502 if (fpword & 0x1) flags |= _EM_INVALID;
503 if (fpword & 0x2) flags |= _EM_DENORMAL;
504 if (fpword & 0x4) flags |= _EM_ZERODIVIDE;
505 if (fpword & 0x8) flags |= _EM_OVERFLOW;
506 if (fpword & 0x10) flags |= _EM_UNDERFLOW;
507 if (fpword & 0x20) flags |= _EM_INEXACT;
508 switch(fpword & 0xC00) {
509 case 0xC00: flags |= _RC_UP|_RC_DOWN; break;
510 case 0x800: flags |= _RC_UP; break;
511 case 0x400: flags |= _RC_DOWN; break;
513 switch(fpword & 0x300) {
514 case 0x0: flags |= _PC_24; break;
515 case 0x200: flags |= _PC_53; break;
516 case 0x300: flags |= _PC_64; break;
518 if (fpword & 0x1000) flags |= _IC_AFFINE;
520 /* Mask with parameters */
521 flags = (flags & ~mask) | (newval & mask);
523 /* Convert (masked) value back to fp word */
525 if (flags & _EM_INVALID) fpword |= 0x1;
526 if (flags & _EM_DENORMAL) fpword |= 0x2;
527 if (flags & _EM_ZERODIVIDE) fpword |= 0x4;
528 if (flags & _EM_OVERFLOW) fpword |= 0x8;
529 if (flags & _EM_UNDERFLOW) fpword |= 0x10;
530 if (flags & _EM_INEXACT) fpword |= 0x20;
531 switch(flags & (_RC_UP | _RC_DOWN)) {
532 case _RC_UP|_RC_DOWN: fpword |= 0xC00; break;
533 case _RC_UP: fpword |= 0x800; break;
534 case _RC_DOWN: fpword |= 0x400; break;
536 switch (flags & (_PC_24 | _PC_53)) {
537 case _PC_64: fpword |= 0x300; break;
538 case _PC_53: fpword |= 0x200; break;
539 case _PC_24: fpword |= 0x0; break;
541 if (flags & _IC_AFFINE) fpword |= 0x1000;
543 TRACE("Control word after : %08x\n", fpword);
545 /* Put fp control word */
546 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
550 FIXME(":Not Implemented!\n");
555 /*********************************************************************
556 * _controlfp (MSVCRT.@)
558 unsigned int _controlfp(unsigned int newval, unsigned int mask)
560 return _control87( newval, mask & ~_EM_DENORMAL );
563 /*********************************************************************
564 * _copysign (MSVCRT.@)
566 double _copysign(double num, double sign)
568 /* FIXME: Behaviour for Nan/Inf? */
570 return num < 0.0 ? num : -num;
571 return num < 0.0 ? -num : num;
574 /*********************************************************************
577 int _finite(double num)
579 return (finite(num)?1:0); /* See comment for _isnan() */
582 /*********************************************************************
583 * _fpreset (MSVCRT.@)
587 #if defined(__GNUC__) && defined(__i386__)
588 __asm__ __volatile__( "fninit" );
590 FIXME(":Not Implemented!\n");
594 /*********************************************************************
597 INT _isnan(double num)
599 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
600 * Do the same, as the result may be used in calculations
602 return isnan(num) ? 1 : 0;
605 /*********************************************************************
608 double _y0(double num)
611 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
613 if (_fpclass(retval) == _FPCLASS_NINF)
615 SET_THREAD_VAR(errno,MSVCRT_EDOM);
621 /*********************************************************************
624 double _y1(double num)
627 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
629 if (_fpclass(retval) == _FPCLASS_NINF)
631 SET_THREAD_VAR(errno,MSVCRT_EDOM);
637 /*********************************************************************
640 double _yn(int order, double num)
643 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
644 retval = yn(order,num);
645 if (_fpclass(retval) == _FPCLASS_NINF)
647 SET_THREAD_VAR(errno,MSVCRT_EDOM);
653 /*********************************************************************
654 * _nextafter (MSVCRT.@)
656 double _nextafter(double num, double next)
659 if (!finite(num) || !finite(next)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
660 retval = nextafter(num,next);
664 #include <stdlib.h> /* div_t, ldiv_t */
666 /*********************************************************************
669 * [i386] Windows binary compatible - returns the struct in eax/edx.
672 LONGLONG MSVCRT_div(int num, int denom)
675 div_t dt = div(num,denom);
676 retval = ((LONGLONG)dt.rem << 32) | dt.quot;
680 /*********************************************************************
683 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
685 MSVCRT_div_t MSVCRT_div(int num, int denom)
687 return div(num,denom);
689 #endif /* ifdef __i386__ */
692 /*********************************************************************
695 * [i386] Windows binary compatible - returns the struct in eax/edx.
698 ULONGLONG MSVCRT_ldiv(long num, long denom)
701 ldiv_t ldt = ldiv(num,denom);
702 retval = ((ULONGLONG)ldt.rem << 32) | (ULONG)ldt.quot;
706 /*********************************************************************
709 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
711 MSVCRT_ldiv_t MSVCRT_ldiv(long num, long denom)
713 return ldiv(num,denom);
715 #endif /* ifdef __i386__ */
717 /***********************************************************************
718 * _adj_fdiv_m16i (MSVCRT.@)
720 * This function is likely to have the wrong number of arguments.
723 * I _think_ this function is intended to work around the Pentium
726 void _adj_fdiv_m16i(void)
731 /***********************************************************************
732 * _adj_fdiv_m32 (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_m32(void)
745 /***********************************************************************
746 * _adj_fdiv_m32i (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_m32i(void)
759 /***********************************************************************
760 * _adj_fdiv_m64 (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_m64(void)
773 /***********************************************************************
774 * _adj_fdiv_r (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_r(void)
787 /***********************************************************************
788 * _adj_fdivr_m16i (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_fdivr_m16i(void)
801 /***********************************************************************
802 * _adj_fdivr_m32 (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_m32(void)
815 /***********************************************************************
816 * _adj_fdivr_m32i (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_m32i(void)
829 /***********************************************************************
830 * _adj_fdivr_m64 (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_m64(void)
843 /***********************************************************************
844 * _adj_fpatan (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_fpatan(void)
857 /***********************************************************************
858 * _adj_fprem (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_fprem(void)
871 /***********************************************************************
872 * _adj_fprem1 (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_fprem1(void)
885 /***********************************************************************
886 * _adj_fptan (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_fptan(void)
899 /***********************************************************************
900 * _adjust_fdiv (MSVCRT.@)
902 * I _think_ this function should be a variable indicating whether
903 * Pentium fdiv bug safe code should be used.
905 void _adjust_fdiv(void)
910 /***********************************************************************
911 * _safe_fdiv (MSVCRT.@)
913 * This function is likely to have the wrong number of arguments.
916 * I _think_ this function is intended to work around the Pentium
919 void _safe_fdiv(void)
924 /***********************************************************************
925 * _safe_fdivr (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_fdivr(void)
938 /***********************************************************************
939 * _safe_fprem (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_fprem(void)
952 /***********************************************************************
953 * _safe_fprem1 (MSVCRT.@)
956 * This function is likely to have the wrong number of arguments.
959 * I _think_ this function is intended to work around the Pentium
962 void _safe_fprem1(void)