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 float CDECL
fmodf( float x
, float y
)
626 float ret
= unix_funcs
->fmodf( x
, y
);
627 if (!isfinite(x
) || !isfinite(y
)) return math_error(_DOMAIN
, "fmodf", x
, 0, ret
);
631 /*********************************************************************
634 float CDECL
logf( float x
)
636 float ret
= unix_funcs
->logf( x
);
637 if (x
< 0.0) return math_error(_DOMAIN
, "logf", x
, 0, ret
);
638 if (x
== 0.0) return math_error(_SING
, "logf", x
, 0, ret
);
642 /*********************************************************************
645 float CDECL
log10f( float x
)
647 float ret
= unix_funcs
->log10f( x
);
648 if (x
< 0.0) return math_error(_DOMAIN
, "log10f", x
, 0, ret
);
649 if (x
== 0.0) return math_error(_SING
, "log10f", x
, 0, ret
);
653 /*********************************************************************
656 float CDECL
powf( float x
, float y
)
658 float z
= unix_funcs
->powf(x
,y
);
659 if (x
< 0 && y
!= floorf(y
)) return math_error(_DOMAIN
, "powf", x
, y
, z
);
660 if (!x
&& isfinite(y
) && y
< 0) return math_error(_SING
, "powf", x
, y
, z
);
661 if (isfinite(x
) && isfinite(y
) && !isfinite(z
)) return math_error(_OVERFLOW
, "powf", x
, y
, z
);
662 if (x
&& isfinite(x
) && isfinite(y
) && !z
) return math_error(_UNDERFLOW
, "powf", x
, y
, z
);
666 /*********************************************************************
669 float CDECL
sinf( float x
)
671 float ret
= unix_funcs
->sinf( x
);
672 if (!isfinite(x
)) return math_error(_DOMAIN
, "sinf", x
, 0, ret
);
676 /*********************************************************************
679 float CDECL
sinhf( float x
)
681 float ret
= unix_funcs
->sinhf( x
);
682 if (isnan(x
)) return math_error(_DOMAIN
, "sinhf", x
, 0, ret
);
686 static BOOL
sqrtf_validate( float *x
)
688 short c
= _fdclass(*x
);
690 if (c
== FP_ZERO
) return FALSE
;
691 if (c
== FP_NAN
) return FALSE
;
694 *x
= math_error(_DOMAIN
, "sqrtf", *x
, 0, ret_nan(TRUE
));
697 if (c
== FP_INFINITE
) return FALSE
;
701 #if defined(__x86_64__) || defined(__i386__)
702 float CDECL
sse2_sqrtf(float);
703 __ASM_GLOBAL_FUNC( sse2_sqrtf
,
704 "sqrtss %xmm0, %xmm0\n\t"
708 /*********************************************************************
711 * Copied from musl: src/math/sqrtf.c
713 float CDECL
sqrtf( float x
)
716 if (!sqrtf_validate(&x
))
719 return sse2_sqrtf(x
);
721 static const float tiny
= 1.0e-30;
729 if (!sqrtf_validate(&x
))
734 if (m
== 0) { /* subnormal x */
735 for (i
= 0; (ix
& 0x00800000) == 0; i
++)
739 m
-= 127; /* unbias exponent */
740 ix
= (ix
& 0x007fffff) | 0x00800000;
741 if (m
& 1) /* odd m, double x to make it even */
743 m
>>= 1; /* m = [m/2] */
745 /* generate sqrt(x) bit by bit */
747 q
= s
= 0; /* q = sqrt(x) */
748 r
= 0x01000000; /* r = moving bit from right to left */
761 /* use floating add to find out rounding direction */
763 z
= 1.0f
- tiny
; /* raise inexact flag */
772 ix
= (q
>> 1) + 0x3f000000;
773 r
= ix
+ ((unsigned int)m
<< 23);
779 /*********************************************************************
782 float CDECL
tanf( float x
)
784 float ret
= unix_funcs
->tanf(x
);
785 if (!isfinite(x
)) return math_error(_DOMAIN
, "tanf", x
, 0, ret
);
789 /*********************************************************************
792 float CDECL
tanhf( float x
)
794 float ret
= unix_funcs
->tanhf(x
);
795 if (!isfinite(x
)) return math_error(_DOMAIN
, "tanhf", x
, 0, ret
);
799 /*********************************************************************
802 * Copied from musl: src/math/ceilf.c
804 float CDECL
ceilf( float x
)
806 union {float f
; UINT32 i
;} u
= {x
};
807 int e
= (int)(u
.i
>> 23 & 0xff) - 0x7f;
828 /*********************************************************************
831 * Copied from musl: src/math/floorf.c
833 float CDECL
floorf( float x
)
835 union {float f
; UINT32 i
;} u
= {x
};
836 int e
= (int)(u
.i
>> 23 & 0xff) - 0x7f;
857 /*********************************************************************
860 float CDECL
frexpf( float x
, int *exp
)
862 return unix_funcs
->frexpf( x
, exp
);
865 /*********************************************************************
868 float CDECL
modff( float x
, float *iptr
)
870 return unix_funcs
->modff( x
, iptr
);
875 #if !defined(__i386__) && !defined(__x86_64__) && (_MSVCR_VER == 0 || _MSVCR_VER >= 110)
877 /*********************************************************************
880 * Copied from musl: src/math/fabsf.c
882 float CDECL
fabsf( float x
)
884 union { float f
; UINT32 i
; } u
= { x
};
891 /*********************************************************************
894 * Copied from musl: src/math/acos.c
896 static double acos_R(double z
)
898 static const double pS0
= 1.66666666666666657415e-01,
899 pS1
= -3.25565818622400915405e-01,
900 pS2
= 2.01212532134862925881e-01,
901 pS3
= -4.00555345006794114027e-02,
902 pS4
= 7.91534994289814532176e-04,
903 pS5
= 3.47933107596021167570e-05,
904 qS1
= -2.40339491173441421878e+00,
905 qS2
= 2.02094576023350569471e+00,
906 qS3
= -6.88283971605453293030e-01,
907 qS4
= 7.70381505559019352791e-02;
910 p
= z
* (pS0
+ z
* (pS1
+ z
* (pS2
+ z
* (pS3
+ z
* (pS4
+ z
* pS5
)))));
911 q
= 1.0 + z
* (qS1
+ z
* (qS2
+ z
* (qS3
+ z
* qS4
)));
915 double CDECL
acos( double x
)
917 static const double pio2_hi
= 1.57079632679489655800e+00,
918 pio2_lo
= 6.12323399573676603587e-17;
920 double z
, w
, s
, c
, df
;
924 hx
= *(ULONGLONG
*)&x
>> 32;
925 ix
= hx
& 0x7fffffff;
926 /* |x| >= 1 or nan */
927 if (ix
>= 0x3ff00000) {
930 lx
= *(ULONGLONG
*)&x
;
931 if (((ix
- 0x3ff00000) | lx
) == 0) {
932 /* acos(1)=0, acos(-1)=pi */
934 return 2 * pio2_hi
+ 7.5231638452626401e-37;
937 if (isnan(x
)) return x
;
938 return math_error(_DOMAIN
, "acos", x
, 0, 0 / (x
- x
));
941 if (ix
< 0x3fe00000) {
942 if (ix
<= 0x3c600000) /* |x| < 2**-57 */
943 return pio2_hi
+ 7.5231638452626401e-37;
944 return pio2_hi
- (x
- (pio2_lo
- x
* acos_R(x
* x
)));
950 w
= acos_R(z
) * s
- pio2_lo
;
951 return 2 * (pio2_hi
- (s
+ w
));
957 llx
= (*(ULONGLONG
*)&df
>> 32) << 32;
959 c
= (z
- df
* df
) / (s
+ df
);
960 w
= acos_R(z
) * s
+ c
;
964 /*********************************************************************
967 * Copied from musl: src/math/asin.c
969 static double asin_R(double z
)
971 /* coefficients for R(x^2) */
972 static const double pS0
= 1.66666666666666657415e-01,
973 pS1
= -3.25565818622400915405e-01,
974 pS2
= 2.01212532134862925881e-01,
975 pS3
= -4.00555345006794114027e-02,
976 pS4
= 7.91534994289814532176e-04,
977 pS5
= 3.47933107596021167570e-05,
978 qS1
= -2.40339491173441421878e+00,
979 qS2
= 2.02094576023350569471e+00,
980 qS3
= -6.88283971605453293030e-01,
981 qS4
= 7.70381505559019352791e-02;
984 p
= z
* (pS0
+ z
* (pS1
+ z
* (pS2
+ z
* (pS3
+ z
* (pS4
+ z
* pS5
)))));
985 q
= 1.0 + z
* (qS1
+ z
* (qS2
+ z
* (qS3
+ z
* qS4
)));
990 double CDECL
x87_asin(double);
991 __ASM_GLOBAL_FUNC( x87_asin
,
1006 double CDECL
asin( double x
)
1008 static const double pio2_hi
= 1.57079632679489655800e+00,
1009 pio2_lo
= 6.12323399573676603587e-17;
1012 unsigned int hx
, ix
;
1015 unsigned int x87_cw
, sse2_cw
;
1018 hx
= *(ULONGLONG
*)&x
>> 32;
1019 ix
= hx
& 0x7fffffff;
1020 /* |x| >= 1 or nan */
1021 if (ix
>= 0x3ff00000) {
1023 lx
= *(ULONGLONG
*)&x
;
1024 if (((ix
- 0x3ff00000) | lx
) == 0)
1025 /* asin(1) = +-pi/2 with inexact */
1026 return x
* pio2_hi
+ 7.5231638452626401e-37;
1030 return math_error(_DOMAIN
, "asin", x
, 0, x
);
1035 return math_error(_DOMAIN
, "asin", x
, 0, 0 / (x
- x
));
1039 __control87_2(0, 0, &x87_cw
, &sse2_cw
);
1040 if (!sse2_enabled
|| (x87_cw
& _MCW_EM
) != _MCW_EM
1041 || (sse2_cw
& (_MCW_EM
| _MCW_RC
)) != _MCW_EM
)
1046 if (ix
< 0x3fe00000) {
1047 /* if 0x1p-1022 <= |x| < 0x1p-26, avoid raising underflow */
1048 if (ix
< 0x3e500000 && ix
>= 0x00100000)
1050 return x
+ x
* asin_R(x
* x
);
1052 /* 1 > |x| >= 0.5 */
1053 z
= (1 - fabs(x
)) * 0.5;
1056 if (ix
>= 0x3fef3333) { /* if |x| > 0.975 */
1057 x
= pio2_hi
- (2 * (s
+ s
* r
) - pio2_lo
);
1062 llx
= (*(ULONGLONG
*)&f
>> 32) << 32;
1064 c
= (z
- f
* f
) / (s
+ f
);
1065 x
= 0.5 * pio2_hi
- (2 * s
* r
- (pio2_lo
- 2 * c
) - (0.5 * pio2_hi
- 2 * f
));
1072 /*********************************************************************
1075 * Copied from musl: src/math/atan.c
1077 double CDECL
atan( double x
)
1079 static const double atanhi
[] = {
1080 4.63647609000806093515e-01,
1081 7.85398163397448278999e-01,
1082 9.82793723247329054082e-01,
1083 1.57079632679489655800e+00,
1085 static const double atanlo
[] = {
1086 2.26987774529616870924e-17,
1087 3.06161699786838301793e-17,
1088 1.39033110312309984516e-17,
1089 6.12323399573676603587e-17,
1091 static const double aT
[] = {
1092 3.33333333333329318027e-01,
1093 -1.99999999998764832476e-01,
1094 1.42857142725034663711e-01,
1095 -1.11111104054623557880e-01,
1096 9.09088713343650656196e-02,
1097 -7.69187620504482999495e-02,
1098 6.66107313738753120669e-02,
1099 -5.83357013379057348645e-02,
1100 4.97687799461593236017e-02,
1101 -3.65315727442169155270e-02,
1102 1.62858201153657823623e-02,
1105 double w
, s1
, s2
, z
;
1106 unsigned int ix
, sign
;
1110 if (isnan(x
)) return math_error(_DOMAIN
, "atan", x
, 0, x
);
1113 ix
= *(ULONGLONG
*)&x
>> 32;
1116 if (ix
>= 0x44100000) { /* if |x| >= 2^66 */
1119 z
= atanhi
[3] + 7.5231638452626401e-37;
1120 return sign
? -z
: z
;
1122 if (ix
< 0x3fdc0000) { /* |x| < 0.4375 */
1123 if (ix
< 0x3e400000) { /* |x| < 2^-27 */
1124 if (ix
< 0x00100000)
1125 /* raise underflow for subnormal x */
1126 fp_barrierf((float)x
);
1132 if (ix
< 0x3ff30000) { /* |x| < 1.1875 */
1133 if (ix
< 0x3fe60000) { /* 7/16 <= |x| < 11/16 */
1135 x
= (2.0 * x
- 1.0) / (2.0 + x
);
1136 } else { /* 11/16 <= |x| < 19/16 */
1138 x
= (x
- 1.0) / (x
+ 1.0);
1141 if (ix
< 0x40038000) { /* |x| < 2.4375 */
1143 x
= (x
- 1.5) / (1.0 + 1.5 * x
);
1144 } else { /* 2.4375 <= |x| < 2^66 */
1150 /* end of argument reduction */
1153 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
1154 s1
= z
* (aT
[0] + w
* (aT
[2] + w
* (aT
[4] + w
* (aT
[6] + w
* (aT
[8] + w
* aT
[10])))));
1155 s2
= w
* (aT
[1] + w
* (aT
[3] + w
* (aT
[5] + w
* (aT
[7] + w
* aT
[9]))));
1157 return x
- x
* (s1
+ s2
);
1158 z
= atanhi
[id
] - (x
* (s1
+ s2
) - atanlo
[id
] - x
);
1159 return sign
? -z
: z
;
1162 /*********************************************************************
1165 * Copied from musl: src/math/atan2.c
1167 double CDECL
atan2( double y
, double x
)
1169 static const double pi
= 3.1415926535897931160E+00,
1170 pi_lo
= 1.2246467991473531772E-16;
1173 unsigned int m
, lx
, ly
, ix
, iy
;
1175 if (isnan(x
) || isnan(y
))
1177 ix
= *(ULONGLONG
*)&x
>> 32;
1178 lx
= *(ULONGLONG
*)&x
;
1179 iy
= *(ULONGLONG
*)&y
>> 32;
1180 ly
= *(ULONGLONG
*)&y
;
1181 if (((ix
- 0x3ff00000) | lx
) == 0) /* x = 1.0 */
1183 m
= ((iy
>> 31) & 1) | ((ix
>> 30) & 2); /* 2*sign(x)+sign(y) */
1184 ix
= ix
& 0x7fffffff;
1185 iy
= iy
& 0x7fffffff;
1188 if ((iy
| ly
) == 0) {
1191 case 1: return y
; /* atan(+-0,+anything)=+-0 */
1192 case 2: return pi
; /* atan(+0,-anything) = pi */
1193 case 3: return -pi
; /* atan(-0,-anything) =-pi */
1198 return m
& 1 ? -pi
/ 2 : pi
/ 2;
1200 if (ix
== 0x7ff00000) {
1201 if (iy
== 0x7ff00000) {
1203 case 0: return pi
/ 4; /* atan(+INF,+INF) */
1204 case 1: return -pi
/ 4; /* atan(-INF,+INF) */
1205 case 2: return 3 * pi
/ 4; /* atan(+INF,-INF) */
1206 case 3: return -3 * pi
/ 4; /* atan(-INF,-INF) */
1210 case 0: return 0.0; /* atan(+...,+INF) */
1211 case 1: return -0.0; /* atan(-...,+INF) */
1212 case 2: return pi
; /* atan(+...,-INF) */
1213 case 3: return -pi
; /* atan(-...,-INF) */
1217 /* |y/x| > 0x1p64 */
1218 if (ix
+ (64 << 20) < iy
|| iy
== 0x7ff00000)
1219 return m
& 1 ? -pi
/ 2 : pi
/ 2;
1221 /* z = atan(|y/x|) without spurious underflow */
1222 if ((m
& 2) && iy
+ (64 << 20) < ix
) /* |y/x| < 0x1p-64, x<0 */
1225 z
= atan(fabs(y
/ x
));
1227 case 0: return z
; /* atan(+,+) */
1228 case 1: return -z
; /* atan(-,+) */
1229 case 2: return pi
- (z
- pi_lo
); /* atan(+,-) */
1230 default: /* case 3 */
1231 return (z
- pi_lo
) - pi
; /* atan(-,-) */
1235 /*********************************************************************
1238 double CDECL
cos( double x
)
1240 double ret
= unix_funcs
->cos( x
);
1241 if (!isfinite(x
)) return math_error(_DOMAIN
, "cos", x
, 0, ret
);
1245 /*********************************************************************
1248 double CDECL
cosh( double x
)
1250 double ret
= unix_funcs
->cosh( x
);
1251 if (isnan(x
)) return math_error(_DOMAIN
, "cosh", x
, 0, ret
);
1255 /*********************************************************************
1258 double CDECL
exp( double x
)
1260 double ret
= unix_funcs
->exp( x
);
1261 if (isnan(x
)) return math_error(_DOMAIN
, "exp", x
, 0, ret
);
1262 if (isfinite(x
) && !ret
) return math_error(_UNDERFLOW
, "exp", x
, 0, ret
);
1263 if (isfinite(x
) && !isfinite(ret
)) return math_error(_OVERFLOW
, "exp", x
, 0, ret
);
1267 /*********************************************************************
1270 double CDECL
fmod( double x
, double y
)
1272 double ret
= unix_funcs
->fmod( x
, y
);
1273 if (!isfinite(x
) || !isfinite(y
)) return math_error(_DOMAIN
, "fmod", x
, y
, ret
);
1277 /*********************************************************************
1280 double CDECL
log( double x
)
1282 double ret
= unix_funcs
->log( x
);
1283 if (x
< 0.0) return math_error(_DOMAIN
, "log", x
, 0, ret
);
1284 if (x
== 0.0) return math_error(_SING
, "log", x
, 0, ret
);
1288 /*********************************************************************
1291 double CDECL
log10( double x
)
1293 double ret
= unix_funcs
->log10( x
);
1294 if (x
< 0.0) return math_error(_DOMAIN
, "log10", x
, 0, ret
);
1295 if (x
== 0.0) return math_error(_SING
, "log10", x
, 0, ret
);
1299 /*********************************************************************
1302 double CDECL
pow( double x
, double y
)
1304 double z
= unix_funcs
->pow(x
,y
);
1305 if (x
< 0 && y
!= floor(y
))
1306 return math_error(_DOMAIN
, "pow", x
, y
, z
);
1307 if (!x
&& isfinite(y
) && y
< 0)
1308 return math_error(_SING
, "pow", x
, y
, z
);
1309 if (isfinite(x
) && isfinite(y
) && !isfinite(z
))
1310 return math_error(_OVERFLOW
, "pow", x
, y
, z
);
1311 if (x
&& isfinite(x
) && isfinite(y
) && !z
)
1312 return math_error(_UNDERFLOW
, "pow", x
, y
, z
);
1316 /*********************************************************************
1319 double CDECL
sin( double x
)
1321 double ret
= unix_funcs
->sin( x
);
1322 if (!isfinite(x
)) return math_error(_DOMAIN
, "sin", x
, 0, ret
);
1326 /*********************************************************************
1329 double CDECL
sinh( double x
)
1331 double ret
= unix_funcs
->sinh( x
);
1332 if (isnan(x
)) return math_error(_DOMAIN
, "sinh", x
, 0, ret
);
1336 static BOOL
sqrt_validate( double *x
, BOOL update_sw
)
1338 short c
= _dclass(*x
);
1340 if (c
== FP_ZERO
) return FALSE
;
1345 *x
= math_error(_DOMAIN
, "sqrt", *x
, 0, *x
);
1347 /* set signaling bit */
1348 *(ULONGLONG
*)x
|= 0x8000000000000ULL
;
1354 *x
= math_error(_DOMAIN
, "sqrt", *x
, 0, ret_nan(update_sw
));
1357 if (c
== FP_INFINITE
) return FALSE
;
1361 #if defined(__x86_64__) || defined(__i386__)
1362 double CDECL
sse2_sqrt(double);
1363 __ASM_GLOBAL_FUNC( sse2_sqrt
,
1364 "sqrtsd %xmm0, %xmm0\n\t"
1369 double CDECL
x87_sqrt(double);
1370 __ASM_GLOBAL_FUNC( x87_sqrt
,
1378 /*********************************************************************
1381 * Copied from musl: src/math/sqrt.c
1383 double CDECL
sqrt( double x
)
1386 if (!sqrt_validate(&x
, TRUE
))
1389 return sse2_sqrt(x
);
1390 #elif defined( __i386__ )
1391 if (!sqrt_validate(&x
, TRUE
))
1396 static const double tiny
= 1.0e-300;
1399 int sign
= 0x80000000;
1401 unsigned int r
,t1
,s1
,ix1
,q1
;
1404 if (!sqrt_validate(&x
, TRUE
))
1407 ix
= *(ULONGLONG
*)&x
;
1413 if (m
== 0) { /* subnormal x */
1419 for (i
=0; (ix0
& 0x00100000) == 0; i
++)
1422 ix0
|= ix1
>> (32 - i
);
1425 m
-= 1023; /* unbias exponent */
1426 ix0
= (ix0
& 0x000fffff) | 0x00100000;
1427 if (m
& 1) { /* odd m, double x to make it even */
1428 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1431 m
>>= 1; /* m = [m/2] */
1433 /* generate sqrt(x) bit by bit */
1434 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1436 q
= q1
= s0
= s1
= 0; /* [q,q1] = sqrt(x) */
1437 r
= 0x00200000; /* r = moving bit from right to left */
1446 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1455 if (t
< ix0
|| (t
== ix0
&& t1
<= ix1
)) {
1457 if ((t1
&sign
) == sign
&& (s1
& sign
) == 0)
1465 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1470 /* use floating add to find out rounding direction */
1471 if ((ix0
| ix1
) != 0) {
1472 z
= 1.0 - tiny
; /* raise inexact flag */
1475 if (q1
== (unsigned int)0xffffffff) {
1478 } else if (z
> 1.0) {
1479 if (q1
== (unsigned int)0xfffffffe)
1486 ix0
= (q
>> 1) + 0x3fe00000;
1490 ix
= ix0
+ ((unsigned int)m
<< 20);
1493 return *(double*)&ix
;
1497 /*********************************************************************
1500 double CDECL
tan( double x
)
1502 double ret
= unix_funcs
->tan(x
);
1503 if (!isfinite(x
)) return math_error(_DOMAIN
, "tan", x
, 0, ret
);
1507 /*********************************************************************
1510 double CDECL
tanh( double x
)
1512 double ret
= unix_funcs
->tanh(x
);
1513 if (isnan(x
)) return math_error(_DOMAIN
, "tanh", x
, 0, ret
);
1518 #if (defined(__GNUC__) || defined(__clang__)) && defined(__i386__)
1520 #define CREATE_FPU_FUNC1(name, call) \
1521 __ASM_GLOBAL_FUNC(name, \
1523 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1524 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1525 "movl %esp, %ebp\n\t" \
1526 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1527 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1528 "fstpl (%esp)\n\t" /* store function argument */ \
1530 "movl $1, %ecx\n\t" /* empty FPU stack */ \
1534 "and $0x4500, %ax\n\t" \
1535 "cmp $0x4100, %ax\n\t" \
1537 "fstpl (%esp,%ecx,8)\n\t" \
1542 "movl %ecx, -4(%ebp)\n\t" \
1543 "call " __ASM_NAME( #call ) "\n\t" \
1544 "movl -4(%ebp), %ecx\n\t" \
1545 "fstpl (%esp)\n\t" /* save result */ \
1546 "3:\n\t" /* restore FPU stack */ \
1548 "fldl (%esp,%ecx,8)\n\t" \
1549 "cmpl $0, %ecx\n\t" \
1552 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1553 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1556 #define CREATE_FPU_FUNC2(name, call) \
1557 __ASM_GLOBAL_FUNC(name, \
1559 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1560 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1561 "movl %esp, %ebp\n\t" \
1562 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1563 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1564 "fstpl 8(%esp)\n\t" /* store function argument */ \
1566 "fstpl (%esp)\n\t" \
1568 "movl $2, %ecx\n\t" /* empty FPU stack */ \
1572 "and $0x4500, %ax\n\t" \
1573 "cmp $0x4100, %ax\n\t" \
1575 "fstpl (%esp,%ecx,8)\n\t" \
1580 "movl %ecx, -4(%ebp)\n\t" \
1581 "call " __ASM_NAME( #call ) "\n\t" \
1582 "movl -4(%ebp), %ecx\n\t" \
1583 "fstpl 8(%esp)\n\t" /* save result */ \
1584 "3:\n\t" /* restore FPU stack */ \
1586 "fldl (%esp,%ecx,8)\n\t" \
1587 "cmpl $1, %ecx\n\t" \
1590 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1591 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1594 CREATE_FPU_FUNC1(_CIacos
, acos
)
1595 CREATE_FPU_FUNC1(_CIasin
, asin
)
1596 CREATE_FPU_FUNC1(_CIatan
, atan
)
1597 CREATE_FPU_FUNC2(_CIatan2
, atan2
)
1598 CREATE_FPU_FUNC1(_CIcos
, cos
)
1599 CREATE_FPU_FUNC1(_CIcosh
, cosh
)
1600 CREATE_FPU_FUNC1(_CIexp
, exp
)
1601 CREATE_FPU_FUNC2(_CIfmod
, fmod
)
1602 CREATE_FPU_FUNC1(_CIlog
, log
)
1603 CREATE_FPU_FUNC1(_CIlog10
, log10
)
1604 CREATE_FPU_FUNC2(_CIpow
, pow
)
1605 CREATE_FPU_FUNC1(_CIsin
, sin
)
1606 CREATE_FPU_FUNC1(_CIsinh
, sinh
)
1607 CREATE_FPU_FUNC1(_CIsqrt
, sqrt
)
1608 CREATE_FPU_FUNC1(_CItan
, tan
)
1609 CREATE_FPU_FUNC1(_CItanh
, tanh
)
1611 __ASM_GLOBAL_FUNC(_ftol
,
1613 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1614 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1615 "movl %esp, %ebp\n\t"
1616 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1617 "subl $12, %esp\n\t" /* sizeof(LONGLONG) + 2*sizeof(WORD) */
1619 "mov (%esp), %ax\n\t"
1620 "or $0xc00, %ax\n\t"
1621 "mov %ax, 2(%esp)\n\t"
1623 "fistpq 4(%esp)\n\t"
1625 "movl 4(%esp), %eax\n\t"
1626 "movl 8(%esp), %edx\n\t"
1628 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1629 __ASM_CFI(".cfi_same_value %ebp\n\t")
1632 #endif /* (defined(__GNUC__) || defined(__clang__)) && defined(__i386__) */
1634 /*********************************************************************
1635 * _fpclass (MSVCRT.@)
1637 int CDECL
_fpclass(double num
)
1639 union { double f
; UINT64 i
; } u
= { num
};
1640 int e
= u
.i
>> 52 & 0x7ff;
1646 if (u
.i
<< 1) return s
? _FPCLASS_ND
: _FPCLASS_PD
;
1647 return s
? _FPCLASS_NZ
: _FPCLASS_PZ
;
1649 if (u
.i
<< 12) return ((u
.i
>> 51) & 1) ? _FPCLASS_QNAN
: _FPCLASS_SNAN
;
1650 return s
? _FPCLASS_NINF
: _FPCLASS_PINF
;
1652 return s
? _FPCLASS_NN
: _FPCLASS_PN
;
1656 /*********************************************************************
1659 unsigned int CDECL
MSVCRT__rotl(unsigned int num
, int shift
)
1662 return (num
<< shift
) | (num
>> (32-shift
));
1665 /*********************************************************************
1668 __msvcrt_ulong CDECL
MSVCRT__lrotl(__msvcrt_ulong num
, int shift
)
1671 return (num
<< shift
) | (num
>> (32-shift
));
1674 /*********************************************************************
1677 __msvcrt_ulong CDECL
MSVCRT__lrotr(__msvcrt_ulong num
, int shift
)
1680 return (num
>> shift
) | (num
<< (32-shift
));
1683 /*********************************************************************
1686 unsigned int CDECL
MSVCRT__rotr(unsigned int num
, int shift
)
1689 return (num
>> shift
) | (num
<< (32-shift
));
1692 /*********************************************************************
1693 * _rotl64 (MSVCRT.@)
1695 unsigned __int64 CDECL
MSVCRT__rotl64(unsigned __int64 num
, int shift
)
1698 return (num
<< shift
) | (num
>> (64-shift
));
1701 /*********************************************************************
1702 * _rotr64 (MSVCRT.@)
1704 unsigned __int64 CDECL
MSVCRT__rotr64(unsigned __int64 num
, int shift
)
1707 return (num
>> shift
) | (num
<< (64-shift
));
1710 /*********************************************************************
1713 int CDECL
abs( int n
)
1715 return n
>= 0 ? n
: -n
;
1718 /*********************************************************************
1721 __msvcrt_long CDECL
labs( __msvcrt_long n
)
1723 return n
>= 0 ? n
: -n
;
1727 /*********************************************************************
1728 * llabs (MSVCR100.@)
1730 __int64 CDECL
llabs( __int64 n
)
1732 return n
>= 0 ? n
: -n
;
1737 /*********************************************************************
1738 * imaxabs (MSVCR120.@)
1740 intmax_t CDECL
imaxabs( intmax_t n
)
1742 return n
>= 0 ? n
: -n
;
1746 /*********************************************************************
1749 __int64 CDECL
_abs64( __int64 n
)
1751 return n
>= 0 ? n
: -n
;
1754 /*********************************************************************
1757 double CDECL
_logb(double num
)
1759 double ret
= unix_funcs
->logb(num
);
1760 if (isnan(num
)) return math_error(_DOMAIN
, "_logb", num
, 0, ret
);
1761 if (!num
) return math_error(_SING
, "_logb", num
, 0, ret
);
1765 /*********************************************************************
1768 double CDECL
_hypot(double x
, double y
)
1770 /* FIXME: errno handling */
1771 return unix_funcs
->hypot( x
, y
);
1774 /*********************************************************************
1775 * _hypotf (MSVCRT.@)
1777 float CDECL
_hypotf(float x
, float y
)
1779 /* FIXME: errno handling */
1780 return unix_funcs
->hypotf( x
, y
);
1783 /*********************************************************************
1786 * Based on musl: src/math/ceilf.c
1788 double CDECL
ceil( double x
)
1790 union {double f
; UINT64 i
;} u
= {x
};
1791 int e
= (u
.i
>> 52 & 0x7ff) - 0x3ff;
1797 m
= 0x000fffffffffffffULL
>> e
;
1812 /*********************************************************************
1815 * Based on musl: src/math/floorf.c
1817 double CDECL
floor( double x
)
1819 union {double f
; UINT64 i
;} u
= {x
};
1820 int e
= (int)(u
.i
>> 52 & 0x7ff) - 0x3ff;
1826 m
= 0x000fffffffffffffULL
>> e
;
1841 /*********************************************************************
1844 double CDECL
fma( double x
, double y
, double z
)
1846 double w
= unix_funcs
->fma(x
, y
, z
);
1847 if ((isinf(x
) && y
== 0) || (x
== 0 && isinf(y
))) *_errno() = EDOM
;
1848 else if (isinf(x
) && isinf(z
) && x
!= z
) *_errno() = EDOM
;
1849 else if (isinf(y
) && isinf(z
) && y
!= z
) *_errno() = EDOM
;
1853 /*********************************************************************
1856 float CDECL
fmaf( float x
, float y
, float z
)
1858 float w
= unix_funcs
->fmaf(x
, y
, z
);
1859 if ((isinf(x
) && y
== 0) || (x
== 0 && isinf(y
))) *_errno() = EDOM
;
1860 else if (isinf(x
) && isinf(z
) && x
!= z
) *_errno() = EDOM
;
1861 else if (isinf(y
) && isinf(z
) && y
!= z
) *_errno() = EDOM
;
1865 /*********************************************************************
1868 * Copied from musl: src/math/fabsf.c
1870 double CDECL
fabs( double x
)
1872 union { double f
; UINT64 i
; } u
= { x
};
1877 /*********************************************************************
1880 double CDECL
frexp( double x
, int *exp
)
1882 return unix_funcs
->frexp( x
, exp
);
1885 /*********************************************************************
1888 double CDECL
modf( double x
, double *iptr
)
1890 return unix_funcs
->modf( x
, iptr
);
1893 /**********************************************************************
1894 * _statusfp2 (MSVCRT.@)
1896 * Not exported by native msvcrt, added in msvcr80.
1898 #if defined(__i386__) || defined(__x86_64__)
1899 void CDECL
_statusfp2( unsigned int *x86_sw
, unsigned int *sse2_sw
)
1901 #if defined(__GNUC__) || defined(__clang__)
1903 unsigned long fpword
;
1907 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) );
1909 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1910 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1911 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1912 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1913 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1914 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1918 if (!sse2_sw
) return;
1922 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1924 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1925 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1926 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1927 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1928 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1929 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1934 FIXME( "not implemented\n" );
1939 /**********************************************************************
1940 * _statusfp (MSVCRT.@)
1942 unsigned int CDECL
_statusfp(void)
1944 unsigned int flags
= 0;
1945 #if defined(__i386__) || defined(__x86_64__)
1946 unsigned int x86_sw
, sse2_sw
;
1948 _statusfp2( &x86_sw
, &sse2_sw
);
1949 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
1950 flags
= x86_sw
| sse2_sw
;
1951 #elif defined(__aarch64__)
1954 __asm__
__volatile__( "mrs %0, fpsr" : "=r" (fpsr
) );
1955 if (fpsr
& 0x1) flags
|= _SW_INVALID
;
1956 if (fpsr
& 0x2) flags
|= _SW_ZERODIVIDE
;
1957 if (fpsr
& 0x4) flags
|= _SW_OVERFLOW
;
1958 if (fpsr
& 0x8) flags
|= _SW_UNDERFLOW
;
1959 if (fpsr
& 0x10) flags
|= _SW_INEXACT
;
1960 if (fpsr
& 0x80) flags
|= _SW_DENORMAL
;
1962 FIXME( "not implemented\n" );
1967 /*********************************************************************
1968 * _clearfp (MSVCRT.@)
1970 unsigned int CDECL
_clearfp(void)
1972 unsigned int flags
= 0;
1973 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
1974 unsigned long fpword
;
1976 __asm__
__volatile__( "fnstsw %0; fnclex" : "=m" (fpword
) );
1977 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1978 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1979 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1980 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1981 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1982 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1986 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1987 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1988 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1989 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1990 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1991 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1992 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1994 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1996 #elif defined(__aarch64__)
1999 __asm__
__volatile__( "mrs %0, fpsr" : "=r" (fpsr
) );
2000 if (fpsr
& 0x1) flags
|= _SW_INVALID
;
2001 if (fpsr
& 0x2) flags
|= _SW_ZERODIVIDE
;
2002 if (fpsr
& 0x4) flags
|= _SW_OVERFLOW
;
2003 if (fpsr
& 0x8) flags
|= _SW_UNDERFLOW
;
2004 if (fpsr
& 0x10) flags
|= _SW_INEXACT
;
2005 if (fpsr
& 0x80) flags
|= _SW_DENORMAL
;
2007 __asm__
__volatile__( "msr fpsr, %0" :: "r" (fpsr
) );
2009 FIXME( "not implemented\n" );
2014 /*********************************************************************
2015 * __fpecode (MSVCRT.@)
2017 int * CDECL
__fpecode(void)
2019 return &msvcrt_get_thread_data()->fpecode
;
2022 /*********************************************************************
2025 double CDECL
ldexp(double num
, int exp
)
2027 double z
= unix_funcs
->ldexp(num
,exp
);
2029 if (isfinite(num
) && !isfinite(z
))
2030 return math_error(_OVERFLOW
, "ldexp", num
, exp
, z
);
2031 if (num
&& isfinite(num
) && !z
)
2032 return math_error(_UNDERFLOW
, "ldexp", num
, exp
, z
);
2033 if (z
== 0 && signbit(z
))
2034 z
= 0.0; /* Convert -0 -> +0 */
2038 /*********************************************************************
2041 double CDECL
_cabs(struct _complex num
)
2043 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
2046 /*********************************************************************
2047 * _chgsign (MSVCRT.@)
2049 double CDECL
_chgsign(double num
)
2051 union { double f
; UINT64 i
; } u
= { num
};
2056 /*********************************************************************
2057 * __control87_2 (MSVCR80.@)
2059 * Not exported by native msvcrt, added in msvcr80.
2062 int CDECL
__control87_2( unsigned int newval
, unsigned int mask
,
2063 unsigned int *x86_cw
, unsigned int *sse2_cw
)
2065 #if defined(__GNUC__) || defined(__clang__)
2066 unsigned long fpword
;
2068 unsigned int old_flags
;
2072 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
2074 /* Convert into mask constants */
2076 if (fpword
& 0x1) flags
|= _EM_INVALID
;
2077 if (fpword
& 0x2) flags
|= _EM_DENORMAL
;
2078 if (fpword
& 0x4) flags
|= _EM_ZERODIVIDE
;
2079 if (fpword
& 0x8) flags
|= _EM_OVERFLOW
;
2080 if (fpword
& 0x10) flags
|= _EM_UNDERFLOW
;
2081 if (fpword
& 0x20) flags
|= _EM_INEXACT
;
2082 switch (fpword
& 0xc00)
2084 case 0xc00: flags
|= _RC_UP
|_RC_DOWN
; break;
2085 case 0x800: flags
|= _RC_UP
; break;
2086 case 0x400: flags
|= _RC_DOWN
; break;
2088 switch (fpword
& 0x300)
2090 case 0x0: flags
|= _PC_24
; break;
2091 case 0x200: flags
|= _PC_53
; break;
2092 case 0x300: flags
|= _PC_64
; break;
2094 if (fpword
& 0x1000) flags
|= _IC_AFFINE
;
2096 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
2099 flags
= (flags
& ~mask
) | (newval
& mask
);
2101 /* Convert (masked) value back to fp word */
2103 if (flags
& _EM_INVALID
) fpword
|= 0x1;
2104 if (flags
& _EM_DENORMAL
) fpword
|= 0x2;
2105 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x4;
2106 if (flags
& _EM_OVERFLOW
) fpword
|= 0x8;
2107 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x10;
2108 if (flags
& _EM_INEXACT
) fpword
|= 0x20;
2109 switch (flags
& _MCW_RC
)
2111 case _RC_UP
|_RC_DOWN
: fpword
|= 0xc00; break;
2112 case _RC_UP
: fpword
|= 0x800; break;
2113 case _RC_DOWN
: fpword
|= 0x400; break;
2115 switch (flags
& _MCW_PC
)
2117 case _PC_64
: fpword
|= 0x300; break;
2118 case _PC_53
: fpword
|= 0x200; break;
2119 case _PC_24
: fpword
|= 0x0; break;
2121 if (flags
& _IC_AFFINE
) fpword
|= 0x1000;
2123 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
2128 if (!sse2_cw
) return 1;
2132 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2134 /* Convert into mask constants */
2136 if (fpword
& 0x80) flags
|= _EM_INVALID
;
2137 if (fpword
& 0x100) flags
|= _EM_DENORMAL
;
2138 if (fpword
& 0x200) flags
|= _EM_ZERODIVIDE
;
2139 if (fpword
& 0x400) flags
|= _EM_OVERFLOW
;
2140 if (fpword
& 0x800) flags
|= _EM_UNDERFLOW
;
2141 if (fpword
& 0x1000) flags
|= _EM_INEXACT
;
2142 switch (fpword
& 0x6000)
2144 case 0x6000: flags
|= _RC_UP
|_RC_DOWN
; break;
2145 case 0x4000: flags
|= _RC_UP
; break;
2146 case 0x2000: flags
|= _RC_DOWN
; break;
2148 switch (fpword
& 0x8040)
2150 case 0x0040: flags
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
2151 case 0x8000: flags
|= _DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
2152 case 0x8040: flags
|= _DN_FLUSH
; break;
2155 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
2159 mask
&= _MCW_EM
| _MCW_RC
| _MCW_DN
;
2160 flags
= (flags
& ~mask
) | (newval
& mask
);
2162 if (flags
!= old_flags
)
2164 /* Convert (masked) value back to fp word */
2166 if (flags
& _EM_INVALID
) fpword
|= 0x80;
2167 if (flags
& _EM_DENORMAL
) fpword
|= 0x100;
2168 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2169 if (flags
& _EM_OVERFLOW
) fpword
|= 0x400;
2170 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x800;
2171 if (flags
& _EM_INEXACT
) fpword
|= 0x1000;
2172 switch (flags
& _MCW_RC
)
2174 case _RC_UP
|_RC_DOWN
: fpword
|= 0x6000; break;
2175 case _RC_UP
: fpword
|= 0x4000; break;
2176 case _RC_DOWN
: fpword
|= 0x2000; break;
2178 switch (flags
& _MCW_DN
)
2180 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
2181 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
2182 case _DN_FLUSH
: fpword
|= 0x8040; break;
2184 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
2193 FIXME( "not implemented\n" );
2199 /*********************************************************************
2200 * _control87 (MSVCRT.@)
2202 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
2204 unsigned int flags
= 0;
2206 unsigned int sse2_cw
;
2208 __control87_2( newval
, mask
, &flags
, &sse2_cw
);
2210 if ((flags
^ sse2_cw
) & (_MCW_EM
| _MCW_RC
)) flags
|= _EM_AMBIGUOUS
;
2212 #elif defined(__x86_64__)
2213 unsigned long fpword
;
2214 unsigned int old_flags
;
2216 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2217 if (fpword
& 0x80) flags
|= _EM_INVALID
;
2218 if (fpword
& 0x100) flags
|= _EM_DENORMAL
;
2219 if (fpword
& 0x200) flags
|= _EM_ZERODIVIDE
;
2220 if (fpword
& 0x400) flags
|= _EM_OVERFLOW
;
2221 if (fpword
& 0x800) flags
|= _EM_UNDERFLOW
;
2222 if (fpword
& 0x1000) flags
|= _EM_INEXACT
;
2223 switch (fpword
& 0x6000)
2225 case 0x6000: flags
|= _RC_CHOP
; break;
2226 case 0x4000: flags
|= _RC_UP
; break;
2227 case 0x2000: flags
|= _RC_DOWN
; break;
2229 switch (fpword
& 0x8040)
2231 case 0x0040: flags
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
2232 case 0x8000: flags
|= _DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
2233 case 0x8040: flags
|= _DN_FLUSH
; break;
2236 mask
&= _MCW_EM
| _MCW_RC
| _MCW_DN
;
2237 flags
= (flags
& ~mask
) | (newval
& mask
);
2238 if (flags
!= old_flags
)
2241 if (flags
& _EM_INVALID
) fpword
|= 0x80;
2242 if (flags
& _EM_DENORMAL
) fpword
|= 0x100;
2243 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2244 if (flags
& _EM_OVERFLOW
) fpword
|= 0x400;
2245 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x800;
2246 if (flags
& _EM_INEXACT
) fpword
|= 0x1000;
2247 switch (flags
& _MCW_RC
)
2249 case _RC_CHOP
: fpword
|= 0x6000; break;
2250 case _RC_UP
: fpword
|= 0x4000; break;
2251 case _RC_DOWN
: fpword
|= 0x2000; break;
2253 switch (flags
& _MCW_DN
)
2255 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
2256 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
2257 case _DN_FLUSH
: fpword
|= 0x8040; break;
2259 __asm__
__volatile__( "ldmxcsr %0" :: "m" (fpword
) );
2261 #elif defined(__aarch64__)
2264 __asm__
__volatile__( "mrs %0, fpcr" : "=r" (fpcr
) );
2265 if (!(fpcr
& 0x100)) flags
|= _EM_INVALID
;
2266 if (!(fpcr
& 0x200)) flags
|= _EM_ZERODIVIDE
;
2267 if (!(fpcr
& 0x400)) flags
|= _EM_OVERFLOW
;
2268 if (!(fpcr
& 0x800)) flags
|= _EM_UNDERFLOW
;
2269 if (!(fpcr
& 0x1000)) flags
|= _EM_INEXACT
;
2270 if (!(fpcr
& 0x8000)) flags
|= _EM_DENORMAL
;
2271 switch (fpcr
& 0xc00000)
2273 case 0x400000: flags
|= _RC_UP
; break;
2274 case 0x800000: flags
|= _RC_DOWN
; break;
2275 case 0xc00000: flags
|= _RC_CHOP
; break;
2277 flags
= (flags
& ~mask
) | (newval
& mask
);
2278 fpcr
&= ~0xc09f00ul
;
2279 if (!(flags
& _EM_INVALID
)) fpcr
|= 0x100;
2280 if (!(flags
& _EM_ZERODIVIDE
)) fpcr
|= 0x200;
2281 if (!(flags
& _EM_OVERFLOW
)) fpcr
|= 0x400;
2282 if (!(flags
& _EM_UNDERFLOW
)) fpcr
|= 0x800;
2283 if (!(flags
& _EM_INEXACT
)) fpcr
|= 0x1000;
2284 if (!(flags
& _EM_DENORMAL
)) fpcr
|= 0x8000;
2285 switch (flags
& _MCW_RC
)
2287 case _RC_CHOP
: fpcr
|= 0xc00000; break;
2288 case _RC_UP
: fpcr
|= 0x400000; break;
2289 case _RC_DOWN
: fpcr
|= 0x800000; break;
2291 __asm__
__volatile__( "msr fpcr, %0" :: "r" (fpcr
) );
2293 FIXME( "not implemented\n" );
2298 /*********************************************************************
2299 * _controlfp (MSVCRT.@)
2301 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
2303 return _control87( newval
, mask
& ~_EM_DENORMAL
);
2306 /*********************************************************************
2307 * _set_controlfp (MSVCRT.@)
2309 void CDECL
_set_controlfp( unsigned int newval
, unsigned int mask
)
2311 _controlfp( newval
, mask
);
2314 /*********************************************************************
2315 * _controlfp_s (MSVCRT.@)
2317 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
2319 static const unsigned int all_flags
= (_MCW_EM
| _MCW_IC
| _MCW_RC
|
2323 if (!MSVCRT_CHECK_PMT( !(newval
& mask
& ~all_flags
) ))
2325 if (cur
) *cur
= _controlfp( 0, 0 ); /* retrieve it anyway */
2328 val
= _controlfp( newval
, mask
);
2329 if (cur
) *cur
= val
;
2333 #if _MSVCR_VER >= 140
2336 FENV_X_INVALID
= 0x00100010,
2337 FENV_X_DENORMAL
= 0x00200020,
2338 FENV_X_ZERODIVIDE
= 0x00080008,
2339 FENV_X_OVERFLOW
= 0x00040004,
2340 FENV_X_UNDERFLOW
= 0x00020002,
2341 FENV_X_INEXACT
= 0x00010001,
2342 FENV_X_AFFINE
= 0x00004000,
2343 FENV_X_UP
= 0x00800200,
2344 FENV_X_DOWN
= 0x00400100,
2345 FENV_X_24
= 0x00002000,
2346 FENV_X_53
= 0x00001000,
2347 FENV_Y_INVALID
= 0x10000010,
2348 FENV_Y_DENORMAL
= 0x20000020,
2349 FENV_Y_ZERODIVIDE
= 0x08000008,
2350 FENV_Y_OVERFLOW
= 0x04000004,
2351 FENV_Y_UNDERFLOW
= 0x02000002,
2352 FENV_Y_INEXACT
= 0x01000001,
2353 FENV_Y_UP
= 0x80000200,
2354 FENV_Y_DOWN
= 0x40000100,
2355 FENV_Y_FLUSH
= 0x00000400,
2356 FENV_Y_FLUSH_SAVE
= 0x00000800
2359 /* encodes x87/sse control/status word in ulong */
2360 static __msvcrt_ulong
fenv_encode(unsigned int x
, unsigned int y
)
2362 __msvcrt_ulong ret
= 0;
2364 if (x
& _EM_INVALID
) ret
|= FENV_X_INVALID
;
2365 if (x
& _EM_DENORMAL
) ret
|= FENV_X_DENORMAL
;
2366 if (x
& _EM_ZERODIVIDE
) ret
|= FENV_X_ZERODIVIDE
;
2367 if (x
& _EM_OVERFLOW
) ret
|= FENV_X_OVERFLOW
;
2368 if (x
& _EM_UNDERFLOW
) ret
|= FENV_X_UNDERFLOW
;
2369 if (x
& _EM_INEXACT
) ret
|= FENV_X_INEXACT
;
2370 if (x
& _IC_AFFINE
) ret
|= FENV_X_AFFINE
;
2371 if (x
& _RC_UP
) ret
|= FENV_X_UP
;
2372 if (x
& _RC_DOWN
) ret
|= FENV_X_DOWN
;
2373 if (x
& _PC_24
) ret
|= FENV_X_24
;
2374 if (x
& _PC_53
) ret
|= FENV_X_53
;
2375 x
&= ~(_MCW_EM
| _MCW_IC
| _MCW_RC
| _MCW_PC
);
2377 if (y
& _EM_INVALID
) ret
|= FENV_Y_INVALID
;
2378 if (y
& _EM_DENORMAL
) ret
|= FENV_Y_DENORMAL
;
2379 if (y
& _EM_ZERODIVIDE
) ret
|= FENV_Y_ZERODIVIDE
;
2380 if (y
& _EM_OVERFLOW
) ret
|= FENV_Y_OVERFLOW
;
2381 if (y
& _EM_UNDERFLOW
) ret
|= FENV_Y_UNDERFLOW
;
2382 if (y
& _EM_INEXACT
) ret
|= FENV_Y_INEXACT
;
2383 if (y
& _RC_UP
) ret
|= FENV_Y_UP
;
2384 if (y
& _RC_DOWN
) ret
|= FENV_Y_DOWN
;
2385 if (y
& _DN_FLUSH
) ret
|= FENV_Y_FLUSH
;
2386 if (y
& _DN_FLUSH_OPERANDS_SAVE_RESULTS
) ret
|= FENV_Y_FLUSH_SAVE
;
2387 y
&= ~(_MCW_EM
| _MCW_IC
| _MCW_RC
| _MCW_DN
);
2389 if(x
|| y
) FIXME("unsupported flags: %x, %x\n", x
, y
);
2393 /* decodes x87/sse control/status word, returns FALSE on error */
2394 #if (defined(__i386__) || defined(__x86_64__))
2395 static BOOL
fenv_decode(__msvcrt_ulong enc
, unsigned int *x
, unsigned int *y
)
2398 if ((enc
& FENV_X_INVALID
) == FENV_X_INVALID
) *x
|= _EM_INVALID
;
2399 if ((enc
& FENV_X_DENORMAL
) == FENV_X_DENORMAL
) *x
|= _EM_DENORMAL
;
2400 if ((enc
& FENV_X_ZERODIVIDE
) == FENV_X_ZERODIVIDE
) *x
|= _EM_ZERODIVIDE
;
2401 if ((enc
& FENV_X_OVERFLOW
) == FENV_X_OVERFLOW
) *x
|= _EM_OVERFLOW
;
2402 if ((enc
& FENV_X_UNDERFLOW
) == FENV_X_UNDERFLOW
) *x
|= _EM_UNDERFLOW
;
2403 if ((enc
& FENV_X_INEXACT
) == FENV_X_INEXACT
) *x
|= _EM_INEXACT
;
2404 if ((enc
& FENV_X_AFFINE
) == FENV_X_AFFINE
) *x
|= _IC_AFFINE
;
2405 if ((enc
& FENV_X_UP
) == FENV_X_UP
) *x
|= _RC_UP
;
2406 if ((enc
& FENV_X_DOWN
) == FENV_X_DOWN
) *x
|= _RC_DOWN
;
2407 if ((enc
& FENV_X_24
) == FENV_X_24
) *x
|= _PC_24
;
2408 if ((enc
& FENV_X_53
) == FENV_X_53
) *x
|= _PC_53
;
2410 if ((enc
& FENV_Y_INVALID
) == FENV_Y_INVALID
) *y
|= _EM_INVALID
;
2411 if ((enc
& FENV_Y_DENORMAL
) == FENV_Y_DENORMAL
) *y
|= _EM_DENORMAL
;
2412 if ((enc
& FENV_Y_ZERODIVIDE
) == FENV_Y_ZERODIVIDE
) *y
|= _EM_ZERODIVIDE
;
2413 if ((enc
& FENV_Y_OVERFLOW
) == FENV_Y_OVERFLOW
) *y
|= _EM_OVERFLOW
;
2414 if ((enc
& FENV_Y_UNDERFLOW
) == FENV_Y_UNDERFLOW
) *y
|= _EM_UNDERFLOW
;
2415 if ((enc
& FENV_Y_INEXACT
) == FENV_Y_INEXACT
) *y
|= _EM_INEXACT
;
2416 if ((enc
& FENV_Y_UP
) == FENV_Y_UP
) *y
|= _RC_UP
;
2417 if ((enc
& FENV_Y_DOWN
) == FENV_Y_DOWN
) *y
|= _RC_DOWN
;
2418 if ((enc
& FENV_Y_FLUSH
) == FENV_Y_FLUSH
) *y
|= _DN_FLUSH
;
2419 if ((enc
& FENV_Y_FLUSH_SAVE
) == FENV_Y_FLUSH_SAVE
) *y
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
;
2421 if (fenv_encode(*x
, *y
) != enc
)
2423 WARN("can't decode: %lx\n", enc
);
2432 /*********************************************************************
2433 * fegetenv (MSVCR120.@)
2435 int CDECL
fegetenv(fenv_t
*env
)
2437 #if _MSVCR_VER>=140 && defined(__i386__)
2438 unsigned int x87
, sse
;
2439 __control87_2(0, 0, &x87
, &sse
);
2440 env
->_Fe_ctl
= fenv_encode(x87
, sse
);
2441 _statusfp2(&x87
, &sse
);
2442 env
->_Fe_stat
= fenv_encode(x87
, sse
);
2443 #elif _MSVCR_VER>=140
2444 env
->_Fe_ctl
= fenv_encode(0, _control87(0, 0));
2445 env
->_Fe_stat
= fenv_encode(0, _statusfp());
2447 env
->_Fe_ctl
= _controlfp(0, 0) & (_EM_INEXACT
| _EM_UNDERFLOW
|
2448 _EM_OVERFLOW
| _EM_ZERODIVIDE
| _EM_INVALID
| _RC_CHOP
);
2449 env
->_Fe_stat
= _statusfp();
2454 /*********************************************************************
2455 * feupdateenv (MSVCR120.@)
2457 int CDECL
feupdateenv(const fenv_t
*env
)
2461 set
._Fe_ctl
= env
->_Fe_ctl
;
2462 set
._Fe_stat
|= env
->_Fe_stat
;
2463 return fesetenv(&set
);
2466 /*********************************************************************
2467 * fetestexcept (MSVCR120.@)
2469 int CDECL
fetestexcept(int flags
)
2471 return _statusfp() & flags
;
2474 /*********************************************************************
2475 * fesetexceptflag (MSVCR120.@)
2477 int CDECL
fesetexceptflag(const fexcept_t
*status
, int excepts
)
2481 excepts
&= FE_ALL_EXCEPT
;
2486 #if _MSVCR_VER>=140 && (defined(__i386__) || defined(__x86_64__))
2487 env
._Fe_stat
&= ~fenv_encode(excepts
, excepts
);
2488 env
._Fe_stat
|= *status
& fenv_encode(excepts
, excepts
);
2489 #elif _MSVCR_VER>=140
2490 env
._Fe_stat
&= ~fenv_encode(0, excepts
);
2491 env
._Fe_stat
|= *status
& fenv_encode(0, excepts
);
2493 env
._Fe_stat
&= ~excepts
;
2494 env
._Fe_stat
|= *status
& excepts
;
2496 return fesetenv(&env
);
2499 /*********************************************************************
2500 * feraiseexcept (MSVCR120.@)
2502 int CDECL
feraiseexcept(int flags
)
2506 flags
&= FE_ALL_EXCEPT
;
2508 #if _MSVCR_VER>=140 && defined(__i386__)
2509 env
._Fe_stat
|= fenv_encode(flags
, flags
);
2510 #elif _MSVCR_VER>=140
2511 env
._Fe_stat
|= fenv_encode(0, flags
);
2513 env
._Fe_stat
|= flags
;
2515 return fesetenv(&env
);
2518 /*********************************************************************
2519 * feclearexcept (MSVCR120.@)
2521 int CDECL
feclearexcept(int flags
)
2526 flags
&= FE_ALL_EXCEPT
;
2528 env
._Fe_stat
&= ~fenv_encode(flags
, flags
);
2530 env
._Fe_stat
&= ~flags
;
2532 return fesetenv(&env
);
2535 /*********************************************************************
2536 * fegetexceptflag (MSVCR120.@)
2538 int CDECL
fegetexceptflag(fexcept_t
*status
, int excepts
)
2540 #if _MSVCR_VER>=140 && defined(__i386__)
2541 unsigned int x87
, sse
;
2542 _statusfp2(&x87
, &sse
);
2543 *status
= fenv_encode(x87
& excepts
, sse
& excepts
);
2544 #elif _MSVCR_VER>=140
2545 *status
= fenv_encode(0, _statusfp() & excepts
);
2547 *status
= _statusfp() & excepts
;
2554 /*********************************************************************
2555 * __fpe_flt_rounds (UCRTBASE.@)
2557 int CDECL
__fpe_flt_rounds(void)
2559 unsigned int fpc
= _controlfp(0, 0) & _RC_CHOP
;
2564 case _RC_CHOP
: return 0;
2565 case _RC_NEAR
: return 1;
2566 case _RC_UP
: return 2;
2574 /*********************************************************************
2575 * fegetround (MSVCR120.@)
2577 int CDECL
fegetround(void)
2579 return _controlfp(0, 0) & _RC_CHOP
;
2582 /*********************************************************************
2583 * fesetround (MSVCR120.@)
2585 int CDECL
fesetround(int round_mode
)
2587 if (round_mode
& (~_RC_CHOP
))
2589 _controlfp(round_mode
, _RC_CHOP
);
2593 #endif /* _MSVCR_VER>=120 */
2595 /*********************************************************************
2596 * _copysign (MSVCRT.@)
2598 * Copied from musl: src/math/copysign.c
2600 double CDECL
_copysign( double x
, double y
)
2602 union { double f
; UINT64 i
; } ux
= { x
}, uy
= { y
};
2604 ux
.i
|= uy
.i
& 1ull << 63;
2608 /*********************************************************************
2609 * _finite (MSVCRT.@)
2611 int CDECL
_finite(double num
)
2613 union { double f
; UINT64 i
; } u
= { num
};
2614 return (u
.i
& ~0ull >> 1) < 0x7ffull
<< 52;
2617 /*********************************************************************
2618 * _fpreset (MSVCRT.@)
2620 void CDECL
_fpreset(void)
2622 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2623 const unsigned int x86_cw
= 0x27f;
2624 __asm__
__volatile__( "fninit; fldcw %0" : : "m" (x86_cw
) );
2627 const unsigned long sse2_cw
= 0x1f80;
2628 __asm__
__volatile__( "ldmxcsr %0" : : "m" (sse2_cw
) );
2631 FIXME( "not implemented\n" );
2636 /*********************************************************************
2637 * fesetenv (MSVCR120.@)
2639 int CDECL
fesetenv(const fenv_t
*env
)
2641 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2642 unsigned int x87_cw
, sse_cw
, x87_stat
, sse_stat
;
2650 DWORD instruction_pointer
;
2658 TRACE( "(%p)\n", env
);
2660 if (!env
->_Fe_ctl
&& !env
->_Fe_stat
) {
2666 if (!fenv_decode(env
->_Fe_ctl
, &x87_cw
, &sse_cw
))
2668 if (!fenv_decode(env
->_Fe_stat
, &x87_stat
, &sse_stat
))
2671 x87_cw
= sse_cw
= env
->_Fe_ctl
;
2672 x87_stat
= sse_stat
= env
->_Fe_stat
;
2675 __asm__
__volatile__( "fnstenv %0" : "=m" (fenv
) );
2677 fenv
.control_word
&= ~0xc3d;
2679 fenv
.control_word
&= ~0x1302;
2681 if (x87_cw
& _EM_INVALID
) fenv
.control_word
|= 0x1;
2682 if (x87_cw
& _EM_ZERODIVIDE
) fenv
.control_word
|= 0x4;
2683 if (x87_cw
& _EM_OVERFLOW
) fenv
.control_word
|= 0x8;
2684 if (x87_cw
& _EM_UNDERFLOW
) fenv
.control_word
|= 0x10;
2685 if (x87_cw
& _EM_INEXACT
) fenv
.control_word
|= 0x20;
2686 switch (x87_cw
& _MCW_RC
)
2688 case _RC_UP
|_RC_DOWN
: fenv
.control_word
|= 0xc00; break;
2689 case _RC_UP
: fenv
.control_word
|= 0x800; break;
2690 case _RC_DOWN
: fenv
.control_word
|= 0x400; break;
2693 if (x87_cw
& _EM_DENORMAL
) fenv
.control_word
|= 0x2;
2694 switch (x87_cw
& _MCW_PC
)
2696 case _PC_64
: fenv
.control_word
|= 0x300; break;
2697 case _PC_53
: fenv
.control_word
|= 0x200; break;
2698 case _PC_24
: fenv
.control_word
|= 0x0; break;
2700 if (x87_cw
& _IC_AFFINE
) fenv
.control_word
|= 0x1000;
2703 fenv
.status_word
&= ~0x3f;
2704 if (x87_stat
& _SW_INVALID
) fenv
.status_word
|= 0x1;
2705 if (x87_stat
& _SW_DENORMAL
) fenv
.status_word
|= 0x2;
2706 if (x87_stat
& _SW_ZERODIVIDE
) fenv
.status_word
|= 0x4;
2707 if (x87_stat
& _SW_OVERFLOW
) fenv
.status_word
|= 0x8;
2708 if (x87_stat
& _SW_UNDERFLOW
) fenv
.status_word
|= 0x10;
2709 if (x87_stat
& _SW_INEXACT
) fenv
.status_word
|= 0x20;
2711 __asm__
__volatile__( "fldenv %0" : : "m" (fenv
) : "st", "st(1)",
2712 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
2717 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2722 if (sse_cw
& _EM_INVALID
) fpword
|= 0x80;
2723 if (sse_cw
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2724 if (sse_cw
& _EM_OVERFLOW
) fpword
|= 0x400;
2725 if (sse_cw
& _EM_UNDERFLOW
) fpword
|= 0x800;
2726 if (sse_cw
& _EM_INEXACT
) fpword
|= 0x1000;
2727 switch (sse_cw
& _MCW_RC
)
2729 case _RC_CHOP
: fpword
|= 0x6000; break;
2730 case _RC_UP
: fpword
|= 0x4000; break;
2731 case _RC_DOWN
: fpword
|= 0x2000; break;
2733 if (sse_stat
& _SW_INVALID
) fpword
|= 0x1;
2734 if (sse_stat
& _SW_DENORMAL
) fpword
|= 0x2;
2735 if (sse_stat
& _SW_ZERODIVIDE
) fpword
|= 0x4;
2736 if (sse_stat
& _SW_OVERFLOW
) fpword
|= 0x8;
2737 if (sse_stat
& _SW_UNDERFLOW
) fpword
|= 0x10;
2738 if (sse_stat
& _SW_INEXACT
) fpword
|= 0x20;
2740 if (sse_cw
& _EM_DENORMAL
) fpword
|= 0x100;
2741 switch (sse_cw
& _MCW_DN
)
2743 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
2744 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
2745 case _DN_FLUSH
: fpword
|= 0x8040; break;
2748 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
2753 FIXME( "not implemented\n" );
2759 /*********************************************************************
2762 int CDECL
_isnan(double num
)
2764 union { double f
; UINT64 i
; } u
= { num
};
2765 return (u
.i
& ~0ull >> 1) > 0x7ffull
<< 52;
2768 static double pzero(double x
)
2770 static const double pR8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
2771 0.00000000000000000000e+00,
2772 -7.03124999999900357484e-02,
2773 -8.08167041275349795626e+00,
2774 -2.57063105679704847262e+02,
2775 -2.48521641009428822144e+03,
2776 -5.25304380490729545272e+03,
2778 1.16534364619668181717e+02,
2779 3.83374475364121826715e+03,
2780 4.05978572648472545552e+04,
2781 1.16752972564375915681e+05,
2782 4.76277284146730962675e+04,
2783 }, pR5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
2784 -1.14125464691894502584e-11,
2785 -7.03124940873599280078e-02,
2786 -4.15961064470587782438e+00,
2787 -6.76747652265167261021e+01,
2788 -3.31231299649172967747e+02,
2789 -3.46433388365604912451e+02,
2791 6.07539382692300335975e+01,
2792 1.05125230595704579173e+03,
2793 5.97897094333855784498e+03,
2794 9.62544514357774460223e+03,
2795 2.40605815922939109441e+03,
2796 }, pR3
[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
2797 -2.54704601771951915620e-09,
2798 -7.03119616381481654654e-02,
2799 -2.40903221549529611423e+00,
2800 -2.19659774734883086467e+01,
2801 -5.80791704701737572236e+01,
2802 -3.14479470594888503854e+01,
2804 3.58560338055209726349e+01,
2805 3.61513983050303863820e+02,
2806 1.19360783792111533330e+03,
2807 1.12799679856907414432e+03,
2808 1.73580930813335754692e+02,
2809 }, pR2
[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
2810 -8.87534333032526411254e-08,
2811 -7.03030995483624743247e-02,
2812 -1.45073846780952986357e+00,
2813 -7.63569613823527770791e+00,
2814 -1.11931668860356747786e+01,
2815 -3.23364579351335335033e+00,
2817 2.22202997532088808441e+01,
2818 1.36206794218215208048e+02,
2819 2.70470278658083486789e+02,
2820 1.53875394208320329881e+02,
2821 1.46576176948256193810e+01,
2824 const double *p
, *q
;
2828 ix
= *(ULONGLONG
*)&x
>> 32;
2830 if (ix
>= 0x40200000) {
2833 } else if (ix
>= 0x40122E8B) {
2836 } else if (ix
>= 0x4006DB6D) {
2839 } else /*ix >= 0x40000000*/ {
2845 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
2846 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* q
[4]))));
2850 static double qzero(double x
)
2852 static const double qR8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
2853 0.00000000000000000000e+00,
2854 7.32421874999935051953e-02,
2855 1.17682064682252693899e+01,
2856 5.57673380256401856059e+02,
2857 8.85919720756468632317e+03,
2858 3.70146267776887834771e+04,
2860 1.63776026895689824414e+02,
2861 8.09834494656449805916e+03,
2862 1.42538291419120476348e+05,
2863 8.03309257119514397345e+05,
2864 8.40501579819060512818e+05,
2865 -3.43899293537866615225e+05,
2866 }, qR5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
2867 1.84085963594515531381e-11,
2868 7.32421766612684765896e-02,
2869 5.83563508962056953777e+00,
2870 1.35111577286449829671e+02,
2871 1.02724376596164097464e+03,
2872 1.98997785864605384631e+03,
2874 8.27766102236537761883e+01,
2875 2.07781416421392987104e+03,
2876 1.88472887785718085070e+04,
2877 5.67511122894947329769e+04,
2878 3.59767538425114471465e+04,
2879 -5.35434275601944773371e+03,
2880 }, qR3
[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
2881 4.37741014089738620906e-09,
2882 7.32411180042911447163e-02,
2883 3.34423137516170720929e+00,
2884 4.26218440745412650017e+01,
2885 1.70808091340565596283e+02,
2886 1.66733948696651168575e+02,
2888 4.87588729724587182091e+01,
2889 7.09689221056606015736e+02,
2890 3.70414822620111362994e+03,
2891 6.46042516752568917582e+03,
2892 2.51633368920368957333e+03,
2893 -1.49247451836156386662e+02,
2894 }, qR2
[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
2895 1.50444444886983272379e-07,
2896 7.32234265963079278272e-02,
2897 1.99819174093815998816e+00,
2898 1.44956029347885735348e+01,
2899 3.16662317504781540833e+01,
2900 1.62527075710929267416e+01,
2902 3.03655848355219184498e+01,
2903 2.69348118608049844624e+02,
2904 8.44783757595320139444e+02,
2905 8.82935845112488550512e+02,
2906 2.12666388511798828631e+02,
2907 -5.31095493882666946917e+00,
2910 const double *p
, *q
;
2914 ix
= *(ULONGLONG
*)&x
>> 32;
2916 if (ix
>= 0x40200000) {
2919 } else if (ix
>= 0x40122E8B) {
2922 } else if (ix
>= 0x4006DB6D) {
2925 } else /*ix >= 0x40000000*/ {
2931 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
2932 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* (q
[4] + z
* q
[5])))));
2933 return (-0.125 + r
/ s
) / x
;
2936 /* j0 and y0 approximation for |x|>=2 */
2937 static double j0_y0_approx(unsigned int ix
, double x
, BOOL y0
)
2939 static const double invsqrtpi
= 5.64189583547756279280e-01;
2941 double s
, c
, ss
, cc
, z
;
2947 /* avoid overflow in 2*x, big ulp error when x>=0x1p1023 */
2948 if (ix
< 0x7fe00000) {
2951 if (s
* c
< 0) cc
= z
/ ss
;
2953 if (ix
< 0x48000000) {
2955 cc
= pzero(x
) * cc
- qzero(x
) * ss
;
2958 return invsqrtpi
* cc
/ sqrt(x
);
2961 /*********************************************************************
2964 * Copied from musl: src/math/j0.c
2966 double CDECL
_j0(double x
)
2968 static const double R02
= 1.56249999999999947958e-02,
2969 R03
= -1.89979294238854721751e-04,
2970 R04
= 1.82954049532700665670e-06,
2971 R05
= -4.61832688532103189199e-09,
2972 S01
= 1.56191029464890010492e-02,
2973 S02
= 1.16926784663337450260e-04,
2974 S03
= 5.13546550207318111446e-07,
2975 S04
= 1.16614003333790000205e-09;
2980 ix
= *(ULONGLONG
*)&x
>> 32;
2983 /* j0(+-inf)=0, j0(nan)=nan */
2984 if (ix
>= 0x7ff00000)
2985 return math_error(_DOMAIN
, "_j0", x
, 0, 1 / (x
* x
));
2988 if (ix
>= 0x40000000) { /* |x| >= 2 */
2989 /* large ulp error near zeros: 2.4, 5.52, 8.6537,.. */
2990 return j0_y0_approx(ix
, x
, FALSE
);
2993 if (ix
>= 0x3f200000) { /* |x| >= 2**-13 */
2994 /* up to 4ulp error close to 2 */
2996 r
= z
* (R02
+ z
* (R03
+ z
* (R04
+ z
* R05
)));
2997 s
= 1 + z
* (S01
+ z
* (S02
+ z
* (S03
+ z
* S04
)));
2998 return (1 + x
/ 2) * (1 - x
/ 2) + z
* (r
/ s
);
3002 /* prevent underflow */
3003 /* inexact should be raised when x!=0, this is not done correctly */
3004 if (ix
>= 0x38000000) /* |x| >= 2**-127 */
3009 static double pone(double x
)
3011 static const double pr8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
3012 0.00000000000000000000e+00,
3013 1.17187499999988647970e-01,
3014 1.32394806593073575129e+01,
3015 4.12051854307378562225e+02,
3016 3.87474538913960532227e+03,
3017 7.91447954031891731574e+03,
3019 1.14207370375678408436e+02,
3020 3.65093083420853463394e+03,
3021 3.69562060269033463555e+04,
3022 9.76027935934950801311e+04,
3023 3.08042720627888811578e+04,
3024 }, pr5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
3025 1.31990519556243522749e-11,
3026 1.17187493190614097638e-01,
3027 6.80275127868432871736e+00,
3028 1.08308182990189109773e+02,
3029 5.17636139533199752805e+02,
3030 5.28715201363337541807e+02,
3032 5.92805987221131331921e+01,
3033 9.91401418733614377743e+02,
3034 5.35326695291487976647e+03,
3035 7.84469031749551231769e+03,
3036 1.50404688810361062679e+03,
3038 3.02503916137373618024e-09,
3039 1.17186865567253592491e-01,
3040 3.93297750033315640650e+00,
3041 3.51194035591636932736e+01,
3042 9.10550110750781271918e+01,
3043 4.85590685197364919645e+01,
3045 3.47913095001251519989e+01,
3046 3.36762458747825746741e+02,
3047 1.04687139975775130551e+03,
3048 8.90811346398256432622e+02,
3049 1.03787932439639277504e+02,
3050 }, pr2
[6] = { /* for x in [2.8570,2]=1/[0.3499,0.5] */
3051 1.07710830106873743082e-07,
3052 1.17176219462683348094e-01,
3053 2.36851496667608785174e+00,
3054 1.22426109148261232917e+01,
3055 1.76939711271687727390e+01,
3056 5.07352312588818499250e+00,
3058 2.14364859363821409488e+01,
3059 1.25290227168402751090e+02,
3060 2.32276469057162813669e+02,
3061 1.17679373287147100768e+02,
3062 8.36463893371618283368e+00,
3065 const double *p
, *q
;
3069 ix
= *(ULONGLONG
*)&x
>> 32;
3071 if (ix
>= 0x40200000) {
3074 } else if (ix
>= 0x40122E8B) {
3077 } else if (ix
>= 0x4006DB6D) {
3080 } else /*ix >= 0x40000000*/ {
3085 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
3086 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* q
[4]))));
3090 static double qone(double x
)
3092 static const double qr8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
3093 0.00000000000000000000e+00,
3094 -1.02539062499992714161e-01,
3095 -1.62717534544589987888e+01,
3096 -7.59601722513950107896e+02,
3097 -1.18498066702429587167e+04,
3098 -4.84385124285750353010e+04,
3100 1.61395369700722909556e+02,
3101 7.82538599923348465381e+03,
3102 1.33875336287249578163e+05,
3103 7.19657723683240939863e+05,
3104 6.66601232617776375264e+05,
3105 -2.94490264303834643215e+05,
3106 }, qr5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
3107 -2.08979931141764104297e-11,
3108 -1.02539050241375426231e-01,
3109 -8.05644828123936029840e+00,
3110 -1.83669607474888380239e+02,
3111 -1.37319376065508163265e+03,
3112 -2.61244440453215656817e+03,
3114 8.12765501384335777857e+01,
3115 1.99179873460485964642e+03,
3116 1.74684851924908907677e+04,
3117 4.98514270910352279316e+04,
3118 2.79480751638918118260e+04,
3119 -4.71918354795128470869e+03,
3121 -5.07831226461766561369e-09,
3122 -1.02537829820837089745e-01,
3123 -4.61011581139473403113e+00,
3124 -5.78472216562783643212e+01,
3125 -2.28244540737631695038e+02,
3126 -2.19210128478909325622e+02,
3128 4.76651550323729509273e+01,
3129 6.73865112676699709482e+02,
3130 3.38015286679526343505e+03,
3131 5.54772909720722782367e+03,
3132 1.90311919338810798763e+03,
3133 -1.35201191444307340817e+02,
3134 }, qr2
[6] = { /* for x in [2.8570,2]=1/[0.3499,0.5] */
3135 -1.78381727510958865572e-07,
3136 -1.02517042607985553460e-01,
3137 -2.75220568278187460720e+00,
3138 -1.96636162643703720221e+01,
3139 -4.23253133372830490089e+01,
3140 -2.13719211703704061733e+01,
3142 2.95333629060523854548e+01,
3143 2.52981549982190529136e+02,
3144 7.57502834868645436472e+02,
3145 7.39393205320467245656e+02,
3146 1.55949003336666123687e+02,
3147 -4.95949898822628210127e+00,
3150 const double *p
, *q
;
3154 ix
= *(ULONGLONG
*)&x
>> 32;
3156 if (ix
>= 0x40200000) {
3159 } else if (ix
>= 0x40122E8B) {
3162 } else if (ix
>= 0x4006DB6D) {
3165 } else /*ix >= 0x40000000*/ {
3170 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
3171 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* (q
[4] + z
* q
[5])))));
3172 return (0.375 + r
/ s
) / x
;
3175 static double j1_y1_approx(unsigned int ix
, double x
, BOOL y1
, int sign
)
3177 static const double invsqrtpi
= 5.64189583547756279280e-01;
3179 double z
, s
, c
, ss
, cc
;
3185 if (ix
< 0x7fe00000) {
3188 if (s
* c
> 0) cc
= z
/ ss
;
3190 if (ix
< 0x48000000) {
3193 cc
= pone(x
) * cc
- qone(x
) * ss
;
3198 return invsqrtpi
* cc
/ sqrt(x
);
3201 /*********************************************************************
3204 * Copied from musl: src/math/j1.c
3206 double CDECL
_j1(double x
)
3208 static const double r00
= -6.25000000000000000000e-02,
3209 r01
= 1.40705666955189706048e-03,
3210 r02
= -1.59955631084035597520e-05,
3211 r03
= 4.96727999609584448412e-08,
3212 s01
= 1.91537599538363460805e-02,
3213 s02
= 1.85946785588630915560e-04,
3214 s03
= 1.17718464042623683263e-06,
3215 s04
= 5.04636257076217042715e-09,
3216 s05
= 1.23542274426137913908e-11;
3222 ix
= *(ULONGLONG
*)&x
>> 32;
3225 if (ix
>= 0x7ff00000)
3226 return math_error(isnan(x
) ? 0 : _DOMAIN
, "_j1", x
, 0, 1 / (x
* x
));
3227 if (ix
>= 0x40000000) /* |x| >= 2 */
3228 return j1_y1_approx(ix
, fabs(x
), FALSE
, sign
);
3229 if (ix
>= 0x38000000) { /* |x| >= 2**-127 */
3231 r
= z
* (r00
+ z
* (r01
+ z
* (r02
+ z
* r03
)));
3232 s
= 1 + z
* (s01
+ z
* (s02
+ z
* (s03
+ z
* (s04
+ z
* s05
))));
3235 /* avoid underflow, raise inexact if x!=0 */
3238 return (0.5 + z
) * x
;
3241 /*********************************************************************
3244 * Copied from musl: src/math/jn.c
3246 double CDECL
_jn(int n
, double x
)
3248 static const double invsqrtpi
= 5.64189583547756279280e-01;
3250 unsigned int ix
, lx
;
3254 ix
= *(ULONGLONG
*)&x
>> 32;
3255 lx
= *(ULONGLONG
*)&x
;
3259 if ((ix
| (lx
| -lx
) >> 31) > 0x7ff00000) /* nan */
3274 sign
&= n
; /* even n: 0, odd n: signbit(x) */
3276 if ((ix
| lx
) == 0 || ix
== 0x7ff00000) /* if x is 0 or inf */
3279 if (ix
>= 0x52d00000) { /* x > 2**302 */
3282 temp
= -cos(x
) + sin(x
);
3285 temp
= -cos(x
) - sin(x
);
3288 temp
= cos(x
) - sin(x
);
3291 temp
= cos(x
) + sin(x
);
3294 b
= invsqrtpi
* temp
/ sqrt(x
);
3298 for (i
= 0; i
< nm1
; ) {
3301 b
= b
* (2.0 * i
/ x
) - a
; /* avoid underflow */
3306 if (ix
< 0x3e100000) { /* x < 2**-29 */
3307 if (nm1
> 32) /* underflow */
3313 for (i
= 2; i
<= nm1
+ 1; i
++) {
3314 a
*= (double)i
; /* a = n! */
3315 b
*= temp
; /* b = (x/2)^n */
3320 double t
, q0
, q1
, w
, h
, z
, tmp
, nf
;
3330 while (q1
< 1.0e9
) {
3337 for (t
= 0.0, i
= k
; i
>= 0; i
--)
3338 t
= 1 / (2 * (i
+ nf
) / x
- t
);
3341 tmp
= nf
* log(fabs(w
));
3342 if (tmp
< 7.09782712893383973096e+02) {
3343 for (i
= nm1
; i
> 0; i
--) {
3345 b
= b
* (2.0 * i
) / x
- a
;
3349 for (i
= nm1
; i
> 0; i
--) {
3351 b
= b
* (2.0 * i
) / x
- a
;
3353 /* scale b to avoid spurious overflow */
3363 if (fabs(z
) >= fabs(w
))
3369 return sign
? -b
: b
;
3372 /*********************************************************************
3375 double CDECL
_y0(double x
)
3377 static const double tpi
= 6.36619772367581382433e-01,
3378 u00
= -7.38042951086872317523e-02,
3379 u01
= 1.76666452509181115538e-01,
3380 u02
= -1.38185671945596898896e-02,
3381 u03
= 3.47453432093683650238e-04,
3382 u04
= -3.81407053724364161125e-06,
3383 u05
= 1.95590137035022920206e-08,
3384 u06
= -3.98205194132103398453e-11,
3385 v01
= 1.27304834834123699328e-02,
3386 v02
= 7.60068627350353253702e-05,
3387 v03
= 2.59150851840457805467e-07,
3388 v04
= 4.41110311332675467403e-10;
3391 unsigned int ix
, lx
;
3393 ix
= *(ULONGLONG
*)&x
>> 32;
3394 lx
= *(ULONGLONG
*)&x
;
3396 /* y0(nan)=nan, y0(<0)=nan, y0(0)=-inf, y0(inf)=0 */
3397 if ((ix
<< 1 | lx
) == 0)
3398 return math_error(_OVERFLOW
, "_y0", x
, 0, -INFINITY
);
3402 return math_error(_DOMAIN
, "_y0", x
, 0, 0 / (x
- x
));
3403 if (ix
>= 0x7ff00000)
3406 if (ix
>= 0x40000000) { /* x >= 2 */
3407 /* large ulp errors near zeros: 3.958, 7.086,.. */
3408 return j0_y0_approx(ix
, x
, TRUE
);
3411 if (ix
>= 0x3e400000) { /* x >= 2**-27 */
3412 /* large ulp error near the first zero, x ~= 0.89 */
3414 u
= u00
+ z
* (u01
+ z
* (u02
+ z
* (u03
+ z
* (u04
+ z
* (u05
+ z
* u06
)))));
3415 v
= 1.0 + z
* (v01
+ z
* (v02
+ z
* (v03
+ z
* v04
)));
3416 return u
/ v
+ tpi
* (j0(x
) * log(x
));
3418 return u00
+ tpi
* log(x
);
3421 /*********************************************************************
3424 double CDECL
_y1(double x
)
3426 static const double tpi
= 6.36619772367581382433e-01,
3427 u00
= -1.96057090646238940668e-01,
3428 u01
= 5.04438716639811282616e-02,
3429 u02
= -1.91256895875763547298e-03,
3430 u03
= 2.35252600561610495928e-05,
3431 u04
= -9.19099158039878874504e-08,
3432 v00
= 1.99167318236649903973e-02,
3433 v01
= 2.02552581025135171496e-04,
3434 v02
= 1.35608801097516229404e-06,
3435 v03
= 6.22741452364621501295e-09,
3436 v04
= 1.66559246207992079114e-11;
3439 unsigned int ix
, lx
;
3441 ix
= *(ULONGLONG
*)&x
>> 32;
3442 lx
= *(ULONGLONG
*)&x
;
3444 /* y1(nan)=nan, y1(<0)=nan, y1(0)=-inf, y1(inf)=0 */
3445 if ((ix
<< 1 | lx
) == 0)
3446 return math_error(_OVERFLOW
, "_y1", x
, 0, -INFINITY
);
3450 return math_error(_DOMAIN
, "_y1", x
, 0, 0 / (x
- x
));
3451 if (ix
>= 0x7ff00000)
3454 if (ix
>= 0x40000000) /* x >= 2 */
3455 return j1_y1_approx(ix
, x
, TRUE
, 0);
3456 if (ix
< 0x3c900000) /* x < 2**-54 */
3459 u
= u00
+ z
* (u01
+ z
* (u02
+ z
* (u03
+ z
* u04
)));
3460 v
= 1 + z
* (v00
+ z
* (v01
+ z
* (v02
+ z
* (v03
+ z
* v04
))));
3461 return x
* (u
/ v
) + tpi
* (j1(x
) * log(x
) - 1 / x
);
3464 /*********************************************************************
3467 * Copied from musl: src/math/jn.c
3469 double CDECL
_yn(int n
, double x
)
3471 static const double invsqrtpi
= 5.64189583547756279280e-01;
3473 unsigned int ix
, lx
, ib
;
3477 ix
= *(ULONGLONG
*)&x
>> 32;
3478 lx
= *(ULONGLONG
*)&x
;
3482 if ((ix
| (lx
| -lx
) >> 31) > 0x7ff00000) /* nan */
3484 if (sign
&& (ix
| lx
) != 0) /* x < 0 */
3485 return math_error(_DOMAIN
, "_y1", x
, 0, 0 / (x
- x
));
3486 if (ix
== 0x7ff00000)
3499 return sign
? -y1(x
) : y1(x
);
3501 if (ix
>= 0x52d00000) { /* x > 2**302 */
3504 temp
= -sin(x
) - cos(x
);
3507 temp
= -sin(x
) + cos(x
);
3510 temp
= sin(x
) + cos(x
);
3513 temp
= sin(x
) - cos(x
);
3516 b
= invsqrtpi
* temp
/ sqrt(x
);
3520 /* quit if b is -inf */
3521 ib
= *(ULONGLONG
*)&b
>> 32;
3522 for (i
= 0; i
< nm1
&& ib
!= 0xfff00000;) {
3525 b
= (2.0 * i
/ x
) * b
- a
;
3526 ib
= *(ULONGLONG
*)&b
>> 32;
3530 return sign
? -b
: b
;
3535 /*********************************************************************
3536 * _nearbyint (MSVCR120.@)
3538 * Based on musl: src/math/nearbyteint.c
3540 double CDECL
nearbyint(double x
)
3545 _control87(_MCW_EM
, _MCW_EM
);
3547 feclearexcept(FE_INEXACT
);
3552 /*********************************************************************
3553 * _nearbyintf (MSVCR120.@)
3555 * Based on musl: src/math/nearbyteintf.c
3557 float CDECL
nearbyintf(float x
)
3562 _control87(_MCW_EM
, _MCW_EM
);
3564 feclearexcept(FE_INEXACT
);
3569 /*********************************************************************
3570 * nexttoward (MSVCR120.@)
3572 double CDECL
MSVCRT_nexttoward(double num
, double next
)
3574 return _nextafter(num
, next
);
3577 /*********************************************************************
3578 * nexttowardf (MSVCR120.@)
3580 * Copied from musl: src/math/nexttowardf.c
3582 float CDECL
MSVCRT_nexttowardf(float x
, double y
)
3584 unsigned int ix
= *(unsigned int*)&x
;
3588 if (isnan(x
) || isnan(y
))
3607 e
= ix
& 0x7f800000;
3608 /* raise overflow if ix is infinite and x is finite */
3609 if (e
== 0x7f800000) {
3614 /* raise underflow if ret is subnormal or zero */
3616 fp_barrierf(x
* x
+ ret
* ret
);
3622 #endif /* _MSVCR_VER>=120 */
3624 /*********************************************************************
3625 * _nextafter (MSVCRT.@)
3627 * Copied from musl: src/math/nextafter.c
3629 double CDECL
_nextafter(double x
, double y
)
3631 ULONGLONG llx
= *(ULONGLONG
*)&x
;
3632 ULONGLONG lly
= *(ULONGLONG
*)&y
;
3636 if (isnan(x
) || isnan(y
))
3639 if (_fpclass(y
) & (_FPCLASS_ND
| _FPCLASS_PD
| _FPCLASS_NZ
| _FPCLASS_PZ
))
3643 ax
= llx
& -1ULL / 2;
3644 ay
= lly
& -1ULL / 2;
3648 llx
= (lly
& 1ULL << 63) | 1;
3649 } else if (ax
> ay
|| ((llx
^ lly
) & 1ULL << 63))
3653 e
= llx
>> 52 & 0x7ff;
3654 /* raise overflow if llx is infinite and x is finite */
3659 /* raise underflow if llx is subnormal or zero */
3662 fp_barrier(x
* x
+ y
* y
);
3668 /*********************************************************************
3671 char * CDECL
_ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
3674 thread_data_t
*data
= msvcrt_get_thread_data();
3675 /* FIXME: check better for overflow (native supports over 300 chars) */
3676 ndigits
= min( ndigits
, 80 - 8); /* 8 : space for sign, dec point, "e",
3677 * 4 for exponent and one for
3678 * terminating '\0' */
3679 if (!data
->efcvt_buffer
)
3680 data
->efcvt_buffer
= malloc( 80 ); /* ought to be enough */
3682 /* handle cases with zero ndigits or less */
3684 if( prec
< 1) prec
= 2;
3685 len
= _snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
3687 if (data
->efcvt_buffer
[0] == '-') {
3688 memmove( data
->efcvt_buffer
, data
->efcvt_buffer
+ 1, len
-- );
3692 /* take the decimal "point away */
3694 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
3695 /* take the exponential "e" out */
3696 data
->efcvt_buffer
[ prec
] = '\0';
3697 /* read the exponent */
3698 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
3700 /* adjust for some border cases */
3701 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
3703 /* handle cases with zero ndigits or less */
3705 if( data
->efcvt_buffer
[ 0] >= '5')
3707 data
->efcvt_buffer
[ 0] = '\0';
3709 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
3710 return data
->efcvt_buffer
;
3713 /*********************************************************************
3714 * _ecvt_s (MSVCRT.@)
3716 int CDECL
_ecvt_s( char *buffer
, size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
3721 if (!MSVCRT_CHECK_PMT(buffer
!= NULL
)) return EINVAL
;
3722 if (!MSVCRT_CHECK_PMT(decpt
!= NULL
)) return EINVAL
;
3723 if (!MSVCRT_CHECK_PMT(sign
!= NULL
)) return EINVAL
;
3724 if (!MSVCRT_CHECK_PMT_ERR( length
> 2, ERANGE
)) return ERANGE
;
3725 if (!MSVCRT_CHECK_PMT_ERR(ndigits
< (int)length
- 1, ERANGE
)) return ERANGE
;
3727 /* handle cases with zero ndigits or less */
3729 if( prec
< 1) prec
= 2;
3730 result
= malloc(prec
+ 8);
3732 len
= _snprintf(result
, prec
+ 8, "%.*le", prec
- 1, number
);
3733 if (result
[0] == '-') {
3734 memmove( result
, result
+ 1, len
-- );
3738 /* take the decimal "point away */
3740 memmove( result
+ 1, result
+ 2, len
- 1 );
3741 /* take the exponential "e" out */
3742 result
[ prec
] = '\0';
3743 /* read the exponent */
3744 sscanf( result
+ prec
+ 1, "%d", decpt
);
3746 /* adjust for some border cases */
3747 if( result
[0] == '0')/* value is zero */
3749 /* handle cases with zero ndigits or less */
3751 if( result
[ 0] >= '5')
3755 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
3760 /***********************************************************************
3763 char * CDECL
_fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
3765 thread_data_t
*data
= msvcrt_get_thread_data();
3766 int stop
, dec1
, dec2
;
3767 char *ptr1
, *ptr2
, *first
;
3768 char buf
[80]; /* ought to be enough */
3769 char decimal_separator
= get_locinfo()->lconv
->decimal_point
[0];
3771 if (!data
->efcvt_buffer
)
3772 data
->efcvt_buffer
= malloc( 80 ); /* ought to be enough */
3774 stop
= _snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
3776 ptr2
= data
->efcvt_buffer
;
3786 /* For numbers below the requested resolution, work out where
3787 the decimal point will be rather than finding it in the string */
3788 if (number
< 1.0 && number
> 0.0) {
3789 dec2
= log10(number
+ 1e-10);
3790 if (-dec2
<= ndigits
) dec2
= 0;
3793 /* If requested digits is zero or less, we will need to truncate
3794 * the returned string */
3799 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
3800 while (*ptr1
!= '\0' && *ptr1
!= decimal_separator
) {
3801 if (!first
) first
= ptr2
;
3802 if ((ptr1
- buf
) < stop
) {
3813 while (*ptr1
== '0') { /* Process leading zeroes */
3818 while (*ptr1
!= '\0') {
3819 if (!first
) first
= ptr2
;
3826 /* We never found a non-zero digit, then our number is either
3827 * smaller than the requested precision, or 0.0 */
3832 first
= data
->efcvt_buffer
;
3837 *decpt
= dec2
? dec2
: dec1
;
3841 /***********************************************************************
3842 * _fcvt_s (MSVCRT.@)
3844 int CDECL
_fcvt_s(char* outbuffer
, size_t size
, double number
, int ndigits
, int *decpt
, int *sign
)
3846 int stop
, dec1
, dec2
;
3847 char *ptr1
, *ptr2
, *first
;
3848 char buf
[80]; /* ought to be enough */
3849 char decimal_separator
= get_locinfo()->lconv
->decimal_point
[0];
3851 if (!outbuffer
|| !decpt
|| !sign
|| size
== 0)
3857 stop
= _snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
3869 /* For numbers below the requested resolution, work out where
3870 the decimal point will be rather than finding it in the string */
3871 if (number
< 1.0 && number
> 0.0) {
3872 dec2
= log10(number
+ 1e-10);
3873 if (-dec2
<= ndigits
) dec2
= 0;
3876 /* If requested digits is zero or less, we will need to truncate
3877 * the returned string */
3882 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
3883 while (*ptr1
!= '\0' && *ptr1
!= decimal_separator
) {
3884 if (!first
) first
= ptr2
;
3885 if ((ptr1
- buf
) < stop
) {
3899 while (*ptr1
== '0') { /* Process leading zeroes */
3900 if (number
== 0.0 && size
> 1) {
3908 while (*ptr1
!= '\0') {
3909 if (!first
) first
= ptr2
;
3919 /* We never found a non-zero digit, then our number is either
3920 * smaller than the requested precision, or 0.0 */
3921 if (!first
&& (number
<= 0.0))
3924 *decpt
= dec2
? dec2
: dec1
;
3928 /***********************************************************************
3931 char * CDECL
_gcvt( double number
, int ndigit
, char *buff
)
3943 sprintf(buff
, "%.*g", ndigit
, number
);
3947 /***********************************************************************
3948 * _gcvt_s (MSVCRT.@)
3950 int CDECL
_gcvt_s(char *buff
, size_t size
, double number
, int digits
)
3959 if( digits
<0 || digits
>=size
) {
3967 len
= _scprintf("%.*g", digits
, number
);
3974 sprintf(buff
, "%.*g", digits
, number
);
3978 #include <stdlib.h> /* div_t, ldiv_t */
3980 /*********************************************************************
3983 * [i386] Windows binary compatible - returns the struct in eax/edx.
3986 unsigned __int64 CDECL
div(int num
, int denom
)
3990 unsigned __int64 uint64
;
3993 ret
.div
.quot
= num
/ denom
;
3994 ret
.div
.rem
= num
% denom
;
3998 /*********************************************************************
4001 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
4003 div_t CDECL
div(int num
, int denom
)
4007 ret
.quot
= num
/ denom
;
4008 ret
.rem
= num
% denom
;
4011 #endif /* ifdef __i386__ */
4014 /*********************************************************************
4017 * [i386] Windows binary compatible - returns the struct in eax/edx.
4020 unsigned __int64 CDECL
ldiv(__msvcrt_long num
, __msvcrt_long denom
)
4024 unsigned __int64 uint64
;
4027 ret
.ldiv
.quot
= num
/ denom
;
4028 ret
.ldiv
.rem
= num
% denom
;
4032 /*********************************************************************
4035 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
4037 ldiv_t CDECL
ldiv(__msvcrt_long num
, __msvcrt_long denom
)
4041 ret
.quot
= num
/ denom
;
4042 ret
.rem
= num
% denom
;
4045 #endif /* ifdef __i386__ */
4048 /*********************************************************************
4049 * lldiv (MSVCR100.@)
4051 lldiv_t CDECL
lldiv(__int64 num
, __int64 denom
)
4055 ret
.quot
= num
/ denom
;
4056 ret
.rem
= num
% denom
;
4064 /*********************************************************************
4065 * _adjust_fdiv (MSVCRT.@)
4066 * Used by the MSVC compiler to work around the Pentium FDIV bug.
4068 int MSVCRT__adjust_fdiv
= 0;
4070 /***********************************************************************
4071 * _adj_fdiv_m16i (MSVCRT.@)
4074 * I _think_ this function is intended to work around the Pentium
4077 void __stdcall
_adj_fdiv_m16i( short arg
)
4079 TRACE("(): stub\n");
4082 /***********************************************************************
4083 * _adj_fdiv_m32 (MSVCRT.@)
4086 * I _think_ this function is intended to work around the Pentium
4089 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
4091 TRACE("(): stub\n");
4094 /***********************************************************************
4095 * _adj_fdiv_m32i (MSVCRT.@)
4098 * I _think_ this function is intended to work around the Pentium
4101 void __stdcall
_adj_fdiv_m32i( int arg
)
4103 TRACE("(): stub\n");
4106 /***********************************************************************
4107 * _adj_fdiv_m64 (MSVCRT.@)
4110 * I _think_ this function is intended to work around the Pentium
4113 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
4115 TRACE("(): stub\n");
4118 /***********************************************************************
4119 * _adj_fdiv_r (MSVCRT.@)
4121 * This function is likely to have the wrong number of arguments.
4124 * I _think_ this function is intended to work around the Pentium
4127 void _adj_fdiv_r(void)
4129 TRACE("(): stub\n");
4132 /***********************************************************************
4133 * _adj_fdivr_m16i (MSVCRT.@)
4136 * I _think_ this function is intended to work around the Pentium
4139 void __stdcall
_adj_fdivr_m16i( short arg
)
4141 TRACE("(): stub\n");
4144 /***********************************************************************
4145 * _adj_fdivr_m32 (MSVCRT.@)
4148 * I _think_ this function is intended to work around the Pentium
4151 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
4153 TRACE("(): stub\n");
4156 /***********************************************************************
4157 * _adj_fdivr_m32i (MSVCRT.@)
4160 * I _think_ this function is intended to work around the Pentium
4163 void __stdcall
_adj_fdivr_m32i( int arg
)
4165 TRACE("(): stub\n");
4168 /***********************************************************************
4169 * _adj_fdivr_m64 (MSVCRT.@)
4172 * I _think_ this function is intended to work around the Pentium
4175 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
4177 TRACE("(): stub\n");
4180 /***********************************************************************
4181 * _adj_fpatan (MSVCRT.@)
4183 * This function is likely to have the wrong number of arguments.
4186 * I _think_ this function is intended to work around the Pentium
4189 void _adj_fpatan(void)
4191 TRACE("(): stub\n");
4194 /***********************************************************************
4195 * _adj_fprem (MSVCRT.@)
4197 * This function is likely to have the wrong number of arguments.
4200 * I _think_ this function is intended to work around the Pentium
4203 void _adj_fprem(void)
4205 TRACE("(): stub\n");
4208 /***********************************************************************
4209 * _adj_fprem1 (MSVCRT.@)
4211 * This function is likely to have the wrong number of arguments.
4214 * I _think_ this function is intended to work around the Pentium
4217 void _adj_fprem1(void)
4219 TRACE("(): stub\n");
4222 /***********************************************************************
4223 * _adj_fptan (MSVCRT.@)
4225 * This function is likely to have the wrong number of arguments.
4228 * I _think_ this function is intended to work around the Pentium
4231 void _adj_fptan(void)
4233 TRACE("(): stub\n");
4236 /***********************************************************************
4237 * _safe_fdiv (MSVCRT.@)
4239 * This function is likely to have the wrong number of arguments.
4242 * I _think_ this function is intended to work around the Pentium
4245 void _safe_fdiv(void)
4247 TRACE("(): stub\n");
4250 /***********************************************************************
4251 * _safe_fdivr (MSVCRT.@)
4253 * This function is likely to have the wrong number of arguments.
4256 * I _think_ this function is intended to work around the Pentium
4259 void _safe_fdivr(void)
4261 TRACE("(): stub\n");
4264 /***********************************************************************
4265 * _safe_fprem (MSVCRT.@)
4267 * This function is likely to have the wrong number of arguments.
4270 * I _think_ this function is intended to work around the Pentium
4273 void _safe_fprem(void)
4275 TRACE("(): stub\n");
4278 /***********************************************************************
4279 * _safe_fprem1 (MSVCRT.@)
4282 * This function is likely to have the wrong number of arguments.
4285 * I _think_ this function is intended to work around the Pentium
4288 void _safe_fprem1(void)
4290 TRACE("(): stub\n");
4293 /***********************************************************************
4294 * __libm_sse2_acos (MSVCRT.@)
4296 void __cdecl
__libm_sse2_acos(void)
4299 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4301 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4304 /***********************************************************************
4305 * __libm_sse2_acosf (MSVCRT.@)
4307 void __cdecl
__libm_sse2_acosf(void)
4310 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4312 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4315 /***********************************************************************
4316 * __libm_sse2_asin (MSVCRT.@)
4318 void __cdecl
__libm_sse2_asin(void)
4321 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4323 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4326 /***********************************************************************
4327 * __libm_sse2_asinf (MSVCRT.@)
4329 void __cdecl
__libm_sse2_asinf(void)
4332 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4334 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4337 /***********************************************************************
4338 * __libm_sse2_atan (MSVCRT.@)
4340 void __cdecl
__libm_sse2_atan(void)
4343 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4345 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4348 /***********************************************************************
4349 * __libm_sse2_atan2 (MSVCRT.@)
4351 void __cdecl
__libm_sse2_atan2(void)
4354 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
4355 d1
= atan2( d1
, d2
);
4356 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
4359 /***********************************************************************
4360 * __libm_sse2_atanf (MSVCRT.@)
4362 void __cdecl
__libm_sse2_atanf(void)
4365 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4367 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4370 /***********************************************************************
4371 * __libm_sse2_cos (MSVCRT.@)
4373 void __cdecl
__libm_sse2_cos(void)
4376 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4378 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4381 /***********************************************************************
4382 * __libm_sse2_cosf (MSVCRT.@)
4384 void __cdecl
__libm_sse2_cosf(void)
4387 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4389 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4392 /***********************************************************************
4393 * __libm_sse2_exp (MSVCRT.@)
4395 void __cdecl
__libm_sse2_exp(void)
4398 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4400 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4403 /***********************************************************************
4404 * __libm_sse2_expf (MSVCRT.@)
4406 void __cdecl
__libm_sse2_expf(void)
4409 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4411 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4414 /***********************************************************************
4415 * __libm_sse2_log (MSVCRT.@)
4417 void __cdecl
__libm_sse2_log(void)
4420 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4422 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4425 /***********************************************************************
4426 * __libm_sse2_log10 (MSVCRT.@)
4428 void __cdecl
__libm_sse2_log10(void)
4431 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4433 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4436 /***********************************************************************
4437 * __libm_sse2_log10f (MSVCRT.@)
4439 void __cdecl
__libm_sse2_log10f(void)
4442 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4444 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4447 /***********************************************************************
4448 * __libm_sse2_logf (MSVCRT.@)
4450 void __cdecl
__libm_sse2_logf(void)
4453 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4455 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4458 /***********************************************************************
4459 * __libm_sse2_pow (MSVCRT.@)
4461 void __cdecl
__libm_sse2_pow(void)
4464 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
4466 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
4469 /***********************************************************************
4470 * __libm_sse2_powf (MSVCRT.@)
4472 void __cdecl
__libm_sse2_powf(void)
4475 __asm__
__volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1
), "=g" (f2
) );
4476 f1
= powf( f1
, f2
);
4477 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f1
) );
4480 /***********************************************************************
4481 * __libm_sse2_sin (MSVCRT.@)
4483 void __cdecl
__libm_sse2_sin(void)
4486 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4488 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4491 /***********************************************************************
4492 * __libm_sse2_sinf (MSVCRT.@)
4494 void __cdecl
__libm_sse2_sinf(void)
4497 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4499 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4502 /***********************************************************************
4503 * __libm_sse2_tan (MSVCRT.@)
4505 void __cdecl
__libm_sse2_tan(void)
4508 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4510 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4513 /***********************************************************************
4514 * __libm_sse2_tanf (MSVCRT.@)
4516 void __cdecl
__libm_sse2_tanf(void)
4519 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4521 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4524 /***********************************************************************
4525 * __libm_sse2_sqrt_precise (MSVCR110.@)
4527 void __cdecl
__libm_sse2_sqrt_precise(void)
4532 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4533 __control87_2(0, 0, NULL
, &cw
);
4537 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4541 if (!sqrt_validate(&d
, FALSE
))
4543 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4546 __asm__
__volatile__( "call " __ASM_NAME( "sse2_sqrt" ) );
4548 #endif /* __i386__ */
4550 /*********************************************************************
4551 * _fdclass (MSVCR120.@)
4553 * Copied from musl: src/math/__fpclassifyf.c
4555 short CDECL
_fdclass(float x
)
4557 union { float f
; UINT32 i
; } u
= { x
};
4558 int e
= u
.i
>> 23 & 0xff;
4560 if (!e
) return u
.i
<< 1 ? FP_SUBNORMAL
: FP_ZERO
;
4561 if (e
== 0xff) return u
.i
<< 9 ? FP_NAN
: FP_INFINITE
;
4565 /*********************************************************************
4566 * _dclass (MSVCR120.@)
4568 * Copied from musl: src/math/__fpclassify.c
4570 short CDECL
_dclass(double x
)
4572 union { double f
; UINT64 i
; } u
= { x
};
4573 int e
= u
.i
>> 52 & 0x7ff;
4575 if (!e
) return u
.i
<< 1 ? FP_SUBNORMAL
: FP_ZERO
;
4576 if (e
== 0x7ff) return (u
.i
<< 12) ? FP_NAN
: FP_INFINITE
;
4582 /*********************************************************************
4585 * Copied from musl: src/math/cbrt.c
4587 double CDECL
cbrt(double x
)
4589 static const UINT32 B1
= 715094163, B2
= 696219795;
4590 static const double P0
= 1.87595182427177009643,
4591 P1
= -1.88497979543377169875,
4592 P2
= 1.621429720105354466140,
4593 P3
= -0.758397934778766047437,
4594 P4
= 0.145996192886612446982;
4596 union {double f
; UINT64 i
;} u
= {x
};
4598 UINT32 hx
= u
.i
>> 32 & 0x7fffffff;
4600 if (hx
>= 0x7ff00000) /* cbrt(NaN,INF) is itself */
4603 if (hx
< 0x00100000) { /* zero or subnormal? */
4605 hx
= u
.i
>>32 & 0x7fffffff;
4612 u
.i
|= (UINT64
)hx
<< 32;
4615 r
= (t
* t
) * (t
/ x
);
4616 t
= t
* ((P0
+ r
* (P1
+ r
* P2
)) + ((r
* r
) * r
) * (P3
+ r
* P4
));
4619 u
.i
= (u
.i
+ 0x80000000) & 0xffffffffc0000000ULL
;
4625 r
= (r
- t
) / (w
+ r
);
4630 /*********************************************************************
4631 * cbrtf (MSVCR120.@)
4633 * Copied from musl: src/math/cbrtf.c
4635 float CDECL
cbrtf(float x
)
4637 static const unsigned B1
= 709958130, B2
= 642849266;
4640 union {float f
; UINT32 i
;} u
= {x
};
4641 UINT32 hx
= u
.i
& 0x7fffffff;
4643 if (hx
>= 0x7f800000)
4646 if (hx
< 0x00800000) { /* zero or subnormal? */
4650 hx
= u
.i
& 0x7fffffff;
4659 T
= T
* (x
+ x
+ r
) / (x
+ r
+ r
);
4662 T
= T
* (x
+ x
+ r
) / (x
+ r
+ r
);
4666 /*********************************************************************
4669 double CDECL
exp2(double x
)
4671 double ret
= unix_funcs
->exp2( x
);
4672 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4676 /*********************************************************************
4677 * exp2f (MSVCR120.@)
4679 float CDECL
exp2f(float x
)
4681 float ret
= unix_funcs
->exp2f( x
);
4682 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4686 /*********************************************************************
4687 * expm1 (MSVCR120.@)
4689 double CDECL
expm1(double x
)
4691 double ret
= unix_funcs
->expm1( x
);
4692 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4696 /*********************************************************************
4697 * expm1f (MSVCR120.@)
4699 float CDECL
expm1f(float x
)
4701 float ret
= unix_funcs
->expm1f( x
);
4702 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4706 /*********************************************************************
4707 * log1p (MSVCR120.@)
4709 double CDECL
log1p(double x
)
4711 if (x
< -1) *_errno() = EDOM
;
4712 else if (x
== -1) *_errno() = ERANGE
;
4713 return unix_funcs
->log1p( x
);
4716 /*********************************************************************
4717 * log1pf (MSVCR120.@)
4719 float CDECL
log1pf(float x
)
4721 if (x
< -1) *_errno() = EDOM
;
4722 else if (x
== -1) *_errno() = ERANGE
;
4723 return unix_funcs
->log1pf( x
);
4726 /*********************************************************************
4729 double CDECL
log2(double x
)
4731 if (x
< 0) *_errno() = EDOM
;
4732 else if (x
== 0) *_errno() = ERANGE
;
4733 return unix_funcs
->log2( x
);
4736 /*********************************************************************
4737 * log2f (MSVCR120.@)
4739 float CDECL
log2f(float x
)
4741 if (x
< 0) *_errno() = EDOM
;
4742 else if (x
== 0) *_errno() = ERANGE
;
4743 return unix_funcs
->log2f( x
);
4746 /*********************************************************************
4749 * Copied from musl: src/math/rint.c
4751 double CDECL
rint(double x
)
4753 static const double toint
= 1 / DBL_EPSILON
;
4755 ULONGLONG llx
= *(ULONGLONG
*)&x
;
4756 int e
= llx
>> 52 & 0x7ff;
4763 cw
= _controlfp(0, 0);
4764 if ((cw
& _MCW_PC
) != _PC_53
)
4765 _controlfp(_PC_53
, _MCW_PC
);
4767 y
= fp_barrier(x
- toint
) + toint
;
4769 y
= fp_barrier(x
+ toint
) - toint
;
4770 if ((cw
& _MCW_PC
) != _PC_53
)
4771 _controlfp(cw
, _MCW_PC
);
4773 return s
? -0.0 : 0;
4777 /*********************************************************************
4778 * rintf (MSVCR120.@)
4780 * Copied from musl: src/math/rintf.c
4782 float CDECL
rintf(float x
)
4784 static const float toint
= 1 / FLT_EPSILON
;
4786 unsigned int ix
= *(unsigned int*)&x
;
4787 int e
= ix
>> 23 & 0xff;
4794 y
= fp_barrierf(x
- toint
) + toint
;
4796 y
= fp_barrierf(x
+ toint
) - toint
;
4798 return s
? -0.0f
: 0.0f
;
4802 /*********************************************************************
4803 * lrint (MSVCR120.@)
4805 __msvcrt_long CDECL
lrint(double x
)
4810 if ((d
< 0 && d
!= (double)(__msvcrt_long
)d
)
4811 || (d
>= 0 && d
!= (double)(__msvcrt_ulong
)d
)) {
4818 /*********************************************************************
4819 * lrintf (MSVCR120.@)
4821 __msvcrt_long CDECL
lrintf(float x
)
4826 if ((f
< 0 && f
!= (float)(__msvcrt_long
)f
)
4827 || (f
>= 0 && f
!= (float)(__msvcrt_ulong
)f
)) {
4834 /*********************************************************************
4835 * llrint (MSVCR120.@)
4837 __int64 CDECL
llrint(double x
)
4842 if ((d
< 0 && d
!= (double)(__int64
)d
)
4843 || (d
>= 0 && d
!= (double)(unsigned __int64
)d
)) {
4850 /*********************************************************************
4851 * llrintf (MSVCR120.@)
4853 __int64 CDECL
llrintf(float x
)
4858 if ((f
< 0 && f
!= (float)(__int64
)f
)
4859 || (f
>= 0 && f
!= (float)(unsigned __int64
)f
)) {
4866 /*********************************************************************
4867 * round (MSVCR120.@)
4869 * Based on musl implementation: src/math/round.c
4871 double CDECL
round(double x
)
4873 ULONGLONG llx
= *(ULONGLONG
*)&x
, tmp
;
4874 int e
= (llx
>> 52 & 0x7ff) - 0x3ff;
4881 return signbit(x
) ? -1 : 1;
4883 tmp
= 0x000fffffffffffffULL
>> e
;
4886 llx
+= 0x0008000000000000ULL
>> e
;
4888 return *(double*)&llx
;
4891 /*********************************************************************
4892 * roundf (MSVCR120.@)
4894 * Copied from musl: src/math/roundf.c
4896 float CDECL
roundf(float x
)
4898 static const float toint
= 1 / FLT_EPSILON
;
4900 unsigned int ix
= *(unsigned int*)&x
;
4901 int e
= ix
>> 23 & 0xff;
4909 return 0 * *(float*)&ix
;
4910 y
= fp_barrierf(x
+ toint
) - toint
- x
;
4913 else if (y
<= -0.5f
)
4922 /*********************************************************************
4923 * lround (MSVCR120.@)
4925 * Copied from musl: src/math/lround.c
4927 __msvcrt_long CDECL
lround(double x
)
4929 double d
= round(x
);
4930 if (d
!= (double)(__msvcrt_long
)d
) {
4937 /*********************************************************************
4938 * lroundf (MSVCR120.@)
4940 * Copied from musl: src/math/lroundf.c
4942 __msvcrt_long CDECL
lroundf(float x
)
4944 float f
= roundf(x
);
4945 if (f
!= (float)(__msvcrt_long
)f
) {
4952 /*********************************************************************
4953 * llround (MSVCR120.@)
4955 * Copied from musl: src/math/llround.c
4957 __int64 CDECL
llround(double x
)
4959 double d
= round(x
);
4960 if (d
!= (double)(__int64
)d
) {
4967 /*********************************************************************
4968 * llroundf (MSVCR120.@)
4970 * Copied from musl: src/math/llroundf.c
4972 __int64 CDECL
llroundf(float x
)
4974 float f
= roundf(x
);
4975 if (f
!= (float)(__int64
)f
) {
4982 /*********************************************************************
4983 * trunc (MSVCR120.@)
4985 double CDECL
trunc(double x
)
4987 return unix_funcs
->trunc(x
);
4990 /*********************************************************************
4991 * truncf (MSVCR120.@)
4993 * Copied from musl: src/math/truncf.c
4995 float CDECL
truncf(float x
)
4997 union {float f
; UINT32 i
;} u
= {x
};
4998 int e
= (u
.i
>> 23 & 0xff) - 0x7f + 9;
5012 /*********************************************************************
5013 * _dtest (MSVCR120.@)
5015 short CDECL
_dtest(double *x
)
5020 /*********************************************************************
5021 * _fdtest (MSVCR120.@)
5023 short CDECL
_fdtest(float *x
)
5025 return _fdclass(*x
);
5028 /*********************************************************************
5031 double CDECL
erf(double x
)
5033 return unix_funcs
->erf( x
);
5036 /*********************************************************************
5039 float CDECL
erff(float x
)
5041 return unix_funcs
->erff( x
);
5044 /*********************************************************************
5047 double CDECL
erfc(double x
)
5049 return unix_funcs
->erfc( x
);
5052 /*********************************************************************
5053 * erfcf (MSVCR120.@)
5055 float CDECL
erfcf(float x
)
5057 return unix_funcs
->erfcf( x
);
5060 /*********************************************************************
5061 * fmaxf (MSVCR120.@)
5063 float CDECL
fmaxf(float x
, float y
)
5070 return signbit(x
) ? y
: x
;
5074 /*********************************************************************
5077 double CDECL
fmax(double x
, double y
)
5084 return signbit(x
) ? y
: x
;
5088 /*********************************************************************
5089 * fdimf (MSVCR120.@)
5091 float CDECL
fdimf(float x
, float y
)
5097 return x
>y
? x
-y
: 0;
5100 /*********************************************************************
5103 double CDECL
fdim(double x
, double y
)
5109 return x
>y
? x
-y
: 0;
5112 /*********************************************************************
5113 * _fdsign (MSVCR120.@)
5115 int CDECL
_fdsign(float x
)
5117 union { float f
; UINT32 i
; } u
= { x
};
5118 return (u
.i
>> 16) & 0x8000;
5121 /*********************************************************************
5122 * _dsign (MSVCR120.@)
5124 int CDECL
_dsign(double x
)
5126 union { double f
; UINT64 i
; } u
= { x
};
5127 return (u
.i
>> 48) & 0x8000;
5131 /*********************************************************************
5132 * _dpcomp (MSVCR120.@)
5134 int CDECL
_dpcomp(double x
, double y
)
5136 if(isnan(x
) || isnan(y
))
5139 if(x
== y
) return 2;
5140 return x
< y
? 1 : 4;
5143 /*********************************************************************
5144 * _fdpcomp (MSVCR120.@)
5146 int CDECL
_fdpcomp(float x
, float y
)
5148 return _dpcomp(x
, y
);
5151 /*********************************************************************
5152 * fminf (MSVCR120.@)
5154 float CDECL
fminf(float x
, float y
)
5161 return signbit(x
) ? x
: y
;
5165 /*********************************************************************
5168 double CDECL
fmin(double x
, double y
)
5175 return signbit(x
) ? x
: y
;
5179 /*********************************************************************
5180 * asinh (MSVCR120.@)
5182 double CDECL
asinh(double x
)
5184 return unix_funcs
->asinh( x
);
5187 /*********************************************************************
5188 * asinhf (MSVCR120.@)
5190 float CDECL
asinhf(float x
)
5192 return unix_funcs
->asinhf( x
);
5195 /*********************************************************************
5196 * acosh (MSVCR120.@)
5198 double CDECL
acosh(double x
)
5203 feraiseexcept(FE_INVALID
);
5206 return unix_funcs
->acosh( x
);
5209 /*********************************************************************
5210 * acoshf (MSVCR120.@)
5212 float CDECL
acoshf(float x
)
5217 feraiseexcept(FE_INVALID
);
5220 return unix_funcs
->acoshf( x
);
5223 /*********************************************************************
5224 * atanh (MSVCR120.@)
5226 double CDECL
atanh(double x
)
5230 if (x
> 1 || x
< -1) {
5232 /* on Linux atanh returns -NAN in this case */
5233 feraiseexcept(FE_INVALID
);
5236 ret
= unix_funcs
->atanh( x
);
5238 if (!isfinite(ret
)) *_errno() = ERANGE
;
5242 /*********************************************************************
5243 * atanhf (MSVCR120.@)
5245 float CDECL
atanhf(float x
)
5249 if (x
> 1 || x
< -1) {
5251 feraiseexcept(FE_INVALID
);
5255 ret
= unix_funcs
->atanh( x
);
5257 if (!isfinite(ret
)) *_errno() = ERANGE
;
5261 #endif /* _MSVCR_VER>=120 */
5263 /*********************************************************************
5265 * scalbn (MSVCR120.@)
5266 * scalbln (MSVCR120.@)
5268 double CDECL
_scalb(double num
, __msvcrt_long power
)
5270 return ldexp(num
, power
);
5273 /*********************************************************************
5274 * _scalbf (MSVCRT.@)
5275 * scalbnf (MSVCR120.@)
5276 * scalblnf (MSVCR120.@)
5278 float CDECL
_scalbf(float num
, __msvcrt_long power
)
5280 return ldexp(num
, power
);
5285 /*********************************************************************
5286 * remainder (MSVCR120.@)
5288 double CDECL
remainder(double x
, double y
)
5290 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
5291 if(!isfinite(x
)) *_errno() = EDOM
;
5292 if(isnan(y
) || y
==0.0) *_errno() = EDOM
;
5293 return unix_funcs
->remainder( x
, y
);
5296 /*********************************************************************
5297 * remainderf (MSVCR120.@)
5299 float CDECL
remainderf(float x
, float y
)
5301 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
5302 if(!isfinite(x
)) *_errno() = EDOM
;
5303 if(isnan(y
) || y
==0.0f
) *_errno() = EDOM
;
5304 return unix_funcs
->remainderf( x
, y
);
5307 /*********************************************************************
5308 * remquo (MSVCR120.@)
5310 double CDECL
remquo(double x
, double y
, int *quo
)
5312 if(!isfinite(x
)) *_errno() = EDOM
;
5313 if(isnan(y
) || y
==0.0) *_errno() = EDOM
;
5314 return unix_funcs
->remquo( x
, y
, quo
);
5317 /*********************************************************************
5318 * remquof (MSVCR120.@)
5320 float CDECL
remquof(float x
, float y
, int *quo
)
5322 if(!isfinite(x
)) *_errno() = EDOM
;
5323 if(isnan(y
) || y
==0.0f
) *_errno() = EDOM
;
5324 return unix_funcs
->remquof( x
, y
, quo
);
5327 /*********************************************************************
5328 * lgamma (MSVCR120.@)
5330 double CDECL
lgamma(double x
)
5332 return unix_funcs
->lgamma( x
);
5335 /*********************************************************************
5336 * lgammaf (MSVCR120.@)
5338 float CDECL
lgammaf(float x
)
5340 return unix_funcs
->lgammaf( x
);
5343 /*********************************************************************
5344 * tgamma (MSVCR120.@)
5346 double CDECL
tgamma(double x
)
5348 return unix_funcs
->tgamma( x
);
5351 /*********************************************************************
5352 * tgammaf (MSVCR120.@)
5354 float CDECL
tgammaf(float x
)
5356 return unix_funcs
->tgammaf( x
);
5359 /*********************************************************************
5362 double CDECL
nan(const char *tagp
)
5364 /* Windows ignores input (MSDN) */
5368 /*********************************************************************
5371 float CDECL
nanf(const char *tagp
)
5376 /*********************************************************************
5377 * _except1 (MSVCR120.@)
5379 * - find meaning of ignored cw and operation bits
5382 double CDECL
_except1(DWORD fpe
, _FP_OPERATION_CODE op
, double arg
, double res
, DWORD cw
, void *unk
)
5384 ULONG_PTR exception_arg
;
5385 DWORD exception
= 0;
5390 TRACE("(%x %x %lf %lf %x %p)\n", fpe
, op
, arg
, res
, cw
, unk
);
5393 cw
= ((cw
>> 7) & 0x3f) | ((cw
>> 3) & 0xc00);
5395 operation
= op
<< 5;
5396 exception_arg
= (ULONG_PTR
)&operation
;
5398 if (fpe
& 0x1) { /* overflow */
5399 if ((fpe
== 0x1 && (cw
& 0x8)) || (fpe
==0x11 && (cw
& 0x28))) {
5400 /* 32-bit version also sets SW_INEXACT here */
5401 raise
|= FE_OVERFLOW
;
5402 if (fpe
& 0x10) raise
|= FE_INEXACT
;
5403 res
= signbit(res
) ? -INFINITY
: INFINITY
;
5405 exception
= EXCEPTION_FLT_OVERFLOW
;
5407 } else if (fpe
& 0x2) { /* underflow */
5408 if ((fpe
== 0x2 && (cw
& 0x10)) || (fpe
==0x12 && (cw
& 0x30))) {
5409 raise
|= FE_UNDERFLOW
;
5410 if (fpe
& 0x10) raise
|= FE_INEXACT
;
5411 res
= signbit(res
) ? -0.0 : 0.0;
5413 exception
= EXCEPTION_FLT_UNDERFLOW
;
5415 } else if (fpe
& 0x4) { /* zerodivide */
5416 if ((fpe
== 0x4 && (cw
& 0x4)) || (fpe
==0x14 && (cw
& 0x24))) {
5417 raise
|= FE_DIVBYZERO
;
5418 if (fpe
& 0x10) raise
|= FE_INEXACT
;
5420 exception
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
5422 } else if (fpe
& 0x8) { /* invalid */
5423 if (fpe
== 0x8 && (cw
& 0x1)) {
5424 raise
|= FE_INVALID
;
5426 exception
= EXCEPTION_FLT_INVALID_OPERATION
;
5428 } else if (fpe
& 0x10) { /* inexact */
5429 if (fpe
== 0x10 && (cw
& 0x20)) {
5430 raise
|= FE_INEXACT
;
5432 exception
= EXCEPTION_FLT_INEXACT_RESULT
;
5438 feraiseexcept(raise
);
5440 RaiseException(exception
, 0, 1, &exception_arg
);
5442 if (cw
& 0x1) fpword
|= _EM_INVALID
;
5443 if (cw
& 0x2) fpword
|= _EM_DENORMAL
;
5444 if (cw
& 0x4) fpword
|= _EM_ZERODIVIDE
;
5445 if (cw
& 0x8) fpword
|= _EM_OVERFLOW
;
5446 if (cw
& 0x10) fpword
|= _EM_UNDERFLOW
;
5447 if (cw
& 0x20) fpword
|= _EM_INEXACT
;
5450 case 0xc00: fpword
|= _RC_UP
|_RC_DOWN
; break;
5451 case 0x800: fpword
|= _RC_UP
; break;
5452 case 0x400: fpword
|= _RC_DOWN
; break;
5456 case 0x0: fpword
|= _PC_24
; break;
5457 case 0x200: fpword
|= _PC_53
; break;
5458 case 0x300: fpword
|= _PC_64
; break;
5460 if (cw
& 0x1000) fpword
|= _IC_AFFINE
;
5461 _control87(fpword
, 0xffffffff);
5466 _Dcomplex
* CDECL
_Cbuild(_Dcomplex
*ret
, double r
, double i
)
5473 double CDECL
MSVCR120_creal(_Dcomplex z
)
5478 /*********************************************************************
5479 * ilogb (MSVCR120.@)
5481 * Copied from musl: src/math/ilogb.c
5483 int CDECL
ilogb(double x
)
5485 union { double f
; UINT64 i
; } u
= { x
};
5486 int e
= u
.i
>> 52 & 0x7ff;
5491 if (u
.i
== 0) return FP_ILOGB0
;
5493 for (e
= -0x3ff; u
.i
>> 63 == 0; e
--, u
.i
<<= 1);
5496 if (e
== 0x7ff) return u
.i
<< 12 ? FP_ILOGBNAN
: INT_MAX
;
5500 /*********************************************************************
5501 * ilogbf (MSVCR120.@)
5503 * Copied from musl: src/math/ilogbf.c
5505 int CDECL
ilogbf(float x
)
5507 union { float f
; UINT32 i
; } u
= { x
};
5508 int e
= u
.i
>> 23 & 0xff;
5513 if (u
.i
== 0) return FP_ILOGB0
;
5515 for (e
= -0x7f; u
.i
>> 31 == 0; e
--, u
.i
<<= 1);
5518 if (e
== 0xff) return u
.i
<< 9 ? FP_ILOGBNAN
: INT_MAX
;
5521 #endif /* _MSVCR_VER>=120 */