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 DEFAULT_DEBUG_CHANNEL(msvcrt);
22 #ifndef finite /* Could be a macro */
24 #define finite(x) isfinite(x)
26 #define finite(x) (!isnan(x)) /* At least catch some cases */
35 /* fpclass constants */
36 #define _FPCLASS_SNAN 1
37 #define _FPCLASS_QNAN 2
38 #define _FPCLASS_NINF 4
40 #define _FPCLASS_ND 16
41 #define _FPCLASS_NZ 32
42 #define _FPCLASS_PZ 64
43 #define _FPCLASS_PD 128
44 #define _FPCLASS_PN 256
45 #define _FPCLASS_PINF 512
47 /* _statusfp bit flags */
48 #define _SW_INEXACT 0x1
49 #define _SW_UNDERFLOW 0x2
50 #define _SW_OVERFLOW 0x4
51 #define _SW_ZERODIVIDE 0x8
52 #define _SW_INVALID 0x10
53 #define _SW_DENORMAL 0x80000
55 /* _controlfp masks and bitflags - x86 only so far*/
57 #define _MCW_EM 0x8001f
58 #define _EM_INEXACT 0x1
59 #define _EM_UNDERFLOW 0x2
60 #define _EM_OVERFLOW 0x4
61 #define _EM_ZERODIVIDE 0x8
62 #define _EM_INVALID 0x10
66 #define _RC_DOWN 0x100
68 #define _RC_CHOP 0x300
70 #define _MCW_PC 0x30000
72 #define _PC_53 0x10000
73 #define _PC_24 0x20000
75 #define _MCW_IC 0x40000
76 #define _IC_AFFINE 0x40000
77 #define _IC_PROJECTIVE 0x0
79 #define _EM_DENORMAL 0x80000
82 typedef struct __MSVCRT_complex
88 typedef struct __MSVCRT_exception
98 typedef int (*MSVCRT_matherr_func)(MSVCRT_exception *);
100 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
102 #if defined(__GNUC__) && defined(__i386__)
104 #define FPU_DOUBLE(var) double var; \
105 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
106 #define FPU_DOUBLES(var1,var2) double var1,var2; \
107 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
108 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
110 /*********************************************************************
116 if (x < -1.0 || x > 1.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
120 /*********************************************************************
126 if (x < -1.0 || x > 1.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
130 /*********************************************************************
136 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
140 /*********************************************************************
141 * _CIatan2 (MSVCRT.@)
143 double _CIatan2(void)
146 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
150 /*********************************************************************
156 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
160 /*********************************************************************
166 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
170 /*********************************************************************
176 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
180 /*********************************************************************
186 if (!finite(x) || !finite(y)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
190 /*********************************************************************
196 if (x < 0.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
197 if (x == 0.0) SET_THREAD_VAR(errno,MSVCRT_ERANGE);
201 /*********************************************************************
202 * _CIlog10 (MSVCRT.@)
204 double _CIlog10(void)
207 if (x < 0.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
208 if (x == 0.0) SET_THREAD_VAR(errno,MSVCRT_ERANGE);
212 /*********************************************************************
219 /* FIXME: If x < 0 and y is not integral, set EDOM */
221 if (!finite(z)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
225 /*********************************************************************
231 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
235 /*********************************************************************
241 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
245 /*********************************************************************
251 if (x < 0.0 || !finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
255 /*********************************************************************
261 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
265 /*********************************************************************
271 if (!finite(x)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
275 #else /* defined(__GNUC__) && defined(__i386__) */
277 /* The above cannot be called on non x86 platforms, stub them for linking */
279 #define IX86_ONLY(func) double MSVCRT_##func(void) { return 0.0; }
298 #endif /* defined(__GNUC__) && defined(__i386__) */
300 /*********************************************************************
301 * _fpclass (MSVCRT.@)
303 int _fpclass(double num)
305 #if defined(HAVE_FPCLASS) || defined(fpclass)
306 switch (fpclass( num ))
308 case FP_SNAN: return _FPCLASS_SNAN;
309 case FP_QNAN: return _FPCLASS_QNAN;
310 case FP_NINF: return _FPCLASS_NINF;
311 case FP_PINF: return _FPCLASS_PINF;
312 case FP_NDENORM: return _FPCLASS_ND;
313 case FP_PDENORM: return _FPCLASS_PD;
314 case FP_NZERO: return _FPCLASS_NZ;
315 case FP_PZERO: return _FPCLASS_PZ;
316 case FP_NNORM: return _FPCLASS_NN;
319 #elif defined (fpclassify)
320 switch (fpclassify( num ))
322 case FP_NAN: return _FPCLASS_QNAN;
323 case FP_INFINITE: return signbit(num) ? _FPCLASS_NINF : _FPCLASS_PINF;
324 case FP_SUBNORMAL: return signbit(num) ?_FPCLASS_ND : _FPCLASS_PD;
325 case FP_ZERO: return signbit(num) ? _FPCLASS_NZ : _FPCLASS_PZ;
327 return signbit(num) ? _FPCLASS_NN : _FPCLASS_PN;
330 return _FPCLASS_QNAN;
331 return num == 0.0 ? _FPCLASS_PZ : (num < 0 ? _FPCLASS_NN : _FPCLASS_PN);
335 /*********************************************************************
338 unsigned int _rotl(unsigned int num, int shift)
341 return (num << shift) | (num >> (32-shift));
344 /*********************************************************************
347 double _logb(double num)
349 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
353 /*********************************************************************
356 unsigned long _lrotl(unsigned long num, int shift)
359 return (num << shift) | (num >> (32-shift));
362 /*********************************************************************
365 unsigned long _lrotr(unsigned long num, int shift)
368 return (num >> shift) | (num << (32-shift));
371 /*********************************************************************
374 unsigned int _rotr(unsigned int num, int shift)
377 return (num >> shift) | (num << (32-shift));
380 /*********************************************************************
383 double _scalb(double num, long power)
385 /* Note - Can't forward directly as libc expects y as double */
386 double dblpower = (double)power;
387 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
388 return scalb(num, dblpower);
391 /*********************************************************************
392 * _matherr (MSVCRT.@)
394 int _matherr(MSVCRT_exception *e)
397 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
401 if (MSVCRT_default_matherr_func)
402 return MSVCRT_default_matherr_func(e);
403 ERR(":Unhandled math error!\n");
407 /*********************************************************************
408 * __setusermatherr (MSVCRT.@)
410 void MSVCRT___setusermatherr(MSVCRT_matherr_func func)
412 MSVCRT_default_matherr_func = func;
413 TRACE(":new matherr handler %p\n", func);
416 /**********************************************************************
417 * _statusfp (MSVCRT.@)
419 unsigned int _statusfp(void)
421 unsigned int retVal = 0;
422 #if defined(__GNUC__) && defined(__i386__)
425 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
426 if (fpword & 0x1) retVal |= _SW_INVALID;
427 if (fpword & 0x2) retVal |= _SW_DENORMAL;
428 if (fpword & 0x4) retVal |= _SW_ZERODIVIDE;
429 if (fpword & 0x8) retVal |= _SW_OVERFLOW;
430 if (fpword & 0x10) retVal |= _SW_UNDERFLOW;
431 if (fpword & 0x20) retVal |= _SW_INEXACT;
433 FIXME(":Not implemented!\n");
438 /*********************************************************************
439 * _clearfp (MSVCRT.@)
441 unsigned int _clearfp(void)
443 unsigned int retVal = _statusfp();
444 #if defined(__GNUC__) && defined(__i386__)
445 __asm__ __volatile__( "fnclex" );
447 FIXME(":Not Implemented\n");
452 /*********************************************************************
455 double MSVCRT_ldexp(double num, long exp)
457 double z = ldexp(num,exp);
460 SET_THREAD_VAR(errno,MSVCRT_ERANGE);
461 else if (z == 0 && signbit(z))
462 z = 0.0; /* Convert -0 -> +0 */
466 /*********************************************************************
469 double _cabs(MSVCRT_complex num)
471 return sqrt(num.real * num.real + num.imaginary * num.imaginary);
474 /*********************************************************************
475 * _chgsign (MSVCRT.@)
477 double _chgsign(double num)
479 /* FIXME: +-infinity,Nan not tested */
483 /*********************************************************************
484 * _control87 (MSVCRT.@)
486 unsigned int _control87(unsigned int newval, unsigned int mask)
488 #if defined(__GNUC__) && defined(__i386__)
489 unsigned int fpword, flags = 0;
491 /* Get fp control word */
492 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
494 /* Convert into mask constants */
495 if (fpword & 0x1) flags |= _EM_INVALID;
496 if (fpword & 0x2) flags |= _EM_DENORMAL;
497 if (fpword & 0x4) flags |= _EM_ZERODIVIDE;
498 if (fpword & 0x8) flags |= _EM_OVERFLOW;
499 if (fpword & 0x10) flags |= _EM_UNDERFLOW;
500 if (fpword & 0x20) flags |= _EM_INEXACT;
501 switch(fpword & 0xC00) {
502 case 0xC00: flags |= _RC_UP|_RC_DOWN; break;
503 case 0x800: flags |= _RC_UP; break;
504 case 0x400: flags |= _RC_DOWN; break;
506 switch(fpword & 0x300) {
507 case 0x0: flags |= _PC_24; break;
508 case 0x200: flags |= _PC_53; break;
509 case 0x300: flags |= _PC_64; break;
511 if (fpword & 0x1000) flags |= _IC_AFFINE;
513 /* Mask with parameters */
514 flags = (flags & ~mask) | (newval & mask);
516 /* Convert (masked) value back to fp word */
518 if (flags & _EM_INVALID) fpword |= 0x1;
519 if (flags & _EM_DENORMAL) fpword |= 0x2;
520 if (flags & _EM_ZERODIVIDE) fpword |= 0x4;
521 if (flags & _EM_OVERFLOW) fpword |= 0x8;
522 if (flags & _EM_UNDERFLOW) fpword |= 0x10;
523 if (flags & _EM_INEXACT) fpword |= 0x20;
524 switch(flags & (_RC_UP | _RC_DOWN)) {
525 case _RC_UP|_RC_DOWN: fpword |= 0xC00; break;
526 case _RC_UP: fpword |= 0x800; break;
527 case _RC_DOWN: fpword |= 0x400; break;
529 switch (flags & (_PC_24 | _PC_53)) {
530 case _PC_64: fpword |= 0x300; break;
531 case _PC_53: fpword |= 0x200; break;
532 case _PC_24: fpword |= 0x0; break;
534 if (!(flags & _IC_AFFINE)) fpword |= 0x1000;
536 /* Put fp control word */
537 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
540 return _controlfp( newval, mask );
544 /*********************************************************************
545 * _controlfp (MSVCRT.@)
547 unsigned int _controlfp(unsigned int newval, unsigned int mask)
549 #if defined(__GNUC__) && defined(__i386__)
550 return _control87( newval, mask );
552 FIXME(":Not Implemented!\n");
557 /*********************************************************************
558 * _copysign (MSVCRT.@)
560 double _copysign(double num, double sign)
562 /* FIXME: Behaviour for Nan/Inf? */
564 return num < 0.0 ? num : -num;
565 return num < 0.0 ? -num : num;
568 /*********************************************************************
571 int _finite(double num)
573 return (finite(num)?1:0); /* See comment for _isnan() */
576 /*********************************************************************
577 * _fpreset (MSVCRT.@)
581 #if defined(__GNUC__) && defined(__i386__)
582 __asm__ __volatile__( "fninit" );
584 FIXME(":Not Implemented!\n");
588 /*********************************************************************
591 INT _isnan(double num)
593 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
594 * Do the same, as the result may be used in calculations
596 return isnan(num) ? 1 : 0;
599 /*********************************************************************
602 double _y0(double num)
605 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
607 if (_fpclass(retval) == _FPCLASS_NINF)
609 SET_THREAD_VAR(errno,MSVCRT_EDOM);
615 /*********************************************************************
618 double _y1(double num)
621 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
623 if (_fpclass(retval) == _FPCLASS_NINF)
625 SET_THREAD_VAR(errno,MSVCRT_EDOM);
631 /*********************************************************************
634 double _yn(int order, double num)
637 if (!finite(num)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
638 retval = yn(order,num);
639 if (_fpclass(retval) == _FPCLASS_NINF)
641 SET_THREAD_VAR(errno,MSVCRT_EDOM);
647 /*********************************************************************
648 * _nextafter (MSVCRT.@)
650 double _nextafter(double num, double next)
653 if (!finite(num) || !finite(next)) SET_THREAD_VAR(errno,MSVCRT_EDOM);
654 retval = nextafter(num,next);
658 #include <stdlib.h> /* div_t, ldiv_t */
660 /*********************************************************************
663 * [i386] Windows binary compatible - returns the struct in eax/edx.
666 LONGLONG MSVCRT_div(int num, int denom)
669 div_t dt = div(num,denom);
670 retval = ((LONGLONG)dt.rem << 32) | dt.quot;
674 /*********************************************************************
677 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
679 MSVCRT_div_t MSVCRT_div(int num, int denom)
681 return div(num,denom);
683 #endif /* ifdef __i386__ */
686 /*********************************************************************
689 * [i386] Windows binary compatible - returns the struct in eax/edx.
692 ULONGLONG MSVCRT_ldiv(long num, long denom)
695 ldiv_t ldt = ldiv(num,denom);
696 retval = ((ULONGLONG)ldt.rem << 32) | (ULONG)ldt.quot;
700 /*********************************************************************
703 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
705 MSVCRT_ldiv_t MSVCRT_ldiv(long num, long denom)
707 return ldiv(num,denom);
709 #endif /* ifdef __i386__ */
711 /***********************************************************************
712 * _adj_fdiv_m16i (MSVCRT.@)
714 * This function is likely to have the wrong number of arguments.
717 * I _think_ this function is intended to work around the Pentium
720 void _adj_fdiv_m16i(void)
725 /***********************************************************************
726 * _adj_fdiv_m32 (MSVCRT.@)
728 * This function is likely to have the wrong number of arguments.
731 * I _think_ this function is intended to work around the Pentium
734 void _adj_fdiv_m32(void)
739 /***********************************************************************
740 * _adj_fdiv_m32i (MSVCRT.@)
742 * This function is likely to have the wrong number of arguments.
745 * I _think_ this function is intended to work around the Pentium
748 void _adj_fdiv_m32i(void)
753 /***********************************************************************
754 * _adj_fdiv_m64 (MSVCRT.@)
756 * This function is likely to have the wrong number of arguments.
759 * I _think_ this function is intended to work around the Pentium
762 void _adj_fdiv_m64(void)
767 /***********************************************************************
768 * _adj_fdiv_r (MSVCRT.@)
770 * This function is likely to have the wrong number of arguments.
773 * I _think_ this function is intended to work around the Pentium
776 void _adj_fdiv_r(void)
781 /***********************************************************************
782 * _adj_fdivr_m16i (MSVCRT.@)
784 * This function is likely to have the wrong number of arguments.
787 * I _think_ this function is intended to work around the Pentium
790 void _adj_fdivr_m16i(void)
795 /***********************************************************************
796 * _adj_fdivr_m32 (MSVCRT.@)
798 * This function is likely to have the wrong number of arguments.
801 * I _think_ this function is intended to work around the Pentium
804 void _adj_fdivr_m32(void)
809 /***********************************************************************
810 * _adj_fdivr_m32i (MSVCRT.@)
812 * This function is likely to have the wrong number of arguments.
815 * I _think_ this function is intended to work around the Pentium
818 void _adj_fdivr_m32i(void)
823 /***********************************************************************
824 * _adj_fdivr_m64 (MSVCRT.@)
826 * This function is likely to have the wrong number of arguments.
829 * I _think_ this function is intended to work around the Pentium
832 void _adj_fdivr_m64(void)
837 /***********************************************************************
838 * _adj_fpatan (MSVCRT.@)
840 * This function is likely to have the wrong number of arguments.
843 * I _think_ this function is intended to work around the Pentium
846 void _adj_fpatan(void)
851 /***********************************************************************
852 * _adj_fprem (MSVCRT.@)
854 * This function is likely to have the wrong number of arguments.
857 * I _think_ this function is intended to work around the Pentium
860 void _adj_fprem(void)
865 /***********************************************************************
866 * _adj_fprem1 (MSVCRT.@)
868 * This function is likely to have the wrong number of arguments.
871 * I _think_ this function is intended to work around the Pentium
874 void _adj_fprem1(void)
879 /***********************************************************************
880 * _adj_fptan (MSVCRT.@)
882 * This function is likely to have the wrong number of arguments.
885 * I _think_ this function is intended to work around the Pentium
888 void _adj_fptan(void)
893 /***********************************************************************
894 * _adjust_fdiv (MSVCRT.@)
896 * I _think_ this function should be a variable indicating whether
897 * Pentium fdiv bug safe code should be used.
899 void _adjust_fdiv(void)
904 /***********************************************************************
905 * _safe_fdiv (MSVCRT.@)
907 * This function is likely to have the wrong number of arguments.
910 * I _think_ this function is intended to work around the Pentium
913 void _safe_fdiv(void)
918 /***********************************************************************
919 * _safe_fdivr (MSVCRT.@)
921 * This function is likely to have the wrong number of arguments.
924 * I _think_ this function is intended to work around the Pentium
927 void _safe_fdivr(void)
932 /***********************************************************************
933 * _safe_fprem (MSVCRT.@)
935 * This function is likely to have the wrong number of arguments.
938 * I _think_ this function is intended to work around the Pentium
941 void _safe_fprem(void)
946 /***********************************************************************
947 * _safe_fprem1 (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_fprem1(void)