msvcrt: Call cdecl functions with empty FPU stack in _CI* functions.
[wine.git] / dlls / msvcrt / math.c
blob0627bfa4b1d8959f667d2eecb2070e75def565a0
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"
21 #include "wine/port.h"
23 #include <stdio.h>
24 #define __USE_ISOC9X 1
25 #define __USE_ISOC99 1
26 #include <math.h>
27 #ifdef HAVE_IEEEFP_H
28 #include <ieeefp.h>
29 #endif
31 #include "msvcrt.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 #ifndef HAVE_FINITEF
38 #define finitef(x) isfinite(x)
39 #endif
41 #ifndef HAVE_ISNANF
42 #ifdef HAVE_ISNAN
43 #define isnanf(x) isnan(x)
44 #else
45 #define isnanf(x) 0
46 #endif
47 #endif
49 /* FIXME: Does not work with -NAN and -0. */
50 #ifndef signbit
51 #define signbit(x) ((x) < 0)
52 #endif
54 #define _DOMAIN 1 /* domain error in argument */
55 #define _SING 2 /* singularity */
56 #define _OVERFLOW 3 /* range overflow */
57 #define _UNDERFLOW 4 /* range underflow */
59 typedef int (CDECL *MSVCRT_matherr_func)(struct MSVCRT__exception *);
60 typedef double LDOUBLE; /* long double is just a double */
62 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
64 static BOOL sse2_supported;
65 static BOOL sse2_enabled;
67 void msvcrt_init_math(void)
69 sse2_supported = sse2_enabled = IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );
72 /*********************************************************************
73 * _matherr (MSVCRT.@)
75 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
77 int ret;
79 if (e)
80 TRACE("(%p = {%d, \"%s\", %g, %g, %g})\n", e, e->type, e->name, e->arg1, e->arg2, e->retval);
81 else
82 TRACE("(null)\n");
84 if (MSVCRT_default_matherr_func)
86 ret = MSVCRT_default_matherr_func(e);
87 if (ret) return ret;
90 switch (e->type)
92 case _DOMAIN:
93 *MSVCRT__errno() = MSVCRT_EDOM;
94 break;
95 case _SING:
96 case _OVERFLOW:
97 *MSVCRT__errno() = MSVCRT_ERANGE;
98 break;
99 case _UNDERFLOW:
100 /* don't set errno */
101 break;
102 default:
103 ERR("Unhandled math error!\n");
106 return 0;
109 /*********************************************************************
110 * __setusermatherr (MSVCRT.@)
112 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
114 MSVCRT_default_matherr_func = func;
115 TRACE("new matherr handler %p\n", func);
118 static inline void math_error(int type, const char *name, double arg1, double arg2, double retval)
120 struct MSVCRT__exception exception = {type, (char *)name, arg1, arg2, retval};
121 MSVCRT__matherr(&exception);
124 /*********************************************************************
125 * _set_SSE2_enable (MSVCRT.@)
127 int CDECL MSVCRT__set_SSE2_enable(int flag)
129 sse2_enabled = flag && sse2_supported;
130 return sse2_enabled;
133 #ifdef _WIN64
134 /*********************************************************************
135 * _set_FMA3_enable (MSVCR120.@)
137 int CDECL MSVCRT__set_FMA3_enable(int flag)
139 FIXME("(%x) stub\n", flag);
140 return 0;
142 #endif
144 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || _MSVCR_VER>=120
146 /*********************************************************************
147 * _chgsignf (MSVCRT.@)
149 float CDECL MSVCRT__chgsignf( float num )
151 /* FIXME: +-infinity,Nan not tested */
152 return -num;
155 /*********************************************************************
156 * _copysignf (MSVCRT.@)
158 float CDECL MSVCRT__copysignf( float num, float sign )
160 if (signbit(sign))
161 return signbit(num) ? num : -num;
162 return signbit(num) ? -num : num;
165 /*********************************************************************
166 * _nextafterf (MSVCRT.@)
168 float CDECL MSVCRT__nextafterf( float num, float next )
170 if (!finitef(num) || !finitef(next)) *MSVCRT__errno() = MSVCRT_EDOM;
171 return nextafterf( num, next );
174 #endif
175 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
177 /*********************************************************************
178 * _finitef (MSVCRT.@)
180 int CDECL MSVCRT__finitef( float num )
182 return finitef(num) != 0; /* See comment for _isnan() */
185 /*********************************************************************
186 * _isnanf (MSVCRT.@)
188 INT CDECL MSVCRT__isnanf( float num )
190 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
191 * Do the same, as the result may be used in calculations
193 return isnanf(num) != 0;
196 /*********************************************************************
197 * _logbf (MSVCRT.@)
199 float CDECL MSVCRT__logbf( float num )
201 float ret = logbf(num);
202 if (isnanf(num)) math_error(_DOMAIN, "_logbf", num, 0, ret);
203 else if (!num) math_error(_SING, "_logbf", num, 0, ret);
204 return ret;
207 /*********************************************************************
208 * MSVCRT_acosf (MSVCRT.@)
210 float CDECL MSVCRT_acosf( float x )
212 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
213 * asin() uses a similar construction. This is bad because as x gets nearer to
214 * 1 the error in the expression "1 - x^2" can get relatively large due to
215 * cancellation. The sqrt() makes things worse. A safer way to calculate
216 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
217 float ret = atan2f(sqrtf((1 - x) * (1 + x)), x);
218 if (x < -1.0 || x > 1.0 || !finitef(x)) math_error(_DOMAIN, "acosf", x, 0, ret);
219 return ret;
222 /*********************************************************************
223 * MSVCRT_asinf (MSVCRT.@)
225 float CDECL MSVCRT_asinf( float x )
227 float ret = atan2f(x, sqrtf((1 - x) * (1 + x)));
228 if (x < -1.0 || x > 1.0 || !finitef(x)) math_error(_DOMAIN, "asinf", x, 0, ret);
229 return ret;
232 /*********************************************************************
233 * MSVCRT_atanf (MSVCRT.@)
235 float CDECL MSVCRT_atanf( float x )
237 float ret = atanf(x);
238 if (!finitef(x)) math_error(_DOMAIN, "atanf", x, 0, ret);
239 return ret;
242 /*********************************************************************
243 * MSVCRT_atan2f (MSVCRT.@)
245 float CDECL MSVCRT_atan2f( float x, float y )
247 float ret = atan2f(x, y);
248 if (isnanf(x)) math_error(_DOMAIN, "atan2f", x, y, ret);
249 return ret;
252 /*********************************************************************
253 * MSVCRT_cosf (MSVCRT.@)
255 float CDECL MSVCRT_cosf( float x )
257 float ret = cosf(x);
258 if (!finitef(x)) math_error(_DOMAIN, "cosf", x, 0, ret);
259 return ret;
262 /*********************************************************************
263 * MSVCRT_coshf (MSVCRT.@)
265 float CDECL MSVCRT_coshf( float x )
267 float ret = coshf(x);
268 if (isnanf(x)) math_error(_DOMAIN, "coshf", x, 0, ret);
269 return ret;
272 /*********************************************************************
273 * MSVCRT_expf (MSVCRT.@)
275 float CDECL MSVCRT_expf( float x )
277 float ret = expf(x);
278 if (isnanf(x)) math_error(_DOMAIN, "expf", x, 0, ret);
279 else if (finitef(x) && !ret) math_error(_UNDERFLOW, "expf", x, 0, ret);
280 else if (finitef(x) && !finitef(ret)) math_error(_OVERFLOW, "expf", x, 0, ret);
281 return ret;
284 /*********************************************************************
285 * MSVCRT_fmodf (MSVCRT.@)
287 float CDECL MSVCRT_fmodf( float x, float y )
289 float ret = fmodf(x, y);
290 if (!finitef(x) || !finitef(y)) math_error(_DOMAIN, "fmodf", x, 0, ret);
291 return ret;
294 /*********************************************************************
295 * MSVCRT_logf (MSVCRT.@)
297 float CDECL MSVCRT_logf( float x )
299 float ret = logf(x);
300 if (x < 0.0) math_error(_DOMAIN, "logf", x, 0, ret);
301 else if (x == 0.0) math_error(_SING, "logf", x, 0, ret);
302 return ret;
305 /*********************************************************************
306 * MSVCRT_log10f (MSVCRT.@)
308 float CDECL MSVCRT_log10f( float x )
310 float ret = log10f(x);
311 if (x < 0.0) math_error(_DOMAIN, "log10f", x, 0, ret);
312 else if (x == 0.0) math_error(_SING, "log10f", x, 0, ret);
313 return ret;
316 /*********************************************************************
317 * MSVCRT_powf (MSVCRT.@)
319 float CDECL MSVCRT_powf( float x, float y )
321 float z = powf(x,y);
322 if (x < 0 && y != floorf(y)) math_error(_DOMAIN, "powf", x, y, z);
323 else if (!x && finitef(y) && y < 0) math_error(_SING, "powf", x, y, z);
324 else if (finitef(x) && finitef(y) && !finitef(z)) math_error(_OVERFLOW, "powf", x, y, z);
325 else if (x && finitef(x) && finitef(y) && !z) math_error(_UNDERFLOW, "powf", x, y, z);
326 return z;
329 /*********************************************************************
330 * MSVCRT_sinf (MSVCRT.@)
332 float CDECL MSVCRT_sinf( float x )
334 float ret = sinf(x);
335 if (!finitef(x)) math_error(_DOMAIN, "sinf", x, 0, ret);
336 return ret;
339 /*********************************************************************
340 * MSVCRT_sinhf (MSVCRT.@)
342 float CDECL MSVCRT_sinhf( float x )
344 float ret = sinhf(x);
345 if (isnanf(x)) math_error(_DOMAIN, "sinhf", x, 0, ret);
346 return ret;
349 /*********************************************************************
350 * MSVCRT_sqrtf (MSVCRT.@)
352 float CDECL MSVCRT_sqrtf( float x )
354 float ret = sqrtf(x);
355 if (x < 0.0) math_error(_DOMAIN, "sqrtf", x, 0, ret);
356 return ret;
359 /*********************************************************************
360 * MSVCRT_tanf (MSVCRT.@)
362 float CDECL MSVCRT_tanf( float x )
364 float ret = tanf(x);
365 if (!finitef(x)) math_error(_DOMAIN, "tanf", x, 0, ret);
366 return ret;
369 /*********************************************************************
370 * MSVCRT_tanhf (MSVCRT.@)
372 float CDECL MSVCRT_tanhf( float x )
374 float ret = tanhf(x);
375 if (!finitef(x)) math_error(_DOMAIN, "tanhf", x, 0, ret);
376 return ret;
379 /*********************************************************************
380 * ceilf (MSVCRT.@)
382 float CDECL MSVCRT_ceilf( float x )
384 return ceilf(x);
387 /*********************************************************************
388 * fabsf (MSVCRT.@)
390 float CDECL MSVCRT_fabsf( float x )
392 return fabsf(x);
395 /*********************************************************************
396 * floorf (MSVCRT.@)
398 float CDECL MSVCRT_floorf( float x )
400 return floorf(x);
403 /*********************************************************************
404 * frexpf (MSVCRT.@)
406 float CDECL MSVCRT_frexpf( float x, int *exp )
408 return frexpf( x, exp );
411 /*********************************************************************
412 * modff (MSVCRT.@)
414 float CDECL MSVCRT_modff( float x, float *iptr )
416 return modff( x, iptr );
419 #endif
421 /*********************************************************************
422 * MSVCRT_acos (MSVCRT.@)
424 double CDECL MSVCRT_acos( double x )
426 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
427 * asin() uses a similar construction. This is bad because as x gets nearer to
428 * 1 the error in the expression "1 - x^2" can get relatively large due to
429 * cancellation. The sqrt() makes things worse. A safer way to calculate
430 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
431 double ret = atan2(sqrt((1 - x) * (1 + x)), x);
432 if (x < -1.0 || x > 1.0 || !isfinite(x)) math_error(_DOMAIN, "acos", x, 0, ret);
433 return ret;
436 /*********************************************************************
437 * MSVCRT_asin (MSVCRT.@)
439 double CDECL MSVCRT_asin( double x )
441 double ret = atan2(x, sqrt((1 - x) * (1 + x)));
442 if (x < -1.0 || x > 1.0 || !isfinite(x)) math_error(_DOMAIN, "asin", x, 0, ret);
443 return ret;
446 /*********************************************************************
447 * MSVCRT_atan (MSVCRT.@)
449 double CDECL MSVCRT_atan( double x )
451 double ret = atan(x);
452 if (isnan(x)) math_error(_DOMAIN, "atan", x, 0, ret);
453 return ret;
456 /*********************************************************************
457 * MSVCRT_atan2 (MSVCRT.@)
459 double CDECL MSVCRT_atan2( double x, double y )
461 double ret = atan2(x, y);
462 if (isnan(x)) math_error(_DOMAIN, "atan2", x, y, ret);
463 return ret;
466 /*********************************************************************
467 * MSVCRT_cos (MSVCRT.@)
469 double CDECL MSVCRT_cos( double x )
471 double ret = cos(x);
472 if (!isfinite(x)) math_error(_DOMAIN, "cos", x, 0, ret);
473 return ret;
476 /*********************************************************************
477 * MSVCRT_cosh (MSVCRT.@)
479 double CDECL MSVCRT_cosh( double x )
481 double ret = cosh(x);
482 if (isnan(x)) math_error(_DOMAIN, "cosh", x, 0, ret);
483 return ret;
486 /*********************************************************************
487 * MSVCRT_exp (MSVCRT.@)
489 double CDECL MSVCRT_exp( double x )
491 double ret = exp(x);
492 if (isnan(x)) math_error(_DOMAIN, "exp", x, 0, ret);
493 else if (isfinite(x) && !ret) math_error(_UNDERFLOW, "exp", x, 0, ret);
494 else if (isfinite(x) && !isfinite(ret)) math_error(_OVERFLOW, "exp", x, 0, ret);
495 return ret;
498 /*********************************************************************
499 * MSVCRT_fmod (MSVCRT.@)
501 double CDECL MSVCRT_fmod( double x, double y )
503 double ret = fmod(x, y);
504 if (!isfinite(x) || !isfinite(y)) math_error(_DOMAIN, "fmod", x, y, ret);
505 return ret;
508 /*********************************************************************
509 * MSVCRT_log (MSVCRT.@)
511 double CDECL MSVCRT_log( double x )
513 double ret = log(x);
514 if (x < 0.0) math_error(_DOMAIN, "log", x, 0, ret);
515 else if (x == 0.0) math_error(_SING, "log", x, 0, ret);
516 return ret;
519 /*********************************************************************
520 * MSVCRT_log10 (MSVCRT.@)
522 double CDECL MSVCRT_log10( double x )
524 double ret = log10(x);
525 if (x < 0.0) math_error(_DOMAIN, "log10", x, 0, ret);
526 else if (x == 0.0) math_error(_SING, "log10", x, 0, ret);
527 return ret;
530 /*********************************************************************
531 * MSVCRT_pow (MSVCRT.@)
533 double CDECL MSVCRT_pow( double x, double y )
535 double z = pow(x,y);
536 if (x < 0 && y != floor(y)) math_error(_DOMAIN, "pow", x, y, z);
537 else if (!x && isfinite(y) && y < 0) math_error(_SING, "pow", x, y, z);
538 else if (isfinite(x) && isfinite(y) && !isfinite(z)) math_error(_OVERFLOW, "pow", x, y, z);
539 else if (x && isfinite(x) && isfinite(y) && !z) math_error(_UNDERFLOW, "pow", x, y, z);
540 return z;
543 /*********************************************************************
544 * MSVCRT_sin (MSVCRT.@)
546 double CDECL MSVCRT_sin( double x )
548 double ret = sin(x);
549 if (!isfinite(x)) math_error(_DOMAIN, "sin", x, 0, ret);
550 return ret;
553 /*********************************************************************
554 * MSVCRT_sinh (MSVCRT.@)
556 double CDECL MSVCRT_sinh( double x )
558 double ret = sinh(x);
559 if (isnan(x)) math_error(_DOMAIN, "sinh", x, 0, ret);
560 return ret;
563 /*********************************************************************
564 * MSVCRT_sqrt (MSVCRT.@)
566 double CDECL MSVCRT_sqrt( double x )
568 double ret = sqrt(x);
569 if (x < 0.0) math_error(_DOMAIN, "sqrt", x, 0, ret);
570 return ret;
573 /*********************************************************************
574 * MSVCRT_tan (MSVCRT.@)
576 double CDECL MSVCRT_tan( double x )
578 double ret = tan(x);
579 if (!isfinite(x)) math_error(_DOMAIN, "tan", x, 0, ret);
580 return ret;
583 /*********************************************************************
584 * MSVCRT_tanh (MSVCRT.@)
586 double CDECL MSVCRT_tanh( double x )
588 double ret = tanh(x);
589 if (isnan(x)) math_error(_DOMAIN, "tanh", x, 0, ret);
590 return ret;
594 #if defined(__GNUC__) && defined(__i386__)
596 #define CREATE_FPU_FUNC1(name, call) \
597 __ASM_GLOBAL_FUNC(name, \
598 "pushl %ebp\n\t" \
599 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
600 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
601 "movl %esp, %ebp\n\t" \
602 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
603 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
604 "fstpl (%esp)\n\t" /* store function argument */ \
605 "fwait\n\t" \
606 "movl $1, %ecx\n\t" /* empty FPU stack */ \
607 "1:\n\t" \
608 "fxam\n\t" \
609 "fstsw %ax\n\t" \
610 "and $0x4500, %ax\n\t" \
611 "cmp $0x4100, %ax\n\t" \
612 "je 2f\n\t" \
613 "fstpl (%esp,%ecx,8)\n\t" \
614 "fwait\n\t" \
615 "incl %ecx\n\t" \
616 "jmp 1b\n\t" \
617 "2:\n\t" \
618 "movl %ecx, -4(%ebp)\n\t" \
619 "call " __ASM_NAME( #call ) "\n\t" \
620 "movl -4(%ebp), %ecx\n\t" \
621 "fstpl (%esp)\n\t" /* save result */ \
622 "3:\n\t" /* restore FPU stack */ \
623 "decl %ecx\n\t" \
624 "fldl (%esp,%ecx,8)\n\t" \
625 "cmpl $0, %ecx\n\t" \
626 "jne 3b\n\t" \
627 "leave\n\t" \
628 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
629 __ASM_CFI(".cfi_same_value %ebp\n\t") \
630 "ret")
632 #define CREATE_FPU_FUNC2(name, call) \
633 __ASM_GLOBAL_FUNC(name, \
634 "pushl %ebp\n\t" \
635 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
636 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
637 "movl %esp, %ebp\n\t" \
638 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
639 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
640 "fstpl 8(%esp)\n\t" /* store function argument */ \
641 "fwait\n\t" \
642 "fstpl (%esp)\n\t" \
643 "fwait\n\t" \
644 "movl $2, %ecx\n\t" /* empty FPU stack */ \
645 "1:\n\t" \
646 "fxam\n\t" \
647 "fstsw %ax\n\t" \
648 "and $0x4500, %ax\n\t" \
649 "cmp $0x4100, %ax\n\t" \
650 "je 2f\n\t" \
651 "fstpl (%esp,%ecx,8)\n\t" \
652 "fwait\n\t" \
653 "incl %ecx\n\t" \
654 "jmp 1b\n\t" \
655 "2:\n\t" \
656 "movl %ecx, -4(%ebp)\n\t" \
657 "call " __ASM_NAME( #call ) "\n\t" \
658 "movl -4(%ebp), %ecx\n\t" \
659 "fstpl 8(%esp)\n\t" /* save result */ \
660 "3:\n\t" /* restore FPU stack */ \
661 "decl %ecx\n\t" \
662 "fldl (%esp,%ecx,8)\n\t" \
663 "cmpl $1, %ecx\n\t" \
664 "jne 3b\n\t" \
665 "leave\n\t" \
666 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
667 __ASM_CFI(".cfi_same_value %ebp\n\t") \
668 "ret")
670 CREATE_FPU_FUNC1(_CIacos, MSVCRT_acos)
671 CREATE_FPU_FUNC1(_CIasin, MSVCRT_asin)
672 CREATE_FPU_FUNC1(_CIatan, MSVCRT_atan)
673 CREATE_FPU_FUNC2(_CIatan2, MSVCRT_atan2)
674 CREATE_FPU_FUNC1(_CIcos, MSVCRT_cos)
675 CREATE_FPU_FUNC1(_CIcosh, MSVCRT_cosh)
676 CREATE_FPU_FUNC1(_CIexp, MSVCRT_exp)
677 CREATE_FPU_FUNC2(_CIfmod, MSVCRT_fmod)
678 CREATE_FPU_FUNC1(_CIlog, MSVCRT_log)
679 CREATE_FPU_FUNC1(_CIlog10, MSVCRT_log10)
680 CREATE_FPU_FUNC2(_CIpow, MSVCRT_pow)
681 CREATE_FPU_FUNC1(_CIsin, MSVCRT_sin)
682 CREATE_FPU_FUNC1(_CIsinh, MSVCRT_sinh)
683 CREATE_FPU_FUNC1(_CIsqrt, MSVCRT_sqrt)
684 CREATE_FPU_FUNC1(_CItan, MSVCRT_tan)
685 CREATE_FPU_FUNC1(_CItanh, MSVCRT_tanh)
687 __ASM_GLOBAL_FUNC(MSVCRT__ftol,
688 "pushl %ebp\n\t"
689 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
690 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
691 "movl %esp, %ebp\n\t"
692 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
693 "subl $12, %esp\n\t" /* sizeof(LONGLONG) + 2*sizeof(WORD) */
694 "fnstcw (%esp)\n\t"
695 "mov (%esp), %ax\n\t"
696 "or $0xc, %ax\n\t"
697 "mov %ax, 2(%esp)\n\t"
698 "fldcw 2(%esp)\n\t"
699 "fistpq 4(%esp)\n\t"
700 "fldcw (%esp)\n\t"
701 "movl 4(%esp), %eax\n\t"
702 "movl 8(%esp), %edx\n\t"
703 "leave\n\t"
704 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
705 __ASM_CFI(".cfi_same_value %ebp\n\t")
706 "ret")
708 #endif /* defined(__GNUC__) && defined(__i386__) */
710 /*********************************************************************
711 * _fpclass (MSVCRT.@)
713 int CDECL MSVCRT__fpclass(double num)
715 #if defined(HAVE_FPCLASS) || defined(fpclass)
716 switch (fpclass( num ))
718 case FP_SNAN: return MSVCRT__FPCLASS_SNAN;
719 case FP_QNAN: return MSVCRT__FPCLASS_QNAN;
720 case FP_NINF: return MSVCRT__FPCLASS_NINF;
721 case FP_PINF: return MSVCRT__FPCLASS_PINF;
722 case FP_NDENORM: return MSVCRT__FPCLASS_ND;
723 case FP_PDENORM: return MSVCRT__FPCLASS_PD;
724 case FP_NZERO: return MSVCRT__FPCLASS_NZ;
725 case FP_PZERO: return MSVCRT__FPCLASS_PZ;
726 case FP_NNORM: return MSVCRT__FPCLASS_NN;
727 case FP_PNORM: return MSVCRT__FPCLASS_PN;
728 default: return MSVCRT__FPCLASS_PN;
730 #elif defined (fpclassify)
731 switch (fpclassify( num ))
733 case FP_NAN: return MSVCRT__FPCLASS_QNAN;
734 case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
735 case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
736 case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
738 return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
739 #else
740 if (!isfinite(num))
741 return MSVCRT__FPCLASS_QNAN;
742 return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
743 #endif
746 /*********************************************************************
747 * _rotl (MSVCRT.@)
749 unsigned int CDECL _rotl(unsigned int num, int shift)
751 shift &= 31;
752 return (num << shift) | (num >> (32-shift));
755 /*********************************************************************
756 * _lrotl (MSVCRT.@)
758 MSVCRT_ulong CDECL MSVCRT__lrotl(MSVCRT_ulong num, int shift)
760 shift &= 0x1f;
761 return (num << shift) | (num >> (32-shift));
764 /*********************************************************************
765 * _lrotr (MSVCRT.@)
767 MSVCRT_ulong CDECL MSVCRT__lrotr(MSVCRT_ulong num, int shift)
769 shift &= 0x1f;
770 return (num >> shift) | (num << (32-shift));
773 /*********************************************************************
774 * _rotr (MSVCRT.@)
776 unsigned int CDECL _rotr(unsigned int num, int shift)
778 shift &= 0x1f;
779 return (num >> shift) | (num << (32-shift));
782 /*********************************************************************
783 * _rotl64 (MSVCRT.@)
785 unsigned __int64 CDECL _rotl64(unsigned __int64 num, int shift)
787 shift &= 63;
788 return (num << shift) | (num >> (64-shift));
791 /*********************************************************************
792 * _rotr64 (MSVCRT.@)
794 unsigned __int64 CDECL _rotr64(unsigned __int64 num, int shift)
796 shift &= 63;
797 return (num >> shift) | (num << (64-shift));
800 /*********************************************************************
801 * abs (MSVCRT.@)
803 int CDECL MSVCRT_abs( int n )
805 return n >= 0 ? n : -n;
808 /*********************************************************************
809 * labs (MSVCRT.@)
811 MSVCRT_long CDECL MSVCRT_labs( MSVCRT_long n )
813 return n >= 0 ? n : -n;
816 /*********************************************************************
817 * llabs (MSVCRT.@)
819 MSVCRT_longlong CDECL MSVCRT_llabs( MSVCRT_longlong n )
821 return n >= 0 ? n : -n;
824 /*********************************************************************
825 * _abs64 (MSVCRT.@)
827 __int64 CDECL _abs64( __int64 n )
829 return n >= 0 ? n : -n;
832 /*********************************************************************
833 * _logb (MSVCRT.@)
835 double CDECL MSVCRT__logb(double num)
837 double ret = logb(num);
838 if (isnan(num)) math_error(_DOMAIN, "_logb", num, 0, ret);
839 else if (!num) math_error(_SING, "_logb", num, 0, ret);
840 return ret;
843 /*********************************************************************
844 * _hypot (MSVCRT.@)
846 double CDECL _hypot(double x, double y)
848 /* FIXME: errno handling */
849 return hypot( x, y );
852 /*********************************************************************
853 * _hypotf (MSVCRT.@)
855 float CDECL MSVCRT__hypotf(float x, float y)
857 /* FIXME: errno handling */
858 return hypotf( x, y );
861 /*********************************************************************
862 * ceil (MSVCRT.@)
864 double CDECL MSVCRT_ceil( double x )
866 return ceil(x);
869 /*********************************************************************
870 * floor (MSVCRT.@)
872 double CDECL MSVCRT_floor( double x )
874 return floor(x);
877 /*********************************************************************
878 * fabs (MSVCRT.@)
880 double CDECL MSVCRT_fabs( double x )
882 return fabs(x);
885 /*********************************************************************
886 * frexp (MSVCRT.@)
888 double CDECL MSVCRT_frexp( double x, int *exp )
890 return frexp( x, exp );
893 /*********************************************************************
894 * modf (MSVCRT.@)
896 double CDECL MSVCRT_modf( double x, double *iptr )
898 return modf( x, iptr );
901 /**********************************************************************
902 * _statusfp2 (MSVCRT.@)
904 * Not exported by native msvcrt, added in msvcr80.
906 #if defined(__i386__) || defined(__x86_64__)
907 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
909 #ifdef __GNUC__
910 unsigned int flags;
911 unsigned long fpword;
913 if (x86_sw)
915 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
916 flags = 0;
917 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
918 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
919 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
920 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
921 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
922 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
923 *x86_sw = flags;
926 if (!sse2_sw) return;
928 if (sse2_supported)
930 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
931 flags = 0;
932 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
933 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
934 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
935 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
936 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
937 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
938 *sse2_sw = flags;
940 else *sse2_sw = 0;
941 #else
942 FIXME( "not implemented\n" );
943 #endif
945 #endif
947 /**********************************************************************
948 * _statusfp (MSVCRT.@)
950 unsigned int CDECL _statusfp(void)
952 #if defined(__i386__) || defined(__x86_64__)
953 unsigned int x86_sw, sse2_sw;
955 _statusfp2( &x86_sw, &sse2_sw );
956 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
957 return x86_sw | sse2_sw;
958 #else
959 FIXME( "not implemented\n" );
960 return 0;
961 #endif
964 /*********************************************************************
965 * _clearfp (MSVCRT.@)
967 unsigned int CDECL _clearfp(void)
969 unsigned int flags = 0;
970 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
971 unsigned long fpword;
973 __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
974 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
975 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
976 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
977 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
978 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
979 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
981 if (sse2_supported)
983 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
984 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
985 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
986 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
987 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
988 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
989 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
990 fpword &= ~0x3f;
991 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
993 #else
994 FIXME( "not implemented\n" );
995 #endif
996 return flags;
999 /*********************************************************************
1000 * __fpecode (MSVCRT.@)
1002 int * CDECL __fpecode(void)
1004 return &msvcrt_get_thread_data()->fpecode;
1007 /*********************************************************************
1008 * ldexp (MSVCRT.@)
1010 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
1012 double z = ldexp(num,exp);
1014 if (isfinite(num) && !isfinite(z))
1015 math_error(_OVERFLOW, "ldexp", num, exp, z);
1016 else if (isfinite(num) && !z)
1017 math_error(_UNDERFLOW, "ldexp", num, exp, z);
1018 else if (z == 0 && signbit(z))
1019 z = 0.0; /* Convert -0 -> +0 */
1020 return z;
1023 /*********************************************************************
1024 * _cabs (MSVCRT.@)
1026 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
1028 return sqrt(num.x * num.x + num.y * num.y);
1031 /*********************************************************************
1032 * _chgsign (MSVCRT.@)
1034 double CDECL MSVCRT__chgsign(double num)
1036 /* FIXME: +-infinity,Nan not tested */
1037 return -num;
1040 /*********************************************************************
1041 * __control87_2 (MSVCRT.@)
1043 * Not exported by native msvcrt, added in msvcr80.
1045 #if defined(__i386__) || defined(__x86_64__)
1046 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1047 unsigned int *x86_cw, unsigned int *sse2_cw )
1049 #ifdef __GNUC__
1050 unsigned long fpword;
1051 unsigned int flags;
1053 if (x86_cw)
1055 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1057 /* Convert into mask constants */
1058 flags = 0;
1059 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
1060 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
1061 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
1062 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
1063 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1064 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1065 switch (fpword & 0xc00)
1067 case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1068 case 0x800: flags |= MSVCRT__RC_UP; break;
1069 case 0x400: flags |= MSVCRT__RC_DOWN; break;
1071 switch (fpword & 0x300)
1073 case 0x0: flags |= MSVCRT__PC_24; break;
1074 case 0x200: flags |= MSVCRT__PC_53; break;
1075 case 0x300: flags |= MSVCRT__PC_64; break;
1077 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1079 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1080 if (mask)
1082 flags = (flags & ~mask) | (newval & mask);
1084 /* Convert (masked) value back to fp word */
1085 fpword = 0;
1086 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
1087 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
1088 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1089 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
1090 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
1091 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
1092 switch (flags & MSVCRT__MCW_RC)
1094 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1095 case MSVCRT__RC_UP: fpword |= 0x800; break;
1096 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
1098 switch (flags & MSVCRT__MCW_PC)
1100 case MSVCRT__PC_64: fpword |= 0x300; break;
1101 case MSVCRT__PC_53: fpword |= 0x200; break;
1102 case MSVCRT__PC_24: fpword |= 0x0; break;
1104 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1106 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1108 *x86_cw = flags;
1111 if (!sse2_cw) return 1;
1113 if (sse2_supported)
1115 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1117 /* Convert into mask constants */
1118 flags = 0;
1119 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1120 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1121 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1122 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1123 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1124 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1125 switch (fpword & 0x6000)
1127 case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1128 case 0x4000: flags |= MSVCRT__RC_UP; break;
1129 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1131 switch (fpword & 0x8040)
1133 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1134 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1135 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1138 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1139 if (mask)
1141 flags = (flags & ~mask) | (newval & mask);
1143 /* Convert (masked) value back to fp word */
1144 fpword = 0;
1145 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1146 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1147 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1148 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1149 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1150 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1151 switch (flags & MSVCRT__MCW_RC)
1153 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1154 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1155 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1157 switch (flags & MSVCRT__MCW_DN)
1159 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1160 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1161 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
1163 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1165 *sse2_cw = flags;
1167 else *sse2_cw = 0;
1169 return 1;
1170 #else
1171 FIXME( "not implemented\n" );
1172 return 0;
1173 #endif
1175 #endif
1177 /*********************************************************************
1178 * _control87 (MSVCRT.@)
1180 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1182 #if defined(__i386__) || defined(__x86_64__)
1183 unsigned int x86_cw, sse2_cw;
1185 __control87_2( newval, mask, &x86_cw, &sse2_cw );
1187 if ((x86_cw ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) x86_cw |= MSVCRT__EM_AMBIGUOUS;
1188 return x86_cw;
1189 #else
1190 FIXME( "not implemented\n" );
1191 return 0;
1192 #endif
1195 /*********************************************************************
1196 * _controlfp (MSVCRT.@)
1198 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
1200 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
1203 /*********************************************************************
1204 * _set_controlfp (MSVCRT.@)
1206 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
1208 _controlfp( newval, mask );
1211 /*********************************************************************
1212 * _controlfp_s (MSVCRT.@)
1214 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
1216 static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
1217 MSVCRT__MCW_PC | MSVCRT__MCW_DN);
1218 unsigned int val;
1220 if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
1222 if (cur) *cur = _controlfp( 0, 0 ); /* retrieve it anyway */
1223 return MSVCRT_EINVAL;
1225 val = _controlfp( newval, mask );
1226 if (cur) *cur = val;
1227 return 0;
1230 /*********************************************************************
1231 * fegetenv (MSVCR120.@)
1233 int CDECL MSVCRT_fegetenv(MSVCRT_fenv_t *env)
1235 env->control = _controlfp(0, 0) & (MSVCRT__EM_INEXACT | MSVCRT__EM_UNDERFLOW |
1236 MSVCRT__EM_OVERFLOW | MSVCRT__EM_ZERODIVIDE | MSVCRT__EM_INVALID);
1237 env->status = _statusfp();
1238 return 0;
1241 /*********************************************************************
1242 * __fpe_flt_rounds (UCRTBASE.@)
1244 int CDECL __fpe_flt_rounds(void)
1246 unsigned int fpc = _controlfp(0, 0) & MSVCRT__RC_CHOP;
1248 TRACE("()\n");
1250 switch(fpc) {
1251 case MSVCRT__RC_CHOP: return 0;
1252 case MSVCRT__RC_NEAR: return 1;
1253 #ifdef _WIN64
1254 case MSVCRT__RC_UP: return 3;
1255 default: return 2;
1256 #else
1257 case MSVCRT__RC_UP: return 2;
1258 default: return 3;
1259 #endif
1263 /*********************************************************************
1264 * fegetround (MSVCR120.@)
1266 int CDECL MSVCRT_fegetround(void)
1268 return _controlfp(0, 0) & MSVCRT__RC_CHOP;
1271 /*********************************************************************
1272 * fesetround (MSVCR120.@)
1274 int CDECL MSVCRT_fesetround(int round_mode)
1276 if (round_mode & (~MSVCRT__RC_CHOP))
1277 return 1;
1278 _controlfp(round_mode, MSVCRT__RC_CHOP);
1279 return 0;
1282 /*********************************************************************
1283 * _copysign (MSVCRT.@)
1285 double CDECL MSVCRT__copysign(double num, double sign)
1287 if (signbit(sign))
1288 return signbit(num) ? num : -num;
1289 return signbit(num) ? -num : num;
1292 /*********************************************************************
1293 * _finite (MSVCRT.@)
1295 int CDECL MSVCRT__finite(double num)
1297 return isfinite(num) != 0; /* See comment for _isnan() */
1300 /*********************************************************************
1301 * _fpreset (MSVCRT.@)
1303 void CDECL _fpreset(void)
1305 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1306 const unsigned int x86_cw = 0x27f;
1307 __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
1308 if (sse2_supported)
1310 const unsigned long sse2_cw = 0x1f80;
1311 __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
1313 #else
1314 FIXME( "not implemented\n" );
1315 #endif
1318 /*********************************************************************
1319 * fesetenv (MSVCR120.@)
1321 int CDECL MSVCRT_fesetenv(const MSVCRT_fenv_t *env)
1323 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1324 struct {
1325 WORD control_word;
1326 WORD unused1;
1327 WORD status_word;
1328 WORD unused2;
1329 WORD tag_word;
1330 WORD unused3;
1331 DWORD instruction_pointer;
1332 WORD code_segment;
1333 WORD unused4;
1334 DWORD operand_addr;
1335 WORD data_segment;
1336 WORD unused5;
1337 } fenv;
1339 TRACE( "(%p)\n", env );
1341 if (!env->control && !env->status) {
1342 _fpreset();
1343 return 0;
1346 __asm__ __volatile__( "fnstenv %0" : "=m" (fenv) );
1348 fenv.control_word &= ~0x3d;
1349 if (env->control & MSVCRT__EM_INVALID) fenv.control_word |= 0x1;
1350 if (env->control & MSVCRT__EM_ZERODIVIDE) fenv.control_word |= 0x4;
1351 if (env->control & MSVCRT__EM_OVERFLOW) fenv.control_word |= 0x8;
1352 if (env->control & MSVCRT__EM_UNDERFLOW) fenv.control_word |= 0x10;
1353 if (env->control & MSVCRT__EM_INEXACT) fenv.control_word |= 0x20;
1355 fenv.status_word &= ~0x3d;
1356 if (env->status & MSVCRT__SW_INVALID) fenv.status_word |= 0x1;
1357 if (env->status & MSVCRT__SW_ZERODIVIDE) fenv.status_word |= 0x4;
1358 if (env->status & MSVCRT__SW_OVERFLOW) fenv.status_word |= 0x8;
1359 if (env->status & MSVCRT__SW_UNDERFLOW) fenv.status_word |= 0x10;
1360 if (env->status & MSVCRT__SW_INEXACT) fenv.status_word |= 0x20;
1362 __asm__ __volatile__( "fldenv %0" : : "m" (fenv) : "st", "st(1)",
1363 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
1365 if (sse2_supported)
1367 DWORD fpword;
1369 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1370 fpword &= ~0x1e80;
1371 if (env->control & MSVCRT__EM_INVALID) fpword |= 0x80;
1372 if (env->control & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1373 if (env->control & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1374 if (env->control & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1375 if (env->control & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1376 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1379 return 0;
1380 #else
1381 FIXME( "not implemented\n" );
1382 #endif
1383 return 1;
1386 /*********************************************************************
1387 * _isnan (MSVCRT.@)
1389 INT CDECL MSVCRT__isnan(double num)
1391 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1392 * Do the same, as the result may be used in calculations
1394 return isnan(num) != 0;
1397 /*********************************************************************
1398 * _j0 (MSVCRT.@)
1400 double CDECL MSVCRT__j0(double num)
1402 /* FIXME: errno handling */
1403 return j0(num);
1406 /*********************************************************************
1407 * _j1 (MSVCRT.@)
1409 double CDECL MSVCRT__j1(double num)
1411 /* FIXME: errno handling */
1412 return j1(num);
1415 /*********************************************************************
1416 * _jn (MSVCRT.@)
1418 double CDECL MSVCRT__jn(int n, double num)
1420 /* FIXME: errno handling */
1421 return jn(n, num);
1424 /*********************************************************************
1425 * _y0 (MSVCRT.@)
1427 double CDECL MSVCRT__y0(double num)
1429 double retval;
1430 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1431 retval = y0(num);
1432 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1434 *MSVCRT__errno() = MSVCRT_EDOM;
1435 retval = sqrt(-1);
1437 return retval;
1440 /*********************************************************************
1441 * _y1 (MSVCRT.@)
1443 double CDECL MSVCRT__y1(double num)
1445 double retval;
1446 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1447 retval = y1(num);
1448 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1450 *MSVCRT__errno() = MSVCRT_EDOM;
1451 retval = sqrt(-1);
1453 return retval;
1456 /*********************************************************************
1457 * _yn (MSVCRT.@)
1459 double CDECL MSVCRT__yn(int order, double num)
1461 double retval;
1462 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1463 retval = yn(order,num);
1464 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1466 *MSVCRT__errno() = MSVCRT_EDOM;
1467 retval = sqrt(-1);
1469 return retval;
1472 /*********************************************************************
1473 * _nearbyint (MSVCRT.@)
1475 double CDECL MSVCRT_nearbyint(double num)
1477 #ifdef HAVE_NEARBYINT
1478 return nearbyint(num);
1479 #else
1480 return num >= 0 ? floor(num + 0.5) : ceil(num - 0.5);
1481 #endif
1484 /*********************************************************************
1485 * _nearbyintf (MSVCRT.@)
1487 float CDECL MSVCRT_nearbyintf(float num)
1489 #ifdef HAVE_NEARBYINTF
1490 return nearbyintf(num);
1491 #else
1492 return MSVCRT_nearbyint(num);
1493 #endif
1496 /*********************************************************************
1497 * _nextafter (MSVCRT.@)
1499 double CDECL MSVCRT__nextafter(double num, double next)
1501 double retval;
1502 if (!isfinite(num) || !isfinite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1503 retval = nextafter(num,next);
1504 return retval;
1507 /*********************************************************************
1508 * _ecvt (MSVCRT.@)
1510 char * CDECL MSVCRT__ecvt( double number, int ndigits, int *decpt, int *sign )
1512 int prec, len;
1513 thread_data_t *data = msvcrt_get_thread_data();
1514 /* FIXME: check better for overflow (native supports over 300 chars) */
1515 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1516 * 4 for exponent and one for
1517 * terminating '\0' */
1518 if (!data->efcvt_buffer)
1519 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1521 if( number < 0) {
1522 *sign = TRUE;
1523 number = -number;
1524 } else
1525 *sign = FALSE;
1526 /* handle cases with zero ndigits or less */
1527 prec = ndigits;
1528 if( prec < 1) prec = 2;
1529 len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1530 /* take the decimal "point away */
1531 if( prec != 1)
1532 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1533 /* take the exponential "e" out */
1534 data->efcvt_buffer[ prec] = '\0';
1535 /* read the exponent */
1536 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1537 (*decpt)++;
1538 /* adjust for some border cases */
1539 if( data->efcvt_buffer[0] == '0')/* value is zero */
1540 *decpt = 0;
1541 /* handle cases with zero ndigits or less */
1542 if( ndigits < 1){
1543 if( data->efcvt_buffer[ 0] >= '5')
1544 (*decpt)++;
1545 data->efcvt_buffer[ 0] = '\0';
1547 TRACE("out=\"%s\"\n",data->efcvt_buffer);
1548 return data->efcvt_buffer;
1551 /*********************************************************************
1552 * _ecvt_s (MSVCRT.@)
1554 int CDECL MSVCRT__ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
1556 int prec, len;
1557 char *result;
1558 const char infret[] = "1#INF";
1560 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
1561 if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
1562 if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
1563 if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1564 if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1566 /* special case - inf */
1567 if(number == HUGE_VAL || number == -HUGE_VAL)
1569 memset(buffer, '0', ndigits);
1570 memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
1571 buffer[ndigits] = '\0';
1572 (*decpt) = 1;
1573 if(number == -HUGE_VAL)
1574 (*sign) = 1;
1575 else
1576 (*sign) = 0;
1577 return 0;
1579 /* handle cases with zero ndigits or less */
1580 prec = ndigits;
1581 if( prec < 1) prec = 2;
1582 result = MSVCRT_malloc(prec + 7);
1584 if( number < 0) {
1585 *sign = TRUE;
1586 number = -number;
1587 } else
1588 *sign = FALSE;
1589 len = snprintf(result, prec + 7, "%.*le", prec - 1, number);
1590 /* take the decimal "point away */
1591 if( prec != 1)
1592 memmove( result + 1, result + 2, len - 1 );
1593 /* take the exponential "e" out */
1594 result[ prec] = '\0';
1595 /* read the exponent */
1596 sscanf( result + prec + 1, "%d", decpt);
1597 (*decpt)++;
1598 /* adjust for some border cases */
1599 if( result[0] == '0')/* value is zero */
1600 *decpt = 0;
1601 /* handle cases with zero ndigits or less */
1602 if( ndigits < 1){
1603 if( result[ 0] >= '5')
1604 (*decpt)++;
1605 result[ 0] = '\0';
1607 memcpy( buffer, result, max(ndigits + 1, 1) );
1608 MSVCRT_free( result );
1609 return 0;
1612 /***********************************************************************
1613 * _fcvt (MSVCRT.@)
1615 char * CDECL MSVCRT__fcvt( double number, int ndigits, int *decpt, int *sign )
1617 thread_data_t *data = msvcrt_get_thread_data();
1618 int stop, dec1, dec2;
1619 char *ptr1, *ptr2, *first;
1620 char buf[80]; /* ought to be enough */
1622 if (!data->efcvt_buffer)
1623 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1625 if (number < 0)
1627 *sign = 1;
1628 number = -number;
1629 } else *sign = 0;
1631 stop = snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1632 ptr1 = buf;
1633 ptr2 = data->efcvt_buffer;
1634 first = NULL;
1635 dec1 = 0;
1636 dec2 = 0;
1638 /* For numbers below the requested resolution, work out where
1639 the decimal point will be rather than finding it in the string */
1640 if (number < 1.0 && number > 0.0) {
1641 dec2 = log10(number + 1e-10);
1642 if (-dec2 <= ndigits) dec2 = 0;
1645 /* If requested digits is zero or less, we will need to truncate
1646 * the returned string */
1647 if (ndigits < 1) {
1648 stop += ndigits;
1651 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1652 while (*ptr1 != '\0' && *ptr1 != '.') {
1653 if (!first) first = ptr2;
1654 if ((ptr1 - buf) < stop) {
1655 *ptr2++ = *ptr1++;
1656 } else {
1657 ptr1++;
1659 dec1++;
1662 if (ndigits > 0) {
1663 ptr1++;
1664 if (!first) {
1665 while (*ptr1 == '0') { /* Process leading zeroes */
1666 *ptr2++ = *ptr1++;
1667 dec1--;
1670 while (*ptr1 != '\0') {
1671 if (!first) first = ptr2;
1672 *ptr2++ = *ptr1++;
1676 *ptr2 = '\0';
1678 /* We never found a non-zero digit, then our number is either
1679 * smaller than the requested precision, or 0.0 */
1680 if (!first) {
1681 if (number > 0.0) {
1682 first = ptr2;
1683 } else {
1684 first = data->efcvt_buffer;
1685 dec1 = 0;
1689 *decpt = dec2 ? dec2 : dec1;
1690 return first;
1693 /***********************************************************************
1694 * _fcvt_s (MSVCRT.@)
1696 int CDECL MSVCRT__fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
1698 int stop, dec1, dec2;
1699 char *ptr1, *ptr2, *first;
1700 char buf[80]; /* ought to be enough */
1702 if (!outbuffer || !decpt || !sign || size == 0)
1704 *MSVCRT__errno() = MSVCRT_EINVAL;
1705 return MSVCRT_EINVAL;
1708 if (number < 0)
1710 *sign = 1;
1711 number = -number;
1712 } else *sign = 0;
1714 stop = snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1715 ptr1 = buf;
1716 ptr2 = outbuffer;
1717 first = NULL;
1718 dec1 = 0;
1719 dec2 = 0;
1721 /* For numbers below the requested resolution, work out where
1722 the decimal point will be rather than finding it in the string */
1723 if (number < 1.0 && number > 0.0) {
1724 dec2 = log10(number + 1e-10);
1725 if (-dec2 <= ndigits) dec2 = 0;
1728 /* If requested digits is zero or less, we will need to truncate
1729 * the returned string */
1730 if (ndigits < 1) {
1731 stop += ndigits;
1734 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1735 while (*ptr1 != '\0' && *ptr1 != '.') {
1736 if (!first) first = ptr2;
1737 if ((ptr1 - buf) < stop) {
1738 if (size > 1) {
1739 *ptr2++ = *ptr1++;
1740 size--;
1742 } else {
1743 ptr1++;
1745 dec1++;
1748 if (ndigits > 0) {
1749 ptr1++;
1750 if (!first) {
1751 while (*ptr1 == '0') { /* Process leading zeroes */
1752 if (number == 0.0 && size > 1) {
1753 *ptr2++ = '0';
1754 size--;
1756 ptr1++;
1757 dec1--;
1760 while (*ptr1 != '\0') {
1761 if (!first) first = ptr2;
1762 if (size > 1) {
1763 *ptr2++ = *ptr1++;
1764 size--;
1769 *ptr2 = '\0';
1771 /* We never found a non-zero digit, then our number is either
1772 * smaller than the requested precision, or 0.0 */
1773 if (!first && (number <= 0.0))
1774 dec1 = 0;
1776 *decpt = dec2 ? dec2 : dec1;
1777 return 0;
1780 /***********************************************************************
1781 * _gcvt (MSVCRT.@)
1783 char * CDECL MSVCRT__gcvt( double number, int ndigit, char *buff )
1785 if(!buff) {
1786 *MSVCRT__errno() = MSVCRT_EINVAL;
1787 return NULL;
1790 if(ndigit < 0) {
1791 *MSVCRT__errno() = MSVCRT_ERANGE;
1792 return NULL;
1795 MSVCRT_sprintf(buff, "%.*g", ndigit, number);
1796 return buff;
1799 /***********************************************************************
1800 * _gcvt_s (MSVCRT.@)
1802 int CDECL MSVCRT__gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
1804 int len;
1806 if(!buff) {
1807 *MSVCRT__errno() = MSVCRT_EINVAL;
1808 return MSVCRT_EINVAL;
1811 if( digits<0 || digits>=size) {
1812 if(size)
1813 buff[0] = '\0';
1815 *MSVCRT__errno() = MSVCRT_ERANGE;
1816 return MSVCRT_ERANGE;
1819 len = MSVCRT__scprintf("%.*g", digits, number);
1820 if(len > size) {
1821 buff[0] = '\0';
1822 *MSVCRT__errno() = MSVCRT_ERANGE;
1823 return MSVCRT_ERANGE;
1826 MSVCRT_sprintf(buff, "%.*g", digits, number);
1827 return 0;
1830 #include <stdlib.h> /* div_t, ldiv_t */
1832 /*********************************************************************
1833 * div (MSVCRT.@)
1834 * VERSION
1835 * [i386] Windows binary compatible - returns the struct in eax/edx.
1837 #ifdef __i386__
1838 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1840 div_t dt = div(num,denom);
1841 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1843 #else
1844 /*********************************************************************
1845 * div (MSVCRT.@)
1846 * VERSION
1847 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1849 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1851 div_t dt = div(num,denom);
1852 MSVCRT_div_t ret;
1853 ret.quot = dt.quot;
1854 ret.rem = dt.rem;
1856 return ret;
1859 #endif /* ifdef __i386__ */
1862 /*********************************************************************
1863 * ldiv (MSVCRT.@)
1864 * VERSION
1865 * [i386] Windows binary compatible - returns the struct in eax/edx.
1867 #ifdef __i386__
1868 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1870 ldiv_t ldt = ldiv(num,denom);
1871 return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1873 #else
1874 /*********************************************************************
1875 * ldiv (MSVCRT.@)
1876 * VERSION
1877 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1879 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1881 ldiv_t result = ldiv(num,denom);
1883 MSVCRT_ldiv_t ret;
1884 ret.quot = result.quot;
1885 ret.rem = result.rem;
1887 return ret;
1889 #endif /* ifdef __i386__ */
1891 /*********************************************************************
1892 * lldiv (MSVCRT.@)
1894 MSVCRT_lldiv_t CDECL MSVCRT_lldiv(MSVCRT_longlong num, MSVCRT_longlong denom)
1896 MSVCRT_lldiv_t ret;
1898 ret.quot = num / denom;
1899 ret.rem = num % denom;
1901 return ret;
1904 #ifdef __i386__
1906 /*********************************************************************
1907 * _adjust_fdiv (MSVCRT.@)
1908 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1910 int MSVCRT__adjust_fdiv = 0;
1912 /***********************************************************************
1913 * _adj_fdiv_m16i (MSVCRT.@)
1915 * NOTE
1916 * I _think_ this function is intended to work around the Pentium
1917 * fdiv bug.
1919 void __stdcall _adj_fdiv_m16i( short arg )
1921 TRACE("(): stub\n");
1924 /***********************************************************************
1925 * _adj_fdiv_m32 (MSVCRT.@)
1927 * NOTE
1928 * I _think_ this function is intended to work around the Pentium
1929 * fdiv bug.
1931 void __stdcall _adj_fdiv_m32( unsigned int arg )
1933 TRACE("(): stub\n");
1936 /***********************************************************************
1937 * _adj_fdiv_m32i (MSVCRT.@)
1939 * NOTE
1940 * I _think_ this function is intended to work around the Pentium
1941 * fdiv bug.
1943 void __stdcall _adj_fdiv_m32i( int arg )
1945 TRACE("(): stub\n");
1948 /***********************************************************************
1949 * _adj_fdiv_m64 (MSVCRT.@)
1951 * NOTE
1952 * I _think_ this function is intended to work around the Pentium
1953 * fdiv bug.
1955 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1957 TRACE("(): stub\n");
1960 /***********************************************************************
1961 * _adj_fdiv_r (MSVCRT.@)
1962 * FIXME
1963 * This function is likely to have the wrong number of arguments.
1965 * NOTE
1966 * I _think_ this function is intended to work around the Pentium
1967 * fdiv bug.
1969 void _adj_fdiv_r(void)
1971 TRACE("(): stub\n");
1974 /***********************************************************************
1975 * _adj_fdivr_m16i (MSVCRT.@)
1977 * NOTE
1978 * I _think_ this function is intended to work around the Pentium
1979 * fdiv bug.
1981 void __stdcall _adj_fdivr_m16i( short arg )
1983 TRACE("(): stub\n");
1986 /***********************************************************************
1987 * _adj_fdivr_m32 (MSVCRT.@)
1989 * NOTE
1990 * I _think_ this function is intended to work around the Pentium
1991 * fdiv bug.
1993 void __stdcall _adj_fdivr_m32( unsigned int arg )
1995 TRACE("(): stub\n");
1998 /***********************************************************************
1999 * _adj_fdivr_m32i (MSVCRT.@)
2001 * NOTE
2002 * I _think_ this function is intended to work around the Pentium
2003 * fdiv bug.
2005 void __stdcall _adj_fdivr_m32i( int arg )
2007 TRACE("(): stub\n");
2010 /***********************************************************************
2011 * _adj_fdivr_m64 (MSVCRT.@)
2013 * NOTE
2014 * I _think_ this function is intended to work around the Pentium
2015 * fdiv bug.
2017 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
2019 TRACE("(): stub\n");
2022 /***********************************************************************
2023 * _adj_fpatan (MSVCRT.@)
2024 * FIXME
2025 * This function is likely to have the wrong number of arguments.
2027 * NOTE
2028 * I _think_ this function is intended to work around the Pentium
2029 * fdiv bug.
2031 void _adj_fpatan(void)
2033 TRACE("(): stub\n");
2036 /***********************************************************************
2037 * _adj_fprem (MSVCRT.@)
2038 * FIXME
2039 * This function is likely to have the wrong number of arguments.
2041 * NOTE
2042 * I _think_ this function is intended to work around the Pentium
2043 * fdiv bug.
2045 void _adj_fprem(void)
2047 TRACE("(): stub\n");
2050 /***********************************************************************
2051 * _adj_fprem1 (MSVCRT.@)
2052 * FIXME
2053 * This function is likely to have the wrong number of arguments.
2055 * NOTE
2056 * I _think_ this function is intended to work around the Pentium
2057 * fdiv bug.
2059 void _adj_fprem1(void)
2061 TRACE("(): stub\n");
2064 /***********************************************************************
2065 * _adj_fptan (MSVCRT.@)
2066 * FIXME
2067 * This function is likely to have the wrong number of arguments.
2069 * NOTE
2070 * I _think_ this function is intended to work around the Pentium
2071 * fdiv bug.
2073 void _adj_fptan(void)
2075 TRACE("(): stub\n");
2078 /***********************************************************************
2079 * _safe_fdiv (MSVCRT.@)
2080 * FIXME
2081 * This function is likely to have the wrong number of arguments.
2083 * NOTE
2084 * I _think_ this function is intended to work around the Pentium
2085 * fdiv bug.
2087 void _safe_fdiv(void)
2089 TRACE("(): stub\n");
2092 /***********************************************************************
2093 * _safe_fdivr (MSVCRT.@)
2094 * FIXME
2095 * This function is likely to have the wrong number of arguments.
2097 * NOTE
2098 * I _think_ this function is intended to work around the Pentium
2099 * fdiv bug.
2101 void _safe_fdivr(void)
2103 TRACE("(): stub\n");
2106 /***********************************************************************
2107 * _safe_fprem (MSVCRT.@)
2108 * FIXME
2109 * This function is likely to have the wrong number of arguments.
2111 * NOTE
2112 * I _think_ this function is intended to work around the Pentium
2113 * fdiv bug.
2115 void _safe_fprem(void)
2117 TRACE("(): stub\n");
2120 /***********************************************************************
2121 * _safe_fprem1 (MSVCRT.@)
2123 * FIXME
2124 * This function is likely to have the wrong number of arguments.
2126 * NOTE
2127 * I _think_ this function is intended to work around the Pentium
2128 * fdiv bug.
2130 void _safe_fprem1(void)
2132 TRACE("(): stub\n");
2135 /***********************************************************************
2136 * __libm_sse2_acos (MSVCRT.@)
2138 void __cdecl MSVCRT___libm_sse2_acos(void)
2140 double d;
2141 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2142 d = acos( d );
2143 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2146 /***********************************************************************
2147 * __libm_sse2_acosf (MSVCRT.@)
2149 void __cdecl MSVCRT___libm_sse2_acosf(void)
2151 float f;
2152 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2153 f = acosf( f );
2154 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2157 /***********************************************************************
2158 * __libm_sse2_asin (MSVCRT.@)
2160 void __cdecl MSVCRT___libm_sse2_asin(void)
2162 double d;
2163 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2164 d = asin( d );
2165 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2168 /***********************************************************************
2169 * __libm_sse2_asinf (MSVCRT.@)
2171 void __cdecl MSVCRT___libm_sse2_asinf(void)
2173 float f;
2174 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2175 f = asinf( f );
2176 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2179 /***********************************************************************
2180 * __libm_sse2_atan (MSVCRT.@)
2182 void __cdecl MSVCRT___libm_sse2_atan(void)
2184 double d;
2185 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2186 d = atan( d );
2187 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2190 /***********************************************************************
2191 * __libm_sse2_atan2 (MSVCRT.@)
2193 void __cdecl MSVCRT___libm_sse2_atan2(void)
2195 double d1, d2;
2196 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2197 d1 = atan2( d1, d2 );
2198 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2201 /***********************************************************************
2202 * __libm_sse2_atanf (MSVCRT.@)
2204 void __cdecl MSVCRT___libm_sse2_atanf(void)
2206 float f;
2207 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2208 f = atanf( f );
2209 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2212 /***********************************************************************
2213 * __libm_sse2_cos (MSVCRT.@)
2215 void __cdecl MSVCRT___libm_sse2_cos(void)
2217 double d;
2218 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2219 d = cos( d );
2220 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2223 /***********************************************************************
2224 * __libm_sse2_cosf (MSVCRT.@)
2226 void __cdecl MSVCRT___libm_sse2_cosf(void)
2228 float f;
2229 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2230 f = cosf( f );
2231 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2234 /***********************************************************************
2235 * __libm_sse2_exp (MSVCRT.@)
2237 void __cdecl MSVCRT___libm_sse2_exp(void)
2239 double d;
2240 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2241 d = exp( d );
2242 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2245 /***********************************************************************
2246 * __libm_sse2_expf (MSVCRT.@)
2248 void __cdecl MSVCRT___libm_sse2_expf(void)
2250 float f;
2251 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2252 f = expf( f );
2253 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2256 /***********************************************************************
2257 * __libm_sse2_log (MSVCRT.@)
2259 void __cdecl MSVCRT___libm_sse2_log(void)
2261 double d;
2262 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2263 d = log( d );
2264 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2267 /***********************************************************************
2268 * __libm_sse2_log10 (MSVCRT.@)
2270 void __cdecl MSVCRT___libm_sse2_log10(void)
2272 double d;
2273 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2274 d = log10( d );
2275 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2278 /***********************************************************************
2279 * __libm_sse2_log10f (MSVCRT.@)
2281 void __cdecl MSVCRT___libm_sse2_log10f(void)
2283 float f;
2284 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2285 f = log10f( f );
2286 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2289 /***********************************************************************
2290 * __libm_sse2_logf (MSVCRT.@)
2292 void __cdecl MSVCRT___libm_sse2_logf(void)
2294 float f;
2295 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2296 f = logf( f );
2297 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2300 /***********************************************************************
2301 * __libm_sse2_pow (MSVCRT.@)
2303 void __cdecl MSVCRT___libm_sse2_pow(void)
2305 double d1, d2;
2306 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2307 d1 = pow( d1, d2 );
2308 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2311 /***********************************************************************
2312 * __libm_sse2_powf (MSVCRT.@)
2314 void __cdecl MSVCRT___libm_sse2_powf(void)
2316 float f1, f2;
2317 __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
2318 f1 = powf( f1, f2 );
2319 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
2322 /***********************************************************************
2323 * __libm_sse2_sin (MSVCRT.@)
2325 void __cdecl MSVCRT___libm_sse2_sin(void)
2327 double d;
2328 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2329 d = sin( d );
2330 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2333 /***********************************************************************
2334 * __libm_sse2_sinf (MSVCRT.@)
2336 void __cdecl MSVCRT___libm_sse2_sinf(void)
2338 float f;
2339 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2340 f = sinf( f );
2341 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2344 /***********************************************************************
2345 * __libm_sse2_tan (MSVCRT.@)
2347 void __cdecl MSVCRT___libm_sse2_tan(void)
2349 double d;
2350 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2351 d = tan( d );
2352 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2355 /***********************************************************************
2356 * __libm_sse2_tanf (MSVCRT.@)
2358 void __cdecl MSVCRT___libm_sse2_tanf(void)
2360 float f;
2361 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2362 f = tanf( f );
2363 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2366 /***********************************************************************
2367 * __libm_sse2_sqrt_precise (MSVCR110.@)
2369 void __cdecl MSVCRT___libm_sse2_sqrt_precise(void)
2371 double d;
2372 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2373 d = sqrt( d );
2374 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2377 #endif /* __i386__ */
2379 /*********************************************************************
2380 * cbrt (MSVCR120.@)
2382 double CDECL MSVCR120_cbrt(double x)
2384 #ifdef HAVE_CBRT
2385 return cbrt(x);
2386 #else
2387 return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
2388 #endif
2391 /*********************************************************************
2392 * cbrtf (MSVCR120.@)
2394 float CDECL MSVCR120_cbrtf(float x)
2396 #ifdef HAVE_CBRTF
2397 return cbrtf(x);
2398 #else
2399 return MSVCR120_cbrt(x);
2400 #endif
2403 /*********************************************************************
2404 * cbrtl (MSVCR120.@)
2406 LDOUBLE CDECL MSVCR120_cbrtl(LDOUBLE x)
2408 return MSVCR120_cbrt(x);
2411 /*********************************************************************
2412 * exp2 (MSVCR120.@)
2414 double CDECL MSVCR120_exp2(double x)
2416 #ifdef HAVE_EXP2
2417 double ret = exp2(x);
2418 #else
2419 double ret = pow(2, x);
2420 #endif
2421 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
2422 return ret;
2425 /*********************************************************************
2426 * exp2f (MSVCR120.@)
2428 float CDECL MSVCR120_exp2f(float x)
2430 #ifdef HAVE_EXP2F
2431 float ret = exp2f(x);
2432 if (finitef(x) && !finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
2433 return ret;
2434 #else
2435 return MSVCR120_exp2(x);
2436 #endif
2439 /*********************************************************************
2440 * exp2l (MSVCR120.@)
2442 LDOUBLE CDECL MSVCR120_exp2l(LDOUBLE x)
2444 return MSVCR120_exp2(x);
2447 /*********************************************************************
2448 * expm1 (MSVCR120.@)
2450 double CDECL MSVCR120_expm1(double x)
2452 #ifdef HAVE_EXPM1
2453 double ret = expm1(x);
2454 #else
2455 double ret = exp(x) - 1;
2456 #endif
2457 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
2458 return ret;
2461 /*********************************************************************
2462 * expm1f (MSVCR120.@)
2464 float CDECL MSVCR120_expm1f(float x)
2466 #ifdef HAVE_EXPM1F
2467 float ret = expm1f(x);
2468 #else
2469 float ret = exp(x) - 1;
2470 #endif
2471 if (finitef(x) && !finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
2472 return ret;
2475 /*********************************************************************
2476 * expm1l (MSVCR120.@)
2478 LDOUBLE CDECL MSVCR120_expm1l(LDOUBLE x)
2480 return MSVCR120_expm1(x);
2483 /*********************************************************************
2484 * log1p (MSVCR120.@)
2486 double CDECL MSVCR120_log1p(double x)
2488 if (x < -1) *MSVCRT__errno() = MSVCRT_EDOM;
2489 else if (x == -1) *MSVCRT__errno() = MSVCRT_ERANGE;
2490 #ifdef HAVE_LOG1P
2491 return log1p(x);
2492 #else
2493 return log(1 + x);
2494 #endif
2497 /*********************************************************************
2498 * log1pf (MSVCR120.@)
2500 float CDECL MSVCR120_log1pf(float x)
2502 if (x < -1) *MSVCRT__errno() = MSVCRT_EDOM;
2503 else if (x == -1) *MSVCRT__errno() = MSVCRT_ERANGE;
2504 #ifdef HAVE_LOG1PF
2505 return log1pf(x);
2506 #else
2507 return log(1 + x);
2508 #endif
2511 /*********************************************************************
2512 * log1pl (MSVCR120.@)
2514 LDOUBLE CDECL MSVCR120_log1pl(LDOUBLE x)
2516 return MSVCR120_log1p(x);
2519 /*********************************************************************
2520 * log2 (MSVCR120.@)
2522 double CDECL MSVCR120_log2(double x)
2524 if (x < 0) *MSVCRT__errno() = MSVCRT_EDOM;
2525 else if (x == 0) *MSVCRT__errno() = MSVCRT_ERANGE;
2526 #ifdef HAVE_LOG2
2527 return log2(x);
2528 #else
2529 return log(x) / log(2);
2530 #endif
2533 /*********************************************************************
2534 * log2f (MSVCR120.@)
2536 float CDECL MSVCR120_log2f(float x)
2538 #ifdef HAVE_LOG2F
2539 if (x < 0) *MSVCRT__errno() = MSVCRT_EDOM;
2540 else if (x == 0) *MSVCRT__errno() = MSVCRT_ERANGE;
2541 return log2f(x);
2542 #else
2543 return MSVCR120_log2(x);
2544 #endif
2547 /*********************************************************************
2548 * log2l (MSVCR120.@)
2550 LDOUBLE CDECL MSVCR120_log2l(LDOUBLE x)
2552 return MSVCR120_log2(x);
2555 /*********************************************************************
2556 * rint (MSVCR120.@)
2558 double CDECL MSVCR120_rint(double x)
2560 return rint(x);
2563 /*********************************************************************
2564 * rintf (MSVCR120.@)
2566 float CDECL MSVCR120_rintf(float x)
2568 return rintf(x);
2571 /*********************************************************************
2572 * rintl (MSVCR120.@)
2574 LDOUBLE CDECL MSVCR120_rintl(LDOUBLE x)
2576 return MSVCR120_rint(x);
2579 /*********************************************************************
2580 * lrint (MSVCR120.@)
2582 MSVCRT_long CDECL MSVCR120_lrint(double x)
2584 return lrint(x);
2587 /*********************************************************************
2588 * lrintf (MSVCR120.@)
2590 MSVCRT_long CDECL MSVCR120_lrintf(float x)
2592 return lrintf(x);
2595 /*********************************************************************
2596 * lrintl (MSVCR120.@)
2598 MSVCRT_long CDECL MSVCR120_lrintl(LDOUBLE x)
2600 return MSVCR120_lrint(x);
2603 /*********************************************************************
2604 * llrint (MSVCR120.@)
2606 MSVCRT_longlong CDECL MSVCR120_llrint(double x)
2608 return llrint(x);
2611 /*********************************************************************
2612 * llrintf (MSVCR120.@)
2614 MSVCRT_longlong CDECL MSVCR120_llrintf(float x)
2616 return llrintf(x);
2619 /*********************************************************************
2620 * rintl (MSVCR120.@)
2622 MSVCRT_longlong CDECL MSVCR120_llrintl(LDOUBLE x)
2624 return MSVCR120_llrint(x);
2627 /*********************************************************************
2628 * round (MSVCR120.@)
2630 double CDECL MSVCR120_round(double x)
2632 #ifdef HAVE_ROUND
2633 return round(x);
2634 #else
2635 return MSVCR120_rint(x);
2636 #endif
2639 /*********************************************************************
2640 * roundf (MSVCR120.@)
2642 float CDECL MSVCR120_roundf(float x)
2644 #ifdef HAVE_ROUNDF
2645 return roundf(x);
2646 #else
2647 return MSVCR120_round(x);
2648 #endif
2651 /*********************************************************************
2652 * roundl (MSVCR120.@)
2654 LDOUBLE CDECL MSVCR120_roundl(LDOUBLE x)
2656 return MSVCR120_round(x);
2659 /*********************************************************************
2660 * lround (MSVCR120.@)
2662 MSVCRT_long CDECL MSVCR120_lround(double x)
2664 #ifdef HAVE_LROUND
2665 return lround(x);
2666 #else
2667 return MSVCR120_round(x);
2668 #endif
2671 /*********************************************************************
2672 * lroundf (MSVCR120.@)
2674 MSVCRT_long CDECL MSVCR120_lroundf(float x)
2676 #ifdef HAVE_LROUNDF
2677 return lroundf(x);
2678 #else
2679 return MSVCR120_lround(x);
2680 #endif
2683 /*********************************************************************
2684 * lroundl (MSVCR120.@)
2686 MSVCRT_long CDECL MSVCR120_lroundl(LDOUBLE x)
2688 return MSVCR120_lround(x);
2691 /*********************************************************************
2692 * llround (MSVCR120.@)
2694 MSVCRT_longlong CDECL MSVCR120_llround(double x)
2696 #ifdef HAVE_LLROUND
2697 return llround(x);
2698 #else
2699 return MSVCR120_round(x);
2700 #endif
2703 /*********************************************************************
2704 * llroundf (MSVCR120.@)
2706 MSVCRT_longlong CDECL MSVCR120_llroundf(float x)
2708 #ifdef HAVE_LLROUNDF
2709 return llroundf(x);
2710 #else
2711 return MSVCR120_llround(x);
2712 #endif
2715 /*********************************************************************
2716 * roundl (MSVCR120.@)
2718 MSVCRT_longlong CDECL MSVCR120_llroundl(LDOUBLE x)
2720 return MSVCR120_llround(x);
2723 /*********************************************************************
2724 * trunc (MSVCR120.@)
2726 double CDECL MSVCR120_trunc(double x)
2728 #ifdef HAVE_TRUNC
2729 return trunc(x);
2730 #else
2731 return (x > 0) ? floor(x) : ceil(x);
2732 #endif
2735 /*********************************************************************
2736 * truncf (MSVCR120.@)
2738 float CDECL MSVCR120_truncf(float x)
2740 #ifdef HAVE_TRUNCF
2741 return truncf(x);
2742 #else
2743 return MSVCR120_trunc(x);
2744 #endif
2747 /*********************************************************************
2748 * truncl (MSVCR120.@)
2750 LDOUBLE CDECL MSVCR120_truncl(LDOUBLE x)
2752 return MSVCR120_trunc(x);
2755 /*********************************************************************
2756 * _dclass (MSVCR120.@)
2758 short CDECL MSVCR120__dclass(double x)
2760 switch (MSVCRT__fpclass(x)) {
2761 case MSVCRT__FPCLASS_QNAN:
2762 case MSVCRT__FPCLASS_SNAN:
2763 return MSVCRT_FP_NAN;
2764 case MSVCRT__FPCLASS_NINF:
2765 case MSVCRT__FPCLASS_PINF:
2766 return MSVCRT_FP_INFINITE;
2767 case MSVCRT__FPCLASS_ND:
2768 case MSVCRT__FPCLASS_PD:
2769 return MSVCRT_FP_SUBNORMAL;
2770 case MSVCRT__FPCLASS_NN:
2771 case MSVCRT__FPCLASS_PN:
2772 default:
2773 return MSVCRT_FP_NORMAL;
2774 case MSVCRT__FPCLASS_NZ:
2775 case MSVCRT__FPCLASS_PZ:
2776 return MSVCRT_FP_ZERO;
2780 /*********************************************************************
2781 * _fdclass (MSVCR120.@)
2783 short CDECL MSVCR120__fdclass(float x)
2785 return MSVCR120__dclass(x);
2788 /*********************************************************************
2789 * _ldclass (MSVCR120.@)
2791 short CDECL MSVCR120__ldclass(LDOUBLE x)
2793 return MSVCR120__dclass(x);
2796 /*********************************************************************
2797 * _dtest (MSVCR120.@)
2799 short CDECL MSVCR120__dtest(double *x)
2801 return MSVCR120__dclass(*x);
2804 /*********************************************************************
2805 * _fdtest (MSVCR120.@)
2807 short CDECL MSVCR120__fdtest(float *x)
2809 return MSVCR120__dclass(*x);
2812 /*********************************************************************
2813 * _ldtest (MSVCR120.@)
2815 short CDECL MSVCR120__ldtest(LDOUBLE *x)
2817 return MSVCR120__dclass(*x);
2820 /*********************************************************************
2821 * erf (MSVCR120.@)
2823 double CDECL MSVCR120_erf(double x)
2825 #ifdef HAVE_ERF
2826 return erf(x);
2827 #else
2828 /* Abramowitz and Stegun approximation, maximum error: 1.5*10^-7 */
2829 double t, y;
2830 int sign = signbit(x);
2832 if (sign) x = -x;
2833 t = 1 / (1 + 0.3275911 * x);
2834 y = ((((1.061405429*t - 1.453152027)*t + 1.421413741)*t - 0.284496736)*t + 0.254829592)*t;
2835 y = 1.0 - y*exp(-x*x);
2836 return sign ? -y : y;
2837 #endif
2840 /*********************************************************************
2841 * erff (MSVCR120.@)
2843 float CDECL MSVCR120_erff(float x)
2845 #ifdef HAVE_ERFF
2846 return erff(x);
2847 #else
2848 return MSVCR120_erf(x);
2849 #endif
2852 /*********************************************************************
2853 * erfl (MSVCR120.@)
2855 LDOUBLE CDECL MSVCR120_erfl(LDOUBLE x)
2857 return MSVCR120_erf(x);
2860 /*********************************************************************
2861 * erfc (MSVCR120.@)
2863 double CDECL MSVCR120_erfc(double x)
2865 #ifdef HAVE_ERFC
2866 return erfc(x);
2867 #else
2868 return 1 - MSVCR120_erf(x);
2869 #endif
2872 /*********************************************************************
2873 * erfcf (MSVCR120.@)
2875 float CDECL MSVCR120_erfcf(float x)
2877 #ifdef HAVE_ERFCF
2878 return erfcf(x);
2879 #else
2880 return MSVCR120_erfc(x);
2881 #endif
2884 /*********************************************************************
2885 * erfcl (MSVCR120.@)
2887 LDOUBLE CDECL MSVCR120_erfcl(LDOUBLE x)
2889 return MSVCR120_erfc(x);
2892 /*********************************************************************
2893 * fmaxf (MSVCR120.@)
2895 float CDECL MSVCR120_fmaxf(float x, float y)
2897 if(isnanf(x))
2898 return y;
2899 if(isnanf(y))
2900 return x;
2901 if(x==0 && y==0)
2902 return signbit(x) ? y : x;
2903 return x<y ? y : x;
2906 /*********************************************************************
2907 * fmax (MSVCR120.@)
2909 double CDECL MSVCR120_fmax(double x, double y)
2911 if(isnan(x))
2912 return y;
2913 if(isnan(y))
2914 return x;
2915 if(x==0 && y==0)
2916 return signbit(x) ? y : x;
2917 return x<y ? y : x;
2920 /*********************************************************************
2921 * _fdsign (MSVCR120.@)
2923 int CDECL MSVCR120__fdsign(float x)
2925 return signbit(x) ? 0x8000 : 0;
2928 /*********************************************************************
2929 * _dsign (MSVCR120.@)
2931 int CDECL MSVCR120__dsign(double x)
2933 return signbit(x) ? 0x8000 : 0;
2937 /*********************************************************************
2938 * _dpcomp (MSVCR120.@)
2940 int CDECL MSVCR120__dpcomp(double x, double y)
2942 if(isnan(x) || isnan(y))
2943 return 0;
2945 if(x == y) return 2;
2946 return x < y ? 1 : 4;
2949 /*********************************************************************
2950 * _fdpcomp (MSVCR120.@)
2952 int CDECL MSVCR120__fdpcomp(float x, float y)
2954 return MSVCR120__dpcomp(x, y);
2957 /*********************************************************************
2958 * fminf (MSVCR120.@)
2960 float CDECL MSVCR120_fminf(float x, float y)
2962 if(isnanf(x))
2963 return y;
2964 if(isnanf(y))
2965 return x;
2966 if(x==0 && y==0)
2967 return signbit(x) ? x : y;
2968 return x<y ? x : y;
2971 /*********************************************************************
2972 * fmin (MSVCR120.@)
2974 double CDECL MSVCR120_fmin(double x, double y)
2976 if(isnan(x))
2977 return y;
2978 if(isnan(y))
2979 return x;
2980 if(x==0 && y==0)
2981 return signbit(x) ? x : y;
2982 return x<y ? x : y;
2985 /*********************************************************************
2986 * asinh (MSVCR120.@)
2988 double CDECL MSVCR120_asinh(double x)
2990 #ifdef HAVE_ASINH
2991 return asinh(x);
2992 #else
2993 if (!isfinite(x*x+1)) return log(2) + log(x);
2994 return log(x + sqrt(x*x+1));
2995 #endif
2998 /*********************************************************************
2999 * asinhf (MSVCR120.@)
3001 float CDECL MSVCR120_asinhf(float x)
3003 #ifdef HAVE_ASINHF
3004 return asinhf(x);
3005 #else
3006 return MSVCR120_asinh(x);
3007 #endif
3010 /*********************************************************************
3011 * asinhl (MSVCR120.@)
3013 LDOUBLE CDECL MSVCR120_asinhl(LDOUBLE x)
3015 return MSVCR120_asinh(x);
3018 /*********************************************************************
3019 * acosh (MSVCR120.@)
3021 double CDECL MSVCR120_acosh(double x)
3023 if (x < 1) *MSVCRT__errno() = MSVCRT_EDOM;
3025 #ifdef HAVE_ACOSH
3026 return acosh(x);
3027 #else
3028 if (x < 1) {
3029 MSVCRT_fenv_t env;
3031 MSVCRT_fegetenv(&env);
3032 env.status |= MSVCRT__SW_INVALID;
3033 MSVCRT_fesetenv(&env);
3034 return NAN;
3036 if (!isfinite(x*x)) return log(2) + log(x);
3037 return log(x + sqrt(x*x-1));
3038 #endif
3041 /*********************************************************************
3042 * acoshf (MSVCR120.@)
3044 float CDECL MSVCR120_acoshf(float x)
3046 #ifdef HAVE_ACOSHF
3047 if (x < 1) *MSVCRT__errno() = MSVCRT_EDOM;
3049 return acoshf(x);
3050 #else
3051 return MSVCR120_acosh(x);
3052 #endif
3055 /*********************************************************************
3056 * acoshl (MSVCR120.@)
3058 LDOUBLE CDECL MSVCR120_acoshl(LDOUBLE x)
3060 return MSVCR120_acosh(x);
3063 /*********************************************************************
3064 * atanh (MSVCR120.@)
3066 double CDECL MSVCR120_atanh(double x)
3068 double ret;
3070 if (x > 1 || x < -1) {
3071 MSVCRT_fenv_t env;
3073 *MSVCRT__errno() = MSVCRT_EDOM;
3075 /* on Linux atanh returns -NAN in this case */
3076 MSVCRT_fegetenv(&env);
3077 env.status |= MSVCRT__SW_INVALID;
3078 MSVCRT_fesetenv(&env);
3079 return NAN;
3082 #ifdef HAVE_ATANH
3083 ret = atanh(x);
3084 #else
3085 if (-1e-6 < x && x < 1e-6) ret = x + x*x*x/3;
3086 else ret = (log(1+x) - log(1-x)) / 2;
3087 #endif
3089 if (!isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3090 return ret;
3093 /*********************************************************************
3094 * atanhf (MSVCR120.@)
3096 float CDECL MSVCR120_atanhf(float x)
3098 #ifdef HAVE_ATANHF
3099 float ret;
3101 if (x > 1 || x < -1) {
3102 MSVCRT_fenv_t env;
3104 *MSVCRT__errno() = MSVCRT_EDOM;
3106 MSVCRT_fegetenv(&env);
3107 env.status |= MSVCRT__SW_INVALID;
3108 MSVCRT_fesetenv(&env);
3109 return NAN;
3112 ret = atanhf(x);
3114 if (!finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3115 return ret;
3116 #else
3117 return MSVCR120_atanh(x);
3118 #endif
3121 /*********************************************************************
3122 * atanhl (MSVCR120.@)
3124 LDOUBLE CDECL MSVCR120_atanhl(LDOUBLE x)
3126 return MSVCR120_atanh(x);
3129 /*********************************************************************
3130 * _scalb (MSVCRT.@)
3131 * scalbn (MSVCR120.@)
3132 * scalbln (MSVCR120.@)
3134 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
3136 return MSVCRT_ldexp(num, power);
3139 /*********************************************************************
3140 * _scalbf (MSVCRT.@)
3141 * scalbnf (MSVCR120.@)
3142 * scalblnf (MSVCR120.@)
3144 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
3146 return MSVCRT_ldexp(num, power);
3149 /*********************************************************************
3150 * scalbnl (MSVCR120.@)
3151 * scalblnl (MSVCR120.@)
3153 LDOUBLE CDECL MSVCR120_scalbnl(LDOUBLE num, MSVCRT_long power)
3155 return MSVCRT__scalb(num, power);
3158 /*********************************************************************
3159 * remainder (MSVCR120.@)
3161 double CDECL MSVCR120_remainder(double x, double y)
3163 #ifdef HAVE_REMAINDER
3164 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3165 if(!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
3166 if(isnan(y) || y==0.0) *MSVCRT__errno() = MSVCRT_EDOM;
3167 return remainder(x, y);
3168 #else
3169 FIXME( "not implemented\n" );
3170 return 0.0;
3171 #endif
3174 /*********************************************************************
3175 * remainderf (MSVCR120.@)
3177 float CDECL MSVCR120_remainderf(float x, float y)
3179 #ifdef HAVE_REMAINDERF
3180 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3181 if(!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
3182 if(isnanf(y) || y==0.0f) *MSVCRT__errno() = MSVCRT_EDOM;
3183 return remainderf(x, y);
3184 #else
3185 FIXME( "not implemented\n" );
3186 return 0.0f;
3187 #endif
3190 /*********************************************************************
3191 * remainderl (MSVCR120.@)
3193 LDOUBLE CDECL MSVCR120_remainderl(LDOUBLE x, LDOUBLE y)
3195 return MSVCR120_remainder(x, y);
3198 /*********************************************************************
3199 * lgamma (MSVCR120.@)
3201 double CDECL MSVCR120_lgamma(double x)
3203 #ifdef HAVE_LGAMMA
3204 return lgamma(x);
3205 #else
3206 FIXME( "not implemented\n" );
3207 return 0.0;
3208 #endif
3211 /*********************************************************************
3212 * lgammaf (MSVCR120.@)
3214 float CDECL MSVCR120_lgammaf(float x)
3216 #ifdef HAVE_LGAMMAF
3217 return lgammaf(x);
3218 #else
3219 FIXME( "not implemented\n" );
3220 return 0.0f;
3221 #endif
3224 /*********************************************************************
3225 * lgammal (MSVCR120.@)
3227 LDOUBLE CDECL MSVCR120_lgammal(LDOUBLE x)
3229 return MSVCR120_lgamma(x);
3232 /*********************************************************************
3233 * nan (MSVCR120.@)
3235 double CDECL MSVCR120_nan(const char *tagp)
3237 /* Windows ignores input (MSDN) */
3238 return NAN;
3241 /*********************************************************************
3242 * nanf (MSVCR120.@)
3244 float CDECL MSVCR120_nanf(const char *tagp)
3246 return NAN;
3249 /*********************************************************************
3250 * _except1 (MSVCR120.@)
3251 * TODO:
3252 * - find meaning of ignored cw and operation bits
3253 * - unk parameter
3255 double CDECL _except1(DWORD fpe, _FP_OPERATION_CODE op, double arg, double res, DWORD cw, void *unk)
3257 ULONG_PTR exception_arg;
3258 DWORD exception = 0;
3259 MSVCRT_fenv_t env;
3260 DWORD fpword = 0;
3261 WORD operation;
3263 TRACE("(%x %x %lf %lf %x %p)\n", fpe, op, arg, res, cw, unk);
3265 #ifdef _WIN64
3266 cw = ((cw >> 7) & 0x3f) | ((cw >> 3) & 0xc00);
3267 #endif
3268 operation = op << 5;
3269 exception_arg = (ULONG_PTR)&operation;
3271 MSVCRT_fegetenv(&env);
3273 if (fpe & 0x1) { /* overflow */
3274 if ((fpe == 0x1 && (cw & 0x8)) || (fpe==0x11 && (cw & 0x28))) {
3275 /* 32-bit version also sets SW_INEXACT here */
3276 env.status |= MSVCRT__SW_OVERFLOW;
3277 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
3278 res = signbit(res) ? -INFINITY : INFINITY;
3279 } else {
3280 exception = EXCEPTION_FLT_OVERFLOW;
3282 } else if (fpe & 0x2) { /* underflow */
3283 if ((fpe == 0x2 && (cw & 0x10)) || (fpe==0x12 && (cw & 0x30))) {
3284 env.status |= MSVCRT__SW_UNDERFLOW;
3285 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
3286 res = signbit(res) ? -0.0 : 0.0;
3287 } else {
3288 exception = EXCEPTION_FLT_UNDERFLOW;
3290 } else if (fpe & 0x4) { /* zerodivide */
3291 if ((fpe == 0x4 && (cw & 0x4)) || (fpe==0x14 && (cw & 0x24))) {
3292 env.status |= MSVCRT__SW_ZERODIVIDE;
3293 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
3294 } else {
3295 exception = EXCEPTION_FLT_DIVIDE_BY_ZERO;
3297 } else if (fpe & 0x8) { /* invalid */
3298 if (fpe == 0x8 && (cw & 0x1)) {
3299 env.status |= MSVCRT__SW_INVALID;
3300 } else {
3301 exception = EXCEPTION_FLT_INVALID_OPERATION;
3303 } else if (fpe & 0x10) { /* inexact */
3304 if (fpe == 0x10 && (cw & 0x20)) {
3305 env.status |= MSVCRT__SW_INEXACT;
3306 } else {
3307 exception = EXCEPTION_FLT_INEXACT_RESULT;
3311 if (exception)
3312 env.status = 0;
3313 MSVCRT_fesetenv(&env);
3314 if (exception)
3315 RaiseException(exception, 0, 1, &exception_arg);
3317 if (cw & 0x1) fpword |= MSVCRT__EM_INVALID;
3318 if (cw & 0x2) fpword |= MSVCRT__EM_DENORMAL;
3319 if (cw & 0x4) fpword |= MSVCRT__EM_ZERODIVIDE;
3320 if (cw & 0x8) fpword |= MSVCRT__EM_OVERFLOW;
3321 if (cw & 0x10) fpword |= MSVCRT__EM_UNDERFLOW;
3322 if (cw & 0x20) fpword |= MSVCRT__EM_INEXACT;
3323 switch (cw & 0xc00)
3325 case 0xc00: fpword |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
3326 case 0x800: fpword |= MSVCRT__RC_UP; break;
3327 case 0x400: fpword |= MSVCRT__RC_DOWN; break;
3329 switch (cw & 0x300)
3331 case 0x0: fpword |= MSVCRT__PC_24; break;
3332 case 0x200: fpword |= MSVCRT__PC_53; break;
3333 case 0x300: fpword |= MSVCRT__PC_64; break;
3335 if (cw & 0x1000) fpword |= MSVCRT__IC_AFFINE;
3336 _control87(fpword, 0xffffffff);
3338 return res;