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 * ====================================================
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
58 #define _DOMAIN 1 /* domain error in argument */
59 #define _SING 2 /* singularity */
60 #define _OVERFLOW 3 /* range overflow */
61 #define _UNDERFLOW 4 /* range underflow */
63 typedef int (CDECL
*MSVCRT_matherr_func
)(struct _exception
*);
65 static MSVCRT_matherr_func MSVCRT_default_matherr_func
= NULL
;
68 static BOOL sse2_enabled
;
70 static const struct unix_funcs
*unix_funcs
;
72 void msvcrt_init_math( void *module
)
74 sse2_supported
= IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE
);
78 sse2_enabled
= sse2_supported
;
80 __wine_init_unix_lib( module
, DLL_PROCESS_ATTACH
, NULL
, &unix_funcs
);
83 /* Copied from musl: src/internal/libm.h */
84 static inline float fp_barrierf(float x
)
90 static inline double fp_barrier(double x
)
92 volatile double y
= x
;
96 static inline double CDECL
ret_nan( BOOL update_sw
)
99 if (!update_sw
) return -NAN
;
100 return (x
- x
) / (x
- x
);
103 #define SET_X87_CW(MASK) \
104 "subl $4, %esp\n\t" \
105 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
106 "fnstcw (%esp)\n\t" \
107 "movw (%esp), %ax\n\t" \
108 "movw %ax, 2(%esp)\n\t" \
109 "testw $" #MASK ", %ax\n\t" \
111 "andw $~" #MASK ", %ax\n\t" \
112 "movw %ax, 2(%esp)\n\t" \
113 "fldcw 2(%esp)\n\t" \
116 #define RESET_X87_CW \
117 "movw (%esp), %ax\n\t" \
118 "cmpw %ax, 2(%esp)\n\t" \
120 "fstpl 8(%esp)\n\t" \
125 "addl $4, %esp\n\t" \
126 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
128 /*********************************************************************
129 * _matherr (CRTDLL.@)
131 int CDECL
_matherr(struct _exception
*e
)
137 static double math_error(int type
, const char *name
, double arg1
, double arg2
, double retval
)
139 struct _exception exception
= {type
, (char *)name
, arg1
, arg2
, retval
};
141 TRACE("(%d, %s, %g, %g, %g)\n", type
, debugstr_a(name
), arg1
, arg2
, retval
);
143 if (MSVCRT_default_matherr_func
&& MSVCRT_default_matherr_func(&exception
))
144 return exception
.retval
;
149 /* don't set errno */
159 /* don't set errno */
162 ERR("Unhandled math error!\n");
165 return exception
.retval
;
168 /*********************************************************************
169 * __setusermatherr (MSVCRT.@)
171 void CDECL
__setusermatherr(MSVCRT_matherr_func func
)
173 MSVCRT_default_matherr_func
= func
;
174 TRACE("new matherr handler %p\n", func
);
177 /*********************************************************************
178 * _set_SSE2_enable (MSVCRT.@)
180 int CDECL
_set_SSE2_enable(int flag
)
182 sse2_enabled
= flag
&& sse2_supported
;
188 /*********************************************************************
189 * _get_FMA3_enable (UCRTBASE.@)
191 int CDECL
_get_FMA3_enable(void)
199 /*********************************************************************
200 * _set_FMA3_enable (MSVCR120.@)
202 int CDECL
_set_FMA3_enable(int flag
)
204 FIXME("(%x) stub\n", flag
);
210 #if !defined(__i386__) || _MSVCR_VER>=120
212 /*********************************************************************
213 * _chgsignf (MSVCRT.@)
215 float CDECL
_chgsignf( float num
)
217 union { float f
; UINT32 i
; } u
= { num
};
222 /*********************************************************************
223 * _copysignf (MSVCRT.@)
225 * Copied from musl: src/math/copysignf.c
227 float CDECL
_copysignf( float x
, float y
)
229 union { float f
; UINT32 i
; } ux
= { x
}, uy
= { y
};
231 ux
.i
|= uy
.i
& 0x80000000;
235 /*********************************************************************
236 * _nextafterf (MSVCRT.@)
238 * Copied from musl: src/math/nextafterf.c
240 float CDECL
_nextafterf( float x
, float y
)
242 unsigned int ix
= *(unsigned int*)&x
;
243 unsigned int iy
= *(unsigned int*)&y
;
244 unsigned int ax
, ay
, e
;
246 if (isnan(x
) || isnan(y
))
249 if (_fpclassf(y
) & (_FPCLASS_ND
| _FPCLASS_PD
| _FPCLASS_NZ
| _FPCLASS_PZ
))
253 ax
= ix
& 0x7fffffff;
254 ay
= iy
& 0x7fffffff;
258 ix
= (iy
& 0x80000000) | 1;
259 } else if (ax
> ay
|| ((ix
^ iy
) & 0x80000000))
264 /* raise overflow if ix is infinite and x is finite */
265 if (e
== 0x7f800000) {
269 /* raise underflow if ix is subnormal or zero */
272 fp_barrierf(x
* x
+ y
* y
);
278 /*********************************************************************
281 float CDECL
_logbf( float num
)
283 float ret
= unix_funcs
->logbf(num
);
284 if (isnan(num
)) return math_error(_DOMAIN
, "_logbf", num
, 0, ret
);
285 if (!num
) return math_error(_SING
, "_logbf", num
, 0, ret
);
293 /*********************************************************************
294 * _fpclassf (MSVCRT.@)
296 int CDECL
_fpclassf( float num
)
298 union { float f
; UINT32 i
; } u
= { num
};
299 int e
= u
.i
>> 23 & 0xff;
305 if (u
.i
<< 1) return s
? _FPCLASS_ND
: _FPCLASS_PD
;
306 return s
? _FPCLASS_NZ
: _FPCLASS_PZ
;
308 if (u
.i
<< 9) return ((u
.i
>> 22) & 1) ? _FPCLASS_QNAN
: _FPCLASS_SNAN
;
309 return s
? _FPCLASS_NINF
: _FPCLASS_PINF
;
311 return s
? _FPCLASS_NN
: _FPCLASS_PN
;
315 /*********************************************************************
316 * _finitef (MSVCRT.@)
318 int CDECL
_finitef( float num
)
320 union { float f
; UINT32 i
; } u
= { num
};
321 return (u
.i
& 0x7fffffff) < 0x7f800000;
324 /*********************************************************************
327 int CDECL
_isnanf( float num
)
329 union { float f
; UINT32 i
; } u
= { num
};
330 return (u
.i
& 0x7fffffff) > 0x7f800000;
333 static float asinf_R(float z
)
335 /* coefficients for R(x^2) */
336 static const float p1
= 1.66666672e-01,
337 p2
= -5.11644611e-02,
338 p3
= -1.21124933e-02,
339 p4
= -3.58742251e-03,
340 q1
= -7.56982703e-01;
343 p
= z
* (p1
+ z
* (p2
+ z
* (p3
+ z
* p4
)));
348 /*********************************************************************
351 * Copied from musl: src/math/acosf.c
353 float CDECL
acosf( float x
)
355 static const double pio2_lo
= 6.12323399573676603587e-17;
357 float z
, w
, s
, c
, df
;
360 hx
= *(unsigned int*)&x
;
361 ix
= hx
& 0x7fffffff;
362 /* |x| >= 1 or nan */
363 if (ix
>= 0x3f800000) {
364 if (ix
== 0x3f800000) {
369 if (isnan(x
)) return x
;
370 return math_error(_DOMAIN
, "acosf", x
, 0, 0 / (x
- x
));
373 if (ix
< 0x3f000000) {
374 if (ix
<= 0x32800000) /* |x| < 2**-26 */
376 return M_PI_2
- (x
- (pio2_lo
- x
* asinf_R(x
* x
)));
382 return M_PI
- 2 * (s
+ ((double)s
* asinf_R(z
)));
387 hx
= *(unsigned int*)&s
& 0xffff0000;
389 c
= (z
- df
* df
) / (s
+ df
);
390 w
= asinf_R(z
) * s
+ c
;
394 /*********************************************************************
397 * Copied from musl: src/math/asinf.c
399 float CDECL
asinf( float x
)
401 static const double pio2
= 1.570796326794896558e+00;
402 static const float pio4_hi
= 0.785398125648;
403 static const float pio2_lo
= 7.54978941586e-08;
408 hx
= *(unsigned int*)&x
;
409 ix
= hx
& 0x7fffffff;
410 if (ix
>= 0x3f800000) { /* |x| >= 1 */
411 if (ix
== 0x3f800000) /* |x| == 1 */
412 return x
* pio2
+ 7.5231638453e-37; /* asin(+-1) = +-pi/2 with inexact */
413 if (isnan(x
)) return x
;
414 return math_error(_DOMAIN
, "asinf", x
, 0, 0 / (x
- x
));
416 if (ix
< 0x3f000000) { /* |x| < 0.5 */
417 /* if 0x1p-126 <= |x| < 0x1p-12, avoid raising underflow */
418 if (ix
< 0x39800000 && ix
>= 0x00800000)
420 return x
+ x
* asinf_R(x
* x
);
423 z
= (1 - fabsf(x
)) * 0.5f
;
426 *(unsigned int*)&f
= *(unsigned int*)&s
& 0xffff0000;
427 c
= (z
- f
* f
) / (s
+ f
);
428 x
= pio4_hi
- (2 * s
* asinf_R(z
) - (pio2_lo
- 2 * c
) - (pio4_hi
- 2 * f
));
434 /*********************************************************************
437 * Copied from musl: src/math/atanf.c
439 float CDECL
atanf( float x
)
441 static const float atanhi
[] = {
447 static const float atanlo
[] = {
453 static const float aT
[] = {
462 unsigned int ix
, sign
;
466 if (isnan(x
)) return math_error(_DOMAIN
, "atanf", x
, 0, x
);
469 ix
= *(unsigned int*)&x
;
472 if (ix
>= 0x4c800000) { /* if |x| >= 2**26 */
475 z
= atanhi
[3] + 7.5231638453e-37;
476 return sign
? -z
: z
;
478 if (ix
< 0x3ee00000) { /* |x| < 0.4375 */
479 if (ix
< 0x39800000) { /* |x| < 2**-12 */
481 /* raise underflow for subnormal x */
488 if (ix
< 0x3f980000) { /* |x| < 1.1875 */
489 if (ix
< 0x3f300000) { /* 7/16 <= |x| < 11/16 */
491 x
= (2.0f
* x
- 1.0f
) / (2.0f
+ x
);
492 } else { /* 11/16 <= |x| < 19/16 */
494 x
= (x
- 1.0f
) / (x
+ 1.0f
);
497 if (ix
< 0x401c0000) { /* |x| < 2.4375 */
499 x
= (x
- 1.5f
) / (1.0f
+ 1.5f
* x
);
500 } else { /* 2.4375 <= |x| < 2**26 */
506 /* end of argument reduction */
509 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
510 s1
= z
* (aT
[0] + w
* (aT
[2] + w
* aT
[4]));
511 s2
= w
* (aT
[1] + w
* aT
[3]);
513 return x
- x
* (s1
+ s2
);
514 z
= atanhi
[id
] - ((x
* (s1
+ s2
) - atanlo
[id
]) - x
);
515 return sign
? -z
: z
;
518 /*********************************************************************
521 * Copied from musl: src/math/atan2f.c
523 float CDECL
atan2f( float y
, float x
)
525 static const float pi
= 3.1415927410e+00,
526 pi_lo
= -8.7422776573e-08;
529 unsigned int m
, ix
, iy
;
531 if (isnan(x
) || isnan(y
))
533 ix
= *(unsigned int*)&x
;
534 iy
= *(unsigned int*)&y
;
535 if (ix
== 0x3f800000) /* x=1.0 */
537 m
= ((iy
>> 31) & 1) | ((ix
>> 30) & 2); /* 2*sign(x)+sign(y) */
545 case 1: return y
; /* atan(+-0,+anything)=+-0 */
546 case 2: return pi
; /* atan(+0,-anything) = pi */
547 case 3: return -pi
; /* atan(-0,-anything) =-pi */
552 return m
& 1 ? -pi
/ 2 : pi
/ 2;
554 if (ix
== 0x7f800000) {
555 if (iy
== 0x7f800000) {
557 case 0: return pi
/ 4; /* atan(+INF,+INF) */
558 case 1: return -pi
/ 4; /* atan(-INF,+INF) */
559 case 2: return 3 * pi
/ 4; /*atan(+INF,-INF)*/
560 case 3: return -3 * pi
/ 4; /*atan(-INF,-INF)*/
564 case 0: return 0.0f
; /* atan(+...,+INF) */
565 case 1: return -0.0f
; /* atan(-...,+INF) */
566 case 2: return pi
; /* atan(+...,-INF) */
567 case 3: return -pi
; /* atan(-...,-INF) */
572 if (ix
+ (26 << 23) < iy
|| iy
== 0x7f800000)
573 return m
& 1 ? -pi
/ 2 : pi
/ 2;
575 /* z = atan(|y/x|) with correct underflow */
576 if ((m
& 2) && iy
+ (26 << 23) < ix
) /*|y/x| < 0x1p-26, x < 0 */
579 z
= atanf(fabsf(y
/ x
));
581 case 0: return z
; /* atan(+,+) */
582 case 1: return -z
; /* atan(-,+) */
583 case 2: return pi
- (z
- pi_lo
); /* atan(+,-) */
584 default: /* case 3 */
585 return (z
- pi_lo
) - pi
; /* atan(-,-) */
589 /*********************************************************************
592 float CDECL
cosf( float x
)
594 float ret
= unix_funcs
->cosf( x
);
595 if (!isfinite(x
)) return math_error(_DOMAIN
, "cosf", x
, 0, ret
);
599 /*********************************************************************
602 float CDECL
coshf( float x
)
604 float ret
= unix_funcs
->coshf( x
);
605 if (isnan(x
)) return math_error(_DOMAIN
, "coshf", x
, 0, ret
);
609 /*********************************************************************
612 float CDECL
expf( float x
)
614 float ret
= unix_funcs
->expf( x
);
615 if (isnan(x
)) return math_error(_DOMAIN
, "expf", x
, 0, ret
);
616 if (isfinite(x
) && !ret
) return math_error(_UNDERFLOW
, "expf", x
, 0, ret
);
617 if (isfinite(x
) && !isfinite(ret
)) return math_error(_OVERFLOW
, "expf", x
, 0, ret
);
621 /*********************************************************************
624 * Copied from musl: src/math/fmodf.c
626 float CDECL
fmodf( float x
, float y
)
628 UINT32 xi
= *(UINT32
*)&x
;
629 UINT32 yi
= *(UINT32
*)&y
;
630 int ex
= xi
>>23 & 0xff;
631 int ey
= yi
>>23 & 0xff;
632 UINT32 sx
= xi
& 0x80000000;
635 if (isinf(x
)) return math_error(_DOMAIN
, "fmodf", x
, y
, (x
* y
) / (x
* y
));
636 if (yi
<< 1 == 0 || isnan(y
) || ex
== 0xff)
637 return (x
* y
) / (x
* y
);
638 if (xi
<< 1 <= yi
<< 1) {
639 if (xi
<< 1 == yi
<< 1)
644 /* normalize x and y */
646 for (i
= xi
<< 9; i
>> 31 == 0; ex
--, i
<<= 1);
653 for (i
= yi
<< 9; i
>> 31 == 0; ey
--, i
<<= 1);
661 for (; ex
> ey
; ex
--) {
676 for (; xi
>>23 == 0; xi
<<= 1, ex
--);
678 /* scale result up */
681 xi
|= (UINT32
)ex
<< 23;
689 /*********************************************************************
692 float CDECL
logf( float x
)
694 float ret
= unix_funcs
->logf( x
);
695 if (x
< 0.0) return math_error(_DOMAIN
, "logf", x
, 0, ret
);
696 if (x
== 0.0) return math_error(_SING
, "logf", x
, 0, ret
);
700 /*********************************************************************
703 float CDECL
log10f( float x
)
705 float ret
= unix_funcs
->log10f( x
);
706 if (x
< 0.0) return math_error(_DOMAIN
, "log10f", x
, 0, ret
);
707 if (x
== 0.0) return math_error(_SING
, "log10f", x
, 0, ret
);
711 /*********************************************************************
714 float CDECL
powf( float x
, float y
)
716 float z
= unix_funcs
->powf(x
,y
);
717 if (x
< 0 && y
!= floorf(y
)) return math_error(_DOMAIN
, "powf", x
, y
, z
);
718 if (!x
&& isfinite(y
) && y
< 0) return math_error(_SING
, "powf", x
, y
, z
);
719 if (isfinite(x
) && isfinite(y
) && !isfinite(z
)) return math_error(_OVERFLOW
, "powf", x
, y
, z
);
720 if (x
&& isfinite(x
) && isfinite(y
) && !z
) return math_error(_UNDERFLOW
, "powf", x
, y
, z
);
724 /*********************************************************************
727 float CDECL
sinf( float x
)
729 float ret
= unix_funcs
->sinf( x
);
730 if (!isfinite(x
)) return math_error(_DOMAIN
, "sinf", x
, 0, ret
);
734 /*********************************************************************
737 float CDECL
sinhf( float x
)
739 float ret
= unix_funcs
->sinhf( x
);
740 if (isnan(x
)) return math_error(_DOMAIN
, "sinhf", x
, 0, ret
);
744 static BOOL
sqrtf_validate( float *x
)
746 short c
= _fdclass(*x
);
748 if (c
== FP_ZERO
) return FALSE
;
749 if (c
== FP_NAN
) return FALSE
;
752 *x
= math_error(_DOMAIN
, "sqrtf", *x
, 0, ret_nan(TRUE
));
755 if (c
== FP_INFINITE
) return FALSE
;
759 #if defined(__x86_64__) || defined(__i386__)
760 float CDECL
sse2_sqrtf(float);
761 __ASM_GLOBAL_FUNC( sse2_sqrtf
,
762 "sqrtss %xmm0, %xmm0\n\t"
766 /*********************************************************************
769 * Copied from musl: src/math/sqrtf.c
771 float CDECL
sqrtf( float x
)
774 if (!sqrtf_validate(&x
))
777 return sse2_sqrtf(x
);
779 static const float tiny
= 1.0e-30;
787 if (!sqrtf_validate(&x
))
792 if (m
== 0) { /* subnormal x */
793 for (i
= 0; (ix
& 0x00800000) == 0; i
++)
797 m
-= 127; /* unbias exponent */
798 ix
= (ix
& 0x007fffff) | 0x00800000;
799 if (m
& 1) /* odd m, double x to make it even */
801 m
>>= 1; /* m = [m/2] */
803 /* generate sqrt(x) bit by bit */
805 q
= s
= 0; /* q = sqrt(x) */
806 r
= 0x01000000; /* r = moving bit from right to left */
819 /* use floating add to find out rounding direction */
821 z
= 1.0f
- tiny
; /* raise inexact flag */
830 ix
= (q
>> 1) + 0x3f000000;
831 r
= ix
+ ((unsigned int)m
<< 23);
837 /*********************************************************************
840 float CDECL
tanf( float x
)
842 float ret
= unix_funcs
->tanf(x
);
843 if (!isfinite(x
)) return math_error(_DOMAIN
, "tanf", x
, 0, ret
);
847 /*********************************************************************
850 float CDECL
tanhf( float x
)
852 float ret
= unix_funcs
->tanhf(x
);
853 if (!isfinite(x
)) return math_error(_DOMAIN
, "tanhf", x
, 0, ret
);
857 /*********************************************************************
860 * Copied from musl: src/math/ceilf.c
862 float CDECL
ceilf( float x
)
864 union {float f
; UINT32 i
;} u
= {x
};
865 int e
= (int)(u
.i
>> 23 & 0xff) - 0x7f;
886 /*********************************************************************
889 * Copied from musl: src/math/floorf.c
891 float CDECL
floorf( float x
)
893 union {float f
; UINT32 i
;} u
= {x
};
894 int e
= (int)(u
.i
>> 23 & 0xff) - 0x7f;
915 /*********************************************************************
918 float CDECL
frexpf( float x
, int *exp
)
920 return unix_funcs
->frexpf( x
, exp
);
923 /*********************************************************************
926 * Copied from musl: src/math/modff.c
928 float CDECL
modff( float x
, float *iptr
)
930 union {float f
; UINT32 i
;} u
= {x
};
932 int e
= (u
.i
>> 23 & 0xff) - 0x7f;
934 /* no fractional part */
937 if (e
== 0x80 && u
.i
<< 9 != 0) { /* nan */
943 /* no integral part */
950 mask
= 0x007fffff >> e
;
951 if ((u
.i
& mask
) == 0) {
963 #if !defined(__i386__) && !defined(__x86_64__) && (_MSVCR_VER == 0 || _MSVCR_VER >= 110)
965 /*********************************************************************
968 * Copied from musl: src/math/fabsf.c
970 float CDECL
fabsf( float x
)
972 union { float f
; UINT32 i
; } u
= { x
};
979 /*********************************************************************
982 * Copied from musl: src/math/acos.c
984 static double acos_R(double z
)
986 static const double pS0
= 1.66666666666666657415e-01,
987 pS1
= -3.25565818622400915405e-01,
988 pS2
= 2.01212532134862925881e-01,
989 pS3
= -4.00555345006794114027e-02,
990 pS4
= 7.91534994289814532176e-04,
991 pS5
= 3.47933107596021167570e-05,
992 qS1
= -2.40339491173441421878e+00,
993 qS2
= 2.02094576023350569471e+00,
994 qS3
= -6.88283971605453293030e-01,
995 qS4
= 7.70381505559019352791e-02;
998 p
= z
* (pS0
+ z
* (pS1
+ z
* (pS2
+ z
* (pS3
+ z
* (pS4
+ z
* pS5
)))));
999 q
= 1.0 + z
* (qS1
+ z
* (qS2
+ z
* (qS3
+ z
* qS4
)));
1003 double CDECL
acos( double x
)
1005 static const double pio2_hi
= 1.57079632679489655800e+00,
1006 pio2_lo
= 6.12323399573676603587e-17;
1008 double z
, w
, s
, c
, df
;
1009 unsigned int hx
, ix
;
1012 hx
= *(ULONGLONG
*)&x
>> 32;
1013 ix
= hx
& 0x7fffffff;
1014 /* |x| >= 1 or nan */
1015 if (ix
>= 0x3ff00000) {
1018 lx
= *(ULONGLONG
*)&x
;
1019 if (((ix
- 0x3ff00000) | lx
) == 0) {
1020 /* acos(1)=0, acos(-1)=pi */
1022 return 2 * pio2_hi
+ 7.5231638452626401e-37;
1025 if (isnan(x
)) return x
;
1026 return math_error(_DOMAIN
, "acos", x
, 0, 0 / (x
- x
));
1029 if (ix
< 0x3fe00000) {
1030 if (ix
<= 0x3c600000) /* |x| < 2**-57 */
1031 return pio2_hi
+ 7.5231638452626401e-37;
1032 return pio2_hi
- (x
- (pio2_lo
- x
* acos_R(x
* x
)));
1036 z
= (1.0 + x
) * 0.5;
1038 w
= acos_R(z
) * s
- pio2_lo
;
1039 return 2 * (pio2_hi
- (s
+ w
));
1042 z
= (1.0 - x
) * 0.5;
1045 llx
= (*(ULONGLONG
*)&df
>> 32) << 32;
1046 df
= *(double*)&llx
;
1047 c
= (z
- df
* df
) / (s
+ df
);
1048 w
= acos_R(z
) * s
+ c
;
1049 return 2 * (df
+ w
);
1052 /*********************************************************************
1055 * Copied from musl: src/math/asin.c
1057 static double asin_R(double z
)
1059 /* coefficients for R(x^2) */
1060 static const double pS0
= 1.66666666666666657415e-01,
1061 pS1
= -3.25565818622400915405e-01,
1062 pS2
= 2.01212532134862925881e-01,
1063 pS3
= -4.00555345006794114027e-02,
1064 pS4
= 7.91534994289814532176e-04,
1065 pS5
= 3.47933107596021167570e-05,
1066 qS1
= -2.40339491173441421878e+00,
1067 qS2
= 2.02094576023350569471e+00,
1068 qS3
= -6.88283971605453293030e-01,
1069 qS4
= 7.70381505559019352791e-02;
1072 p
= z
* (pS0
+ z
* (pS1
+ z
* (pS2
+ z
* (pS3
+ z
* (pS4
+ z
* pS5
)))));
1073 q
= 1.0 + z
* (qS1
+ z
* (qS2
+ z
* (qS3
+ z
* qS4
)));
1078 double CDECL
x87_asin(double);
1079 __ASM_GLOBAL_FUNC( x87_asin
,
1094 double CDECL
asin( double x
)
1096 static const double pio2_hi
= 1.57079632679489655800e+00,
1097 pio2_lo
= 6.12323399573676603587e-17;
1100 unsigned int hx
, ix
;
1103 unsigned int x87_cw
, sse2_cw
;
1106 hx
= *(ULONGLONG
*)&x
>> 32;
1107 ix
= hx
& 0x7fffffff;
1108 /* |x| >= 1 or nan */
1109 if (ix
>= 0x3ff00000) {
1111 lx
= *(ULONGLONG
*)&x
;
1112 if (((ix
- 0x3ff00000) | lx
) == 0)
1113 /* asin(1) = +-pi/2 with inexact */
1114 return x
* pio2_hi
+ 7.5231638452626401e-37;
1118 return math_error(_DOMAIN
, "asin", x
, 0, x
);
1123 return math_error(_DOMAIN
, "asin", x
, 0, 0 / (x
- x
));
1127 __control87_2(0, 0, &x87_cw
, &sse2_cw
);
1128 if (!sse2_enabled
|| (x87_cw
& _MCW_EM
) != _MCW_EM
1129 || (sse2_cw
& (_MCW_EM
| _MCW_RC
)) != _MCW_EM
)
1134 if (ix
< 0x3fe00000) {
1135 /* if 0x1p-1022 <= |x| < 0x1p-26, avoid raising underflow */
1136 if (ix
< 0x3e500000 && ix
>= 0x00100000)
1138 return x
+ x
* asin_R(x
* x
);
1140 /* 1 > |x| >= 0.5 */
1141 z
= (1 - fabs(x
)) * 0.5;
1144 if (ix
>= 0x3fef3333) { /* if |x| > 0.975 */
1145 x
= pio2_hi
- (2 * (s
+ s
* r
) - pio2_lo
);
1150 llx
= (*(ULONGLONG
*)&f
>> 32) << 32;
1152 c
= (z
- f
* f
) / (s
+ f
);
1153 x
= 0.5 * pio2_hi
- (2 * s
* r
- (pio2_lo
- 2 * c
) - (0.5 * pio2_hi
- 2 * f
));
1160 /*********************************************************************
1163 * Copied from musl: src/math/atan.c
1165 double CDECL
atan( double x
)
1167 static const double atanhi
[] = {
1168 4.63647609000806093515e-01,
1169 7.85398163397448278999e-01,
1170 9.82793723247329054082e-01,
1171 1.57079632679489655800e+00,
1173 static const double atanlo
[] = {
1174 2.26987774529616870924e-17,
1175 3.06161699786838301793e-17,
1176 1.39033110312309984516e-17,
1177 6.12323399573676603587e-17,
1179 static const double aT
[] = {
1180 3.33333333333329318027e-01,
1181 -1.99999999998764832476e-01,
1182 1.42857142725034663711e-01,
1183 -1.11111104054623557880e-01,
1184 9.09088713343650656196e-02,
1185 -7.69187620504482999495e-02,
1186 6.66107313738753120669e-02,
1187 -5.83357013379057348645e-02,
1188 4.97687799461593236017e-02,
1189 -3.65315727442169155270e-02,
1190 1.62858201153657823623e-02,
1193 double w
, s1
, s2
, z
;
1194 unsigned int ix
, sign
;
1198 if (isnan(x
)) return math_error(_DOMAIN
, "atan", x
, 0, x
);
1201 ix
= *(ULONGLONG
*)&x
>> 32;
1204 if (ix
>= 0x44100000) { /* if |x| >= 2^66 */
1207 z
= atanhi
[3] + 7.5231638452626401e-37;
1208 return sign
? -z
: z
;
1210 if (ix
< 0x3fdc0000) { /* |x| < 0.4375 */
1211 if (ix
< 0x3e400000) { /* |x| < 2^-27 */
1212 if (ix
< 0x00100000)
1213 /* raise underflow for subnormal x */
1214 fp_barrierf((float)x
);
1220 if (ix
< 0x3ff30000) { /* |x| < 1.1875 */
1221 if (ix
< 0x3fe60000) { /* 7/16 <= |x| < 11/16 */
1223 x
= (2.0 * x
- 1.0) / (2.0 + x
);
1224 } else { /* 11/16 <= |x| < 19/16 */
1226 x
= (x
- 1.0) / (x
+ 1.0);
1229 if (ix
< 0x40038000) { /* |x| < 2.4375 */
1231 x
= (x
- 1.5) / (1.0 + 1.5 * x
);
1232 } else { /* 2.4375 <= |x| < 2^66 */
1238 /* end of argument reduction */
1241 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
1242 s1
= z
* (aT
[0] + w
* (aT
[2] + w
* (aT
[4] + w
* (aT
[6] + w
* (aT
[8] + w
* aT
[10])))));
1243 s2
= w
* (aT
[1] + w
* (aT
[3] + w
* (aT
[5] + w
* (aT
[7] + w
* aT
[9]))));
1245 return x
- x
* (s1
+ s2
);
1246 z
= atanhi
[id
] - (x
* (s1
+ s2
) - atanlo
[id
] - x
);
1247 return sign
? -z
: z
;
1250 /*********************************************************************
1253 * Copied from musl: src/math/atan2.c
1255 double CDECL
atan2( double y
, double x
)
1257 static const double pi
= 3.1415926535897931160E+00,
1258 pi_lo
= 1.2246467991473531772E-16;
1261 unsigned int m
, lx
, ly
, ix
, iy
;
1263 if (isnan(x
) || isnan(y
))
1265 ix
= *(ULONGLONG
*)&x
>> 32;
1266 lx
= *(ULONGLONG
*)&x
;
1267 iy
= *(ULONGLONG
*)&y
>> 32;
1268 ly
= *(ULONGLONG
*)&y
;
1269 if (((ix
- 0x3ff00000) | lx
) == 0) /* x = 1.0 */
1271 m
= ((iy
>> 31) & 1) | ((ix
>> 30) & 2); /* 2*sign(x)+sign(y) */
1272 ix
= ix
& 0x7fffffff;
1273 iy
= iy
& 0x7fffffff;
1276 if ((iy
| ly
) == 0) {
1279 case 1: return y
; /* atan(+-0,+anything)=+-0 */
1280 case 2: return pi
; /* atan(+0,-anything) = pi */
1281 case 3: return -pi
; /* atan(-0,-anything) =-pi */
1286 return m
& 1 ? -pi
/ 2 : pi
/ 2;
1288 if (ix
== 0x7ff00000) {
1289 if (iy
== 0x7ff00000) {
1291 case 0: return pi
/ 4; /* atan(+INF,+INF) */
1292 case 1: return -pi
/ 4; /* atan(-INF,+INF) */
1293 case 2: return 3 * pi
/ 4; /* atan(+INF,-INF) */
1294 case 3: return -3 * pi
/ 4; /* atan(-INF,-INF) */
1298 case 0: return 0.0; /* atan(+...,+INF) */
1299 case 1: return -0.0; /* atan(-...,+INF) */
1300 case 2: return pi
; /* atan(+...,-INF) */
1301 case 3: return -pi
; /* atan(-...,-INF) */
1305 /* |y/x| > 0x1p64 */
1306 if (ix
+ (64 << 20) < iy
|| iy
== 0x7ff00000)
1307 return m
& 1 ? -pi
/ 2 : pi
/ 2;
1309 /* z = atan(|y/x|) without spurious underflow */
1310 if ((m
& 2) && iy
+ (64 << 20) < ix
) /* |y/x| < 0x1p-64, x<0 */
1313 z
= atan(fabs(y
/ x
));
1315 case 0: return z
; /* atan(+,+) */
1316 case 1: return -z
; /* atan(-,+) */
1317 case 2: return pi
- (z
- pi_lo
); /* atan(+,-) */
1318 default: /* case 3 */
1319 return (z
- pi_lo
) - pi
; /* atan(-,-) */
1323 /*********************************************************************
1326 double CDECL
cos( double x
)
1328 double ret
= unix_funcs
->cos( x
);
1329 if (!isfinite(x
)) return math_error(_DOMAIN
, "cos", x
, 0, ret
);
1333 /*********************************************************************
1336 double CDECL
cosh( double x
)
1338 double ret
= unix_funcs
->cosh( x
);
1339 if (isnan(x
)) return math_error(_DOMAIN
, "cosh", x
, 0, ret
);
1343 /*********************************************************************
1346 double CDECL
exp( double x
)
1348 double ret
= unix_funcs
->exp( x
);
1349 if (isnan(x
)) return math_error(_DOMAIN
, "exp", x
, 0, ret
);
1350 if (isfinite(x
) && !ret
) return math_error(_UNDERFLOW
, "exp", x
, 0, ret
);
1351 if (isfinite(x
) && !isfinite(ret
)) return math_error(_OVERFLOW
, "exp", x
, 0, ret
);
1355 /*********************************************************************
1358 * Copied from musl: src/math/fmod.c
1360 double CDECL
fmod( double x
, double y
)
1362 UINT64 xi
= *(UINT64
*)&x
;
1363 UINT64 yi
= *(UINT64
*)&y
;
1364 int ex
= xi
>> 52 & 0x7ff;
1365 int ey
= yi
>> 52 & 0x7ff;
1369 if (isinf(x
)) return math_error(_DOMAIN
, "fmod", x
, y
, (x
* y
) / (x
* y
));
1370 if (yi
<< 1 == 0 || isnan(y
) || ex
== 0x7ff)
1371 return (x
* y
) / (x
* y
);
1372 if (xi
<< 1 <= yi
<< 1) {
1373 if (xi
<< 1 == yi
<< 1)
1378 /* normalize x and y */
1380 for (i
= xi
<< 12; i
>> 63 == 0; ex
--, i
<<= 1);
1387 for (i
= yi
<< 12; i
>> 63 == 0; ey
--, i
<<= 1);
1395 for (; ex
> ey
; ex
--) {
1410 for (; xi
>> 52 == 0; xi
<<= 1, ex
--);
1415 xi
|= (UINT64
)ex
<< 52;
1419 xi
|= (UINT64
)sx
<< 63;
1420 return *(double*)&xi
;
1423 /*********************************************************************
1426 double CDECL
log( double x
)
1428 double ret
= unix_funcs
->log( x
);
1429 if (x
< 0.0) return math_error(_DOMAIN
, "log", x
, 0, ret
);
1430 if (x
== 0.0) return math_error(_SING
, "log", x
, 0, ret
);
1434 /*********************************************************************
1437 double CDECL
log10( double x
)
1439 double ret
= unix_funcs
->log10( x
);
1440 if (x
< 0.0) return math_error(_DOMAIN
, "log10", x
, 0, ret
);
1441 if (x
== 0.0) return math_error(_SING
, "log10", x
, 0, ret
);
1445 /*********************************************************************
1448 double CDECL
pow( double x
, double y
)
1450 double z
= unix_funcs
->pow(x
,y
);
1451 if (x
< 0 && y
!= floor(y
))
1452 return math_error(_DOMAIN
, "pow", x
, y
, z
);
1453 if (!x
&& isfinite(y
) && y
< 0)
1454 return math_error(_SING
, "pow", x
, y
, z
);
1455 if (isfinite(x
) && isfinite(y
) && !isfinite(z
))
1456 return math_error(_OVERFLOW
, "pow", x
, y
, z
);
1457 if (x
&& isfinite(x
) && isfinite(y
) && !z
)
1458 return math_error(_UNDERFLOW
, "pow", x
, y
, z
);
1462 /*********************************************************************
1465 double CDECL
sin( double x
)
1467 double ret
= unix_funcs
->sin( x
);
1468 if (!isfinite(x
)) return math_error(_DOMAIN
, "sin", x
, 0, ret
);
1472 /*********************************************************************
1475 double CDECL
sinh( double x
)
1477 double ret
= unix_funcs
->sinh( x
);
1478 if (isnan(x
)) return math_error(_DOMAIN
, "sinh", x
, 0, ret
);
1482 static BOOL
sqrt_validate( double *x
, BOOL update_sw
)
1484 short c
= _dclass(*x
);
1486 if (c
== FP_ZERO
) return FALSE
;
1491 *x
= math_error(_DOMAIN
, "sqrt", *x
, 0, *x
);
1493 /* set signaling bit */
1494 *(ULONGLONG
*)x
|= 0x8000000000000ULL
;
1500 *x
= math_error(_DOMAIN
, "sqrt", *x
, 0, ret_nan(update_sw
));
1503 if (c
== FP_INFINITE
) return FALSE
;
1507 #if defined(__x86_64__) || defined(__i386__)
1508 double CDECL
sse2_sqrt(double);
1509 __ASM_GLOBAL_FUNC( sse2_sqrt
,
1510 "sqrtsd %xmm0, %xmm0\n\t"
1515 double CDECL
x87_sqrt(double);
1516 __ASM_GLOBAL_FUNC( x87_sqrt
,
1524 /*********************************************************************
1527 * Copied from musl: src/math/sqrt.c
1529 double CDECL
sqrt( double x
)
1532 if (!sqrt_validate(&x
, TRUE
))
1535 return sse2_sqrt(x
);
1536 #elif defined( __i386__ )
1537 if (!sqrt_validate(&x
, TRUE
))
1542 static const double tiny
= 1.0e-300;
1545 int sign
= 0x80000000;
1547 unsigned int r
,t1
,s1
,ix1
,q1
;
1550 if (!sqrt_validate(&x
, TRUE
))
1553 ix
= *(ULONGLONG
*)&x
;
1559 if (m
== 0) { /* subnormal x */
1565 for (i
=0; (ix0
& 0x00100000) == 0; i
++)
1568 ix0
|= ix1
>> (32 - i
);
1571 m
-= 1023; /* unbias exponent */
1572 ix0
= (ix0
& 0x000fffff) | 0x00100000;
1573 if (m
& 1) { /* odd m, double x to make it even */
1574 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1577 m
>>= 1; /* m = [m/2] */
1579 /* generate sqrt(x) bit by bit */
1580 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1582 q
= q1
= s0
= s1
= 0; /* [q,q1] = sqrt(x) */
1583 r
= 0x00200000; /* r = moving bit from right to left */
1592 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1601 if (t
< ix0
|| (t
== ix0
&& t1
<= ix1
)) {
1603 if ((t1
&sign
) == sign
&& (s1
& sign
) == 0)
1611 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1616 /* use floating add to find out rounding direction */
1617 if ((ix0
| ix1
) != 0) {
1618 z
= 1.0 - tiny
; /* raise inexact flag */
1621 if (q1
== (unsigned int)0xffffffff) {
1624 } else if (z
> 1.0) {
1625 if (q1
== (unsigned int)0xfffffffe)
1632 ix0
= (q
>> 1) + 0x3fe00000;
1636 ix
= ix0
+ ((unsigned int)m
<< 20);
1639 return *(double*)&ix
;
1643 /*********************************************************************
1646 double CDECL
tan( double x
)
1648 double ret
= unix_funcs
->tan(x
);
1649 if (!isfinite(x
)) return math_error(_DOMAIN
, "tan", x
, 0, ret
);
1653 /*********************************************************************
1656 double CDECL
tanh( double x
)
1658 double ret
= unix_funcs
->tanh(x
);
1659 if (isnan(x
)) return math_error(_DOMAIN
, "tanh", x
, 0, ret
);
1664 #if (defined(__GNUC__) || defined(__clang__)) && defined(__i386__)
1666 #define CREATE_FPU_FUNC1(name, call) \
1667 __ASM_GLOBAL_FUNC(name, \
1669 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1670 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1671 "movl %esp, %ebp\n\t" \
1672 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1673 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1674 "fstpl (%esp)\n\t" /* store function argument */ \
1676 "movl $1, %ecx\n\t" /* empty FPU stack */ \
1680 "and $0x4500, %ax\n\t" \
1681 "cmp $0x4100, %ax\n\t" \
1683 "fstpl (%esp,%ecx,8)\n\t" \
1688 "movl %ecx, -4(%ebp)\n\t" \
1689 "call " __ASM_NAME( #call ) "\n\t" \
1690 "movl -4(%ebp), %ecx\n\t" \
1691 "fstpl (%esp)\n\t" /* save result */ \
1692 "3:\n\t" /* restore FPU stack */ \
1694 "fldl (%esp,%ecx,8)\n\t" \
1695 "cmpl $0, %ecx\n\t" \
1698 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1699 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1702 #define CREATE_FPU_FUNC2(name, call) \
1703 __ASM_GLOBAL_FUNC(name, \
1705 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1706 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1707 "movl %esp, %ebp\n\t" \
1708 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1709 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1710 "fstpl 8(%esp)\n\t" /* store function argument */ \
1712 "fstpl (%esp)\n\t" \
1714 "movl $2, %ecx\n\t" /* empty FPU stack */ \
1718 "and $0x4500, %ax\n\t" \
1719 "cmp $0x4100, %ax\n\t" \
1721 "fstpl (%esp,%ecx,8)\n\t" \
1726 "movl %ecx, -4(%ebp)\n\t" \
1727 "call " __ASM_NAME( #call ) "\n\t" \
1728 "movl -4(%ebp), %ecx\n\t" \
1729 "fstpl 8(%esp)\n\t" /* save result */ \
1730 "3:\n\t" /* restore FPU stack */ \
1732 "fldl (%esp,%ecx,8)\n\t" \
1733 "cmpl $1, %ecx\n\t" \
1736 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1737 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1740 CREATE_FPU_FUNC1(_CIacos
, acos
)
1741 CREATE_FPU_FUNC1(_CIasin
, asin
)
1742 CREATE_FPU_FUNC1(_CIatan
, atan
)
1743 CREATE_FPU_FUNC2(_CIatan2
, atan2
)
1744 CREATE_FPU_FUNC1(_CIcos
, cos
)
1745 CREATE_FPU_FUNC1(_CIcosh
, cosh
)
1746 CREATE_FPU_FUNC1(_CIexp
, exp
)
1747 CREATE_FPU_FUNC2(_CIfmod
, fmod
)
1748 CREATE_FPU_FUNC1(_CIlog
, log
)
1749 CREATE_FPU_FUNC1(_CIlog10
, log10
)
1750 CREATE_FPU_FUNC2(_CIpow
, pow
)
1751 CREATE_FPU_FUNC1(_CIsin
, sin
)
1752 CREATE_FPU_FUNC1(_CIsinh
, sinh
)
1753 CREATE_FPU_FUNC1(_CIsqrt
, sqrt
)
1754 CREATE_FPU_FUNC1(_CItan
, tan
)
1755 CREATE_FPU_FUNC1(_CItanh
, tanh
)
1757 __ASM_GLOBAL_FUNC(_ftol
,
1759 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1760 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1761 "movl %esp, %ebp\n\t"
1762 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1763 "subl $12, %esp\n\t" /* sizeof(LONGLONG) + 2*sizeof(WORD) */
1765 "mov (%esp), %ax\n\t"
1766 "or $0xc00, %ax\n\t"
1767 "mov %ax, 2(%esp)\n\t"
1769 "fistpq 4(%esp)\n\t"
1771 "movl 4(%esp), %eax\n\t"
1772 "movl 8(%esp), %edx\n\t"
1774 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1775 __ASM_CFI(".cfi_same_value %ebp\n\t")
1778 #endif /* (defined(__GNUC__) || defined(__clang__)) && defined(__i386__) */
1780 /*********************************************************************
1781 * _fpclass (MSVCRT.@)
1783 int CDECL
_fpclass(double num
)
1785 union { double f
; UINT64 i
; } u
= { num
};
1786 int e
= u
.i
>> 52 & 0x7ff;
1792 if (u
.i
<< 1) return s
? _FPCLASS_ND
: _FPCLASS_PD
;
1793 return s
? _FPCLASS_NZ
: _FPCLASS_PZ
;
1795 if (u
.i
<< 12) return ((u
.i
>> 51) & 1) ? _FPCLASS_QNAN
: _FPCLASS_SNAN
;
1796 return s
? _FPCLASS_NINF
: _FPCLASS_PINF
;
1798 return s
? _FPCLASS_NN
: _FPCLASS_PN
;
1802 /*********************************************************************
1805 unsigned int CDECL
MSVCRT__rotl(unsigned int num
, int shift
)
1808 return (num
<< shift
) | (num
>> (32-shift
));
1811 /*********************************************************************
1814 __msvcrt_ulong CDECL
MSVCRT__lrotl(__msvcrt_ulong num
, int shift
)
1817 return (num
<< shift
) | (num
>> (32-shift
));
1820 /*********************************************************************
1823 __msvcrt_ulong CDECL
MSVCRT__lrotr(__msvcrt_ulong num
, int shift
)
1826 return (num
>> shift
) | (num
<< (32-shift
));
1829 /*********************************************************************
1832 unsigned int CDECL
MSVCRT__rotr(unsigned int num
, int shift
)
1835 return (num
>> shift
) | (num
<< (32-shift
));
1838 /*********************************************************************
1839 * _rotl64 (MSVCRT.@)
1841 unsigned __int64 CDECL
MSVCRT__rotl64(unsigned __int64 num
, int shift
)
1844 return (num
<< shift
) | (num
>> (64-shift
));
1847 /*********************************************************************
1848 * _rotr64 (MSVCRT.@)
1850 unsigned __int64 CDECL
MSVCRT__rotr64(unsigned __int64 num
, int shift
)
1853 return (num
>> shift
) | (num
<< (64-shift
));
1856 /*********************************************************************
1859 int CDECL
abs( int n
)
1861 return n
>= 0 ? n
: -n
;
1864 /*********************************************************************
1867 __msvcrt_long CDECL
labs( __msvcrt_long n
)
1869 return n
>= 0 ? n
: -n
;
1873 /*********************************************************************
1874 * llabs (MSVCR100.@)
1876 __int64 CDECL
llabs( __int64 n
)
1878 return n
>= 0 ? n
: -n
;
1883 /*********************************************************************
1884 * imaxabs (MSVCR120.@)
1886 intmax_t CDECL
imaxabs( intmax_t n
)
1888 return n
>= 0 ? n
: -n
;
1892 /*********************************************************************
1895 __int64 CDECL
_abs64( __int64 n
)
1897 return n
>= 0 ? n
: -n
;
1900 /*********************************************************************
1903 double CDECL
_logb(double num
)
1905 double ret
= unix_funcs
->logb(num
);
1906 if (isnan(num
)) return math_error(_DOMAIN
, "_logb", num
, 0, ret
);
1907 if (!num
) return math_error(_SING
, "_logb", num
, 0, ret
);
1911 /*********************************************************************
1914 double CDECL
_hypot(double x
, double y
)
1916 /* FIXME: errno handling */
1917 return unix_funcs
->hypot( x
, y
);
1920 /*********************************************************************
1921 * _hypotf (MSVCRT.@)
1923 float CDECL
_hypotf(float x
, float y
)
1925 /* FIXME: errno handling */
1926 return unix_funcs
->hypotf( x
, y
);
1929 /*********************************************************************
1932 * Based on musl: src/math/ceilf.c
1934 double CDECL
ceil( double x
)
1936 union {double f
; UINT64 i
;} u
= {x
};
1937 int e
= (u
.i
>> 52 & 0x7ff) - 0x3ff;
1943 m
= 0x000fffffffffffffULL
>> e
;
1958 /*********************************************************************
1961 * Based on musl: src/math/floorf.c
1963 double CDECL
floor( double x
)
1965 union {double f
; UINT64 i
;} u
= {x
};
1966 int e
= (int)(u
.i
>> 52 & 0x7ff) - 0x3ff;
1972 m
= 0x000fffffffffffffULL
>> e
;
1987 /*********************************************************************
1990 double CDECL
fma( double x
, double y
, double z
)
1992 double w
= unix_funcs
->fma(x
, y
, z
);
1993 if ((isinf(x
) && y
== 0) || (x
== 0 && isinf(y
))) *_errno() = EDOM
;
1994 else if (isinf(x
) && isinf(z
) && x
!= z
) *_errno() = EDOM
;
1995 else if (isinf(y
) && isinf(z
) && y
!= z
) *_errno() = EDOM
;
1999 /*********************************************************************
2002 float CDECL
fmaf( float x
, float y
, float z
)
2004 float w
= unix_funcs
->fmaf(x
, y
, z
);
2005 if ((isinf(x
) && y
== 0) || (x
== 0 && isinf(y
))) *_errno() = EDOM
;
2006 else if (isinf(x
) && isinf(z
) && x
!= z
) *_errno() = EDOM
;
2007 else if (isinf(y
) && isinf(z
) && y
!= z
) *_errno() = EDOM
;
2011 /*********************************************************************
2014 * Copied from musl: src/math/fabsf.c
2016 double CDECL
fabs( double x
)
2018 union { double f
; UINT64 i
; } u
= { x
};
2023 /*********************************************************************
2026 double CDECL
frexp( double x
, int *exp
)
2028 return unix_funcs
->frexp( x
, exp
);
2031 /*********************************************************************
2034 * Copied from musl: src/math/modf.c
2036 double CDECL
modf( double x
, double *iptr
)
2038 union {double f
; UINT64 i
;} u
= {x
};
2040 int e
= (u
.i
>> 52 & 0x7ff) - 0x3ff;
2042 /* no fractional part */
2045 if (e
== 0x400 && u
.i
<< 12 != 0) /* nan */
2051 /* no integral part*/
2058 mask
= -1ULL >> 12 >> e
;
2059 if ((u
.i
& mask
) == 0) {
2069 /**********************************************************************
2070 * _statusfp2 (MSVCRT.@)
2072 * Not exported by native msvcrt, added in msvcr80.
2074 #if defined(__i386__) || defined(__x86_64__)
2075 void CDECL
_statusfp2( unsigned int *x86_sw
, unsigned int *sse2_sw
)
2077 #if defined(__GNUC__) || defined(__clang__)
2079 unsigned long fpword
;
2083 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) );
2085 if (fpword
& 0x1) flags
|= _SW_INVALID
;
2086 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
2087 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
2088 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
2089 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
2090 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
2094 if (!sse2_sw
) return;
2098 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2100 if (fpword
& 0x1) flags
|= _SW_INVALID
;
2101 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
2102 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
2103 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
2104 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
2105 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
2110 FIXME( "not implemented\n" );
2115 /**********************************************************************
2116 * _statusfp (MSVCRT.@)
2118 unsigned int CDECL
_statusfp(void)
2120 unsigned int flags
= 0;
2121 #if defined(__i386__) || defined(__x86_64__)
2122 unsigned int x86_sw
, sse2_sw
;
2124 _statusfp2( &x86_sw
, &sse2_sw
);
2125 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
2126 flags
= x86_sw
| sse2_sw
;
2127 #elif defined(__aarch64__)
2130 __asm__
__volatile__( "mrs %0, fpsr" : "=r" (fpsr
) );
2131 if (fpsr
& 0x1) flags
|= _SW_INVALID
;
2132 if (fpsr
& 0x2) flags
|= _SW_ZERODIVIDE
;
2133 if (fpsr
& 0x4) flags
|= _SW_OVERFLOW
;
2134 if (fpsr
& 0x8) flags
|= _SW_UNDERFLOW
;
2135 if (fpsr
& 0x10) flags
|= _SW_INEXACT
;
2136 if (fpsr
& 0x80) flags
|= _SW_DENORMAL
;
2138 FIXME( "not implemented\n" );
2143 /*********************************************************************
2144 * _clearfp (MSVCRT.@)
2146 unsigned int CDECL
_clearfp(void)
2148 unsigned int flags
= 0;
2149 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2150 unsigned long fpword
;
2152 __asm__
__volatile__( "fnstsw %0; fnclex" : "=m" (fpword
) );
2153 if (fpword
& 0x1) flags
|= _SW_INVALID
;
2154 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
2155 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
2156 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
2157 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
2158 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
2162 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2163 if (fpword
& 0x1) flags
|= _SW_INVALID
;
2164 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
2165 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
2166 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
2167 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
2168 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
2170 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
2172 #elif defined(__aarch64__)
2175 __asm__
__volatile__( "mrs %0, fpsr" : "=r" (fpsr
) );
2176 if (fpsr
& 0x1) flags
|= _SW_INVALID
;
2177 if (fpsr
& 0x2) flags
|= _SW_ZERODIVIDE
;
2178 if (fpsr
& 0x4) flags
|= _SW_OVERFLOW
;
2179 if (fpsr
& 0x8) flags
|= _SW_UNDERFLOW
;
2180 if (fpsr
& 0x10) flags
|= _SW_INEXACT
;
2181 if (fpsr
& 0x80) flags
|= _SW_DENORMAL
;
2183 __asm__
__volatile__( "msr fpsr, %0" :: "r" (fpsr
) );
2185 FIXME( "not implemented\n" );
2190 /*********************************************************************
2191 * __fpecode (MSVCRT.@)
2193 int * CDECL
__fpecode(void)
2195 return &msvcrt_get_thread_data()->fpecode
;
2198 /*********************************************************************
2201 double CDECL
ldexp(double num
, int exp
)
2203 double z
= unix_funcs
->ldexp(num
,exp
);
2205 if (isfinite(num
) && !isfinite(z
))
2206 return math_error(_OVERFLOW
, "ldexp", num
, exp
, z
);
2207 if (num
&& isfinite(num
) && !z
)
2208 return math_error(_UNDERFLOW
, "ldexp", num
, exp
, z
);
2209 if (z
== 0 && signbit(z
))
2210 z
= 0.0; /* Convert -0 -> +0 */
2214 /*********************************************************************
2217 double CDECL
_cabs(struct _complex num
)
2219 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
2222 /*********************************************************************
2223 * _chgsign (MSVCRT.@)
2225 double CDECL
_chgsign(double num
)
2227 union { double f
; UINT64 i
; } u
= { num
};
2232 /*********************************************************************
2233 * __control87_2 (MSVCR80.@)
2235 * Not exported by native msvcrt, added in msvcr80.
2238 int CDECL
__control87_2( unsigned int newval
, unsigned int mask
,
2239 unsigned int *x86_cw
, unsigned int *sse2_cw
)
2241 #if defined(__GNUC__) || defined(__clang__)
2242 unsigned long fpword
;
2244 unsigned int old_flags
;
2248 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
2250 /* Convert into mask constants */
2252 if (fpword
& 0x1) flags
|= _EM_INVALID
;
2253 if (fpword
& 0x2) flags
|= _EM_DENORMAL
;
2254 if (fpword
& 0x4) flags
|= _EM_ZERODIVIDE
;
2255 if (fpword
& 0x8) flags
|= _EM_OVERFLOW
;
2256 if (fpword
& 0x10) flags
|= _EM_UNDERFLOW
;
2257 if (fpword
& 0x20) flags
|= _EM_INEXACT
;
2258 switch (fpword
& 0xc00)
2260 case 0xc00: flags
|= _RC_UP
|_RC_DOWN
; break;
2261 case 0x800: flags
|= _RC_UP
; break;
2262 case 0x400: flags
|= _RC_DOWN
; break;
2264 switch (fpword
& 0x300)
2266 case 0x0: flags
|= _PC_24
; break;
2267 case 0x200: flags
|= _PC_53
; break;
2268 case 0x300: flags
|= _PC_64
; break;
2270 if (fpword
& 0x1000) flags
|= _IC_AFFINE
;
2272 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
2275 flags
= (flags
& ~mask
) | (newval
& mask
);
2277 /* Convert (masked) value back to fp word */
2279 if (flags
& _EM_INVALID
) fpword
|= 0x1;
2280 if (flags
& _EM_DENORMAL
) fpword
|= 0x2;
2281 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x4;
2282 if (flags
& _EM_OVERFLOW
) fpword
|= 0x8;
2283 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x10;
2284 if (flags
& _EM_INEXACT
) fpword
|= 0x20;
2285 switch (flags
& _MCW_RC
)
2287 case _RC_UP
|_RC_DOWN
: fpword
|= 0xc00; break;
2288 case _RC_UP
: fpword
|= 0x800; break;
2289 case _RC_DOWN
: fpword
|= 0x400; break;
2291 switch (flags
& _MCW_PC
)
2293 case _PC_64
: fpword
|= 0x300; break;
2294 case _PC_53
: fpword
|= 0x200; break;
2295 case _PC_24
: fpword
|= 0x0; break;
2297 if (flags
& _IC_AFFINE
) fpword
|= 0x1000;
2299 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
2304 if (!sse2_cw
) return 1;
2308 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2310 /* Convert into mask constants */
2312 if (fpword
& 0x80) flags
|= _EM_INVALID
;
2313 if (fpword
& 0x100) flags
|= _EM_DENORMAL
;
2314 if (fpword
& 0x200) flags
|= _EM_ZERODIVIDE
;
2315 if (fpword
& 0x400) flags
|= _EM_OVERFLOW
;
2316 if (fpword
& 0x800) flags
|= _EM_UNDERFLOW
;
2317 if (fpword
& 0x1000) flags
|= _EM_INEXACT
;
2318 switch (fpword
& 0x6000)
2320 case 0x6000: flags
|= _RC_UP
|_RC_DOWN
; break;
2321 case 0x4000: flags
|= _RC_UP
; break;
2322 case 0x2000: flags
|= _RC_DOWN
; break;
2324 switch (fpword
& 0x8040)
2326 case 0x0040: flags
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
2327 case 0x8000: flags
|= _DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
2328 case 0x8040: flags
|= _DN_FLUSH
; break;
2331 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
2335 mask
&= _MCW_EM
| _MCW_RC
| _MCW_DN
;
2336 flags
= (flags
& ~mask
) | (newval
& mask
);
2338 if (flags
!= old_flags
)
2340 /* Convert (masked) value back to fp word */
2342 if (flags
& _EM_INVALID
) fpword
|= 0x80;
2343 if (flags
& _EM_DENORMAL
) fpword
|= 0x100;
2344 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2345 if (flags
& _EM_OVERFLOW
) fpword
|= 0x400;
2346 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x800;
2347 if (flags
& _EM_INEXACT
) fpword
|= 0x1000;
2348 switch (flags
& _MCW_RC
)
2350 case _RC_UP
|_RC_DOWN
: fpword
|= 0x6000; break;
2351 case _RC_UP
: fpword
|= 0x4000; break;
2352 case _RC_DOWN
: fpword
|= 0x2000; break;
2354 switch (flags
& _MCW_DN
)
2356 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
2357 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
2358 case _DN_FLUSH
: fpword
|= 0x8040; break;
2360 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
2369 FIXME( "not implemented\n" );
2375 /*********************************************************************
2376 * _control87 (MSVCRT.@)
2378 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
2380 unsigned int flags
= 0;
2382 unsigned int sse2_cw
;
2384 __control87_2( newval
, mask
, &flags
, &sse2_cw
);
2386 if ((flags
^ sse2_cw
) & (_MCW_EM
| _MCW_RC
)) flags
|= _EM_AMBIGUOUS
;
2388 #elif defined(__x86_64__)
2389 unsigned long fpword
;
2390 unsigned int old_flags
;
2392 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2393 if (fpword
& 0x80) flags
|= _EM_INVALID
;
2394 if (fpword
& 0x100) flags
|= _EM_DENORMAL
;
2395 if (fpword
& 0x200) flags
|= _EM_ZERODIVIDE
;
2396 if (fpword
& 0x400) flags
|= _EM_OVERFLOW
;
2397 if (fpword
& 0x800) flags
|= _EM_UNDERFLOW
;
2398 if (fpword
& 0x1000) flags
|= _EM_INEXACT
;
2399 switch (fpword
& 0x6000)
2401 case 0x6000: flags
|= _RC_CHOP
; break;
2402 case 0x4000: flags
|= _RC_UP
; break;
2403 case 0x2000: flags
|= _RC_DOWN
; break;
2405 switch (fpword
& 0x8040)
2407 case 0x0040: flags
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
2408 case 0x8000: flags
|= _DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
2409 case 0x8040: flags
|= _DN_FLUSH
; break;
2412 mask
&= _MCW_EM
| _MCW_RC
| _MCW_DN
;
2413 flags
= (flags
& ~mask
) | (newval
& mask
);
2414 if (flags
!= old_flags
)
2417 if (flags
& _EM_INVALID
) fpword
|= 0x80;
2418 if (flags
& _EM_DENORMAL
) fpword
|= 0x100;
2419 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2420 if (flags
& _EM_OVERFLOW
) fpword
|= 0x400;
2421 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x800;
2422 if (flags
& _EM_INEXACT
) fpword
|= 0x1000;
2423 switch (flags
& _MCW_RC
)
2425 case _RC_CHOP
: fpword
|= 0x6000; break;
2426 case _RC_UP
: fpword
|= 0x4000; break;
2427 case _RC_DOWN
: fpword
|= 0x2000; break;
2429 switch (flags
& _MCW_DN
)
2431 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
2432 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
2433 case _DN_FLUSH
: fpword
|= 0x8040; break;
2435 __asm__
__volatile__( "ldmxcsr %0" :: "m" (fpword
) );
2437 #elif defined(__aarch64__)
2440 __asm__
__volatile__( "mrs %0, fpcr" : "=r" (fpcr
) );
2441 if (!(fpcr
& 0x100)) flags
|= _EM_INVALID
;
2442 if (!(fpcr
& 0x200)) flags
|= _EM_ZERODIVIDE
;
2443 if (!(fpcr
& 0x400)) flags
|= _EM_OVERFLOW
;
2444 if (!(fpcr
& 0x800)) flags
|= _EM_UNDERFLOW
;
2445 if (!(fpcr
& 0x1000)) flags
|= _EM_INEXACT
;
2446 if (!(fpcr
& 0x8000)) flags
|= _EM_DENORMAL
;
2447 switch (fpcr
& 0xc00000)
2449 case 0x400000: flags
|= _RC_UP
; break;
2450 case 0x800000: flags
|= _RC_DOWN
; break;
2451 case 0xc00000: flags
|= _RC_CHOP
; break;
2453 flags
= (flags
& ~mask
) | (newval
& mask
);
2454 fpcr
&= ~0xc09f00ul
;
2455 if (!(flags
& _EM_INVALID
)) fpcr
|= 0x100;
2456 if (!(flags
& _EM_ZERODIVIDE
)) fpcr
|= 0x200;
2457 if (!(flags
& _EM_OVERFLOW
)) fpcr
|= 0x400;
2458 if (!(flags
& _EM_UNDERFLOW
)) fpcr
|= 0x800;
2459 if (!(flags
& _EM_INEXACT
)) fpcr
|= 0x1000;
2460 if (!(flags
& _EM_DENORMAL
)) fpcr
|= 0x8000;
2461 switch (flags
& _MCW_RC
)
2463 case _RC_CHOP
: fpcr
|= 0xc00000; break;
2464 case _RC_UP
: fpcr
|= 0x400000; break;
2465 case _RC_DOWN
: fpcr
|= 0x800000; break;
2467 __asm__
__volatile__( "msr fpcr, %0" :: "r" (fpcr
) );
2469 FIXME( "not implemented\n" );
2474 /*********************************************************************
2475 * _controlfp (MSVCRT.@)
2477 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
2479 return _control87( newval
, mask
& ~_EM_DENORMAL
);
2482 /*********************************************************************
2483 * _set_controlfp (MSVCRT.@)
2485 void CDECL
_set_controlfp( unsigned int newval
, unsigned int mask
)
2487 _controlfp( newval
, mask
);
2490 /*********************************************************************
2491 * _controlfp_s (MSVCRT.@)
2493 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
2495 static const unsigned int all_flags
= (_MCW_EM
| _MCW_IC
| _MCW_RC
|
2499 if (!MSVCRT_CHECK_PMT( !(newval
& mask
& ~all_flags
) ))
2501 if (cur
) *cur
= _controlfp( 0, 0 ); /* retrieve it anyway */
2504 val
= _controlfp( newval
, mask
);
2505 if (cur
) *cur
= val
;
2509 #if _MSVCR_VER >= 140
2512 FENV_X_INVALID
= 0x00100010,
2513 FENV_X_DENORMAL
= 0x00200020,
2514 FENV_X_ZERODIVIDE
= 0x00080008,
2515 FENV_X_OVERFLOW
= 0x00040004,
2516 FENV_X_UNDERFLOW
= 0x00020002,
2517 FENV_X_INEXACT
= 0x00010001,
2518 FENV_X_AFFINE
= 0x00004000,
2519 FENV_X_UP
= 0x00800200,
2520 FENV_X_DOWN
= 0x00400100,
2521 FENV_X_24
= 0x00002000,
2522 FENV_X_53
= 0x00001000,
2523 FENV_Y_INVALID
= 0x10000010,
2524 FENV_Y_DENORMAL
= 0x20000020,
2525 FENV_Y_ZERODIVIDE
= 0x08000008,
2526 FENV_Y_OVERFLOW
= 0x04000004,
2527 FENV_Y_UNDERFLOW
= 0x02000002,
2528 FENV_Y_INEXACT
= 0x01000001,
2529 FENV_Y_UP
= 0x80000200,
2530 FENV_Y_DOWN
= 0x40000100,
2531 FENV_Y_FLUSH
= 0x00000400,
2532 FENV_Y_FLUSH_SAVE
= 0x00000800
2535 /* encodes x87/sse control/status word in ulong */
2536 static __msvcrt_ulong
fenv_encode(unsigned int x
, unsigned int y
)
2538 __msvcrt_ulong ret
= 0;
2540 if (x
& _EM_INVALID
) ret
|= FENV_X_INVALID
;
2541 if (x
& _EM_DENORMAL
) ret
|= FENV_X_DENORMAL
;
2542 if (x
& _EM_ZERODIVIDE
) ret
|= FENV_X_ZERODIVIDE
;
2543 if (x
& _EM_OVERFLOW
) ret
|= FENV_X_OVERFLOW
;
2544 if (x
& _EM_UNDERFLOW
) ret
|= FENV_X_UNDERFLOW
;
2545 if (x
& _EM_INEXACT
) ret
|= FENV_X_INEXACT
;
2546 if (x
& _IC_AFFINE
) ret
|= FENV_X_AFFINE
;
2547 if (x
& _RC_UP
) ret
|= FENV_X_UP
;
2548 if (x
& _RC_DOWN
) ret
|= FENV_X_DOWN
;
2549 if (x
& _PC_24
) ret
|= FENV_X_24
;
2550 if (x
& _PC_53
) ret
|= FENV_X_53
;
2551 x
&= ~(_MCW_EM
| _MCW_IC
| _MCW_RC
| _MCW_PC
);
2553 if (y
& _EM_INVALID
) ret
|= FENV_Y_INVALID
;
2554 if (y
& _EM_DENORMAL
) ret
|= FENV_Y_DENORMAL
;
2555 if (y
& _EM_ZERODIVIDE
) ret
|= FENV_Y_ZERODIVIDE
;
2556 if (y
& _EM_OVERFLOW
) ret
|= FENV_Y_OVERFLOW
;
2557 if (y
& _EM_UNDERFLOW
) ret
|= FENV_Y_UNDERFLOW
;
2558 if (y
& _EM_INEXACT
) ret
|= FENV_Y_INEXACT
;
2559 if (y
& _RC_UP
) ret
|= FENV_Y_UP
;
2560 if (y
& _RC_DOWN
) ret
|= FENV_Y_DOWN
;
2561 if (y
& _DN_FLUSH
) ret
|= FENV_Y_FLUSH
;
2562 if (y
& _DN_FLUSH_OPERANDS_SAVE_RESULTS
) ret
|= FENV_Y_FLUSH_SAVE
;
2563 y
&= ~(_MCW_EM
| _MCW_IC
| _MCW_RC
| _MCW_DN
);
2565 if(x
|| y
) FIXME("unsupported flags: %x, %x\n", x
, y
);
2569 /* decodes x87/sse control/status word, returns FALSE on error */
2570 #if (defined(__i386__) || defined(__x86_64__))
2571 static BOOL
fenv_decode(__msvcrt_ulong enc
, unsigned int *x
, unsigned int *y
)
2574 if ((enc
& FENV_X_INVALID
) == FENV_X_INVALID
) *x
|= _EM_INVALID
;
2575 if ((enc
& FENV_X_DENORMAL
) == FENV_X_DENORMAL
) *x
|= _EM_DENORMAL
;
2576 if ((enc
& FENV_X_ZERODIVIDE
) == FENV_X_ZERODIVIDE
) *x
|= _EM_ZERODIVIDE
;
2577 if ((enc
& FENV_X_OVERFLOW
) == FENV_X_OVERFLOW
) *x
|= _EM_OVERFLOW
;
2578 if ((enc
& FENV_X_UNDERFLOW
) == FENV_X_UNDERFLOW
) *x
|= _EM_UNDERFLOW
;
2579 if ((enc
& FENV_X_INEXACT
) == FENV_X_INEXACT
) *x
|= _EM_INEXACT
;
2580 if ((enc
& FENV_X_AFFINE
) == FENV_X_AFFINE
) *x
|= _IC_AFFINE
;
2581 if ((enc
& FENV_X_UP
) == FENV_X_UP
) *x
|= _RC_UP
;
2582 if ((enc
& FENV_X_DOWN
) == FENV_X_DOWN
) *x
|= _RC_DOWN
;
2583 if ((enc
& FENV_X_24
) == FENV_X_24
) *x
|= _PC_24
;
2584 if ((enc
& FENV_X_53
) == FENV_X_53
) *x
|= _PC_53
;
2586 if ((enc
& FENV_Y_INVALID
) == FENV_Y_INVALID
) *y
|= _EM_INVALID
;
2587 if ((enc
& FENV_Y_DENORMAL
) == FENV_Y_DENORMAL
) *y
|= _EM_DENORMAL
;
2588 if ((enc
& FENV_Y_ZERODIVIDE
) == FENV_Y_ZERODIVIDE
) *y
|= _EM_ZERODIVIDE
;
2589 if ((enc
& FENV_Y_OVERFLOW
) == FENV_Y_OVERFLOW
) *y
|= _EM_OVERFLOW
;
2590 if ((enc
& FENV_Y_UNDERFLOW
) == FENV_Y_UNDERFLOW
) *y
|= _EM_UNDERFLOW
;
2591 if ((enc
& FENV_Y_INEXACT
) == FENV_Y_INEXACT
) *y
|= _EM_INEXACT
;
2592 if ((enc
& FENV_Y_UP
) == FENV_Y_UP
) *y
|= _RC_UP
;
2593 if ((enc
& FENV_Y_DOWN
) == FENV_Y_DOWN
) *y
|= _RC_DOWN
;
2594 if ((enc
& FENV_Y_FLUSH
) == FENV_Y_FLUSH
) *y
|= _DN_FLUSH
;
2595 if ((enc
& FENV_Y_FLUSH_SAVE
) == FENV_Y_FLUSH_SAVE
) *y
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
;
2597 if (fenv_encode(*x
, *y
) != enc
)
2599 WARN("can't decode: %lx\n", enc
);
2608 /*********************************************************************
2609 * fegetenv (MSVCR120.@)
2611 int CDECL
fegetenv(fenv_t
*env
)
2613 #if _MSVCR_VER>=140 && defined(__i386__)
2614 unsigned int x87
, sse
;
2615 __control87_2(0, 0, &x87
, &sse
);
2616 env
->_Fe_ctl
= fenv_encode(x87
, sse
);
2617 _statusfp2(&x87
, &sse
);
2618 env
->_Fe_stat
= fenv_encode(x87
, sse
);
2619 #elif _MSVCR_VER>=140
2620 env
->_Fe_ctl
= fenv_encode(0, _control87(0, 0));
2621 env
->_Fe_stat
= fenv_encode(0, _statusfp());
2623 env
->_Fe_ctl
= _controlfp(0, 0) & (_EM_INEXACT
| _EM_UNDERFLOW
|
2624 _EM_OVERFLOW
| _EM_ZERODIVIDE
| _EM_INVALID
| _RC_CHOP
);
2625 env
->_Fe_stat
= _statusfp();
2630 /*********************************************************************
2631 * feupdateenv (MSVCR120.@)
2633 int CDECL
feupdateenv(const fenv_t
*env
)
2637 set
._Fe_ctl
= env
->_Fe_ctl
;
2638 set
._Fe_stat
|= env
->_Fe_stat
;
2639 return fesetenv(&set
);
2642 /*********************************************************************
2643 * fetestexcept (MSVCR120.@)
2645 int CDECL
fetestexcept(int flags
)
2647 return _statusfp() & flags
;
2650 /*********************************************************************
2651 * fesetexceptflag (MSVCR120.@)
2653 int CDECL
fesetexceptflag(const fexcept_t
*status
, int excepts
)
2657 excepts
&= FE_ALL_EXCEPT
;
2662 #if _MSVCR_VER>=140 && (defined(__i386__) || defined(__x86_64__))
2663 env
._Fe_stat
&= ~fenv_encode(excepts
, excepts
);
2664 env
._Fe_stat
|= *status
& fenv_encode(excepts
, excepts
);
2665 #elif _MSVCR_VER>=140
2666 env
._Fe_stat
&= ~fenv_encode(0, excepts
);
2667 env
._Fe_stat
|= *status
& fenv_encode(0, excepts
);
2669 env
._Fe_stat
&= ~excepts
;
2670 env
._Fe_stat
|= *status
& excepts
;
2672 return fesetenv(&env
);
2675 /*********************************************************************
2676 * feraiseexcept (MSVCR120.@)
2678 int CDECL
feraiseexcept(int flags
)
2682 flags
&= FE_ALL_EXCEPT
;
2684 #if _MSVCR_VER>=140 && defined(__i386__)
2685 env
._Fe_stat
|= fenv_encode(flags
, flags
);
2686 #elif _MSVCR_VER>=140
2687 env
._Fe_stat
|= fenv_encode(0, flags
);
2689 env
._Fe_stat
|= flags
;
2691 return fesetenv(&env
);
2694 /*********************************************************************
2695 * feclearexcept (MSVCR120.@)
2697 int CDECL
feclearexcept(int flags
)
2702 flags
&= FE_ALL_EXCEPT
;
2704 env
._Fe_stat
&= ~fenv_encode(flags
, flags
);
2706 env
._Fe_stat
&= ~flags
;
2708 return fesetenv(&env
);
2711 /*********************************************************************
2712 * fegetexceptflag (MSVCR120.@)
2714 int CDECL
fegetexceptflag(fexcept_t
*status
, int excepts
)
2716 #if _MSVCR_VER>=140 && defined(__i386__)
2717 unsigned int x87
, sse
;
2718 _statusfp2(&x87
, &sse
);
2719 *status
= fenv_encode(x87
& excepts
, sse
& excepts
);
2720 #elif _MSVCR_VER>=140
2721 *status
= fenv_encode(0, _statusfp() & excepts
);
2723 *status
= _statusfp() & excepts
;
2730 /*********************************************************************
2731 * __fpe_flt_rounds (UCRTBASE.@)
2733 int CDECL
__fpe_flt_rounds(void)
2735 unsigned int fpc
= _controlfp(0, 0) & _RC_CHOP
;
2740 case _RC_CHOP
: return 0;
2741 case _RC_NEAR
: return 1;
2742 case _RC_UP
: return 2;
2750 /*********************************************************************
2751 * fegetround (MSVCR120.@)
2753 int CDECL
fegetround(void)
2755 return _controlfp(0, 0) & _RC_CHOP
;
2758 /*********************************************************************
2759 * fesetround (MSVCR120.@)
2761 int CDECL
fesetround(int round_mode
)
2763 if (round_mode
& (~_RC_CHOP
))
2765 _controlfp(round_mode
, _RC_CHOP
);
2769 #endif /* _MSVCR_VER>=120 */
2771 /*********************************************************************
2772 * _copysign (MSVCRT.@)
2774 * Copied from musl: src/math/copysign.c
2776 double CDECL
_copysign( double x
, double y
)
2778 union { double f
; UINT64 i
; } ux
= { x
}, uy
= { y
};
2780 ux
.i
|= uy
.i
& 1ull << 63;
2784 /*********************************************************************
2785 * _finite (MSVCRT.@)
2787 int CDECL
_finite(double num
)
2789 union { double f
; UINT64 i
; } u
= { num
};
2790 return (u
.i
& ~0ull >> 1) < 0x7ffull
<< 52;
2793 /*********************************************************************
2794 * _fpreset (MSVCRT.@)
2796 void CDECL
_fpreset(void)
2798 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2799 const unsigned int x86_cw
= 0x27f;
2800 __asm__
__volatile__( "fninit; fldcw %0" : : "m" (x86_cw
) );
2803 const unsigned long sse2_cw
= 0x1f80;
2804 __asm__
__volatile__( "ldmxcsr %0" : : "m" (sse2_cw
) );
2807 FIXME( "not implemented\n" );
2812 /*********************************************************************
2813 * fesetenv (MSVCR120.@)
2815 int CDECL
fesetenv(const fenv_t
*env
)
2817 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2818 unsigned int x87_cw
, sse_cw
, x87_stat
, sse_stat
;
2826 DWORD instruction_pointer
;
2834 TRACE( "(%p)\n", env
);
2836 if (!env
->_Fe_ctl
&& !env
->_Fe_stat
) {
2842 if (!fenv_decode(env
->_Fe_ctl
, &x87_cw
, &sse_cw
))
2844 if (!fenv_decode(env
->_Fe_stat
, &x87_stat
, &sse_stat
))
2847 x87_cw
= sse_cw
= env
->_Fe_ctl
;
2848 x87_stat
= sse_stat
= env
->_Fe_stat
;
2851 __asm__
__volatile__( "fnstenv %0" : "=m" (fenv
) );
2853 fenv
.control_word
&= ~0xc3d;
2855 fenv
.control_word
&= ~0x1302;
2857 if (x87_cw
& _EM_INVALID
) fenv
.control_word
|= 0x1;
2858 if (x87_cw
& _EM_ZERODIVIDE
) fenv
.control_word
|= 0x4;
2859 if (x87_cw
& _EM_OVERFLOW
) fenv
.control_word
|= 0x8;
2860 if (x87_cw
& _EM_UNDERFLOW
) fenv
.control_word
|= 0x10;
2861 if (x87_cw
& _EM_INEXACT
) fenv
.control_word
|= 0x20;
2862 switch (x87_cw
& _MCW_RC
)
2864 case _RC_UP
|_RC_DOWN
: fenv
.control_word
|= 0xc00; break;
2865 case _RC_UP
: fenv
.control_word
|= 0x800; break;
2866 case _RC_DOWN
: fenv
.control_word
|= 0x400; break;
2869 if (x87_cw
& _EM_DENORMAL
) fenv
.control_word
|= 0x2;
2870 switch (x87_cw
& _MCW_PC
)
2872 case _PC_64
: fenv
.control_word
|= 0x300; break;
2873 case _PC_53
: fenv
.control_word
|= 0x200; break;
2874 case _PC_24
: fenv
.control_word
|= 0x0; break;
2876 if (x87_cw
& _IC_AFFINE
) fenv
.control_word
|= 0x1000;
2879 fenv
.status_word
&= ~0x3f;
2880 if (x87_stat
& _SW_INVALID
) fenv
.status_word
|= 0x1;
2881 if (x87_stat
& _SW_DENORMAL
) fenv
.status_word
|= 0x2;
2882 if (x87_stat
& _SW_ZERODIVIDE
) fenv
.status_word
|= 0x4;
2883 if (x87_stat
& _SW_OVERFLOW
) fenv
.status_word
|= 0x8;
2884 if (x87_stat
& _SW_UNDERFLOW
) fenv
.status_word
|= 0x10;
2885 if (x87_stat
& _SW_INEXACT
) fenv
.status_word
|= 0x20;
2887 __asm__
__volatile__( "fldenv %0" : : "m" (fenv
) : "st", "st(1)",
2888 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
2893 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2898 if (sse_cw
& _EM_INVALID
) fpword
|= 0x80;
2899 if (sse_cw
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2900 if (sse_cw
& _EM_OVERFLOW
) fpword
|= 0x400;
2901 if (sse_cw
& _EM_UNDERFLOW
) fpword
|= 0x800;
2902 if (sse_cw
& _EM_INEXACT
) fpword
|= 0x1000;
2903 switch (sse_cw
& _MCW_RC
)
2905 case _RC_CHOP
: fpword
|= 0x6000; break;
2906 case _RC_UP
: fpword
|= 0x4000; break;
2907 case _RC_DOWN
: fpword
|= 0x2000; break;
2909 if (sse_stat
& _SW_INVALID
) fpword
|= 0x1;
2910 if (sse_stat
& _SW_DENORMAL
) fpword
|= 0x2;
2911 if (sse_stat
& _SW_ZERODIVIDE
) fpword
|= 0x4;
2912 if (sse_stat
& _SW_OVERFLOW
) fpword
|= 0x8;
2913 if (sse_stat
& _SW_UNDERFLOW
) fpword
|= 0x10;
2914 if (sse_stat
& _SW_INEXACT
) fpword
|= 0x20;
2916 if (sse_cw
& _EM_DENORMAL
) fpword
|= 0x100;
2917 switch (sse_cw
& _MCW_DN
)
2919 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
2920 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
2921 case _DN_FLUSH
: fpword
|= 0x8040; break;
2924 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
2929 FIXME( "not implemented\n" );
2935 /*********************************************************************
2938 int CDECL
_isnan(double num
)
2940 union { double f
; UINT64 i
; } u
= { num
};
2941 return (u
.i
& ~0ull >> 1) > 0x7ffull
<< 52;
2944 static double pzero(double x
)
2946 static const double pR8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
2947 0.00000000000000000000e+00,
2948 -7.03124999999900357484e-02,
2949 -8.08167041275349795626e+00,
2950 -2.57063105679704847262e+02,
2951 -2.48521641009428822144e+03,
2952 -5.25304380490729545272e+03,
2954 1.16534364619668181717e+02,
2955 3.83374475364121826715e+03,
2956 4.05978572648472545552e+04,
2957 1.16752972564375915681e+05,
2958 4.76277284146730962675e+04,
2959 }, pR5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
2960 -1.14125464691894502584e-11,
2961 -7.03124940873599280078e-02,
2962 -4.15961064470587782438e+00,
2963 -6.76747652265167261021e+01,
2964 -3.31231299649172967747e+02,
2965 -3.46433388365604912451e+02,
2967 6.07539382692300335975e+01,
2968 1.05125230595704579173e+03,
2969 5.97897094333855784498e+03,
2970 9.62544514357774460223e+03,
2971 2.40605815922939109441e+03,
2972 }, pR3
[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
2973 -2.54704601771951915620e-09,
2974 -7.03119616381481654654e-02,
2975 -2.40903221549529611423e+00,
2976 -2.19659774734883086467e+01,
2977 -5.80791704701737572236e+01,
2978 -3.14479470594888503854e+01,
2980 3.58560338055209726349e+01,
2981 3.61513983050303863820e+02,
2982 1.19360783792111533330e+03,
2983 1.12799679856907414432e+03,
2984 1.73580930813335754692e+02,
2985 }, pR2
[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
2986 -8.87534333032526411254e-08,
2987 -7.03030995483624743247e-02,
2988 -1.45073846780952986357e+00,
2989 -7.63569613823527770791e+00,
2990 -1.11931668860356747786e+01,
2991 -3.23364579351335335033e+00,
2993 2.22202997532088808441e+01,
2994 1.36206794218215208048e+02,
2995 2.70470278658083486789e+02,
2996 1.53875394208320329881e+02,
2997 1.46576176948256193810e+01,
3000 const double *p
, *q
;
3004 ix
= *(ULONGLONG
*)&x
>> 32;
3006 if (ix
>= 0x40200000) {
3009 } else if (ix
>= 0x40122E8B) {
3012 } else if (ix
>= 0x4006DB6D) {
3015 } else /*ix >= 0x40000000*/ {
3021 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
3022 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* q
[4]))));
3026 static double qzero(double x
)
3028 static const double qR8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
3029 0.00000000000000000000e+00,
3030 7.32421874999935051953e-02,
3031 1.17682064682252693899e+01,
3032 5.57673380256401856059e+02,
3033 8.85919720756468632317e+03,
3034 3.70146267776887834771e+04,
3036 1.63776026895689824414e+02,
3037 8.09834494656449805916e+03,
3038 1.42538291419120476348e+05,
3039 8.03309257119514397345e+05,
3040 8.40501579819060512818e+05,
3041 -3.43899293537866615225e+05,
3042 }, qR5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
3043 1.84085963594515531381e-11,
3044 7.32421766612684765896e-02,
3045 5.83563508962056953777e+00,
3046 1.35111577286449829671e+02,
3047 1.02724376596164097464e+03,
3048 1.98997785864605384631e+03,
3050 8.27766102236537761883e+01,
3051 2.07781416421392987104e+03,
3052 1.88472887785718085070e+04,
3053 5.67511122894947329769e+04,
3054 3.59767538425114471465e+04,
3055 -5.35434275601944773371e+03,
3056 }, qR3
[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
3057 4.37741014089738620906e-09,
3058 7.32411180042911447163e-02,
3059 3.34423137516170720929e+00,
3060 4.26218440745412650017e+01,
3061 1.70808091340565596283e+02,
3062 1.66733948696651168575e+02,
3064 4.87588729724587182091e+01,
3065 7.09689221056606015736e+02,
3066 3.70414822620111362994e+03,
3067 6.46042516752568917582e+03,
3068 2.51633368920368957333e+03,
3069 -1.49247451836156386662e+02,
3070 }, qR2
[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
3071 1.50444444886983272379e-07,
3072 7.32234265963079278272e-02,
3073 1.99819174093815998816e+00,
3074 1.44956029347885735348e+01,
3075 3.16662317504781540833e+01,
3076 1.62527075710929267416e+01,
3078 3.03655848355219184498e+01,
3079 2.69348118608049844624e+02,
3080 8.44783757595320139444e+02,
3081 8.82935845112488550512e+02,
3082 2.12666388511798828631e+02,
3083 -5.31095493882666946917e+00,
3086 const double *p
, *q
;
3090 ix
= *(ULONGLONG
*)&x
>> 32;
3092 if (ix
>= 0x40200000) {
3095 } else if (ix
>= 0x40122E8B) {
3098 } else if (ix
>= 0x4006DB6D) {
3101 } else /*ix >= 0x40000000*/ {
3107 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
3108 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* (q
[4] + z
* q
[5])))));
3109 return (-0.125 + r
/ s
) / x
;
3112 /* j0 and y0 approximation for |x|>=2 */
3113 static double j0_y0_approx(unsigned int ix
, double x
, BOOL y0
)
3115 static const double invsqrtpi
= 5.64189583547756279280e-01;
3117 double s
, c
, ss
, cc
, z
;
3123 /* avoid overflow in 2*x, big ulp error when x>=0x1p1023 */
3124 if (ix
< 0x7fe00000) {
3127 if (s
* c
< 0) cc
= z
/ ss
;
3129 if (ix
< 0x48000000) {
3131 cc
= pzero(x
) * cc
- qzero(x
) * ss
;
3134 return invsqrtpi
* cc
/ sqrt(x
);
3137 /*********************************************************************
3140 * Copied from musl: src/math/j0.c
3142 double CDECL
_j0(double x
)
3144 static const double R02
= 1.56249999999999947958e-02,
3145 R03
= -1.89979294238854721751e-04,
3146 R04
= 1.82954049532700665670e-06,
3147 R05
= -4.61832688532103189199e-09,
3148 S01
= 1.56191029464890010492e-02,
3149 S02
= 1.16926784663337450260e-04,
3150 S03
= 5.13546550207318111446e-07,
3151 S04
= 1.16614003333790000205e-09;
3156 ix
= *(ULONGLONG
*)&x
>> 32;
3159 /* j0(+-inf)=0, j0(nan)=nan */
3160 if (ix
>= 0x7ff00000)
3161 return math_error(_DOMAIN
, "_j0", x
, 0, 1 / (x
* x
));
3164 if (ix
>= 0x40000000) { /* |x| >= 2 */
3165 /* large ulp error near zeros: 2.4, 5.52, 8.6537,.. */
3166 return j0_y0_approx(ix
, x
, FALSE
);
3169 if (ix
>= 0x3f200000) { /* |x| >= 2**-13 */
3170 /* up to 4ulp error close to 2 */
3172 r
= z
* (R02
+ z
* (R03
+ z
* (R04
+ z
* R05
)));
3173 s
= 1 + z
* (S01
+ z
* (S02
+ z
* (S03
+ z
* S04
)));
3174 return (1 + x
/ 2) * (1 - x
/ 2) + z
* (r
/ s
);
3178 /* prevent underflow */
3179 /* inexact should be raised when x!=0, this is not done correctly */
3180 if (ix
>= 0x38000000) /* |x| >= 2**-127 */
3185 static double pone(double x
)
3187 static const double pr8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
3188 0.00000000000000000000e+00,
3189 1.17187499999988647970e-01,
3190 1.32394806593073575129e+01,
3191 4.12051854307378562225e+02,
3192 3.87474538913960532227e+03,
3193 7.91447954031891731574e+03,
3195 1.14207370375678408436e+02,
3196 3.65093083420853463394e+03,
3197 3.69562060269033463555e+04,
3198 9.76027935934950801311e+04,
3199 3.08042720627888811578e+04,
3200 }, pr5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
3201 1.31990519556243522749e-11,
3202 1.17187493190614097638e-01,
3203 6.80275127868432871736e+00,
3204 1.08308182990189109773e+02,
3205 5.17636139533199752805e+02,
3206 5.28715201363337541807e+02,
3208 5.92805987221131331921e+01,
3209 9.91401418733614377743e+02,
3210 5.35326695291487976647e+03,
3211 7.84469031749551231769e+03,
3212 1.50404688810361062679e+03,
3214 3.02503916137373618024e-09,
3215 1.17186865567253592491e-01,
3216 3.93297750033315640650e+00,
3217 3.51194035591636932736e+01,
3218 9.10550110750781271918e+01,
3219 4.85590685197364919645e+01,
3221 3.47913095001251519989e+01,
3222 3.36762458747825746741e+02,
3223 1.04687139975775130551e+03,
3224 8.90811346398256432622e+02,
3225 1.03787932439639277504e+02,
3226 }, pr2
[6] = { /* for x in [2.8570,2]=1/[0.3499,0.5] */
3227 1.07710830106873743082e-07,
3228 1.17176219462683348094e-01,
3229 2.36851496667608785174e+00,
3230 1.22426109148261232917e+01,
3231 1.76939711271687727390e+01,
3232 5.07352312588818499250e+00,
3234 2.14364859363821409488e+01,
3235 1.25290227168402751090e+02,
3236 2.32276469057162813669e+02,
3237 1.17679373287147100768e+02,
3238 8.36463893371618283368e+00,
3241 const double *p
, *q
;
3245 ix
= *(ULONGLONG
*)&x
>> 32;
3247 if (ix
>= 0x40200000) {
3250 } else if (ix
>= 0x40122E8B) {
3253 } else if (ix
>= 0x4006DB6D) {
3256 } else /*ix >= 0x40000000*/ {
3261 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
3262 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* q
[4]))));
3266 static double qone(double x
)
3268 static const double qr8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
3269 0.00000000000000000000e+00,
3270 -1.02539062499992714161e-01,
3271 -1.62717534544589987888e+01,
3272 -7.59601722513950107896e+02,
3273 -1.18498066702429587167e+04,
3274 -4.84385124285750353010e+04,
3276 1.61395369700722909556e+02,
3277 7.82538599923348465381e+03,
3278 1.33875336287249578163e+05,
3279 7.19657723683240939863e+05,
3280 6.66601232617776375264e+05,
3281 -2.94490264303834643215e+05,
3282 }, qr5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
3283 -2.08979931141764104297e-11,
3284 -1.02539050241375426231e-01,
3285 -8.05644828123936029840e+00,
3286 -1.83669607474888380239e+02,
3287 -1.37319376065508163265e+03,
3288 -2.61244440453215656817e+03,
3290 8.12765501384335777857e+01,
3291 1.99179873460485964642e+03,
3292 1.74684851924908907677e+04,
3293 4.98514270910352279316e+04,
3294 2.79480751638918118260e+04,
3295 -4.71918354795128470869e+03,
3297 -5.07831226461766561369e-09,
3298 -1.02537829820837089745e-01,
3299 -4.61011581139473403113e+00,
3300 -5.78472216562783643212e+01,
3301 -2.28244540737631695038e+02,
3302 -2.19210128478909325622e+02,
3304 4.76651550323729509273e+01,
3305 6.73865112676699709482e+02,
3306 3.38015286679526343505e+03,
3307 5.54772909720722782367e+03,
3308 1.90311919338810798763e+03,
3309 -1.35201191444307340817e+02,
3310 }, qr2
[6] = { /* for x in [2.8570,2]=1/[0.3499,0.5] */
3311 -1.78381727510958865572e-07,
3312 -1.02517042607985553460e-01,
3313 -2.75220568278187460720e+00,
3314 -1.96636162643703720221e+01,
3315 -4.23253133372830490089e+01,
3316 -2.13719211703704061733e+01,
3318 2.95333629060523854548e+01,
3319 2.52981549982190529136e+02,
3320 7.57502834868645436472e+02,
3321 7.39393205320467245656e+02,
3322 1.55949003336666123687e+02,
3323 -4.95949898822628210127e+00,
3326 const double *p
, *q
;
3330 ix
= *(ULONGLONG
*)&x
>> 32;
3332 if (ix
>= 0x40200000) {
3335 } else if (ix
>= 0x40122E8B) {
3338 } else if (ix
>= 0x4006DB6D) {
3341 } else /*ix >= 0x40000000*/ {
3346 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
3347 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* (q
[4] + z
* q
[5])))));
3348 return (0.375 + r
/ s
) / x
;
3351 static double j1_y1_approx(unsigned int ix
, double x
, BOOL y1
, int sign
)
3353 static const double invsqrtpi
= 5.64189583547756279280e-01;
3355 double z
, s
, c
, ss
, cc
;
3361 if (ix
< 0x7fe00000) {
3364 if (s
* c
> 0) cc
= z
/ ss
;
3366 if (ix
< 0x48000000) {
3369 cc
= pone(x
) * cc
- qone(x
) * ss
;
3374 return invsqrtpi
* cc
/ sqrt(x
);
3377 /*********************************************************************
3380 * Copied from musl: src/math/j1.c
3382 double CDECL
_j1(double x
)
3384 static const double r00
= -6.25000000000000000000e-02,
3385 r01
= 1.40705666955189706048e-03,
3386 r02
= -1.59955631084035597520e-05,
3387 r03
= 4.96727999609584448412e-08,
3388 s01
= 1.91537599538363460805e-02,
3389 s02
= 1.85946785588630915560e-04,
3390 s03
= 1.17718464042623683263e-06,
3391 s04
= 5.04636257076217042715e-09,
3392 s05
= 1.23542274426137913908e-11;
3398 ix
= *(ULONGLONG
*)&x
>> 32;
3401 if (ix
>= 0x7ff00000)
3402 return math_error(isnan(x
) ? 0 : _DOMAIN
, "_j1", x
, 0, 1 / (x
* x
));
3403 if (ix
>= 0x40000000) /* |x| >= 2 */
3404 return j1_y1_approx(ix
, fabs(x
), FALSE
, sign
);
3405 if (ix
>= 0x38000000) { /* |x| >= 2**-127 */
3407 r
= z
* (r00
+ z
* (r01
+ z
* (r02
+ z
* r03
)));
3408 s
= 1 + z
* (s01
+ z
* (s02
+ z
* (s03
+ z
* (s04
+ z
* s05
))));
3411 /* avoid underflow, raise inexact if x!=0 */
3414 return (0.5 + z
) * x
;
3417 /*********************************************************************
3420 * Copied from musl: src/math/jn.c
3422 double CDECL
_jn(int n
, double x
)
3424 static const double invsqrtpi
= 5.64189583547756279280e-01;
3426 unsigned int ix
, lx
;
3430 ix
= *(ULONGLONG
*)&x
>> 32;
3431 lx
= *(ULONGLONG
*)&x
;
3435 if ((ix
| (lx
| -lx
) >> 31) > 0x7ff00000) /* nan */
3450 sign
&= n
; /* even n: 0, odd n: signbit(x) */
3452 if ((ix
| lx
) == 0 || ix
== 0x7ff00000) /* if x is 0 or inf */
3455 if (ix
>= 0x52d00000) { /* x > 2**302 */
3458 temp
= -cos(x
) + sin(x
);
3461 temp
= -cos(x
) - sin(x
);
3464 temp
= cos(x
) - sin(x
);
3467 temp
= cos(x
) + sin(x
);
3470 b
= invsqrtpi
* temp
/ sqrt(x
);
3474 for (i
= 0; i
< nm1
; ) {
3477 b
= b
* (2.0 * i
/ x
) - a
; /* avoid underflow */
3482 if (ix
< 0x3e100000) { /* x < 2**-29 */
3483 if (nm1
> 32) /* underflow */
3489 for (i
= 2; i
<= nm1
+ 1; i
++) {
3490 a
*= (double)i
; /* a = n! */
3491 b
*= temp
; /* b = (x/2)^n */
3496 double t
, q0
, q1
, w
, h
, z
, tmp
, nf
;
3506 while (q1
< 1.0e9
) {
3513 for (t
= 0.0, i
= k
; i
>= 0; i
--)
3514 t
= 1 / (2 * (i
+ nf
) / x
- t
);
3517 tmp
= nf
* log(fabs(w
));
3518 if (tmp
< 7.09782712893383973096e+02) {
3519 for (i
= nm1
; i
> 0; i
--) {
3521 b
= b
* (2.0 * i
) / x
- a
;
3525 for (i
= nm1
; i
> 0; i
--) {
3527 b
= b
* (2.0 * i
) / x
- a
;
3529 /* scale b to avoid spurious overflow */
3539 if (fabs(z
) >= fabs(w
))
3545 return sign
? -b
: b
;
3548 /*********************************************************************
3551 double CDECL
_y0(double x
)
3553 static const double tpi
= 6.36619772367581382433e-01,
3554 u00
= -7.38042951086872317523e-02,
3555 u01
= 1.76666452509181115538e-01,
3556 u02
= -1.38185671945596898896e-02,
3557 u03
= 3.47453432093683650238e-04,
3558 u04
= -3.81407053724364161125e-06,
3559 u05
= 1.95590137035022920206e-08,
3560 u06
= -3.98205194132103398453e-11,
3561 v01
= 1.27304834834123699328e-02,
3562 v02
= 7.60068627350353253702e-05,
3563 v03
= 2.59150851840457805467e-07,
3564 v04
= 4.41110311332675467403e-10;
3567 unsigned int ix
, lx
;
3569 ix
= *(ULONGLONG
*)&x
>> 32;
3570 lx
= *(ULONGLONG
*)&x
;
3572 /* y0(nan)=nan, y0(<0)=nan, y0(0)=-inf, y0(inf)=0 */
3573 if ((ix
<< 1 | lx
) == 0)
3574 return math_error(_OVERFLOW
, "_y0", x
, 0, -INFINITY
);
3578 return math_error(_DOMAIN
, "_y0", x
, 0, 0 / (x
- x
));
3579 if (ix
>= 0x7ff00000)
3582 if (ix
>= 0x40000000) { /* x >= 2 */
3583 /* large ulp errors near zeros: 3.958, 7.086,.. */
3584 return j0_y0_approx(ix
, x
, TRUE
);
3587 if (ix
>= 0x3e400000) { /* x >= 2**-27 */
3588 /* large ulp error near the first zero, x ~= 0.89 */
3590 u
= u00
+ z
* (u01
+ z
* (u02
+ z
* (u03
+ z
* (u04
+ z
* (u05
+ z
* u06
)))));
3591 v
= 1.0 + z
* (v01
+ z
* (v02
+ z
* (v03
+ z
* v04
)));
3592 return u
/ v
+ tpi
* (j0(x
) * log(x
));
3594 return u00
+ tpi
* log(x
);
3597 /*********************************************************************
3600 double CDECL
_y1(double x
)
3602 static const double tpi
= 6.36619772367581382433e-01,
3603 u00
= -1.96057090646238940668e-01,
3604 u01
= 5.04438716639811282616e-02,
3605 u02
= -1.91256895875763547298e-03,
3606 u03
= 2.35252600561610495928e-05,
3607 u04
= -9.19099158039878874504e-08,
3608 v00
= 1.99167318236649903973e-02,
3609 v01
= 2.02552581025135171496e-04,
3610 v02
= 1.35608801097516229404e-06,
3611 v03
= 6.22741452364621501295e-09,
3612 v04
= 1.66559246207992079114e-11;
3615 unsigned int ix
, lx
;
3617 ix
= *(ULONGLONG
*)&x
>> 32;
3618 lx
= *(ULONGLONG
*)&x
;
3620 /* y1(nan)=nan, y1(<0)=nan, y1(0)=-inf, y1(inf)=0 */
3621 if ((ix
<< 1 | lx
) == 0)
3622 return math_error(_OVERFLOW
, "_y1", x
, 0, -INFINITY
);
3626 return math_error(_DOMAIN
, "_y1", x
, 0, 0 / (x
- x
));
3627 if (ix
>= 0x7ff00000)
3630 if (ix
>= 0x40000000) /* x >= 2 */
3631 return j1_y1_approx(ix
, x
, TRUE
, 0);
3632 if (ix
< 0x3c900000) /* x < 2**-54 */
3635 u
= u00
+ z
* (u01
+ z
* (u02
+ z
* (u03
+ z
* u04
)));
3636 v
= 1 + z
* (v00
+ z
* (v01
+ z
* (v02
+ z
* (v03
+ z
* v04
))));
3637 return x
* (u
/ v
) + tpi
* (j1(x
) * log(x
) - 1 / x
);
3640 /*********************************************************************
3643 * Copied from musl: src/math/jn.c
3645 double CDECL
_yn(int n
, double x
)
3647 static const double invsqrtpi
= 5.64189583547756279280e-01;
3649 unsigned int ix
, lx
, ib
;
3653 ix
= *(ULONGLONG
*)&x
>> 32;
3654 lx
= *(ULONGLONG
*)&x
;
3658 if ((ix
| (lx
| -lx
) >> 31) > 0x7ff00000) /* nan */
3660 if (sign
&& (ix
| lx
) != 0) /* x < 0 */
3661 return math_error(_DOMAIN
, "_y1", x
, 0, 0 / (x
- x
));
3662 if (ix
== 0x7ff00000)
3675 return sign
? -y1(x
) : y1(x
);
3677 if (ix
>= 0x52d00000) { /* x > 2**302 */
3680 temp
= -sin(x
) - cos(x
);
3683 temp
= -sin(x
) + cos(x
);
3686 temp
= sin(x
) + cos(x
);
3689 temp
= sin(x
) - cos(x
);
3692 b
= invsqrtpi
* temp
/ sqrt(x
);
3696 /* quit if b is -inf */
3697 ib
= *(ULONGLONG
*)&b
>> 32;
3698 for (i
= 0; i
< nm1
&& ib
!= 0xfff00000;) {
3701 b
= (2.0 * i
/ x
) * b
- a
;
3702 ib
= *(ULONGLONG
*)&b
>> 32;
3706 return sign
? -b
: b
;
3711 /*********************************************************************
3712 * _nearbyint (MSVCR120.@)
3714 * Based on musl: src/math/nearbyteint.c
3716 double CDECL
nearbyint(double x
)
3721 _control87(_MCW_EM
, _MCW_EM
);
3723 feclearexcept(FE_INEXACT
);
3728 /*********************************************************************
3729 * _nearbyintf (MSVCR120.@)
3731 * Based on musl: src/math/nearbyteintf.c
3733 float CDECL
nearbyintf(float x
)
3738 _control87(_MCW_EM
, _MCW_EM
);
3740 feclearexcept(FE_INEXACT
);
3745 /*********************************************************************
3746 * nexttoward (MSVCR120.@)
3748 double CDECL
MSVCRT_nexttoward(double num
, double next
)
3750 return _nextafter(num
, next
);
3753 /*********************************************************************
3754 * nexttowardf (MSVCR120.@)
3756 * Copied from musl: src/math/nexttowardf.c
3758 float CDECL
MSVCRT_nexttowardf(float x
, double y
)
3760 unsigned int ix
= *(unsigned int*)&x
;
3764 if (isnan(x
) || isnan(y
))
3783 e
= ix
& 0x7f800000;
3784 /* raise overflow if ix is infinite and x is finite */
3785 if (e
== 0x7f800000) {
3790 /* raise underflow if ret is subnormal or zero */
3792 fp_barrierf(x
* x
+ ret
* ret
);
3798 #endif /* _MSVCR_VER>=120 */
3800 /*********************************************************************
3801 * _nextafter (MSVCRT.@)
3803 * Copied from musl: src/math/nextafter.c
3805 double CDECL
_nextafter(double x
, double y
)
3807 ULONGLONG llx
= *(ULONGLONG
*)&x
;
3808 ULONGLONG lly
= *(ULONGLONG
*)&y
;
3812 if (isnan(x
) || isnan(y
))
3815 if (_fpclass(y
) & (_FPCLASS_ND
| _FPCLASS_PD
| _FPCLASS_NZ
| _FPCLASS_PZ
))
3819 ax
= llx
& -1ULL / 2;
3820 ay
= lly
& -1ULL / 2;
3824 llx
= (lly
& 1ULL << 63) | 1;
3825 } else if (ax
> ay
|| ((llx
^ lly
) & 1ULL << 63))
3829 e
= llx
>> 52 & 0x7ff;
3830 /* raise overflow if llx is infinite and x is finite */
3835 /* raise underflow if llx is subnormal or zero */
3838 fp_barrier(x
* x
+ y
* y
);
3844 /*********************************************************************
3847 char * CDECL
_ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
3850 thread_data_t
*data
= msvcrt_get_thread_data();
3851 /* FIXME: check better for overflow (native supports over 300 chars) */
3852 ndigits
= min( ndigits
, 80 - 8); /* 8 : space for sign, dec point, "e",
3853 * 4 for exponent and one for
3854 * terminating '\0' */
3855 if (!data
->efcvt_buffer
)
3856 data
->efcvt_buffer
= malloc( 80 ); /* ought to be enough */
3858 /* handle cases with zero ndigits or less */
3860 if( prec
< 1) prec
= 2;
3861 len
= _snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
3863 if (data
->efcvt_buffer
[0] == '-') {
3864 memmove( data
->efcvt_buffer
, data
->efcvt_buffer
+ 1, len
-- );
3868 /* take the decimal "point away */
3870 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
3871 /* take the exponential "e" out */
3872 data
->efcvt_buffer
[ prec
] = '\0';
3873 /* read the exponent */
3874 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
3876 /* adjust for some border cases */
3877 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
3879 /* handle cases with zero ndigits or less */
3881 if( data
->efcvt_buffer
[ 0] >= '5')
3883 data
->efcvt_buffer
[ 0] = '\0';
3885 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
3886 return data
->efcvt_buffer
;
3889 /*********************************************************************
3890 * _ecvt_s (MSVCRT.@)
3892 int CDECL
_ecvt_s( char *buffer
, size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
3897 if (!MSVCRT_CHECK_PMT(buffer
!= NULL
)) return EINVAL
;
3898 if (!MSVCRT_CHECK_PMT(decpt
!= NULL
)) return EINVAL
;
3899 if (!MSVCRT_CHECK_PMT(sign
!= NULL
)) return EINVAL
;
3900 if (!MSVCRT_CHECK_PMT_ERR( length
> 2, ERANGE
)) return ERANGE
;
3901 if (!MSVCRT_CHECK_PMT_ERR(ndigits
< (int)length
- 1, ERANGE
)) return ERANGE
;
3903 /* handle cases with zero ndigits or less */
3905 if( prec
< 1) prec
= 2;
3906 result
= malloc(prec
+ 8);
3908 len
= _snprintf(result
, prec
+ 8, "%.*le", prec
- 1, number
);
3909 if (result
[0] == '-') {
3910 memmove( result
, result
+ 1, len
-- );
3914 /* take the decimal "point away */
3916 memmove( result
+ 1, result
+ 2, len
- 1 );
3917 /* take the exponential "e" out */
3918 result
[ prec
] = '\0';
3919 /* read the exponent */
3920 sscanf( result
+ prec
+ 1, "%d", decpt
);
3922 /* adjust for some border cases */
3923 if( result
[0] == '0')/* value is zero */
3925 /* handle cases with zero ndigits or less */
3927 if( result
[ 0] >= '5')
3931 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
3936 /***********************************************************************
3939 char * CDECL
_fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
3941 thread_data_t
*data
= msvcrt_get_thread_data();
3942 int stop
, dec1
, dec2
;
3943 char *ptr1
, *ptr2
, *first
;
3944 char buf
[80]; /* ought to be enough */
3945 char decimal_separator
= get_locinfo()->lconv
->decimal_point
[0];
3947 if (!data
->efcvt_buffer
)
3948 data
->efcvt_buffer
= malloc( 80 ); /* ought to be enough */
3950 stop
= _snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
3952 ptr2
= data
->efcvt_buffer
;
3962 /* For numbers below the requested resolution, work out where
3963 the decimal point will be rather than finding it in the string */
3964 if (number
< 1.0 && number
> 0.0) {
3965 dec2
= log10(number
+ 1e-10);
3966 if (-dec2
<= ndigits
) dec2
= 0;
3969 /* If requested digits is zero or less, we will need to truncate
3970 * the returned string */
3975 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
3976 while (*ptr1
!= '\0' && *ptr1
!= decimal_separator
) {
3977 if (!first
) first
= ptr2
;
3978 if ((ptr1
- buf
) < stop
) {
3989 while (*ptr1
== '0') { /* Process leading zeroes */
3994 while (*ptr1
!= '\0') {
3995 if (!first
) first
= ptr2
;
4002 /* We never found a non-zero digit, then our number is either
4003 * smaller than the requested precision, or 0.0 */
4008 first
= data
->efcvt_buffer
;
4013 *decpt
= dec2
? dec2
: dec1
;
4017 /***********************************************************************
4018 * _fcvt_s (MSVCRT.@)
4020 int CDECL
_fcvt_s(char* outbuffer
, size_t size
, double number
, int ndigits
, int *decpt
, int *sign
)
4022 int stop
, dec1
, dec2
;
4023 char *ptr1
, *ptr2
, *first
;
4024 char buf
[80]; /* ought to be enough */
4025 char decimal_separator
= get_locinfo()->lconv
->decimal_point
[0];
4027 if (!outbuffer
|| !decpt
|| !sign
|| size
== 0)
4033 stop
= _snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
4045 /* For numbers below the requested resolution, work out where
4046 the decimal point will be rather than finding it in the string */
4047 if (number
< 1.0 && number
> 0.0) {
4048 dec2
= log10(number
+ 1e-10);
4049 if (-dec2
<= ndigits
) dec2
= 0;
4052 /* If requested digits is zero or less, we will need to truncate
4053 * the returned string */
4058 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
4059 while (*ptr1
!= '\0' && *ptr1
!= decimal_separator
) {
4060 if (!first
) first
= ptr2
;
4061 if ((ptr1
- buf
) < stop
) {
4075 while (*ptr1
== '0') { /* Process leading zeroes */
4076 if (number
== 0.0 && size
> 1) {
4084 while (*ptr1
!= '\0') {
4085 if (!first
) first
= ptr2
;
4095 /* We never found a non-zero digit, then our number is either
4096 * smaller than the requested precision, or 0.0 */
4097 if (!first
&& (number
<= 0.0))
4100 *decpt
= dec2
? dec2
: dec1
;
4104 /***********************************************************************
4107 char * CDECL
_gcvt( double number
, int ndigit
, char *buff
)
4119 sprintf(buff
, "%.*g", ndigit
, number
);
4123 /***********************************************************************
4124 * _gcvt_s (MSVCRT.@)
4126 int CDECL
_gcvt_s(char *buff
, size_t size
, double number
, int digits
)
4135 if( digits
<0 || digits
>=size
) {
4143 len
= _scprintf("%.*g", digits
, number
);
4150 sprintf(buff
, "%.*g", digits
, number
);
4154 #include <stdlib.h> /* div_t, ldiv_t */
4156 /*********************************************************************
4159 * [i386] Windows binary compatible - returns the struct in eax/edx.
4162 unsigned __int64 CDECL
div(int num
, int denom
)
4166 unsigned __int64 uint64
;
4169 ret
.div
.quot
= num
/ denom
;
4170 ret
.div
.rem
= num
% denom
;
4174 /*********************************************************************
4177 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
4179 div_t CDECL
div(int num
, int denom
)
4183 ret
.quot
= num
/ denom
;
4184 ret
.rem
= num
% denom
;
4187 #endif /* ifdef __i386__ */
4190 /*********************************************************************
4193 * [i386] Windows binary compatible - returns the struct in eax/edx.
4196 unsigned __int64 CDECL
ldiv(__msvcrt_long num
, __msvcrt_long denom
)
4200 unsigned __int64 uint64
;
4203 ret
.ldiv
.quot
= num
/ denom
;
4204 ret
.ldiv
.rem
= num
% denom
;
4208 /*********************************************************************
4211 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
4213 ldiv_t CDECL
ldiv(__msvcrt_long num
, __msvcrt_long denom
)
4217 ret
.quot
= num
/ denom
;
4218 ret
.rem
= num
% denom
;
4221 #endif /* ifdef __i386__ */
4224 /*********************************************************************
4225 * lldiv (MSVCR100.@)
4227 lldiv_t CDECL
lldiv(__int64 num
, __int64 denom
)
4231 ret
.quot
= num
/ denom
;
4232 ret
.rem
= num
% denom
;
4240 /*********************************************************************
4241 * _adjust_fdiv (MSVCRT.@)
4242 * Used by the MSVC compiler to work around the Pentium FDIV bug.
4244 int MSVCRT__adjust_fdiv
= 0;
4246 /***********************************************************************
4247 * _adj_fdiv_m16i (MSVCRT.@)
4250 * I _think_ this function is intended to work around the Pentium
4253 void __stdcall
_adj_fdiv_m16i( short arg
)
4255 TRACE("(): stub\n");
4258 /***********************************************************************
4259 * _adj_fdiv_m32 (MSVCRT.@)
4262 * I _think_ this function is intended to work around the Pentium
4265 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
4267 TRACE("(): stub\n");
4270 /***********************************************************************
4271 * _adj_fdiv_m32i (MSVCRT.@)
4274 * I _think_ this function is intended to work around the Pentium
4277 void __stdcall
_adj_fdiv_m32i( int arg
)
4279 TRACE("(): stub\n");
4282 /***********************************************************************
4283 * _adj_fdiv_m64 (MSVCRT.@)
4286 * I _think_ this function is intended to work around the Pentium
4289 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
4291 TRACE("(): stub\n");
4294 /***********************************************************************
4295 * _adj_fdiv_r (MSVCRT.@)
4297 * This function is likely to have the wrong number of arguments.
4300 * I _think_ this function is intended to work around the Pentium
4303 void _adj_fdiv_r(void)
4305 TRACE("(): stub\n");
4308 /***********************************************************************
4309 * _adj_fdivr_m16i (MSVCRT.@)
4312 * I _think_ this function is intended to work around the Pentium
4315 void __stdcall
_adj_fdivr_m16i( short arg
)
4317 TRACE("(): stub\n");
4320 /***********************************************************************
4321 * _adj_fdivr_m32 (MSVCRT.@)
4324 * I _think_ this function is intended to work around the Pentium
4327 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
4329 TRACE("(): stub\n");
4332 /***********************************************************************
4333 * _adj_fdivr_m32i (MSVCRT.@)
4336 * I _think_ this function is intended to work around the Pentium
4339 void __stdcall
_adj_fdivr_m32i( int arg
)
4341 TRACE("(): stub\n");
4344 /***********************************************************************
4345 * _adj_fdivr_m64 (MSVCRT.@)
4348 * I _think_ this function is intended to work around the Pentium
4351 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
4353 TRACE("(): stub\n");
4356 /***********************************************************************
4357 * _adj_fpatan (MSVCRT.@)
4359 * This function is likely to have the wrong number of arguments.
4362 * I _think_ this function is intended to work around the Pentium
4365 void _adj_fpatan(void)
4367 TRACE("(): stub\n");
4370 /***********************************************************************
4371 * _adj_fprem (MSVCRT.@)
4373 * This function is likely to have the wrong number of arguments.
4376 * I _think_ this function is intended to work around the Pentium
4379 void _adj_fprem(void)
4381 TRACE("(): stub\n");
4384 /***********************************************************************
4385 * _adj_fprem1 (MSVCRT.@)
4387 * This function is likely to have the wrong number of arguments.
4390 * I _think_ this function is intended to work around the Pentium
4393 void _adj_fprem1(void)
4395 TRACE("(): stub\n");
4398 /***********************************************************************
4399 * _adj_fptan (MSVCRT.@)
4401 * This function is likely to have the wrong number of arguments.
4404 * I _think_ this function is intended to work around the Pentium
4407 void _adj_fptan(void)
4409 TRACE("(): stub\n");
4412 /***********************************************************************
4413 * _safe_fdiv (MSVCRT.@)
4415 * This function is likely to have the wrong number of arguments.
4418 * I _think_ this function is intended to work around the Pentium
4421 void _safe_fdiv(void)
4423 TRACE("(): stub\n");
4426 /***********************************************************************
4427 * _safe_fdivr (MSVCRT.@)
4429 * This function is likely to have the wrong number of arguments.
4432 * I _think_ this function is intended to work around the Pentium
4435 void _safe_fdivr(void)
4437 TRACE("(): stub\n");
4440 /***********************************************************************
4441 * _safe_fprem (MSVCRT.@)
4443 * This function is likely to have the wrong number of arguments.
4446 * I _think_ this function is intended to work around the Pentium
4449 void _safe_fprem(void)
4451 TRACE("(): stub\n");
4454 /***********************************************************************
4455 * _safe_fprem1 (MSVCRT.@)
4458 * This function is likely to have the wrong number of arguments.
4461 * I _think_ this function is intended to work around the Pentium
4464 void _safe_fprem1(void)
4466 TRACE("(): stub\n");
4469 /***********************************************************************
4470 * __libm_sse2_acos (MSVCRT.@)
4472 void __cdecl
__libm_sse2_acos(void)
4475 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4477 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4480 /***********************************************************************
4481 * __libm_sse2_acosf (MSVCRT.@)
4483 void __cdecl
__libm_sse2_acosf(void)
4486 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4488 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4491 /***********************************************************************
4492 * __libm_sse2_asin (MSVCRT.@)
4494 void __cdecl
__libm_sse2_asin(void)
4497 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4499 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4502 /***********************************************************************
4503 * __libm_sse2_asinf (MSVCRT.@)
4505 void __cdecl
__libm_sse2_asinf(void)
4508 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4510 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4513 /***********************************************************************
4514 * __libm_sse2_atan (MSVCRT.@)
4516 void __cdecl
__libm_sse2_atan(void)
4519 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4521 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4524 /***********************************************************************
4525 * __libm_sse2_atan2 (MSVCRT.@)
4527 void __cdecl
__libm_sse2_atan2(void)
4530 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
4531 d1
= atan2( d1
, d2
);
4532 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
4535 /***********************************************************************
4536 * __libm_sse2_atanf (MSVCRT.@)
4538 void __cdecl
__libm_sse2_atanf(void)
4541 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4543 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4546 /***********************************************************************
4547 * __libm_sse2_cos (MSVCRT.@)
4549 void __cdecl
__libm_sse2_cos(void)
4552 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4554 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4557 /***********************************************************************
4558 * __libm_sse2_cosf (MSVCRT.@)
4560 void __cdecl
__libm_sse2_cosf(void)
4563 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4565 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4568 /***********************************************************************
4569 * __libm_sse2_exp (MSVCRT.@)
4571 void __cdecl
__libm_sse2_exp(void)
4574 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4576 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4579 /***********************************************************************
4580 * __libm_sse2_expf (MSVCRT.@)
4582 void __cdecl
__libm_sse2_expf(void)
4585 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4587 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4590 /***********************************************************************
4591 * __libm_sse2_log (MSVCRT.@)
4593 void __cdecl
__libm_sse2_log(void)
4596 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4598 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4601 /***********************************************************************
4602 * __libm_sse2_log10 (MSVCRT.@)
4604 void __cdecl
__libm_sse2_log10(void)
4607 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4609 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4612 /***********************************************************************
4613 * __libm_sse2_log10f (MSVCRT.@)
4615 void __cdecl
__libm_sse2_log10f(void)
4618 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4620 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4623 /***********************************************************************
4624 * __libm_sse2_logf (MSVCRT.@)
4626 void __cdecl
__libm_sse2_logf(void)
4629 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4631 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4634 /***********************************************************************
4635 * __libm_sse2_pow (MSVCRT.@)
4637 void __cdecl
__libm_sse2_pow(void)
4640 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
4642 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
4645 /***********************************************************************
4646 * __libm_sse2_powf (MSVCRT.@)
4648 void __cdecl
__libm_sse2_powf(void)
4651 __asm__
__volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1
), "=g" (f2
) );
4652 f1
= powf( f1
, f2
);
4653 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f1
) );
4656 /***********************************************************************
4657 * __libm_sse2_sin (MSVCRT.@)
4659 void __cdecl
__libm_sse2_sin(void)
4662 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4664 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4667 /***********************************************************************
4668 * __libm_sse2_sinf (MSVCRT.@)
4670 void __cdecl
__libm_sse2_sinf(void)
4673 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4675 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4678 /***********************************************************************
4679 * __libm_sse2_tan (MSVCRT.@)
4681 void __cdecl
__libm_sse2_tan(void)
4684 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4686 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4689 /***********************************************************************
4690 * __libm_sse2_tanf (MSVCRT.@)
4692 void __cdecl
__libm_sse2_tanf(void)
4695 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4697 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4700 /***********************************************************************
4701 * __libm_sse2_sqrt_precise (MSVCR110.@)
4703 void __cdecl
__libm_sse2_sqrt_precise(void)
4708 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4709 __control87_2(0, 0, NULL
, &cw
);
4713 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4717 if (!sqrt_validate(&d
, FALSE
))
4719 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4722 __asm__
__volatile__( "call " __ASM_NAME( "sse2_sqrt" ) );
4724 #endif /* __i386__ */
4726 /*********************************************************************
4727 * _fdclass (MSVCR120.@)
4729 * Copied from musl: src/math/__fpclassifyf.c
4731 short CDECL
_fdclass(float x
)
4733 union { float f
; UINT32 i
; } u
= { x
};
4734 int e
= u
.i
>> 23 & 0xff;
4736 if (!e
) return u
.i
<< 1 ? FP_SUBNORMAL
: FP_ZERO
;
4737 if (e
== 0xff) return u
.i
<< 9 ? FP_NAN
: FP_INFINITE
;
4741 /*********************************************************************
4742 * _dclass (MSVCR120.@)
4744 * Copied from musl: src/math/__fpclassify.c
4746 short CDECL
_dclass(double x
)
4748 union { double f
; UINT64 i
; } u
= { x
};
4749 int e
= u
.i
>> 52 & 0x7ff;
4751 if (!e
) return u
.i
<< 1 ? FP_SUBNORMAL
: FP_ZERO
;
4752 if (e
== 0x7ff) return (u
.i
<< 12) ? FP_NAN
: FP_INFINITE
;
4758 /*********************************************************************
4761 * Copied from musl: src/math/cbrt.c
4763 double CDECL
cbrt(double x
)
4765 static const UINT32 B1
= 715094163, B2
= 696219795;
4766 static const double P0
= 1.87595182427177009643,
4767 P1
= -1.88497979543377169875,
4768 P2
= 1.621429720105354466140,
4769 P3
= -0.758397934778766047437,
4770 P4
= 0.145996192886612446982;
4772 union {double f
; UINT64 i
;} u
= {x
};
4774 UINT32 hx
= u
.i
>> 32 & 0x7fffffff;
4776 if (hx
>= 0x7ff00000) /* cbrt(NaN,INF) is itself */
4779 if (hx
< 0x00100000) { /* zero or subnormal? */
4781 hx
= u
.i
>>32 & 0x7fffffff;
4788 u
.i
|= (UINT64
)hx
<< 32;
4791 r
= (t
* t
) * (t
/ x
);
4792 t
= t
* ((P0
+ r
* (P1
+ r
* P2
)) + ((r
* r
) * r
) * (P3
+ r
* P4
));
4795 u
.i
= (u
.i
+ 0x80000000) & 0xffffffffc0000000ULL
;
4801 r
= (r
- t
) / (w
+ r
);
4806 /*********************************************************************
4807 * cbrtf (MSVCR120.@)
4809 * Copied from musl: src/math/cbrtf.c
4811 float CDECL
cbrtf(float x
)
4813 static const unsigned B1
= 709958130, B2
= 642849266;
4816 union {float f
; UINT32 i
;} u
= {x
};
4817 UINT32 hx
= u
.i
& 0x7fffffff;
4819 if (hx
>= 0x7f800000)
4822 if (hx
< 0x00800000) { /* zero or subnormal? */
4826 hx
= u
.i
& 0x7fffffff;
4835 T
= T
* (x
+ x
+ r
) / (x
+ r
+ r
);
4838 T
= T
* (x
+ x
+ r
) / (x
+ r
+ r
);
4842 /*********************************************************************
4845 double CDECL
exp2(double x
)
4847 double ret
= unix_funcs
->exp2( x
);
4848 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4852 /*********************************************************************
4853 * exp2f (MSVCR120.@)
4855 float CDECL
exp2f(float x
)
4857 float ret
= unix_funcs
->exp2f( x
);
4858 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4862 /*********************************************************************
4863 * expm1 (MSVCR120.@)
4865 double CDECL
expm1(double x
)
4867 double ret
= unix_funcs
->expm1( x
);
4868 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4872 /*********************************************************************
4873 * expm1f (MSVCR120.@)
4875 float CDECL
expm1f(float x
)
4877 float ret
= unix_funcs
->expm1f( x
);
4878 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4882 /*********************************************************************
4883 * log1p (MSVCR120.@)
4885 double CDECL
log1p(double x
)
4887 if (x
< -1) *_errno() = EDOM
;
4888 else if (x
== -1) *_errno() = ERANGE
;
4889 return unix_funcs
->log1p( x
);
4892 /*********************************************************************
4893 * log1pf (MSVCR120.@)
4895 float CDECL
log1pf(float x
)
4897 if (x
< -1) *_errno() = EDOM
;
4898 else if (x
== -1) *_errno() = ERANGE
;
4899 return unix_funcs
->log1pf( x
);
4902 /*********************************************************************
4905 double CDECL
log2(double x
)
4907 if (x
< 0) *_errno() = EDOM
;
4908 else if (x
== 0) *_errno() = ERANGE
;
4909 return unix_funcs
->log2( x
);
4912 /*********************************************************************
4913 * log2f (MSVCR120.@)
4915 float CDECL
log2f(float x
)
4917 if (x
< 0) *_errno() = EDOM
;
4918 else if (x
== 0) *_errno() = ERANGE
;
4919 return unix_funcs
->log2f( x
);
4922 /*********************************************************************
4925 * Copied from musl: src/math/rint.c
4927 double CDECL
rint(double x
)
4929 static const double toint
= 1 / DBL_EPSILON
;
4931 ULONGLONG llx
= *(ULONGLONG
*)&x
;
4932 int e
= llx
>> 52 & 0x7ff;
4939 cw
= _controlfp(0, 0);
4940 if ((cw
& _MCW_PC
) != _PC_53
)
4941 _controlfp(_PC_53
, _MCW_PC
);
4943 y
= fp_barrier(x
- toint
) + toint
;
4945 y
= fp_barrier(x
+ toint
) - toint
;
4946 if ((cw
& _MCW_PC
) != _PC_53
)
4947 _controlfp(cw
, _MCW_PC
);
4949 return s
? -0.0 : 0;
4953 /*********************************************************************
4954 * rintf (MSVCR120.@)
4956 * Copied from musl: src/math/rintf.c
4958 float CDECL
rintf(float x
)
4960 static const float toint
= 1 / FLT_EPSILON
;
4962 unsigned int ix
= *(unsigned int*)&x
;
4963 int e
= ix
>> 23 & 0xff;
4970 y
= fp_barrierf(x
- toint
) + toint
;
4972 y
= fp_barrierf(x
+ toint
) - toint
;
4974 return s
? -0.0f
: 0.0f
;
4978 /*********************************************************************
4979 * lrint (MSVCR120.@)
4981 __msvcrt_long CDECL
lrint(double x
)
4986 if ((d
< 0 && d
!= (double)(__msvcrt_long
)d
)
4987 || (d
>= 0 && d
!= (double)(__msvcrt_ulong
)d
)) {
4994 /*********************************************************************
4995 * lrintf (MSVCR120.@)
4997 __msvcrt_long CDECL
lrintf(float x
)
5002 if ((f
< 0 && f
!= (float)(__msvcrt_long
)f
)
5003 || (f
>= 0 && f
!= (float)(__msvcrt_ulong
)f
)) {
5010 /*********************************************************************
5011 * llrint (MSVCR120.@)
5013 __int64 CDECL
llrint(double x
)
5018 if ((d
< 0 && d
!= (double)(__int64
)d
)
5019 || (d
>= 0 && d
!= (double)(unsigned __int64
)d
)) {
5026 /*********************************************************************
5027 * llrintf (MSVCR120.@)
5029 __int64 CDECL
llrintf(float x
)
5034 if ((f
< 0 && f
!= (float)(__int64
)f
)
5035 || (f
>= 0 && f
!= (float)(unsigned __int64
)f
)) {
5042 /*********************************************************************
5043 * round (MSVCR120.@)
5045 * Based on musl implementation: src/math/round.c
5047 double CDECL
round(double x
)
5049 ULONGLONG llx
= *(ULONGLONG
*)&x
, tmp
;
5050 int e
= (llx
>> 52 & 0x7ff) - 0x3ff;
5057 return signbit(x
) ? -1 : 1;
5059 tmp
= 0x000fffffffffffffULL
>> e
;
5062 llx
+= 0x0008000000000000ULL
>> e
;
5064 return *(double*)&llx
;
5067 /*********************************************************************
5068 * roundf (MSVCR120.@)
5070 * Copied from musl: src/math/roundf.c
5072 float CDECL
roundf(float x
)
5074 static const float toint
= 1 / FLT_EPSILON
;
5076 unsigned int ix
= *(unsigned int*)&x
;
5077 int e
= ix
>> 23 & 0xff;
5085 return 0 * *(float*)&ix
;
5086 y
= fp_barrierf(x
+ toint
) - toint
- x
;
5089 else if (y
<= -0.5f
)
5098 /*********************************************************************
5099 * lround (MSVCR120.@)
5101 * Copied from musl: src/math/lround.c
5103 __msvcrt_long CDECL
lround(double x
)
5105 double d
= round(x
);
5106 if (d
!= (double)(__msvcrt_long
)d
) {
5113 /*********************************************************************
5114 * lroundf (MSVCR120.@)
5116 * Copied from musl: src/math/lroundf.c
5118 __msvcrt_long CDECL
lroundf(float x
)
5120 float f
= roundf(x
);
5121 if (f
!= (float)(__msvcrt_long
)f
) {
5128 /*********************************************************************
5129 * llround (MSVCR120.@)
5131 * Copied from musl: src/math/llround.c
5133 __int64 CDECL
llround(double x
)
5135 double d
= round(x
);
5136 if (d
!= (double)(__int64
)d
) {
5143 /*********************************************************************
5144 * llroundf (MSVCR120.@)
5146 * Copied from musl: src/math/llroundf.c
5148 __int64 CDECL
llroundf(float x
)
5150 float f
= roundf(x
);
5151 if (f
!= (float)(__int64
)f
) {
5158 /*********************************************************************
5159 * trunc (MSVCR120.@)
5161 * Copied from musl: src/math/trunc.c
5163 double CDECL
trunc(double x
)
5165 union {double f
; UINT64 i
;} u
= {x
};
5166 int e
= (u
.i
>> 52 & 0x7ff) - 0x3ff + 12;
5180 /*********************************************************************
5181 * truncf (MSVCR120.@)
5183 * Copied from musl: src/math/truncf.c
5185 float CDECL
truncf(float x
)
5187 union {float f
; UINT32 i
;} u
= {x
};
5188 int e
= (u
.i
>> 23 & 0xff) - 0x7f + 9;
5202 /*********************************************************************
5203 * _dtest (MSVCR120.@)
5205 short CDECL
_dtest(double *x
)
5210 /*********************************************************************
5211 * _fdtest (MSVCR120.@)
5213 short CDECL
_fdtest(float *x
)
5215 return _fdclass(*x
);
5218 /*********************************************************************
5221 double CDECL
erf(double x
)
5223 return unix_funcs
->erf( x
);
5226 /*********************************************************************
5229 float CDECL
erff(float x
)
5231 return unix_funcs
->erff( x
);
5234 /*********************************************************************
5237 double CDECL
erfc(double x
)
5239 return unix_funcs
->erfc( x
);
5242 /*********************************************************************
5243 * erfcf (MSVCR120.@)
5245 float CDECL
erfcf(float x
)
5247 return unix_funcs
->erfcf( x
);
5250 /*********************************************************************
5251 * fmaxf (MSVCR120.@)
5253 float CDECL
fmaxf(float x
, float y
)
5260 return signbit(x
) ? y
: x
;
5264 /*********************************************************************
5267 double CDECL
fmax(double x
, double y
)
5274 return signbit(x
) ? y
: x
;
5278 /*********************************************************************
5279 * fdimf (MSVCR120.@)
5281 float CDECL
fdimf(float x
, float y
)
5287 return x
>y
? x
-y
: 0;
5290 /*********************************************************************
5293 double CDECL
fdim(double x
, double y
)
5299 return x
>y
? x
-y
: 0;
5302 /*********************************************************************
5303 * _fdsign (MSVCR120.@)
5305 int CDECL
_fdsign(float x
)
5307 union { float f
; UINT32 i
; } u
= { x
};
5308 return (u
.i
>> 16) & 0x8000;
5311 /*********************************************************************
5312 * _dsign (MSVCR120.@)
5314 int CDECL
_dsign(double x
)
5316 union { double f
; UINT64 i
; } u
= { x
};
5317 return (u
.i
>> 48) & 0x8000;
5321 /*********************************************************************
5322 * _dpcomp (MSVCR120.@)
5324 int CDECL
_dpcomp(double x
, double y
)
5326 if(isnan(x
) || isnan(y
))
5329 if(x
== y
) return 2;
5330 return x
< y
? 1 : 4;
5333 /*********************************************************************
5334 * _fdpcomp (MSVCR120.@)
5336 int CDECL
_fdpcomp(float x
, float y
)
5338 return _dpcomp(x
, y
);
5341 /*********************************************************************
5342 * fminf (MSVCR120.@)
5344 float CDECL
fminf(float x
, float y
)
5351 return signbit(x
) ? x
: y
;
5355 /*********************************************************************
5358 double CDECL
fmin(double x
, double y
)
5365 return signbit(x
) ? x
: y
;
5369 /*********************************************************************
5370 * asinh (MSVCR120.@)
5372 double CDECL
asinh(double x
)
5374 return unix_funcs
->asinh( x
);
5377 /*********************************************************************
5378 * asinhf (MSVCR120.@)
5380 float CDECL
asinhf(float x
)
5382 return unix_funcs
->asinhf( x
);
5385 /*********************************************************************
5386 * acosh (MSVCR120.@)
5388 double CDECL
acosh(double x
)
5393 feraiseexcept(FE_INVALID
);
5396 return unix_funcs
->acosh( x
);
5399 /*********************************************************************
5400 * acoshf (MSVCR120.@)
5402 float CDECL
acoshf(float x
)
5407 feraiseexcept(FE_INVALID
);
5410 return unix_funcs
->acoshf( x
);
5413 /*********************************************************************
5414 * atanh (MSVCR120.@)
5416 double CDECL
atanh(double x
)
5420 if (x
> 1 || x
< -1) {
5422 /* on Linux atanh returns -NAN in this case */
5423 feraiseexcept(FE_INVALID
);
5426 ret
= unix_funcs
->atanh( x
);
5428 if (!isfinite(ret
)) *_errno() = ERANGE
;
5432 /*********************************************************************
5433 * atanhf (MSVCR120.@)
5435 float CDECL
atanhf(float x
)
5439 if (x
> 1 || x
< -1) {
5441 feraiseexcept(FE_INVALID
);
5445 ret
= unix_funcs
->atanh( x
);
5447 if (!isfinite(ret
)) *_errno() = ERANGE
;
5451 #endif /* _MSVCR_VER>=120 */
5453 /*********************************************************************
5455 * scalbn (MSVCR120.@)
5456 * scalbln (MSVCR120.@)
5458 double CDECL
_scalb(double num
, __msvcrt_long power
)
5460 return ldexp(num
, power
);
5463 /*********************************************************************
5464 * _scalbf (MSVCRT.@)
5465 * scalbnf (MSVCR120.@)
5466 * scalblnf (MSVCR120.@)
5468 float CDECL
_scalbf(float num
, __msvcrt_long power
)
5470 return ldexp(num
, power
);
5475 /*********************************************************************
5476 * remainder (MSVCR120.@)
5478 double CDECL
remainder(double x
, double y
)
5480 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
5481 if(!isfinite(x
)) *_errno() = EDOM
;
5482 if(isnan(y
) || y
==0.0) *_errno() = EDOM
;
5483 return unix_funcs
->remainder( x
, y
);
5486 /*********************************************************************
5487 * remainderf (MSVCR120.@)
5489 float CDECL
remainderf(float x
, float y
)
5491 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
5492 if(!isfinite(x
)) *_errno() = EDOM
;
5493 if(isnan(y
) || y
==0.0f
) *_errno() = EDOM
;
5494 return unix_funcs
->remainderf( x
, y
);
5497 /*********************************************************************
5498 * remquo (MSVCR120.@)
5500 * Copied from musl: src/math/remquo.c
5502 double CDECL
remquo(double x
, double y
, int *quo
)
5504 UINT64 uxi
= *(UINT64
*)&x
;
5505 UINT64 uyi
= *(UINT64
*)&y
;
5506 int ex
= uxi
>> 52 & 0x7ff;
5507 int ey
= uyi
>> 52 & 0x7ff;
5514 if (y
== 0 || isinf(x
)) *_errno() = EDOM
;
5515 if (uyi
<< 1 == 0 || isnan(y
) || ex
== 0x7ff)
5516 return (x
* y
) / (x
* y
);
5520 /* normalize x and y */
5522 for (i
= uxi
<< 12; i
>> 63 == 0; ex
--, i
<<= 1);
5529 for (i
= uyi
<< 12; i
>> 63 == 0; ey
--, i
<<= 1);
5544 for (; ex
> ey
; ex
--) {
5561 for (; uxi
>> 52 == 0; uxi
<<= 1, ex
--);
5563 /* scale result and decide between |x| and |x|-|y| */
5566 uxi
|= (UINT64
)ex
<< 52;
5573 if (ex
== ey
|| (ex
+ 1 == ey
&& (2 * x
> y
|| (2 * x
== y
&& q
% 2)))) {
5578 *quo
= sx
^ sy
? -(int)q
: (int)q
;
5582 /*********************************************************************
5583 * remquof (MSVCR120.@)
5585 * Copied from musl: src/math/remquof.c
5587 float CDECL
remquof(float x
, float y
, int *quo
)
5589 UINT32 uxi
= *(UINT32
*)&x
;
5590 UINT32 uyi
= *(UINT32
*)&y
;
5591 int ex
= uxi
>> 23 & 0xff;
5592 int ey
= uyi
>> 23 & 0xff;
5598 if (y
== 0 || isinf(x
)) *_errno() = EDOM
;
5599 if (uyi
<< 1 == 0 || isnan(y
) || ex
== 0xff)
5600 return (x
* y
) / (x
* y
);
5604 /* normalize x and y */
5606 for (i
= uxi
<< 9; i
>> 31 == 0; ex
--, i
<<= 1);
5613 for (i
= uyi
<< 9; i
>> 31 == 0; ey
--, i
<<= 1);
5628 for (; ex
> ey
; ex
--) {
5645 for (; uxi
>> 23 == 0; uxi
<<= 1, ex
--);
5647 /* scale result and decide between |x| and |x|-|y| */
5650 uxi
|= (UINT32
)ex
<< 23;
5657 if (ex
== ey
|| (ex
+ 1 == ey
&& (2 * x
> y
|| (2 * x
== y
&& q
% 2)))) {
5662 *quo
= sx
^ sy
? -(int)q
: (int)q
;
5666 /*********************************************************************
5667 * lgamma (MSVCR120.@)
5669 double CDECL
lgamma(double x
)
5671 return unix_funcs
->lgamma( x
);
5674 /*********************************************************************
5675 * lgammaf (MSVCR120.@)
5677 float CDECL
lgammaf(float x
)
5679 return unix_funcs
->lgammaf( x
);
5682 /*********************************************************************
5683 * tgamma (MSVCR120.@)
5685 double CDECL
tgamma(double x
)
5687 return unix_funcs
->tgamma( x
);
5690 /*********************************************************************
5691 * tgammaf (MSVCR120.@)
5693 float CDECL
tgammaf(float x
)
5695 return unix_funcs
->tgammaf( x
);
5698 /*********************************************************************
5701 double CDECL
nan(const char *tagp
)
5703 /* Windows ignores input (MSDN) */
5707 /*********************************************************************
5710 float CDECL
nanf(const char *tagp
)
5715 /*********************************************************************
5716 * _except1 (MSVCR120.@)
5718 * - find meaning of ignored cw and operation bits
5721 double CDECL
_except1(DWORD fpe
, _FP_OPERATION_CODE op
, double arg
, double res
, DWORD cw
, void *unk
)
5723 ULONG_PTR exception_arg
;
5724 DWORD exception
= 0;
5729 TRACE("(%x %x %lf %lf %x %p)\n", fpe
, op
, arg
, res
, cw
, unk
);
5732 cw
= ((cw
>> 7) & 0x3f) | ((cw
>> 3) & 0xc00);
5734 operation
= op
<< 5;
5735 exception_arg
= (ULONG_PTR
)&operation
;
5737 if (fpe
& 0x1) { /* overflow */
5738 if ((fpe
== 0x1 && (cw
& 0x8)) || (fpe
==0x11 && (cw
& 0x28))) {
5739 /* 32-bit version also sets SW_INEXACT here */
5740 raise
|= FE_OVERFLOW
;
5741 if (fpe
& 0x10) raise
|= FE_INEXACT
;
5742 res
= signbit(res
) ? -INFINITY
: INFINITY
;
5744 exception
= EXCEPTION_FLT_OVERFLOW
;
5746 } else if (fpe
& 0x2) { /* underflow */
5747 if ((fpe
== 0x2 && (cw
& 0x10)) || (fpe
==0x12 && (cw
& 0x30))) {
5748 raise
|= FE_UNDERFLOW
;
5749 if (fpe
& 0x10) raise
|= FE_INEXACT
;
5750 res
= signbit(res
) ? -0.0 : 0.0;
5752 exception
= EXCEPTION_FLT_UNDERFLOW
;
5754 } else if (fpe
& 0x4) { /* zerodivide */
5755 if ((fpe
== 0x4 && (cw
& 0x4)) || (fpe
==0x14 && (cw
& 0x24))) {
5756 raise
|= FE_DIVBYZERO
;
5757 if (fpe
& 0x10) raise
|= FE_INEXACT
;
5759 exception
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
5761 } else if (fpe
& 0x8) { /* invalid */
5762 if (fpe
== 0x8 && (cw
& 0x1)) {
5763 raise
|= FE_INVALID
;
5765 exception
= EXCEPTION_FLT_INVALID_OPERATION
;
5767 } else if (fpe
& 0x10) { /* inexact */
5768 if (fpe
== 0x10 && (cw
& 0x20)) {
5769 raise
|= FE_INEXACT
;
5771 exception
= EXCEPTION_FLT_INEXACT_RESULT
;
5777 feraiseexcept(raise
);
5779 RaiseException(exception
, 0, 1, &exception_arg
);
5781 if (cw
& 0x1) fpword
|= _EM_INVALID
;
5782 if (cw
& 0x2) fpword
|= _EM_DENORMAL
;
5783 if (cw
& 0x4) fpword
|= _EM_ZERODIVIDE
;
5784 if (cw
& 0x8) fpword
|= _EM_OVERFLOW
;
5785 if (cw
& 0x10) fpword
|= _EM_UNDERFLOW
;
5786 if (cw
& 0x20) fpword
|= _EM_INEXACT
;
5789 case 0xc00: fpword
|= _RC_UP
|_RC_DOWN
; break;
5790 case 0x800: fpword
|= _RC_UP
; break;
5791 case 0x400: fpword
|= _RC_DOWN
; break;
5795 case 0x0: fpword
|= _PC_24
; break;
5796 case 0x200: fpword
|= _PC_53
; break;
5797 case 0x300: fpword
|= _PC_64
; break;
5799 if (cw
& 0x1000) fpword
|= _IC_AFFINE
;
5800 _control87(fpword
, 0xffffffff);
5805 _Dcomplex
* CDECL
_Cbuild(_Dcomplex
*ret
, double r
, double i
)
5812 double CDECL
MSVCR120_creal(_Dcomplex z
)
5817 /*********************************************************************
5818 * ilogb (MSVCR120.@)
5820 * Copied from musl: src/math/ilogb.c
5822 int CDECL
ilogb(double x
)
5824 union { double f
; UINT64 i
; } u
= { x
};
5825 int e
= u
.i
>> 52 & 0x7ff;
5830 if (u
.i
== 0) return FP_ILOGB0
;
5832 for (e
= -0x3ff; u
.i
>> 63 == 0; e
--, u
.i
<<= 1);
5835 if (e
== 0x7ff) return u
.i
<< 12 ? FP_ILOGBNAN
: INT_MAX
;
5839 /*********************************************************************
5840 * ilogbf (MSVCR120.@)
5842 * Copied from musl: src/math/ilogbf.c
5844 int CDECL
ilogbf(float x
)
5846 union { float f
; UINT32 i
; } u
= { x
};
5847 int e
= u
.i
>> 23 & 0xff;
5852 if (u
.i
== 0) return FP_ILOGB0
;
5854 for (e
= -0x7f; u
.i
>> 31 == 0; e
--, u
.i
<<= 1);
5857 if (e
== 0xff) return u
.i
<< 9 ? FP_ILOGBNAN
: INT_MAX
;
5860 #endif /* _MSVCR_VER>=120 */