msvcrt: Reimplement _finite().
[wine.git] / dlls / msvcrt / math.c
blob94a20e663fe498e96d6b9df5af846d10aba4147a
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
21 * For functions copied from musl (http://www.musl-libc.org/):
22 * ====================================================
23 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
25 * Developed at SunPro, a Sun Microsystems, Inc. business.
26 * Permission to use, copy, modify, and distribute this
27 * software is freely granted, provided that this notice
28 * is preserved.
29 * ====================================================
32 #include "config.h"
33 #include "wine/port.h"
35 #include <stdio.h>
36 #define __USE_ISOC9X 1
37 #define __USE_ISOC99 1
38 #include <math.h>
39 #ifdef HAVE_IEEEFP_H
40 #include <ieeefp.h>
41 #endif
43 #include "msvcrt.h"
45 #include "wine/asm.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
50 #ifndef HAVE_FINITE
51 #define finite(x) isfinite(x)
52 #endif
53 #ifndef HAVE_FINITEF
54 #define finitef(x) isfinite(x)
55 #endif
57 /* FIXME: Does not work with -NAN and -0. */
58 #ifndef signbit
59 #define signbit(x) ((x) < 0)
60 #endif
62 #define _DOMAIN 1 /* domain error in argument */
63 #define _SING 2 /* singularity */
64 #define _OVERFLOW 3 /* range overflow */
65 #define _UNDERFLOW 4 /* range underflow */
67 typedef int (CDECL *MSVCRT_matherr_func)(struct MSVCRT__exception *);
68 typedef double LDOUBLE; /* long double is just a double */
70 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
72 static BOOL sse2_supported;
73 static BOOL sse2_enabled;
75 void msvcrt_init_math(void)
77 sse2_supported = sse2_enabled = IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );
80 /* Copied from musl: src/internal/libm.h */
81 static inline float fp_barrierf(float x)
83 volatile float y = x;
84 return y;
87 /*********************************************************************
88 * _matherr (CRTDLL.@)
90 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
92 return 0;
96 static double math_error(int type, const char *name, double arg1, double arg2, double retval)
98 struct MSVCRT__exception exception = {type, (char *)name, arg1, arg2, retval};
100 TRACE("(%d, %s, %g, %g, %g)\n", type, debugstr_a(name), arg1, arg2, retval);
102 if (MSVCRT_default_matherr_func && MSVCRT_default_matherr_func(&exception))
103 return exception.retval;
105 switch (type)
107 case _DOMAIN:
108 *MSVCRT__errno() = MSVCRT_EDOM;
109 break;
110 case _SING:
111 case _OVERFLOW:
112 *MSVCRT__errno() = MSVCRT_ERANGE;
113 break;
114 case _UNDERFLOW:
115 /* don't set errno */
116 break;
117 default:
118 ERR("Unhandled math error!\n");
121 return exception.retval;
124 /*********************************************************************
125 * __setusermatherr (MSVCRT.@)
127 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
129 MSVCRT_default_matherr_func = func;
130 TRACE("new matherr handler %p\n", func);
133 /*********************************************************************
134 * _set_SSE2_enable (MSVCRT.@)
136 int CDECL MSVCRT__set_SSE2_enable(int flag)
138 sse2_enabled = flag && sse2_supported;
139 return sse2_enabled;
142 #if defined(_WIN64)
143 # if _MSVCR_VER>=140
144 /*********************************************************************
145 * _get_FMA3_enable (UCRTBASE.@)
147 int CDECL MSVCRT__get_FMA3_enable(void)
149 FIXME("() stub\n");
150 return 0;
152 # endif
154 # if _MSVCR_VER>=120
155 /*********************************************************************
156 * _set_FMA3_enable (MSVCR120.@)
158 int CDECL MSVCRT__set_FMA3_enable(int flag)
160 FIXME("(%x) stub\n", flag);
161 return 0;
163 # endif
164 #endif
166 #if !defined(__i386__) || _MSVCR_VER>=120
168 /*********************************************************************
169 * _chgsignf (MSVCRT.@)
171 float CDECL MSVCRT__chgsignf( float num )
173 /* FIXME: +-infinity,Nan not tested */
174 return -num;
177 /*********************************************************************
178 * _copysignf (MSVCRT.@)
180 float CDECL MSVCRT__copysignf( float num, float sign )
182 if (signbit(sign))
183 return signbit(num) ? num : -num;
184 return signbit(num) ? -num : num;
187 /*********************************************************************
188 * _nextafterf (MSVCRT.@)
190 float CDECL MSVCRT__nextafterf( float num, float next )
192 if (!finitef(num) || !finitef(next)) *MSVCRT__errno() = MSVCRT_EDOM;
193 return nextafterf( num, next );
196 /*********************************************************************
197 * _logbf (MSVCRT.@)
199 float CDECL MSVCRT__logbf( float num )
201 float ret = logbf(num);
202 if (isnan(num)) return math_error(_DOMAIN, "_logbf", num, 0, ret);
203 if (!num) return math_error(_SING, "_logbf", num, 0, ret);
204 return ret;
207 #endif
209 #ifndef __i386__
211 /*********************************************************************
212 * _fpclassf (MSVCRT.@)
214 int CDECL MSVCRT__fpclassf( float num )
216 union { float f; UINT32 i; } u = { num };
217 int e = u.i >> 23 & 0xff;
218 int s = u.i >> 31;
220 switch (e)
222 case 0:
223 if (u.i << 1) return s ? MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
224 return s ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
225 case 0xff:
226 if (u.i << 9) return ((u.i >> 22) & 1) ? MSVCRT__FPCLASS_QNAN : MSVCRT__FPCLASS_SNAN;
227 return s ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
228 default:
229 return s ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
233 /*********************************************************************
234 * _finitef (MSVCRT.@)
236 int CDECL MSVCRT__finitef( float num )
238 union { float f; UINT32 i; } u = { num };
239 return (u.i & 0x7fffffff) < 0x7f800000;
242 /*********************************************************************
243 * _isnanf (MSVCRT.@)
245 INT CDECL MSVCRT__isnanf( float num )
247 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
248 * Do the same, as the result may be used in calculations
250 return isnan(num) != 0;
253 /*********************************************************************
254 * MSVCRT_acosf (MSVCRT.@)
256 * Copied from musl: src/math/acosf.c
258 static float acosf_R(float z)
260 static const float pS0 = 1.6666586697e-01,
261 pS1 = -4.2743422091e-02,
262 pS2 = -8.6563630030e-03,
263 qS1 = -7.0662963390e-01;
265 float p, q;
266 p = z * (pS0 + z * (pS1 + z * pS2));
267 q = 1.0f + z * qS1;
268 return p / q;
271 float CDECL MSVCRT_acosf( float x )
273 static const float pio2_hi = 1.5707962513e+00,
274 pio2_lo = 7.5497894159e-08;
276 float z, w, s, c, df;
277 unsigned int hx, ix;
279 hx = *(unsigned int*)&x;
280 ix = hx & 0x7fffffff;
281 /* |x| >= 1 or nan */
282 if (ix >= 0x3f800000) {
283 if (ix == 0x3f800000) {
284 if (hx >> 31)
285 return 2 * pio2_lo + 2 * pio2_hi + 7.5231638453e-37;
286 return 0;
288 if (MSVCRT__isnanf(x)) return x;
289 return math_error(_DOMAIN, "acosf", x, 0, 0 / (x - x));
291 /* |x| < 0.5 */
292 if (ix < 0x3f000000) {
293 if (ix <= 0x32800000) /* |x| < 2**-26 */
294 return pio2_lo + pio2_hi + 7.5231638453e-37;
295 return pio2_hi - (x - (pio2_lo - x * acosf_R(x * x)));
297 /* x < -0.5 */
298 if (hx >> 31) {
299 z = (1 + x) * 0.5f;
300 s = sqrtf(z);
301 w = acosf_R(z) * s - pio2_lo;
302 return 2 * (pio2_hi - (s + w));
304 /* x > 0.5 */
305 z = (1 - x) * 0.5f;
306 s = sqrtf(z);
307 hx = *(unsigned int*)&s & 0xfffff000;
308 df = *(float*)&hx;
309 c = (z - df * df) / (s + df);
310 w = acosf_R(z) * s + c;
311 return 2 * (df + w);
314 /*********************************************************************
315 * MSVCRT_asinf (MSVCRT.@)
317 * Copied from musl: src/math/asinf.c
319 static float asinf_R(float z)
321 /* coefficients for R(x^2) */
322 static const float pS0 = 1.6666586697e-01,
323 pS1 = -4.2743422091e-02,
324 pS2 = -8.6563630030e-03,
325 qS1 = -7.0662963390e-01;
327 float_t p, q;
328 p = z * (pS0 + z * (pS1 + z * pS2));
329 q = 1.0f + z * qS1;
330 return p / q;
333 float CDECL MSVCRT_asinf( float x )
335 static const double pio2 = 1.570796326794896558e+00;
337 double s;
338 float z;
339 unsigned int hx, ix;
341 hx = *(unsigned int*)&x;
342 ix = hx & 0x7fffffff;
343 if (ix >= 0x3f800000) { /* |x| >= 1 */
344 if (ix == 0x3f800000) /* |x| == 1 */
345 return x * pio2 + 7.5231638453e-37; /* asin(+-1) = +-pi/2 with inexact */
346 if (MSVCRT__isnanf(x)) return x;
347 return math_error(_DOMAIN, "asinf", x, 0, 0 / (x - x));
349 if (ix < 0x3f000000) { /* |x| < 0.5 */
350 /* if 0x1p-126 <= |x| < 0x1p-12, avoid raising underflow */
351 if (ix < 0x39800000 && ix >= 0x00800000)
352 return x;
353 return x + x * asinf_R(x * x);
355 /* 1 > |x| >= 0.5 */
356 z = (1 - fabsf(x)) * 0.5f;
357 s = sqrt(z);
358 x = pio2 - 2 * (s + s * asinf_R(z));
359 if (hx >> 31)
360 return -x;
361 return x;
364 /*********************************************************************
365 * MSVCRT_atanf (MSVCRT.@)
367 * Copied from musl: src/math/atanf.c
369 float CDECL MSVCRT_atanf( float x )
371 static const float atanhi[] = {
372 4.6364760399e-01,
373 7.8539812565e-01,
374 9.8279368877e-01,
375 1.5707962513e+00,
377 static const float atanlo[] = {
378 5.0121582440e-09,
379 3.7748947079e-08,
380 3.4473217170e-08,
381 7.5497894159e-08,
383 static const float aT[] = {
384 3.3333328366e-01,
385 -1.9999158382e-01,
386 1.4253635705e-01,
387 -1.0648017377e-01,
388 6.1687607318e-02,
391 float w, s1, s2, z;
392 unsigned int ix, sign;
393 int id;
395 #if _MSVCR_VER == 0
396 if (MSVCRT__isnanf(x)) return math_error(_DOMAIN, "atanf", x, 0, x);
397 #endif
399 ix = *(unsigned int*)&x;
400 sign = ix >> 31;
401 ix &= 0x7fffffff;
402 if (ix >= 0x4c800000) { /* if |x| >= 2**26 */
403 if (MSVCRT__isnanf(x))
404 return x;
405 z = atanhi[3] + 7.5231638453e-37;
406 return sign ? -z : z;
408 if (ix < 0x3ee00000) { /* |x| < 0.4375 */
409 if (ix < 0x39800000) { /* |x| < 2**-12 */
410 if (ix < 0x00800000)
411 /* raise underflow for subnormal x */
412 fp_barrierf(x*x);
413 return x;
415 id = -1;
416 } else {
417 x = fabsf(x);
418 if (ix < 0x3f980000) { /* |x| < 1.1875 */
419 if (ix < 0x3f300000) { /* 7/16 <= |x| < 11/16 */
420 id = 0;
421 x = (2.0f * x - 1.0f) / (2.0f + x);
422 } else { /* 11/16 <= |x| < 19/16 */
423 id = 1;
424 x = (x - 1.0f) / (x + 1.0f);
426 } else {
427 if (ix < 0x401c0000) { /* |x| < 2.4375 */
428 id = 2;
429 x = (x - 1.5f) / (1.0f + 1.5f * x);
430 } else { /* 2.4375 <= |x| < 2**26 */
431 id = 3;
432 x = -1.0f / x;
436 /* end of argument reduction */
437 z = x * x;
438 w = z * z;
439 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
440 s1 = z * (aT[0] + w * (aT[2] + w * aT[4]));
441 s2 = w * (aT[1] + w * aT[3]);
442 if (id < 0)
443 return x - x * (s1 + s2);
444 z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
445 return sign ? -z : z;
448 /*********************************************************************
449 * MSVCRT_atan2f (MSVCRT.@)
451 * Copied from musl: src/math/atan2f.c
453 float CDECL MSVCRT_atan2f( float y, float x )
455 static const float pi = 3.1415927410e+00,
456 pi_lo = -8.7422776573e-08;
458 float z;
459 unsigned int m, ix, iy;
461 if (MSVCRT__isnanf(x) || MSVCRT__isnanf(y))
462 return x + y;
463 ix = *(unsigned int*)&x;
464 iy = *(unsigned int*)&y;
465 if (ix == 0x3f800000) /* x=1.0 */
466 return atanf(y);
467 m = ((iy >> 31) & 1) | ((ix >> 30) & 2); /* 2*sign(x)+sign(y) */
468 ix &= 0x7fffffff;
469 iy &= 0x7fffffff;
471 /* when y = 0 */
472 if (iy == 0) {
473 switch (m) {
474 case 0:
475 case 1: return y; /* atan(+-0,+anything)=+-0 */
476 case 2: return pi; /* atan(+0,-anything) = pi */
477 case 3: return -pi; /* atan(-0,-anything) =-pi */
480 /* when x = 0 */
481 if (ix == 0)
482 return m & 1 ? -pi / 2 : pi / 2;
483 /* when x is INF */
484 if (ix == 0x7f800000) {
485 if (iy == 0x7f800000) {
486 switch (m) {
487 case 0: return pi / 4; /* atan(+INF,+INF) */
488 case 1: return -pi / 4; /* atan(-INF,+INF) */
489 case 2: return 3 * pi / 4; /*atan(+INF,-INF)*/
490 case 3: return -3 * pi / 4; /*atan(-INF,-INF)*/
492 } else {
493 switch (m) {
494 case 0: return 0.0f; /* atan(+...,+INF) */
495 case 1: return -0.0f; /* atan(-...,+INF) */
496 case 2: return pi; /* atan(+...,-INF) */
497 case 3: return -pi; /* atan(-...,-INF) */
501 /* |y/x| > 0x1p26 */
502 if (ix + (26 << 23) < iy || iy == 0x7f800000)
503 return m & 1 ? -pi / 2 : pi / 2;
505 /* z = atan(|y/x|) with correct underflow */
506 if ((m & 2) && iy + (26 << 23) < ix) /*|y/x| < 0x1p-26, x < 0 */
507 z = 0.0;
508 else
509 z = atanf(fabsf(y / x));
510 switch (m) {
511 case 0: return z; /* atan(+,+) */
512 case 1: return -z; /* atan(-,+) */
513 case 2: return pi - (z - pi_lo); /* atan(+,-) */
514 default: /* case 3 */
515 return (z - pi_lo) - pi; /* atan(-,-) */
519 /*********************************************************************
520 * MSVCRT_cosf (MSVCRT.@)
522 float CDECL MSVCRT_cosf( float x )
524 float ret = cosf(x);
525 if (!finitef(x)) return math_error(_DOMAIN, "cosf", x, 0, ret);
526 return ret;
529 /*********************************************************************
530 * MSVCRT_coshf (MSVCRT.@)
532 float CDECL MSVCRT_coshf( float x )
534 float ret = coshf(x);
535 if (isnan(x)) return math_error(_DOMAIN, "coshf", x, 0, ret);
536 return ret;
539 /*********************************************************************
540 * MSVCRT_expf (MSVCRT.@)
542 float CDECL MSVCRT_expf( float x )
544 float ret = expf(x);
545 if (isnan(x)) return math_error(_DOMAIN, "expf", x, 0, ret);
546 if (finitef(x) && !ret) return math_error(_UNDERFLOW, "expf", x, 0, ret);
547 if (finitef(x) && !finitef(ret)) return math_error(_OVERFLOW, "expf", x, 0, ret);
548 return ret;
551 /*********************************************************************
552 * MSVCRT_fmodf (MSVCRT.@)
554 float CDECL MSVCRT_fmodf( float x, float y )
556 float ret = fmodf(x, y);
557 if (!finitef(x) || !finitef(y)) return math_error(_DOMAIN, "fmodf", x, 0, ret);
558 return ret;
561 /*********************************************************************
562 * MSVCRT_logf (MSVCRT.@)
564 float CDECL MSVCRT_logf( float x )
566 float ret = logf(x);
567 if (x < 0.0) return math_error(_DOMAIN, "logf", x, 0, ret);
568 if (x == 0.0) return math_error(_SING, "logf", x, 0, ret);
569 return ret;
572 /*********************************************************************
573 * MSVCRT_log10f (MSVCRT.@)
575 float CDECL MSVCRT_log10f( float x )
577 float ret = log10f(x);
578 if (x < 0.0) return math_error(_DOMAIN, "log10f", x, 0, ret);
579 if (x == 0.0) return math_error(_SING, "log10f", x, 0, ret);
580 return ret;
583 /*********************************************************************
584 * MSVCRT_powf (MSVCRT.@)
586 float CDECL MSVCRT_powf( float x, float y )
588 float z = powf(x,y);
589 if (x < 0 && y != floorf(y)) return math_error(_DOMAIN, "powf", x, y, z);
590 if (!x && finitef(y) && y < 0) return math_error(_SING, "powf", x, y, z);
591 if (finitef(x) && finitef(y) && !finitef(z)) return math_error(_OVERFLOW, "powf", x, y, z);
592 if (x && finitef(x) && finitef(y) && !z) return math_error(_UNDERFLOW, "powf", x, y, z);
593 return z;
596 /*********************************************************************
597 * MSVCRT_sinf (MSVCRT.@)
599 float CDECL MSVCRT_sinf( float x )
601 float ret = sinf(x);
602 if (!finitef(x)) return math_error(_DOMAIN, "sinf", x, 0, ret);
603 return ret;
606 /*********************************************************************
607 * MSVCRT_sinhf (MSVCRT.@)
609 float CDECL MSVCRT_sinhf( float x )
611 float ret = sinhf(x);
612 if (isnan(x)) return math_error(_DOMAIN, "sinhf", x, 0, ret);
613 return ret;
616 /*********************************************************************
617 * MSVCRT_sqrtf (MSVCRT.@)
619 * Copied from musl: src/math/sqrtf.c
621 float CDECL MSVCRT_sqrtf( float x )
623 static const float tiny = 1.0e-30;
625 float z;
626 int sign = 0x80000000;
627 int ix,s,q,m,t,i;
628 unsigned int r;
630 ix = *(int*)&x;
632 /* take care of Inf and NaN */
633 if ((ix & 0x7f800000) == 0x7f800000 && (ix == 0x7f800000 || ix & 0x7fffff))
634 return x;
636 /* take care of zero */
637 if (ix <= 0) {
638 if ((ix & ~sign) == 0)
639 return x; /* sqrt(+-0) = +-0 */
640 return math_error(_DOMAIN, "sqrtf", x, 0, (x - x) / (x - x)); /* sqrt(-ve) = sNaN */
642 /* normalize x */
643 m = ix >> 23;
644 if (m == 0) { /* subnormal x */
645 for (i = 0; (ix & 0x00800000) == 0; i++)
646 ix <<= 1;
647 m -= i - 1;
649 m -= 127; /* unbias exponent */
650 ix = (ix & 0x007fffff) | 0x00800000;
651 if (m & 1) /* odd m, double x to make it even */
652 ix += ix;
653 m >>= 1; /* m = [m/2] */
655 /* generate sqrt(x) bit by bit */
656 ix += ix;
657 q = s = 0; /* q = sqrt(x) */
658 r = 0x01000000; /* r = moving bit from right to left */
660 while (r != 0) {
661 t = s + r;
662 if (t <= ix) {
663 s = t + r;
664 ix -= t;
665 q += r;
667 ix += ix;
668 r >>= 1;
671 /* use floating add to find out rounding direction */
672 if (ix != 0) {
673 z = 1.0f - tiny; /* raise inexact flag */
674 if (z >= 1.0f) {
675 z = 1.0f + tiny;
676 if (z > 1.0f)
677 q += 2;
678 else
679 q += q & 1;
682 ix = (q >> 1) + 0x3f000000;
683 r = ix + ((unsigned int)m << 23);
684 z = *(float*)&r;
685 return z;
688 /*********************************************************************
689 * MSVCRT_tanf (MSVCRT.@)
691 float CDECL MSVCRT_tanf( float x )
693 float ret = tanf(x);
694 if (!finitef(x)) return math_error(_DOMAIN, "tanf", x, 0, ret);
695 return ret;
698 /*********************************************************************
699 * MSVCRT_tanhf (MSVCRT.@)
701 float CDECL MSVCRT_tanhf( float x )
703 float ret = tanhf(x);
704 if (!finitef(x)) return math_error(_DOMAIN, "tanhf", x, 0, ret);
705 return ret;
708 /*********************************************************************
709 * ceilf (MSVCRT.@)
711 float CDECL MSVCRT_ceilf( float x )
713 return ceilf(x);
716 /*********************************************************************
717 * fabsf (MSVCRT.@)
719 float CDECL MSVCRT_fabsf( float x )
721 return fabsf(x);
724 /*********************************************************************
725 * floorf (MSVCRT.@)
727 float CDECL MSVCRT_floorf( float x )
729 return floorf(x);
732 /*********************************************************************
733 * frexpf (MSVCRT.@)
735 float CDECL MSVCRT_frexpf( float x, int *exp )
737 return frexpf( x, exp );
740 /*********************************************************************
741 * modff (MSVCRT.@)
743 float CDECL MSVCRT_modff( float x, float *iptr )
745 return modff( x, iptr );
748 #endif
750 /*********************************************************************
751 * MSVCRT_acos (MSVCRT.@)
753 * Copied from musl: src/math/acos.c
755 static double acos_R(double z)
757 static const double pS0 = 1.66666666666666657415e-01,
758 pS1 = -3.25565818622400915405e-01,
759 pS2 = 2.01212532134862925881e-01,
760 pS3 = -4.00555345006794114027e-02,
761 pS4 = 7.91534994289814532176e-04,
762 pS5 = 3.47933107596021167570e-05,
763 qS1 = -2.40339491173441421878e+00,
764 qS2 = 2.02094576023350569471e+00,
765 qS3 = -6.88283971605453293030e-01,
766 qS4 = 7.70381505559019352791e-02;
768 double p, q;
769 p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
770 q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
771 return p/q;
774 double CDECL MSVCRT_acos( double x )
776 static const double pio2_hi = 1.57079632679489655800e+00,
777 pio2_lo = 6.12323399573676603587e-17;
779 double z, w, s, c, df;
780 unsigned int hx, ix;
781 ULONGLONG llx;
783 hx = *(ULONGLONG*)&x >> 32;
784 ix = hx & 0x7fffffff;
785 /* |x| >= 1 or nan */
786 if (ix >= 0x3ff00000) {
787 unsigned int lx;
789 lx = *(ULONGLONG*)&x;
790 if (((ix - 0x3ff00000) | lx) == 0) {
791 /* acos(1)=0, acos(-1)=pi */
792 if (hx >> 31)
793 return 2 * pio2_hi + 7.5231638452626401e-37;
794 return 0;
796 if (isnan(x)) return x;
797 return math_error(_DOMAIN, "acos", x, 0, 0 / (x - x));
799 /* |x| < 0.5 */
800 if (ix < 0x3fe00000) {
801 if (ix <= 0x3c600000) /* |x| < 2**-57 */
802 return pio2_hi + 7.5231638452626401e-37;
803 return pio2_hi - (x - (pio2_lo - x * acos_R(x * x)));
805 /* x < -0.5 */
806 if (hx >> 31) {
807 z = (1.0 + x) * 0.5;
808 s = sqrt(z);
809 w = acos_R(z) * s - pio2_lo;
810 return 2 * (pio2_hi - (s + w));
812 /* x > 0.5 */
813 z = (1.0 - x) * 0.5;
814 s = sqrt(z);
815 df = s;
816 llx = (*(ULONGLONG*)&df >> 32) << 32;
817 df = *(double*)&llx;
818 c = (z - df * df) / (s + df);
819 w = acos_R(z) * s + c;
820 return 2 * (df + w);
823 /*********************************************************************
824 * MSVCRT_asin (MSVCRT.@)
826 * Copied from musl: src/math/asin.c
828 static double asin_R(double z)
830 /* coefficients for R(x^2) */
831 static const double pS0 = 1.66666666666666657415e-01,
832 pS1 = -3.25565818622400915405e-01,
833 pS2 = 2.01212532134862925881e-01,
834 pS3 = -4.00555345006794114027e-02,
835 pS4 = 7.91534994289814532176e-04,
836 pS5 = 3.47933107596021167570e-05,
837 qS1 = -2.40339491173441421878e+00,
838 qS2 = 2.02094576023350569471e+00,
839 qS3 = -6.88283971605453293030e-01,
840 qS4 = 7.70381505559019352791e-02;
842 double p, q;
843 p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
844 q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
845 return p / q;
848 double CDECL MSVCRT_asin( double x )
850 static const double pio2_hi = 1.57079632679489655800e+00,
851 pio2_lo = 6.12323399573676603587e-17;
853 double z, r, s;
854 unsigned int hx, ix;
855 ULONGLONG llx;
857 hx = *(ULONGLONG*)&x >> 32;
858 ix = hx & 0x7fffffff;
859 /* |x| >= 1 or nan */
860 if (ix >= 0x3ff00000) {
861 unsigned int lx;
862 lx = *(ULONGLONG*)&x;
863 if (((ix - 0x3ff00000) | lx) == 0)
864 /* asin(1) = +-pi/2 with inexact */
865 return x * pio2_hi + 7.5231638452626401e-37;
866 if (isnan(x)) return x;
867 return math_error(_DOMAIN, "asin", x, 0, 0 / (x - x));
869 /* |x| < 0.5 */
870 if (ix < 0x3fe00000) {
871 /* if 0x1p-1022 <= |x| < 0x1p-26, avoid raising underflow */
872 if (ix < 0x3e500000 && ix >= 0x00100000)
873 return x;
874 return x + x * asin_R(x * x);
876 /* 1 > |x| >= 0.5 */
877 z = (1 - fabs(x)) * 0.5;
878 s = sqrt(z);
879 r = asin_R(z);
880 if (ix >= 0x3fef3333) { /* if |x| > 0.975 */
881 x = pio2_hi - (2 * (s + s * r) - pio2_lo);
882 } else {
883 double f, c;
884 /* f+c = sqrt(z) */
885 f = s;
886 llx = (*(ULONGLONG*)&f >> 32) << 32;
887 f = *(double*)&llx;
888 c = (z - f * f) / (s + f);
889 x = 0.5 * pio2_hi - (2 * s * r - (pio2_lo - 2 * c) - (0.5 * pio2_hi - 2 * f));
891 if (hx >> 31)
892 return -x;
893 return x;
896 /*********************************************************************
897 * MSVCRT_atan (MSVCRT.@)
899 * Copied from musl: src/math/atan.c
901 double CDECL MSVCRT_atan( double x )
903 static const double atanhi[] = {
904 4.63647609000806093515e-01,
905 7.85398163397448278999e-01,
906 9.82793723247329054082e-01,
907 1.57079632679489655800e+00,
909 static const double atanlo[] = {
910 2.26987774529616870924e-17,
911 3.06161699786838301793e-17,
912 1.39033110312309984516e-17,
913 6.12323399573676603587e-17,
915 static const double aT[] = {
916 3.33333333333329318027e-01,
917 -1.99999999998764832476e-01,
918 1.42857142725034663711e-01,
919 -1.11111104054623557880e-01,
920 9.09088713343650656196e-02,
921 -7.69187620504482999495e-02,
922 6.66107313738753120669e-02,
923 -5.83357013379057348645e-02,
924 4.97687799461593236017e-02,
925 -3.65315727442169155270e-02,
926 1.62858201153657823623e-02,
929 double w, s1, s2, z;
930 unsigned int ix, sign;
931 int id;
933 #if _MSVCR_VER == 0
934 if (isnan(x)) return math_error(_DOMAIN, "atan", x, 0, x);
935 #endif
937 ix = *(ULONGLONG*)&x >> 32;
938 sign = ix >> 31;
939 ix &= 0x7fffffff;
940 if (ix >= 0x44100000) { /* if |x| >= 2^66 */
941 if (isnan(x))
942 return x;
943 z = atanhi[3] + 7.5231638452626401e-37;
944 return sign ? -z : z;
946 if (ix < 0x3fdc0000) { /* |x| < 0.4375 */
947 if (ix < 0x3e400000) { /* |x| < 2^-27 */
948 if (ix < 0x00100000)
949 /* raise underflow for subnormal x */
950 fp_barrierf((float)x);
951 return x;
953 id = -1;
954 } else {
955 x = fabs(x);
956 if (ix < 0x3ff30000) { /* |x| < 1.1875 */
957 if (ix < 0x3fe60000) { /* 7/16 <= |x| < 11/16 */
958 id = 0;
959 x = (2.0 * x - 1.0) / (2.0 + x);
960 } else { /* 11/16 <= |x| < 19/16 */
961 id = 1;
962 x = (x - 1.0) / (x + 1.0);
964 } else {
965 if (ix < 0x40038000) { /* |x| < 2.4375 */
966 id = 2;
967 x = (x - 1.5) / (1.0 + 1.5 * x);
968 } else { /* 2.4375 <= |x| < 2^66 */
969 id = 3;
970 x = -1.0 / x;
974 /* end of argument reduction */
975 z = x * x;
976 w = z * z;
977 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
978 s1 = z * (aT[0] + w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
979 s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
980 if (id < 0)
981 return x - x * (s1 + s2);
982 z = atanhi[id] - (x * (s1 + s2) - atanlo[id] - x);
983 return sign ? -z : z;
986 /*********************************************************************
987 * MSVCRT_atan2 (MSVCRT.@)
989 * Copied from musl: src/math/atan2.c
991 double CDECL MSVCRT_atan2( double y, double x )
993 static const double pi = 3.1415926535897931160E+00,
994 pi_lo = 1.2246467991473531772E-16;
996 double z;
997 unsigned int m, lx, ly, ix, iy;
999 if (isnan(x) || isnan(y))
1000 return x+y;
1001 ix = *(ULONGLONG*)&x >> 32;
1002 lx = *(ULONGLONG*)&x;
1003 iy = *(ULONGLONG*)&y >> 32;
1004 ly = *(ULONGLONG*)&y;
1005 if (((ix - 0x3ff00000) | lx) == 0) /* x = 1.0 */
1006 return atan(y);
1007 m = ((iy >> 31) & 1) | ((ix >> 30) & 2); /* 2*sign(x)+sign(y) */
1008 ix = ix & 0x7fffffff;
1009 iy = iy & 0x7fffffff;
1011 /* when y = 0 */
1012 if ((iy | ly) == 0) {
1013 switch(m) {
1014 case 0:
1015 case 1: return y; /* atan(+-0,+anything)=+-0 */
1016 case 2: return pi; /* atan(+0,-anything) = pi */
1017 case 3: return -pi; /* atan(-0,-anything) =-pi */
1020 /* when x = 0 */
1021 if ((ix | lx) == 0)
1022 return m & 1 ? -pi / 2 : pi / 2;
1023 /* when x is INF */
1024 if (ix == 0x7ff00000) {
1025 if (iy == 0x7ff00000) {
1026 switch(m) {
1027 case 0: return pi / 4; /* atan(+INF,+INF) */
1028 case 1: return -pi / 4; /* atan(-INF,+INF) */
1029 case 2: return 3 * pi / 4; /* atan(+INF,-INF) */
1030 case 3: return -3 * pi / 4; /* atan(-INF,-INF) */
1032 } else {
1033 switch(m) {
1034 case 0: return 0.0; /* atan(+...,+INF) */
1035 case 1: return -0.0; /* atan(-...,+INF) */
1036 case 2: return pi; /* atan(+...,-INF) */
1037 case 3: return -pi; /* atan(-...,-INF) */
1041 /* |y/x| > 0x1p64 */
1042 if (ix + (64 << 20) < iy || iy == 0x7ff00000)
1043 return m & 1 ? -pi / 2 : pi / 2;
1045 /* z = atan(|y/x|) without spurious underflow */
1046 if ((m & 2) && iy + (64 << 20) < ix) /* |y/x| < 0x1p-64, x<0 */
1047 z = 0;
1048 else
1049 z = atan(fabs(y / x));
1050 switch (m) {
1051 case 0: return z; /* atan(+,+) */
1052 case 1: return -z; /* atan(-,+) */
1053 case 2: return pi - (z - pi_lo); /* atan(+,-) */
1054 default: /* case 3 */
1055 return (z - pi_lo) - pi; /* atan(-,-) */
1059 /*********************************************************************
1060 * MSVCRT_cos (MSVCRT.@)
1062 double CDECL MSVCRT_cos( double x )
1064 double ret = cos(x);
1065 if (!isfinite(x)) return math_error(_DOMAIN, "cos", x, 0, ret);
1066 return ret;
1069 /*********************************************************************
1070 * MSVCRT_cosh (MSVCRT.@)
1072 double CDECL MSVCRT_cosh( double x )
1074 double ret = cosh(x);
1075 if (isnan(x)) return math_error(_DOMAIN, "cosh", x, 0, ret);
1076 return ret;
1079 /*********************************************************************
1080 * MSVCRT_exp (MSVCRT.@)
1082 double CDECL MSVCRT_exp( double x )
1084 double ret = exp(x);
1085 if (isnan(x)) return math_error(_DOMAIN, "exp", x, 0, ret);
1086 if (isfinite(x) && !ret) return math_error(_UNDERFLOW, "exp", x, 0, ret);
1087 if (isfinite(x) && !isfinite(ret)) return math_error(_OVERFLOW, "exp", x, 0, ret);
1088 return ret;
1091 /*********************************************************************
1092 * MSVCRT_fmod (MSVCRT.@)
1094 double CDECL MSVCRT_fmod( double x, double y )
1096 double ret = fmod(x, y);
1097 if (!isfinite(x) || !isfinite(y)) return math_error(_DOMAIN, "fmod", x, y, ret);
1098 return ret;
1101 /*********************************************************************
1102 * MSVCRT_log (MSVCRT.@)
1104 double CDECL MSVCRT_log( double x )
1106 double ret = log(x);
1107 if (x < 0.0) return math_error(_DOMAIN, "log", x, 0, ret);
1108 if (x == 0.0) return math_error(_SING, "log", x, 0, ret);
1109 return ret;
1112 /*********************************************************************
1113 * MSVCRT_log10 (MSVCRT.@)
1115 double CDECL MSVCRT_log10( double x )
1117 double ret = log10(x);
1118 if (x < 0.0) return math_error(_DOMAIN, "log10", x, 0, ret);
1119 if (x == 0.0) return math_error(_SING, "log10", x, 0, ret);
1120 return ret;
1123 /*********************************************************************
1124 * MSVCRT_pow (MSVCRT.@)
1126 double CDECL MSVCRT_pow( double x, double y )
1128 double z = pow(x,y);
1129 if (x < 0 && y != floor(y))
1130 return math_error(_DOMAIN, "pow", x, y, z);
1131 if (!x && isfinite(y) && y < 0)
1132 return math_error(_SING, "pow", x, y, z);
1133 if (isfinite(x) && isfinite(y) && !isfinite(z))
1134 return math_error(_OVERFLOW, "pow", x, y, z);
1135 if (x && isfinite(x) && isfinite(y) && !z)
1136 return math_error(_UNDERFLOW, "pow", x, y, z);
1137 return z;
1140 /*********************************************************************
1141 * MSVCRT_sin (MSVCRT.@)
1143 double CDECL MSVCRT_sin( double x )
1145 double ret = sin(x);
1146 if (!isfinite(x)) return math_error(_DOMAIN, "sin", x, 0, ret);
1147 return ret;
1150 /*********************************************************************
1151 * MSVCRT_sinh (MSVCRT.@)
1153 double CDECL MSVCRT_sinh( double x )
1155 double ret = sinh(x);
1156 if (isnan(x)) return math_error(_DOMAIN, "sinh", x, 0, ret);
1157 return ret;
1160 /*********************************************************************
1161 * MSVCRT_sqrt (MSVCRT.@)
1163 * Copied from musl: src/math/sqrt.c
1165 double CDECL MSVCRT_sqrt( double x )
1167 static const double tiny = 1.0e-300;
1169 double z;
1170 int sign = 0x80000000;
1171 int ix0,s0,q,m,t,i;
1172 unsigned int r,t1,s1,ix1,q1;
1173 ULONGLONG ix;
1175 ix = *(ULONGLONG*)&x;
1176 ix0 = ix >> 32;
1177 ix1 = ix;
1179 /* take care of Inf and NaN */
1180 if (isnan(x) || (isinf(x) && x > 0))
1181 return x;
1183 /* take care of zero */
1184 if (ix0 <= 0) {
1185 if (((ix0 & ~sign) | ix1) == 0)
1186 return x; /* sqrt(+-0) = +-0 */
1187 if (ix0 < 0)
1188 return math_error(_DOMAIN, "sqrt", x, 0, (x - x) / (x - x));
1190 /* normalize x */
1191 m = ix0 >> 20;
1192 if (m == 0) { /* subnormal x */
1193 while (ix0 == 0) {
1194 m -= 21;
1195 ix0 |= (ix1 >> 11);
1196 ix1 <<= 21;
1198 for (i=0; (ix0 & 0x00100000) == 0; i++)
1199 ix0 <<= 1;
1200 m -= i - 1;
1201 ix0 |= ix1 >> (32 - i);
1202 ix1 <<= i;
1204 m -= 1023; /* unbias exponent */
1205 ix0 = (ix0 & 0x000fffff) | 0x00100000;
1206 if (m & 1) { /* odd m, double x to make it even */
1207 ix0 += ix0 + ((ix1 & sign) >> 31);
1208 ix1 += ix1;
1210 m >>= 1; /* m = [m/2] */
1212 /* generate sqrt(x) bit by bit */
1213 ix0 += ix0 + ((ix1 & sign) >> 31);
1214 ix1 += ix1;
1215 q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
1216 r = 0x00200000; /* r = moving bit from right to left */
1218 while (r != 0) {
1219 t = s0 + r;
1220 if (t <= ix0) {
1221 s0 = t + r;
1222 ix0 -= t;
1223 q += r;
1225 ix0 += ix0 + ((ix1 & sign) >> 31);
1226 ix1 += ix1;
1227 r >>= 1;
1230 r = sign;
1231 while (r != 0) {
1232 t1 = s1 + r;
1233 t = s0;
1234 if (t < ix0 || (t == ix0 && t1 <= ix1)) {
1235 s1 = t1 + r;
1236 if ((t1&sign) == sign && (s1 & sign) == 0)
1237 s0++;
1238 ix0 -= t;
1239 if (ix1 < t1)
1240 ix0--;
1241 ix1 -= t1;
1242 q1 += r;
1244 ix0 += ix0 + ((ix1 & sign) >> 31);
1245 ix1 += ix1;
1246 r >>= 1;
1249 /* use floating add to find out rounding direction */
1250 if ((ix0 | ix1) != 0) {
1251 z = 1.0 - tiny; /* raise inexact flag */
1252 if (z >= 1.0) {
1253 z = 1.0 + tiny;
1254 if (q1 == (unsigned int)0xffffffff) {
1255 q1 = 0;
1256 q++;
1257 } else if (z > 1.0) {
1258 if (q1 == (unsigned int)0xfffffffe)
1259 q++;
1260 q1 += 2;
1261 } else
1262 q1 += q1 & 1;
1265 ix0 = (q >> 1) + 0x3fe00000;
1266 ix1 = q1 >> 1;
1267 if (q & 1)
1268 ix1 |= sign;
1269 ix = ix0 + ((unsigned int)m << 20);
1270 ix <<= 32;
1271 ix |= ix1;
1272 return *(double*)&ix;
1275 /*********************************************************************
1276 * MSVCRT_tan (MSVCRT.@)
1278 double CDECL MSVCRT_tan( double x )
1280 double ret = tan(x);
1281 if (!isfinite(x)) return math_error(_DOMAIN, "tan", x, 0, ret);
1282 return ret;
1285 /*********************************************************************
1286 * MSVCRT_tanh (MSVCRT.@)
1288 double CDECL MSVCRT_tanh( double x )
1290 double ret = tanh(x);
1291 if (isnan(x)) return math_error(_DOMAIN, "tanh", x, 0, ret);
1292 return ret;
1296 #if defined(__GNUC__) && defined(__i386__)
1298 #define CREATE_FPU_FUNC1(name, call) \
1299 __ASM_GLOBAL_FUNC(name, \
1300 "pushl %ebp\n\t" \
1301 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1302 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1303 "movl %esp, %ebp\n\t" \
1304 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1305 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1306 "fstpl (%esp)\n\t" /* store function argument */ \
1307 "fwait\n\t" \
1308 "movl $1, %ecx\n\t" /* empty FPU stack */ \
1309 "1:\n\t" \
1310 "fxam\n\t" \
1311 "fstsw %ax\n\t" \
1312 "and $0x4500, %ax\n\t" \
1313 "cmp $0x4100, %ax\n\t" \
1314 "je 2f\n\t" \
1315 "fstpl (%esp,%ecx,8)\n\t" \
1316 "fwait\n\t" \
1317 "incl %ecx\n\t" \
1318 "jmp 1b\n\t" \
1319 "2:\n\t" \
1320 "movl %ecx, -4(%ebp)\n\t" \
1321 "call " __ASM_NAME( #call ) "\n\t" \
1322 "movl -4(%ebp), %ecx\n\t" \
1323 "fstpl (%esp)\n\t" /* save result */ \
1324 "3:\n\t" /* restore FPU stack */ \
1325 "decl %ecx\n\t" \
1326 "fldl (%esp,%ecx,8)\n\t" \
1327 "cmpl $0, %ecx\n\t" \
1328 "jne 3b\n\t" \
1329 "leave\n\t" \
1330 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1331 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1332 "ret")
1334 #define CREATE_FPU_FUNC2(name, call) \
1335 __ASM_GLOBAL_FUNC(name, \
1336 "pushl %ebp\n\t" \
1337 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1338 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1339 "movl %esp, %ebp\n\t" \
1340 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1341 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1342 "fstpl 8(%esp)\n\t" /* store function argument */ \
1343 "fwait\n\t" \
1344 "fstpl (%esp)\n\t" \
1345 "fwait\n\t" \
1346 "movl $2, %ecx\n\t" /* empty FPU stack */ \
1347 "1:\n\t" \
1348 "fxam\n\t" \
1349 "fstsw %ax\n\t" \
1350 "and $0x4500, %ax\n\t" \
1351 "cmp $0x4100, %ax\n\t" \
1352 "je 2f\n\t" \
1353 "fstpl (%esp,%ecx,8)\n\t" \
1354 "fwait\n\t" \
1355 "incl %ecx\n\t" \
1356 "jmp 1b\n\t" \
1357 "2:\n\t" \
1358 "movl %ecx, -4(%ebp)\n\t" \
1359 "call " __ASM_NAME( #call ) "\n\t" \
1360 "movl -4(%ebp), %ecx\n\t" \
1361 "fstpl 8(%esp)\n\t" /* save result */ \
1362 "3:\n\t" /* restore FPU stack */ \
1363 "decl %ecx\n\t" \
1364 "fldl (%esp,%ecx,8)\n\t" \
1365 "cmpl $1, %ecx\n\t" \
1366 "jne 3b\n\t" \
1367 "leave\n\t" \
1368 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1369 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1370 "ret")
1372 CREATE_FPU_FUNC1(_CIacos, MSVCRT_acos)
1373 CREATE_FPU_FUNC1(_CIasin, MSVCRT_asin)
1374 CREATE_FPU_FUNC1(_CIatan, MSVCRT_atan)
1375 CREATE_FPU_FUNC2(_CIatan2, MSVCRT_atan2)
1376 CREATE_FPU_FUNC1(_CIcos, MSVCRT_cos)
1377 CREATE_FPU_FUNC1(_CIcosh, MSVCRT_cosh)
1378 CREATE_FPU_FUNC1(_CIexp, MSVCRT_exp)
1379 CREATE_FPU_FUNC2(_CIfmod, MSVCRT_fmod)
1380 CREATE_FPU_FUNC1(_CIlog, MSVCRT_log)
1381 CREATE_FPU_FUNC1(_CIlog10, MSVCRT_log10)
1382 CREATE_FPU_FUNC2(_CIpow, MSVCRT_pow)
1383 CREATE_FPU_FUNC1(_CIsin, MSVCRT_sin)
1384 CREATE_FPU_FUNC1(_CIsinh, MSVCRT_sinh)
1385 CREATE_FPU_FUNC1(_CIsqrt, MSVCRT_sqrt)
1386 CREATE_FPU_FUNC1(_CItan, MSVCRT_tan)
1387 CREATE_FPU_FUNC1(_CItanh, MSVCRT_tanh)
1389 __ASM_GLOBAL_FUNC(MSVCRT__ftol,
1390 "pushl %ebp\n\t"
1391 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1392 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1393 "movl %esp, %ebp\n\t"
1394 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1395 "subl $12, %esp\n\t" /* sizeof(LONGLONG) + 2*sizeof(WORD) */
1396 "fnstcw (%esp)\n\t"
1397 "mov (%esp), %ax\n\t"
1398 "or $0xc00, %ax\n\t"
1399 "mov %ax, 2(%esp)\n\t"
1400 "fldcw 2(%esp)\n\t"
1401 "fistpq 4(%esp)\n\t"
1402 "fldcw (%esp)\n\t"
1403 "movl 4(%esp), %eax\n\t"
1404 "movl 8(%esp), %edx\n\t"
1405 "leave\n\t"
1406 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1407 __ASM_CFI(".cfi_same_value %ebp\n\t")
1408 "ret")
1410 #endif /* defined(__GNUC__) && defined(__i386__) */
1412 /*********************************************************************
1413 * _fpclass (MSVCRT.@)
1415 int CDECL MSVCRT__fpclass(double num)
1417 union { double f; UINT64 i; } u = { num };
1418 int e = u.i >> 52 & 0x7ff;
1419 int s = u.i >> 63;
1421 switch (e)
1423 case 0:
1424 if (u.i << 1) return s ? MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
1425 return s ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
1426 case 0x7ff:
1427 if (u.i << 12) return ((u.i >> 51) & 1) ? MSVCRT__FPCLASS_QNAN : MSVCRT__FPCLASS_SNAN;
1428 return s ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
1429 default:
1430 return s ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
1434 /*********************************************************************
1435 * _rotl (MSVCRT.@)
1437 unsigned int CDECL _rotl(unsigned int num, int shift)
1439 shift &= 31;
1440 return (num << shift) | (num >> (32-shift));
1443 /*********************************************************************
1444 * _lrotl (MSVCRT.@)
1446 MSVCRT_ulong CDECL MSVCRT__lrotl(MSVCRT_ulong num, int shift)
1448 shift &= 0x1f;
1449 return (num << shift) | (num >> (32-shift));
1452 /*********************************************************************
1453 * _lrotr (MSVCRT.@)
1455 MSVCRT_ulong CDECL MSVCRT__lrotr(MSVCRT_ulong num, int shift)
1457 shift &= 0x1f;
1458 return (num >> shift) | (num << (32-shift));
1461 /*********************************************************************
1462 * _rotr (MSVCRT.@)
1464 unsigned int CDECL _rotr(unsigned int num, int shift)
1466 shift &= 0x1f;
1467 return (num >> shift) | (num << (32-shift));
1470 /*********************************************************************
1471 * _rotl64 (MSVCRT.@)
1473 unsigned __int64 CDECL _rotl64(unsigned __int64 num, int shift)
1475 shift &= 63;
1476 return (num << shift) | (num >> (64-shift));
1479 /*********************************************************************
1480 * _rotr64 (MSVCRT.@)
1482 unsigned __int64 CDECL _rotr64(unsigned __int64 num, int shift)
1484 shift &= 63;
1485 return (num >> shift) | (num << (64-shift));
1488 /*********************************************************************
1489 * abs (MSVCRT.@)
1491 int CDECL MSVCRT_abs( int n )
1493 return n >= 0 ? n : -n;
1496 /*********************************************************************
1497 * labs (MSVCRT.@)
1499 MSVCRT_long CDECL MSVCRT_labs( MSVCRT_long n )
1501 return n >= 0 ? n : -n;
1504 #if _MSVCR_VER>=100
1505 /*********************************************************************
1506 * llabs (MSVCR100.@)
1508 MSVCRT_longlong CDECL MSVCRT_llabs( MSVCRT_longlong n )
1510 return n >= 0 ? n : -n;
1512 #endif
1514 #if _MSVCR_VER>=120
1515 /*********************************************************************
1516 * imaxabs (MSVCR120.@)
1518 MSVCRT_intmax_t CDECL MSVCRT_imaxabs( MSVCRT_intmax_t n )
1520 return n >= 0 ? n : -n;
1522 #endif
1524 /*********************************************************************
1525 * _abs64 (MSVCRT.@)
1527 __int64 CDECL _abs64( __int64 n )
1529 return n >= 0 ? n : -n;
1532 /*********************************************************************
1533 * _logb (MSVCRT.@)
1535 double CDECL MSVCRT__logb(double num)
1537 double ret = logb(num);
1538 if (isnan(num)) return math_error(_DOMAIN, "_logb", num, 0, ret);
1539 if (!num) return math_error(_SING, "_logb", num, 0, ret);
1540 return ret;
1543 /*********************************************************************
1544 * _hypot (MSVCRT.@)
1546 double CDECL _hypot(double x, double y)
1548 /* FIXME: errno handling */
1549 return hypot( x, y );
1552 /*********************************************************************
1553 * _hypotf (MSVCRT.@)
1555 float CDECL MSVCRT__hypotf(float x, float y)
1557 /* FIXME: errno handling */
1558 return hypotf( x, y );
1561 /*********************************************************************
1562 * ceil (MSVCRT.@)
1564 double CDECL MSVCRT_ceil( double x )
1566 return ceil(x);
1569 /*********************************************************************
1570 * floor (MSVCRT.@)
1572 double CDECL MSVCRT_floor( double x )
1574 return floor(x);
1577 /*********************************************************************
1578 * fma (MSVCRT.@)
1580 double CDECL MSVCRT_fma( double x, double y, double z )
1582 #ifdef HAVE_FMA
1583 double w = fma(x, y, z);
1584 #else
1585 double w = x * y + z;
1586 #endif
1587 if ((isinf(x) && y == 0) || (x == 0 && isinf(y))) *MSVCRT__errno() = MSVCRT_EDOM;
1588 else if (isinf(x) && isinf(z) && x != z) *MSVCRT__errno() = MSVCRT_EDOM;
1589 else if (isinf(y) && isinf(z) && y != z) *MSVCRT__errno() = MSVCRT_EDOM;
1590 return w;
1593 /*********************************************************************
1594 * fmaf (MSVCRT.@)
1596 float CDECL MSVCRT_fmaf( float x, float y, float z )
1598 #ifdef HAVE_FMAF
1599 float w = fmaf(x, y, z);
1600 #else
1601 float w = x * y + z;
1602 #endif
1603 if ((isinf(x) && y == 0) || (x == 0 && isinf(y))) *MSVCRT__errno() = MSVCRT_EDOM;
1604 else if (isinf(x) && isinf(z) && x != z) *MSVCRT__errno() = MSVCRT_EDOM;
1605 else if (isinf(y) && isinf(z) && y != z) *MSVCRT__errno() = MSVCRT_EDOM;
1606 return w;
1609 /*********************************************************************
1610 * fabs (MSVCRT.@)
1612 double CDECL MSVCRT_fabs( double x )
1614 return fabs(x);
1617 /*********************************************************************
1618 * frexp (MSVCRT.@)
1620 double CDECL MSVCRT_frexp( double x, int *exp )
1622 return frexp( x, exp );
1625 /*********************************************************************
1626 * modf (MSVCRT.@)
1628 double CDECL MSVCRT_modf( double x, double *iptr )
1630 return modf( x, iptr );
1633 /**********************************************************************
1634 * _statusfp2 (MSVCRT.@)
1636 * Not exported by native msvcrt, added in msvcr80.
1638 #if defined(__i386__) || defined(__x86_64__)
1639 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
1641 #ifdef __GNUC__
1642 unsigned int flags;
1643 unsigned long fpword;
1645 if (x86_sw)
1647 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
1648 flags = 0;
1649 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
1650 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
1651 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
1652 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
1653 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
1654 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
1655 *x86_sw = flags;
1658 if (!sse2_sw) return;
1660 if (sse2_supported)
1662 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1663 flags = 0;
1664 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
1665 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
1666 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
1667 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
1668 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
1669 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
1670 *sse2_sw = flags;
1672 else *sse2_sw = 0;
1673 #else
1674 FIXME( "not implemented\n" );
1675 #endif
1677 #endif
1679 /**********************************************************************
1680 * _statusfp (MSVCRT.@)
1682 unsigned int CDECL _statusfp(void)
1684 unsigned int flags = 0;
1685 #if defined(__i386__) || defined(__x86_64__)
1686 unsigned int x86_sw, sse2_sw;
1688 _statusfp2( &x86_sw, &sse2_sw );
1689 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
1690 flags = x86_sw | sse2_sw;
1691 #elif defined(__aarch64__)
1692 unsigned long fpsr;
1694 __asm__ __volatile__( "mrs %0, fpsr" : "=r" (fpsr) );
1695 if (fpsr & 0x1) flags |= MSVCRT__SW_INVALID;
1696 if (fpsr & 0x2) flags |= MSVCRT__SW_ZERODIVIDE;
1697 if (fpsr & 0x4) flags |= MSVCRT__SW_OVERFLOW;
1698 if (fpsr & 0x8) flags |= MSVCRT__SW_UNDERFLOW;
1699 if (fpsr & 0x10) flags |= MSVCRT__SW_INEXACT;
1700 if (fpsr & 0x80) flags |= MSVCRT__SW_DENORMAL;
1701 #else
1702 FIXME( "not implemented\n" );
1703 #endif
1704 return flags;
1707 /*********************************************************************
1708 * _clearfp (MSVCRT.@)
1710 unsigned int CDECL _clearfp(void)
1712 unsigned int flags = 0;
1713 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1714 unsigned long fpword;
1716 __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
1717 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
1718 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
1719 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
1720 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
1721 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
1722 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
1724 if (sse2_supported)
1726 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1727 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
1728 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
1729 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
1730 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
1731 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
1732 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
1733 fpword &= ~0x3f;
1734 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1736 #elif defined(__aarch64__)
1737 unsigned long fpsr;
1739 __asm__ __volatile__( "mrs %0, fpsr" : "=r" (fpsr) );
1740 if (fpsr & 0x1) flags |= MSVCRT__SW_INVALID;
1741 if (fpsr & 0x2) flags |= MSVCRT__SW_ZERODIVIDE;
1742 if (fpsr & 0x4) flags |= MSVCRT__SW_OVERFLOW;
1743 if (fpsr & 0x8) flags |= MSVCRT__SW_UNDERFLOW;
1744 if (fpsr & 0x10) flags |= MSVCRT__SW_INEXACT;
1745 if (fpsr & 0x80) flags |= MSVCRT__SW_DENORMAL;
1746 fpsr &= ~0x9f;
1747 __asm__ __volatile__( "msr fpsr, %0" :: "r" (fpsr) );
1748 #else
1749 FIXME( "not implemented\n" );
1750 #endif
1751 return flags;
1754 /*********************************************************************
1755 * __fpecode (MSVCRT.@)
1757 int * CDECL __fpecode(void)
1759 return &msvcrt_get_thread_data()->fpecode;
1762 /*********************************************************************
1763 * ldexp (MSVCRT.@)
1765 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
1767 double z = ldexp(num,exp);
1769 if (isfinite(num) && !isfinite(z))
1770 return math_error(_OVERFLOW, "ldexp", num, exp, z);
1771 if (num && isfinite(num) && !z)
1772 return math_error(_UNDERFLOW, "ldexp", num, exp, z);
1773 if (z == 0 && signbit(z))
1774 z = 0.0; /* Convert -0 -> +0 */
1775 return z;
1778 /*********************************************************************
1779 * _cabs (MSVCRT.@)
1781 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
1783 return sqrt(num.x * num.x + num.y * num.y);
1786 /*********************************************************************
1787 * _chgsign (MSVCRT.@)
1789 double CDECL MSVCRT__chgsign(double num)
1791 /* FIXME: +-infinity,Nan not tested */
1792 return -num;
1795 /*********************************************************************
1796 * __control87_2 (MSVCR80.@)
1798 * Not exported by native msvcrt, added in msvcr80.
1800 #ifdef __i386__
1801 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1802 unsigned int *x86_cw, unsigned int *sse2_cw )
1804 #ifdef __GNUC__
1805 unsigned long fpword;
1806 unsigned int flags;
1807 unsigned int old_flags;
1809 if (x86_cw)
1811 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1813 /* Convert into mask constants */
1814 flags = 0;
1815 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
1816 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
1817 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
1818 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
1819 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1820 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1821 switch (fpword & 0xc00)
1823 case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1824 case 0x800: flags |= MSVCRT__RC_UP; break;
1825 case 0x400: flags |= MSVCRT__RC_DOWN; break;
1827 switch (fpword & 0x300)
1829 case 0x0: flags |= MSVCRT__PC_24; break;
1830 case 0x200: flags |= MSVCRT__PC_53; break;
1831 case 0x300: flags |= MSVCRT__PC_64; break;
1833 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1835 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1836 if (mask)
1838 flags = (flags & ~mask) | (newval & mask);
1840 /* Convert (masked) value back to fp word */
1841 fpword = 0;
1842 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
1843 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
1844 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1845 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
1846 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
1847 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
1848 switch (flags & MSVCRT__MCW_RC)
1850 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1851 case MSVCRT__RC_UP: fpword |= 0x800; break;
1852 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
1854 switch (flags & MSVCRT__MCW_PC)
1856 case MSVCRT__PC_64: fpword |= 0x300; break;
1857 case MSVCRT__PC_53: fpword |= 0x200; break;
1858 case MSVCRT__PC_24: fpword |= 0x0; break;
1860 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1862 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1864 *x86_cw = flags;
1867 if (!sse2_cw) return 1;
1869 if (sse2_supported)
1871 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1873 /* Convert into mask constants */
1874 flags = 0;
1875 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1876 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1877 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1878 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1879 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1880 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1881 switch (fpword & 0x6000)
1883 case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1884 case 0x4000: flags |= MSVCRT__RC_UP; break;
1885 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1887 switch (fpword & 0x8040)
1889 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1890 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1891 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1894 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1895 if (mask)
1897 old_flags = flags;
1898 mask &= MSVCRT__MCW_EM | MSVCRT__MCW_RC | MSVCRT__MCW_DN;
1899 flags = (flags & ~mask) | (newval & mask);
1901 if (flags != old_flags)
1903 /* Convert (masked) value back to fp word */
1904 fpword = 0;
1905 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1906 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1907 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1908 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1909 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1910 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1911 switch (flags & MSVCRT__MCW_RC)
1913 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1914 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1915 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1917 switch (flags & MSVCRT__MCW_DN)
1919 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1920 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1921 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
1923 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1926 *sse2_cw = flags;
1928 else *sse2_cw = 0;
1930 return 1;
1931 #else
1932 FIXME( "not implemented\n" );
1933 return 0;
1934 #endif
1936 #endif
1938 /*********************************************************************
1939 * _control87 (MSVCRT.@)
1941 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1943 unsigned int flags = 0;
1944 #ifdef __i386__
1945 unsigned int sse2_cw;
1947 __control87_2( newval, mask, &flags, &sse2_cw );
1949 if ((flags ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) flags |= MSVCRT__EM_AMBIGUOUS;
1950 flags |= sse2_cw;
1951 #elif defined(__x86_64__)
1952 unsigned long fpword;
1953 unsigned int old_flags;
1955 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1956 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1957 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1958 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1959 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1960 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1961 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1962 switch (fpword & 0x6000)
1964 case 0x6000: flags |= MSVCRT__RC_CHOP; break;
1965 case 0x4000: flags |= MSVCRT__RC_UP; break;
1966 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1968 switch (fpword & 0x8040)
1970 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1971 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1972 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1974 old_flags = flags;
1975 mask &= MSVCRT__MCW_EM | MSVCRT__MCW_RC | MSVCRT__MCW_DN;
1976 flags = (flags & ~mask) | (newval & mask);
1977 if (flags != old_flags)
1979 fpword = 0;
1980 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1981 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1982 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1983 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1984 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1985 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1986 switch (flags & MSVCRT__MCW_RC)
1988 case MSVCRT__RC_CHOP: fpword |= 0x6000; break;
1989 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1990 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1992 switch (flags & MSVCRT__MCW_DN)
1994 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1995 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1996 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
1998 __asm__ __volatile__( "ldmxcsr %0" :: "m" (fpword) );
2000 #elif defined(__aarch64__)
2001 unsigned long fpcr;
2003 __asm__ __volatile__( "mrs %0, fpcr" : "=r" (fpcr) );
2004 if (!(fpcr & 0x100)) flags |= MSVCRT__EM_INVALID;
2005 if (!(fpcr & 0x200)) flags |= MSVCRT__EM_ZERODIVIDE;
2006 if (!(fpcr & 0x400)) flags |= MSVCRT__EM_OVERFLOW;
2007 if (!(fpcr & 0x800)) flags |= MSVCRT__EM_UNDERFLOW;
2008 if (!(fpcr & 0x1000)) flags |= MSVCRT__EM_INEXACT;
2009 if (!(fpcr & 0x8000)) flags |= MSVCRT__EM_DENORMAL;
2010 switch (fpcr & 0xc00000)
2012 case 0x400000: flags |= MSVCRT__RC_UP; break;
2013 case 0x800000: flags |= MSVCRT__RC_DOWN; break;
2014 case 0xc00000: flags |= MSVCRT__RC_CHOP; break;
2016 flags = (flags & ~mask) | (newval & mask);
2017 fpcr &= ~0xc09f00ul;
2018 if (!(flags & MSVCRT__EM_INVALID)) fpcr |= 0x100;
2019 if (!(flags & MSVCRT__EM_ZERODIVIDE)) fpcr |= 0x200;
2020 if (!(flags & MSVCRT__EM_OVERFLOW)) fpcr |= 0x400;
2021 if (!(flags & MSVCRT__EM_UNDERFLOW)) fpcr |= 0x800;
2022 if (!(flags & MSVCRT__EM_INEXACT)) fpcr |= 0x1000;
2023 if (!(flags & MSVCRT__EM_DENORMAL)) fpcr |= 0x8000;
2024 switch (flags & MSVCRT__MCW_RC)
2026 case MSVCRT__RC_CHOP: fpcr |= 0xc00000; break;
2027 case MSVCRT__RC_UP: fpcr |= 0x400000; break;
2028 case MSVCRT__RC_DOWN: fpcr |= 0x800000; break;
2030 __asm__ __volatile__( "msr fpcr, %0" :: "r" (fpcr) );
2031 #else
2032 FIXME( "not implemented\n" );
2033 #endif
2034 return flags;
2037 /*********************************************************************
2038 * _controlfp (MSVCRT.@)
2040 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
2042 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
2045 /*********************************************************************
2046 * _set_controlfp (MSVCRT.@)
2048 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
2050 _controlfp( newval, mask );
2053 /*********************************************************************
2054 * _controlfp_s (MSVCRT.@)
2056 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
2058 static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
2059 MSVCRT__MCW_PC | MSVCRT__MCW_DN);
2060 unsigned int val;
2062 if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
2064 if (cur) *cur = _controlfp( 0, 0 ); /* retrieve it anyway */
2065 return MSVCRT_EINVAL;
2067 val = _controlfp( newval, mask );
2068 if (cur) *cur = val;
2069 return 0;
2072 #if _MSVCR_VER>=120
2073 /*********************************************************************
2074 * fegetenv (MSVCR120.@)
2076 int CDECL MSVCRT_fegetenv(MSVCRT_fenv_t *env)
2078 env->control = _controlfp(0, 0) & (MSVCRT__EM_INEXACT | MSVCRT__EM_UNDERFLOW |
2079 MSVCRT__EM_OVERFLOW | MSVCRT__EM_ZERODIVIDE | MSVCRT__EM_INVALID);
2080 env->status = _statusfp();
2081 return 0;
2083 #endif
2085 #if _MSVCR_VER>=140
2086 /*********************************************************************
2087 * __fpe_flt_rounds (UCRTBASE.@)
2089 int CDECL __fpe_flt_rounds(void)
2091 unsigned int fpc = _controlfp(0, 0) & MSVCRT__RC_CHOP;
2093 TRACE("()\n");
2095 switch(fpc) {
2096 case MSVCRT__RC_CHOP: return 0;
2097 case MSVCRT__RC_NEAR: return 1;
2098 case MSVCRT__RC_UP: return 2;
2099 default: return 3;
2102 #endif
2104 #if _MSVCR_VER>=120
2106 /*********************************************************************
2107 * fegetround (MSVCR120.@)
2109 int CDECL MSVCRT_fegetround(void)
2111 return _controlfp(0, 0) & MSVCRT__RC_CHOP;
2114 /*********************************************************************
2115 * fesetround (MSVCR120.@)
2117 int CDECL MSVCRT_fesetround(int round_mode)
2119 if (round_mode & (~MSVCRT__RC_CHOP))
2120 return 1;
2121 _controlfp(round_mode, MSVCRT__RC_CHOP);
2122 return 0;
2125 #endif /* _MSVCR_VER>=120 */
2127 /*********************************************************************
2128 * _copysign (MSVCRT.@)
2130 double CDECL MSVCRT__copysign(double num, double sign)
2132 if (signbit(sign))
2133 return signbit(num) ? num : -num;
2134 return signbit(num) ? -num : num;
2137 /*********************************************************************
2138 * _finite (MSVCRT.@)
2140 int CDECL MSVCRT__finite(double num)
2142 union { double f; UINT64 i; } u = { num };
2143 return (u.i & ~0ull >> 1) < 0x7ffull << 52;
2146 /*********************************************************************
2147 * _fpreset (MSVCRT.@)
2149 void CDECL _fpreset(void)
2151 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2152 const unsigned int x86_cw = 0x27f;
2153 __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
2154 if (sse2_supported)
2156 const unsigned long sse2_cw = 0x1f80;
2157 __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
2159 #else
2160 FIXME( "not implemented\n" );
2161 #endif
2164 #if _MSVCR_VER>=120
2165 /*********************************************************************
2166 * fesetenv (MSVCR120.@)
2168 int CDECL MSVCRT_fesetenv(const MSVCRT_fenv_t *env)
2170 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2171 struct {
2172 WORD control_word;
2173 WORD unused1;
2174 WORD status_word;
2175 WORD unused2;
2176 WORD tag_word;
2177 WORD unused3;
2178 DWORD instruction_pointer;
2179 WORD code_segment;
2180 WORD unused4;
2181 DWORD operand_addr;
2182 WORD data_segment;
2183 WORD unused5;
2184 } fenv;
2186 TRACE( "(%p)\n", env );
2188 if (!env->control && !env->status) {
2189 _fpreset();
2190 return 0;
2193 __asm__ __volatile__( "fnstenv %0" : "=m" (fenv) );
2195 fenv.control_word &= ~0x3d;
2196 if (env->control & MSVCRT__EM_INVALID) fenv.control_word |= 0x1;
2197 if (env->control & MSVCRT__EM_ZERODIVIDE) fenv.control_word |= 0x4;
2198 if (env->control & MSVCRT__EM_OVERFLOW) fenv.control_word |= 0x8;
2199 if (env->control & MSVCRT__EM_UNDERFLOW) fenv.control_word |= 0x10;
2200 if (env->control & MSVCRT__EM_INEXACT) fenv.control_word |= 0x20;
2202 fenv.status_word &= ~0x3d;
2203 if (env->status & MSVCRT__SW_INVALID) fenv.status_word |= 0x1;
2204 if (env->status & MSVCRT__SW_ZERODIVIDE) fenv.status_word |= 0x4;
2205 if (env->status & MSVCRT__SW_OVERFLOW) fenv.status_word |= 0x8;
2206 if (env->status & MSVCRT__SW_UNDERFLOW) fenv.status_word |= 0x10;
2207 if (env->status & MSVCRT__SW_INEXACT) fenv.status_word |= 0x20;
2209 __asm__ __volatile__( "fldenv %0" : : "m" (fenv) : "st", "st(1)",
2210 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
2212 if (sse2_supported)
2214 DWORD fpword;
2216 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
2217 fpword &= ~0x1e80;
2218 if (env->control & MSVCRT__EM_INVALID) fpword |= 0x80;
2219 if (env->control & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
2220 if (env->control & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
2221 if (env->control & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
2222 if (env->control & MSVCRT__EM_INEXACT) fpword |= 0x1000;
2223 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
2226 return 0;
2227 #else
2228 FIXME( "not implemented\n" );
2229 #endif
2230 return 1;
2232 #endif
2234 /*********************************************************************
2235 * _isnan (MSVCRT.@)
2237 INT CDECL MSVCRT__isnan(double num)
2239 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
2240 * Do the same, as the result may be used in calculations
2242 return isnan(num) != 0;
2245 /*********************************************************************
2246 * _j0 (MSVCRT.@)
2248 double CDECL MSVCRT__j0(double num)
2250 /* FIXME: errno handling */
2251 #ifdef HAVE_J0
2252 return j0(num);
2253 #else
2254 FIXME("not implemented\n");
2255 return 0;
2256 #endif
2259 /*********************************************************************
2260 * _j1 (MSVCRT.@)
2262 double CDECL MSVCRT__j1(double num)
2264 /* FIXME: errno handling */
2265 #ifdef HAVE_J1
2266 return j1(num);
2267 #else
2268 FIXME("not implemented\n");
2269 return 0;
2270 #endif
2273 /*********************************************************************
2274 * _jn (MSVCRT.@)
2276 double CDECL MSVCRT__jn(int n, double num)
2278 /* FIXME: errno handling */
2279 #ifdef HAVE_JN
2280 return jn(n, num);
2281 #else
2282 FIXME("not implemented\n");
2283 return 0;
2284 #endif
2287 /*********************************************************************
2288 * _y0 (MSVCRT.@)
2290 double CDECL MSVCRT__y0(double num)
2292 double retval;
2293 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
2294 #ifdef HAVE_Y0
2295 retval = y0(num);
2296 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
2298 *MSVCRT__errno() = MSVCRT_EDOM;
2299 retval = NAN;
2301 #else
2302 FIXME("not implemented\n");
2303 retval = 0;
2304 #endif
2305 return retval;
2308 /*********************************************************************
2309 * _y1 (MSVCRT.@)
2311 double CDECL MSVCRT__y1(double num)
2313 double retval;
2314 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
2315 #ifdef HAVE_Y1
2316 retval = y1(num);
2317 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
2319 *MSVCRT__errno() = MSVCRT_EDOM;
2320 retval = NAN;
2322 #else
2323 FIXME("not implemented\n");
2324 retval = 0;
2325 #endif
2326 return retval;
2329 /*********************************************************************
2330 * _yn (MSVCRT.@)
2332 double CDECL MSVCRT__yn(int order, double num)
2334 double retval;
2335 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
2336 #ifdef HAVE_YN
2337 retval = yn(order,num);
2338 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
2340 *MSVCRT__errno() = MSVCRT_EDOM;
2341 retval = NAN;
2343 #else
2344 FIXME("not implemented\n");
2345 retval = 0;
2346 #endif
2347 return retval;
2350 #if _MSVCR_VER>=120
2352 /*********************************************************************
2353 * _nearbyint (MSVCR120.@)
2355 double CDECL MSVCRT_nearbyint(double num)
2357 #ifdef HAVE_NEARBYINT
2358 return nearbyint(num);
2359 #else
2360 return num >= 0 ? floor(num + 0.5) : ceil(num - 0.5);
2361 #endif
2364 /*********************************************************************
2365 * _nearbyintf (MSVCR120.@)
2367 float CDECL MSVCRT_nearbyintf(float num)
2369 #ifdef HAVE_NEARBYINTF
2370 return nearbyintf(num);
2371 #else
2372 return MSVCRT_nearbyint(num);
2373 #endif
2376 /*********************************************************************
2377 * nexttoward (MSVCR120.@)
2379 double CDECL MSVCRT_nexttoward(double num, double next)
2381 #ifdef HAVE_NEXTTOWARD
2382 double ret = nexttoward(num, next);
2383 if (!(MSVCRT__fpclass(ret) & (MSVCRT__FPCLASS_PN | MSVCRT__FPCLASS_NN
2384 | MSVCRT__FPCLASS_SNAN | MSVCRT__FPCLASS_QNAN)) && !isinf(num))
2386 *MSVCRT__errno() = MSVCRT_ERANGE;
2388 return ret;
2389 #else
2390 FIXME("not implemented\n");
2391 return 0;
2392 #endif
2395 /*********************************************************************
2396 * nexttowardf (MSVCR120.@)
2398 float CDECL MSVCRT_nexttowardf(float num, double next)
2400 #ifdef HAVE_NEXTTOWARDF
2401 float ret = nexttowardf(num, next);
2402 if (!(MSVCRT__fpclass(ret) & (MSVCRT__FPCLASS_PN | MSVCRT__FPCLASS_NN
2403 | MSVCRT__FPCLASS_SNAN | MSVCRT__FPCLASS_QNAN)) && !isinf(num))
2405 *MSVCRT__errno() = MSVCRT_ERANGE;
2407 return ret;
2408 #else
2409 FIXME("not implemented\n");
2410 return 0;
2411 #endif
2414 #endif /* _MSVCR_VER>=120 */
2416 /*********************************************************************
2417 * _nextafter (MSVCRT.@)
2419 double CDECL MSVCRT__nextafter(double num, double next)
2421 double retval;
2422 if (!isfinite(num) || !isfinite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
2423 retval = nextafter(num,next);
2424 return retval;
2427 /*********************************************************************
2428 * _ecvt (MSVCRT.@)
2430 char * CDECL MSVCRT__ecvt( double number, int ndigits, int *decpt, int *sign )
2432 int prec, len;
2433 thread_data_t *data = msvcrt_get_thread_data();
2434 /* FIXME: check better for overflow (native supports over 300 chars) */
2435 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
2436 * 4 for exponent and one for
2437 * terminating '\0' */
2438 if (!data->efcvt_buffer)
2439 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
2441 if( number < 0) {
2442 *sign = TRUE;
2443 number = -number;
2444 } else
2445 *sign = FALSE;
2446 /* handle cases with zero ndigits or less */
2447 prec = ndigits;
2448 if( prec < 1) prec = 2;
2449 len = MSVCRT__snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
2450 /* take the decimal "point away */
2451 if( prec != 1)
2452 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
2453 /* take the exponential "e" out */
2454 data->efcvt_buffer[ prec] = '\0';
2455 /* read the exponent */
2456 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
2457 (*decpt)++;
2458 /* adjust for some border cases */
2459 if( data->efcvt_buffer[0] == '0')/* value is zero */
2460 *decpt = 0;
2461 /* handle cases with zero ndigits or less */
2462 if( ndigits < 1){
2463 if( data->efcvt_buffer[ 0] >= '5')
2464 (*decpt)++;
2465 data->efcvt_buffer[ 0] = '\0';
2467 TRACE("out=\"%s\"\n",data->efcvt_buffer);
2468 return data->efcvt_buffer;
2471 /*********************************************************************
2472 * _ecvt_s (MSVCRT.@)
2474 int CDECL MSVCRT__ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
2476 int prec, len;
2477 char *result;
2478 const char infret[] = "1#INF";
2480 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
2481 if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
2482 if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
2483 if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
2484 if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
2486 /* special case - inf */
2487 if(number == HUGE_VAL || number == -HUGE_VAL)
2489 memset(buffer, '0', ndigits);
2490 memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
2491 buffer[ndigits] = '\0';
2492 (*decpt) = 1;
2493 if(number == -HUGE_VAL)
2494 (*sign) = 1;
2495 else
2496 (*sign) = 0;
2497 return 0;
2499 /* handle cases with zero ndigits or less */
2500 prec = ndigits;
2501 if( prec < 1) prec = 2;
2502 result = MSVCRT_malloc(prec + 7);
2504 if( number < 0) {
2505 *sign = TRUE;
2506 number = -number;
2507 } else
2508 *sign = FALSE;
2509 len = MSVCRT__snprintf(result, prec + 7, "%.*le", prec - 1, number);
2510 /* take the decimal "point away */
2511 if( prec != 1)
2512 memmove( result + 1, result + 2, len - 1 );
2513 /* take the exponential "e" out */
2514 result[ prec] = '\0';
2515 /* read the exponent */
2516 sscanf( result + prec + 1, "%d", decpt);
2517 (*decpt)++;
2518 /* adjust for some border cases */
2519 if( result[0] == '0')/* value is zero */
2520 *decpt = 0;
2521 /* handle cases with zero ndigits or less */
2522 if( ndigits < 1){
2523 if( result[ 0] >= '5')
2524 (*decpt)++;
2525 result[ 0] = '\0';
2527 memcpy( buffer, result, max(ndigits + 1, 1) );
2528 MSVCRT_free( result );
2529 return 0;
2532 /***********************************************************************
2533 * _fcvt (MSVCRT.@)
2535 char * CDECL MSVCRT__fcvt( double number, int ndigits, int *decpt, int *sign )
2537 thread_data_t *data = msvcrt_get_thread_data();
2538 int stop, dec1, dec2;
2539 char *ptr1, *ptr2, *first;
2540 char buf[80]; /* ought to be enough */
2541 char decimal_separator = get_locinfo()->lconv->decimal_point[0];
2543 if (!data->efcvt_buffer)
2544 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
2546 if (number < 0)
2548 *sign = 1;
2549 number = -number;
2550 } else *sign = 0;
2552 stop = MSVCRT__snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
2553 ptr1 = buf;
2554 ptr2 = data->efcvt_buffer;
2555 first = NULL;
2556 dec1 = 0;
2557 dec2 = 0;
2559 /* For numbers below the requested resolution, work out where
2560 the decimal point will be rather than finding it in the string */
2561 if (number < 1.0 && number > 0.0) {
2562 dec2 = log10(number + 1e-10);
2563 if (-dec2 <= ndigits) dec2 = 0;
2566 /* If requested digits is zero or less, we will need to truncate
2567 * the returned string */
2568 if (ndigits < 1) {
2569 stop += ndigits;
2572 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
2573 while (*ptr1 != '\0' && *ptr1 != decimal_separator) {
2574 if (!first) first = ptr2;
2575 if ((ptr1 - buf) < stop) {
2576 *ptr2++ = *ptr1++;
2577 } else {
2578 ptr1++;
2580 dec1++;
2583 if (ndigits > 0) {
2584 ptr1++;
2585 if (!first) {
2586 while (*ptr1 == '0') { /* Process leading zeroes */
2587 *ptr2++ = *ptr1++;
2588 dec1--;
2591 while (*ptr1 != '\0') {
2592 if (!first) first = ptr2;
2593 *ptr2++ = *ptr1++;
2597 *ptr2 = '\0';
2599 /* We never found a non-zero digit, then our number is either
2600 * smaller than the requested precision, or 0.0 */
2601 if (!first) {
2602 if (number > 0.0) {
2603 first = ptr2;
2604 } else {
2605 first = data->efcvt_buffer;
2606 dec1 = 0;
2610 *decpt = dec2 ? dec2 : dec1;
2611 return first;
2614 /***********************************************************************
2615 * _fcvt_s (MSVCRT.@)
2617 int CDECL MSVCRT__fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
2619 int stop, dec1, dec2;
2620 char *ptr1, *ptr2, *first;
2621 char buf[80]; /* ought to be enough */
2622 char decimal_separator = get_locinfo()->lconv->decimal_point[0];
2624 if (!outbuffer || !decpt || !sign || size == 0)
2626 *MSVCRT__errno() = MSVCRT_EINVAL;
2627 return MSVCRT_EINVAL;
2630 if (number < 0)
2632 *sign = 1;
2633 number = -number;
2634 } else *sign = 0;
2636 stop = MSVCRT__snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
2637 ptr1 = buf;
2638 ptr2 = outbuffer;
2639 first = NULL;
2640 dec1 = 0;
2641 dec2 = 0;
2643 /* For numbers below the requested resolution, work out where
2644 the decimal point will be rather than finding it in the string */
2645 if (number < 1.0 && number > 0.0) {
2646 dec2 = log10(number + 1e-10);
2647 if (-dec2 <= ndigits) dec2 = 0;
2650 /* If requested digits is zero or less, we will need to truncate
2651 * the returned string */
2652 if (ndigits < 1) {
2653 stop += ndigits;
2656 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
2657 while (*ptr1 != '\0' && *ptr1 != decimal_separator) {
2658 if (!first) first = ptr2;
2659 if ((ptr1 - buf) < stop) {
2660 if (size > 1) {
2661 *ptr2++ = *ptr1++;
2662 size--;
2664 } else {
2665 ptr1++;
2667 dec1++;
2670 if (ndigits > 0) {
2671 ptr1++;
2672 if (!first) {
2673 while (*ptr1 == '0') { /* Process leading zeroes */
2674 if (number == 0.0 && size > 1) {
2675 *ptr2++ = '0';
2676 size--;
2678 ptr1++;
2679 dec1--;
2682 while (*ptr1 != '\0') {
2683 if (!first) first = ptr2;
2684 if (size > 1) {
2685 *ptr2++ = *ptr1++;
2686 size--;
2691 *ptr2 = '\0';
2693 /* We never found a non-zero digit, then our number is either
2694 * smaller than the requested precision, or 0.0 */
2695 if (!first && (number <= 0.0))
2696 dec1 = 0;
2698 *decpt = dec2 ? dec2 : dec1;
2699 return 0;
2702 /***********************************************************************
2703 * _gcvt (MSVCRT.@)
2705 char * CDECL MSVCRT__gcvt( double number, int ndigit, char *buff )
2707 if(!buff) {
2708 *MSVCRT__errno() = MSVCRT_EINVAL;
2709 return NULL;
2712 if(ndigit < 0) {
2713 *MSVCRT__errno() = MSVCRT_ERANGE;
2714 return NULL;
2717 MSVCRT_sprintf(buff, "%.*g", ndigit, number);
2718 return buff;
2721 /***********************************************************************
2722 * _gcvt_s (MSVCRT.@)
2724 int CDECL MSVCRT__gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
2726 int len;
2728 if(!buff) {
2729 *MSVCRT__errno() = MSVCRT_EINVAL;
2730 return MSVCRT_EINVAL;
2733 if( digits<0 || digits>=size) {
2734 if(size)
2735 buff[0] = '\0';
2737 *MSVCRT__errno() = MSVCRT_ERANGE;
2738 return MSVCRT_ERANGE;
2741 len = MSVCRT__scprintf("%.*g", digits, number);
2742 if(len > size) {
2743 buff[0] = '\0';
2744 *MSVCRT__errno() = MSVCRT_ERANGE;
2745 return MSVCRT_ERANGE;
2748 MSVCRT_sprintf(buff, "%.*g", digits, number);
2749 return 0;
2752 #include <stdlib.h> /* div_t, ldiv_t */
2754 /*********************************************************************
2755 * div (MSVCRT.@)
2756 * VERSION
2757 * [i386] Windows binary compatible - returns the struct in eax/edx.
2759 #ifdef __i386__
2760 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
2762 union {
2763 MSVCRT_div_t div;
2764 unsigned __int64 uint64;
2765 } ret;
2767 ret.div.quot = num / denom;
2768 ret.div.rem = num % denom;
2769 return ret.uint64;
2771 #else
2772 /*********************************************************************
2773 * div (MSVCRT.@)
2774 * VERSION
2775 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
2777 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
2779 MSVCRT_div_t ret;
2781 ret.quot = num / denom;
2782 ret.rem = num % denom;
2783 return ret;
2785 #endif /* ifdef __i386__ */
2788 /*********************************************************************
2789 * ldiv (MSVCRT.@)
2790 * VERSION
2791 * [i386] Windows binary compatible - returns the struct in eax/edx.
2793 #ifdef __i386__
2794 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
2796 union {
2797 MSVCRT_ldiv_t ldiv;
2798 unsigned __int64 uint64;
2799 } ret;
2801 ret.ldiv.quot = num / denom;
2802 ret.ldiv.rem = num % denom;
2803 return ret.uint64;
2805 #else
2806 /*********************************************************************
2807 * ldiv (MSVCRT.@)
2808 * VERSION
2809 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
2811 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
2813 MSVCRT_ldiv_t ret;
2815 ret.quot = num / denom;
2816 ret.rem = num % denom;
2817 return ret;
2819 #endif /* ifdef __i386__ */
2821 #if _MSVCR_VER>=100
2822 /*********************************************************************
2823 * lldiv (MSVCR100.@)
2825 MSVCRT_lldiv_t CDECL MSVCRT_lldiv(MSVCRT_longlong num, MSVCRT_longlong denom)
2827 MSVCRT_lldiv_t ret;
2829 ret.quot = num / denom;
2830 ret.rem = num % denom;
2832 return ret;
2834 #endif
2836 #ifdef __i386__
2838 /*********************************************************************
2839 * _adjust_fdiv (MSVCRT.@)
2840 * Used by the MSVC compiler to work around the Pentium FDIV bug.
2842 int MSVCRT__adjust_fdiv = 0;
2844 /***********************************************************************
2845 * _adj_fdiv_m16i (MSVCRT.@)
2847 * NOTE
2848 * I _think_ this function is intended to work around the Pentium
2849 * fdiv bug.
2851 void __stdcall _adj_fdiv_m16i( short arg )
2853 TRACE("(): stub\n");
2856 /***********************************************************************
2857 * _adj_fdiv_m32 (MSVCRT.@)
2859 * NOTE
2860 * I _think_ this function is intended to work around the Pentium
2861 * fdiv bug.
2863 void __stdcall _adj_fdiv_m32( unsigned int arg )
2865 TRACE("(): stub\n");
2868 /***********************************************************************
2869 * _adj_fdiv_m32i (MSVCRT.@)
2871 * NOTE
2872 * I _think_ this function is intended to work around the Pentium
2873 * fdiv bug.
2875 void __stdcall _adj_fdiv_m32i( int arg )
2877 TRACE("(): stub\n");
2880 /***********************************************************************
2881 * _adj_fdiv_m64 (MSVCRT.@)
2883 * NOTE
2884 * I _think_ this function is intended to work around the Pentium
2885 * fdiv bug.
2887 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
2889 TRACE("(): stub\n");
2892 /***********************************************************************
2893 * _adj_fdiv_r (MSVCRT.@)
2894 * FIXME
2895 * This function is likely to have the wrong number of arguments.
2897 * NOTE
2898 * I _think_ this function is intended to work around the Pentium
2899 * fdiv bug.
2901 void _adj_fdiv_r(void)
2903 TRACE("(): stub\n");
2906 /***********************************************************************
2907 * _adj_fdivr_m16i (MSVCRT.@)
2909 * NOTE
2910 * I _think_ this function is intended to work around the Pentium
2911 * fdiv bug.
2913 void __stdcall _adj_fdivr_m16i( short arg )
2915 TRACE("(): stub\n");
2918 /***********************************************************************
2919 * _adj_fdivr_m32 (MSVCRT.@)
2921 * NOTE
2922 * I _think_ this function is intended to work around the Pentium
2923 * fdiv bug.
2925 void __stdcall _adj_fdivr_m32( unsigned int arg )
2927 TRACE("(): stub\n");
2930 /***********************************************************************
2931 * _adj_fdivr_m32i (MSVCRT.@)
2933 * NOTE
2934 * I _think_ this function is intended to work around the Pentium
2935 * fdiv bug.
2937 void __stdcall _adj_fdivr_m32i( int arg )
2939 TRACE("(): stub\n");
2942 /***********************************************************************
2943 * _adj_fdivr_m64 (MSVCRT.@)
2945 * NOTE
2946 * I _think_ this function is intended to work around the Pentium
2947 * fdiv bug.
2949 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
2951 TRACE("(): stub\n");
2954 /***********************************************************************
2955 * _adj_fpatan (MSVCRT.@)
2956 * FIXME
2957 * This function is likely to have the wrong number of arguments.
2959 * NOTE
2960 * I _think_ this function is intended to work around the Pentium
2961 * fdiv bug.
2963 void _adj_fpatan(void)
2965 TRACE("(): stub\n");
2968 /***********************************************************************
2969 * _adj_fprem (MSVCRT.@)
2970 * FIXME
2971 * This function is likely to have the wrong number of arguments.
2973 * NOTE
2974 * I _think_ this function is intended to work around the Pentium
2975 * fdiv bug.
2977 void _adj_fprem(void)
2979 TRACE("(): stub\n");
2982 /***********************************************************************
2983 * _adj_fprem1 (MSVCRT.@)
2984 * FIXME
2985 * This function is likely to have the wrong number of arguments.
2987 * NOTE
2988 * I _think_ this function is intended to work around the Pentium
2989 * fdiv bug.
2991 void _adj_fprem1(void)
2993 TRACE("(): stub\n");
2996 /***********************************************************************
2997 * _adj_fptan (MSVCRT.@)
2998 * FIXME
2999 * This function is likely to have the wrong number of arguments.
3001 * NOTE
3002 * I _think_ this function is intended to work around the Pentium
3003 * fdiv bug.
3005 void _adj_fptan(void)
3007 TRACE("(): stub\n");
3010 /***********************************************************************
3011 * _safe_fdiv (MSVCRT.@)
3012 * FIXME
3013 * This function is likely to have the wrong number of arguments.
3015 * NOTE
3016 * I _think_ this function is intended to work around the Pentium
3017 * fdiv bug.
3019 void _safe_fdiv(void)
3021 TRACE("(): stub\n");
3024 /***********************************************************************
3025 * _safe_fdivr (MSVCRT.@)
3026 * FIXME
3027 * This function is likely to have the wrong number of arguments.
3029 * NOTE
3030 * I _think_ this function is intended to work around the Pentium
3031 * fdiv bug.
3033 void _safe_fdivr(void)
3035 TRACE("(): stub\n");
3038 /***********************************************************************
3039 * _safe_fprem (MSVCRT.@)
3040 * FIXME
3041 * This function is likely to have the wrong number of arguments.
3043 * NOTE
3044 * I _think_ this function is intended to work around the Pentium
3045 * fdiv bug.
3047 void _safe_fprem(void)
3049 TRACE("(): stub\n");
3052 /***********************************************************************
3053 * _safe_fprem1 (MSVCRT.@)
3055 * FIXME
3056 * This function is likely to have the wrong number of arguments.
3058 * NOTE
3059 * I _think_ this function is intended to work around the Pentium
3060 * fdiv bug.
3062 void _safe_fprem1(void)
3064 TRACE("(): stub\n");
3067 /***********************************************************************
3068 * __libm_sse2_acos (MSVCRT.@)
3070 void __cdecl MSVCRT___libm_sse2_acos(void)
3072 double d;
3073 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3074 d = acos( d );
3075 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3078 /***********************************************************************
3079 * __libm_sse2_acosf (MSVCRT.@)
3081 void __cdecl MSVCRT___libm_sse2_acosf(void)
3083 float f;
3084 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3085 f = acosf( f );
3086 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3089 /***********************************************************************
3090 * __libm_sse2_asin (MSVCRT.@)
3092 void __cdecl MSVCRT___libm_sse2_asin(void)
3094 double d;
3095 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3096 d = asin( d );
3097 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3100 /***********************************************************************
3101 * __libm_sse2_asinf (MSVCRT.@)
3103 void __cdecl MSVCRT___libm_sse2_asinf(void)
3105 float f;
3106 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3107 f = asinf( f );
3108 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3111 /***********************************************************************
3112 * __libm_sse2_atan (MSVCRT.@)
3114 void __cdecl MSVCRT___libm_sse2_atan(void)
3116 double d;
3117 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3118 d = atan( d );
3119 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3122 /***********************************************************************
3123 * __libm_sse2_atan2 (MSVCRT.@)
3125 void __cdecl MSVCRT___libm_sse2_atan2(void)
3127 double d1, d2;
3128 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
3129 d1 = atan2( d1, d2 );
3130 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
3133 /***********************************************************************
3134 * __libm_sse2_atanf (MSVCRT.@)
3136 void __cdecl MSVCRT___libm_sse2_atanf(void)
3138 float f;
3139 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3140 f = atanf( f );
3141 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3144 /***********************************************************************
3145 * __libm_sse2_cos (MSVCRT.@)
3147 void __cdecl MSVCRT___libm_sse2_cos(void)
3149 double d;
3150 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3151 d = cos( d );
3152 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3155 /***********************************************************************
3156 * __libm_sse2_cosf (MSVCRT.@)
3158 void __cdecl MSVCRT___libm_sse2_cosf(void)
3160 float f;
3161 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3162 f = cosf( f );
3163 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3166 /***********************************************************************
3167 * __libm_sse2_exp (MSVCRT.@)
3169 void __cdecl MSVCRT___libm_sse2_exp(void)
3171 double d;
3172 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3173 d = exp( d );
3174 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3177 /***********************************************************************
3178 * __libm_sse2_expf (MSVCRT.@)
3180 void __cdecl MSVCRT___libm_sse2_expf(void)
3182 float f;
3183 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3184 f = expf( f );
3185 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3188 /***********************************************************************
3189 * __libm_sse2_log (MSVCRT.@)
3191 void __cdecl MSVCRT___libm_sse2_log(void)
3193 double d;
3194 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3195 d = log( d );
3196 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3199 /***********************************************************************
3200 * __libm_sse2_log10 (MSVCRT.@)
3202 void __cdecl MSVCRT___libm_sse2_log10(void)
3204 double d;
3205 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3206 d = log10( d );
3207 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3210 /***********************************************************************
3211 * __libm_sse2_log10f (MSVCRT.@)
3213 void __cdecl MSVCRT___libm_sse2_log10f(void)
3215 float f;
3216 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3217 f = log10f( f );
3218 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3221 /***********************************************************************
3222 * __libm_sse2_logf (MSVCRT.@)
3224 void __cdecl MSVCRT___libm_sse2_logf(void)
3226 float f;
3227 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3228 f = logf( f );
3229 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3232 /***********************************************************************
3233 * __libm_sse2_pow (MSVCRT.@)
3235 void __cdecl MSVCRT___libm_sse2_pow(void)
3237 double d1, d2;
3238 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
3239 d1 = pow( d1, d2 );
3240 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
3243 /***********************************************************************
3244 * __libm_sse2_powf (MSVCRT.@)
3246 void __cdecl MSVCRT___libm_sse2_powf(void)
3248 float f1, f2;
3249 __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
3250 f1 = powf( f1, f2 );
3251 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
3254 /***********************************************************************
3255 * __libm_sse2_sin (MSVCRT.@)
3257 void __cdecl MSVCRT___libm_sse2_sin(void)
3259 double d;
3260 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3261 d = sin( d );
3262 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3265 /***********************************************************************
3266 * __libm_sse2_sinf (MSVCRT.@)
3268 void __cdecl MSVCRT___libm_sse2_sinf(void)
3270 float f;
3271 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3272 f = sinf( f );
3273 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3276 /***********************************************************************
3277 * __libm_sse2_tan (MSVCRT.@)
3279 void __cdecl MSVCRT___libm_sse2_tan(void)
3281 double d;
3282 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3283 d = tan( d );
3284 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3287 /***********************************************************************
3288 * __libm_sse2_tanf (MSVCRT.@)
3290 void __cdecl MSVCRT___libm_sse2_tanf(void)
3292 float f;
3293 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3294 f = tanf( f );
3295 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3298 /***********************************************************************
3299 * __libm_sse2_sqrt_precise (MSVCR110.@)
3301 void __cdecl MSVCRT___libm_sse2_sqrt_precise(void)
3303 double d;
3304 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3305 d = sqrt( d );
3306 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3309 #endif /* __i386__ */
3311 /*********************************************************************
3312 * cbrt (MSVCR120.@)
3314 double CDECL MSVCR120_cbrt(double x)
3316 #ifdef HAVE_CBRT
3317 return cbrt(x);
3318 #else
3319 return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
3320 #endif
3323 /*********************************************************************
3324 * cbrtf (MSVCR120.@)
3326 float CDECL MSVCR120_cbrtf(float x)
3328 #ifdef HAVE_CBRTF
3329 return cbrtf(x);
3330 #else
3331 return MSVCR120_cbrt(x);
3332 #endif
3335 /*********************************************************************
3336 * cbrtl (MSVCR120.@)
3338 LDOUBLE CDECL MSVCR120_cbrtl(LDOUBLE x)
3340 return MSVCR120_cbrt(x);
3343 /*********************************************************************
3344 * exp2 (MSVCR120.@)
3346 double CDECL MSVCR120_exp2(double x)
3348 #ifdef HAVE_EXP2
3349 double ret = exp2(x);
3350 #else
3351 double ret = pow(2, x);
3352 #endif
3353 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3354 return ret;
3357 /*********************************************************************
3358 * exp2f (MSVCR120.@)
3360 float CDECL MSVCR120_exp2f(float x)
3362 #ifdef HAVE_EXP2F
3363 float ret = exp2f(x);
3364 if (finitef(x) && !finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3365 return ret;
3366 #else
3367 return MSVCR120_exp2(x);
3368 #endif
3371 /*********************************************************************
3372 * exp2l (MSVCR120.@)
3374 LDOUBLE CDECL MSVCR120_exp2l(LDOUBLE x)
3376 return MSVCR120_exp2(x);
3379 /*********************************************************************
3380 * expm1 (MSVCR120.@)
3382 double CDECL MSVCR120_expm1(double x)
3384 #ifdef HAVE_EXPM1
3385 double ret = expm1(x);
3386 #else
3387 double ret = exp(x) - 1;
3388 #endif
3389 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3390 return ret;
3393 /*********************************************************************
3394 * expm1f (MSVCR120.@)
3396 float CDECL MSVCR120_expm1f(float x)
3398 #ifdef HAVE_EXPM1F
3399 float ret = expm1f(x);
3400 #else
3401 float ret = exp(x) - 1;
3402 #endif
3403 if (finitef(x) && !finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3404 return ret;
3407 /*********************************************************************
3408 * expm1l (MSVCR120.@)
3410 LDOUBLE CDECL MSVCR120_expm1l(LDOUBLE x)
3412 return MSVCR120_expm1(x);
3415 /*********************************************************************
3416 * log1p (MSVCR120.@)
3418 double CDECL MSVCR120_log1p(double x)
3420 if (x < -1) *MSVCRT__errno() = MSVCRT_EDOM;
3421 else if (x == -1) *MSVCRT__errno() = MSVCRT_ERANGE;
3422 #ifdef HAVE_LOG1P
3423 return log1p(x);
3424 #else
3425 return log(1 + x);
3426 #endif
3429 /*********************************************************************
3430 * log1pf (MSVCR120.@)
3432 float CDECL MSVCR120_log1pf(float x)
3434 if (x < -1) *MSVCRT__errno() = MSVCRT_EDOM;
3435 else if (x == -1) *MSVCRT__errno() = MSVCRT_ERANGE;
3436 #ifdef HAVE_LOG1PF
3437 return log1pf(x);
3438 #else
3439 return log(1 + x);
3440 #endif
3443 /*********************************************************************
3444 * log1pl (MSVCR120.@)
3446 LDOUBLE CDECL MSVCR120_log1pl(LDOUBLE x)
3448 return MSVCR120_log1p(x);
3451 /*********************************************************************
3452 * log2 (MSVCR120.@)
3454 double CDECL MSVCR120_log2(double x)
3456 if (x < 0) *MSVCRT__errno() = MSVCRT_EDOM;
3457 else if (x == 0) *MSVCRT__errno() = MSVCRT_ERANGE;
3458 #ifdef HAVE_LOG2
3459 return log2(x);
3460 #else
3461 return log(x) / log(2);
3462 #endif
3465 /*********************************************************************
3466 * log2f (MSVCR120.@)
3468 float CDECL MSVCR120_log2f(float x)
3470 #ifdef HAVE_LOG2F
3471 if (x < 0) *MSVCRT__errno() = MSVCRT_EDOM;
3472 else if (x == 0) *MSVCRT__errno() = MSVCRT_ERANGE;
3473 return log2f(x);
3474 #else
3475 return MSVCR120_log2(x);
3476 #endif
3479 /*********************************************************************
3480 * log2l (MSVCR120.@)
3482 LDOUBLE CDECL MSVCR120_log2l(LDOUBLE x)
3484 return MSVCR120_log2(x);
3487 /*********************************************************************
3488 * rint (MSVCR120.@)
3490 double CDECL MSVCR120_rint(double x)
3492 return rint(x);
3495 /*********************************************************************
3496 * rintf (MSVCR120.@)
3498 float CDECL MSVCR120_rintf(float x)
3500 return rintf(x);
3503 /*********************************************************************
3504 * rintl (MSVCR120.@)
3506 LDOUBLE CDECL MSVCR120_rintl(LDOUBLE x)
3508 return MSVCR120_rint(x);
3511 /*********************************************************************
3512 * lrint (MSVCR120.@)
3514 MSVCRT_long CDECL MSVCR120_lrint(double x)
3516 return lrint(x);
3519 /*********************************************************************
3520 * lrintf (MSVCR120.@)
3522 MSVCRT_long CDECL MSVCR120_lrintf(float x)
3524 return lrintf(x);
3527 /*********************************************************************
3528 * lrintl (MSVCR120.@)
3530 MSVCRT_long CDECL MSVCR120_lrintl(LDOUBLE x)
3532 return MSVCR120_lrint(x);
3535 /*********************************************************************
3536 * llrint (MSVCR120.@)
3538 MSVCRT_longlong CDECL MSVCR120_llrint(double x)
3540 return llrint(x);
3543 /*********************************************************************
3544 * llrintf (MSVCR120.@)
3546 MSVCRT_longlong CDECL MSVCR120_llrintf(float x)
3548 return llrintf(x);
3551 /*********************************************************************
3552 * rintl (MSVCR120.@)
3554 MSVCRT_longlong CDECL MSVCR120_llrintl(LDOUBLE x)
3556 return MSVCR120_llrint(x);
3559 #if _MSVCR_VER>=120
3561 /*********************************************************************
3562 * round (MSVCR120.@)
3564 double CDECL MSVCR120_round(double x)
3566 #ifdef HAVE_ROUND
3567 return round(x);
3568 #else
3569 return MSVCR120_rint(x);
3570 #endif
3573 /*********************************************************************
3574 * roundf (MSVCR120.@)
3576 float CDECL MSVCR120_roundf(float x)
3578 #ifdef HAVE_ROUNDF
3579 return roundf(x);
3580 #else
3581 return MSVCR120_round(x);
3582 #endif
3585 /*********************************************************************
3586 * roundl (MSVCR120.@)
3588 LDOUBLE CDECL MSVCR120_roundl(LDOUBLE x)
3590 return MSVCR120_round(x);
3593 /*********************************************************************
3594 * lround (MSVCR120.@)
3596 MSVCRT_long CDECL MSVCR120_lround(double x)
3598 #ifdef HAVE_LROUND
3599 return lround(x);
3600 #else
3601 return MSVCR120_round(x);
3602 #endif
3605 /*********************************************************************
3606 * lroundf (MSVCR120.@)
3608 MSVCRT_long CDECL MSVCR120_lroundf(float x)
3610 #ifdef HAVE_LROUNDF
3611 return lroundf(x);
3612 #else
3613 return MSVCR120_lround(x);
3614 #endif
3617 /*********************************************************************
3618 * lroundl (MSVCR120.@)
3620 MSVCRT_long CDECL MSVCR120_lroundl(LDOUBLE x)
3622 return MSVCR120_lround(x);
3625 /*********************************************************************
3626 * llround (MSVCR120.@)
3628 MSVCRT_longlong CDECL MSVCR120_llround(double x)
3630 #ifdef HAVE_LLROUND
3631 return llround(x);
3632 #else
3633 return MSVCR120_round(x);
3634 #endif
3637 /*********************************************************************
3638 * llroundf (MSVCR120.@)
3640 MSVCRT_longlong CDECL MSVCR120_llroundf(float x)
3642 #ifdef HAVE_LLROUNDF
3643 return llroundf(x);
3644 #else
3645 return MSVCR120_llround(x);
3646 #endif
3649 /*********************************************************************
3650 * roundl (MSVCR120.@)
3652 MSVCRT_longlong CDECL MSVCR120_llroundl(LDOUBLE x)
3654 return MSVCR120_llround(x);
3657 /*********************************************************************
3658 * trunc (MSVCR120.@)
3660 double CDECL MSVCR120_trunc(double x)
3662 #ifdef HAVE_TRUNC
3663 return trunc(x);
3664 #else
3665 return (x > 0) ? floor(x) : ceil(x);
3666 #endif
3669 /*********************************************************************
3670 * truncf (MSVCR120.@)
3672 float CDECL MSVCR120_truncf(float x)
3674 #ifdef HAVE_TRUNCF
3675 return truncf(x);
3676 #else
3677 return MSVCR120_trunc(x);
3678 #endif
3681 /*********************************************************************
3682 * truncl (MSVCR120.@)
3684 LDOUBLE CDECL MSVCR120_truncl(LDOUBLE x)
3686 return MSVCR120_trunc(x);
3689 /*********************************************************************
3690 * _dclass (MSVCR120.@)
3692 * Copied from musl: src/math/__fpclassify.c
3694 short CDECL MSVCR120__dclass(double x)
3696 union { double f; UINT64 i; } u = { x };
3697 int e = u.i >> 52 & 0x7ff;
3699 if (!e) return u.i << 1 ? MSVCRT_FP_SUBNORMAL : MSVCRT_FP_ZERO;
3700 if (e == 0x7ff) return (u.i << 12) ? MSVCRT_FP_NAN : MSVCRT_FP_INFINITE;
3701 return MSVCRT_FP_NORMAL;
3704 /*********************************************************************
3705 * _fdclass (MSVCR120.@)
3707 * Copied from musl: src/math/__fpclassifyf.c
3709 short CDECL MSVCR120__fdclass(float x)
3711 union { float f; UINT32 i; } u = { x };
3712 int e = u.i >> 23 & 0xff;
3714 if (!e) return u.i << 1 ? MSVCRT_FP_SUBNORMAL : MSVCRT_FP_ZERO;
3715 if (e == 0xff) return u.i << 9 ? MSVCRT_FP_NAN : MSVCRT_FP_INFINITE;
3716 return MSVCRT_FP_NORMAL;
3719 /*********************************************************************
3720 * _ldclass (MSVCR120.@)
3722 short CDECL MSVCR120__ldclass(LDOUBLE x)
3724 return MSVCR120__dclass(x);
3727 /*********************************************************************
3728 * _dtest (MSVCR120.@)
3730 short CDECL MSVCR120__dtest(double *x)
3732 return MSVCR120__dclass(*x);
3735 /*********************************************************************
3736 * _fdtest (MSVCR120.@)
3738 short CDECL MSVCR120__fdtest(float *x)
3740 return MSVCR120__fdclass(*x);
3743 /*********************************************************************
3744 * _ldtest (MSVCR120.@)
3746 short CDECL MSVCR120__ldtest(LDOUBLE *x)
3748 return MSVCR120__dclass(*x);
3751 /*********************************************************************
3752 * erf (MSVCR120.@)
3754 double CDECL MSVCR120_erf(double x)
3756 #ifdef HAVE_ERF
3757 return erf(x);
3758 #else
3759 /* Abramowitz and Stegun approximation, maximum error: 1.5*10^-7 */
3760 double t, y;
3761 int sign = signbit(x);
3763 if (sign) x = -x;
3764 t = 1 / (1 + 0.3275911 * x);
3765 y = ((((1.061405429*t - 1.453152027)*t + 1.421413741)*t - 0.284496736)*t + 0.254829592)*t;
3766 y = 1.0 - y*exp(-x*x);
3767 return sign ? -y : y;
3768 #endif
3771 /*********************************************************************
3772 * erff (MSVCR120.@)
3774 float CDECL MSVCR120_erff(float x)
3776 #ifdef HAVE_ERFF
3777 return erff(x);
3778 #else
3779 return MSVCR120_erf(x);
3780 #endif
3783 /*********************************************************************
3784 * erfl (MSVCR120.@)
3786 LDOUBLE CDECL MSVCR120_erfl(LDOUBLE x)
3788 return MSVCR120_erf(x);
3791 /*********************************************************************
3792 * erfc (MSVCR120.@)
3794 double CDECL MSVCR120_erfc(double x)
3796 #ifdef HAVE_ERFC
3797 return erfc(x);
3798 #else
3799 return 1 - MSVCR120_erf(x);
3800 #endif
3803 /*********************************************************************
3804 * erfcf (MSVCR120.@)
3806 float CDECL MSVCR120_erfcf(float x)
3808 #ifdef HAVE_ERFCF
3809 return erfcf(x);
3810 #else
3811 return MSVCR120_erfc(x);
3812 #endif
3815 /*********************************************************************
3816 * erfcl (MSVCR120.@)
3818 LDOUBLE CDECL MSVCR120_erfcl(LDOUBLE x)
3820 return MSVCR120_erfc(x);
3823 /*********************************************************************
3824 * fmaxf (MSVCR120.@)
3826 float CDECL MSVCR120_fmaxf(float x, float y)
3828 if(isnan(x))
3829 return y;
3830 if(isnan(y))
3831 return x;
3832 if(x==0 && y==0)
3833 return signbit(x) ? y : x;
3834 return x<y ? y : x;
3837 /*********************************************************************
3838 * fmax (MSVCR120.@)
3840 double CDECL MSVCR120_fmax(double x, double y)
3842 if(isnan(x))
3843 return y;
3844 if(isnan(y))
3845 return x;
3846 if(x==0 && y==0)
3847 return signbit(x) ? y : x;
3848 return x<y ? y : x;
3851 /*********************************************************************
3852 * fdimf (MSVCR120.@)
3854 float CDECL MSVCR120_fdimf(float x, float y)
3856 if(isnan(x))
3857 return x;
3858 if(isnan(y))
3859 return y;
3860 return x>y ? x-y : 0;
3863 /*********************************************************************
3864 * fdim (MSVCR120.@)
3866 double CDECL MSVCR120_fdim(double x, double y)
3868 if(isnan(x))
3869 return x;
3870 if(isnan(y))
3871 return y;
3872 return x>y ? x-y : 0;
3875 /*********************************************************************
3876 * _fdsign (MSVCR120.@)
3878 int CDECL MSVCR120__fdsign(float x)
3880 return signbit(x) ? 0x8000 : 0;
3883 /*********************************************************************
3884 * _dsign (MSVCR120.@)
3886 int CDECL MSVCR120__dsign(double x)
3888 return signbit(x) ? 0x8000 : 0;
3892 /*********************************************************************
3893 * _dpcomp (MSVCR120.@)
3895 int CDECL MSVCR120__dpcomp(double x, double y)
3897 if(isnan(x) || isnan(y))
3898 return 0;
3900 if(x == y) return 2;
3901 return x < y ? 1 : 4;
3904 /*********************************************************************
3905 * _fdpcomp (MSVCR120.@)
3907 int CDECL MSVCR120__fdpcomp(float x, float y)
3909 return MSVCR120__dpcomp(x, y);
3912 /*********************************************************************
3913 * fminf (MSVCR120.@)
3915 float CDECL MSVCR120_fminf(float x, float y)
3917 if(isnan(x))
3918 return y;
3919 if(isnan(y))
3920 return x;
3921 if(x==0 && y==0)
3922 return signbit(x) ? x : y;
3923 return x<y ? x : y;
3926 /*********************************************************************
3927 * fmin (MSVCR120.@)
3929 double CDECL MSVCR120_fmin(double x, double y)
3931 if(isnan(x))
3932 return y;
3933 if(isnan(y))
3934 return x;
3935 if(x==0 && y==0)
3936 return signbit(x) ? x : y;
3937 return x<y ? x : y;
3940 /*********************************************************************
3941 * asinh (MSVCR120.@)
3943 double CDECL MSVCR120_asinh(double x)
3945 #ifdef HAVE_ASINH
3946 return asinh(x);
3947 #else
3948 if (!isfinite(x*x+1)) {
3949 if (x > 0) return log(2) + log(x);
3950 else return -log(2) - log(-x);
3952 return log(x + sqrt(x*x+1));
3953 #endif
3956 /*********************************************************************
3957 * asinhf (MSVCR120.@)
3959 float CDECL MSVCR120_asinhf(float x)
3961 #ifdef HAVE_ASINHF
3962 return asinhf(x);
3963 #else
3964 return MSVCR120_asinh(x);
3965 #endif
3968 /*********************************************************************
3969 * asinhl (MSVCR120.@)
3971 LDOUBLE CDECL MSVCR120_asinhl(LDOUBLE x)
3973 return MSVCR120_asinh(x);
3976 /*********************************************************************
3977 * acosh (MSVCR120.@)
3979 double CDECL MSVCR120_acosh(double x)
3981 if (x < 1) *MSVCRT__errno() = MSVCRT_EDOM;
3983 #ifdef HAVE_ACOSH
3984 return acosh(x);
3985 #else
3986 if (x < 1) {
3987 MSVCRT_fenv_t env;
3989 MSVCRT_fegetenv(&env);
3990 env.status |= MSVCRT__SW_INVALID;
3991 MSVCRT_fesetenv(&env);
3992 return NAN;
3994 if (!isfinite(x*x)) return log(2) + log(x);
3995 return log(x + sqrt(x*x-1));
3996 #endif
3999 /*********************************************************************
4000 * acoshf (MSVCR120.@)
4002 float CDECL MSVCR120_acoshf(float x)
4004 #ifdef HAVE_ACOSHF
4005 if (x < 1) *MSVCRT__errno() = MSVCRT_EDOM;
4007 return acoshf(x);
4008 #else
4009 return MSVCR120_acosh(x);
4010 #endif
4013 /*********************************************************************
4014 * acoshl (MSVCR120.@)
4016 LDOUBLE CDECL MSVCR120_acoshl(LDOUBLE x)
4018 return MSVCR120_acosh(x);
4021 /*********************************************************************
4022 * atanh (MSVCR120.@)
4024 double CDECL MSVCR120_atanh(double x)
4026 double ret;
4028 if (x > 1 || x < -1) {
4029 MSVCRT_fenv_t env;
4031 *MSVCRT__errno() = MSVCRT_EDOM;
4033 /* on Linux atanh returns -NAN in this case */
4034 MSVCRT_fegetenv(&env);
4035 env.status |= MSVCRT__SW_INVALID;
4036 MSVCRT_fesetenv(&env);
4037 return NAN;
4040 #ifdef HAVE_ATANH
4041 ret = atanh(x);
4042 #else
4043 if (-1e-6 < x && x < 1e-6) ret = x + x*x*x/3;
4044 else ret = (log(1+x) - log(1-x)) / 2;
4045 #endif
4047 if (!isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
4048 return ret;
4051 /*********************************************************************
4052 * atanhf (MSVCR120.@)
4054 float CDECL MSVCR120_atanhf(float x)
4056 #ifdef HAVE_ATANHF
4057 float ret;
4059 if (x > 1 || x < -1) {
4060 MSVCRT_fenv_t env;
4062 *MSVCRT__errno() = MSVCRT_EDOM;
4064 MSVCRT_fegetenv(&env);
4065 env.status |= MSVCRT__SW_INVALID;
4066 MSVCRT_fesetenv(&env);
4067 return NAN;
4070 ret = atanhf(x);
4072 if (!finitef(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
4073 return ret;
4074 #else
4075 return MSVCR120_atanh(x);
4076 #endif
4079 /*********************************************************************
4080 * atanhl (MSVCR120.@)
4082 LDOUBLE CDECL MSVCR120_atanhl(LDOUBLE x)
4084 return MSVCR120_atanh(x);
4087 #endif /* _MSVCR_VER>=120 */
4089 /*********************************************************************
4090 * _scalb (MSVCRT.@)
4091 * scalbn (MSVCR120.@)
4092 * scalbln (MSVCR120.@)
4094 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
4096 return MSVCRT_ldexp(num, power);
4099 /*********************************************************************
4100 * _scalbf (MSVCRT.@)
4101 * scalbnf (MSVCR120.@)
4102 * scalblnf (MSVCR120.@)
4104 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
4106 return MSVCRT_ldexp(num, power);
4109 #if _MSVCR_VER>=120
4111 /*********************************************************************
4112 * scalbnl (MSVCR120.@)
4113 * scalblnl (MSVCR120.@)
4115 LDOUBLE CDECL MSVCR120_scalbnl(LDOUBLE num, MSVCRT_long power)
4117 return MSVCRT__scalb(num, power);
4120 /*********************************************************************
4121 * remainder (MSVCR120.@)
4123 double CDECL MSVCR120_remainder(double x, double y)
4125 #ifdef HAVE_REMAINDER
4126 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
4127 if(!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
4128 if(isnan(y) || y==0.0) *MSVCRT__errno() = MSVCRT_EDOM;
4129 return remainder(x, y);
4130 #else
4131 FIXME( "not implemented\n" );
4132 return 0.0;
4133 #endif
4136 /*********************************************************************
4137 * remainderf (MSVCR120.@)
4139 float CDECL MSVCR120_remainderf(float x, float y)
4141 #ifdef HAVE_REMAINDERF
4142 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
4143 if(!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
4144 if(isnan(y) || y==0.0f) *MSVCRT__errno() = MSVCRT_EDOM;
4145 return remainderf(x, y);
4146 #else
4147 FIXME( "not implemented\n" );
4148 return 0.0f;
4149 #endif
4152 /*********************************************************************
4153 * remainderl (MSVCR120.@)
4155 LDOUBLE CDECL MSVCR120_remainderl(LDOUBLE x, LDOUBLE y)
4157 return MSVCR120_remainder(x, y);
4160 /*********************************************************************
4161 * remquo (MSVCR120.@)
4163 double CDECL MSVCR120_remquo(double x, double y, int *quo)
4165 #ifdef HAVE_REMQUO
4166 if(!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
4167 if(isnan(y) || y==0.0) *MSVCRT__errno() = MSVCRT_EDOM;
4168 return remquo(x, y, quo);
4169 #else
4170 FIXME( "not implemented\n" );
4171 return 0.0;
4172 #endif
4175 /*********************************************************************
4176 * remquof (MSVCR120.@)
4178 float CDECL MSVCR120_remquof(float x, float y, int *quo)
4180 #ifdef HAVE_REMQUOF
4181 if(!finitef(x)) *MSVCRT__errno() = MSVCRT_EDOM;
4182 if(isnan(y) || y==0.0f) *MSVCRT__errno() = MSVCRT_EDOM;
4183 return remquof(x, y, quo);
4184 #else
4185 FIXME( "not implemented\n" );
4186 return 0.0f;
4187 #endif
4190 /*********************************************************************
4191 * remquol (MSVCR120.@)
4193 LDOUBLE CDECL MSVCR120_remquol(LDOUBLE x, LDOUBLE y, int *quo)
4195 return MSVCR120_remquo(x, y, quo);
4198 /*********************************************************************
4199 * lgamma (MSVCR120.@)
4201 double CDECL MSVCR120_lgamma(double x)
4203 #ifdef HAVE_LGAMMA
4204 return lgamma(x);
4205 #else
4206 FIXME( "not implemented\n" );
4207 return 0.0;
4208 #endif
4211 /*********************************************************************
4212 * lgammaf (MSVCR120.@)
4214 float CDECL MSVCR120_lgammaf(float x)
4216 #ifdef HAVE_LGAMMAF
4217 return lgammaf(x);
4218 #else
4219 FIXME( "not implemented\n" );
4220 return 0.0f;
4221 #endif
4224 /*********************************************************************
4225 * lgammal (MSVCR120.@)
4227 LDOUBLE CDECL MSVCR120_lgammal(LDOUBLE x)
4229 return MSVCR120_lgamma(x);
4232 /*********************************************************************
4233 * tgamma (MSVCR120.@)
4235 double CDECL MSVCR120_tgamma(double x)
4237 #ifdef HAVE_TGAMMA
4238 if(x==0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
4239 if(x<0.0f) {
4240 double integral;
4241 if (modf(x, &integral) == 0)
4242 *MSVCRT__errno() = MSVCRT_EDOM;
4244 return tgamma(x);
4245 #else
4246 FIXME( "not implemented\n" );
4247 return 0.0;
4248 #endif
4251 /*********************************************************************
4252 * tgammaf (MSVCR120.@)
4254 float CDECL MSVCR120_tgammaf(float x)
4256 #ifdef HAVE_TGAMMAF
4257 if(x==0.0f) *MSVCRT__errno() = MSVCRT_ERANGE;
4258 if(x<0.0f) {
4259 float integral;
4260 if (modff(x, &integral) == 0)
4261 *MSVCRT__errno() = MSVCRT_EDOM;
4263 return tgammaf(x);
4264 #else
4265 FIXME( "not implemented\n" );
4266 return 0.0f;
4267 #endif
4270 /*********************************************************************
4271 * nan (MSVCR120.@)
4273 double CDECL MSVCR120_nan(const char *tagp)
4275 /* Windows ignores input (MSDN) */
4276 return NAN;
4279 /*********************************************************************
4280 * nanf (MSVCR120.@)
4282 float CDECL MSVCR120_nanf(const char *tagp)
4284 return NAN;
4287 /*********************************************************************
4288 * _except1 (MSVCR120.@)
4289 * TODO:
4290 * - find meaning of ignored cw and operation bits
4291 * - unk parameter
4293 double CDECL _except1(DWORD fpe, _FP_OPERATION_CODE op, double arg, double res, DWORD cw, void *unk)
4295 ULONG_PTR exception_arg;
4296 DWORD exception = 0;
4297 MSVCRT_fenv_t env;
4298 DWORD fpword = 0;
4299 WORD operation;
4301 TRACE("(%x %x %lf %lf %x %p)\n", fpe, op, arg, res, cw, unk);
4303 #ifdef _WIN64
4304 cw = ((cw >> 7) & 0x3f) | ((cw >> 3) & 0xc00);
4305 #endif
4306 operation = op << 5;
4307 exception_arg = (ULONG_PTR)&operation;
4309 MSVCRT_fegetenv(&env);
4311 if (fpe & 0x1) { /* overflow */
4312 if ((fpe == 0x1 && (cw & 0x8)) || (fpe==0x11 && (cw & 0x28))) {
4313 /* 32-bit version also sets SW_INEXACT here */
4314 env.status |= MSVCRT__SW_OVERFLOW;
4315 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
4316 res = signbit(res) ? -INFINITY : INFINITY;
4317 } else {
4318 exception = EXCEPTION_FLT_OVERFLOW;
4320 } else if (fpe & 0x2) { /* underflow */
4321 if ((fpe == 0x2 && (cw & 0x10)) || (fpe==0x12 && (cw & 0x30))) {
4322 env.status |= MSVCRT__SW_UNDERFLOW;
4323 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
4324 res = signbit(res) ? -0.0 : 0.0;
4325 } else {
4326 exception = EXCEPTION_FLT_UNDERFLOW;
4328 } else if (fpe & 0x4) { /* zerodivide */
4329 if ((fpe == 0x4 && (cw & 0x4)) || (fpe==0x14 && (cw & 0x24))) {
4330 env.status |= MSVCRT__SW_ZERODIVIDE;
4331 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
4332 } else {
4333 exception = EXCEPTION_FLT_DIVIDE_BY_ZERO;
4335 } else if (fpe & 0x8) { /* invalid */
4336 if (fpe == 0x8 && (cw & 0x1)) {
4337 env.status |= MSVCRT__SW_INVALID;
4338 } else {
4339 exception = EXCEPTION_FLT_INVALID_OPERATION;
4341 } else if (fpe & 0x10) { /* inexact */
4342 if (fpe == 0x10 && (cw & 0x20)) {
4343 env.status |= MSVCRT__SW_INEXACT;
4344 } else {
4345 exception = EXCEPTION_FLT_INEXACT_RESULT;
4349 if (exception)
4350 env.status = 0;
4351 MSVCRT_fesetenv(&env);
4352 if (exception)
4353 RaiseException(exception, 0, 1, &exception_arg);
4355 if (cw & 0x1) fpword |= MSVCRT__EM_INVALID;
4356 if (cw & 0x2) fpword |= MSVCRT__EM_DENORMAL;
4357 if (cw & 0x4) fpword |= MSVCRT__EM_ZERODIVIDE;
4358 if (cw & 0x8) fpword |= MSVCRT__EM_OVERFLOW;
4359 if (cw & 0x10) fpword |= MSVCRT__EM_UNDERFLOW;
4360 if (cw & 0x20) fpword |= MSVCRT__EM_INEXACT;
4361 switch (cw & 0xc00)
4363 case 0xc00: fpword |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
4364 case 0x800: fpword |= MSVCRT__RC_UP; break;
4365 case 0x400: fpword |= MSVCRT__RC_DOWN; break;
4367 switch (cw & 0x300)
4369 case 0x0: fpword |= MSVCRT__PC_24; break;
4370 case 0x200: fpword |= MSVCRT__PC_53; break;
4371 case 0x300: fpword |= MSVCRT__PC_64; break;
4373 if (cw & 0x1000) fpword |= MSVCRT__IC_AFFINE;
4374 _control87(fpword, 0xffffffff);
4376 return res;
4379 _Dcomplex* CDECL MSVCR120__Cbuild(_Dcomplex *ret, double r, double i)
4381 ret->x = r;
4382 ret->y = i;
4383 return ret;
4386 double CDECL MSVCR120_creal(_Dcomplex z)
4388 return z.x;
4391 int CDECL MSVCR120_ilogb(double x)
4393 if (!x) return MSVCRT_FP_ILOGB0;
4394 if (isnan(x)) return MSVCRT_FP_ILOGBNAN;
4395 if (isinf(x)) return MSVCRT_INT_MAX;
4397 #ifdef HAVE_ILOGB
4398 return ilogb(x);
4399 #else
4400 return logb(x);
4401 #endif
4404 int CDECL MSVCR120_ilogbf(float x)
4406 if (!x) return MSVCRT_FP_ILOGB0;
4407 if (isnan(x)) return MSVCRT_FP_ILOGBNAN;
4408 if (isinf(x)) return MSVCRT_INT_MAX;
4410 #ifdef HAVE_ILOGBF
4411 return ilogbf(x);
4412 #else
4413 return logbf(x);
4414 #endif
4417 int CDECL MSVCR120_ilogbl(LDOUBLE x)
4419 return MSVCR120_ilogb(x);
4422 #endif /* _MSVCR_VER>=120 */