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"
25 #define __USE_ISOC9X 1
26 #define __USE_ISOC99 1
32 #include "msvcrt/stdlib.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 #ifndef finite /* Could be a macro */
41 #define finite(x) isfinite(x)
43 #define finite(x) (!isnan(x)) /* At least catch some cases */
52 /* fpclass constants */
53 #define _FPCLASS_SNAN 1
54 #define _FPCLASS_QNAN 2
55 #define _FPCLASS_NINF 4
57 #define _FPCLASS_ND 16
58 #define _FPCLASS_NZ 32
59 #define _FPCLASS_PZ 64
60 #define _FPCLASS_PD 128
61 #define _FPCLASS_PN 256
62 #define _FPCLASS_PINF 512
64 /* _statusfp bit flags */
65 #define _SW_INEXACT 0x1
66 #define _SW_UNDERFLOW 0x2
67 #define _SW_OVERFLOW 0x4
68 #define _SW_ZERODIVIDE 0x8
69 #define _SW_INVALID 0x10
70 #define _SW_DENORMAL 0x80000
72 /* _controlfp masks and bitflags - x86 only so far*/
74 #define _MCW_EM 0x8001f
75 #define _EM_INEXACT 0x1
76 #define _EM_UNDERFLOW 0x2
77 #define _EM_OVERFLOW 0x4
78 #define _EM_ZERODIVIDE 0x8
79 #define _EM_INVALID 0x10
83 #define _RC_DOWN 0x100
85 #define _RC_CHOP 0x300
87 #define _MCW_PC 0x30000
89 #define _PC_53 0x10000
90 #define _PC_24 0x20000
92 #define _MCW_IC 0x40000
93 #define _IC_AFFINE 0x40000
94 #define _IC_PROJECTIVE 0x0
96 #define _EM_DENORMAL 0x80000
99 typedef struct __MSVCRT_complex
105 typedef struct __MSVCRT_exception
115 typedef int (*MSVCRT_matherr_func)(MSVCRT_exception *);
117 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
119 #if defined(__GNUC__) && defined(__i386__)
121 #define FPU_DOUBLE(var) double var; \
122 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
123 #define FPU_DOUBLES(var1,var2) double var1,var2; \
124 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
125 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
127 /*********************************************************************
133 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
137 /*********************************************************************
143 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
147 /*********************************************************************
153 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
157 /*********************************************************************
158 * _CIatan2 (MSVCRT.@)
160 double _CIatan2(void)
163 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
167 /*********************************************************************
173 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
177 /*********************************************************************
183 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
187 /*********************************************************************
193 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
197 /*********************************************************************
203 if (!finite(x) || !finite(y)) *MSVCRT__errno() = MSVCRT_EDOM;
207 /*********************************************************************
213 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
214 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
218 /*********************************************************************
219 * _CIlog10 (MSVCRT.@)
221 double _CIlog10(void)
224 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
225 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
229 /*********************************************************************
236 /* FIXME: If x < 0 and y is not integral, set EDOM */
238 if (!finite(z)) *MSVCRT__errno() = MSVCRT_EDOM;
242 /*********************************************************************
248 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
252 /*********************************************************************
258 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
262 /*********************************************************************
268 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
272 /*********************************************************************
278 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
282 /*********************************************************************
288 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
292 #else /* defined(__GNUC__) && defined(__i386__) */
294 /* The above cannot be called on non x86 platforms, stub them for linking */
296 #define IX86_ONLY(func) double func(void) { return 0.0; }
315 #endif /* defined(__GNUC__) && defined(__i386__) */
317 /*********************************************************************
318 * _fpclass (MSVCRT.@)
320 int _fpclass(double num)
322 #if defined(HAVE_FPCLASS) || defined(fpclass)
323 switch (fpclass( num ))
325 case FP_SNAN: return _FPCLASS_SNAN;
326 case FP_QNAN: return _FPCLASS_QNAN;
327 case FP_NINF: return _FPCLASS_NINF;
328 case FP_PINF: return _FPCLASS_PINF;
329 case FP_NDENORM: return _FPCLASS_ND;
330 case FP_PDENORM: return _FPCLASS_PD;
331 case FP_NZERO: return _FPCLASS_NZ;
332 case FP_PZERO: return _FPCLASS_PZ;
333 case FP_NNORM: return _FPCLASS_NN;
334 case FP_PNORM: return _FPCLASS_PN;
337 #elif defined (fpclassify)
338 switch (fpclassify( num ))
340 case FP_NAN: return _FPCLASS_QNAN;
341 case FP_INFINITE: return signbit(num) ? _FPCLASS_NINF : _FPCLASS_PINF;
342 case FP_SUBNORMAL: return signbit(num) ?_FPCLASS_ND : _FPCLASS_PD;
343 case FP_ZERO: return signbit(num) ? _FPCLASS_NZ : _FPCLASS_PZ;
345 return signbit(num) ? _FPCLASS_NN : _FPCLASS_PN;
348 return _FPCLASS_QNAN;
349 return num == 0.0 ? _FPCLASS_PZ : (num < 0 ? _FPCLASS_NN : _FPCLASS_PN);
353 /*********************************************************************
356 unsigned int _rotl(unsigned int num, int shift)
359 return (num << shift) | (num >> (32-shift));
362 /*********************************************************************
365 double _logb(double num)
367 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
371 /*********************************************************************
374 unsigned long _lrotl(unsigned long num, int shift)
377 return (num << shift) | (num >> (32-shift));
380 /*********************************************************************
383 unsigned long _lrotr(unsigned long num, int shift)
386 return (num >> shift) | (num << (32-shift));
389 /*********************************************************************
392 unsigned int _rotr(unsigned int num, int shift)
395 return (num >> shift) | (num << (32-shift));
398 /*********************************************************************
401 double _scalb(double num, long power)
403 /* Note - Can't forward directly as libc expects y as double */
404 double dblpower = (double)power;
405 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
406 return scalb(num, dblpower);
409 /*********************************************************************
410 * _matherr (MSVCRT.@)
412 int _matherr(MSVCRT_exception *e)
415 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
419 if (MSVCRT_default_matherr_func)
420 return MSVCRT_default_matherr_func(e);
421 ERR(":Unhandled math error!\n");
425 /*********************************************************************
426 * __setusermatherr (MSVCRT.@)
428 void MSVCRT___setusermatherr(MSVCRT_matherr_func func)
430 MSVCRT_default_matherr_func = func;
431 TRACE(":new matherr handler %p\n", func);
434 /**********************************************************************
435 * _statusfp (MSVCRT.@)
437 unsigned int _statusfp(void)
439 unsigned int retVal = 0;
440 #if defined(__GNUC__) && defined(__i386__)
443 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
444 if (fpword & 0x1) retVal |= _SW_INVALID;
445 if (fpword & 0x2) retVal |= _SW_DENORMAL;
446 if (fpword & 0x4) retVal |= _SW_ZERODIVIDE;
447 if (fpword & 0x8) retVal |= _SW_OVERFLOW;
448 if (fpword & 0x10) retVal |= _SW_UNDERFLOW;
449 if (fpword & 0x20) retVal |= _SW_INEXACT;
451 FIXME(":Not implemented!\n");
456 /*********************************************************************
457 * _clearfp (MSVCRT.@)
459 unsigned int _clearfp(void)
461 unsigned int retVal = _statusfp();
462 #if defined(__GNUC__) && defined(__i386__)
463 __asm__ __volatile__( "fnclex" );
465 FIXME(":Not Implemented\n");
470 /*********************************************************************
471 * __fpecode (MSVCRT.@)
475 return &msvcrt_get_thread_data()->fpecode;
478 /*********************************************************************
481 double MSVCRT_ldexp(double num, long exp)
483 double z = ldexp(num,exp);
486 *MSVCRT__errno() = MSVCRT_ERANGE;
487 else if (z == 0 && signbit(z))
488 z = 0.0; /* Convert -0 -> +0 */
492 /*********************************************************************
495 double _cabs(MSVCRT_complex num)
497 return sqrt(num.real * num.real + num.imaginary * num.imaginary);
500 /*********************************************************************
501 * _chgsign (MSVCRT.@)
503 double _chgsign(double num)
505 /* FIXME: +-infinity,Nan not tested */
509 /*********************************************************************
510 * _control87 (MSVCRT.@)
512 unsigned int _control87(unsigned int newval, unsigned int mask)
514 #if defined(__GNUC__) && defined(__i386__)
515 unsigned int fpword = 0;
516 unsigned int flags = 0;
518 TRACE("(%08x, %08x): Called\n", newval, mask);
520 /* Get fp control word */
521 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) : );
523 TRACE("Control word before : %08x\n", fpword);
525 /* Convert into mask constants */
526 if (fpword & 0x1) flags |= _EM_INVALID;
527 if (fpword & 0x2) flags |= _EM_DENORMAL;
528 if (fpword & 0x4) flags |= _EM_ZERODIVIDE;
529 if (fpword & 0x8) flags |= _EM_OVERFLOW;
530 if (fpword & 0x10) flags |= _EM_UNDERFLOW;
531 if (fpword & 0x20) flags |= _EM_INEXACT;
532 switch(fpword & 0xC00) {
533 case 0xC00: flags |= _RC_UP|_RC_DOWN; break;
534 case 0x800: flags |= _RC_UP; break;
535 case 0x400: flags |= _RC_DOWN; break;
537 switch(fpword & 0x300) {
538 case 0x0: flags |= _PC_24; break;
539 case 0x200: flags |= _PC_53; break;
540 case 0x300: flags |= _PC_64; break;
542 if (fpword & 0x1000) flags |= _IC_AFFINE;
544 /* Mask with parameters */
545 flags = (flags & ~mask) | (newval & mask);
547 /* Convert (masked) value back to fp word */
549 if (flags & _EM_INVALID) fpword |= 0x1;
550 if (flags & _EM_DENORMAL) fpword |= 0x2;
551 if (flags & _EM_ZERODIVIDE) fpword |= 0x4;
552 if (flags & _EM_OVERFLOW) fpword |= 0x8;
553 if (flags & _EM_UNDERFLOW) fpword |= 0x10;
554 if (flags & _EM_INEXACT) fpword |= 0x20;
555 switch(flags & (_RC_UP | _RC_DOWN)) {
556 case _RC_UP|_RC_DOWN: fpword |= 0xC00; break;
557 case _RC_UP: fpword |= 0x800; break;
558 case _RC_DOWN: fpword |= 0x400; break;
560 switch (flags & (_PC_24 | _PC_53)) {
561 case _PC_64: fpword |= 0x300; break;
562 case _PC_53: fpword |= 0x200; break;
563 case _PC_24: fpword |= 0x0; break;
565 if (flags & _IC_AFFINE) fpword |= 0x1000;
567 TRACE("Control word after : %08x\n", fpword);
569 /* Put fp control word */
570 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
574 FIXME(":Not Implemented!\n");
579 /*********************************************************************
580 * _controlfp (MSVCRT.@)
582 unsigned int _controlfp(unsigned int newval, unsigned int mask)
585 return _control87( newval, mask & ~_EM_DENORMAL );
587 FIXME(":Not Implemented!\n");
592 /*********************************************************************
593 * _copysign (MSVCRT.@)
595 double _copysign(double num, double sign)
597 /* FIXME: Behaviour for Nan/Inf? */
599 return num < 0.0 ? num : -num;
600 return num < 0.0 ? -num : num;
603 /*********************************************************************
606 int _finite(double num)
608 return (finite(num)?1:0); /* See comment for _isnan() */
611 /*********************************************************************
612 * _fpreset (MSVCRT.@)
616 #if defined(__GNUC__) && defined(__i386__)
617 __asm__ __volatile__( "fninit" );
619 FIXME(":Not Implemented!\n");
623 /*********************************************************************
626 INT _isnan(double num)
628 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
629 * Do the same, as the result may be used in calculations
631 return isnan(num) ? 1 : 0;
634 /*********************************************************************
637 double _y0(double num)
640 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
642 if (_fpclass(retval) == _FPCLASS_NINF)
644 *MSVCRT__errno() = MSVCRT_EDOM;
650 /*********************************************************************
653 double _y1(double num)
656 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
658 if (_fpclass(retval) == _FPCLASS_NINF)
660 *MSVCRT__errno() = MSVCRT_EDOM;
666 /*********************************************************************
669 double _yn(int order, double num)
672 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
673 retval = yn(order,num);
674 if (_fpclass(retval) == _FPCLASS_NINF)
676 *MSVCRT__errno() = MSVCRT_EDOM;
682 /*********************************************************************
683 * _nextafter (MSVCRT.@)
685 double _nextafter(double num, double next)
688 if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
689 retval = nextafter(num,next);
693 /*********************************************************************
696 char *_ecvt( double number, int ndigits, int *decpt, int *sign )
698 MSVCRT_thread_data *data = msvcrt_get_thread_data();
701 if (!data->efcvt_buffer)
702 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
704 snprintf(data->efcvt_buffer, 80, "%.*e", ndigits /* FIXME wrong */, number);
705 *sign = (number < 0);
706 dec = strchr(data->efcvt_buffer, '.');
707 *decpt = (dec) ? dec - data->efcvt_buffer : -1;
708 return data->efcvt_buffer;
711 /***********************************************************************
714 char *_fcvt( double number, int ndigits, int *decpt, int *sign )
716 MSVCRT_thread_data *data = msvcrt_get_thread_data();
719 if (!data->efcvt_buffer)
720 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
722 snprintf(data->efcvt_buffer, 80, "%.*e", ndigits, number);
723 *sign = (number < 0);
724 dec = strchr(data->efcvt_buffer, '.');
725 *decpt = (dec) ? dec - data->efcvt_buffer : -1;
726 return data->efcvt_buffer;
729 /***********************************************************************
732 * FIXME: uses both E and F.
734 char *_gcvt( double number, int ndigit, char *buff )
736 sprintf(buff, "%.*E", ndigit, number);
740 #include <stdlib.h> /* div_t, ldiv_t */
742 /*********************************************************************
745 * [i386] Windows binary compatible - returns the struct in eax/edx.
748 unsigned __int64 MSVCRT_div(int num, int denom)
750 div_t dt = div(num,denom);
751 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
754 /*********************************************************************
757 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
759 MSVCRT_div_t MSVCRT_div(int num, int denom)
761 div_t dt = div(num,denom);
769 #endif /* ifdef __i386__ */
772 /*********************************************************************
775 * [i386] Windows binary compatible - returns the struct in eax/edx.
778 unsigned __int64 MSVCRT_ldiv(long num, long denom)
780 ldiv_t ldt = ldiv(num,denom);
781 return ((unsigned __int64)ldt.rem << 32) | (unsigned long)ldt.quot;
784 /*********************************************************************
787 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
789 MSVCRT_ldiv_t MSVCRT_ldiv(long num, long denom)
791 ldiv_t result = ldiv(num,denom);
794 ret.quot = result.quot;
795 ret.rem = result.rem;
799 #endif /* ifdef __i386__ */
801 /***********************************************************************
802 * _adj_fdiv_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_fdiv_m16i(void)
815 /***********************************************************************
816 * _adj_fdiv_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_fdiv_m32(void)
829 /***********************************************************************
830 * _adj_fdiv_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_fdiv_m32i(void)
843 /***********************************************************************
844 * _adj_fdiv_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_fdiv_m64(void)
857 /***********************************************************************
858 * _adj_fdiv_r (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_fdiv_r(void)
871 /***********************************************************************
872 * _adj_fdivr_m16i (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_fdivr_m16i(void)
885 /***********************************************************************
886 * _adj_fdivr_m32 (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_fdivr_m32(void)
899 /***********************************************************************
900 * _adj_fdivr_m32i (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_fdivr_m32i(void)
913 /***********************************************************************
914 * _adj_fdivr_m64 (MSVCRT.@)
916 * This function is likely to have the wrong number of arguments.
919 * I _think_ this function is intended to work around the Pentium
922 void _adj_fdivr_m64(void)
927 /***********************************************************************
928 * _adj_fpatan (MSVCRT.@)
930 * This function is likely to have the wrong number of arguments.
933 * I _think_ this function is intended to work around the Pentium
936 void _adj_fpatan(void)
941 /***********************************************************************
942 * _adj_fprem (MSVCRT.@)
944 * This function is likely to have the wrong number of arguments.
947 * I _think_ this function is intended to work around the Pentium
950 void _adj_fprem(void)
955 /***********************************************************************
956 * _adj_fprem1 (MSVCRT.@)
958 * This function is likely to have the wrong number of arguments.
961 * I _think_ this function is intended to work around the Pentium
964 void _adj_fprem1(void)
969 /***********************************************************************
970 * _adj_fptan (MSVCRT.@)
972 * This function is likely to have the wrong number of arguments.
975 * I _think_ this function is intended to work around the Pentium
978 void _adj_fptan(void)
983 /***********************************************************************
984 * _adjust_fdiv (MSVCRT.@)
986 * I _think_ this function should be a variable indicating whether
987 * Pentium fdiv bug safe code should be used.
989 void _adjust_fdiv(void)
994 /***********************************************************************
995 * _safe_fdiv (MSVCRT.@)
997 * This function is likely to have the wrong number of arguments.
1000 * I _think_ this function is intended to work around the Pentium
1003 void _safe_fdiv(void)
1005 TRACE("(): stub\n");
1008 /***********************************************************************
1009 * _safe_fdivr (MSVCRT.@)
1011 * This function is likely to have the wrong number of arguments.
1014 * I _think_ this function is intended to work around the Pentium
1017 void _safe_fdivr(void)
1019 TRACE("(): stub\n");
1022 /***********************************************************************
1023 * _safe_fprem (MSVCRT.@)
1025 * This function is likely to have the wrong number of arguments.
1028 * I _think_ this function is intended to work around the Pentium
1031 void _safe_fprem(void)
1033 TRACE("(): stub\n");
1036 /***********************************************************************
1037 * _safe_fprem1 (MSVCRT.@)
1040 * This function is likely to have the wrong number of arguments.
1043 * I _think_ this function is intended to work around the Pentium
1046 void _safe_fprem1(void)
1048 TRACE("(): stub\n");