msvcrt: Added _gcvt_s implementation.
[wine/wine-gecko.git] / dlls / msvcrt / math.c
blob4a3f65e2f35edea3d4f35876d44620c4ffb0a7f9
1 /*
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
20 #include "config.h"
22 #include <stdio.h>
23 #define __USE_ISOC9X 1
24 #define __USE_ISOC99 1
25 #include <math.h>
26 #ifdef HAVE_IEEEFP_H
27 #include <ieeefp.h>
28 #endif
30 #include "msvcrt.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 #ifndef HAVE_FINITE
37 #ifndef finite /* Could be a macro */
38 #ifdef isfinite
39 #define finite(x) isfinite(x)
40 #else
41 #define finite(x) (!isnan(x)) /* At least catch some cases */
42 #endif
43 #endif
44 #endif
46 #ifndef signbit
47 #define signbit(x) 0
48 #endif
50 typedef int (CDECL *MSVCRT_matherr_func)(struct MSVCRT__exception *);
52 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
54 /*********************************************************************
55 * _set_SSE2_enable (MSVCRT.@)
57 int CDECL MSVCRT__set_SSE2_enable(int flag)
59 FIXME("(%x) stub\n", flag);
60 return flag;
63 #ifdef __x86_64__
65 /*********************************************************************
66 * MSVCRT_acosf (MSVCRT.@)
68 float CDECL MSVCRT_acosf( float x )
70 if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
71 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
72 * asin() uses a similar construction. This is bad because as x gets nearer to
73 * 1 the error in the expression "1 - x^2" can get relatively large due to
74 * cancellation. The sqrt() makes things worse. A safer way to calculate
75 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
76 return atan2f(sqrtf((1 - x) * (1 + x)), x);
79 /*********************************************************************
80 * MSVCRT_asinf (MSVCRT.@)
82 float CDECL MSVCRT_asinf( float x )
84 if (x < -1.0 || x > 1.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
85 return atan2f(x, sqrtf((1 - x) * (1 + x)));
88 /*********************************************************************
89 * MSVCRT_atanf (MSVCRT.@)
91 float CDECL MSVCRT_atanf( float x )
93 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
94 return atanf(x);
97 /*********************************************************************
98 * MSVCRT_atan2f (MSVCRT.@)
100 float CDECL MSVCRT_atan2f( float x, float y )
102 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
103 return atan2f(x,y);
106 /*********************************************************************
107 * MSVCRT_cosf (MSVCRT.@)
109 float CDECL MSVCRT_cosf( float x )
111 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
112 return cosf(x);
115 /*********************************************************************
116 * MSVCRT_coshf (MSVCRT.@)
118 float CDECL MSVCRT_coshf( float x )
120 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
121 return coshf(x);
124 /*********************************************************************
125 * MSVCRT_expf (MSVCRT.@)
127 float CDECL MSVCRT_expf( float x )
129 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
130 return expf(x);
133 /*********************************************************************
134 * MSVCRT_fmodf (MSVCRT.@)
136 float CDECL MSVCRT_fmodf( float x, float y )
138 if (!finitef(x) || !finitef(y)) *MSVCRT__errno() = MSVCRT_EDOM;
139 return fmodf(x,y);
142 /*********************************************************************
143 * MSVCRT_logf (MSVCRT.@)
145 float CDECL MSVCRT_logf( float x)
147 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
148 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
149 return logf(x);
152 /*********************************************************************
153 * MSVCRT_log10f (MSVCRT.@)
155 float CDECL MSVCRT_log10f( float x )
157 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
158 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
159 return log10f(x);
162 /*********************************************************************
163 * MSVCRT_powf (MSVCRT.@)
165 float CDECL MSVCRT_powf( float x, float y )
167 /* FIXME: If x < 0 and y is not integral, set EDOM */
168 float z = powf(x,y);
169 if (!finitef(z)) *MSVCRT__errno() = MSVCRT_EDOM;
170 return z;
173 /*********************************************************************
174 * MSVCRT_sinf (MSVCRT.@)
176 float CDECL MSVCRT_sinf( float x )
178 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
179 return sinf(x);
182 /*********************************************************************
183 * MSVCRT_sinhf (MSVCRT.@)
185 float CDECL MSVCRT_sinhf( float x )
187 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
188 return sinhf(x);
191 /*********************************************************************
192 * MSVCRT_sqrtf (MSVCRT.@)
194 float CDECL MSVCRT_sqrtf( float x )
196 if (x < 0.0 || !finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
197 return sqrtf(x);
200 /*********************************************************************
201 * MSVCRT_tanf (MSVCRT.@)
203 float CDECL MSVCRT_tanf( float x )
205 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
206 return tanf(x);
209 /*********************************************************************
210 * MSVCRT_tanhf (MSVCRT.@)
212 float CDECL MSVCRT_tanhf( float x )
214 if (!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
215 return tanhf(x);
218 /*********************************************************************
219 * ceilf (MSVCRT.@)
221 float CDECL MSVCRT_ceilf( float x )
223 return ceilf(x);
226 /*********************************************************************
227 * floorf (MSVCRT.@)
229 float CDECL MSVCRT_floorf( float x )
231 return floorf(x);
234 /*********************************************************************
235 * frexpf (MSVCRT.@)
237 float CDECL MSVCRT_frexpf( float x, int *exp )
239 return frexpf( x, exp );
242 /*********************************************************************
243 * _scalbf (MSVCRT.@)
245 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
247 if (!finitef(num)) *MSVCRT__errno() = MSVCRT_EDOM;
248 return ldexpf(num, power);
251 /*********************************************************************
252 * modff (MSVCRT.@)
254 double CDECL MSVCRT_modff( float x, float *iptr )
256 return modff( x, iptr );
259 #endif
261 /*********************************************************************
262 * MSVCRT_acos (MSVCRT.@)
264 double CDECL MSVCRT_acos( double x )
266 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
267 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
268 * asin() uses a similar construction. This is bad because as x gets nearer to
269 * 1 the error in the expression "1 - x^2" can get relatively large due to
270 * cancellation. The sqrt() makes things worse. A safer way to calculate
271 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
272 return atan2(sqrt((1 - x) * (1 + x)), x);
275 /*********************************************************************
276 * MSVCRT_asin (MSVCRT.@)
278 double CDECL MSVCRT_asin( double x )
280 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
281 return atan2(x, sqrt((1 - x) * (1 + x)));
284 /*********************************************************************
285 * MSVCRT_atan (MSVCRT.@)
287 double CDECL MSVCRT_atan( double x )
289 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
290 return atan(x);
293 /*********************************************************************
294 * MSVCRT_atan2 (MSVCRT.@)
296 double CDECL MSVCRT_atan2( double x, double y )
298 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
299 return atan2(x,y);
302 /*********************************************************************
303 * MSVCRT_cos (MSVCRT.@)
305 double CDECL MSVCRT_cos( double x )
307 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
308 return cos(x);
311 /*********************************************************************
312 * MSVCRT_cosh (MSVCRT.@)
314 double CDECL MSVCRT_cosh( double x )
316 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
317 return cosh(x);
320 /*********************************************************************
321 * MSVCRT_exp (MSVCRT.@)
323 double CDECL MSVCRT_exp( double x )
325 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
326 return exp(x);
329 /*********************************************************************
330 * MSVCRT_fmod (MSVCRT.@)
332 double CDECL MSVCRT_fmod( double x, double y )
334 if (!finite(x) || !finite(y)) *MSVCRT__errno() = MSVCRT_EDOM;
335 return fmod(x,y);
338 /*********************************************************************
339 * MSVCRT_log (MSVCRT.@)
341 double CDECL MSVCRT_log( double x)
343 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
344 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
345 return log(x);
348 /*********************************************************************
349 * MSVCRT_log10 (MSVCRT.@)
351 double CDECL MSVCRT_log10( double x )
353 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
354 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
355 return log10(x);
358 /*********************************************************************
359 * MSVCRT_pow (MSVCRT.@)
361 double CDECL MSVCRT_pow( double x, double y )
363 /* FIXME: If x < 0 and y is not integral, set EDOM */
364 double z = pow(x,y);
365 if (!finite(z)) *MSVCRT__errno() = MSVCRT_EDOM;
366 return z;
369 /*********************************************************************
370 * MSVCRT_sin (MSVCRT.@)
372 double CDECL MSVCRT_sin( double x )
374 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
375 return sin(x);
378 /*********************************************************************
379 * MSVCRT_sinh (MSVCRT.@)
381 double CDECL MSVCRT_sinh( double x )
383 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
384 return sinh(x);
387 /*********************************************************************
388 * MSVCRT_sqrt (MSVCRT.@)
390 double CDECL MSVCRT_sqrt( double x )
392 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
393 return sqrt(x);
396 /*********************************************************************
397 * MSVCRT_tan (MSVCRT.@)
399 double CDECL MSVCRT_tan( double x )
401 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
402 return tan(x);
405 /*********************************************************************
406 * MSVCRT_tanh (MSVCRT.@)
408 double CDECL MSVCRT_tanh( double x )
410 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
411 return tanh(x);
415 #if defined(__GNUC__) && defined(__i386__)
417 #define FPU_DOUBLE(var) double var; \
418 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
419 #define FPU_DOUBLES(var1,var2) double var1,var2; \
420 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
421 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
423 /*********************************************************************
424 * _CIacos (MSVCRT.@)
426 double CDECL _CIacos(void)
428 FPU_DOUBLE(x);
429 return MSVCRT_acos(x);
432 /*********************************************************************
433 * _CIasin (MSVCRT.@)
435 double CDECL _CIasin(void)
437 FPU_DOUBLE(x);
438 return MSVCRT_asin(x);
441 /*********************************************************************
442 * _CIatan (MSVCRT.@)
444 double CDECL _CIatan(void)
446 FPU_DOUBLE(x);
447 return MSVCRT_atan(x);
450 /*********************************************************************
451 * _CIatan2 (MSVCRT.@)
453 double CDECL _CIatan2(void)
455 FPU_DOUBLES(x,y);
456 return MSVCRT_atan2(x,y);
459 /*********************************************************************
460 * _CIcos (MSVCRT.@)
462 double CDECL _CIcos(void)
464 FPU_DOUBLE(x);
465 return MSVCRT_cos(x);
468 /*********************************************************************
469 * _CIcosh (MSVCRT.@)
471 double CDECL _CIcosh(void)
473 FPU_DOUBLE(x);
474 return MSVCRT_cosh(x);
477 /*********************************************************************
478 * _CIexp (MSVCRT.@)
480 double CDECL _CIexp(void)
482 FPU_DOUBLE(x);
483 return MSVCRT_exp(x);
486 /*********************************************************************
487 * _CIfmod (MSVCRT.@)
489 double CDECL _CIfmod(void)
491 FPU_DOUBLES(x,y);
492 return MSVCRT_fmod(x,y);
495 /*********************************************************************
496 * _CIlog (MSVCRT.@)
498 double CDECL _CIlog(void)
500 FPU_DOUBLE(x);
501 return MSVCRT_log(x);
504 /*********************************************************************
505 * _CIlog10 (MSVCRT.@)
507 double CDECL _CIlog10(void)
509 FPU_DOUBLE(x);
510 return MSVCRT_log10(x);
513 /*********************************************************************
514 * _CIpow (MSVCRT.@)
516 double CDECL _CIpow(void)
518 FPU_DOUBLES(x,y);
519 return MSVCRT_pow(x,y);
522 /*********************************************************************
523 * _CIsin (MSVCRT.@)
525 double CDECL _CIsin(void)
527 FPU_DOUBLE(x);
528 return MSVCRT_sin(x);
531 /*********************************************************************
532 * _CIsinh (MSVCRT.@)
534 double CDECL _CIsinh(void)
536 FPU_DOUBLE(x);
537 return MSVCRT_sinh(x);
540 /*********************************************************************
541 * _CIsqrt (MSVCRT.@)
543 double CDECL _CIsqrt(void)
545 FPU_DOUBLE(x);
546 return MSVCRT_sqrt(x);
549 /*********************************************************************
550 * _CItan (MSVCRT.@)
552 double CDECL _CItan(void)
554 FPU_DOUBLE(x);
555 return MSVCRT_tan(x);
558 /*********************************************************************
559 * _CItanh (MSVCRT.@)
561 double CDECL _CItanh(void)
563 FPU_DOUBLE(x);
564 return MSVCRT_tanh(x);
567 #endif /* defined(__GNUC__) && defined(__i386__) */
569 /*********************************************************************
570 * _fpclass (MSVCRT.@)
572 int CDECL _fpclass(double num)
574 #if defined(HAVE_FPCLASS) || defined(fpclass)
575 switch (fpclass( num ))
577 #ifdef FP_SNAN
578 case FP_SNAN: return MSVCRT__FPCLASS_SNAN;
579 #endif
580 #ifdef FP_QNAN
581 case FP_QNAN: return MSVCRT__FPCLASS_QNAN;
582 #endif
583 #ifdef FP_NINF
584 case FP_NINF: return MSVCRT__FPCLASS_NINF;
585 #endif
586 #ifdef FP_PINF
587 case FP_PINF: return MSVCRT__FPCLASS_PINF;
588 #endif
589 #ifdef FP_NDENORM
590 case FP_NDENORM: return MSVCRT__FPCLASS_ND;
591 #endif
592 #ifdef FP_PDENORM
593 case FP_PDENORM: return MSVCRT__FPCLASS_PD;
594 #endif
595 #ifdef FP_NZERO
596 case FP_NZERO: return MSVCRT__FPCLASS_NZ;
597 #endif
598 #ifdef FP_PZERO
599 case FP_PZERO: return MSVCRT__FPCLASS_PZ;
600 #endif
601 #ifdef FP_NNORM
602 case FP_NNORM: return MSVCRT__FPCLASS_NN;
603 #endif
604 #ifdef FP_PNORM
605 case FP_PNORM: return MSVCRT__FPCLASS_PN;
606 #endif
607 default: return MSVCRT__FPCLASS_PN;
609 #elif defined (fpclassify)
610 switch (fpclassify( num ))
612 case FP_NAN: return MSVCRT__FPCLASS_QNAN;
613 case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
614 case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
615 case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
617 return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
618 #else
619 if (!finite(num))
620 return MSVCRT__FPCLASS_QNAN;
621 return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
622 #endif
625 /*********************************************************************
626 * _rotl (MSVCRT.@)
628 unsigned int CDECL _rotl(unsigned int num, int shift)
630 shift &= 31;
631 return (num << shift) | (num >> (32-shift));
634 /*********************************************************************
635 * _logb (MSVCRT.@)
637 double CDECL _logb(double num)
639 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
640 return logb(num);
643 /*********************************************************************
644 * _lrotl (MSVCRT.@)
646 MSVCRT_ulong CDECL MSVCRT__lrotl(MSVCRT_ulong num, int shift)
648 shift &= 0x1f;
649 return (num << shift) | (num >> (32-shift));
652 /*********************************************************************
653 * _lrotr (MSVCRT.@)
655 MSVCRT_ulong CDECL MSVCRT__lrotr(MSVCRT_ulong num, int shift)
657 shift &= 0x1f;
658 return (num >> shift) | (num << (32-shift));
661 /*********************************************************************
662 * _rotr (MSVCRT.@)
664 unsigned int CDECL _rotr(unsigned int num, int shift)
666 shift &= 0x1f;
667 return (num >> shift) | (num << (32-shift));
670 /*********************************************************************
671 * _scalb (MSVCRT.@)
673 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
675 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
676 return ldexp(num, power);
679 /*********************************************************************
680 * _hypot (MSVCRT.@)
682 double CDECL _hypot(double x, double y)
684 /* FIXME: errno handling */
685 return hypot( x, y );
688 /*********************************************************************
689 * _hypotf (MSVCRT.@)
691 float CDECL _hypotf(float x, float y)
693 /* FIXME: errno handling */
694 return hypotf( x, y );
697 /*********************************************************************
698 * ceil (MSVCRT.@)
700 double CDECL MSVCRT_ceil( double x )
702 return ceil(x);
705 /*********************************************************************
706 * floor (MSVCRT.@)
708 double CDECL MSVCRT_floor( double x )
710 return floor(x);
713 /*********************************************************************
714 * fabs (MSVCRT.@)
716 double CDECL MSVCRT_fabs( double x )
718 return fabs(x);
721 /*********************************************************************
722 * frexp (MSVCRT.@)
724 double CDECL MSVCRT_frexp( double x, int *exp )
726 return frexp( x, exp );
729 /*********************************************************************
730 * modf (MSVCRT.@)
732 double CDECL MSVCRT_modf( double x, double *iptr )
734 return modf( x, iptr );
737 /*********************************************************************
738 * _matherr (MSVCRT.@)
740 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
742 if (e)
743 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
744 e->retval);
745 else
746 TRACE("(null)\n");
747 if (MSVCRT_default_matherr_func)
748 return MSVCRT_default_matherr_func(e);
749 ERR(":Unhandled math error!\n");
750 return 0;
753 /*********************************************************************
754 * __setusermatherr (MSVCRT.@)
756 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
758 MSVCRT_default_matherr_func = func;
759 TRACE(":new matherr handler %p\n", func);
762 /**********************************************************************
763 * _statusfp (MSVCRT.@)
765 unsigned int CDECL _statusfp(void)
767 unsigned int retVal = 0;
768 #if defined(__GNUC__) && defined(__i386__)
769 unsigned int fpword;
771 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
772 if (fpword & 0x1) retVal |= MSVCRT__SW_INVALID;
773 if (fpword & 0x2) retVal |= MSVCRT__SW_DENORMAL;
774 if (fpword & 0x4) retVal |= MSVCRT__SW_ZERODIVIDE;
775 if (fpword & 0x8) retVal |= MSVCRT__SW_OVERFLOW;
776 if (fpword & 0x10) retVal |= MSVCRT__SW_UNDERFLOW;
777 if (fpword & 0x20) retVal |= MSVCRT__SW_INEXACT;
778 #else
779 FIXME(":Not implemented!\n");
780 #endif
781 return retVal;
784 /*********************************************************************
785 * _clearfp (MSVCRT.@)
787 unsigned int CDECL _clearfp(void)
789 unsigned int retVal = _statusfp();
790 #if defined(__GNUC__) && defined(__i386__)
791 __asm__ __volatile__( "fnclex" );
792 #else
793 FIXME(":Not Implemented\n");
794 #endif
795 return retVal;
798 /*********************************************************************
799 * __fpecode (MSVCRT.@)
801 int * CDECL __fpecode(void)
803 return &msvcrt_get_thread_data()->fpecode;
806 /*********************************************************************
807 * ldexp (MSVCRT.@)
809 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
811 double z = ldexp(num,exp);
813 if (!finite(z))
814 *MSVCRT__errno() = MSVCRT_ERANGE;
815 else if (z == 0 && signbit(z))
816 z = 0.0; /* Convert -0 -> +0 */
817 return z;
820 /*********************************************************************
821 * _cabs (MSVCRT.@)
823 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
825 return sqrt(num.x * num.x + num.y * num.y);
828 /*********************************************************************
829 * _chgsign (MSVCRT.@)
831 double CDECL _chgsign(double num)
833 /* FIXME: +-infinity,Nan not tested */
834 return -num;
837 /*********************************************************************
838 * _control87 (MSVCRT.@)
840 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
842 #if defined(__GNUC__) && defined(__i386__)
843 unsigned int fpword = 0;
844 unsigned int flags = 0;
846 TRACE("(%08x, %08x): Called\n", newval, mask);
848 /* Get fp control word */
849 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) : );
851 TRACE("Control word before : %08x\n", fpword);
853 /* Convert into mask constants */
854 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
855 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
856 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
857 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
858 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
859 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
860 switch(fpword & 0xC00) {
861 case 0xC00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
862 case 0x800: flags |= MSVCRT__RC_UP; break;
863 case 0x400: flags |= MSVCRT__RC_DOWN; break;
865 switch(fpword & 0x300) {
866 case 0x0: flags |= MSVCRT__PC_24; break;
867 case 0x200: flags |= MSVCRT__PC_53; break;
868 case 0x300: flags |= MSVCRT__PC_64; break;
870 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
872 /* Mask with parameters */
873 flags = (flags & ~mask) | (newval & mask);
875 /* Convert (masked) value back to fp word */
876 fpword = 0;
877 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
878 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
879 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
880 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
881 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
882 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
883 switch(flags & (MSVCRT__RC_UP | MSVCRT__RC_DOWN)) {
884 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xC00; break;
885 case MSVCRT__RC_UP: fpword |= 0x800; break;
886 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
888 switch (flags & (MSVCRT__PC_24 | MSVCRT__PC_53)) {
889 case MSVCRT__PC_64: fpword |= 0x300; break;
890 case MSVCRT__PC_53: fpword |= 0x200; break;
891 case MSVCRT__PC_24: fpword |= 0x0; break;
893 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
895 TRACE("Control word after : %08x\n", fpword);
897 /* Put fp control word */
898 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
900 return flags;
901 #else
902 FIXME(":Not Implemented!\n");
903 return 0;
904 #endif
907 /*********************************************************************
908 * _controlfp (MSVCRT.@)
910 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
912 #ifdef __i386__
913 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
914 #else
915 FIXME(":Not Implemented!\n");
916 return 0;
917 #endif
920 /*********************************************************************
921 * _controlfp_s (MSVCRT.@)
923 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
925 #ifdef __i386__
926 unsigned int flags;
928 FIXME("(%p %u %u) semi-stub\n", cur, newval, mask);
930 flags = _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
932 if(cur)
933 *cur = flags;
935 return 0;
936 #else
937 FIXME(":Not Implemented!\n");
938 return 0;
939 #endif
942 /*********************************************************************
943 * _copysign (MSVCRT.@)
945 double CDECL _copysign(double num, double sign)
947 /* FIXME: Behaviour for Nan/Inf? */
948 if (sign < 0.0)
949 return num < 0.0 ? num : -num;
950 return num < 0.0 ? -num : num;
953 /*********************************************************************
954 * _finite (MSVCRT.@)
956 int CDECL _finite(double num)
958 return (finite(num)?1:0); /* See comment for _isnan() */
961 /*********************************************************************
962 * _fpreset (MSVCRT.@)
964 void CDECL _fpreset(void)
966 #if defined(__GNUC__) && defined(__i386__)
967 __asm__ __volatile__( "fninit" );
968 #else
969 FIXME(":Not Implemented!\n");
970 #endif
973 /*********************************************************************
974 * _isnan (MSVCRT.@)
976 INT CDECL _isnan(double num)
978 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
979 * Do the same, as the result may be used in calculations
981 return isnan(num) ? 1 : 0;
984 /*********************************************************************
985 * _j0 (MSVCRT.@)
987 double CDECL _j0(double num)
989 /* FIXME: errno handling */
990 return j0(num);
993 /*********************************************************************
994 * _j1 (MSVCRT.@)
996 double CDECL _j1(double num)
998 /* FIXME: errno handling */
999 return j1(num);
1002 /*********************************************************************
1003 * jn (MSVCRT.@)
1005 double CDECL _jn(int n, double num)
1007 /* FIXME: errno handling */
1008 return jn(n, num);
1011 /*********************************************************************
1012 * _y0 (MSVCRT.@)
1014 double CDECL _y0(double num)
1016 double retval;
1017 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1018 retval = y0(num);
1019 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
1021 *MSVCRT__errno() = MSVCRT_EDOM;
1022 retval = sqrt(-1);
1024 return retval;
1027 /*********************************************************************
1028 * _y1 (MSVCRT.@)
1030 double CDECL _y1(double num)
1032 double retval;
1033 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1034 retval = y1(num);
1035 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
1037 *MSVCRT__errno() = MSVCRT_EDOM;
1038 retval = sqrt(-1);
1040 return retval;
1043 /*********************************************************************
1044 * _yn (MSVCRT.@)
1046 double CDECL _yn(int order, double num)
1048 double retval;
1049 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1050 retval = yn(order,num);
1051 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
1053 *MSVCRT__errno() = MSVCRT_EDOM;
1054 retval = sqrt(-1);
1056 return retval;
1059 /*********************************************************************
1060 * _nextafter (MSVCRT.@)
1062 double CDECL _nextafter(double num, double next)
1064 double retval;
1065 if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1066 retval = nextafter(num,next);
1067 return retval;
1070 /*********************************************************************
1071 * _ecvt (MSVCRT.@)
1073 char * CDECL _ecvt( double number, int ndigits, int *decpt, int *sign )
1075 int prec, len;
1076 thread_data_t *data = msvcrt_get_thread_data();
1077 /* FIXME: check better for overflow (native supports over 300 chars's) */
1078 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1079 * 4 for exponent and one for
1080 * terminating '\0' */
1081 if (!data->efcvt_buffer)
1082 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1084 if( number < 0) {
1085 *sign = TRUE;
1086 number = -number;
1087 } else
1088 *sign = FALSE;
1089 /* handle cases with zero ndigits or less */
1090 prec = ndigits;
1091 if( prec < 1) prec = 2;
1092 len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1093 /* take the decimal "point away */
1094 if( prec != 1)
1095 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1096 /* take the exponential "e" out */
1097 data->efcvt_buffer[ prec] = '\0';
1098 /* read the exponent */
1099 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1100 (*decpt)++;
1101 /* adjust for some border cases */
1102 if( data->efcvt_buffer[0] == '0')/* value is zero */
1103 *decpt = 0;
1104 /* handle cases with zero ndigits or less */
1105 if( ndigits < 1){
1106 if( data->efcvt_buffer[ 0] >= '5')
1107 (*decpt)++;
1108 data->efcvt_buffer[ 0] = '\0';
1110 TRACE("out=\"%s\"\n",data->efcvt_buffer);
1111 return data->efcvt_buffer;
1114 /***********************************************************************
1115 * _fcvt (MSVCRT.@)
1117 char * CDECL _fcvt( double number, int ndigits, int *decpt, int *sign )
1119 thread_data_t *data = msvcrt_get_thread_data();
1120 int stop, dec1, dec2;
1121 char *ptr1, *ptr2, *first;
1122 char buf[80]; /* ought to be enough */
1124 if (!data->efcvt_buffer)
1125 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1127 if (number < 0)
1129 *sign = 1;
1130 number = -number;
1131 } else *sign = 0;
1133 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1134 ptr1 = buf;
1135 ptr2 = data->efcvt_buffer;
1136 first = NULL;
1137 dec1 = 0;
1138 dec2 = 0;
1140 /* For numbers below the requested resolution, work out where
1141 the decimal point will be rather than finding it in the string */
1142 if (number < 1.0 && number > 0.0) {
1143 dec2 = log10(number + 1e-10);
1144 if (-dec2 <= ndigits) dec2 = 0;
1147 /* If requested digits is zero or less, we will need to truncate
1148 * the returned string */
1149 if (ndigits < 1) {
1150 stop = strlen(buf) + ndigits;
1151 } else {
1152 stop = strlen(buf);
1155 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1156 while (*ptr1 != '\0' && *ptr1 != '.') {
1157 if (!first) first = ptr2;
1158 if ((ptr1 - buf) < stop) {
1159 *ptr2++ = *ptr1++;
1160 } else {
1161 ptr1++;
1163 dec1++;
1166 if (ndigits > 0) {
1167 ptr1++;
1168 if (!first) {
1169 while (*ptr1 == '0') { /* Process leading zeroes */
1170 *ptr2++ = *ptr1++;
1171 dec1--;
1174 while (*ptr1 != '\0') {
1175 if (!first) first = ptr2;
1176 *ptr2++ = *ptr1++;
1180 *ptr2 = '\0';
1182 /* We never found a non-zero digit, then our number is either
1183 * smaller than the requested precision, or 0.0 */
1184 if (!first) {
1185 if (number > 0.0) {
1186 first = ptr2;
1187 } else {
1188 first = data->efcvt_buffer;
1189 dec1 = 0;
1193 *decpt = dec2 ? dec2 : dec1;
1194 return first;
1197 /***********************************************************************
1198 * _gcvt (MSVCRT.@)
1200 char * CDECL _gcvt( double number, int ndigit, char *buff )
1202 if(!buff) {
1203 *MSVCRT__errno() = MSVCRT_EINVAL;
1204 return NULL;
1207 if(ndigit < 0) {
1208 *MSVCRT__errno() = MSVCRT_ERANGE;
1209 return NULL;
1212 MSVCRT_sprintf(buff, "%.*g", ndigit, number);
1213 return buff;
1216 /***********************************************************************
1217 * _gcvt_s (MSVCRT.@)
1219 int CDECL _gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
1221 int len;
1223 if(!buff) {
1224 *MSVCRT__errno() = MSVCRT_EINVAL;
1225 return MSVCRT_EINVAL;
1228 if( digits<0 || digits>=size) {
1229 if(size)
1230 buff[0] = '\0';
1232 *MSVCRT__errno() = MSVCRT_ERANGE;
1233 return MSVCRT_ERANGE;
1236 len = MSVCRT__scprintf("%.*g", digits, number);
1237 if(len > size) {
1238 buff[0] = '\0';
1239 *MSVCRT__errno() = MSVCRT_ERANGE;
1240 return MSVCRT_ERANGE;
1243 MSVCRT_sprintf(buff, "%.*g", digits, number);
1244 return 0;
1247 #include <stdlib.h> /* div_t, ldiv_t */
1249 /*********************************************************************
1250 * div (MSVCRT.@)
1251 * VERSION
1252 * [i386] Windows binary compatible - returns the struct in eax/edx.
1254 #ifdef __i386__
1255 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1257 div_t dt = div(num,denom);
1258 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1260 #else
1261 /*********************************************************************
1262 * div (MSVCRT.@)
1263 * VERSION
1264 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1266 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1268 div_t dt = div(num,denom);
1269 MSVCRT_div_t ret;
1270 ret.quot = dt.quot;
1271 ret.rem = dt.rem;
1273 return ret;
1276 #endif /* ifdef __i386__ */
1279 /*********************************************************************
1280 * ldiv (MSVCRT.@)
1281 * VERSION
1282 * [i386] Windows binary compatible - returns the struct in eax/edx.
1284 #ifdef __i386__
1285 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1287 ldiv_t ldt = ldiv(num,denom);
1288 return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1290 #else
1291 /*********************************************************************
1292 * ldiv (MSVCRT.@)
1293 * VERSION
1294 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1296 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1298 ldiv_t result = ldiv(num,denom);
1300 MSVCRT_ldiv_t ret;
1301 ret.quot = result.quot;
1302 ret.rem = result.rem;
1304 return ret;
1306 #endif /* ifdef __i386__ */
1308 #ifdef __i386__
1310 /*********************************************************************
1311 * _adjust_fdiv (MSVCRT.@)
1312 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1314 int MSVCRT__adjust_fdiv = 0;
1316 /***********************************************************************
1317 * _adj_fdiv_m16i (MSVCRT.@)
1319 * NOTE
1320 * I _think_ this function is intended to work around the Pentium
1321 * fdiv bug.
1323 void __stdcall _adj_fdiv_m16i( short arg )
1325 TRACE("(): stub\n");
1328 /***********************************************************************
1329 * _adj_fdiv_m32 (MSVCRT.@)
1331 * NOTE
1332 * I _think_ this function is intended to work around the Pentium
1333 * fdiv bug.
1335 void __stdcall _adj_fdiv_m32( unsigned int arg )
1337 TRACE("(): stub\n");
1340 /***********************************************************************
1341 * _adj_fdiv_m32i (MSVCRT.@)
1343 * NOTE
1344 * I _think_ this function is intended to work around the Pentium
1345 * fdiv bug.
1347 void __stdcall _adj_fdiv_m32i( int arg )
1349 TRACE("(): stub\n");
1352 /***********************************************************************
1353 * _adj_fdiv_m64 (MSVCRT.@)
1355 * NOTE
1356 * I _think_ this function is intended to work around the Pentium
1357 * fdiv bug.
1359 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1361 TRACE("(): stub\n");
1364 /***********************************************************************
1365 * _adj_fdiv_r (MSVCRT.@)
1366 * FIXME
1367 * This function is likely to have the wrong number of arguments.
1369 * NOTE
1370 * I _think_ this function is intended to work around the Pentium
1371 * fdiv bug.
1373 void _adj_fdiv_r(void)
1375 TRACE("(): stub\n");
1378 /***********************************************************************
1379 * _adj_fdivr_m16i (MSVCRT.@)
1381 * NOTE
1382 * I _think_ this function is intended to work around the Pentium
1383 * fdiv bug.
1385 void __stdcall _adj_fdivr_m16i( short arg )
1387 TRACE("(): stub\n");
1390 /***********************************************************************
1391 * _adj_fdivr_m32 (MSVCRT.@)
1393 * NOTE
1394 * I _think_ this function is intended to work around the Pentium
1395 * fdiv bug.
1397 void __stdcall _adj_fdivr_m32( unsigned int arg )
1399 TRACE("(): stub\n");
1402 /***********************************************************************
1403 * _adj_fdivr_m32i (MSVCRT.@)
1405 * NOTE
1406 * I _think_ this function is intended to work around the Pentium
1407 * fdiv bug.
1409 void __stdcall _adj_fdivr_m32i( int arg )
1411 TRACE("(): stub\n");
1414 /***********************************************************************
1415 * _adj_fdivr_m64 (MSVCRT.@)
1417 * NOTE
1418 * I _think_ this function is intended to work around the Pentium
1419 * fdiv bug.
1421 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1423 TRACE("(): stub\n");
1426 /***********************************************************************
1427 * _adj_fpatan (MSVCRT.@)
1428 * FIXME
1429 * This function is likely to have the wrong number of arguments.
1431 * NOTE
1432 * I _think_ this function is intended to work around the Pentium
1433 * fdiv bug.
1435 void _adj_fpatan(void)
1437 TRACE("(): stub\n");
1440 /***********************************************************************
1441 * _adj_fprem (MSVCRT.@)
1442 * FIXME
1443 * This function is likely to have the wrong number of arguments.
1445 * NOTE
1446 * I _think_ this function is intended to work around the Pentium
1447 * fdiv bug.
1449 void _adj_fprem(void)
1451 TRACE("(): stub\n");
1454 /***********************************************************************
1455 * _adj_fprem1 (MSVCRT.@)
1456 * FIXME
1457 * This function is likely to have the wrong number of arguments.
1459 * NOTE
1460 * I _think_ this function is intended to work around the Pentium
1461 * fdiv bug.
1463 void _adj_fprem1(void)
1465 TRACE("(): stub\n");
1468 /***********************************************************************
1469 * _adj_fptan (MSVCRT.@)
1470 * FIXME
1471 * This function is likely to have the wrong number of arguments.
1473 * NOTE
1474 * I _think_ this function is intended to work around the Pentium
1475 * fdiv bug.
1477 void _adj_fptan(void)
1479 TRACE("(): stub\n");
1482 /***********************************************************************
1483 * _safe_fdiv (MSVCRT.@)
1484 * FIXME
1485 * This function is likely to have the wrong number of arguments.
1487 * NOTE
1488 * I _think_ this function is intended to work around the Pentium
1489 * fdiv bug.
1491 void _safe_fdiv(void)
1493 TRACE("(): stub\n");
1496 /***********************************************************************
1497 * _safe_fdivr (MSVCRT.@)
1498 * FIXME
1499 * This function is likely to have the wrong number of arguments.
1501 * NOTE
1502 * I _think_ this function is intended to work around the Pentium
1503 * fdiv bug.
1505 void _safe_fdivr(void)
1507 TRACE("(): stub\n");
1510 /***********************************************************************
1511 * _safe_fprem (MSVCRT.@)
1512 * FIXME
1513 * This function is likely to have the wrong number of arguments.
1515 * NOTE
1516 * I _think_ this function is intended to work around the Pentium
1517 * fdiv bug.
1519 void _safe_fprem(void)
1521 TRACE("(): stub\n");
1524 /***********************************************************************
1525 * _safe_fprem1 (MSVCRT.@)
1527 * FIXME
1528 * This function is likely to have the wrong number of arguments.
1530 * NOTE
1531 * I _think_ this function is intended to work around the Pentium
1532 * fdiv bug.
1534 void _safe_fprem1(void)
1536 TRACE("(): stub\n");
1539 #endif /* __i386__ */