ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / dlls / msvcrt / math.c
blob89400f833c905bef6f3cfd8695d8860a59dd8df9
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 #if defined(_WIN64) && _MSVCR_VER>=120
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 $0xc00, %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 #if _MSVCR_VER>=100
817 /*********************************************************************
818 * llabs (MSVCR100.@)
820 MSVCRT_longlong CDECL MSVCRT_llabs( MSVCRT_longlong n )
822 return n >= 0 ? n : -n;
824 #endif
826 /*********************************************************************
827 * _abs64 (MSVCRT.@)
829 __int64 CDECL _abs64( __int64 n )
831 return n >= 0 ? n : -n;
834 /*********************************************************************
835 * _logb (MSVCRT.@)
837 double CDECL MSVCRT__logb(double num)
839 double ret = logb(num);
840 if (isnan(num)) math_error(_DOMAIN, "_logb", num, 0, ret);
841 else if (!num) math_error(_SING, "_logb", num, 0, ret);
842 return ret;
845 /*********************************************************************
846 * _hypot (MSVCRT.@)
848 double CDECL _hypot(double x, double y)
850 /* FIXME: errno handling */
851 return hypot( x, y );
854 /*********************************************************************
855 * _hypotf (MSVCRT.@)
857 float CDECL MSVCRT__hypotf(float x, float y)
859 /* FIXME: errno handling */
860 return hypotf( x, y );
863 /*********************************************************************
864 * ceil (MSVCRT.@)
866 double CDECL MSVCRT_ceil( double x )
868 return ceil(x);
871 /*********************************************************************
872 * floor (MSVCRT.@)
874 double CDECL MSVCRT_floor( double x )
876 return floor(x);
879 /*********************************************************************
880 * fabs (MSVCRT.@)
882 double CDECL MSVCRT_fabs( double x )
884 return fabs(x);
887 /*********************************************************************
888 * frexp (MSVCRT.@)
890 double CDECL MSVCRT_frexp( double x, int *exp )
892 return frexp( x, exp );
895 /*********************************************************************
896 * modf (MSVCRT.@)
898 double CDECL MSVCRT_modf( double x, double *iptr )
900 return modf( x, iptr );
903 /**********************************************************************
904 * _statusfp2 (MSVCRT.@)
906 * Not exported by native msvcrt, added in msvcr80.
908 #if defined(__i386__) || defined(__x86_64__)
909 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
911 #ifdef __GNUC__
912 unsigned int flags;
913 unsigned long fpword;
915 if (x86_sw)
917 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
918 flags = 0;
919 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
920 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
921 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
922 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
923 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
924 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
925 *x86_sw = flags;
928 if (!sse2_sw) return;
930 if (sse2_supported)
932 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
933 flags = 0;
934 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
935 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
936 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
937 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
938 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
939 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
940 *sse2_sw = flags;
942 else *sse2_sw = 0;
943 #else
944 FIXME( "not implemented\n" );
945 #endif
947 #endif
949 /**********************************************************************
950 * _statusfp (MSVCRT.@)
952 unsigned int CDECL _statusfp(void)
954 #if defined(__i386__) || defined(__x86_64__)
955 unsigned int x86_sw, sse2_sw;
957 _statusfp2( &x86_sw, &sse2_sw );
958 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
959 return x86_sw | sse2_sw;
960 #else
961 FIXME( "not implemented\n" );
962 return 0;
963 #endif
966 /*********************************************************************
967 * _clearfp (MSVCRT.@)
969 unsigned int CDECL _clearfp(void)
971 unsigned int flags = 0;
972 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
973 unsigned long fpword;
975 __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
976 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
977 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
978 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
979 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
980 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
981 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
983 if (sse2_supported)
985 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
986 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
987 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
988 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
989 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
990 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
991 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
992 fpword &= ~0x3f;
993 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
995 #else
996 FIXME( "not implemented\n" );
997 #endif
998 return flags;
1001 /*********************************************************************
1002 * __fpecode (MSVCRT.@)
1004 int * CDECL __fpecode(void)
1006 return &msvcrt_get_thread_data()->fpecode;
1009 /*********************************************************************
1010 * ldexp (MSVCRT.@)
1012 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
1014 double z = ldexp(num,exp);
1016 if (isfinite(num) && !isfinite(z))
1017 math_error(_OVERFLOW, "ldexp", num, exp, z);
1018 else if (isfinite(num) && !z)
1019 math_error(_UNDERFLOW, "ldexp", num, exp, z);
1020 else if (z == 0 && signbit(z))
1021 z = 0.0; /* Convert -0 -> +0 */
1022 return z;
1025 /*********************************************************************
1026 * _cabs (MSVCRT.@)
1028 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
1030 return sqrt(num.x * num.x + num.y * num.y);
1033 /*********************************************************************
1034 * _chgsign (MSVCRT.@)
1036 double CDECL MSVCRT__chgsign(double num)
1038 /* FIXME: +-infinity,Nan not tested */
1039 return -num;
1042 /*********************************************************************
1043 * __control87_2 (MSVCRT.@)
1045 * Not exported by native msvcrt, added in msvcr80.
1047 #if defined(__i386__) || defined(__x86_64__)
1048 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1049 unsigned int *x86_cw, unsigned int *sse2_cw )
1051 #ifdef __GNUC__
1052 unsigned long fpword;
1053 unsigned int flags;
1055 if (x86_cw)
1057 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1059 /* Convert into mask constants */
1060 flags = 0;
1061 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
1062 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
1063 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
1064 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
1065 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1066 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1067 switch (fpword & 0xc00)
1069 case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1070 case 0x800: flags |= MSVCRT__RC_UP; break;
1071 case 0x400: flags |= MSVCRT__RC_DOWN; break;
1073 switch (fpword & 0x300)
1075 case 0x0: flags |= MSVCRT__PC_24; break;
1076 case 0x200: flags |= MSVCRT__PC_53; break;
1077 case 0x300: flags |= MSVCRT__PC_64; break;
1079 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1081 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1082 if (mask)
1084 flags = (flags & ~mask) | (newval & mask);
1086 /* Convert (masked) value back to fp word */
1087 fpword = 0;
1088 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
1089 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
1090 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1091 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
1092 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
1093 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
1094 switch (flags & MSVCRT__MCW_RC)
1096 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1097 case MSVCRT__RC_UP: fpword |= 0x800; break;
1098 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
1100 switch (flags & MSVCRT__MCW_PC)
1102 case MSVCRT__PC_64: fpword |= 0x300; break;
1103 case MSVCRT__PC_53: fpword |= 0x200; break;
1104 case MSVCRT__PC_24: fpword |= 0x0; break;
1106 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1108 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1110 *x86_cw = flags;
1113 if (!sse2_cw) return 1;
1115 if (sse2_supported)
1117 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1119 /* Convert into mask constants */
1120 flags = 0;
1121 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1122 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1123 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1124 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1125 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1126 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1127 switch (fpword & 0x6000)
1129 case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1130 case 0x4000: flags |= MSVCRT__RC_UP; break;
1131 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1133 switch (fpword & 0x8040)
1135 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1136 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1137 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1140 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1141 if (mask)
1143 flags = (flags & ~mask) | (newval & mask);
1145 /* Convert (masked) value back to fp word */
1146 fpword = 0;
1147 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1148 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1149 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1150 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1151 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1152 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1153 switch (flags & MSVCRT__MCW_RC)
1155 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1156 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1157 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1159 switch (flags & MSVCRT__MCW_DN)
1161 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1162 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1163 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
1165 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1167 *sse2_cw = flags;
1169 else *sse2_cw = 0;
1171 return 1;
1172 #else
1173 FIXME( "not implemented\n" );
1174 return 0;
1175 #endif
1177 #endif
1179 /*********************************************************************
1180 * _control87 (MSVCRT.@)
1182 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1184 #if defined(__i386__) || defined(__x86_64__)
1185 unsigned int x86_cw, sse2_cw;
1187 __control87_2( newval, mask, &x86_cw, &sse2_cw );
1189 if ((x86_cw ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) x86_cw |= MSVCRT__EM_AMBIGUOUS;
1190 return x86_cw;
1191 #else
1192 FIXME( "not implemented\n" );
1193 return 0;
1194 #endif
1197 /*********************************************************************
1198 * _controlfp (MSVCRT.@)
1200 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
1202 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
1205 /*********************************************************************
1206 * _set_controlfp (MSVCRT.@)
1208 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
1210 _controlfp( newval, mask );
1213 /*********************************************************************
1214 * _controlfp_s (MSVCRT.@)
1216 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
1218 static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
1219 MSVCRT__MCW_PC | MSVCRT__MCW_DN);
1220 unsigned int val;
1222 if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
1224 if (cur) *cur = _controlfp( 0, 0 ); /* retrieve it anyway */
1225 return MSVCRT_EINVAL;
1227 val = _controlfp( newval, mask );
1228 if (cur) *cur = val;
1229 return 0;
1232 #if _MSVCR_VER>=120
1233 /*********************************************************************
1234 * fegetenv (MSVCR120.@)
1236 int CDECL MSVCRT_fegetenv(MSVCRT_fenv_t *env)
1238 env->control = _controlfp(0, 0) & (MSVCRT__EM_INEXACT | MSVCRT__EM_UNDERFLOW |
1239 MSVCRT__EM_OVERFLOW | MSVCRT__EM_ZERODIVIDE | MSVCRT__EM_INVALID);
1240 env->status = _statusfp();
1241 return 0;
1243 #endif
1245 #if _MSVCR_VER>=140
1246 /*********************************************************************
1247 * __fpe_flt_rounds (UCRTBASE.@)
1249 int CDECL __fpe_flt_rounds(void)
1251 unsigned int fpc = _controlfp(0, 0) & MSVCRT__RC_CHOP;
1253 TRACE("()\n");
1255 switch(fpc) {
1256 case MSVCRT__RC_CHOP: return 0;
1257 case MSVCRT__RC_NEAR: return 1;
1258 #ifdef _WIN64
1259 case MSVCRT__RC_UP: return 3;
1260 default: return 2;
1261 #else
1262 case MSVCRT__RC_UP: return 2;
1263 default: return 3;
1264 #endif
1267 #endif
1269 #if _MSVCR_VER>=120
1271 /*********************************************************************
1272 * fegetround (MSVCR120.@)
1274 int CDECL MSVCRT_fegetround(void)
1276 return _controlfp(0, 0) & MSVCRT__RC_CHOP;
1279 /*********************************************************************
1280 * fesetround (MSVCR120.@)
1282 int CDECL MSVCRT_fesetround(int round_mode)
1284 if (round_mode & (~MSVCRT__RC_CHOP))
1285 return 1;
1286 _controlfp(round_mode, MSVCRT__RC_CHOP);
1287 return 0;
1290 #endif /* _MSVCR_VER>=120 */
1292 /*********************************************************************
1293 * _copysign (MSVCRT.@)
1295 double CDECL MSVCRT__copysign(double num, double sign)
1297 if (signbit(sign))
1298 return signbit(num) ? num : -num;
1299 return signbit(num) ? -num : num;
1302 /*********************************************************************
1303 * _finite (MSVCRT.@)
1305 int CDECL MSVCRT__finite(double num)
1307 return isfinite(num) != 0; /* See comment for _isnan() */
1310 /*********************************************************************
1311 * _fpreset (MSVCRT.@)
1313 void CDECL _fpreset(void)
1315 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1316 const unsigned int x86_cw = 0x27f;
1317 __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
1318 if (sse2_supported)
1320 const unsigned long sse2_cw = 0x1f80;
1321 __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
1323 #else
1324 FIXME( "not implemented\n" );
1325 #endif
1328 #if _MSVCR_VER>=120
1329 /*********************************************************************
1330 * fesetenv (MSVCR120.@)
1332 int CDECL MSVCRT_fesetenv(const MSVCRT_fenv_t *env)
1334 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1335 struct {
1336 WORD control_word;
1337 WORD unused1;
1338 WORD status_word;
1339 WORD unused2;
1340 WORD tag_word;
1341 WORD unused3;
1342 DWORD instruction_pointer;
1343 WORD code_segment;
1344 WORD unused4;
1345 DWORD operand_addr;
1346 WORD data_segment;
1347 WORD unused5;
1348 } fenv;
1350 TRACE( "(%p)\n", env );
1352 if (!env->control && !env->status) {
1353 _fpreset();
1354 return 0;
1357 __asm__ __volatile__( "fnstenv %0" : "=m" (fenv) );
1359 fenv.control_word &= ~0x3d;
1360 if (env->control & MSVCRT__EM_INVALID) fenv.control_word |= 0x1;
1361 if (env->control & MSVCRT__EM_ZERODIVIDE) fenv.control_word |= 0x4;
1362 if (env->control & MSVCRT__EM_OVERFLOW) fenv.control_word |= 0x8;
1363 if (env->control & MSVCRT__EM_UNDERFLOW) fenv.control_word |= 0x10;
1364 if (env->control & MSVCRT__EM_INEXACT) fenv.control_word |= 0x20;
1366 fenv.status_word &= ~0x3d;
1367 if (env->status & MSVCRT__SW_INVALID) fenv.status_word |= 0x1;
1368 if (env->status & MSVCRT__SW_ZERODIVIDE) fenv.status_word |= 0x4;
1369 if (env->status & MSVCRT__SW_OVERFLOW) fenv.status_word |= 0x8;
1370 if (env->status & MSVCRT__SW_UNDERFLOW) fenv.status_word |= 0x10;
1371 if (env->status & MSVCRT__SW_INEXACT) fenv.status_word |= 0x20;
1373 __asm__ __volatile__( "fldenv %0" : : "m" (fenv) : "st", "st(1)",
1374 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
1376 if (sse2_supported)
1378 DWORD fpword;
1380 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1381 fpword &= ~0x1e80;
1382 if (env->control & MSVCRT__EM_INVALID) fpword |= 0x80;
1383 if (env->control & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1384 if (env->control & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1385 if (env->control & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1386 if (env->control & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1387 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1390 return 0;
1391 #else
1392 FIXME( "not implemented\n" );
1393 #endif
1394 return 1;
1396 #endif
1398 /*********************************************************************
1399 * _isnan (MSVCRT.@)
1401 INT CDECL MSVCRT__isnan(double num)
1403 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1404 * Do the same, as the result may be used in calculations
1406 return isnan(num) != 0;
1409 /*********************************************************************
1410 * _j0 (MSVCRT.@)
1412 double CDECL MSVCRT__j0(double num)
1414 /* FIXME: errno handling */
1415 return j0(num);
1418 /*********************************************************************
1419 * _j1 (MSVCRT.@)
1421 double CDECL MSVCRT__j1(double num)
1423 /* FIXME: errno handling */
1424 return j1(num);
1427 /*********************************************************************
1428 * _jn (MSVCRT.@)
1430 double CDECL MSVCRT__jn(int n, double num)
1432 /* FIXME: errno handling */
1433 return jn(n, num);
1436 /*********************************************************************
1437 * _y0 (MSVCRT.@)
1439 double CDECL MSVCRT__y0(double num)
1441 double retval;
1442 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1443 retval = y0(num);
1444 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1446 *MSVCRT__errno() = MSVCRT_EDOM;
1447 retval = sqrt(-1);
1449 return retval;
1452 /*********************************************************************
1453 * _y1 (MSVCRT.@)
1455 double CDECL MSVCRT__y1(double num)
1457 double retval;
1458 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1459 retval = y1(num);
1460 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1462 *MSVCRT__errno() = MSVCRT_EDOM;
1463 retval = sqrt(-1);
1465 return retval;
1468 /*********************************************************************
1469 * _yn (MSVCRT.@)
1471 double CDECL MSVCRT__yn(int order, double num)
1473 double retval;
1474 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
1475 retval = yn(order,num);
1476 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
1478 *MSVCRT__errno() = MSVCRT_EDOM;
1479 retval = sqrt(-1);
1481 return retval;
1484 #if _MSVCR_VER>=120
1486 /*********************************************************************
1487 * _nearbyint (MSVCR120.@)
1489 double CDECL MSVCRT_nearbyint(double num)
1491 #ifdef HAVE_NEARBYINT
1492 return nearbyint(num);
1493 #else
1494 return num >= 0 ? floor(num + 0.5) : ceil(num - 0.5);
1495 #endif
1498 /*********************************************************************
1499 * _nearbyintf (MSVCR120.@)
1501 float CDECL MSVCRT_nearbyintf(float num)
1503 #ifdef HAVE_NEARBYINTF
1504 return nearbyintf(num);
1505 #else
1506 return MSVCRT_nearbyint(num);
1507 #endif
1510 #endif /* _MSVCR_VER>=120 */
1512 /*********************************************************************
1513 * _nextafter (MSVCRT.@)
1515 double CDECL MSVCRT__nextafter(double num, double next)
1517 double retval;
1518 if (!isfinite(num) || !isfinite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
1519 retval = nextafter(num,next);
1520 return retval;
1523 /*********************************************************************
1524 * _ecvt (MSVCRT.@)
1526 char * CDECL MSVCRT__ecvt( double number, int ndigits, int *decpt, int *sign )
1528 int prec, len;
1529 thread_data_t *data = msvcrt_get_thread_data();
1530 /* FIXME: check better for overflow (native supports over 300 chars) */
1531 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
1532 * 4 for exponent and one for
1533 * terminating '\0' */
1534 if (!data->efcvt_buffer)
1535 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1537 if( number < 0) {
1538 *sign = TRUE;
1539 number = -number;
1540 } else
1541 *sign = FALSE;
1542 /* handle cases with zero ndigits or less */
1543 prec = ndigits;
1544 if( prec < 1) prec = 2;
1545 len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
1546 /* take the decimal "point away */
1547 if( prec != 1)
1548 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
1549 /* take the exponential "e" out */
1550 data->efcvt_buffer[ prec] = '\0';
1551 /* read the exponent */
1552 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
1553 (*decpt)++;
1554 /* adjust for some border cases */
1555 if( data->efcvt_buffer[0] == '0')/* value is zero */
1556 *decpt = 0;
1557 /* handle cases with zero ndigits or less */
1558 if( ndigits < 1){
1559 if( data->efcvt_buffer[ 0] >= '5')
1560 (*decpt)++;
1561 data->efcvt_buffer[ 0] = '\0';
1563 TRACE("out=\"%s\"\n",data->efcvt_buffer);
1564 return data->efcvt_buffer;
1567 /*********************************************************************
1568 * _ecvt_s (MSVCRT.@)
1570 int CDECL MSVCRT__ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
1572 int prec, len;
1573 char *result;
1574 const char infret[] = "1#INF";
1576 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
1577 if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
1578 if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
1579 if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1580 if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
1582 /* special case - inf */
1583 if(number == HUGE_VAL || number == -HUGE_VAL)
1585 memset(buffer, '0', ndigits);
1586 memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
1587 buffer[ndigits] = '\0';
1588 (*decpt) = 1;
1589 if(number == -HUGE_VAL)
1590 (*sign) = 1;
1591 else
1592 (*sign) = 0;
1593 return 0;
1595 /* handle cases with zero ndigits or less */
1596 prec = ndigits;
1597 if( prec < 1) prec = 2;
1598 result = MSVCRT_malloc(prec + 7);
1600 if( number < 0) {
1601 *sign = TRUE;
1602 number = -number;
1603 } else
1604 *sign = FALSE;
1605 len = snprintf(result, prec + 7, "%.*le", prec - 1, number);
1606 /* take the decimal "point away */
1607 if( prec != 1)
1608 memmove( result + 1, result + 2, len - 1 );
1609 /* take the exponential "e" out */
1610 result[ prec] = '\0';
1611 /* read the exponent */
1612 sscanf( result + prec + 1, "%d", decpt);
1613 (*decpt)++;
1614 /* adjust for some border cases */
1615 if( result[0] == '0')/* value is zero */
1616 *decpt = 0;
1617 /* handle cases with zero ndigits or less */
1618 if( ndigits < 1){
1619 if( result[ 0] >= '5')
1620 (*decpt)++;
1621 result[ 0] = '\0';
1623 memcpy( buffer, result, max(ndigits + 1, 1) );
1624 MSVCRT_free( result );
1625 return 0;
1628 /***********************************************************************
1629 * _fcvt (MSVCRT.@)
1631 char * CDECL MSVCRT__fcvt( double number, int ndigits, int *decpt, int *sign )
1633 thread_data_t *data = msvcrt_get_thread_data();
1634 int stop, dec1, dec2;
1635 char *ptr1, *ptr2, *first;
1636 char buf[80]; /* ought to be enough */
1638 if (!data->efcvt_buffer)
1639 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
1641 if (number < 0)
1643 *sign = 1;
1644 number = -number;
1645 } else *sign = 0;
1647 stop = snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1648 ptr1 = buf;
1649 ptr2 = data->efcvt_buffer;
1650 first = NULL;
1651 dec1 = 0;
1652 dec2 = 0;
1654 /* For numbers below the requested resolution, work out where
1655 the decimal point will be rather than finding it in the string */
1656 if (number < 1.0 && number > 0.0) {
1657 dec2 = log10(number + 1e-10);
1658 if (-dec2 <= ndigits) dec2 = 0;
1661 /* If requested digits is zero or less, we will need to truncate
1662 * the returned string */
1663 if (ndigits < 1) {
1664 stop += ndigits;
1667 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1668 while (*ptr1 != '\0' && *ptr1 != '.') {
1669 if (!first) first = ptr2;
1670 if ((ptr1 - buf) < stop) {
1671 *ptr2++ = *ptr1++;
1672 } else {
1673 ptr1++;
1675 dec1++;
1678 if (ndigits > 0) {
1679 ptr1++;
1680 if (!first) {
1681 while (*ptr1 == '0') { /* Process leading zeroes */
1682 *ptr2++ = *ptr1++;
1683 dec1--;
1686 while (*ptr1 != '\0') {
1687 if (!first) first = ptr2;
1688 *ptr2++ = *ptr1++;
1692 *ptr2 = '\0';
1694 /* We never found a non-zero digit, then our number is either
1695 * smaller than the requested precision, or 0.0 */
1696 if (!first) {
1697 if (number > 0.0) {
1698 first = ptr2;
1699 } else {
1700 first = data->efcvt_buffer;
1701 dec1 = 0;
1705 *decpt = dec2 ? dec2 : dec1;
1706 return first;
1709 /***********************************************************************
1710 * _fcvt_s (MSVCRT.@)
1712 int CDECL MSVCRT__fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
1714 int stop, dec1, dec2;
1715 char *ptr1, *ptr2, *first;
1716 char buf[80]; /* ought to be enough */
1718 if (!outbuffer || !decpt || !sign || size == 0)
1720 *MSVCRT__errno() = MSVCRT_EINVAL;
1721 return MSVCRT_EINVAL;
1724 if (number < 0)
1726 *sign = 1;
1727 number = -number;
1728 } else *sign = 0;
1730 stop = snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
1731 ptr1 = buf;
1732 ptr2 = outbuffer;
1733 first = NULL;
1734 dec1 = 0;
1735 dec2 = 0;
1737 /* For numbers below the requested resolution, work out where
1738 the decimal point will be rather than finding it in the string */
1739 if (number < 1.0 && number > 0.0) {
1740 dec2 = log10(number + 1e-10);
1741 if (-dec2 <= ndigits) dec2 = 0;
1744 /* If requested digits is zero or less, we will need to truncate
1745 * the returned string */
1746 if (ndigits < 1) {
1747 stop += ndigits;
1750 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
1751 while (*ptr1 != '\0' && *ptr1 != '.') {
1752 if (!first) first = ptr2;
1753 if ((ptr1 - buf) < stop) {
1754 if (size > 1) {
1755 *ptr2++ = *ptr1++;
1756 size--;
1758 } else {
1759 ptr1++;
1761 dec1++;
1764 if (ndigits > 0) {
1765 ptr1++;
1766 if (!first) {
1767 while (*ptr1 == '0') { /* Process leading zeroes */
1768 if (number == 0.0 && size > 1) {
1769 *ptr2++ = '0';
1770 size--;
1772 ptr1++;
1773 dec1--;
1776 while (*ptr1 != '\0') {
1777 if (!first) first = ptr2;
1778 if (size > 1) {
1779 *ptr2++ = *ptr1++;
1780 size--;
1785 *ptr2 = '\0';
1787 /* We never found a non-zero digit, then our number is either
1788 * smaller than the requested precision, or 0.0 */
1789 if (!first && (number <= 0.0))
1790 dec1 = 0;
1792 *decpt = dec2 ? dec2 : dec1;
1793 return 0;
1796 /***********************************************************************
1797 * _gcvt (MSVCRT.@)
1799 char * CDECL MSVCRT__gcvt( double number, int ndigit, char *buff )
1801 if(!buff) {
1802 *MSVCRT__errno() = MSVCRT_EINVAL;
1803 return NULL;
1806 if(ndigit < 0) {
1807 *MSVCRT__errno() = MSVCRT_ERANGE;
1808 return NULL;
1811 MSVCRT_sprintf(buff, "%.*g", ndigit, number);
1812 return buff;
1815 /***********************************************************************
1816 * _gcvt_s (MSVCRT.@)
1818 int CDECL MSVCRT__gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
1820 int len;
1822 if(!buff) {
1823 *MSVCRT__errno() = MSVCRT_EINVAL;
1824 return MSVCRT_EINVAL;
1827 if( digits<0 || digits>=size) {
1828 if(size)
1829 buff[0] = '\0';
1831 *MSVCRT__errno() = MSVCRT_ERANGE;
1832 return MSVCRT_ERANGE;
1835 len = MSVCRT__scprintf("%.*g", digits, number);
1836 if(len > size) {
1837 buff[0] = '\0';
1838 *MSVCRT__errno() = MSVCRT_ERANGE;
1839 return MSVCRT_ERANGE;
1842 MSVCRT_sprintf(buff, "%.*g", digits, number);
1843 return 0;
1846 #include <stdlib.h> /* div_t, ldiv_t */
1848 /*********************************************************************
1849 * div (MSVCRT.@)
1850 * VERSION
1851 * [i386] Windows binary compatible - returns the struct in eax/edx.
1853 #ifdef __i386__
1854 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
1856 div_t dt = div(num,denom);
1857 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
1859 #else
1860 /*********************************************************************
1861 * div (MSVCRT.@)
1862 * VERSION
1863 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1865 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
1867 div_t dt = div(num,denom);
1868 MSVCRT_div_t ret;
1869 ret.quot = dt.quot;
1870 ret.rem = dt.rem;
1872 return ret;
1875 #endif /* ifdef __i386__ */
1878 /*********************************************************************
1879 * ldiv (MSVCRT.@)
1880 * VERSION
1881 * [i386] Windows binary compatible - returns the struct in eax/edx.
1883 #ifdef __i386__
1884 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1886 ldiv_t ldt = ldiv(num,denom);
1887 return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1889 #else
1890 /*********************************************************************
1891 * ldiv (MSVCRT.@)
1892 * VERSION
1893 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1895 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1897 ldiv_t result = ldiv(num,denom);
1899 MSVCRT_ldiv_t ret;
1900 ret.quot = result.quot;
1901 ret.rem = result.rem;
1903 return ret;
1905 #endif /* ifdef __i386__ */
1907 #if _MSVCR_VER>=100
1908 /*********************************************************************
1909 * lldiv (MSVCR100.@)
1911 MSVCRT_lldiv_t CDECL MSVCRT_lldiv(MSVCRT_longlong num, MSVCRT_longlong denom)
1913 MSVCRT_lldiv_t ret;
1915 ret.quot = num / denom;
1916 ret.rem = num % denom;
1918 return ret;
1920 #endif
1922 #ifdef __i386__
1924 /*********************************************************************
1925 * _adjust_fdiv (MSVCRT.@)
1926 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1928 int MSVCRT__adjust_fdiv = 0;
1930 /***********************************************************************
1931 * _adj_fdiv_m16i (MSVCRT.@)
1933 * NOTE
1934 * I _think_ this function is intended to work around the Pentium
1935 * fdiv bug.
1937 void __stdcall _adj_fdiv_m16i( short arg )
1939 TRACE("(): stub\n");
1942 /***********************************************************************
1943 * _adj_fdiv_m32 (MSVCRT.@)
1945 * NOTE
1946 * I _think_ this function is intended to work around the Pentium
1947 * fdiv bug.
1949 void __stdcall _adj_fdiv_m32( unsigned int arg )
1951 TRACE("(): stub\n");
1954 /***********************************************************************
1955 * _adj_fdiv_m32i (MSVCRT.@)
1957 * NOTE
1958 * I _think_ this function is intended to work around the Pentium
1959 * fdiv bug.
1961 void __stdcall _adj_fdiv_m32i( int arg )
1963 TRACE("(): stub\n");
1966 /***********************************************************************
1967 * _adj_fdiv_m64 (MSVCRT.@)
1969 * NOTE
1970 * I _think_ this function is intended to work around the Pentium
1971 * fdiv bug.
1973 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1975 TRACE("(): stub\n");
1978 /***********************************************************************
1979 * _adj_fdiv_r (MSVCRT.@)
1980 * FIXME
1981 * This function is likely to have the wrong number of arguments.
1983 * NOTE
1984 * I _think_ this function is intended to work around the Pentium
1985 * fdiv bug.
1987 void _adj_fdiv_r(void)
1989 TRACE("(): stub\n");
1992 /***********************************************************************
1993 * _adj_fdivr_m16i (MSVCRT.@)
1995 * NOTE
1996 * I _think_ this function is intended to work around the Pentium
1997 * fdiv bug.
1999 void __stdcall _adj_fdivr_m16i( short arg )
2001 TRACE("(): stub\n");
2004 /***********************************************************************
2005 * _adj_fdivr_m32 (MSVCRT.@)
2007 * NOTE
2008 * I _think_ this function is intended to work around the Pentium
2009 * fdiv bug.
2011 void __stdcall _adj_fdivr_m32( unsigned int arg )
2013 TRACE("(): stub\n");
2016 /***********************************************************************
2017 * _adj_fdivr_m32i (MSVCRT.@)
2019 * NOTE
2020 * I _think_ this function is intended to work around the Pentium
2021 * fdiv bug.
2023 void __stdcall _adj_fdivr_m32i( int arg )
2025 TRACE("(): stub\n");
2028 /***********************************************************************
2029 * _adj_fdivr_m64 (MSVCRT.@)
2031 * NOTE
2032 * I _think_ this function is intended to work around the Pentium
2033 * fdiv bug.
2035 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
2037 TRACE("(): stub\n");
2040 /***********************************************************************
2041 * _adj_fpatan (MSVCRT.@)
2042 * FIXME
2043 * This function is likely to have the wrong number of arguments.
2045 * NOTE
2046 * I _think_ this function is intended to work around the Pentium
2047 * fdiv bug.
2049 void _adj_fpatan(void)
2051 TRACE("(): stub\n");
2054 /***********************************************************************
2055 * _adj_fprem (MSVCRT.@)
2056 * FIXME
2057 * This function is likely to have the wrong number of arguments.
2059 * NOTE
2060 * I _think_ this function is intended to work around the Pentium
2061 * fdiv bug.
2063 void _adj_fprem(void)
2065 TRACE("(): stub\n");
2068 /***********************************************************************
2069 * _adj_fprem1 (MSVCRT.@)
2070 * FIXME
2071 * This function is likely to have the wrong number of arguments.
2073 * NOTE
2074 * I _think_ this function is intended to work around the Pentium
2075 * fdiv bug.
2077 void _adj_fprem1(void)
2079 TRACE("(): stub\n");
2082 /***********************************************************************
2083 * _adj_fptan (MSVCRT.@)
2084 * FIXME
2085 * This function is likely to have the wrong number of arguments.
2087 * NOTE
2088 * I _think_ this function is intended to work around the Pentium
2089 * fdiv bug.
2091 void _adj_fptan(void)
2093 TRACE("(): stub\n");
2096 /***********************************************************************
2097 * _safe_fdiv (MSVCRT.@)
2098 * FIXME
2099 * This function is likely to have the wrong number of arguments.
2101 * NOTE
2102 * I _think_ this function is intended to work around the Pentium
2103 * fdiv bug.
2105 void _safe_fdiv(void)
2107 TRACE("(): stub\n");
2110 /***********************************************************************
2111 * _safe_fdivr (MSVCRT.@)
2112 * FIXME
2113 * This function is likely to have the wrong number of arguments.
2115 * NOTE
2116 * I _think_ this function is intended to work around the Pentium
2117 * fdiv bug.
2119 void _safe_fdivr(void)
2121 TRACE("(): stub\n");
2124 /***********************************************************************
2125 * _safe_fprem (MSVCRT.@)
2126 * FIXME
2127 * This function is likely to have the wrong number of arguments.
2129 * NOTE
2130 * I _think_ this function is intended to work around the Pentium
2131 * fdiv bug.
2133 void _safe_fprem(void)
2135 TRACE("(): stub\n");
2138 /***********************************************************************
2139 * _safe_fprem1 (MSVCRT.@)
2141 * FIXME
2142 * This function is likely to have the wrong number of arguments.
2144 * NOTE
2145 * I _think_ this function is intended to work around the Pentium
2146 * fdiv bug.
2148 void _safe_fprem1(void)
2150 TRACE("(): stub\n");
2153 /***********************************************************************
2154 * __libm_sse2_acos (MSVCRT.@)
2156 void __cdecl MSVCRT___libm_sse2_acos(void)
2158 double d;
2159 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2160 d = acos( d );
2161 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2164 /***********************************************************************
2165 * __libm_sse2_acosf (MSVCRT.@)
2167 void __cdecl MSVCRT___libm_sse2_acosf(void)
2169 float f;
2170 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2171 f = acosf( f );
2172 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2175 /***********************************************************************
2176 * __libm_sse2_asin (MSVCRT.@)
2178 void __cdecl MSVCRT___libm_sse2_asin(void)
2180 double d;
2181 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2182 d = asin( d );
2183 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2186 /***********************************************************************
2187 * __libm_sse2_asinf (MSVCRT.@)
2189 void __cdecl MSVCRT___libm_sse2_asinf(void)
2191 float f;
2192 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2193 f = asinf( f );
2194 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2197 /***********************************************************************
2198 * __libm_sse2_atan (MSVCRT.@)
2200 void __cdecl MSVCRT___libm_sse2_atan(void)
2202 double d;
2203 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2204 d = atan( d );
2205 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2208 /***********************************************************************
2209 * __libm_sse2_atan2 (MSVCRT.@)
2211 void __cdecl MSVCRT___libm_sse2_atan2(void)
2213 double d1, d2;
2214 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2215 d1 = atan2( d1, d2 );
2216 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2219 /***********************************************************************
2220 * __libm_sse2_atanf (MSVCRT.@)
2222 void __cdecl MSVCRT___libm_sse2_atanf(void)
2224 float f;
2225 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2226 f = atanf( f );
2227 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2230 /***********************************************************************
2231 * __libm_sse2_cos (MSVCRT.@)
2233 void __cdecl MSVCRT___libm_sse2_cos(void)
2235 double d;
2236 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2237 d = cos( d );
2238 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2241 /***********************************************************************
2242 * __libm_sse2_cosf (MSVCRT.@)
2244 void __cdecl MSVCRT___libm_sse2_cosf(void)
2246 float f;
2247 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2248 f = cosf( f );
2249 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2252 /***********************************************************************
2253 * __libm_sse2_exp (MSVCRT.@)
2255 void __cdecl MSVCRT___libm_sse2_exp(void)
2257 double d;
2258 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2259 d = exp( d );
2260 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2263 /***********************************************************************
2264 * __libm_sse2_expf (MSVCRT.@)
2266 void __cdecl MSVCRT___libm_sse2_expf(void)
2268 float f;
2269 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2270 f = expf( f );
2271 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2274 /***********************************************************************
2275 * __libm_sse2_log (MSVCRT.@)
2277 void __cdecl MSVCRT___libm_sse2_log(void)
2279 double d;
2280 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2281 d = log( d );
2282 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2285 /***********************************************************************
2286 * __libm_sse2_log10 (MSVCRT.@)
2288 void __cdecl MSVCRT___libm_sse2_log10(void)
2290 double d;
2291 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2292 d = log10( d );
2293 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2296 /***********************************************************************
2297 * __libm_sse2_log10f (MSVCRT.@)
2299 void __cdecl MSVCRT___libm_sse2_log10f(void)
2301 float f;
2302 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2303 f = log10f( f );
2304 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2307 /***********************************************************************
2308 * __libm_sse2_logf (MSVCRT.@)
2310 void __cdecl MSVCRT___libm_sse2_logf(void)
2312 float f;
2313 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2314 f = logf( f );
2315 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2318 /***********************************************************************
2319 * __libm_sse2_pow (MSVCRT.@)
2321 void __cdecl MSVCRT___libm_sse2_pow(void)
2323 double d1, d2;
2324 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
2325 d1 = pow( d1, d2 );
2326 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
2329 /***********************************************************************
2330 * __libm_sse2_powf (MSVCRT.@)
2332 void __cdecl MSVCRT___libm_sse2_powf(void)
2334 float f1, f2;
2335 __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
2336 f1 = powf( f1, f2 );
2337 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
2340 /***********************************************************************
2341 * __libm_sse2_sin (MSVCRT.@)
2343 void __cdecl MSVCRT___libm_sse2_sin(void)
2345 double d;
2346 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2347 d = sin( d );
2348 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2351 /***********************************************************************
2352 * __libm_sse2_sinf (MSVCRT.@)
2354 void __cdecl MSVCRT___libm_sse2_sinf(void)
2356 float f;
2357 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2358 f = sinf( f );
2359 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2362 /***********************************************************************
2363 * __libm_sse2_tan (MSVCRT.@)
2365 void __cdecl MSVCRT___libm_sse2_tan(void)
2367 double d;
2368 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2369 d = tan( d );
2370 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2373 /***********************************************************************
2374 * __libm_sse2_tanf (MSVCRT.@)
2376 void __cdecl MSVCRT___libm_sse2_tanf(void)
2378 float f;
2379 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
2380 f = tanf( f );
2381 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
2384 /***********************************************************************
2385 * __libm_sse2_sqrt_precise (MSVCR110.@)
2387 void __cdecl MSVCRT___libm_sse2_sqrt_precise(void)
2389 double d;
2390 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
2391 d = sqrt( d );
2392 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
2395 #endif /* __i386__ */
2397 /*********************************************************************
2398 * cbrt (MSVCR120.@)
2400 double CDECL MSVCR120_cbrt(double x)
2402 #ifdef HAVE_CBRT
2403 return cbrt(x);
2404 #else
2405 return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
2406 #endif
2409 /*********************************************************************
2410 * cbrtf (MSVCR120.@)
2412 float CDECL MSVCR120_cbrtf(float x)
2414 #ifdef HAVE_CBRTF
2415 return cbrtf(x);
2416 #else
2417 return MSVCR120_cbrt(x);
2418 #endif
2421 /*********************************************************************
2422 * cbrtl (MSVCR120.@)
2424 LDOUBLE CDECL MSVCR120_cbrtl(LDOUBLE x)
2426 return MSVCR120_cbrt(x);
2429 /*********************************************************************
2430 * exp2 (MSVCR120.@)
2432 double CDECL MSVCR120_exp2(double x)
2434 #ifdef HAVE_EXP2
2435 double ret = exp2(x);
2436 #else
2437 double ret = pow(2, x);
2438 #endif
2439 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
2440 return ret;
2443 /*********************************************************************
2444 * exp2f (MSVCR120.@)
2446 float CDECL MSVCR120_exp2f(float x)
2448 #ifdef HAVE_EXP2F
2449 float ret = exp2f(x);
2450 if (finitef(x) && !finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
2451 return ret;
2452 #else
2453 return MSVCR120_exp2(x);
2454 #endif
2457 /*********************************************************************
2458 * exp2l (MSVCR120.@)
2460 LDOUBLE CDECL MSVCR120_exp2l(LDOUBLE x)
2462 return MSVCR120_exp2(x);
2465 /*********************************************************************
2466 * expm1 (MSVCR120.@)
2468 double CDECL MSVCR120_expm1(double x)
2470 #ifdef HAVE_EXPM1
2471 double ret = expm1(x);
2472 #else
2473 double ret = exp(x) - 1;
2474 #endif
2475 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
2476 return ret;
2479 /*********************************************************************
2480 * expm1f (MSVCR120.@)
2482 float CDECL MSVCR120_expm1f(float x)
2484 #ifdef HAVE_EXPM1F
2485 float ret = expm1f(x);
2486 #else
2487 float ret = exp(x) - 1;
2488 #endif
2489 if (finitef(x) && !finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
2490 return ret;
2493 /*********************************************************************
2494 * expm1l (MSVCR120.@)
2496 LDOUBLE CDECL MSVCR120_expm1l(LDOUBLE x)
2498 return MSVCR120_expm1(x);
2501 /*********************************************************************
2502 * log1p (MSVCR120.@)
2504 double CDECL MSVCR120_log1p(double x)
2506 if (x < -1) *MSVCRT__errno() = MSVCRT_EDOM;
2507 else if (x == -1) *MSVCRT__errno() = MSVCRT_ERANGE;
2508 #ifdef HAVE_LOG1P
2509 return log1p(x);
2510 #else
2511 return log(1 + x);
2512 #endif
2515 /*********************************************************************
2516 * log1pf (MSVCR120.@)
2518 float CDECL MSVCR120_log1pf(float x)
2520 if (x < -1) *MSVCRT__errno() = MSVCRT_EDOM;
2521 else if (x == -1) *MSVCRT__errno() = MSVCRT_ERANGE;
2522 #ifdef HAVE_LOG1PF
2523 return log1pf(x);
2524 #else
2525 return log(1 + x);
2526 #endif
2529 /*********************************************************************
2530 * log1pl (MSVCR120.@)
2532 LDOUBLE CDECL MSVCR120_log1pl(LDOUBLE x)
2534 return MSVCR120_log1p(x);
2537 /*********************************************************************
2538 * log2 (MSVCR120.@)
2540 double CDECL MSVCR120_log2(double x)
2542 if (x < 0) *MSVCRT__errno() = MSVCRT_EDOM;
2543 else if (x == 0) *MSVCRT__errno() = MSVCRT_ERANGE;
2544 #ifdef HAVE_LOG2
2545 return log2(x);
2546 #else
2547 return log(x) / log(2);
2548 #endif
2551 /*********************************************************************
2552 * log2f (MSVCR120.@)
2554 float CDECL MSVCR120_log2f(float x)
2556 #ifdef HAVE_LOG2F
2557 if (x < 0) *MSVCRT__errno() = MSVCRT_EDOM;
2558 else if (x == 0) *MSVCRT__errno() = MSVCRT_ERANGE;
2559 return log2f(x);
2560 #else
2561 return MSVCR120_log2(x);
2562 #endif
2565 /*********************************************************************
2566 * log2l (MSVCR120.@)
2568 LDOUBLE CDECL MSVCR120_log2l(LDOUBLE x)
2570 return MSVCR120_log2(x);
2573 /*********************************************************************
2574 * rint (MSVCR120.@)
2576 double CDECL MSVCR120_rint(double x)
2578 return rint(x);
2581 /*********************************************************************
2582 * rintf (MSVCR120.@)
2584 float CDECL MSVCR120_rintf(float x)
2586 return rintf(x);
2589 /*********************************************************************
2590 * rintl (MSVCR120.@)
2592 LDOUBLE CDECL MSVCR120_rintl(LDOUBLE x)
2594 return MSVCR120_rint(x);
2597 /*********************************************************************
2598 * lrint (MSVCR120.@)
2600 MSVCRT_long CDECL MSVCR120_lrint(double x)
2602 return lrint(x);
2605 /*********************************************************************
2606 * lrintf (MSVCR120.@)
2608 MSVCRT_long CDECL MSVCR120_lrintf(float x)
2610 return lrintf(x);
2613 /*********************************************************************
2614 * lrintl (MSVCR120.@)
2616 MSVCRT_long CDECL MSVCR120_lrintl(LDOUBLE x)
2618 return MSVCR120_lrint(x);
2621 /*********************************************************************
2622 * llrint (MSVCR120.@)
2624 MSVCRT_longlong CDECL MSVCR120_llrint(double x)
2626 return llrint(x);
2629 /*********************************************************************
2630 * llrintf (MSVCR120.@)
2632 MSVCRT_longlong CDECL MSVCR120_llrintf(float x)
2634 return llrintf(x);
2637 /*********************************************************************
2638 * rintl (MSVCR120.@)
2640 MSVCRT_longlong CDECL MSVCR120_llrintl(LDOUBLE x)
2642 return MSVCR120_llrint(x);
2645 #if _MSVCR_VER>=120
2647 /*********************************************************************
2648 * round (MSVCR120.@)
2650 double CDECL MSVCR120_round(double x)
2652 #ifdef HAVE_ROUND
2653 return round(x);
2654 #else
2655 return MSVCR120_rint(x);
2656 #endif
2659 /*********************************************************************
2660 * roundf (MSVCR120.@)
2662 float CDECL MSVCR120_roundf(float x)
2664 #ifdef HAVE_ROUNDF
2665 return roundf(x);
2666 #else
2667 return MSVCR120_round(x);
2668 #endif
2671 /*********************************************************************
2672 * roundl (MSVCR120.@)
2674 LDOUBLE CDECL MSVCR120_roundl(LDOUBLE x)
2676 return MSVCR120_round(x);
2679 /*********************************************************************
2680 * lround (MSVCR120.@)
2682 MSVCRT_long CDECL MSVCR120_lround(double x)
2684 #ifdef HAVE_LROUND
2685 return lround(x);
2686 #else
2687 return MSVCR120_round(x);
2688 #endif
2691 /*********************************************************************
2692 * lroundf (MSVCR120.@)
2694 MSVCRT_long CDECL MSVCR120_lroundf(float x)
2696 #ifdef HAVE_LROUNDF
2697 return lroundf(x);
2698 #else
2699 return MSVCR120_lround(x);
2700 #endif
2703 /*********************************************************************
2704 * lroundl (MSVCR120.@)
2706 MSVCRT_long CDECL MSVCR120_lroundl(LDOUBLE x)
2708 return MSVCR120_lround(x);
2711 /*********************************************************************
2712 * llround (MSVCR120.@)
2714 MSVCRT_longlong CDECL MSVCR120_llround(double x)
2716 #ifdef HAVE_LLROUND
2717 return llround(x);
2718 #else
2719 return MSVCR120_round(x);
2720 #endif
2723 /*********************************************************************
2724 * llroundf (MSVCR120.@)
2726 MSVCRT_longlong CDECL MSVCR120_llroundf(float x)
2728 #ifdef HAVE_LLROUNDF
2729 return llroundf(x);
2730 #else
2731 return MSVCR120_llround(x);
2732 #endif
2735 /*********************************************************************
2736 * roundl (MSVCR120.@)
2738 MSVCRT_longlong CDECL MSVCR120_llroundl(LDOUBLE x)
2740 return MSVCR120_llround(x);
2743 /*********************************************************************
2744 * trunc (MSVCR120.@)
2746 double CDECL MSVCR120_trunc(double x)
2748 #ifdef HAVE_TRUNC
2749 return trunc(x);
2750 #else
2751 return (x > 0) ? floor(x) : ceil(x);
2752 #endif
2755 /*********************************************************************
2756 * truncf (MSVCR120.@)
2758 float CDECL MSVCR120_truncf(float x)
2760 #ifdef HAVE_TRUNCF
2761 return truncf(x);
2762 #else
2763 return MSVCR120_trunc(x);
2764 #endif
2767 /*********************************************************************
2768 * truncl (MSVCR120.@)
2770 LDOUBLE CDECL MSVCR120_truncl(LDOUBLE x)
2772 return MSVCR120_trunc(x);
2775 /*********************************************************************
2776 * _dclass (MSVCR120.@)
2778 short CDECL MSVCR120__dclass(double x)
2780 switch (MSVCRT__fpclass(x)) {
2781 case MSVCRT__FPCLASS_QNAN:
2782 case MSVCRT__FPCLASS_SNAN:
2783 return MSVCRT_FP_NAN;
2784 case MSVCRT__FPCLASS_NINF:
2785 case MSVCRT__FPCLASS_PINF:
2786 return MSVCRT_FP_INFINITE;
2787 case MSVCRT__FPCLASS_ND:
2788 case MSVCRT__FPCLASS_PD:
2789 return MSVCRT_FP_SUBNORMAL;
2790 case MSVCRT__FPCLASS_NN:
2791 case MSVCRT__FPCLASS_PN:
2792 default:
2793 return MSVCRT_FP_NORMAL;
2794 case MSVCRT__FPCLASS_NZ:
2795 case MSVCRT__FPCLASS_PZ:
2796 return MSVCRT_FP_ZERO;
2800 /*********************************************************************
2801 * _fdclass (MSVCR120.@)
2803 short CDECL MSVCR120__fdclass(float x)
2805 return MSVCR120__dclass(x);
2808 /*********************************************************************
2809 * _ldclass (MSVCR120.@)
2811 short CDECL MSVCR120__ldclass(LDOUBLE x)
2813 return MSVCR120__dclass(x);
2816 /*********************************************************************
2817 * _dtest (MSVCR120.@)
2819 short CDECL MSVCR120__dtest(double *x)
2821 return MSVCR120__dclass(*x);
2824 /*********************************************************************
2825 * _fdtest (MSVCR120.@)
2827 short CDECL MSVCR120__fdtest(float *x)
2829 return MSVCR120__dclass(*x);
2832 /*********************************************************************
2833 * _ldtest (MSVCR120.@)
2835 short CDECL MSVCR120__ldtest(LDOUBLE *x)
2837 return MSVCR120__dclass(*x);
2840 /*********************************************************************
2841 * erf (MSVCR120.@)
2843 double CDECL MSVCR120_erf(double x)
2845 #ifdef HAVE_ERF
2846 return erf(x);
2847 #else
2848 /* Abramowitz and Stegun approximation, maximum error: 1.5*10^-7 */
2849 double t, y;
2850 int sign = signbit(x);
2852 if (sign) x = -x;
2853 t = 1 / (1 + 0.3275911 * x);
2854 y = ((((1.061405429*t - 1.453152027)*t + 1.421413741)*t - 0.284496736)*t + 0.254829592)*t;
2855 y = 1.0 - y*exp(-x*x);
2856 return sign ? -y : y;
2857 #endif
2860 /*********************************************************************
2861 * erff (MSVCR120.@)
2863 float CDECL MSVCR120_erff(float x)
2865 #ifdef HAVE_ERFF
2866 return erff(x);
2867 #else
2868 return MSVCR120_erf(x);
2869 #endif
2872 /*********************************************************************
2873 * erfl (MSVCR120.@)
2875 LDOUBLE CDECL MSVCR120_erfl(LDOUBLE x)
2877 return MSVCR120_erf(x);
2880 /*********************************************************************
2881 * erfc (MSVCR120.@)
2883 double CDECL MSVCR120_erfc(double x)
2885 #ifdef HAVE_ERFC
2886 return erfc(x);
2887 #else
2888 return 1 - MSVCR120_erf(x);
2889 #endif
2892 /*********************************************************************
2893 * erfcf (MSVCR120.@)
2895 float CDECL MSVCR120_erfcf(float x)
2897 #ifdef HAVE_ERFCF
2898 return erfcf(x);
2899 #else
2900 return MSVCR120_erfc(x);
2901 #endif
2904 /*********************************************************************
2905 * erfcl (MSVCR120.@)
2907 LDOUBLE CDECL MSVCR120_erfcl(LDOUBLE x)
2909 return MSVCR120_erfc(x);
2912 /*********************************************************************
2913 * fmaxf (MSVCR120.@)
2915 float CDECL MSVCR120_fmaxf(float x, float y)
2917 if(isnanf(x))
2918 return y;
2919 if(isnanf(y))
2920 return x;
2921 if(x==0 && y==0)
2922 return signbit(x) ? y : x;
2923 return x<y ? y : x;
2926 /*********************************************************************
2927 * fmax (MSVCR120.@)
2929 double CDECL MSVCR120_fmax(double x, double y)
2931 if(isnan(x))
2932 return y;
2933 if(isnan(y))
2934 return x;
2935 if(x==0 && y==0)
2936 return signbit(x) ? y : x;
2937 return x<y ? y : x;
2940 /*********************************************************************
2941 * _fdsign (MSVCR120.@)
2943 int CDECL MSVCR120__fdsign(float x)
2945 return signbit(x) ? 0x8000 : 0;
2948 /*********************************************************************
2949 * _dsign (MSVCR120.@)
2951 int CDECL MSVCR120__dsign(double x)
2953 return signbit(x) ? 0x8000 : 0;
2957 /*********************************************************************
2958 * _dpcomp (MSVCR120.@)
2960 int CDECL MSVCR120__dpcomp(double x, double y)
2962 if(isnan(x) || isnan(y))
2963 return 0;
2965 if(x == y) return 2;
2966 return x < y ? 1 : 4;
2969 /*********************************************************************
2970 * _fdpcomp (MSVCR120.@)
2972 int CDECL MSVCR120__fdpcomp(float x, float y)
2974 return MSVCR120__dpcomp(x, y);
2977 /*********************************************************************
2978 * fminf (MSVCR120.@)
2980 float CDECL MSVCR120_fminf(float x, float y)
2982 if(isnanf(x))
2983 return y;
2984 if(isnanf(y))
2985 return x;
2986 if(x==0 && y==0)
2987 return signbit(x) ? x : y;
2988 return x<y ? x : y;
2991 /*********************************************************************
2992 * fmin (MSVCR120.@)
2994 double CDECL MSVCR120_fmin(double x, double y)
2996 if(isnan(x))
2997 return y;
2998 if(isnan(y))
2999 return x;
3000 if(x==0 && y==0)
3001 return signbit(x) ? x : y;
3002 return x<y ? x : y;
3005 /*********************************************************************
3006 * asinh (MSVCR120.@)
3008 double CDECL MSVCR120_asinh(double x)
3010 #ifdef HAVE_ASINH
3011 return asinh(x);
3012 #else
3013 if (!isfinite(x*x+1)) return log(2) + log(x);
3014 return log(x + sqrt(x*x+1));
3015 #endif
3018 /*********************************************************************
3019 * asinhf (MSVCR120.@)
3021 float CDECL MSVCR120_asinhf(float x)
3023 #ifdef HAVE_ASINHF
3024 return asinhf(x);
3025 #else
3026 return MSVCR120_asinh(x);
3027 #endif
3030 /*********************************************************************
3031 * asinhl (MSVCR120.@)
3033 LDOUBLE CDECL MSVCR120_asinhl(LDOUBLE x)
3035 return MSVCR120_asinh(x);
3038 /*********************************************************************
3039 * acosh (MSVCR120.@)
3041 double CDECL MSVCR120_acosh(double x)
3043 if (x < 1) *MSVCRT__errno() = MSVCRT_EDOM;
3045 #ifdef HAVE_ACOSH
3046 return acosh(x);
3047 #else
3048 if (x < 1) {
3049 MSVCRT_fenv_t env;
3051 MSVCRT_fegetenv(&env);
3052 env.status |= MSVCRT__SW_INVALID;
3053 MSVCRT_fesetenv(&env);
3054 return NAN;
3056 if (!isfinite(x*x)) return log(2) + log(x);
3057 return log(x + sqrt(x*x-1));
3058 #endif
3061 /*********************************************************************
3062 * acoshf (MSVCR120.@)
3064 float CDECL MSVCR120_acoshf(float x)
3066 #ifdef HAVE_ACOSHF
3067 if (x < 1) *MSVCRT__errno() = MSVCRT_EDOM;
3069 return acoshf(x);
3070 #else
3071 return MSVCR120_acosh(x);
3072 #endif
3075 /*********************************************************************
3076 * acoshl (MSVCR120.@)
3078 LDOUBLE CDECL MSVCR120_acoshl(LDOUBLE x)
3080 return MSVCR120_acosh(x);
3083 /*********************************************************************
3084 * atanh (MSVCR120.@)
3086 double CDECL MSVCR120_atanh(double x)
3088 double ret;
3090 if (x > 1 || x < -1) {
3091 MSVCRT_fenv_t env;
3093 *MSVCRT__errno() = MSVCRT_EDOM;
3095 /* on Linux atanh returns -NAN in this case */
3096 MSVCRT_fegetenv(&env);
3097 env.status |= MSVCRT__SW_INVALID;
3098 MSVCRT_fesetenv(&env);
3099 return NAN;
3102 #ifdef HAVE_ATANH
3103 ret = atanh(x);
3104 #else
3105 if (-1e-6 < x && x < 1e-6) ret = x + x*x*x/3;
3106 else ret = (log(1+x) - log(1-x)) / 2;
3107 #endif
3109 if (!isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3110 return ret;
3113 /*********************************************************************
3114 * atanhf (MSVCR120.@)
3116 float CDECL MSVCR120_atanhf(float x)
3118 #ifdef HAVE_ATANHF
3119 float ret;
3121 if (x > 1 || x < -1) {
3122 MSVCRT_fenv_t env;
3124 *MSVCRT__errno() = MSVCRT_EDOM;
3126 MSVCRT_fegetenv(&env);
3127 env.status |= MSVCRT__SW_INVALID;
3128 MSVCRT_fesetenv(&env);
3129 return NAN;
3132 ret = atanhf(x);
3134 if (!finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3135 return ret;
3136 #else
3137 return MSVCR120_atanh(x);
3138 #endif
3141 /*********************************************************************
3142 * atanhl (MSVCR120.@)
3144 LDOUBLE CDECL MSVCR120_atanhl(LDOUBLE x)
3146 return MSVCR120_atanh(x);
3149 #endif /* _MSVCR_VER>=120 */
3151 /*********************************************************************
3152 * _scalb (MSVCRT.@)
3153 * scalbn (MSVCR120.@)
3154 * scalbln (MSVCR120.@)
3156 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
3158 return MSVCRT_ldexp(num, power);
3161 /*********************************************************************
3162 * _scalbf (MSVCRT.@)
3163 * scalbnf (MSVCR120.@)
3164 * scalblnf (MSVCR120.@)
3166 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
3168 return MSVCRT_ldexp(num, power);
3171 #if _MSVCR_VER>=120
3173 /*********************************************************************
3174 * scalbnl (MSVCR120.@)
3175 * scalblnl (MSVCR120.@)
3177 LDOUBLE CDECL MSVCR120_scalbnl(LDOUBLE num, MSVCRT_long power)
3179 return MSVCRT__scalb(num, power);
3182 /*********************************************************************
3183 * remainder (MSVCR120.@)
3185 double CDECL MSVCR120_remainder(double x, double y)
3187 #ifdef HAVE_REMAINDER
3188 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3189 if(!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
3190 if(isnan(y) || y==0.0) *MSVCRT__errno() = MSVCRT_EDOM;
3191 return remainder(x, y);
3192 #else
3193 FIXME( "not implemented\n" );
3194 return 0.0;
3195 #endif
3198 /*********************************************************************
3199 * remainderf (MSVCR120.@)
3201 float CDECL MSVCR120_remainderf(float x, float y)
3203 #ifdef HAVE_REMAINDERF
3204 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3205 if(!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
3206 if(isnanf(y) || y==0.0f) *MSVCRT__errno() = MSVCRT_EDOM;
3207 return remainderf(x, y);
3208 #else
3209 FIXME( "not implemented\n" );
3210 return 0.0f;
3211 #endif
3214 /*********************************************************************
3215 * remainderl (MSVCR120.@)
3217 LDOUBLE CDECL MSVCR120_remainderl(LDOUBLE x, LDOUBLE y)
3219 return MSVCR120_remainder(x, y);
3222 /*********************************************************************
3223 * lgamma (MSVCR120.@)
3225 double CDECL MSVCR120_lgamma(double x)
3227 #ifdef HAVE_LGAMMA
3228 return lgamma(x);
3229 #else
3230 FIXME( "not implemented\n" );
3231 return 0.0;
3232 #endif
3235 /*********************************************************************
3236 * lgammaf (MSVCR120.@)
3238 float CDECL MSVCR120_lgammaf(float x)
3240 #ifdef HAVE_LGAMMAF
3241 return lgammaf(x);
3242 #else
3243 FIXME( "not implemented\n" );
3244 return 0.0f;
3245 #endif
3248 /*********************************************************************
3249 * lgammal (MSVCR120.@)
3251 LDOUBLE CDECL MSVCR120_lgammal(LDOUBLE x)
3253 return MSVCR120_lgamma(x);
3256 /*********************************************************************
3257 * nan (MSVCR120.@)
3259 double CDECL MSVCR120_nan(const char *tagp)
3261 /* Windows ignores input (MSDN) */
3262 return NAN;
3265 /*********************************************************************
3266 * nanf (MSVCR120.@)
3268 float CDECL MSVCR120_nanf(const char *tagp)
3270 return NAN;
3273 /*********************************************************************
3274 * _except1 (MSVCR120.@)
3275 * TODO:
3276 * - find meaning of ignored cw and operation bits
3277 * - unk parameter
3279 double CDECL _except1(DWORD fpe, _FP_OPERATION_CODE op, double arg, double res, DWORD cw, void *unk)
3281 ULONG_PTR exception_arg;
3282 DWORD exception = 0;
3283 MSVCRT_fenv_t env;
3284 DWORD fpword = 0;
3285 WORD operation;
3287 TRACE("(%x %x %lf %lf %x %p)\n", fpe, op, arg, res, cw, unk);
3289 #ifdef _WIN64
3290 cw = ((cw >> 7) & 0x3f) | ((cw >> 3) & 0xc00);
3291 #endif
3292 operation = op << 5;
3293 exception_arg = (ULONG_PTR)&operation;
3295 MSVCRT_fegetenv(&env);
3297 if (fpe & 0x1) { /* overflow */
3298 if ((fpe == 0x1 && (cw & 0x8)) || (fpe==0x11 && (cw & 0x28))) {
3299 /* 32-bit version also sets SW_INEXACT here */
3300 env.status |= MSVCRT__SW_OVERFLOW;
3301 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
3302 res = signbit(res) ? -INFINITY : INFINITY;
3303 } else {
3304 exception = EXCEPTION_FLT_OVERFLOW;
3306 } else if (fpe & 0x2) { /* underflow */
3307 if ((fpe == 0x2 && (cw & 0x10)) || (fpe==0x12 && (cw & 0x30))) {
3308 env.status |= MSVCRT__SW_UNDERFLOW;
3309 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
3310 res = signbit(res) ? -0.0 : 0.0;
3311 } else {
3312 exception = EXCEPTION_FLT_UNDERFLOW;
3314 } else if (fpe & 0x4) { /* zerodivide */
3315 if ((fpe == 0x4 && (cw & 0x4)) || (fpe==0x14 && (cw & 0x24))) {
3316 env.status |= MSVCRT__SW_ZERODIVIDE;
3317 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
3318 } else {
3319 exception = EXCEPTION_FLT_DIVIDE_BY_ZERO;
3321 } else if (fpe & 0x8) { /* invalid */
3322 if (fpe == 0x8 && (cw & 0x1)) {
3323 env.status |= MSVCRT__SW_INVALID;
3324 } else {
3325 exception = EXCEPTION_FLT_INVALID_OPERATION;
3327 } else if (fpe & 0x10) { /* inexact */
3328 if (fpe == 0x10 && (cw & 0x20)) {
3329 env.status |= MSVCRT__SW_INEXACT;
3330 } else {
3331 exception = EXCEPTION_FLT_INEXACT_RESULT;
3335 if (exception)
3336 env.status = 0;
3337 MSVCRT_fesetenv(&env);
3338 if (exception)
3339 RaiseException(exception, 0, 1, &exception_arg);
3341 if (cw & 0x1) fpword |= MSVCRT__EM_INVALID;
3342 if (cw & 0x2) fpword |= MSVCRT__EM_DENORMAL;
3343 if (cw & 0x4) fpword |= MSVCRT__EM_ZERODIVIDE;
3344 if (cw & 0x8) fpword |= MSVCRT__EM_OVERFLOW;
3345 if (cw & 0x10) fpword |= MSVCRT__EM_UNDERFLOW;
3346 if (cw & 0x20) fpword |= MSVCRT__EM_INEXACT;
3347 switch (cw & 0xc00)
3349 case 0xc00: fpword |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
3350 case 0x800: fpword |= MSVCRT__RC_UP; break;
3351 case 0x400: fpword |= MSVCRT__RC_DOWN; break;
3353 switch (cw & 0x300)
3355 case 0x0: fpword |= MSVCRT__PC_24; break;
3356 case 0x200: fpword |= MSVCRT__PC_53; break;
3357 case 0x300: fpword |= MSVCRT__PC_64; break;
3359 if (cw & 0x1000) fpword |= MSVCRT__IC_AFFINE;
3360 _control87(fpword, 0xffffffff);
3362 return res;
3365 #endif /* _MSVCR_VER>=120 */