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 /*********************************************************************
473 double MSVCRT_ldexp(double num, long exp)
475 double z = ldexp(num,exp);
478 *MSVCRT__errno() = MSVCRT_ERANGE;
479 else if (z == 0 && signbit(z))
480 z = 0.0; /* Convert -0 -> +0 */
484 /*********************************************************************
487 double _cabs(MSVCRT_complex num)
489 return sqrt(num.real * num.real + num.imaginary * num.imaginary);
492 /*********************************************************************
493 * _chgsign (MSVCRT.@)
495 double _chgsign(double num)
497 /* FIXME: +-infinity,Nan not tested */
501 /*********************************************************************
502 * _control87 (MSVCRT.@)
504 unsigned int _control87(unsigned int newval, unsigned int mask)
506 #if defined(__GNUC__) && defined(__i386__)
507 unsigned int fpword = 0;
508 unsigned int flags = 0;
510 TRACE("(%08x, %08x): Called\n", newval, mask);
512 /* Get fp control word */
513 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) : );
515 TRACE("Control word before : %08x\n", fpword);
517 /* Convert into mask constants */
518 if (fpword & 0x1) flags |= _EM_INVALID;
519 if (fpword & 0x2) flags |= _EM_DENORMAL;
520 if (fpword & 0x4) flags |= _EM_ZERODIVIDE;
521 if (fpword & 0x8) flags |= _EM_OVERFLOW;
522 if (fpword & 0x10) flags |= _EM_UNDERFLOW;
523 if (fpword & 0x20) flags |= _EM_INEXACT;
524 switch(fpword & 0xC00) {
525 case 0xC00: flags |= _RC_UP|_RC_DOWN; break;
526 case 0x800: flags |= _RC_UP; break;
527 case 0x400: flags |= _RC_DOWN; break;
529 switch(fpword & 0x300) {
530 case 0x0: flags |= _PC_24; break;
531 case 0x200: flags |= _PC_53; break;
532 case 0x300: flags |= _PC_64; break;
534 if (fpword & 0x1000) flags |= _IC_AFFINE;
536 /* Mask with parameters */
537 flags = (flags & ~mask) | (newval & mask);
539 /* Convert (masked) value back to fp word */
541 if (flags & _EM_INVALID) fpword |= 0x1;
542 if (flags & _EM_DENORMAL) fpword |= 0x2;
543 if (flags & _EM_ZERODIVIDE) fpword |= 0x4;
544 if (flags & _EM_OVERFLOW) fpword |= 0x8;
545 if (flags & _EM_UNDERFLOW) fpword |= 0x10;
546 if (flags & _EM_INEXACT) fpword |= 0x20;
547 switch(flags & (_RC_UP | _RC_DOWN)) {
548 case _RC_UP|_RC_DOWN: fpword |= 0xC00; break;
549 case _RC_UP: fpword |= 0x800; break;
550 case _RC_DOWN: fpword |= 0x400; break;
552 switch (flags & (_PC_24 | _PC_53)) {
553 case _PC_64: fpword |= 0x300; break;
554 case _PC_53: fpword |= 0x200; break;
555 case _PC_24: fpword |= 0x0; break;
557 if (flags & _IC_AFFINE) fpword |= 0x1000;
559 TRACE("Control word after : %08x\n", fpword);
561 /* Put fp control word */
562 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
566 FIXME(":Not Implemented!\n");
571 /*********************************************************************
572 * _controlfp (MSVCRT.@)
574 unsigned int _controlfp(unsigned int newval, unsigned int mask)
577 return _control87( newval, mask & ~_EM_DENORMAL );
579 FIXME(":Not Implemented!\n");
584 /*********************************************************************
585 * _copysign (MSVCRT.@)
587 double _copysign(double num, double sign)
589 /* FIXME: Behaviour for Nan/Inf? */
591 return num < 0.0 ? num : -num;
592 return num < 0.0 ? -num : num;
595 /*********************************************************************
598 int _finite(double num)
600 return (finite(num)?1:0); /* See comment for _isnan() */
603 /*********************************************************************
604 * _fpreset (MSVCRT.@)
608 #if defined(__GNUC__) && defined(__i386__)
609 __asm__ __volatile__( "fninit" );
611 FIXME(":Not Implemented!\n");
615 /*********************************************************************
618 INT _isnan(double num)
620 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
621 * Do the same, as the result may be used in calculations
623 return isnan(num) ? 1 : 0;
626 /*********************************************************************
629 double _y0(double num)
632 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
634 if (_fpclass(retval) == _FPCLASS_NINF)
636 *MSVCRT__errno() = MSVCRT_EDOM;
642 /*********************************************************************
645 double _y1(double num)
648 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
650 if (_fpclass(retval) == _FPCLASS_NINF)
652 *MSVCRT__errno() = MSVCRT_EDOM;
658 /*********************************************************************
661 double _yn(int order, double num)
664 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
665 retval = yn(order,num);
666 if (_fpclass(retval) == _FPCLASS_NINF)
668 *MSVCRT__errno() = MSVCRT_EDOM;
674 /*********************************************************************
675 * _nextafter (MSVCRT.@)
677 double _nextafter(double num, double next)
680 if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
681 retval = nextafter(num,next);
685 /*********************************************************************
688 char *_ecvt( double number, int ndigits, int *decpt, int *sign )
690 MSVCRT_thread_data *data = msvcrt_get_thread_data();
693 if (!data->efcvt_buffer)
694 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
696 snprintf(data->efcvt_buffer, 80, "%.*e", ndigits /* FIXME wrong */, number);
697 *sign = (number < 0);
698 dec = strchr(data->efcvt_buffer, '.');
699 *decpt = (dec) ? dec - data->efcvt_buffer : -1;
700 return data->efcvt_buffer;
703 /***********************************************************************
706 char *_fcvt( double number, int ndigits, int *decpt, int *sign )
708 MSVCRT_thread_data *data = msvcrt_get_thread_data();
711 if (!data->efcvt_buffer)
712 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
714 snprintf(data->efcvt_buffer, 80, "%.*e", ndigits, number);
715 *sign = (number < 0);
716 dec = strchr(data->efcvt_buffer, '.');
717 *decpt = (dec) ? dec - data->efcvt_buffer : -1;
718 return data->efcvt_buffer;
721 /***********************************************************************
724 * FIXME: uses both E and F.
726 char *_gcvt( double number, int ndigit, char *buff )
728 sprintf(buff, "%.*E", ndigit, number);
732 #include <stdlib.h> /* div_t, ldiv_t */
734 /*********************************************************************
737 * [i386] Windows binary compatible - returns the struct in eax/edx.
740 LONGLONG MSVCRT_div(int num, int denom)
743 div_t dt = div(num,denom);
744 retval = ((LONGLONG)dt.rem << 32) | dt.quot;
748 /*********************************************************************
751 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
753 MSVCRT_div_t MSVCRT_div(int num, int denom)
755 div_t dt = div(num,denom);
763 #endif /* ifdef __i386__ */
766 /*********************************************************************
769 * [i386] Windows binary compatible - returns the struct in eax/edx.
772 ULONGLONG MSVCRT_ldiv(long num, long denom)
775 ldiv_t ldt = ldiv(num,denom);
776 retval = ((ULONGLONG)ldt.rem << 32) | (ULONG)ldt.quot;
780 /*********************************************************************
783 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
785 MSVCRT_ldiv_t MSVCRT_ldiv(long num, long denom)
787 ldiv_t result = ldiv(num,denom);
790 ret.quot = result.quot;
791 ret.rem = result.rem;
795 #endif /* ifdef __i386__ */
797 /***********************************************************************
798 * _adj_fdiv_m16i (MSVCRT.@)
800 * This function is likely to have the wrong number of arguments.
803 * I _think_ this function is intended to work around the Pentium
806 void _adj_fdiv_m16i(void)
811 /***********************************************************************
812 * _adj_fdiv_m32 (MSVCRT.@)
814 * This function is likely to have the wrong number of arguments.
817 * I _think_ this function is intended to work around the Pentium
820 void _adj_fdiv_m32(void)
825 /***********************************************************************
826 * _adj_fdiv_m32i (MSVCRT.@)
828 * This function is likely to have the wrong number of arguments.
831 * I _think_ this function is intended to work around the Pentium
834 void _adj_fdiv_m32i(void)
839 /***********************************************************************
840 * _adj_fdiv_m64 (MSVCRT.@)
842 * This function is likely to have the wrong number of arguments.
845 * I _think_ this function is intended to work around the Pentium
848 void _adj_fdiv_m64(void)
853 /***********************************************************************
854 * _adj_fdiv_r (MSVCRT.@)
856 * This function is likely to have the wrong number of arguments.
859 * I _think_ this function is intended to work around the Pentium
862 void _adj_fdiv_r(void)
867 /***********************************************************************
868 * _adj_fdivr_m16i (MSVCRT.@)
870 * This function is likely to have the wrong number of arguments.
873 * I _think_ this function is intended to work around the Pentium
876 void _adj_fdivr_m16i(void)
881 /***********************************************************************
882 * _adj_fdivr_m32 (MSVCRT.@)
884 * This function is likely to have the wrong number of arguments.
887 * I _think_ this function is intended to work around the Pentium
890 void _adj_fdivr_m32(void)
895 /***********************************************************************
896 * _adj_fdivr_m32i (MSVCRT.@)
898 * This function is likely to have the wrong number of arguments.
901 * I _think_ this function is intended to work around the Pentium
904 void _adj_fdivr_m32i(void)
909 /***********************************************************************
910 * _adj_fdivr_m64 (MSVCRT.@)
912 * This function is likely to have the wrong number of arguments.
915 * I _think_ this function is intended to work around the Pentium
918 void _adj_fdivr_m64(void)
923 /***********************************************************************
924 * _adj_fpatan (MSVCRT.@)
926 * This function is likely to have the wrong number of arguments.
929 * I _think_ this function is intended to work around the Pentium
932 void _adj_fpatan(void)
937 /***********************************************************************
938 * _adj_fprem (MSVCRT.@)
940 * This function is likely to have the wrong number of arguments.
943 * I _think_ this function is intended to work around the Pentium
946 void _adj_fprem(void)
951 /***********************************************************************
952 * _adj_fprem1 (MSVCRT.@)
954 * This function is likely to have the wrong number of arguments.
957 * I _think_ this function is intended to work around the Pentium
960 void _adj_fprem1(void)
965 /***********************************************************************
966 * _adj_fptan (MSVCRT.@)
968 * This function is likely to have the wrong number of arguments.
971 * I _think_ this function is intended to work around the Pentium
974 void _adj_fptan(void)
979 /***********************************************************************
980 * _adjust_fdiv (MSVCRT.@)
982 * I _think_ this function should be a variable indicating whether
983 * Pentium fdiv bug safe code should be used.
985 void _adjust_fdiv(void)
990 /***********************************************************************
991 * _safe_fdiv (MSVCRT.@)
993 * This function is likely to have the wrong number of arguments.
996 * I _think_ this function is intended to work around the Pentium
999 void _safe_fdiv(void)
1001 TRACE("(): stub\n");
1004 /***********************************************************************
1005 * _safe_fdivr (MSVCRT.@)
1007 * This function is likely to have the wrong number of arguments.
1010 * I _think_ this function is intended to work around the Pentium
1013 void _safe_fdivr(void)
1015 TRACE("(): stub\n");
1018 /***********************************************************************
1019 * _safe_fprem (MSVCRT.@)
1021 * This function is likely to have the wrong number of arguments.
1024 * I _think_ this function is intended to work around the Pentium
1027 void _safe_fprem(void)
1029 TRACE("(): stub\n");
1032 /***********************************************************************
1033 * _safe_fprem1 (MSVCRT.@)
1036 * This function is likely to have the wrong number of arguments.
1039 * I _think_ this function is intended to work around the Pentium
1042 void _safe_fprem1(void)
1044 TRACE("(): stub\n");