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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define __USE_ISOC9X 1
24 #define __USE_ISOC99 1
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 #ifndef finite /* Could be a macro */
39 #define finite(x) isfinite(x)
41 #define finite(x) (!isnan(x)) /* At least catch some cases */
50 typedef int (CDECL *MSVCRT_matherr_func)(struct MSVCRT__exception *);
52 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
56 /*********************************************************************
57 * MSVCRT_acosf (MSVCRT.@)
59 float CDECL MSVCRT_acosf( float x )
61 if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
62 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
63 * asin() uses a similar construction. This is bad because as x gets nearer to
64 * 1 the error in the expression "1 - x^2" can get relatively large due to
65 * cancellation. The sqrt() makes things worse. A safer way to calculate
66 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
67 return atan2f(sqrtf((1 - x) * (1 + x)), x);
70 /*********************************************************************
71 * MSVCRT_asinf (MSVCRT.@)
73 float CDECL MSVCRT_asinf( float x )
75 if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
76 return atan2f(x, sqrtf((1 - x) * (1 + x)));
79 /*********************************************************************
80 * MSVCRT_atanf (MSVCRT.@)
82 float CDECL MSVCRT_atanf( float x )
84 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
88 /*********************************************************************
89 * MSVCRT_atan2f (MSVCRT.@)
91 float CDECL MSVCRT_atan2f( float x, float y )
93 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
97 /*********************************************************************
98 * MSVCRT_cosf (MSVCRT.@)
100 float CDECL MSVCRT_cosf( float x )
102 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
106 /*********************************************************************
107 * MSVCRT_coshf (MSVCRT.@)
109 float CDECL MSVCRT_coshf( float x )
111 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
115 /*********************************************************************
116 * MSVCRT_expf (MSVCRT.@)
118 float CDECL MSVCRT_expf( float x )
120 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
124 /*********************************************************************
125 * MSVCRT_fmodf (MSVCRT.@)
127 float CDECL MSVCRT_fmodf( float x, float y )
129 if (!finitef(x) || !finitef(y)) *MSVCRT__errno() = MSVCRT_EDOM;
133 /*********************************************************************
134 * MSVCRT_logf (MSVCRT.@)
136 float CDECL MSVCRT_logf( float x)
138 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
139 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
143 /*********************************************************************
144 * MSVCRT_log10f (MSVCRT.@)
146 float CDECL MSVCRT_log10f( float x )
148 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
149 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
153 /*********************************************************************
154 * MSVCRT_powf (MSVCRT.@)
156 float CDECL MSVCRT_powf( float x, float y )
158 /* FIXME: If x < 0 and y is not integral, set EDOM */
160 if (!finitef(z)) *MSVCRT__errno() = MSVCRT_EDOM;
164 /*********************************************************************
165 * MSVCRT_sinf (MSVCRT.@)
167 float CDECL MSVCRT_sinf( float x )
169 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
173 /*********************************************************************
174 * MSVCRT_sinhf (MSVCRT.@)
176 float CDECL MSVCRT_sinhf( float x )
178 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
182 /*********************************************************************
183 * MSVCRT_sqrtf (MSVCRT.@)
185 float CDECL MSVCRT_sqrtf( float x )
187 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
191 /*********************************************************************
192 * MSVCRT_tanf (MSVCRT.@)
194 float CDECL MSVCRT_tanf( float x )
196 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
200 /*********************************************************************
201 * MSVCRT_tanhf (MSVCRT.@)
203 float CDECL MSVCRT_tanhf( float x )
205 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
209 /*********************************************************************
212 float CDECL MSVCRT_ceilf( float x )
217 /*********************************************************************
220 float CDECL MSVCRT_floorf( float x )
225 /*********************************************************************
228 float CDECL MSVCRT_frexpf( float x, int *exp )
230 return frexpf( x, exp );
233 /*********************************************************************
236 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
238 if (!finitef(num)) *MSVCRT__errno() = MSVCRT_EDOM;
239 return ldexpf(num, power);
242 /*********************************************************************
245 double CDECL MSVCRT_modff( float x, float *iptr )
247 return modff( x, iptr );
252 /*********************************************************************
253 * MSVCRT_acos (MSVCRT.@)
255 double CDECL MSVCRT_acos( double x )
257 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
258 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
259 * asin() uses a similar construction. This is bad because as x gets nearer to
260 * 1 the error in the expression "1 - x^2" can get relatively large due to
261 * cancellation. The sqrt() makes things worse. A safer way to calculate
262 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
263 return atan2(sqrt((1 - x) * (1 + x)), x);
266 /*********************************************************************
267 * MSVCRT_asin (MSVCRT.@)
269 double CDECL MSVCRT_asin( double x )
271 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
272 return atan2(x, sqrt((1 - x) * (1 + x)));
275 /*********************************************************************
276 * MSVCRT_atan (MSVCRT.@)
278 double CDECL MSVCRT_atan( double x )
280 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
284 /*********************************************************************
285 * MSVCRT_atan2 (MSVCRT.@)
287 double CDECL MSVCRT_atan2( double x, double y )
289 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
293 /*********************************************************************
294 * MSVCRT_cos (MSVCRT.@)
296 double CDECL MSVCRT_cos( double x )
298 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
302 /*********************************************************************
303 * MSVCRT_cosh (MSVCRT.@)
305 double CDECL MSVCRT_cosh( double x )
307 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
311 /*********************************************************************
312 * MSVCRT_exp (MSVCRT.@)
314 double CDECL MSVCRT_exp( double x )
316 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
320 /*********************************************************************
321 * MSVCRT_fmod (MSVCRT.@)
323 double CDECL MSVCRT_fmod( double x, double y )
325 if (!finite(x) || !finite(y)) *MSVCRT__errno() = MSVCRT_EDOM;
329 /*********************************************************************
330 * MSVCRT_log (MSVCRT.@)
332 double CDECL MSVCRT_log( double x)
334 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
335 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
339 /*********************************************************************
340 * MSVCRT_log10 (MSVCRT.@)
342 double CDECL MSVCRT_log10( double x )
344 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
345 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
349 /*********************************************************************
350 * MSVCRT_pow (MSVCRT.@)
352 double CDECL MSVCRT_pow( double x, double y )
354 /* FIXME: If x < 0 and y is not integral, set EDOM */
356 if (!finite(z)) *MSVCRT__errno() = MSVCRT_EDOM;
360 /*********************************************************************
361 * MSVCRT_sin (MSVCRT.@)
363 double CDECL MSVCRT_sin( double x )
365 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
369 /*********************************************************************
370 * MSVCRT_sinh (MSVCRT.@)
372 double CDECL MSVCRT_sinh( double x )
374 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
378 /*********************************************************************
379 * MSVCRT_sqrt (MSVCRT.@)
381 double CDECL MSVCRT_sqrt( double x )
383 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
387 /*********************************************************************
388 * MSVCRT_tan (MSVCRT.@)
390 double CDECL MSVCRT_tan( double x )
392 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
396 /*********************************************************************
397 * MSVCRT_tanh (MSVCRT.@)
399 double CDECL MSVCRT_tanh( double x )
401 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
406 #if defined(__GNUC__) && defined(__i386__)
408 #define FPU_DOUBLE(var) double var; \
409 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
410 #define FPU_DOUBLES(var1,var2) double var1,var2; \
411 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
412 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
414 /*********************************************************************
417 double CDECL _CIacos(void)
420 return MSVCRT_acos(x);
423 /*********************************************************************
426 double CDECL _CIasin(void)
429 return MSVCRT_asin(x);
432 /*********************************************************************
435 double CDECL _CIatan(void)
438 return MSVCRT_atan(x);
441 /*********************************************************************
442 * _CIatan2 (MSVCRT.@)
444 double CDECL _CIatan2(void)
447 return MSVCRT_atan2(x,y);
450 /*********************************************************************
453 double CDECL _CIcos(void)
456 return MSVCRT_cos(x);
459 /*********************************************************************
462 double CDECL _CIcosh(void)
465 return MSVCRT_cosh(x);
468 /*********************************************************************
471 double CDECL _CIexp(void)
474 return MSVCRT_exp(x);
477 /*********************************************************************
480 double CDECL _CIfmod(void)
483 return MSVCRT_fmod(x,y);
486 /*********************************************************************
489 double CDECL _CIlog(void)
492 return MSVCRT_log(x);
495 /*********************************************************************
496 * _CIlog10 (MSVCRT.@)
498 double CDECL _CIlog10(void)
501 return MSVCRT_log10(x);
504 /*********************************************************************
507 double CDECL _CIpow(void)
510 return MSVCRT_pow(x,y);
513 /*********************************************************************
516 double CDECL _CIsin(void)
519 return MSVCRT_sin(x);
522 /*********************************************************************
525 double CDECL _CIsinh(void)
528 return MSVCRT_sinh(x);
531 /*********************************************************************
534 double CDECL _CIsqrt(void)
537 return MSVCRT_sqrt(x);
540 /*********************************************************************
543 double CDECL _CItan(void)
546 return MSVCRT_tan(x);
549 /*********************************************************************
552 double CDECL _CItanh(void)
555 return MSVCRT_tanh(x);
558 #endif /* defined(__GNUC__) && defined(__i386__) */
560 /*********************************************************************
561 * _fpclass (MSVCRT.@)
563 int CDECL _fpclass(double num)
565 #if defined(HAVE_FPCLASS) || defined(fpclass)
566 switch (fpclass( num ))
569 case FP_SNAN: return MSVCRT__FPCLASS_SNAN;
572 case FP_QNAN: return MSVCRT__FPCLASS_QNAN;
575 case FP_NINF: return MSVCRT__FPCLASS_NINF;
578 case FP_PINF: return MSVCRT__FPCLASS_PINF;
581 case FP_NDENORM: return MSVCRT__FPCLASS_ND;
584 case FP_PDENORM: return MSVCRT__FPCLASS_PD;
587 case FP_NZERO: return MSVCRT__FPCLASS_NZ;
590 case FP_PZERO: return MSVCRT__FPCLASS_PZ;
593 case FP_NNORM: return MSVCRT__FPCLASS_NN;
596 case FP_PNORM: return MSVCRT__FPCLASS_PN;
598 default: return MSVCRT__FPCLASS_PN;
600 #elif defined (fpclassify)
601 switch (fpclassify( num ))
603 case FP_NAN: return MSVCRT__FPCLASS_QNAN;
604 case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
605 case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
606 case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
608 return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
611 return MSVCRT__FPCLASS_QNAN;
612 return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
616 /*********************************************************************
619 unsigned int CDECL _rotl(unsigned int num, int shift)
622 return (num << shift) | (num >> (32-shift));
625 /*********************************************************************
628 double CDECL _logb(double num)
630 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
634 /*********************************************************************
637 MSVCRT_ulong CDECL MSVCRT__lrotl(MSVCRT_ulong num, int shift)
640 return (num << shift) | (num >> (32-shift));
643 /*********************************************************************
646 MSVCRT_ulong CDECL MSVCRT__lrotr(MSVCRT_ulong num, int shift)
649 return (num >> shift) | (num << (32-shift));
652 /*********************************************************************
655 unsigned int CDECL _rotr(unsigned int num, int shift)
658 return (num >> shift) | (num << (32-shift));
661 /*********************************************************************
664 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
666 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
667 return ldexp(num, power);
670 /*********************************************************************
673 double CDECL _hypot(double x, double y)
675 /* FIXME: errno handling */
676 return hypot( x, y );
679 /*********************************************************************
682 float CDECL _hypotf(float x, float y)
684 /* FIXME: errno handling */
685 return hypotf( x, y );
688 /*********************************************************************
691 double CDECL MSVCRT_ceil( double x )
696 /*********************************************************************
699 double CDECL MSVCRT_floor( double x )
704 /*********************************************************************
707 double CDECL MSVCRT_fabs( double x )
712 /*********************************************************************
715 double CDECL MSVCRT_frexp( double x, int *exp )
717 return frexp( x, exp );
720 /*********************************************************************
723 double CDECL MSVCRT_modf( double x, double *iptr )
725 return modf( x, iptr );
728 /*********************************************************************
729 * _matherr (MSVCRT.@)
731 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
734 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
738 if (MSVCRT_default_matherr_func)
739 return MSVCRT_default_matherr_func(e);
740 ERR(":Unhandled math error!\n");
744 /*********************************************************************
745 * __setusermatherr (MSVCRT.@)
747 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
749 MSVCRT_default_matherr_func = func;
750 TRACE(":new matherr handler %p\n", func);
753 /**********************************************************************
754 * _statusfp (MSVCRT.@)
756 unsigned int CDECL _statusfp(void)
758 unsigned int retVal = 0;
759 #if defined(__GNUC__) && defined(__i386__)
762 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
763 if (fpword & 0x1) retVal |= MSVCRT__SW_INVALID;
764 if (fpword & 0x2) retVal |= MSVCRT__SW_DENORMAL;
765 if (fpword & 0x4) retVal |= MSVCRT__SW_ZERODIVIDE;
766 if (fpword & 0x8) retVal |= MSVCRT__SW_OVERFLOW;
767 if (fpword & 0x10) retVal |= MSVCRT__SW_UNDERFLOW;
768 if (fpword & 0x20) retVal |= MSVCRT__SW_INEXACT;
770 FIXME(":Not implemented!\n");
775 /*********************************************************************
776 * _clearfp (MSVCRT.@)
778 unsigned int CDECL _clearfp(void)
780 unsigned int retVal = _statusfp();
781 #if defined(__GNUC__) && defined(__i386__)
782 __asm__ __volatile__( "fnclex" );
784 FIXME(":Not Implemented\n");
789 /*********************************************************************
790 * __fpecode (MSVCRT.@)
792 int * CDECL __fpecode(void)
794 return &msvcrt_get_thread_data()->fpecode;
797 /*********************************************************************
800 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
802 double z = ldexp(num,exp);
805 *MSVCRT__errno() = MSVCRT_ERANGE;
806 else if (z == 0 && signbit(z))
807 z = 0.0; /* Convert -0 -> +0 */
811 /*********************************************************************
814 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
816 return sqrt(num.x * num.x + num.y * num.y);
819 /*********************************************************************
820 * _chgsign (MSVCRT.@)
822 double CDECL _chgsign(double num)
824 /* FIXME: +-infinity,Nan not tested */
828 /*********************************************************************
829 * _control87 (MSVCRT.@)
831 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
833 #if defined(__GNUC__) && defined(__i386__)
834 unsigned int fpword = 0;
835 unsigned int flags = 0;
837 TRACE("(%08x, %08x): Called\n", newval, mask);
839 /* Get fp control word */
840 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) : );
842 TRACE("Control word before : %08x\n", fpword);
844 /* Convert into mask constants */
845 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
846 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
847 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
848 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
849 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
850 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
851 switch(fpword & 0xC00) {
852 case 0xC00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
853 case 0x800: flags |= MSVCRT__RC_UP; break;
854 case 0x400: flags |= MSVCRT__RC_DOWN; break;
856 switch(fpword & 0x300) {
857 case 0x0: flags |= MSVCRT__PC_24; break;
858 case 0x200: flags |= MSVCRT__PC_53; break;
859 case 0x300: flags |= MSVCRT__PC_64; break;
861 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
863 /* Mask with parameters */
864 flags = (flags & ~mask) | (newval & mask);
866 /* Convert (masked) value back to fp word */
868 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
869 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
870 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
871 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
872 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
873 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
874 switch(flags & (MSVCRT__RC_UP | MSVCRT__RC_DOWN)) {
875 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xC00; break;
876 case MSVCRT__RC_UP: fpword |= 0x800; break;
877 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
879 switch (flags & (MSVCRT__PC_24 | MSVCRT__PC_53)) {
880 case MSVCRT__PC_64: fpword |= 0x300; break;
881 case MSVCRT__PC_53: fpword |= 0x200; break;
882 case MSVCRT__PC_24: fpword |= 0x0; break;
884 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
886 TRACE("Control word after : %08x\n", fpword);
888 /* Put fp control word */
889 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
893 FIXME(":Not Implemented!\n");
898 /*********************************************************************
899 * _controlfp (MSVCRT.@)
901 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
904 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
906 FIXME(":Not Implemented!\n");
911 /*********************************************************************
912 * _controlfp_s (MSVCRT.@)
914 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
919 FIXME("(%p %u %u) semi-stub\n", cur, newval, mask);
921 flags = _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
928 FIXME(":Not Implemented!\n");
933 /*********************************************************************
934 * _copysign (MSVCRT.@)
936 double CDECL _copysign(double num, double sign)
938 /* FIXME: Behaviour for Nan/Inf? */
940 return num < 0.0 ? num : -num;
941 return num < 0.0 ? -num : num;
944 /*********************************************************************
947 int CDECL _finite(double num)
949 return (finite(num)?1:0); /* See comment for _isnan() */
952 /*********************************************************************
953 * _fpreset (MSVCRT.@)
955 void CDECL _fpreset(void)
957 #if defined(__GNUC__) && defined(__i386__)
958 __asm__ __volatile__( "fninit" );
960 FIXME(":Not Implemented!\n");
964 /*********************************************************************
967 INT CDECL _isnan(double num)
969 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
970 * Do the same, as the result may be used in calculations
972 return isnan(num) ? 1 : 0;
975 /*********************************************************************
978 double CDECL _j0(double num)
980 /* FIXME: errno handling */
984 /*********************************************************************
987 double CDECL _j1(double num)
989 /* FIXME: errno handling */
993 /*********************************************************************
996 double CDECL _jn(int n, double num)
998 /* FIXME: errno handling */
1002 /*********************************************************************
1005 double CDECL _y0(double num)
1008 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1010 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
1012 *MSVCRT__errno() = MSVCRT_EDOM;
1018 /*********************************************************************
1021 double CDECL _y1(double num)
1024 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1026 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
1028 *MSVCRT__errno() = MSVCRT_EDOM;
1034 /*********************************************************************
1037 double CDECL _yn(int order, double num)
1040 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1041 retval = yn(order,num);
1042 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
1044 *MSVCRT__errno() = MSVCRT_EDOM;
1050 /*********************************************************************
1051 * _nextafter (MSVCRT.@)
1053 double CDECL _nextafter(double num, double next)
1056 if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1057 retval = nextafter(num,next);
1061 /*********************************************************************
1064 char * CDECL _ecvt( double number, int ndigits, int *decpt, int *sign )
1067 thread_data_t *data = msvcrt_get_thread_data();
1068 /* FIXME: check better for overflow (native supports over 300 chars's) */
1069 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1070 * 4 for exponent and one for
1071 * terminating '\0' */
1072 if (!data->efcvt_buffer)
1073 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1080 /* handle cases with zero ndigits or less */
1082 if( prec < 1) prec = 2;
1083 len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1084 /* take the decimal "point away */
1086 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1087 /* take the exponential "e" out */
1088 data->efcvt_buffer[ prec] = '\0';
1089 /* read the exponent */
1090 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1092 /* adjust for some border cases */
1093 if( data->efcvt_buffer[0] == '0')/* value is zero */
1095 /* handle cases with zero ndigits or less */
1097 if( data->efcvt_buffer[ 0] >= '5')
1099 data->efcvt_buffer[ 0] = '\0';
1101 TRACE("out=\"%s\"\n",data->efcvt_buffer);
1102 return data->efcvt_buffer;
1105 /***********************************************************************
1108 char * CDECL _fcvt( double number, int ndigits, int *decpt, int *sign )
1110 thread_data_t *data = msvcrt_get_thread_data();
1111 int stop, dec1, dec2;
1112 char *ptr1, *ptr2, *first;
1113 char buf[80]; /* ought to be enough */
1115 if (!data->efcvt_buffer)
1116 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1124 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1126 ptr2 = data->efcvt_buffer;
1131 /* For numbers below the requested resolution, work out where
1132 the decimal point will be rather than finding it in the string */
1133 if (number < 1.0 && number > 0.0) {
1134 dec2 = log10(number + 1e-10);
1135 if (-dec2 <= ndigits) dec2 = 0;
1138 /* If requested digits is zero or less, we will need to truncate
1139 * the returned string */
1141 stop = strlen(buf) + ndigits;
1146 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1147 while (*ptr1 != '\0' && *ptr1 != '.') {
1148 if (!first) first = ptr2;
1149 if ((ptr1 - buf) < stop) {
1160 while (*ptr1 == '0') { /* Process leading zeroes */
1165 while (*ptr1 != '\0') {
1166 if (!first) first = ptr2;
1173 /* We never found a non-zero digit, then our number is either
1174 * smaller than the requested precision, or 0.0 */
1179 first = data->efcvt_buffer;
1184 *decpt = dec2 ? dec2 : dec1;
1188 /***********************************************************************
1191 * FIXME: uses both E and F.
1193 char * CDECL _gcvt( double number, int ndigit, char *buff )
1195 sprintf(buff, "%.*E", ndigit, number);
1199 #include <stdlib.h> /* div_t, ldiv_t */
1201 /*********************************************************************
1204 * [i386] Windows binary compatible - returns the struct in eax/edx.
1207 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1209 div_t dt = div(num,denom);
1210 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1213 /*********************************************************************
1216 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1218 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1220 div_t dt = div(num,denom);
1228 #endif /* ifdef __i386__ */
1231 /*********************************************************************
1234 * [i386] Windows binary compatible - returns the struct in eax/edx.
1237 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1239 ldiv_t ldt = ldiv(num,denom);
1240 return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1243 /*********************************************************************
1246 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1248 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1250 ldiv_t result = ldiv(num,denom);
1253 ret.quot = result.quot;
1254 ret.rem = result.rem;
1258 #endif /* ifdef __i386__ */
1262 /*********************************************************************
1263 * _adjust_fdiv (MSVCRT.@)
1264 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1266 int MSVCRT__adjust_fdiv = 0;
1268 /***********************************************************************
1269 * _adj_fdiv_m16i (MSVCRT.@)
1272 * I _think_ this function is intended to work around the Pentium
1275 void __stdcall _adj_fdiv_m16i( short arg )
1277 TRACE("(): stub\n");
1280 /***********************************************************************
1281 * _adj_fdiv_m32 (MSVCRT.@)
1284 * I _think_ this function is intended to work around the Pentium
1287 void __stdcall _adj_fdiv_m32( unsigned int arg )
1289 TRACE("(): stub\n");
1292 /***********************************************************************
1293 * _adj_fdiv_m32i (MSVCRT.@)
1296 * I _think_ this function is intended to work around the Pentium
1299 void __stdcall _adj_fdiv_m32i( int arg )
1301 TRACE("(): stub\n");
1304 /***********************************************************************
1305 * _adj_fdiv_m64 (MSVCRT.@)
1308 * I _think_ this function is intended to work around the Pentium
1311 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1313 TRACE("(): stub\n");
1316 /***********************************************************************
1317 * _adj_fdiv_r (MSVCRT.@)
1319 * This function is likely to have the wrong number of arguments.
1322 * I _think_ this function is intended to work around the Pentium
1325 void _adj_fdiv_r(void)
1327 TRACE("(): stub\n");
1330 /***********************************************************************
1331 * _adj_fdivr_m16i (MSVCRT.@)
1334 * I _think_ this function is intended to work around the Pentium
1337 void __stdcall _adj_fdivr_m16i( short arg )
1339 TRACE("(): stub\n");
1342 /***********************************************************************
1343 * _adj_fdivr_m32 (MSVCRT.@)
1346 * I _think_ this function is intended to work around the Pentium
1349 void __stdcall _adj_fdivr_m32( unsigned int arg )
1351 TRACE("(): stub\n");
1354 /***********************************************************************
1355 * _adj_fdivr_m32i (MSVCRT.@)
1358 * I _think_ this function is intended to work around the Pentium
1361 void __stdcall _adj_fdivr_m32i( int arg )
1363 TRACE("(): stub\n");
1366 /***********************************************************************
1367 * _adj_fdivr_m64 (MSVCRT.@)
1370 * I _think_ this function is intended to work around the Pentium
1373 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1375 TRACE("(): stub\n");
1378 /***********************************************************************
1379 * _adj_fpatan (MSVCRT.@)
1381 * This function is likely to have the wrong number of arguments.
1384 * I _think_ this function is intended to work around the Pentium
1387 void _adj_fpatan(void)
1389 TRACE("(): stub\n");
1392 /***********************************************************************
1393 * _adj_fprem (MSVCRT.@)
1395 * This function is likely to have the wrong number of arguments.
1398 * I _think_ this function is intended to work around the Pentium
1401 void _adj_fprem(void)
1403 TRACE("(): stub\n");
1406 /***********************************************************************
1407 * _adj_fprem1 (MSVCRT.@)
1409 * This function is likely to have the wrong number of arguments.
1412 * I _think_ this function is intended to work around the Pentium
1415 void _adj_fprem1(void)
1417 TRACE("(): stub\n");
1420 /***********************************************************************
1421 * _adj_fptan (MSVCRT.@)
1423 * This function is likely to have the wrong number of arguments.
1426 * I _think_ this function is intended to work around the Pentium
1429 void _adj_fptan(void)
1431 TRACE("(): stub\n");
1434 /***********************************************************************
1435 * _safe_fdiv (MSVCRT.@)
1437 * This function is likely to have the wrong number of arguments.
1440 * I _think_ this function is intended to work around the Pentium
1443 void _safe_fdiv(void)
1445 TRACE("(): stub\n");
1448 /***********************************************************************
1449 * _safe_fdivr (MSVCRT.@)
1451 * This function is likely to have the wrong number of arguments.
1454 * I _think_ this function is intended to work around the Pentium
1457 void _safe_fdivr(void)
1459 TRACE("(): stub\n");
1462 /***********************************************************************
1463 * _safe_fprem (MSVCRT.@)
1465 * This function is likely to have the wrong number of arguments.
1468 * I _think_ this function is intended to work around the Pentium
1471 void _safe_fprem(void)
1473 TRACE("(): stub\n");
1476 /***********************************************************************
1477 * _safe_fprem1 (MSVCRT.@)
1480 * This function is likely to have the wrong number of arguments.
1483 * I _think_ this function is intended to work around the Pentium
1486 void _safe_fprem1(void)
1488 TRACE("(): stub\n");
1491 #endif /* __i386__ */