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
23 #define __USE_ISOC9X 1
24 #define __USE_ISOC99 1
31 #include "msvcrt/errno.h"
32 #include "msvcrt/stdlib.h"
33 #include "msvcrt/math.h"
34 #include "msvcrt/float.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 #ifndef finite /* Could be a macro */
43 #define finite(x) isfinite(x)
45 #define finite(x) (!isnan(x)) /* At least catch some cases */
54 typedef int (*MSVCRT_matherr_func)(struct MSVCRT__exception *);
56 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
58 #if defined(__GNUC__) && defined(__i386__)
60 #define FPU_DOUBLE(var) double var; \
61 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
62 #define FPU_DOUBLES(var1,var2) double var1,var2; \
63 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
64 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
66 /*********************************************************************
72 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
76 /*********************************************************************
82 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
86 /*********************************************************************
92 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
96 /*********************************************************************
102 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
106 /*********************************************************************
112 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
116 /*********************************************************************
122 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
126 /*********************************************************************
132 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
136 /*********************************************************************
142 if (!finite(x) || !finite(y)) *MSVCRT__errno() = MSVCRT_EDOM;
146 /*********************************************************************
152 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
153 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
157 /*********************************************************************
158 * _CIlog10 (MSVCRT.@)
160 double _CIlog10(void)
163 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
164 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
168 /*********************************************************************
175 /* FIXME: If x < 0 and y is not integral, set EDOM */
177 if (!finite(z)) *MSVCRT__errno() = MSVCRT_EDOM;
181 /*********************************************************************
187 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
191 /*********************************************************************
197 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
201 /*********************************************************************
207 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
211 /*********************************************************************
217 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
221 /*********************************************************************
227 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
231 #else /* defined(__GNUC__) && defined(__i386__) */
233 /* The above cannot be called on non x86 platforms, stub them for linking */
235 #define IX86_ONLY(func) double func(void) { return 0.0; }
254 #endif /* defined(__GNUC__) && defined(__i386__) */
256 /*********************************************************************
257 * _fpclass (MSVCRT.@)
259 int _fpclass(double num)
261 #if defined(HAVE_FPCLASS) || defined(fpclass)
262 switch (fpclass( num ))
265 case FP_SNAN: return _FPCLASS_SNAN;
268 case FP_QNAN: return _FPCLASS_QNAN;
271 case FP_NINF: return _FPCLASS_NINF;
274 case FP_PINF: return _FPCLASS_PINF;
277 case FP_NDENORM: return _FPCLASS_ND;
280 case FP_PDENORM: return _FPCLASS_PD;
283 case FP_NZERO: return _FPCLASS_NZ;
286 case FP_PZERO: return _FPCLASS_PZ;
289 case FP_NNORM: return _FPCLASS_NN;
292 case FP_PNORM: return _FPCLASS_PN;
296 #elif defined (fpclassify)
297 switch (fpclassify( num ))
299 case FP_NAN: return _FPCLASS_QNAN;
300 case FP_INFINITE: return signbit(num) ? _FPCLASS_NINF : _FPCLASS_PINF;
301 case FP_SUBNORMAL: return signbit(num) ?_FPCLASS_ND : _FPCLASS_PD;
302 case FP_ZERO: return signbit(num) ? _FPCLASS_NZ : _FPCLASS_PZ;
304 return signbit(num) ? _FPCLASS_NN : _FPCLASS_PN;
307 return _FPCLASS_QNAN;
308 return num == 0.0 ? _FPCLASS_PZ : (num < 0 ? _FPCLASS_NN : _FPCLASS_PN);
312 /*********************************************************************
315 unsigned int _rotl(unsigned int num, int shift)
318 return (num << shift) | (num >> (32-shift));
321 /*********************************************************************
324 double _logb(double num)
326 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
330 /*********************************************************************
333 unsigned long _lrotl(unsigned long num, int shift)
336 return (num << shift) | (num >> (32-shift));
339 /*********************************************************************
342 unsigned long _lrotr(unsigned long num, int shift)
345 return (num >> shift) | (num << (32-shift));
348 /*********************************************************************
351 unsigned int _rotr(unsigned int num, int shift)
354 return (num >> shift) | (num << (32-shift));
357 /*********************************************************************
360 double _scalb(double num, long power)
362 /* Note - Can't forward directly as libc expects y as double */
363 double dblpower = (double)power;
364 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
365 return scalb(num, dblpower);
368 /*********************************************************************
369 * _matherr (MSVCRT.@)
371 int MSVCRT__matherr(struct MSVCRT__exception *e)
374 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
378 if (MSVCRT_default_matherr_func)
379 return MSVCRT_default_matherr_func(e);
380 ERR(":Unhandled math error!\n");
384 /*********************************************************************
385 * __setusermatherr (MSVCRT.@)
387 void MSVCRT___setusermatherr(MSVCRT_matherr_func func)
389 MSVCRT_default_matherr_func = func;
390 TRACE(":new matherr handler %p\n", func);
393 /**********************************************************************
394 * _statusfp (MSVCRT.@)
396 unsigned int _statusfp(void)
398 unsigned int retVal = 0;
399 #if defined(__GNUC__) && defined(__i386__)
402 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
403 if (fpword & 0x1) retVal |= _SW_INVALID;
404 if (fpword & 0x2) retVal |= _SW_DENORMAL;
405 if (fpword & 0x4) retVal |= _SW_ZERODIVIDE;
406 if (fpword & 0x8) retVal |= _SW_OVERFLOW;
407 if (fpword & 0x10) retVal |= _SW_UNDERFLOW;
408 if (fpword & 0x20) retVal |= _SW_INEXACT;
410 FIXME(":Not implemented!\n");
415 /*********************************************************************
416 * _clearfp (MSVCRT.@)
418 unsigned int _clearfp(void)
420 unsigned int retVal = _statusfp();
421 #if defined(__GNUC__) && defined(__i386__)
422 __asm__ __volatile__( "fnclex" );
424 FIXME(":Not Implemented\n");
429 /*********************************************************************
430 * __fpecode (MSVCRT.@)
434 return &msvcrt_get_thread_data()->fpecode;
437 /*********************************************************************
440 double MSVCRT_ldexp(double num, long exp)
442 double z = ldexp(num,exp);
445 *MSVCRT__errno() = MSVCRT_ERANGE;
446 else if (z == 0 && signbit(z))
447 z = 0.0; /* Convert -0 -> +0 */
451 /*********************************************************************
454 double MSVCRT__cabs(struct MSVCRT__complex num)
456 return sqrt(num.x * num.x + num.y * num.y);
459 /*********************************************************************
460 * _chgsign (MSVCRT.@)
462 double _chgsign(double num)
464 /* FIXME: +-infinity,Nan not tested */
468 /*********************************************************************
469 * _control87 (MSVCRT.@)
471 unsigned int _control87(unsigned int newval, unsigned int mask)
473 #if defined(__GNUC__) && defined(__i386__)
474 unsigned int fpword = 0;
475 unsigned int flags = 0;
477 TRACE("(%08x, %08x): Called\n", newval, mask);
479 /* Get fp control word */
480 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) : );
482 TRACE("Control word before : %08x\n", fpword);
484 /* Convert into mask constants */
485 if (fpword & 0x1) flags |= _EM_INVALID;
486 if (fpword & 0x2) flags |= _EM_DENORMAL;
487 if (fpword & 0x4) flags |= _EM_ZERODIVIDE;
488 if (fpword & 0x8) flags |= _EM_OVERFLOW;
489 if (fpword & 0x10) flags |= _EM_UNDERFLOW;
490 if (fpword & 0x20) flags |= _EM_INEXACT;
491 switch(fpword & 0xC00) {
492 case 0xC00: flags |= _RC_UP|_RC_DOWN; break;
493 case 0x800: flags |= _RC_UP; break;
494 case 0x400: flags |= _RC_DOWN; break;
496 switch(fpword & 0x300) {
497 case 0x0: flags |= _PC_24; break;
498 case 0x200: flags |= _PC_53; break;
499 case 0x300: flags |= _PC_64; break;
501 if (fpword & 0x1000) flags |= _IC_AFFINE;
503 /* Mask with parameters */
504 flags = (flags & ~mask) | (newval & mask);
506 /* Convert (masked) value back to fp word */
508 if (flags & _EM_INVALID) fpword |= 0x1;
509 if (flags & _EM_DENORMAL) fpword |= 0x2;
510 if (flags & _EM_ZERODIVIDE) fpword |= 0x4;
511 if (flags & _EM_OVERFLOW) fpword |= 0x8;
512 if (flags & _EM_UNDERFLOW) fpword |= 0x10;
513 if (flags & _EM_INEXACT) fpword |= 0x20;
514 switch(flags & (_RC_UP | _RC_DOWN)) {
515 case _RC_UP|_RC_DOWN: fpword |= 0xC00; break;
516 case _RC_UP: fpword |= 0x800; break;
517 case _RC_DOWN: fpword |= 0x400; break;
519 switch (flags & (_PC_24 | _PC_53)) {
520 case _PC_64: fpword |= 0x300; break;
521 case _PC_53: fpword |= 0x200; break;
522 case _PC_24: fpword |= 0x0; break;
524 if (flags & _IC_AFFINE) fpword |= 0x1000;
526 TRACE("Control word after : %08x\n", fpword);
528 /* Put fp control word */
529 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
533 FIXME(":Not Implemented!\n");
538 /*********************************************************************
539 * _controlfp (MSVCRT.@)
541 unsigned int _controlfp(unsigned int newval, unsigned int mask)
544 return _control87( newval, mask & ~_EM_DENORMAL );
546 FIXME(":Not Implemented!\n");
551 /*********************************************************************
552 * _copysign (MSVCRT.@)
554 double _copysign(double num, double sign)
556 /* FIXME: Behaviour for Nan/Inf? */
558 return num < 0.0 ? num : -num;
559 return num < 0.0 ? -num : num;
562 /*********************************************************************
565 int _finite(double num)
567 return (finite(num)?1:0); /* See comment for _isnan() */
570 /*********************************************************************
571 * _fpreset (MSVCRT.@)
575 #if defined(__GNUC__) && defined(__i386__)
576 __asm__ __volatile__( "fninit" );
578 FIXME(":Not Implemented!\n");
582 /*********************************************************************
585 INT _isnan(double num)
587 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
588 * Do the same, as the result may be used in calculations
590 return isnan(num) ? 1 : 0;
593 /*********************************************************************
596 double _y0(double num)
599 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
601 if (_fpclass(retval) == _FPCLASS_NINF)
603 *MSVCRT__errno() = MSVCRT_EDOM;
609 /*********************************************************************
612 double _y1(double num)
615 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
617 if (_fpclass(retval) == _FPCLASS_NINF)
619 *MSVCRT__errno() = MSVCRT_EDOM;
625 /*********************************************************************
628 double _yn(int order, double num)
631 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
632 retval = yn(order,num);
633 if (_fpclass(retval) == _FPCLASS_NINF)
635 *MSVCRT__errno() = MSVCRT_EDOM;
641 /*********************************************************************
642 * _nextafter (MSVCRT.@)
644 double _nextafter(double num, double next)
647 if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
648 retval = nextafter(num,next);
652 /*********************************************************************
655 char *_ecvt( double number, int ndigits, int *decpt, int *sign )
657 MSVCRT_thread_data *data = msvcrt_get_thread_data();
660 if (!data->efcvt_buffer)
661 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
663 snprintf(data->efcvt_buffer, 80, "%.*e", ndigits /* FIXME wrong */, number);
664 *sign = (number < 0);
665 dec = strchr(data->efcvt_buffer, '.');
666 *decpt = (dec) ? dec - data->efcvt_buffer : -1;
667 return data->efcvt_buffer;
670 /***********************************************************************
673 char *_fcvt( double number, int ndigits, int *decpt, int *sign )
675 MSVCRT_thread_data *data = msvcrt_get_thread_data();
678 if (!data->efcvt_buffer)
679 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
681 snprintf(data->efcvt_buffer, 80, "%.*e", ndigits, number);
682 *sign = (number < 0);
683 dec = strchr(data->efcvt_buffer, '.');
684 *decpt = (dec) ? dec - data->efcvt_buffer : -1;
685 return data->efcvt_buffer;
688 /***********************************************************************
691 * FIXME: uses both E and F.
693 char *_gcvt( double number, int ndigit, char *buff )
695 sprintf(buff, "%.*E", ndigit, number);
699 #include <stdlib.h> /* div_t, ldiv_t */
701 /*********************************************************************
704 * [i386] Windows binary compatible - returns the struct in eax/edx.
707 unsigned __int64 MSVCRT_div(int num, int denom)
709 div_t dt = div(num,denom);
710 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
713 /*********************************************************************
716 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
718 MSVCRT_div_t MSVCRT_div(int num, int denom)
720 div_t dt = div(num,denom);
728 #endif /* ifdef __i386__ */
731 /*********************************************************************
734 * [i386] Windows binary compatible - returns the struct in eax/edx.
737 unsigned __int64 MSVCRT_ldiv(long num, long denom)
739 ldiv_t ldt = ldiv(num,denom);
740 return ((unsigned __int64)ldt.rem << 32) | (unsigned long)ldt.quot;
743 /*********************************************************************
746 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
748 MSVCRT_ldiv_t MSVCRT_ldiv(long num, long denom)
750 ldiv_t result = ldiv(num,denom);
753 ret.quot = result.quot;
754 ret.rem = result.rem;
758 #endif /* ifdef __i386__ */
760 /***********************************************************************
761 * _adj_fdiv_m16i (MSVCRT.@)
763 * This function is likely to have the wrong number of arguments.
766 * I _think_ this function is intended to work around the Pentium
769 void _adj_fdiv_m16i(void)
774 /***********************************************************************
775 * _adj_fdiv_m32 (MSVCRT.@)
777 * This function is likely to have the wrong number of arguments.
780 * I _think_ this function is intended to work around the Pentium
783 void _adj_fdiv_m32(void)
788 /***********************************************************************
789 * _adj_fdiv_m32i (MSVCRT.@)
791 * This function is likely to have the wrong number of arguments.
794 * I _think_ this function is intended to work around the Pentium
797 void _adj_fdiv_m32i(void)
802 /***********************************************************************
803 * _adj_fdiv_m64 (MSVCRT.@)
805 * This function is likely to have the wrong number of arguments.
808 * I _think_ this function is intended to work around the Pentium
811 void _adj_fdiv_m64(void)
816 /***********************************************************************
817 * _adj_fdiv_r (MSVCRT.@)
819 * This function is likely to have the wrong number of arguments.
822 * I _think_ this function is intended to work around the Pentium
825 void _adj_fdiv_r(void)
830 /***********************************************************************
831 * _adj_fdivr_m16i (MSVCRT.@)
833 * This function is likely to have the wrong number of arguments.
836 * I _think_ this function is intended to work around the Pentium
839 void _adj_fdivr_m16i(void)
844 /***********************************************************************
845 * _adj_fdivr_m32 (MSVCRT.@)
847 * This function is likely to have the wrong number of arguments.
850 * I _think_ this function is intended to work around the Pentium
853 void _adj_fdivr_m32(void)
858 /***********************************************************************
859 * _adj_fdivr_m32i (MSVCRT.@)
861 * This function is likely to have the wrong number of arguments.
864 * I _think_ this function is intended to work around the Pentium
867 void _adj_fdivr_m32i(void)
872 /***********************************************************************
873 * _adj_fdivr_m64 (MSVCRT.@)
875 * This function is likely to have the wrong number of arguments.
878 * I _think_ this function is intended to work around the Pentium
881 void _adj_fdivr_m64(void)
886 /***********************************************************************
887 * _adj_fpatan (MSVCRT.@)
889 * This function is likely to have the wrong number of arguments.
892 * I _think_ this function is intended to work around the Pentium
895 void _adj_fpatan(void)
900 /***********************************************************************
901 * _adj_fprem (MSVCRT.@)
903 * This function is likely to have the wrong number of arguments.
906 * I _think_ this function is intended to work around the Pentium
909 void _adj_fprem(void)
914 /***********************************************************************
915 * _adj_fprem1 (MSVCRT.@)
917 * This function is likely to have the wrong number of arguments.
920 * I _think_ this function is intended to work around the Pentium
923 void _adj_fprem1(void)
928 /***********************************************************************
929 * _adj_fptan (MSVCRT.@)
931 * This function is likely to have the wrong number of arguments.
934 * I _think_ this function is intended to work around the Pentium
937 void _adj_fptan(void)
942 /***********************************************************************
943 * _adjust_fdiv (MSVCRT.@)
945 * I _think_ this function should be a variable indicating whether
946 * Pentium fdiv bug safe code should be used.
948 void _adjust_fdiv(void)
953 /***********************************************************************
954 * _safe_fdiv (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_fdiv(void)
967 /***********************************************************************
968 * _safe_fdivr (MSVCRT.@)
970 * This function is likely to have the wrong number of arguments.
973 * I _think_ this function is intended to work around the Pentium
976 void _safe_fdivr(void)
981 /***********************************************************************
982 * _safe_fprem (MSVCRT.@)
984 * This function is likely to have the wrong number of arguments.
987 * I _think_ this function is intended to work around the Pentium
990 void _safe_fprem(void)
995 /***********************************************************************
996 * _safe_fprem1 (MSVCRT.@)
999 * This function is likely to have the wrong number of arguments.
1002 * I _think_ this function is intended to work around the Pentium
1005 void _safe_fprem1(void)
1007 TRACE("(): stub\n");