directmanipulation: Return S_OK form viewport_SetViewportOptions stub.
[wine/zf.git] / dlls / msvcrt / math.c
blob84a530ede502de7e77c32edf6925678ab74b88ef
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 libc (http://musl.libc.org/):
22 * ====================================================
23 * Copyright 2005-2020 Rich Felker, et al.
25 * Permission is hereby granted, free of charge, to any person obtaining
26 * a copy of this software and associated documentation files (the
27 * "Software"), to deal in the Software without restriction, including
28 * without limitation the rights to use, copy, modify, merge, publish,
29 * distribute, sublicense, and/or sell copies of the Software, and to
30 * permit persons to whom the Software is furnished to do so, subject to
31 * the following conditions:
33 * The above copyright notice and this permission notice shall be
34 * included in all copies or substantial portions of the Software.
35 * ====================================================
38 #include "config.h"
39 #include "wine/port.h"
41 #include <stdio.h>
42 #define __USE_ISOC9X 1
43 #define __USE_ISOC99 1
44 #include <math.h>
45 #ifdef HAVE_IEEEFP_H
46 #include <ieeefp.h>
47 #endif
49 #include "msvcrt.h"
51 #include "wine/asm.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
56 #define _DOMAIN 1 /* domain error in argument */
57 #define _SING 2 /* singularity */
58 #define _OVERFLOW 3 /* range overflow */
59 #define _UNDERFLOW 4 /* range underflow */
61 typedef int (CDECL *MSVCRT_matherr_func)(struct MSVCRT__exception *);
62 typedef double LDOUBLE; /* long double is just a double */
64 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
66 static BOOL sse2_supported;
67 static BOOL sse2_enabled;
69 void msvcrt_init_math(void)
71 sse2_supported = sse2_enabled = IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE );
74 /* Copied from musl: src/internal/libm.h */
75 static inline float fp_barrierf(float x)
77 volatile float y = x;
78 return y;
81 /*********************************************************************
82 * _matherr (CRTDLL.@)
84 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
86 return 0;
90 static double math_error(int type, const char *name, double arg1, double arg2, double retval)
92 struct MSVCRT__exception exception = {type, (char *)name, arg1, arg2, retval};
94 TRACE("(%d, %s, %g, %g, %g)\n", type, debugstr_a(name), arg1, arg2, retval);
96 if (MSVCRT_default_matherr_func && MSVCRT_default_matherr_func(&exception))
97 return exception.retval;
99 switch (type)
101 case _DOMAIN:
102 *MSVCRT__errno() = MSVCRT_EDOM;
103 break;
104 case _SING:
105 case _OVERFLOW:
106 *MSVCRT__errno() = MSVCRT_ERANGE;
107 break;
108 case _UNDERFLOW:
109 /* don't set errno */
110 break;
111 default:
112 ERR("Unhandled math error!\n");
115 return exception.retval;
118 /*********************************************************************
119 * __setusermatherr (MSVCRT.@)
121 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
123 MSVCRT_default_matherr_func = func;
124 TRACE("new matherr handler %p\n", func);
127 /*********************************************************************
128 * _set_SSE2_enable (MSVCRT.@)
130 int CDECL MSVCRT__set_SSE2_enable(int flag)
132 sse2_enabled = flag && sse2_supported;
133 return sse2_enabled;
136 #if defined(_WIN64)
137 # if _MSVCR_VER>=140
138 /*********************************************************************
139 * _get_FMA3_enable (UCRTBASE.@)
141 int CDECL MSVCRT__get_FMA3_enable(void)
143 FIXME("() stub\n");
144 return 0;
146 # endif
148 # if _MSVCR_VER>=120
149 /*********************************************************************
150 * _set_FMA3_enable (MSVCR120.@)
152 int CDECL MSVCRT__set_FMA3_enable(int flag)
154 FIXME("(%x) stub\n", flag);
155 return 0;
157 # endif
158 #endif
160 #if !defined(__i386__) || _MSVCR_VER>=120
162 /*********************************************************************
163 * _chgsignf (MSVCRT.@)
165 float CDECL MSVCRT__chgsignf( float num )
167 union { float f; UINT32 i; } u = { num };
168 u.i ^= 0x80000000;
169 return u.f;
172 /*********************************************************************
173 * _copysignf (MSVCRT.@)
175 * Copied from musl: src/math/copysignf.c
177 float CDECL MSVCRT__copysignf( float x, float y )
179 union { float f; UINT32 i; } ux = { x }, uy = { y };
180 ux.i &= 0x7fffffff;
181 ux.i |= uy.i & 0x80000000;
182 return ux.f;
185 /*********************************************************************
186 * _nextafterf (MSVCRT.@)
188 float CDECL MSVCRT__nextafterf( float num, float next )
190 if (!isfinite(num) || !isfinite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
191 return nextafterf( num, next );
194 /*********************************************************************
195 * _logbf (MSVCRT.@)
197 float CDECL MSVCRT__logbf( float num )
199 float ret = logbf(num);
200 if (isnan(num)) return math_error(_DOMAIN, "_logbf", num, 0, ret);
201 if (!num) return math_error(_SING, "_logbf", num, 0, ret);
202 return ret;
205 #endif
207 #ifndef __i386__
209 /*********************************************************************
210 * _fpclassf (MSVCRT.@)
212 int CDECL MSVCRT__fpclassf( float num )
214 union { float f; UINT32 i; } u = { num };
215 int e = u.i >> 23 & 0xff;
216 int s = u.i >> 31;
218 switch (e)
220 case 0:
221 if (u.i << 1) return s ? MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
222 return s ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
223 case 0xff:
224 if (u.i << 9) return ((u.i >> 22) & 1) ? MSVCRT__FPCLASS_QNAN : MSVCRT__FPCLASS_SNAN;
225 return s ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
226 default:
227 return s ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
231 /*********************************************************************
232 * _finitef (MSVCRT.@)
234 int CDECL MSVCRT__finitef( float num )
236 union { float f; UINT32 i; } u = { num };
237 return (u.i & 0x7fffffff) < 0x7f800000;
240 /*********************************************************************
241 * _isnanf (MSVCRT.@)
243 int CDECL MSVCRT__isnanf( float num )
245 union { float f; UINT32 i; } u = { num };
246 return (u.i & 0x7fffffff) > 0x7f800000;
249 /*********************************************************************
250 * MSVCRT_acosf (MSVCRT.@)
252 * Copied from musl: src/math/acosf.c
254 static float acosf_R(float z)
256 static const float pS0 = 1.6666586697e-01,
257 pS1 = -4.2743422091e-02,
258 pS2 = -8.6563630030e-03,
259 qS1 = -7.0662963390e-01;
261 float p, q;
262 p = z * (pS0 + z * (pS1 + z * pS2));
263 q = 1.0f + z * qS1;
264 return p / q;
267 float CDECL MSVCRT_acosf( float x )
269 static const float pio2_hi = 1.5707962513e+00,
270 pio2_lo = 7.5497894159e-08;
272 float z, w, s, c, df;
273 unsigned int hx, ix;
275 hx = *(unsigned int*)&x;
276 ix = hx & 0x7fffffff;
277 /* |x| >= 1 or nan */
278 if (ix >= 0x3f800000) {
279 if (ix == 0x3f800000) {
280 if (hx >> 31)
281 return 2 * pio2_lo + 2 * pio2_hi + 7.5231638453e-37;
282 return 0;
284 if (isnan(x)) return x;
285 return math_error(_DOMAIN, "acosf", x, 0, 0 / (x - x));
287 /* |x| < 0.5 */
288 if (ix < 0x3f000000) {
289 if (ix <= 0x32800000) /* |x| < 2**-26 */
290 return pio2_lo + pio2_hi + 7.5231638453e-37;
291 return pio2_hi - (x - (pio2_lo - x * acosf_R(x * x)));
293 /* x < -0.5 */
294 if (hx >> 31) {
295 z = (1 + x) * 0.5f;
296 s = sqrtf(z);
297 w = acosf_R(z) * s - pio2_lo;
298 return 2 * (pio2_hi - (s + w));
300 /* x > 0.5 */
301 z = (1 - x) * 0.5f;
302 s = sqrtf(z);
303 hx = *(unsigned int*)&s & 0xfffff000;
304 df = *(float*)&hx;
305 c = (z - df * df) / (s + df);
306 w = acosf_R(z) * s + c;
307 return 2 * (df + w);
310 /*********************************************************************
311 * MSVCRT_asinf (MSVCRT.@)
313 * Copied from musl: src/math/asinf.c
315 static float asinf_R(float z)
317 /* coefficients for R(x^2) */
318 static const float pS0 = 1.6666586697e-01,
319 pS1 = -4.2743422091e-02,
320 pS2 = -8.6563630030e-03,
321 qS1 = -7.0662963390e-01;
323 float_t p, q;
324 p = z * (pS0 + z * (pS1 + z * pS2));
325 q = 1.0f + z * qS1;
326 return p / q;
329 float CDECL MSVCRT_asinf( float x )
331 static const double pio2 = 1.570796326794896558e+00;
333 double s;
334 float z;
335 unsigned int hx, ix;
337 hx = *(unsigned int*)&x;
338 ix = hx & 0x7fffffff;
339 if (ix >= 0x3f800000) { /* |x| >= 1 */
340 if (ix == 0x3f800000) /* |x| == 1 */
341 return x * pio2 + 7.5231638453e-37; /* asin(+-1) = +-pi/2 with inexact */
342 if (isnan(x)) return x;
343 return math_error(_DOMAIN, "asinf", x, 0, 0 / (x - x));
345 if (ix < 0x3f000000) { /* |x| < 0.5 */
346 /* if 0x1p-126 <= |x| < 0x1p-12, avoid raising underflow */
347 if (ix < 0x39800000 && ix >= 0x00800000)
348 return x;
349 return x + x * asinf_R(x * x);
351 /* 1 > |x| >= 0.5 */
352 z = (1 - fabsf(x)) * 0.5f;
353 s = sqrt(z);
354 x = pio2 - 2 * (s + s * asinf_R(z));
355 if (hx >> 31)
356 return -x;
357 return x;
360 /*********************************************************************
361 * MSVCRT_atanf (MSVCRT.@)
363 * Copied from musl: src/math/atanf.c
365 float CDECL MSVCRT_atanf( float x )
367 static const float atanhi[] = {
368 4.6364760399e-01,
369 7.8539812565e-01,
370 9.8279368877e-01,
371 1.5707962513e+00,
373 static const float atanlo[] = {
374 5.0121582440e-09,
375 3.7748947079e-08,
376 3.4473217170e-08,
377 7.5497894159e-08,
379 static const float aT[] = {
380 3.3333328366e-01,
381 -1.9999158382e-01,
382 1.4253635705e-01,
383 -1.0648017377e-01,
384 6.1687607318e-02,
387 float w, s1, s2, z;
388 unsigned int ix, sign;
389 int id;
391 #if _MSVCR_VER == 0
392 if (isnan(x)) return math_error(_DOMAIN, "atanf", x, 0, x);
393 #endif
395 ix = *(unsigned int*)&x;
396 sign = ix >> 31;
397 ix &= 0x7fffffff;
398 if (ix >= 0x4c800000) { /* if |x| >= 2**26 */
399 if (isnan(x))
400 return x;
401 z = atanhi[3] + 7.5231638453e-37;
402 return sign ? -z : z;
404 if (ix < 0x3ee00000) { /* |x| < 0.4375 */
405 if (ix < 0x39800000) { /* |x| < 2**-12 */
406 if (ix < 0x00800000)
407 /* raise underflow for subnormal x */
408 fp_barrierf(x*x);
409 return x;
411 id = -1;
412 } else {
413 x = fabsf(x);
414 if (ix < 0x3f980000) { /* |x| < 1.1875 */
415 if (ix < 0x3f300000) { /* 7/16 <= |x| < 11/16 */
416 id = 0;
417 x = (2.0f * x - 1.0f) / (2.0f + x);
418 } else { /* 11/16 <= |x| < 19/16 */
419 id = 1;
420 x = (x - 1.0f) / (x + 1.0f);
422 } else {
423 if (ix < 0x401c0000) { /* |x| < 2.4375 */
424 id = 2;
425 x = (x - 1.5f) / (1.0f + 1.5f * x);
426 } else { /* 2.4375 <= |x| < 2**26 */
427 id = 3;
428 x = -1.0f / x;
432 /* end of argument reduction */
433 z = x * x;
434 w = z * z;
435 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
436 s1 = z * (aT[0] + w * (aT[2] + w * aT[4]));
437 s2 = w * (aT[1] + w * aT[3]);
438 if (id < 0)
439 return x - x * (s1 + s2);
440 z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
441 return sign ? -z : z;
444 /*********************************************************************
445 * MSVCRT_atan2f (MSVCRT.@)
447 * Copied from musl: src/math/atan2f.c
449 float CDECL MSVCRT_atan2f( float y, float x )
451 static const float pi = 3.1415927410e+00,
452 pi_lo = -8.7422776573e-08;
454 float z;
455 unsigned int m, ix, iy;
457 if (isnan(x) || isnan(y))
458 return x + y;
459 ix = *(unsigned int*)&x;
460 iy = *(unsigned int*)&y;
461 if (ix == 0x3f800000) /* x=1.0 */
462 return atanf(y);
463 m = ((iy >> 31) & 1) | ((ix >> 30) & 2); /* 2*sign(x)+sign(y) */
464 ix &= 0x7fffffff;
465 iy &= 0x7fffffff;
467 /* when y = 0 */
468 if (iy == 0) {
469 switch (m) {
470 case 0:
471 case 1: return y; /* atan(+-0,+anything)=+-0 */
472 case 2: return pi; /* atan(+0,-anything) = pi */
473 case 3: return -pi; /* atan(-0,-anything) =-pi */
476 /* when x = 0 */
477 if (ix == 0)
478 return m & 1 ? -pi / 2 : pi / 2;
479 /* when x is INF */
480 if (ix == 0x7f800000) {
481 if (iy == 0x7f800000) {
482 switch (m) {
483 case 0: return pi / 4; /* atan(+INF,+INF) */
484 case 1: return -pi / 4; /* atan(-INF,+INF) */
485 case 2: return 3 * pi / 4; /*atan(+INF,-INF)*/
486 case 3: return -3 * pi / 4; /*atan(-INF,-INF)*/
488 } else {
489 switch (m) {
490 case 0: return 0.0f; /* atan(+...,+INF) */
491 case 1: return -0.0f; /* atan(-...,+INF) */
492 case 2: return pi; /* atan(+...,-INF) */
493 case 3: return -pi; /* atan(-...,-INF) */
497 /* |y/x| > 0x1p26 */
498 if (ix + (26 << 23) < iy || iy == 0x7f800000)
499 return m & 1 ? -pi / 2 : pi / 2;
501 /* z = atan(|y/x|) with correct underflow */
502 if ((m & 2) && iy + (26 << 23) < ix) /*|y/x| < 0x1p-26, x < 0 */
503 z = 0.0;
504 else
505 z = atanf(fabsf(y / x));
506 switch (m) {
507 case 0: return z; /* atan(+,+) */
508 case 1: return -z; /* atan(-,+) */
509 case 2: return pi - (z - pi_lo); /* atan(+,-) */
510 default: /* case 3 */
511 return (z - pi_lo) - pi; /* atan(-,-) */
515 /*********************************************************************
516 * MSVCRT_cosf (MSVCRT.@)
518 float CDECL MSVCRT_cosf( float x )
520 float ret = cosf(x);
521 if (!isfinite(x)) return math_error(_DOMAIN, "cosf", x, 0, ret);
522 return ret;
525 /*********************************************************************
526 * MSVCRT_coshf (MSVCRT.@)
528 float CDECL MSVCRT_coshf( float x )
530 float ret = coshf(x);
531 if (isnan(x)) return math_error(_DOMAIN, "coshf", x, 0, ret);
532 return ret;
535 /*********************************************************************
536 * MSVCRT_expf (MSVCRT.@)
538 float CDECL MSVCRT_expf( float x )
540 float ret = expf(x);
541 if (isnan(x)) return math_error(_DOMAIN, "expf", x, 0, ret);
542 if (isfinite(x) && !ret) return math_error(_UNDERFLOW, "expf", x, 0, ret);
543 if (isfinite(x) && !isfinite(ret)) return math_error(_OVERFLOW, "expf", x, 0, ret);
544 return ret;
547 /*********************************************************************
548 * MSVCRT_fmodf (MSVCRT.@)
550 float CDECL MSVCRT_fmodf( float x, float y )
552 float ret = fmodf(x, y);
553 if (!isfinite(x) || !isfinite(y)) return math_error(_DOMAIN, "fmodf", x, 0, ret);
554 return ret;
557 /*********************************************************************
558 * MSVCRT_logf (MSVCRT.@)
560 float CDECL MSVCRT_logf( float x )
562 float ret = logf(x);
563 if (x < 0.0) return math_error(_DOMAIN, "logf", x, 0, ret);
564 if (x == 0.0) return math_error(_SING, "logf", x, 0, ret);
565 return ret;
568 /*********************************************************************
569 * MSVCRT_log10f (MSVCRT.@)
571 float CDECL MSVCRT_log10f( float x )
573 float ret = log10f(x);
574 if (x < 0.0) return math_error(_DOMAIN, "log10f", x, 0, ret);
575 if (x == 0.0) return math_error(_SING, "log10f", x, 0, ret);
576 return ret;
579 /*********************************************************************
580 * MSVCRT_powf (MSVCRT.@)
582 float CDECL MSVCRT_powf( float x, float y )
584 float z = powf(x,y);
585 if (x < 0 && y != floorf(y)) return math_error(_DOMAIN, "powf", x, y, z);
586 if (!x && isfinite(y) && y < 0) return math_error(_SING, "powf", x, y, z);
587 if (isfinite(x) && isfinite(y) && !isfinite(z)) return math_error(_OVERFLOW, "powf", x, y, z);
588 if (x && isfinite(x) && isfinite(y) && !z) return math_error(_UNDERFLOW, "powf", x, y, z);
589 return z;
592 /*********************************************************************
593 * MSVCRT_sinf (MSVCRT.@)
595 float CDECL MSVCRT_sinf( float x )
597 float ret = sinf(x);
598 if (!isfinite(x)) return math_error(_DOMAIN, "sinf", x, 0, ret);
599 return ret;
602 /*********************************************************************
603 * MSVCRT_sinhf (MSVCRT.@)
605 float CDECL MSVCRT_sinhf( float x )
607 float ret = sinhf(x);
608 if (isnan(x)) return math_error(_DOMAIN, "sinhf", x, 0, ret);
609 return ret;
612 /*********************************************************************
613 * MSVCRT_sqrtf (MSVCRT.@)
615 * Copied from musl: src/math/sqrtf.c
617 float CDECL MSVCRT_sqrtf( float x )
619 static const float tiny = 1.0e-30;
621 float z;
622 int sign = 0x80000000;
623 int ix,s,q,m,t,i;
624 unsigned int r;
626 ix = *(int*)&x;
628 /* take care of Inf and NaN */
629 if ((ix & 0x7f800000) == 0x7f800000 && (ix == 0x7f800000 || ix & 0x7fffff))
630 return x;
632 /* take care of zero */
633 if (ix <= 0) {
634 if ((ix & ~sign) == 0)
635 return x; /* sqrt(+-0) = +-0 */
636 return math_error(_DOMAIN, "sqrtf", x, 0, (x - x) / (x - x)); /* sqrt(-ve) = sNaN */
638 /* normalize x */
639 m = ix >> 23;
640 if (m == 0) { /* subnormal x */
641 for (i = 0; (ix & 0x00800000) == 0; i++)
642 ix <<= 1;
643 m -= i - 1;
645 m -= 127; /* unbias exponent */
646 ix = (ix & 0x007fffff) | 0x00800000;
647 if (m & 1) /* odd m, double x to make it even */
648 ix += ix;
649 m >>= 1; /* m = [m/2] */
651 /* generate sqrt(x) bit by bit */
652 ix += ix;
653 q = s = 0; /* q = sqrt(x) */
654 r = 0x01000000; /* r = moving bit from right to left */
656 while (r != 0) {
657 t = s + r;
658 if (t <= ix) {
659 s = t + r;
660 ix -= t;
661 q += r;
663 ix += ix;
664 r >>= 1;
667 /* use floating add to find out rounding direction */
668 if (ix != 0) {
669 z = 1.0f - tiny; /* raise inexact flag */
670 if (z >= 1.0f) {
671 z = 1.0f + tiny;
672 if (z > 1.0f)
673 q += 2;
674 else
675 q += q & 1;
678 ix = (q >> 1) + 0x3f000000;
679 r = ix + ((unsigned int)m << 23);
680 z = *(float*)&r;
681 return z;
684 /*********************************************************************
685 * MSVCRT_tanf (MSVCRT.@)
687 float CDECL MSVCRT_tanf( float x )
689 float ret = tanf(x);
690 if (!isfinite(x)) return math_error(_DOMAIN, "tanf", x, 0, ret);
691 return ret;
694 /*********************************************************************
695 * MSVCRT_tanhf (MSVCRT.@)
697 float CDECL MSVCRT_tanhf( float x )
699 float ret = tanhf(x);
700 if (!isfinite(x)) return math_error(_DOMAIN, "tanhf", x, 0, ret);
701 return ret;
704 /*********************************************************************
705 * ceilf (MSVCRT.@)
707 float CDECL MSVCRT_ceilf( float x )
709 return ceilf(x);
712 /*********************************************************************
713 * fabsf (MSVCRT.@)
715 * Copied from musl: src/math/fabsf.c
717 float CDECL MSVCRT_fabsf( float x )
719 union { float f; UINT32 i; } u = { x };
720 u.i &= 0x7fffffff;
721 return u.f;
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 * Copied from musl: src/math/fabsf.c
1614 double CDECL MSVCRT_fabs( double x )
1616 union { double f; UINT64 i; } u = { x };
1617 u.i &= ~0ull >> 1;
1618 return u.f;
1621 /*********************************************************************
1622 * frexp (MSVCRT.@)
1624 double CDECL MSVCRT_frexp( double x, int *exp )
1626 return frexp( x, exp );
1629 /*********************************************************************
1630 * modf (MSVCRT.@)
1632 double CDECL MSVCRT_modf( double x, double *iptr )
1634 return modf( x, iptr );
1637 /**********************************************************************
1638 * _statusfp2 (MSVCRT.@)
1640 * Not exported by native msvcrt, added in msvcr80.
1642 #if defined(__i386__) || defined(__x86_64__)
1643 void CDECL _statusfp2( unsigned int *x86_sw, unsigned int *sse2_sw )
1645 #ifdef __GNUC__
1646 unsigned int flags;
1647 unsigned long fpword;
1649 if (x86_sw)
1651 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) );
1652 flags = 0;
1653 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
1654 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
1655 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
1656 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
1657 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
1658 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
1659 *x86_sw = flags;
1662 if (!sse2_sw) return;
1664 if (sse2_supported)
1666 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1667 flags = 0;
1668 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
1669 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
1670 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
1671 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
1672 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
1673 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
1674 *sse2_sw = flags;
1676 else *sse2_sw = 0;
1677 #else
1678 FIXME( "not implemented\n" );
1679 #endif
1681 #endif
1683 /**********************************************************************
1684 * _statusfp (MSVCRT.@)
1686 unsigned int CDECL _statusfp(void)
1688 unsigned int flags = 0;
1689 #if defined(__i386__) || defined(__x86_64__)
1690 unsigned int x86_sw, sse2_sw;
1692 _statusfp2( &x86_sw, &sse2_sw );
1693 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
1694 flags = x86_sw | sse2_sw;
1695 #elif defined(__aarch64__)
1696 unsigned long fpsr;
1698 __asm__ __volatile__( "mrs %0, fpsr" : "=r" (fpsr) );
1699 if (fpsr & 0x1) flags |= MSVCRT__SW_INVALID;
1700 if (fpsr & 0x2) flags |= MSVCRT__SW_ZERODIVIDE;
1701 if (fpsr & 0x4) flags |= MSVCRT__SW_OVERFLOW;
1702 if (fpsr & 0x8) flags |= MSVCRT__SW_UNDERFLOW;
1703 if (fpsr & 0x10) flags |= MSVCRT__SW_INEXACT;
1704 if (fpsr & 0x80) flags |= MSVCRT__SW_DENORMAL;
1705 #else
1706 FIXME( "not implemented\n" );
1707 #endif
1708 return flags;
1711 /*********************************************************************
1712 * _clearfp (MSVCRT.@)
1714 unsigned int CDECL _clearfp(void)
1716 unsigned int flags = 0;
1717 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1718 unsigned long fpword;
1720 __asm__ __volatile__( "fnstsw %0; fnclex" : "=m" (fpword) );
1721 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
1722 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
1723 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
1724 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
1725 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
1726 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
1728 if (sse2_supported)
1730 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1731 if (fpword & 0x1) flags |= MSVCRT__SW_INVALID;
1732 if (fpword & 0x2) flags |= MSVCRT__SW_DENORMAL;
1733 if (fpword & 0x4) flags |= MSVCRT__SW_ZERODIVIDE;
1734 if (fpword & 0x8) flags |= MSVCRT__SW_OVERFLOW;
1735 if (fpword & 0x10) flags |= MSVCRT__SW_UNDERFLOW;
1736 if (fpword & 0x20) flags |= MSVCRT__SW_INEXACT;
1737 fpword &= ~0x3f;
1738 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1740 #elif defined(__aarch64__)
1741 unsigned long fpsr;
1743 __asm__ __volatile__( "mrs %0, fpsr" : "=r" (fpsr) );
1744 if (fpsr & 0x1) flags |= MSVCRT__SW_INVALID;
1745 if (fpsr & 0x2) flags |= MSVCRT__SW_ZERODIVIDE;
1746 if (fpsr & 0x4) flags |= MSVCRT__SW_OVERFLOW;
1747 if (fpsr & 0x8) flags |= MSVCRT__SW_UNDERFLOW;
1748 if (fpsr & 0x10) flags |= MSVCRT__SW_INEXACT;
1749 if (fpsr & 0x80) flags |= MSVCRT__SW_DENORMAL;
1750 fpsr &= ~0x9f;
1751 __asm__ __volatile__( "msr fpsr, %0" :: "r" (fpsr) );
1752 #else
1753 FIXME( "not implemented\n" );
1754 #endif
1755 return flags;
1758 /*********************************************************************
1759 * __fpecode (MSVCRT.@)
1761 int * CDECL __fpecode(void)
1763 return &msvcrt_get_thread_data()->fpecode;
1766 /*********************************************************************
1767 * ldexp (MSVCRT.@)
1769 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
1771 double z = ldexp(num,exp);
1773 if (isfinite(num) && !isfinite(z))
1774 return math_error(_OVERFLOW, "ldexp", num, exp, z);
1775 if (num && isfinite(num) && !z)
1776 return math_error(_UNDERFLOW, "ldexp", num, exp, z);
1777 if (z == 0 && signbit(z))
1778 z = 0.0; /* Convert -0 -> +0 */
1779 return z;
1782 /*********************************************************************
1783 * _cabs (MSVCRT.@)
1785 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
1787 return sqrt(num.x * num.x + num.y * num.y);
1790 /*********************************************************************
1791 * _chgsign (MSVCRT.@)
1793 double CDECL MSVCRT__chgsign(double num)
1795 union { double f; UINT64 i; } u = { num };
1796 u.i ^= 1ull << 63;
1797 return u.f;
1800 /*********************************************************************
1801 * __control87_2 (MSVCR80.@)
1803 * Not exported by native msvcrt, added in msvcr80.
1805 #ifdef __i386__
1806 int CDECL __control87_2( unsigned int newval, unsigned int mask,
1807 unsigned int *x86_cw, unsigned int *sse2_cw )
1809 #ifdef __GNUC__
1810 unsigned long fpword;
1811 unsigned int flags;
1812 unsigned int old_flags;
1814 if (x86_cw)
1816 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) );
1818 /* Convert into mask constants */
1819 flags = 0;
1820 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
1821 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
1822 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
1823 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
1824 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
1825 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
1826 switch (fpword & 0xc00)
1828 case 0xc00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1829 case 0x800: flags |= MSVCRT__RC_UP; break;
1830 case 0x400: flags |= MSVCRT__RC_DOWN; break;
1832 switch (fpword & 0x300)
1834 case 0x0: flags |= MSVCRT__PC_24; break;
1835 case 0x200: flags |= MSVCRT__PC_53; break;
1836 case 0x300: flags |= MSVCRT__PC_64; break;
1838 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
1840 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1841 if (mask)
1843 flags = (flags & ~mask) | (newval & mask);
1845 /* Convert (masked) value back to fp word */
1846 fpword = 0;
1847 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
1848 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
1849 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
1850 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
1851 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
1852 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
1853 switch (flags & MSVCRT__MCW_RC)
1855 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xc00; break;
1856 case MSVCRT__RC_UP: fpword |= 0x800; break;
1857 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
1859 switch (flags & MSVCRT__MCW_PC)
1861 case MSVCRT__PC_64: fpword |= 0x300; break;
1862 case MSVCRT__PC_53: fpword |= 0x200; break;
1863 case MSVCRT__PC_24: fpword |= 0x0; break;
1865 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
1867 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
1869 *x86_cw = flags;
1872 if (!sse2_cw) return 1;
1874 if (sse2_supported)
1876 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1878 /* Convert into mask constants */
1879 flags = 0;
1880 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1881 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1882 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1883 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1884 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1885 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1886 switch (fpword & 0x6000)
1888 case 0x6000: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
1889 case 0x4000: flags |= MSVCRT__RC_UP; break;
1890 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1892 switch (fpword & 0x8040)
1894 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1895 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1896 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1899 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags, newval, mask );
1900 if (mask)
1902 old_flags = flags;
1903 mask &= MSVCRT__MCW_EM | MSVCRT__MCW_RC | MSVCRT__MCW_DN;
1904 flags = (flags & ~mask) | (newval & mask);
1906 if (flags != old_flags)
1908 /* Convert (masked) value back to fp word */
1909 fpword = 0;
1910 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1911 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1912 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1913 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1914 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1915 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1916 switch (flags & MSVCRT__MCW_RC)
1918 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0x6000; break;
1919 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1920 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1922 switch (flags & MSVCRT__MCW_DN)
1924 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
1925 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
1926 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
1928 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
1931 *sse2_cw = flags;
1933 else *sse2_cw = 0;
1935 return 1;
1936 #else
1937 FIXME( "not implemented\n" );
1938 return 0;
1939 #endif
1941 #endif
1943 /*********************************************************************
1944 * _control87 (MSVCRT.@)
1946 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
1948 unsigned int flags = 0;
1949 #ifdef __i386__
1950 unsigned int sse2_cw;
1952 __control87_2( newval, mask, &flags, &sse2_cw );
1954 if ((flags ^ sse2_cw) & (MSVCRT__MCW_EM | MSVCRT__MCW_RC)) flags |= MSVCRT__EM_AMBIGUOUS;
1955 flags |= sse2_cw;
1956 #elif defined(__x86_64__)
1957 unsigned long fpword;
1958 unsigned int old_flags;
1960 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
1961 if (fpword & 0x80) flags |= MSVCRT__EM_INVALID;
1962 if (fpword & 0x100) flags |= MSVCRT__EM_DENORMAL;
1963 if (fpword & 0x200) flags |= MSVCRT__EM_ZERODIVIDE;
1964 if (fpword & 0x400) flags |= MSVCRT__EM_OVERFLOW;
1965 if (fpword & 0x800) flags |= MSVCRT__EM_UNDERFLOW;
1966 if (fpword & 0x1000) flags |= MSVCRT__EM_INEXACT;
1967 switch (fpword & 0x6000)
1969 case 0x6000: flags |= MSVCRT__RC_CHOP; break;
1970 case 0x4000: flags |= MSVCRT__RC_UP; break;
1971 case 0x2000: flags |= MSVCRT__RC_DOWN; break;
1973 switch (fpword & 0x8040)
1975 case 0x0040: flags |= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS; break;
1976 case 0x8000: flags |= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS; break;
1977 case 0x8040: flags |= MSVCRT__DN_FLUSH; break;
1979 old_flags = flags;
1980 mask &= MSVCRT__MCW_EM | MSVCRT__MCW_RC | MSVCRT__MCW_DN;
1981 flags = (flags & ~mask) | (newval & mask);
1982 if (flags != old_flags)
1984 fpword = 0;
1985 if (flags & MSVCRT__EM_INVALID) fpword |= 0x80;
1986 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x100;
1987 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
1988 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
1989 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
1990 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x1000;
1991 switch (flags & MSVCRT__MCW_RC)
1993 case MSVCRT__RC_CHOP: fpword |= 0x6000; break;
1994 case MSVCRT__RC_UP: fpword |= 0x4000; break;
1995 case MSVCRT__RC_DOWN: fpword |= 0x2000; break;
1997 switch (flags & MSVCRT__MCW_DN)
1999 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS: fpword |= 0x0040; break;
2000 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS: fpword |= 0x8000; break;
2001 case MSVCRT__DN_FLUSH: fpword |= 0x8040; break;
2003 __asm__ __volatile__( "ldmxcsr %0" :: "m" (fpword) );
2005 #elif defined(__aarch64__)
2006 unsigned long fpcr;
2008 __asm__ __volatile__( "mrs %0, fpcr" : "=r" (fpcr) );
2009 if (!(fpcr & 0x100)) flags |= MSVCRT__EM_INVALID;
2010 if (!(fpcr & 0x200)) flags |= MSVCRT__EM_ZERODIVIDE;
2011 if (!(fpcr & 0x400)) flags |= MSVCRT__EM_OVERFLOW;
2012 if (!(fpcr & 0x800)) flags |= MSVCRT__EM_UNDERFLOW;
2013 if (!(fpcr & 0x1000)) flags |= MSVCRT__EM_INEXACT;
2014 if (!(fpcr & 0x8000)) flags |= MSVCRT__EM_DENORMAL;
2015 switch (fpcr & 0xc00000)
2017 case 0x400000: flags |= MSVCRT__RC_UP; break;
2018 case 0x800000: flags |= MSVCRT__RC_DOWN; break;
2019 case 0xc00000: flags |= MSVCRT__RC_CHOP; break;
2021 flags = (flags & ~mask) | (newval & mask);
2022 fpcr &= ~0xc09f00ul;
2023 if (!(flags & MSVCRT__EM_INVALID)) fpcr |= 0x100;
2024 if (!(flags & MSVCRT__EM_ZERODIVIDE)) fpcr |= 0x200;
2025 if (!(flags & MSVCRT__EM_OVERFLOW)) fpcr |= 0x400;
2026 if (!(flags & MSVCRT__EM_UNDERFLOW)) fpcr |= 0x800;
2027 if (!(flags & MSVCRT__EM_INEXACT)) fpcr |= 0x1000;
2028 if (!(flags & MSVCRT__EM_DENORMAL)) fpcr |= 0x8000;
2029 switch (flags & MSVCRT__MCW_RC)
2031 case MSVCRT__RC_CHOP: fpcr |= 0xc00000; break;
2032 case MSVCRT__RC_UP: fpcr |= 0x400000; break;
2033 case MSVCRT__RC_DOWN: fpcr |= 0x800000; break;
2035 __asm__ __volatile__( "msr fpcr, %0" :: "r" (fpcr) );
2036 #else
2037 FIXME( "not implemented\n" );
2038 #endif
2039 return flags;
2042 /*********************************************************************
2043 * _controlfp (MSVCRT.@)
2045 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
2047 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
2050 /*********************************************************************
2051 * _set_controlfp (MSVCRT.@)
2053 void CDECL _set_controlfp( unsigned int newval, unsigned int mask )
2055 _controlfp( newval, mask );
2058 /*********************************************************************
2059 * _controlfp_s (MSVCRT.@)
2061 int CDECL _controlfp_s(unsigned int *cur, unsigned int newval, unsigned int mask)
2063 static const unsigned int all_flags = (MSVCRT__MCW_EM | MSVCRT__MCW_IC | MSVCRT__MCW_RC |
2064 MSVCRT__MCW_PC | MSVCRT__MCW_DN);
2065 unsigned int val;
2067 if (!MSVCRT_CHECK_PMT( !(newval & mask & ~all_flags) ))
2069 if (cur) *cur = _controlfp( 0, 0 ); /* retrieve it anyway */
2070 return MSVCRT_EINVAL;
2072 val = _controlfp( newval, mask );
2073 if (cur) *cur = val;
2074 return 0;
2077 #if _MSVCR_VER>=120
2078 /*********************************************************************
2079 * fegetenv (MSVCR120.@)
2081 int CDECL MSVCRT_fegetenv(MSVCRT_fenv_t *env)
2083 env->control = _controlfp(0, 0) & (MSVCRT__EM_INEXACT | MSVCRT__EM_UNDERFLOW |
2084 MSVCRT__EM_OVERFLOW | MSVCRT__EM_ZERODIVIDE | MSVCRT__EM_INVALID);
2085 env->status = _statusfp();
2086 return 0;
2088 #endif
2090 #if _MSVCR_VER>=140
2091 /*********************************************************************
2092 * __fpe_flt_rounds (UCRTBASE.@)
2094 int CDECL __fpe_flt_rounds(void)
2096 unsigned int fpc = _controlfp(0, 0) & MSVCRT__RC_CHOP;
2098 TRACE("()\n");
2100 switch(fpc) {
2101 case MSVCRT__RC_CHOP: return 0;
2102 case MSVCRT__RC_NEAR: return 1;
2103 case MSVCRT__RC_UP: return 2;
2104 default: return 3;
2107 #endif
2109 #if _MSVCR_VER>=120
2111 /*********************************************************************
2112 * fegetround (MSVCR120.@)
2114 int CDECL MSVCRT_fegetround(void)
2116 return _controlfp(0, 0) & MSVCRT__RC_CHOP;
2119 /*********************************************************************
2120 * fesetround (MSVCR120.@)
2122 int CDECL MSVCRT_fesetround(int round_mode)
2124 if (round_mode & (~MSVCRT__RC_CHOP))
2125 return 1;
2126 _controlfp(round_mode, MSVCRT__RC_CHOP);
2127 return 0;
2130 #endif /* _MSVCR_VER>=120 */
2132 /*********************************************************************
2133 * _copysign (MSVCRT.@)
2135 * Copied from musl: src/math/copysign.c
2137 double CDECL MSVCRT__copysign( double x, double y )
2139 union { double f; UINT64 i; } ux = { x }, uy = { y };
2140 ux.i &= ~0ull >> 1;
2141 ux.i |= uy.i & 1ull << 63;
2142 return ux.f;
2145 /*********************************************************************
2146 * _finite (MSVCRT.@)
2148 int CDECL MSVCRT__finite(double num)
2150 union { double f; UINT64 i; } u = { num };
2151 return (u.i & ~0ull >> 1) < 0x7ffull << 52;
2154 /*********************************************************************
2155 * _fpreset (MSVCRT.@)
2157 void CDECL _fpreset(void)
2159 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2160 const unsigned int x86_cw = 0x27f;
2161 __asm__ __volatile__( "fninit; fldcw %0" : : "m" (x86_cw) );
2162 if (sse2_supported)
2164 const unsigned long sse2_cw = 0x1f80;
2165 __asm__ __volatile__( "ldmxcsr %0" : : "m" (sse2_cw) );
2167 #else
2168 FIXME( "not implemented\n" );
2169 #endif
2172 #if _MSVCR_VER>=120
2173 /*********************************************************************
2174 * fesetenv (MSVCR120.@)
2176 int CDECL MSVCRT_fesetenv(const MSVCRT_fenv_t *env)
2178 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2179 struct {
2180 WORD control_word;
2181 WORD unused1;
2182 WORD status_word;
2183 WORD unused2;
2184 WORD tag_word;
2185 WORD unused3;
2186 DWORD instruction_pointer;
2187 WORD code_segment;
2188 WORD unused4;
2189 DWORD operand_addr;
2190 WORD data_segment;
2191 WORD unused5;
2192 } fenv;
2194 TRACE( "(%p)\n", env );
2196 if (!env->control && !env->status) {
2197 _fpreset();
2198 return 0;
2201 __asm__ __volatile__( "fnstenv %0" : "=m" (fenv) );
2203 fenv.control_word &= ~0x3d;
2204 if (env->control & MSVCRT__EM_INVALID) fenv.control_word |= 0x1;
2205 if (env->control & MSVCRT__EM_ZERODIVIDE) fenv.control_word |= 0x4;
2206 if (env->control & MSVCRT__EM_OVERFLOW) fenv.control_word |= 0x8;
2207 if (env->control & MSVCRT__EM_UNDERFLOW) fenv.control_word |= 0x10;
2208 if (env->control & MSVCRT__EM_INEXACT) fenv.control_word |= 0x20;
2210 fenv.status_word &= ~0x3d;
2211 if (env->status & MSVCRT__SW_INVALID) fenv.status_word |= 0x1;
2212 if (env->status & MSVCRT__SW_ZERODIVIDE) fenv.status_word |= 0x4;
2213 if (env->status & MSVCRT__SW_OVERFLOW) fenv.status_word |= 0x8;
2214 if (env->status & MSVCRT__SW_UNDERFLOW) fenv.status_word |= 0x10;
2215 if (env->status & MSVCRT__SW_INEXACT) fenv.status_word |= 0x20;
2217 __asm__ __volatile__( "fldenv %0" : : "m" (fenv) : "st", "st(1)",
2218 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
2220 if (sse2_supported)
2222 DWORD fpword;
2224 __asm__ __volatile__( "stmxcsr %0" : "=m" (fpword) );
2225 fpword &= ~0x1e80;
2226 if (env->control & MSVCRT__EM_INVALID) fpword |= 0x80;
2227 if (env->control & MSVCRT__EM_ZERODIVIDE) fpword |= 0x200;
2228 if (env->control & MSVCRT__EM_OVERFLOW) fpword |= 0x400;
2229 if (env->control & MSVCRT__EM_UNDERFLOW) fpword |= 0x800;
2230 if (env->control & MSVCRT__EM_INEXACT) fpword |= 0x1000;
2231 __asm__ __volatile__( "ldmxcsr %0" : : "m" (fpword) );
2234 return 0;
2235 #else
2236 FIXME( "not implemented\n" );
2237 #endif
2238 return 1;
2240 #endif
2242 /*********************************************************************
2243 * _isnan (MSVCRT.@)
2245 int CDECL MSVCRT__isnan(double num)
2247 union { double f; UINT64 i; } u = { num };
2248 return (u.i & ~0ull >> 1) > 0x7ffull << 52;
2251 /*********************************************************************
2252 * _j0 (MSVCRT.@)
2254 double CDECL MSVCRT__j0(double num)
2256 /* FIXME: errno handling */
2257 #ifdef HAVE_J0
2258 return j0(num);
2259 #else
2260 FIXME("not implemented\n");
2261 return 0;
2262 #endif
2265 /*********************************************************************
2266 * _j1 (MSVCRT.@)
2268 double CDECL MSVCRT__j1(double num)
2270 /* FIXME: errno handling */
2271 #ifdef HAVE_J1
2272 return j1(num);
2273 #else
2274 FIXME("not implemented\n");
2275 return 0;
2276 #endif
2279 /*********************************************************************
2280 * _jn (MSVCRT.@)
2282 double CDECL MSVCRT__jn(int n, double num)
2284 /* FIXME: errno handling */
2285 #ifdef HAVE_JN
2286 return jn(n, num);
2287 #else
2288 FIXME("not implemented\n");
2289 return 0;
2290 #endif
2293 /*********************************************************************
2294 * _y0 (MSVCRT.@)
2296 double CDECL MSVCRT__y0(double num)
2298 double retval;
2299 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
2300 #ifdef HAVE_Y0
2301 retval = y0(num);
2302 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
2304 *MSVCRT__errno() = MSVCRT_EDOM;
2305 retval = NAN;
2307 #else
2308 FIXME("not implemented\n");
2309 retval = 0;
2310 #endif
2311 return retval;
2314 /*********************************************************************
2315 * _y1 (MSVCRT.@)
2317 double CDECL MSVCRT__y1(double num)
2319 double retval;
2320 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
2321 #ifdef HAVE_Y1
2322 retval = y1(num);
2323 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
2325 *MSVCRT__errno() = MSVCRT_EDOM;
2326 retval = NAN;
2328 #else
2329 FIXME("not implemented\n");
2330 retval = 0;
2331 #endif
2332 return retval;
2335 /*********************************************************************
2336 * _yn (MSVCRT.@)
2338 double CDECL MSVCRT__yn(int order, double num)
2340 double retval;
2341 if (!isfinite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
2342 #ifdef HAVE_YN
2343 retval = yn(order,num);
2344 if (MSVCRT__fpclass(retval) == MSVCRT__FPCLASS_NINF)
2346 *MSVCRT__errno() = MSVCRT_EDOM;
2347 retval = NAN;
2349 #else
2350 FIXME("not implemented\n");
2351 retval = 0;
2352 #endif
2353 return retval;
2356 #if _MSVCR_VER>=120
2358 /*********************************************************************
2359 * _nearbyint (MSVCR120.@)
2361 double CDECL MSVCRT_nearbyint(double num)
2363 #ifdef HAVE_NEARBYINT
2364 return nearbyint(num);
2365 #else
2366 return num >= 0 ? floor(num + 0.5) : ceil(num - 0.5);
2367 #endif
2370 /*********************************************************************
2371 * _nearbyintf (MSVCR120.@)
2373 float CDECL MSVCRT_nearbyintf(float num)
2375 #ifdef HAVE_NEARBYINTF
2376 return nearbyintf(num);
2377 #else
2378 return MSVCRT_nearbyint(num);
2379 #endif
2382 /*********************************************************************
2383 * nexttoward (MSVCR120.@)
2385 double CDECL MSVCRT_nexttoward(double num, double next)
2387 #ifdef HAVE_NEXTTOWARD
2388 double ret = nexttoward(num, next);
2389 if (!(MSVCRT__fpclass(ret) & (MSVCRT__FPCLASS_PN | MSVCRT__FPCLASS_NN
2390 | MSVCRT__FPCLASS_SNAN | MSVCRT__FPCLASS_QNAN)) && !isinf(num))
2392 *MSVCRT__errno() = MSVCRT_ERANGE;
2394 return ret;
2395 #else
2396 FIXME("not implemented\n");
2397 return 0;
2398 #endif
2401 /*********************************************************************
2402 * nexttowardf (MSVCR120.@)
2404 float CDECL MSVCRT_nexttowardf(float num, double next)
2406 #ifdef HAVE_NEXTTOWARDF
2407 float ret = nexttowardf(num, next);
2408 if (!(MSVCRT__fpclass(ret) & (MSVCRT__FPCLASS_PN | MSVCRT__FPCLASS_NN
2409 | MSVCRT__FPCLASS_SNAN | MSVCRT__FPCLASS_QNAN)) && !isinf(num))
2411 *MSVCRT__errno() = MSVCRT_ERANGE;
2413 return ret;
2414 #else
2415 FIXME("not implemented\n");
2416 return 0;
2417 #endif
2420 #endif /* _MSVCR_VER>=120 */
2422 /*********************************************************************
2423 * _nextafter (MSVCRT.@)
2425 double CDECL MSVCRT__nextafter(double num, double next)
2427 double retval;
2428 if (!isfinite(num) || !isfinite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
2429 retval = nextafter(num,next);
2430 return retval;
2433 /*********************************************************************
2434 * _ecvt (MSVCRT.@)
2436 char * CDECL MSVCRT__ecvt( double number, int ndigits, int *decpt, int *sign )
2438 int prec, len;
2439 thread_data_t *data = msvcrt_get_thread_data();
2440 /* FIXME: check better for overflow (native supports over 300 chars) */
2441 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
2442 * 4 for exponent and one for
2443 * terminating '\0' */
2444 if (!data->efcvt_buffer)
2445 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
2447 if( number < 0) {
2448 *sign = TRUE;
2449 number = -number;
2450 } else
2451 *sign = FALSE;
2452 /* handle cases with zero ndigits or less */
2453 prec = ndigits;
2454 if( prec < 1) prec = 2;
2455 len = MSVCRT__snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
2456 /* take the decimal "point away */
2457 if( prec != 1)
2458 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
2459 /* take the exponential "e" out */
2460 data->efcvt_buffer[ prec] = '\0';
2461 /* read the exponent */
2462 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
2463 (*decpt)++;
2464 /* adjust for some border cases */
2465 if( data->efcvt_buffer[0] == '0')/* value is zero */
2466 *decpt = 0;
2467 /* handle cases with zero ndigits or less */
2468 if( ndigits < 1){
2469 if( data->efcvt_buffer[ 0] >= '5')
2470 (*decpt)++;
2471 data->efcvt_buffer[ 0] = '\0';
2473 TRACE("out=\"%s\"\n",data->efcvt_buffer);
2474 return data->efcvt_buffer;
2477 /*********************************************************************
2478 * _ecvt_s (MSVCRT.@)
2480 int CDECL MSVCRT__ecvt_s( char *buffer, MSVCRT_size_t length, double number, int ndigits, int *decpt, int *sign )
2482 int prec, len;
2483 char *result;
2484 const char infret[] = "1#INF";
2486 if (!MSVCRT_CHECK_PMT(buffer != NULL)) return MSVCRT_EINVAL;
2487 if (!MSVCRT_CHECK_PMT(decpt != NULL)) return MSVCRT_EINVAL;
2488 if (!MSVCRT_CHECK_PMT(sign != NULL)) return MSVCRT_EINVAL;
2489 if (!MSVCRT_CHECK_PMT_ERR( length > 2, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
2490 if (!MSVCRT_CHECK_PMT_ERR(ndigits < (int)length - 1, MSVCRT_ERANGE )) return MSVCRT_ERANGE;
2492 /* special case - inf */
2493 if(number == HUGE_VAL || number == -HUGE_VAL)
2495 memset(buffer, '0', ndigits);
2496 memcpy(buffer, infret, min(ndigits, sizeof(infret) - 1 ) );
2497 buffer[ndigits] = '\0';
2498 (*decpt) = 1;
2499 if(number == -HUGE_VAL)
2500 (*sign) = 1;
2501 else
2502 (*sign) = 0;
2503 return 0;
2505 /* handle cases with zero ndigits or less */
2506 prec = ndigits;
2507 if( prec < 1) prec = 2;
2508 result = MSVCRT_malloc(prec + 7);
2510 if( number < 0) {
2511 *sign = TRUE;
2512 number = -number;
2513 } else
2514 *sign = FALSE;
2515 len = MSVCRT__snprintf(result, prec + 7, "%.*le", prec - 1, number);
2516 /* take the decimal "point away */
2517 if( prec != 1)
2518 memmove( result + 1, result + 2, len - 1 );
2519 /* take the exponential "e" out */
2520 result[ prec] = '\0';
2521 /* read the exponent */
2522 sscanf( result + prec + 1, "%d", decpt);
2523 (*decpt)++;
2524 /* adjust for some border cases */
2525 if( result[0] == '0')/* value is zero */
2526 *decpt = 0;
2527 /* handle cases with zero ndigits or less */
2528 if( ndigits < 1){
2529 if( result[ 0] >= '5')
2530 (*decpt)++;
2531 result[ 0] = '\0';
2533 memcpy( buffer, result, max(ndigits + 1, 1) );
2534 MSVCRT_free( result );
2535 return 0;
2538 /***********************************************************************
2539 * _fcvt (MSVCRT.@)
2541 char * CDECL MSVCRT__fcvt( double number, int ndigits, int *decpt, int *sign )
2543 thread_data_t *data = msvcrt_get_thread_data();
2544 int stop, dec1, dec2;
2545 char *ptr1, *ptr2, *first;
2546 char buf[80]; /* ought to be enough */
2547 char decimal_separator = get_locinfo()->lconv->decimal_point[0];
2549 if (!data->efcvt_buffer)
2550 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
2552 if (number < 0)
2554 *sign = 1;
2555 number = -number;
2556 } else *sign = 0;
2558 stop = MSVCRT__snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
2559 ptr1 = buf;
2560 ptr2 = data->efcvt_buffer;
2561 first = NULL;
2562 dec1 = 0;
2563 dec2 = 0;
2565 /* For numbers below the requested resolution, work out where
2566 the decimal point will be rather than finding it in the string */
2567 if (number < 1.0 && number > 0.0) {
2568 dec2 = log10(number + 1e-10);
2569 if (-dec2 <= ndigits) dec2 = 0;
2572 /* If requested digits is zero or less, we will need to truncate
2573 * the returned string */
2574 if (ndigits < 1) {
2575 stop += ndigits;
2578 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
2579 while (*ptr1 != '\0' && *ptr1 != decimal_separator) {
2580 if (!first) first = ptr2;
2581 if ((ptr1 - buf) < stop) {
2582 *ptr2++ = *ptr1++;
2583 } else {
2584 ptr1++;
2586 dec1++;
2589 if (ndigits > 0) {
2590 ptr1++;
2591 if (!first) {
2592 while (*ptr1 == '0') { /* Process leading zeroes */
2593 *ptr2++ = *ptr1++;
2594 dec1--;
2597 while (*ptr1 != '\0') {
2598 if (!first) first = ptr2;
2599 *ptr2++ = *ptr1++;
2603 *ptr2 = '\0';
2605 /* We never found a non-zero digit, then our number is either
2606 * smaller than the requested precision, or 0.0 */
2607 if (!first) {
2608 if (number > 0.0) {
2609 first = ptr2;
2610 } else {
2611 first = data->efcvt_buffer;
2612 dec1 = 0;
2616 *decpt = dec2 ? dec2 : dec1;
2617 return first;
2620 /***********************************************************************
2621 * _fcvt_s (MSVCRT.@)
2623 int CDECL MSVCRT__fcvt_s(char* outbuffer, MSVCRT_size_t size, double number, int ndigits, int *decpt, int *sign)
2625 int stop, dec1, dec2;
2626 char *ptr1, *ptr2, *first;
2627 char buf[80]; /* ought to be enough */
2628 char decimal_separator = get_locinfo()->lconv->decimal_point[0];
2630 if (!outbuffer || !decpt || !sign || size == 0)
2632 *MSVCRT__errno() = MSVCRT_EINVAL;
2633 return MSVCRT_EINVAL;
2636 if (number < 0)
2638 *sign = 1;
2639 number = -number;
2640 } else *sign = 0;
2642 stop = MSVCRT__snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
2643 ptr1 = buf;
2644 ptr2 = outbuffer;
2645 first = NULL;
2646 dec1 = 0;
2647 dec2 = 0;
2649 /* For numbers below the requested resolution, work out where
2650 the decimal point will be rather than finding it in the string */
2651 if (number < 1.0 && number > 0.0) {
2652 dec2 = log10(number + 1e-10);
2653 if (-dec2 <= ndigits) dec2 = 0;
2656 /* If requested digits is zero or less, we will need to truncate
2657 * the returned string */
2658 if (ndigits < 1) {
2659 stop += ndigits;
2662 while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
2663 while (*ptr1 != '\0' && *ptr1 != decimal_separator) {
2664 if (!first) first = ptr2;
2665 if ((ptr1 - buf) < stop) {
2666 if (size > 1) {
2667 *ptr2++ = *ptr1++;
2668 size--;
2670 } else {
2671 ptr1++;
2673 dec1++;
2676 if (ndigits > 0) {
2677 ptr1++;
2678 if (!first) {
2679 while (*ptr1 == '0') { /* Process leading zeroes */
2680 if (number == 0.0 && size > 1) {
2681 *ptr2++ = '0';
2682 size--;
2684 ptr1++;
2685 dec1--;
2688 while (*ptr1 != '\0') {
2689 if (!first) first = ptr2;
2690 if (size > 1) {
2691 *ptr2++ = *ptr1++;
2692 size--;
2697 *ptr2 = '\0';
2699 /* We never found a non-zero digit, then our number is either
2700 * smaller than the requested precision, or 0.0 */
2701 if (!first && (number <= 0.0))
2702 dec1 = 0;
2704 *decpt = dec2 ? dec2 : dec1;
2705 return 0;
2708 /***********************************************************************
2709 * _gcvt (MSVCRT.@)
2711 char * CDECL MSVCRT__gcvt( double number, int ndigit, char *buff )
2713 if(!buff) {
2714 *MSVCRT__errno() = MSVCRT_EINVAL;
2715 return NULL;
2718 if(ndigit < 0) {
2719 *MSVCRT__errno() = MSVCRT_ERANGE;
2720 return NULL;
2723 MSVCRT_sprintf(buff, "%.*g", ndigit, number);
2724 return buff;
2727 /***********************************************************************
2728 * _gcvt_s (MSVCRT.@)
2730 int CDECL MSVCRT__gcvt_s(char *buff, MSVCRT_size_t size, double number, int digits)
2732 int len;
2734 if(!buff) {
2735 *MSVCRT__errno() = MSVCRT_EINVAL;
2736 return MSVCRT_EINVAL;
2739 if( digits<0 || digits>=size) {
2740 if(size)
2741 buff[0] = '\0';
2743 *MSVCRT__errno() = MSVCRT_ERANGE;
2744 return MSVCRT_ERANGE;
2747 len = MSVCRT__scprintf("%.*g", digits, number);
2748 if(len > size) {
2749 buff[0] = '\0';
2750 *MSVCRT__errno() = MSVCRT_ERANGE;
2751 return MSVCRT_ERANGE;
2754 MSVCRT_sprintf(buff, "%.*g", digits, number);
2755 return 0;
2758 #include <stdlib.h> /* div_t, ldiv_t */
2760 /*********************************************************************
2761 * div (MSVCRT.@)
2762 * VERSION
2763 * [i386] Windows binary compatible - returns the struct in eax/edx.
2765 #ifdef __i386__
2766 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
2768 union {
2769 MSVCRT_div_t div;
2770 unsigned __int64 uint64;
2771 } ret;
2773 ret.div.quot = num / denom;
2774 ret.div.rem = num % denom;
2775 return ret.uint64;
2777 #else
2778 /*********************************************************************
2779 * div (MSVCRT.@)
2780 * VERSION
2781 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
2783 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
2785 MSVCRT_div_t ret;
2787 ret.quot = num / denom;
2788 ret.rem = num % denom;
2789 return ret;
2791 #endif /* ifdef __i386__ */
2794 /*********************************************************************
2795 * ldiv (MSVCRT.@)
2796 * VERSION
2797 * [i386] Windows binary compatible - returns the struct in eax/edx.
2799 #ifdef __i386__
2800 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
2802 union {
2803 MSVCRT_ldiv_t ldiv;
2804 unsigned __int64 uint64;
2805 } ret;
2807 ret.ldiv.quot = num / denom;
2808 ret.ldiv.rem = num % denom;
2809 return ret.uint64;
2811 #else
2812 /*********************************************************************
2813 * ldiv (MSVCRT.@)
2814 * VERSION
2815 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
2817 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
2819 MSVCRT_ldiv_t ret;
2821 ret.quot = num / denom;
2822 ret.rem = num % denom;
2823 return ret;
2825 #endif /* ifdef __i386__ */
2827 #if _MSVCR_VER>=100
2828 /*********************************************************************
2829 * lldiv (MSVCR100.@)
2831 MSVCRT_lldiv_t CDECL MSVCRT_lldiv(MSVCRT_longlong num, MSVCRT_longlong denom)
2833 MSVCRT_lldiv_t ret;
2835 ret.quot = num / denom;
2836 ret.rem = num % denom;
2838 return ret;
2840 #endif
2842 #ifdef __i386__
2844 /*********************************************************************
2845 * _adjust_fdiv (MSVCRT.@)
2846 * Used by the MSVC compiler to work around the Pentium FDIV bug.
2848 int MSVCRT__adjust_fdiv = 0;
2850 /***********************************************************************
2851 * _adj_fdiv_m16i (MSVCRT.@)
2853 * NOTE
2854 * I _think_ this function is intended to work around the Pentium
2855 * fdiv bug.
2857 void __stdcall _adj_fdiv_m16i( short arg )
2859 TRACE("(): stub\n");
2862 /***********************************************************************
2863 * _adj_fdiv_m32 (MSVCRT.@)
2865 * NOTE
2866 * I _think_ this function is intended to work around the Pentium
2867 * fdiv bug.
2869 void __stdcall _adj_fdiv_m32( unsigned int arg )
2871 TRACE("(): stub\n");
2874 /***********************************************************************
2875 * _adj_fdiv_m32i (MSVCRT.@)
2877 * NOTE
2878 * I _think_ this function is intended to work around the Pentium
2879 * fdiv bug.
2881 void __stdcall _adj_fdiv_m32i( int arg )
2883 TRACE("(): stub\n");
2886 /***********************************************************************
2887 * _adj_fdiv_m64 (MSVCRT.@)
2889 * NOTE
2890 * I _think_ this function is intended to work around the Pentium
2891 * fdiv bug.
2893 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
2895 TRACE("(): stub\n");
2898 /***********************************************************************
2899 * _adj_fdiv_r (MSVCRT.@)
2900 * FIXME
2901 * This function is likely to have the wrong number of arguments.
2903 * NOTE
2904 * I _think_ this function is intended to work around the Pentium
2905 * fdiv bug.
2907 void _adj_fdiv_r(void)
2909 TRACE("(): stub\n");
2912 /***********************************************************************
2913 * _adj_fdivr_m16i (MSVCRT.@)
2915 * NOTE
2916 * I _think_ this function is intended to work around the Pentium
2917 * fdiv bug.
2919 void __stdcall _adj_fdivr_m16i( short arg )
2921 TRACE("(): stub\n");
2924 /***********************************************************************
2925 * _adj_fdivr_m32 (MSVCRT.@)
2927 * NOTE
2928 * I _think_ this function is intended to work around the Pentium
2929 * fdiv bug.
2931 void __stdcall _adj_fdivr_m32( unsigned int arg )
2933 TRACE("(): stub\n");
2936 /***********************************************************************
2937 * _adj_fdivr_m32i (MSVCRT.@)
2939 * NOTE
2940 * I _think_ this function is intended to work around the Pentium
2941 * fdiv bug.
2943 void __stdcall _adj_fdivr_m32i( int arg )
2945 TRACE("(): stub\n");
2948 /***********************************************************************
2949 * _adj_fdivr_m64 (MSVCRT.@)
2951 * NOTE
2952 * I _think_ this function is intended to work around the Pentium
2953 * fdiv bug.
2955 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
2957 TRACE("(): stub\n");
2960 /***********************************************************************
2961 * _adj_fpatan (MSVCRT.@)
2962 * FIXME
2963 * This function is likely to have the wrong number of arguments.
2965 * NOTE
2966 * I _think_ this function is intended to work around the Pentium
2967 * fdiv bug.
2969 void _adj_fpatan(void)
2971 TRACE("(): stub\n");
2974 /***********************************************************************
2975 * _adj_fprem (MSVCRT.@)
2976 * FIXME
2977 * This function is likely to have the wrong number of arguments.
2979 * NOTE
2980 * I _think_ this function is intended to work around the Pentium
2981 * fdiv bug.
2983 void _adj_fprem(void)
2985 TRACE("(): stub\n");
2988 /***********************************************************************
2989 * _adj_fprem1 (MSVCRT.@)
2990 * FIXME
2991 * This function is likely to have the wrong number of arguments.
2993 * NOTE
2994 * I _think_ this function is intended to work around the Pentium
2995 * fdiv bug.
2997 void _adj_fprem1(void)
2999 TRACE("(): stub\n");
3002 /***********************************************************************
3003 * _adj_fptan (MSVCRT.@)
3004 * FIXME
3005 * This function is likely to have the wrong number of arguments.
3007 * NOTE
3008 * I _think_ this function is intended to work around the Pentium
3009 * fdiv bug.
3011 void _adj_fptan(void)
3013 TRACE("(): stub\n");
3016 /***********************************************************************
3017 * _safe_fdiv (MSVCRT.@)
3018 * FIXME
3019 * This function is likely to have the wrong number of arguments.
3021 * NOTE
3022 * I _think_ this function is intended to work around the Pentium
3023 * fdiv bug.
3025 void _safe_fdiv(void)
3027 TRACE("(): stub\n");
3030 /***********************************************************************
3031 * _safe_fdivr (MSVCRT.@)
3032 * FIXME
3033 * This function is likely to have the wrong number of arguments.
3035 * NOTE
3036 * I _think_ this function is intended to work around the Pentium
3037 * fdiv bug.
3039 void _safe_fdivr(void)
3041 TRACE("(): stub\n");
3044 /***********************************************************************
3045 * _safe_fprem (MSVCRT.@)
3046 * FIXME
3047 * This function is likely to have the wrong number of arguments.
3049 * NOTE
3050 * I _think_ this function is intended to work around the Pentium
3051 * fdiv bug.
3053 void _safe_fprem(void)
3055 TRACE("(): stub\n");
3058 /***********************************************************************
3059 * _safe_fprem1 (MSVCRT.@)
3061 * FIXME
3062 * This function is likely to have the wrong number of arguments.
3064 * NOTE
3065 * I _think_ this function is intended to work around the Pentium
3066 * fdiv bug.
3068 void _safe_fprem1(void)
3070 TRACE("(): stub\n");
3073 /***********************************************************************
3074 * __libm_sse2_acos (MSVCRT.@)
3076 void __cdecl MSVCRT___libm_sse2_acos(void)
3078 double d;
3079 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3080 d = acos( d );
3081 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3084 /***********************************************************************
3085 * __libm_sse2_acosf (MSVCRT.@)
3087 void __cdecl MSVCRT___libm_sse2_acosf(void)
3089 float f;
3090 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3091 f = acosf( f );
3092 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3095 /***********************************************************************
3096 * __libm_sse2_asin (MSVCRT.@)
3098 void __cdecl MSVCRT___libm_sse2_asin(void)
3100 double d;
3101 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3102 d = asin( d );
3103 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3106 /***********************************************************************
3107 * __libm_sse2_asinf (MSVCRT.@)
3109 void __cdecl MSVCRT___libm_sse2_asinf(void)
3111 float f;
3112 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3113 f = asinf( f );
3114 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3117 /***********************************************************************
3118 * __libm_sse2_atan (MSVCRT.@)
3120 void __cdecl MSVCRT___libm_sse2_atan(void)
3122 double d;
3123 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3124 d = atan( d );
3125 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3128 /***********************************************************************
3129 * __libm_sse2_atan2 (MSVCRT.@)
3131 void __cdecl MSVCRT___libm_sse2_atan2(void)
3133 double d1, d2;
3134 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
3135 d1 = atan2( d1, d2 );
3136 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
3139 /***********************************************************************
3140 * __libm_sse2_atanf (MSVCRT.@)
3142 void __cdecl MSVCRT___libm_sse2_atanf(void)
3144 float f;
3145 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3146 f = atanf( f );
3147 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3150 /***********************************************************************
3151 * __libm_sse2_cos (MSVCRT.@)
3153 void __cdecl MSVCRT___libm_sse2_cos(void)
3155 double d;
3156 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3157 d = cos( d );
3158 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3161 /***********************************************************************
3162 * __libm_sse2_cosf (MSVCRT.@)
3164 void __cdecl MSVCRT___libm_sse2_cosf(void)
3166 float f;
3167 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3168 f = cosf( f );
3169 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3172 /***********************************************************************
3173 * __libm_sse2_exp (MSVCRT.@)
3175 void __cdecl MSVCRT___libm_sse2_exp(void)
3177 double d;
3178 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3179 d = exp( d );
3180 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3183 /***********************************************************************
3184 * __libm_sse2_expf (MSVCRT.@)
3186 void __cdecl MSVCRT___libm_sse2_expf(void)
3188 float f;
3189 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3190 f = expf( f );
3191 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3194 /***********************************************************************
3195 * __libm_sse2_log (MSVCRT.@)
3197 void __cdecl MSVCRT___libm_sse2_log(void)
3199 double d;
3200 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3201 d = log( d );
3202 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3205 /***********************************************************************
3206 * __libm_sse2_log10 (MSVCRT.@)
3208 void __cdecl MSVCRT___libm_sse2_log10(void)
3210 double d;
3211 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3212 d = log10( d );
3213 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3216 /***********************************************************************
3217 * __libm_sse2_log10f (MSVCRT.@)
3219 void __cdecl MSVCRT___libm_sse2_log10f(void)
3221 float f;
3222 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3223 f = log10f( f );
3224 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3227 /***********************************************************************
3228 * __libm_sse2_logf (MSVCRT.@)
3230 void __cdecl MSVCRT___libm_sse2_logf(void)
3232 float f;
3233 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3234 f = logf( f );
3235 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3238 /***********************************************************************
3239 * __libm_sse2_pow (MSVCRT.@)
3241 void __cdecl MSVCRT___libm_sse2_pow(void)
3243 double d1, d2;
3244 __asm__ __volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1), "=m" (d2) );
3245 d1 = pow( d1, d2 );
3246 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d1) );
3249 /***********************************************************************
3250 * __libm_sse2_powf (MSVCRT.@)
3252 void __cdecl MSVCRT___libm_sse2_powf(void)
3254 float f1, f2;
3255 __asm__ __volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1), "=g" (f2) );
3256 f1 = powf( f1, f2 );
3257 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f1) );
3260 /***********************************************************************
3261 * __libm_sse2_sin (MSVCRT.@)
3263 void __cdecl MSVCRT___libm_sse2_sin(void)
3265 double d;
3266 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3267 d = sin( d );
3268 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3271 /***********************************************************************
3272 * __libm_sse2_sinf (MSVCRT.@)
3274 void __cdecl MSVCRT___libm_sse2_sinf(void)
3276 float f;
3277 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3278 f = sinf( f );
3279 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3282 /***********************************************************************
3283 * __libm_sse2_tan (MSVCRT.@)
3285 void __cdecl MSVCRT___libm_sse2_tan(void)
3287 double d;
3288 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3289 d = tan( d );
3290 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3293 /***********************************************************************
3294 * __libm_sse2_tanf (MSVCRT.@)
3296 void __cdecl MSVCRT___libm_sse2_tanf(void)
3298 float f;
3299 __asm__ __volatile__( "movd %%xmm0,%0" : "=g" (f) );
3300 f = tanf( f );
3301 __asm__ __volatile__( "movd %0,%%xmm0" : : "g" (f) );
3304 /***********************************************************************
3305 * __libm_sse2_sqrt_precise (MSVCR110.@)
3307 void __cdecl MSVCRT___libm_sse2_sqrt_precise(void)
3309 double d;
3310 __asm__ __volatile__( "movq %%xmm0,%0" : "=m" (d) );
3311 d = sqrt( d );
3312 __asm__ __volatile__( "movq %0,%%xmm0" : : "m" (d) );
3315 #endif /* __i386__ */
3317 /*********************************************************************
3318 * cbrt (MSVCR120.@)
3320 double CDECL MSVCR120_cbrt(double x)
3322 #ifdef HAVE_CBRT
3323 return cbrt(x);
3324 #else
3325 return x < 0 ? -pow(-x, 1.0 / 3.0) : pow(x, 1.0 / 3.0);
3326 #endif
3329 /*********************************************************************
3330 * cbrtf (MSVCR120.@)
3332 float CDECL MSVCR120_cbrtf(float x)
3334 #ifdef HAVE_CBRTF
3335 return cbrtf(x);
3336 #else
3337 return MSVCR120_cbrt(x);
3338 #endif
3341 /*********************************************************************
3342 * cbrtl (MSVCR120.@)
3344 LDOUBLE CDECL MSVCR120_cbrtl(LDOUBLE x)
3346 return MSVCR120_cbrt(x);
3349 /*********************************************************************
3350 * exp2 (MSVCR120.@)
3352 double CDECL MSVCR120_exp2(double x)
3354 #ifdef HAVE_EXP2
3355 double ret = exp2(x);
3356 #else
3357 double ret = pow(2, x);
3358 #endif
3359 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3360 return ret;
3363 /*********************************************************************
3364 * exp2f (MSVCR120.@)
3366 float CDECL MSVCR120_exp2f(float x)
3368 #ifdef HAVE_EXP2F
3369 float ret = exp2f(x);
3370 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3371 return ret;
3372 #else
3373 return MSVCR120_exp2(x);
3374 #endif
3377 /*********************************************************************
3378 * exp2l (MSVCR120.@)
3380 LDOUBLE CDECL MSVCR120_exp2l(LDOUBLE x)
3382 return MSVCR120_exp2(x);
3385 /*********************************************************************
3386 * expm1 (MSVCR120.@)
3388 double CDECL MSVCR120_expm1(double x)
3390 #ifdef HAVE_EXPM1
3391 double ret = expm1(x);
3392 #else
3393 double ret = exp(x) - 1;
3394 #endif
3395 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3396 return ret;
3399 /*********************************************************************
3400 * expm1f (MSVCR120.@)
3402 float CDECL MSVCR120_expm1f(float x)
3404 #ifdef HAVE_EXPM1F
3405 float ret = expm1f(x);
3406 #else
3407 float ret = exp(x) - 1;
3408 #endif
3409 if (isfinite(x) && !isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
3410 return ret;
3413 /*********************************************************************
3414 * expm1l (MSVCR120.@)
3416 LDOUBLE CDECL MSVCR120_expm1l(LDOUBLE x)
3418 return MSVCR120_expm1(x);
3421 /*********************************************************************
3422 * log1p (MSVCR120.@)
3424 double CDECL MSVCR120_log1p(double x)
3426 if (x < -1) *MSVCRT__errno() = MSVCRT_EDOM;
3427 else if (x == -1) *MSVCRT__errno() = MSVCRT_ERANGE;
3428 #ifdef HAVE_LOG1P
3429 return log1p(x);
3430 #else
3431 return log(1 + x);
3432 #endif
3435 /*********************************************************************
3436 * log1pf (MSVCR120.@)
3438 float CDECL MSVCR120_log1pf(float x)
3440 if (x < -1) *MSVCRT__errno() = MSVCRT_EDOM;
3441 else if (x == -1) *MSVCRT__errno() = MSVCRT_ERANGE;
3442 #ifdef HAVE_LOG1PF
3443 return log1pf(x);
3444 #else
3445 return log(1 + x);
3446 #endif
3449 /*********************************************************************
3450 * log1pl (MSVCR120.@)
3452 LDOUBLE CDECL MSVCR120_log1pl(LDOUBLE x)
3454 return MSVCR120_log1p(x);
3457 /*********************************************************************
3458 * log2 (MSVCR120.@)
3460 double CDECL MSVCR120_log2(double x)
3462 if (x < 0) *MSVCRT__errno() = MSVCRT_EDOM;
3463 else if (x == 0) *MSVCRT__errno() = MSVCRT_ERANGE;
3464 #ifdef HAVE_LOG2
3465 return log2(x);
3466 #else
3467 return log(x) / log(2);
3468 #endif
3471 /*********************************************************************
3472 * log2f (MSVCR120.@)
3474 float CDECL MSVCR120_log2f(float x)
3476 #ifdef HAVE_LOG2F
3477 if (x < 0) *MSVCRT__errno() = MSVCRT_EDOM;
3478 else if (x == 0) *MSVCRT__errno() = MSVCRT_ERANGE;
3479 return log2f(x);
3480 #else
3481 return MSVCR120_log2(x);
3482 #endif
3485 /*********************************************************************
3486 * log2l (MSVCR120.@)
3488 LDOUBLE CDECL MSVCR120_log2l(LDOUBLE x)
3490 return MSVCR120_log2(x);
3493 /*********************************************************************
3494 * rint (MSVCR120.@)
3496 double CDECL MSVCR120_rint(double x)
3498 return rint(x);
3501 /*********************************************************************
3502 * rintf (MSVCR120.@)
3504 float CDECL MSVCR120_rintf(float x)
3506 return rintf(x);
3509 /*********************************************************************
3510 * rintl (MSVCR120.@)
3512 LDOUBLE CDECL MSVCR120_rintl(LDOUBLE x)
3514 return MSVCR120_rint(x);
3517 /*********************************************************************
3518 * lrint (MSVCR120.@)
3520 MSVCRT_long CDECL MSVCR120_lrint(double x)
3522 return lrint(x);
3525 /*********************************************************************
3526 * lrintf (MSVCR120.@)
3528 MSVCRT_long CDECL MSVCR120_lrintf(float x)
3530 return lrintf(x);
3533 /*********************************************************************
3534 * lrintl (MSVCR120.@)
3536 MSVCRT_long CDECL MSVCR120_lrintl(LDOUBLE x)
3538 return MSVCR120_lrint(x);
3541 /*********************************************************************
3542 * llrint (MSVCR120.@)
3544 MSVCRT_longlong CDECL MSVCR120_llrint(double x)
3546 return llrint(x);
3549 /*********************************************************************
3550 * llrintf (MSVCR120.@)
3552 MSVCRT_longlong CDECL MSVCR120_llrintf(float x)
3554 return llrintf(x);
3557 /*********************************************************************
3558 * rintl (MSVCR120.@)
3560 MSVCRT_longlong CDECL MSVCR120_llrintl(LDOUBLE x)
3562 return MSVCR120_llrint(x);
3565 #if _MSVCR_VER>=120
3567 /*********************************************************************
3568 * round (MSVCR120.@)
3570 double CDECL MSVCR120_round(double x)
3572 #ifdef HAVE_ROUND
3573 return round(x);
3574 #else
3575 return MSVCR120_rint(x);
3576 #endif
3579 /*********************************************************************
3580 * roundf (MSVCR120.@)
3582 float CDECL MSVCR120_roundf(float x)
3584 #ifdef HAVE_ROUNDF
3585 return roundf(x);
3586 #else
3587 return MSVCR120_round(x);
3588 #endif
3591 /*********************************************************************
3592 * roundl (MSVCR120.@)
3594 LDOUBLE CDECL MSVCR120_roundl(LDOUBLE x)
3596 return MSVCR120_round(x);
3599 /*********************************************************************
3600 * lround (MSVCR120.@)
3602 MSVCRT_long CDECL MSVCR120_lround(double x)
3604 #ifdef HAVE_LROUND
3605 return lround(x);
3606 #else
3607 return MSVCR120_round(x);
3608 #endif
3611 /*********************************************************************
3612 * lroundf (MSVCR120.@)
3614 MSVCRT_long CDECL MSVCR120_lroundf(float x)
3616 #ifdef HAVE_LROUNDF
3617 return lroundf(x);
3618 #else
3619 return MSVCR120_lround(x);
3620 #endif
3623 /*********************************************************************
3624 * lroundl (MSVCR120.@)
3626 MSVCRT_long CDECL MSVCR120_lroundl(LDOUBLE x)
3628 return MSVCR120_lround(x);
3631 /*********************************************************************
3632 * llround (MSVCR120.@)
3634 MSVCRT_longlong CDECL MSVCR120_llround(double x)
3636 #ifdef HAVE_LLROUND
3637 return llround(x);
3638 #else
3639 return MSVCR120_round(x);
3640 #endif
3643 /*********************************************************************
3644 * llroundf (MSVCR120.@)
3646 MSVCRT_longlong CDECL MSVCR120_llroundf(float x)
3648 #ifdef HAVE_LLROUNDF
3649 return llroundf(x);
3650 #else
3651 return MSVCR120_llround(x);
3652 #endif
3655 /*********************************************************************
3656 * roundl (MSVCR120.@)
3658 MSVCRT_longlong CDECL MSVCR120_llroundl(LDOUBLE x)
3660 return MSVCR120_llround(x);
3663 /*********************************************************************
3664 * trunc (MSVCR120.@)
3666 double CDECL MSVCR120_trunc(double x)
3668 #ifdef HAVE_TRUNC
3669 return trunc(x);
3670 #else
3671 return (x > 0) ? floor(x) : ceil(x);
3672 #endif
3675 /*********************************************************************
3676 * truncf (MSVCR120.@)
3678 float CDECL MSVCR120_truncf(float x)
3680 #ifdef HAVE_TRUNCF
3681 return truncf(x);
3682 #else
3683 return MSVCR120_trunc(x);
3684 #endif
3687 /*********************************************************************
3688 * truncl (MSVCR120.@)
3690 LDOUBLE CDECL MSVCR120_truncl(LDOUBLE x)
3692 return MSVCR120_trunc(x);
3695 /*********************************************************************
3696 * _dclass (MSVCR120.@)
3698 * Copied from musl: src/math/__fpclassify.c
3700 short CDECL MSVCR120__dclass(double x)
3702 union { double f; UINT64 i; } u = { x };
3703 int e = u.i >> 52 & 0x7ff;
3705 if (!e) return u.i << 1 ? MSVCRT_FP_SUBNORMAL : MSVCRT_FP_ZERO;
3706 if (e == 0x7ff) return (u.i << 12) ? MSVCRT_FP_NAN : MSVCRT_FP_INFINITE;
3707 return MSVCRT_FP_NORMAL;
3710 /*********************************************************************
3711 * _fdclass (MSVCR120.@)
3713 * Copied from musl: src/math/__fpclassifyf.c
3715 short CDECL MSVCR120__fdclass(float x)
3717 union { float f; UINT32 i; } u = { x };
3718 int e = u.i >> 23 & 0xff;
3720 if (!e) return u.i << 1 ? MSVCRT_FP_SUBNORMAL : MSVCRT_FP_ZERO;
3721 if (e == 0xff) return u.i << 9 ? MSVCRT_FP_NAN : MSVCRT_FP_INFINITE;
3722 return MSVCRT_FP_NORMAL;
3725 /*********************************************************************
3726 * _ldclass (MSVCR120.@)
3728 short CDECL MSVCR120__ldclass(LDOUBLE x)
3730 return MSVCR120__dclass(x);
3733 /*********************************************************************
3734 * _dtest (MSVCR120.@)
3736 short CDECL MSVCR120__dtest(double *x)
3738 return MSVCR120__dclass(*x);
3741 /*********************************************************************
3742 * _fdtest (MSVCR120.@)
3744 short CDECL MSVCR120__fdtest(float *x)
3746 return MSVCR120__fdclass(*x);
3749 /*********************************************************************
3750 * _ldtest (MSVCR120.@)
3752 short CDECL MSVCR120__ldtest(LDOUBLE *x)
3754 return MSVCR120__dclass(*x);
3757 /*********************************************************************
3758 * erf (MSVCR120.@)
3760 double CDECL MSVCR120_erf(double x)
3762 #ifdef HAVE_ERF
3763 return erf(x);
3764 #else
3765 /* Abramowitz and Stegun approximation, maximum error: 1.5*10^-7 */
3766 double t, y;
3767 int sign = signbit(x);
3769 if (sign) x = -x;
3770 t = 1 / (1 + 0.3275911 * x);
3771 y = ((((1.061405429*t - 1.453152027)*t + 1.421413741)*t - 0.284496736)*t + 0.254829592)*t;
3772 y = 1.0 - y*exp(-x*x);
3773 return sign ? -y : y;
3774 #endif
3777 /*********************************************************************
3778 * erff (MSVCR120.@)
3780 float CDECL MSVCR120_erff(float x)
3782 #ifdef HAVE_ERFF
3783 return erff(x);
3784 #else
3785 return MSVCR120_erf(x);
3786 #endif
3789 /*********************************************************************
3790 * erfl (MSVCR120.@)
3792 LDOUBLE CDECL MSVCR120_erfl(LDOUBLE x)
3794 return MSVCR120_erf(x);
3797 /*********************************************************************
3798 * erfc (MSVCR120.@)
3800 double CDECL MSVCR120_erfc(double x)
3802 #ifdef HAVE_ERFC
3803 return erfc(x);
3804 #else
3805 return 1 - MSVCR120_erf(x);
3806 #endif
3809 /*********************************************************************
3810 * erfcf (MSVCR120.@)
3812 float CDECL MSVCR120_erfcf(float x)
3814 #ifdef HAVE_ERFCF
3815 return erfcf(x);
3816 #else
3817 return MSVCR120_erfc(x);
3818 #endif
3821 /*********************************************************************
3822 * erfcl (MSVCR120.@)
3824 LDOUBLE CDECL MSVCR120_erfcl(LDOUBLE x)
3826 return MSVCR120_erfc(x);
3829 /*********************************************************************
3830 * fmaxf (MSVCR120.@)
3832 float CDECL MSVCR120_fmaxf(float x, float y)
3834 if(isnan(x))
3835 return y;
3836 if(isnan(y))
3837 return x;
3838 if(x==0 && y==0)
3839 return signbit(x) ? y : x;
3840 return x<y ? y : x;
3843 /*********************************************************************
3844 * fmax (MSVCR120.@)
3846 double CDECL MSVCR120_fmax(double x, double y)
3848 if(isnan(x))
3849 return y;
3850 if(isnan(y))
3851 return x;
3852 if(x==0 && y==0)
3853 return signbit(x) ? y : x;
3854 return x<y ? y : x;
3857 /*********************************************************************
3858 * fdimf (MSVCR120.@)
3860 float CDECL MSVCR120_fdimf(float x, float y)
3862 if(isnan(x))
3863 return x;
3864 if(isnan(y))
3865 return y;
3866 return x>y ? x-y : 0;
3869 /*********************************************************************
3870 * fdim (MSVCR120.@)
3872 double CDECL MSVCR120_fdim(double x, double y)
3874 if(isnan(x))
3875 return x;
3876 if(isnan(y))
3877 return y;
3878 return x>y ? x-y : 0;
3881 /*********************************************************************
3882 * _fdsign (MSVCR120.@)
3884 int CDECL MSVCR120__fdsign(float x)
3886 union { float f; UINT32 i; } u = { x };
3887 return (u.i >> 16) & 0x8000;
3890 /*********************************************************************
3891 * _dsign (MSVCR120.@)
3893 int CDECL MSVCR120__dsign(double x)
3895 union { double f; UINT64 i; } u = { x };
3896 return (u.i >> 48) & 0x8000;
3900 /*********************************************************************
3901 * _dpcomp (MSVCR120.@)
3903 int CDECL MSVCR120__dpcomp(double x, double y)
3905 if(isnan(x) || isnan(y))
3906 return 0;
3908 if(x == y) return 2;
3909 return x < y ? 1 : 4;
3912 /*********************************************************************
3913 * _fdpcomp (MSVCR120.@)
3915 int CDECL MSVCR120__fdpcomp(float x, float y)
3917 return MSVCR120__dpcomp(x, y);
3920 /*********************************************************************
3921 * fminf (MSVCR120.@)
3923 float CDECL MSVCR120_fminf(float x, float y)
3925 if(isnan(x))
3926 return y;
3927 if(isnan(y))
3928 return x;
3929 if(x==0 && y==0)
3930 return signbit(x) ? x : y;
3931 return x<y ? x : y;
3934 /*********************************************************************
3935 * fmin (MSVCR120.@)
3937 double CDECL MSVCR120_fmin(double x, double y)
3939 if(isnan(x))
3940 return y;
3941 if(isnan(y))
3942 return x;
3943 if(x==0 && y==0)
3944 return signbit(x) ? x : y;
3945 return x<y ? x : y;
3948 /*********************************************************************
3949 * asinh (MSVCR120.@)
3951 double CDECL MSVCR120_asinh(double x)
3953 #ifdef HAVE_ASINH
3954 return asinh(x);
3955 #else
3956 if (!isfinite(x*x+1)) {
3957 if (x > 0) return log(2) + log(x);
3958 else return -log(2) - log(-x);
3960 return log(x + sqrt(x*x+1));
3961 #endif
3964 /*********************************************************************
3965 * asinhf (MSVCR120.@)
3967 float CDECL MSVCR120_asinhf(float x)
3969 #ifdef HAVE_ASINHF
3970 return asinhf(x);
3971 #else
3972 return MSVCR120_asinh(x);
3973 #endif
3976 /*********************************************************************
3977 * asinhl (MSVCR120.@)
3979 LDOUBLE CDECL MSVCR120_asinhl(LDOUBLE x)
3981 return MSVCR120_asinh(x);
3984 /*********************************************************************
3985 * acosh (MSVCR120.@)
3987 double CDECL MSVCR120_acosh(double x)
3989 if (x < 1) *MSVCRT__errno() = MSVCRT_EDOM;
3991 #ifdef HAVE_ACOSH
3992 return acosh(x);
3993 #else
3994 if (x < 1) {
3995 MSVCRT_fenv_t env;
3997 MSVCRT_fegetenv(&env);
3998 env.status |= MSVCRT__SW_INVALID;
3999 MSVCRT_fesetenv(&env);
4000 return NAN;
4002 if (!isfinite(x*x)) return log(2) + log(x);
4003 return log(x + sqrt(x*x-1));
4004 #endif
4007 /*********************************************************************
4008 * acoshf (MSVCR120.@)
4010 float CDECL MSVCR120_acoshf(float x)
4012 #ifdef HAVE_ACOSHF
4013 if (x < 1) *MSVCRT__errno() = MSVCRT_EDOM;
4015 return acoshf(x);
4016 #else
4017 return MSVCR120_acosh(x);
4018 #endif
4021 /*********************************************************************
4022 * acoshl (MSVCR120.@)
4024 LDOUBLE CDECL MSVCR120_acoshl(LDOUBLE x)
4026 return MSVCR120_acosh(x);
4029 /*********************************************************************
4030 * atanh (MSVCR120.@)
4032 double CDECL MSVCR120_atanh(double x)
4034 double ret;
4036 if (x > 1 || x < -1) {
4037 MSVCRT_fenv_t env;
4039 *MSVCRT__errno() = MSVCRT_EDOM;
4041 /* on Linux atanh returns -NAN in this case */
4042 MSVCRT_fegetenv(&env);
4043 env.status |= MSVCRT__SW_INVALID;
4044 MSVCRT_fesetenv(&env);
4045 return NAN;
4048 #ifdef HAVE_ATANH
4049 ret = atanh(x);
4050 #else
4051 if (-1e-6 < x && x < 1e-6) ret = x + x*x*x/3;
4052 else ret = (log(1+x) - log(1-x)) / 2;
4053 #endif
4055 if (!isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
4056 return ret;
4059 /*********************************************************************
4060 * atanhf (MSVCR120.@)
4062 float CDECL MSVCR120_atanhf(float x)
4064 #ifdef HAVE_ATANHF
4065 float ret;
4067 if (x > 1 || x < -1) {
4068 MSVCRT_fenv_t env;
4070 *MSVCRT__errno() = MSVCRT_EDOM;
4072 MSVCRT_fegetenv(&env);
4073 env.status |= MSVCRT__SW_INVALID;
4074 MSVCRT_fesetenv(&env);
4075 return NAN;
4078 ret = atanhf(x);
4080 if (!isfinite(ret)) *MSVCRT__errno() = MSVCRT_ERANGE;
4081 return ret;
4082 #else
4083 return MSVCR120_atanh(x);
4084 #endif
4087 /*********************************************************************
4088 * atanhl (MSVCR120.@)
4090 LDOUBLE CDECL MSVCR120_atanhl(LDOUBLE x)
4092 return MSVCR120_atanh(x);
4095 #endif /* _MSVCR_VER>=120 */
4097 /*********************************************************************
4098 * _scalb (MSVCRT.@)
4099 * scalbn (MSVCR120.@)
4100 * scalbln (MSVCR120.@)
4102 double CDECL MSVCRT__scalb(double num, MSVCRT_long power)
4104 return MSVCRT_ldexp(num, power);
4107 /*********************************************************************
4108 * _scalbf (MSVCRT.@)
4109 * scalbnf (MSVCR120.@)
4110 * scalblnf (MSVCR120.@)
4112 float CDECL MSVCRT__scalbf(float num, MSVCRT_long power)
4114 return MSVCRT_ldexp(num, power);
4117 #if _MSVCR_VER>=120
4119 /*********************************************************************
4120 * scalbnl (MSVCR120.@)
4121 * scalblnl (MSVCR120.@)
4123 LDOUBLE CDECL MSVCR120_scalbnl(LDOUBLE num, MSVCRT_long power)
4125 return MSVCRT__scalb(num, power);
4128 /*********************************************************************
4129 * remainder (MSVCR120.@)
4131 double CDECL MSVCR120_remainder(double x, double y)
4133 #ifdef HAVE_REMAINDER
4134 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
4135 if(!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
4136 if(isnan(y) || y==0.0) *MSVCRT__errno() = MSVCRT_EDOM;
4137 return remainder(x, y);
4138 #else
4139 FIXME( "not implemented\n" );
4140 return 0.0;
4141 #endif
4144 /*********************************************************************
4145 * remainderf (MSVCR120.@)
4147 float CDECL MSVCR120_remainderf(float x, float y)
4149 #ifdef HAVE_REMAINDERF
4150 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
4151 if(!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
4152 if(isnan(y) || y==0.0f) *MSVCRT__errno() = MSVCRT_EDOM;
4153 return remainderf(x, y);
4154 #else
4155 FIXME( "not implemented\n" );
4156 return 0.0f;
4157 #endif
4160 /*********************************************************************
4161 * remainderl (MSVCR120.@)
4163 LDOUBLE CDECL MSVCR120_remainderl(LDOUBLE x, LDOUBLE y)
4165 return MSVCR120_remainder(x, y);
4168 /*********************************************************************
4169 * remquo (MSVCR120.@)
4171 double CDECL MSVCR120_remquo(double x, double y, int *quo)
4173 #ifdef HAVE_REMQUO
4174 if(!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
4175 if(isnan(y) || y==0.0) *MSVCRT__errno() = MSVCRT_EDOM;
4176 return remquo(x, y, quo);
4177 #else
4178 FIXME( "not implemented\n" );
4179 return 0.0;
4180 #endif
4183 /*********************************************************************
4184 * remquof (MSVCR120.@)
4186 float CDECL MSVCR120_remquof(float x, float y, int *quo)
4188 #ifdef HAVE_REMQUOF
4189 if(!isfinite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
4190 if(isnan(y) || y==0.0f) *MSVCRT__errno() = MSVCRT_EDOM;
4191 return remquof(x, y, quo);
4192 #else
4193 FIXME( "not implemented\n" );
4194 return 0.0f;
4195 #endif
4198 /*********************************************************************
4199 * remquol (MSVCR120.@)
4201 LDOUBLE CDECL MSVCR120_remquol(LDOUBLE x, LDOUBLE y, int *quo)
4203 return MSVCR120_remquo(x, y, quo);
4206 /*********************************************************************
4207 * lgamma (MSVCR120.@)
4209 double CDECL MSVCR120_lgamma(double x)
4211 #ifdef HAVE_LGAMMA
4212 return lgamma(x);
4213 #else
4214 FIXME( "not implemented\n" );
4215 return 0.0;
4216 #endif
4219 /*********************************************************************
4220 * lgammaf (MSVCR120.@)
4222 float CDECL MSVCR120_lgammaf(float x)
4224 #ifdef HAVE_LGAMMAF
4225 return lgammaf(x);
4226 #else
4227 FIXME( "not implemented\n" );
4228 return 0.0f;
4229 #endif
4232 /*********************************************************************
4233 * lgammal (MSVCR120.@)
4235 LDOUBLE CDECL MSVCR120_lgammal(LDOUBLE x)
4237 return MSVCR120_lgamma(x);
4240 /*********************************************************************
4241 * tgamma (MSVCR120.@)
4243 double CDECL MSVCR120_tgamma(double x)
4245 #ifdef HAVE_TGAMMA
4246 if(x==0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
4247 if(x<0.0f) {
4248 double integral;
4249 if (modf(x, &integral) == 0)
4250 *MSVCRT__errno() = MSVCRT_EDOM;
4252 return tgamma(x);
4253 #else
4254 FIXME( "not implemented\n" );
4255 return 0.0;
4256 #endif
4259 /*********************************************************************
4260 * tgammaf (MSVCR120.@)
4262 float CDECL MSVCR120_tgammaf(float x)
4264 #ifdef HAVE_TGAMMAF
4265 if(x==0.0f) *MSVCRT__errno() = MSVCRT_ERANGE;
4266 if(x<0.0f) {
4267 float integral;
4268 if (modff(x, &integral) == 0)
4269 *MSVCRT__errno() = MSVCRT_EDOM;
4271 return tgammaf(x);
4272 #else
4273 FIXME( "not implemented\n" );
4274 return 0.0f;
4275 #endif
4278 /*********************************************************************
4279 * nan (MSVCR120.@)
4281 double CDECL MSVCR120_nan(const char *tagp)
4283 /* Windows ignores input (MSDN) */
4284 return NAN;
4287 /*********************************************************************
4288 * nanf (MSVCR120.@)
4290 float CDECL MSVCR120_nanf(const char *tagp)
4292 return NAN;
4295 /*********************************************************************
4296 * _except1 (MSVCR120.@)
4297 * TODO:
4298 * - find meaning of ignored cw and operation bits
4299 * - unk parameter
4301 double CDECL _except1(DWORD fpe, _FP_OPERATION_CODE op, double arg, double res, DWORD cw, void *unk)
4303 ULONG_PTR exception_arg;
4304 DWORD exception = 0;
4305 MSVCRT_fenv_t env;
4306 DWORD fpword = 0;
4307 WORD operation;
4309 TRACE("(%x %x %lf %lf %x %p)\n", fpe, op, arg, res, cw, unk);
4311 #ifdef _WIN64
4312 cw = ((cw >> 7) & 0x3f) | ((cw >> 3) & 0xc00);
4313 #endif
4314 operation = op << 5;
4315 exception_arg = (ULONG_PTR)&operation;
4317 MSVCRT_fegetenv(&env);
4319 if (fpe & 0x1) { /* overflow */
4320 if ((fpe == 0x1 && (cw & 0x8)) || (fpe==0x11 && (cw & 0x28))) {
4321 /* 32-bit version also sets SW_INEXACT here */
4322 env.status |= MSVCRT__SW_OVERFLOW;
4323 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
4324 res = signbit(res) ? -INFINITY : INFINITY;
4325 } else {
4326 exception = EXCEPTION_FLT_OVERFLOW;
4328 } else if (fpe & 0x2) { /* underflow */
4329 if ((fpe == 0x2 && (cw & 0x10)) || (fpe==0x12 && (cw & 0x30))) {
4330 env.status |= MSVCRT__SW_UNDERFLOW;
4331 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
4332 res = signbit(res) ? -0.0 : 0.0;
4333 } else {
4334 exception = EXCEPTION_FLT_UNDERFLOW;
4336 } else if (fpe & 0x4) { /* zerodivide */
4337 if ((fpe == 0x4 && (cw & 0x4)) || (fpe==0x14 && (cw & 0x24))) {
4338 env.status |= MSVCRT__SW_ZERODIVIDE;
4339 if (fpe & 0x10) env.status |= MSVCRT__SW_INEXACT;
4340 } else {
4341 exception = EXCEPTION_FLT_DIVIDE_BY_ZERO;
4343 } else if (fpe & 0x8) { /* invalid */
4344 if (fpe == 0x8 && (cw & 0x1)) {
4345 env.status |= MSVCRT__SW_INVALID;
4346 } else {
4347 exception = EXCEPTION_FLT_INVALID_OPERATION;
4349 } else if (fpe & 0x10) { /* inexact */
4350 if (fpe == 0x10 && (cw & 0x20)) {
4351 env.status |= MSVCRT__SW_INEXACT;
4352 } else {
4353 exception = EXCEPTION_FLT_INEXACT_RESULT;
4357 if (exception)
4358 env.status = 0;
4359 MSVCRT_fesetenv(&env);
4360 if (exception)
4361 RaiseException(exception, 0, 1, &exception_arg);
4363 if (cw & 0x1) fpword |= MSVCRT__EM_INVALID;
4364 if (cw & 0x2) fpword |= MSVCRT__EM_DENORMAL;
4365 if (cw & 0x4) fpword |= MSVCRT__EM_ZERODIVIDE;
4366 if (cw & 0x8) fpword |= MSVCRT__EM_OVERFLOW;
4367 if (cw & 0x10) fpword |= MSVCRT__EM_UNDERFLOW;
4368 if (cw & 0x20) fpword |= MSVCRT__EM_INEXACT;
4369 switch (cw & 0xc00)
4371 case 0xc00: fpword |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
4372 case 0x800: fpword |= MSVCRT__RC_UP; break;
4373 case 0x400: fpword |= MSVCRT__RC_DOWN; break;
4375 switch (cw & 0x300)
4377 case 0x0: fpword |= MSVCRT__PC_24; break;
4378 case 0x200: fpword |= MSVCRT__PC_53; break;
4379 case 0x300: fpword |= MSVCRT__PC_64; break;
4381 if (cw & 0x1000) fpword |= MSVCRT__IC_AFFINE;
4382 _control87(fpword, 0xffffffff);
4384 return res;
4387 _Dcomplex* CDECL MSVCR120__Cbuild(_Dcomplex *ret, double r, double i)
4389 ret->x = r;
4390 ret->y = i;
4391 return ret;
4394 double CDECL MSVCR120_creal(_Dcomplex z)
4396 return z.x;
4399 /*********************************************************************
4400 * ilogb (MSVCR120.@)
4402 * Copied from musl: src/math/ilogb.c
4404 int CDECL MSVCR120_ilogb(double x)
4406 union { double f; UINT64 i; } u = { x };
4407 int e = u.i >> 52 & 0x7ff;
4409 if (!e)
4411 u.i <<= 12;
4412 if (u.i == 0) return MSVCRT_FP_ILOGB0;
4413 /* subnormal x */
4414 for (e = -0x3ff; u.i >> 63 == 0; e--, u.i <<= 1);
4415 return e;
4417 if (e == 0x7ff) return u.i << 12 ? MSVCRT_FP_ILOGBNAN : MSVCRT_INT_MAX;
4418 return e - 0x3ff;
4421 /*********************************************************************
4422 * ilogbf (MSVCR120.@)
4424 * Copied from musl: src/math/ilogbf.c
4426 int CDECL MSVCR120_ilogbf(float x)
4428 union { float f; UINT32 i; } u = { x };
4429 int e = u.i >> 23 & 0xff;
4431 if (!e)
4433 u.i <<= 9;
4434 if (u.i == 0) return MSVCRT_FP_ILOGB0;
4435 /* subnormal x */
4436 for (e = -0x7f; u.i >> 31 == 0; e--, u.i <<= 1);
4437 return e;
4439 if (e == 0xff) return u.i << 9 ? MSVCRT_FP_ILOGBNAN : MSVCRT_INT_MAX;
4440 return e - 0x7f;
4443 /*********************************************************************
4444 * ilogbl (MSVCR120.@)
4446 int CDECL MSVCR120_ilogbl(LDOUBLE x)
4448 return MSVCR120_ilogb(x);
4451 #endif /* _MSVCR_VER>=120 */