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
)
91 static inline double fp_barrier(double x
)
93 volatile double y
= x
;
98 static inline double CDECL
ret_nan( BOOL update_sw
)
101 if (!update_sw
) return -NAN
;
102 return (x
- x
) / (x
- x
);
105 #define SET_X87_CW(MASK) \
106 "subl $4, %esp\n\t" \
107 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
108 "fnstcw (%esp)\n\t" \
109 "movw (%esp), %ax\n\t" \
110 "movw %ax, 2(%esp)\n\t" \
111 "testw $" #MASK ", %ax\n\t" \
113 "andw $~" #MASK ", %ax\n\t" \
114 "movw %ax, 2(%esp)\n\t" \
115 "fldcw 2(%esp)\n\t" \
118 #define RESET_X87_CW \
119 "movw (%esp), %ax\n\t" \
120 "cmpw %ax, 2(%esp)\n\t" \
122 "fstpl 8(%esp)\n\t" \
127 "addl $4, %esp\n\t" \
128 __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
130 /*********************************************************************
131 * _matherr (CRTDLL.@)
133 int CDECL
_matherr(struct _exception
*e
)
139 static double math_error(int type
, const char *name
, double arg1
, double arg2
, double retval
)
141 struct _exception exception
= {type
, (char *)name
, arg1
, arg2
, retval
};
143 TRACE("(%d, %s, %g, %g, %g)\n", type
, debugstr_a(name
), arg1
, arg2
, retval
);
145 if (MSVCRT_default_matherr_func
&& MSVCRT_default_matherr_func(&exception
))
146 return exception
.retval
;
151 /* don't set errno */
161 /* don't set errno */
164 ERR("Unhandled math error!\n");
167 return exception
.retval
;
170 /*********************************************************************
171 * __setusermatherr (MSVCRT.@)
173 void CDECL
__setusermatherr(MSVCRT_matherr_func func
)
175 MSVCRT_default_matherr_func
= func
;
176 TRACE("new matherr handler %p\n", func
);
179 /*********************************************************************
180 * _set_SSE2_enable (MSVCRT.@)
182 int CDECL
_set_SSE2_enable(int flag
)
184 sse2_enabled
= flag
&& sse2_supported
;
190 /*********************************************************************
191 * _get_FMA3_enable (UCRTBASE.@)
193 int CDECL
_get_FMA3_enable(void)
201 /*********************************************************************
202 * _set_FMA3_enable (MSVCR120.@)
204 int CDECL
_set_FMA3_enable(int flag
)
206 FIXME("(%x) stub\n", flag
);
212 #if !defined(__i386__) || _MSVCR_VER>=120
214 /*********************************************************************
215 * _chgsignf (MSVCRT.@)
217 float CDECL
_chgsignf( float num
)
219 union { float f
; UINT32 i
; } u
= { num
};
224 /*********************************************************************
225 * _copysignf (MSVCRT.@)
227 * Copied from musl: src/math/copysignf.c
229 float CDECL
_copysignf( float x
, float y
)
231 union { float f
; UINT32 i
; } ux
= { x
}, uy
= { y
};
233 ux
.i
|= uy
.i
& 0x80000000;
237 /*********************************************************************
238 * _nextafterf (MSVCRT.@)
240 float CDECL
_nextafterf( float num
, float next
)
242 if (!isfinite(num
) || !isfinite(next
)) *_errno() = EDOM
;
243 return unix_funcs
->nextafterf( num
, next
);
246 /*********************************************************************
249 float CDECL
_logbf( float num
)
251 float ret
= unix_funcs
->logbf(num
);
252 if (isnan(num
)) return math_error(_DOMAIN
, "_logbf", num
, 0, ret
);
253 if (!num
) return math_error(_SING
, "_logbf", num
, 0, ret
);
261 /*********************************************************************
262 * _fpclassf (MSVCRT.@)
264 int CDECL
_fpclassf( float num
)
266 union { float f
; UINT32 i
; } u
= { num
};
267 int e
= u
.i
>> 23 & 0xff;
273 if (u
.i
<< 1) return s
? _FPCLASS_ND
: _FPCLASS_PD
;
274 return s
? _FPCLASS_NZ
: _FPCLASS_PZ
;
276 if (u
.i
<< 9) return ((u
.i
>> 22) & 1) ? _FPCLASS_QNAN
: _FPCLASS_SNAN
;
277 return s
? _FPCLASS_NINF
: _FPCLASS_PINF
;
279 return s
? _FPCLASS_NN
: _FPCLASS_PN
;
283 /*********************************************************************
284 * _finitef (MSVCRT.@)
286 int CDECL
_finitef( float num
)
288 union { float f
; UINT32 i
; } u
= { num
};
289 return (u
.i
& 0x7fffffff) < 0x7f800000;
292 /*********************************************************************
295 int CDECL
_isnanf( float num
)
297 union { float f
; UINT32 i
; } u
= { num
};
298 return (u
.i
& 0x7fffffff) > 0x7f800000;
301 static float asinf_R(float z
)
303 /* coefficients for R(x^2) */
304 static const float p1
= 1.66666672e-01,
305 p2
= -5.11644611e-02,
306 p3
= -1.21124933e-02,
307 p4
= -3.58742251e-03,
308 q1
= -7.56982703e-01;
311 p
= z
* (p1
+ z
* (p2
+ z
* (p3
+ z
* p4
)));
316 /*********************************************************************
319 * Copied from musl: src/math/acosf.c
321 float CDECL
acosf( float x
)
323 static const double pio2_lo
= 6.12323399573676603587e-17;
325 float z
, w
, s
, c
, df
;
328 hx
= *(unsigned int*)&x
;
329 ix
= hx
& 0x7fffffff;
330 /* |x| >= 1 or nan */
331 if (ix
>= 0x3f800000) {
332 if (ix
== 0x3f800000) {
337 if (isnan(x
)) return x
;
338 return math_error(_DOMAIN
, "acosf", x
, 0, 0 / (x
- x
));
341 if (ix
< 0x3f000000) {
342 if (ix
<= 0x32800000) /* |x| < 2**-26 */
344 return M_PI_2
- (x
- (pio2_lo
- x
* asinf_R(x
* x
)));
350 return M_PI
- 2 * (s
+ ((double)s
* asinf_R(z
)));
355 hx
= *(unsigned int*)&s
& 0xffff0000;
357 c
= (z
- df
* df
) / (s
+ df
);
358 w
= asinf_R(z
) * s
+ c
;
362 /*********************************************************************
365 * Copied from musl: src/math/asinf.c
367 float CDECL
asinf( float x
)
369 static const double pio2
= 1.570796326794896558e+00;
370 static const float pio4_hi
= 0.785398125648;
371 static const float pio2_lo
= 7.54978941586e-08;
376 hx
= *(unsigned int*)&x
;
377 ix
= hx
& 0x7fffffff;
378 if (ix
>= 0x3f800000) { /* |x| >= 1 */
379 if (ix
== 0x3f800000) /* |x| == 1 */
380 return x
* pio2
+ 7.5231638453e-37; /* asin(+-1) = +-pi/2 with inexact */
381 if (isnan(x
)) return x
;
382 return math_error(_DOMAIN
, "asinf", x
, 0, 0 / (x
- x
));
384 if (ix
< 0x3f000000) { /* |x| < 0.5 */
385 /* if 0x1p-126 <= |x| < 0x1p-12, avoid raising underflow */
386 if (ix
< 0x39800000 && ix
>= 0x00800000)
388 return x
+ x
* asinf_R(x
* x
);
391 z
= (1 - fabsf(x
)) * 0.5f
;
394 *(unsigned int*)&f
= *(unsigned int*)&s
& 0xffff0000;
395 c
= (z
- f
* f
) / (s
+ f
);
396 x
= pio4_hi
- (2 * s
* asinf_R(z
) - (pio2_lo
- 2 * c
) - (pio4_hi
- 2 * f
));
402 /*********************************************************************
405 * Copied from musl: src/math/atanf.c
407 float CDECL
atanf( float x
)
409 static const float atanhi
[] = {
415 static const float atanlo
[] = {
421 static const float aT
[] = {
430 unsigned int ix
, sign
;
434 if (isnan(x
)) return math_error(_DOMAIN
, "atanf", x
, 0, x
);
437 ix
= *(unsigned int*)&x
;
440 if (ix
>= 0x4c800000) { /* if |x| >= 2**26 */
443 z
= atanhi
[3] + 7.5231638453e-37;
444 return sign
? -z
: z
;
446 if (ix
< 0x3ee00000) { /* |x| < 0.4375 */
447 if (ix
< 0x39800000) { /* |x| < 2**-12 */
449 /* raise underflow for subnormal x */
456 if (ix
< 0x3f980000) { /* |x| < 1.1875 */
457 if (ix
< 0x3f300000) { /* 7/16 <= |x| < 11/16 */
459 x
= (2.0f
* x
- 1.0f
) / (2.0f
+ x
);
460 } else { /* 11/16 <= |x| < 19/16 */
462 x
= (x
- 1.0f
) / (x
+ 1.0f
);
465 if (ix
< 0x401c0000) { /* |x| < 2.4375 */
467 x
= (x
- 1.5f
) / (1.0f
+ 1.5f
* x
);
468 } else { /* 2.4375 <= |x| < 2**26 */
474 /* end of argument reduction */
477 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
478 s1
= z
* (aT
[0] + w
* (aT
[2] + w
* aT
[4]));
479 s2
= w
* (aT
[1] + w
* aT
[3]);
481 return x
- x
* (s1
+ s2
);
482 z
= atanhi
[id
] - ((x
* (s1
+ s2
) - atanlo
[id
]) - x
);
483 return sign
? -z
: z
;
486 /*********************************************************************
489 * Copied from musl: src/math/atan2f.c
491 float CDECL
atan2f( float y
, float x
)
493 static const float pi
= 3.1415927410e+00,
494 pi_lo
= -8.7422776573e-08;
497 unsigned int m
, ix
, iy
;
499 if (isnan(x
) || isnan(y
))
501 ix
= *(unsigned int*)&x
;
502 iy
= *(unsigned int*)&y
;
503 if (ix
== 0x3f800000) /* x=1.0 */
505 m
= ((iy
>> 31) & 1) | ((ix
>> 30) & 2); /* 2*sign(x)+sign(y) */
513 case 1: return y
; /* atan(+-0,+anything)=+-0 */
514 case 2: return pi
; /* atan(+0,-anything) = pi */
515 case 3: return -pi
; /* atan(-0,-anything) =-pi */
520 return m
& 1 ? -pi
/ 2 : pi
/ 2;
522 if (ix
== 0x7f800000) {
523 if (iy
== 0x7f800000) {
525 case 0: return pi
/ 4; /* atan(+INF,+INF) */
526 case 1: return -pi
/ 4; /* atan(-INF,+INF) */
527 case 2: return 3 * pi
/ 4; /*atan(+INF,-INF)*/
528 case 3: return -3 * pi
/ 4; /*atan(-INF,-INF)*/
532 case 0: return 0.0f
; /* atan(+...,+INF) */
533 case 1: return -0.0f
; /* atan(-...,+INF) */
534 case 2: return pi
; /* atan(+...,-INF) */
535 case 3: return -pi
; /* atan(-...,-INF) */
540 if (ix
+ (26 << 23) < iy
|| iy
== 0x7f800000)
541 return m
& 1 ? -pi
/ 2 : pi
/ 2;
543 /* z = atan(|y/x|) with correct underflow */
544 if ((m
& 2) && iy
+ (26 << 23) < ix
) /*|y/x| < 0x1p-26, x < 0 */
547 z
= atanf(fabsf(y
/ x
));
549 case 0: return z
; /* atan(+,+) */
550 case 1: return -z
; /* atan(-,+) */
551 case 2: return pi
- (z
- pi_lo
); /* atan(+,-) */
552 default: /* case 3 */
553 return (z
- pi_lo
) - pi
; /* atan(-,-) */
557 /*********************************************************************
560 float CDECL
cosf( float x
)
562 float ret
= unix_funcs
->cosf( x
);
563 if (!isfinite(x
)) return math_error(_DOMAIN
, "cosf", x
, 0, ret
);
567 /*********************************************************************
570 float CDECL
coshf( float x
)
572 float ret
= unix_funcs
->coshf( x
);
573 if (isnan(x
)) return math_error(_DOMAIN
, "coshf", x
, 0, ret
);
577 /*********************************************************************
580 float CDECL
expf( float x
)
582 float ret
= unix_funcs
->expf( x
);
583 if (isnan(x
)) return math_error(_DOMAIN
, "expf", x
, 0, ret
);
584 if (isfinite(x
) && !ret
) return math_error(_UNDERFLOW
, "expf", x
, 0, ret
);
585 if (isfinite(x
) && !isfinite(ret
)) return math_error(_OVERFLOW
, "expf", x
, 0, ret
);
589 /*********************************************************************
592 float CDECL
fmodf( float x
, float y
)
594 float ret
= unix_funcs
->fmodf( x
, y
);
595 if (!isfinite(x
) || !isfinite(y
)) return math_error(_DOMAIN
, "fmodf", x
, 0, ret
);
599 /*********************************************************************
602 float CDECL
logf( float x
)
604 float ret
= unix_funcs
->logf( x
);
605 if (x
< 0.0) return math_error(_DOMAIN
, "logf", x
, 0, ret
);
606 if (x
== 0.0) return math_error(_SING
, "logf", x
, 0, ret
);
610 /*********************************************************************
613 float CDECL
log10f( float x
)
615 float ret
= unix_funcs
->log10f( x
);
616 if (x
< 0.0) return math_error(_DOMAIN
, "log10f", x
, 0, ret
);
617 if (x
== 0.0) return math_error(_SING
, "log10f", x
, 0, ret
);
621 /*********************************************************************
624 float CDECL
powf( float x
, float y
)
626 float z
= unix_funcs
->powf(x
,y
);
627 if (x
< 0 && y
!= floorf(y
)) return math_error(_DOMAIN
, "powf", x
, y
, z
);
628 if (!x
&& isfinite(y
) && y
< 0) return math_error(_SING
, "powf", x
, y
, z
);
629 if (isfinite(x
) && isfinite(y
) && !isfinite(z
)) return math_error(_OVERFLOW
, "powf", x
, y
, z
);
630 if (x
&& isfinite(x
) && isfinite(y
) && !z
) return math_error(_UNDERFLOW
, "powf", x
, y
, z
);
634 /*********************************************************************
637 float CDECL
sinf( float x
)
639 float ret
= unix_funcs
->sinf( x
);
640 if (!isfinite(x
)) return math_error(_DOMAIN
, "sinf", x
, 0, ret
);
644 /*********************************************************************
647 float CDECL
sinhf( float x
)
649 float ret
= unix_funcs
->sinhf( x
);
650 if (isnan(x
)) return math_error(_DOMAIN
, "sinhf", x
, 0, ret
);
654 static BOOL
sqrtf_validate( float *x
)
656 short c
= _fdclass(*x
);
658 if (c
== FP_ZERO
) return FALSE
;
659 if (c
== FP_NAN
) return FALSE
;
662 *x
= math_error(_DOMAIN
, "sqrtf", *x
, 0, ret_nan(TRUE
));
665 if (c
== FP_INFINITE
) return FALSE
;
669 #if defined(__x86_64__) || defined(__i386__)
670 float CDECL
sse2_sqrtf(float);
671 __ASM_GLOBAL_FUNC( sse2_sqrtf
,
672 "sqrtss %xmm0, %xmm0\n\t"
676 /*********************************************************************
679 * Copied from musl: src/math/sqrtf.c
681 float CDECL
sqrtf( float x
)
684 if (!sqrtf_validate(&x
))
687 return sse2_sqrtf(x
);
689 static const float tiny
= 1.0e-30;
697 if (!sqrtf_validate(&x
))
702 if (m
== 0) { /* subnormal x */
703 for (i
= 0; (ix
& 0x00800000) == 0; i
++)
707 m
-= 127; /* unbias exponent */
708 ix
= (ix
& 0x007fffff) | 0x00800000;
709 if (m
& 1) /* odd m, double x to make it even */
711 m
>>= 1; /* m = [m/2] */
713 /* generate sqrt(x) bit by bit */
715 q
= s
= 0; /* q = sqrt(x) */
716 r
= 0x01000000; /* r = moving bit from right to left */
729 /* use floating add to find out rounding direction */
731 z
= 1.0f
- tiny
; /* raise inexact flag */
740 ix
= (q
>> 1) + 0x3f000000;
741 r
= ix
+ ((unsigned int)m
<< 23);
747 /*********************************************************************
750 float CDECL
tanf( float x
)
752 float ret
= unix_funcs
->tanf(x
);
753 if (!isfinite(x
)) return math_error(_DOMAIN
, "tanf", x
, 0, ret
);
757 /*********************************************************************
760 float CDECL
tanhf( float x
)
762 float ret
= unix_funcs
->tanhf(x
);
763 if (!isfinite(x
)) return math_error(_DOMAIN
, "tanhf", x
, 0, ret
);
767 /*********************************************************************
770 float CDECL
ceilf( float x
)
772 return unix_funcs
->ceilf(x
);
775 /*********************************************************************
778 float CDECL
floorf( float x
)
780 return unix_funcs
->floorf(x
);
783 /*********************************************************************
786 float CDECL
frexpf( float x
, int *exp
)
788 return unix_funcs
->frexpf( x
, exp
);
791 /*********************************************************************
794 float CDECL
modff( float x
, float *iptr
)
796 return unix_funcs
->modff( x
, iptr
);
801 #if !defined(__i386__) && !defined(__x86_64__) && (_MSVCR_VER == 0 || _MSVCR_VER >= 110)
803 /*********************************************************************
806 * Copied from musl: src/math/fabsf.c
808 float CDECL
fabsf( float x
)
810 union { float f
; UINT32 i
; } u
= { x
};
817 /*********************************************************************
820 * Copied from musl: src/math/acos.c
822 static double acos_R(double z
)
824 static const double pS0
= 1.66666666666666657415e-01,
825 pS1
= -3.25565818622400915405e-01,
826 pS2
= 2.01212532134862925881e-01,
827 pS3
= -4.00555345006794114027e-02,
828 pS4
= 7.91534994289814532176e-04,
829 pS5
= 3.47933107596021167570e-05,
830 qS1
= -2.40339491173441421878e+00,
831 qS2
= 2.02094576023350569471e+00,
832 qS3
= -6.88283971605453293030e-01,
833 qS4
= 7.70381505559019352791e-02;
836 p
= z
* (pS0
+ z
* (pS1
+ z
* (pS2
+ z
* (pS3
+ z
* (pS4
+ z
* pS5
)))));
837 q
= 1.0 + z
* (qS1
+ z
* (qS2
+ z
* (qS3
+ z
* qS4
)));
841 double CDECL
acos( double x
)
843 static const double pio2_hi
= 1.57079632679489655800e+00,
844 pio2_lo
= 6.12323399573676603587e-17;
846 double z
, w
, s
, c
, df
;
850 hx
= *(ULONGLONG
*)&x
>> 32;
851 ix
= hx
& 0x7fffffff;
852 /* |x| >= 1 or nan */
853 if (ix
>= 0x3ff00000) {
856 lx
= *(ULONGLONG
*)&x
;
857 if (((ix
- 0x3ff00000) | lx
) == 0) {
858 /* acos(1)=0, acos(-1)=pi */
860 return 2 * pio2_hi
+ 7.5231638452626401e-37;
863 if (isnan(x
)) return x
;
864 return math_error(_DOMAIN
, "acos", x
, 0, 0 / (x
- x
));
867 if (ix
< 0x3fe00000) {
868 if (ix
<= 0x3c600000) /* |x| < 2**-57 */
869 return pio2_hi
+ 7.5231638452626401e-37;
870 return pio2_hi
- (x
- (pio2_lo
- x
* acos_R(x
* x
)));
876 w
= acos_R(z
) * s
- pio2_lo
;
877 return 2 * (pio2_hi
- (s
+ w
));
883 llx
= (*(ULONGLONG
*)&df
>> 32) << 32;
885 c
= (z
- df
* df
) / (s
+ df
);
886 w
= acos_R(z
) * s
+ c
;
890 /*********************************************************************
893 * Copied from musl: src/math/asin.c
895 static double asin_R(double z
)
897 /* coefficients for R(x^2) */
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
)));
916 double CDECL
x87_asin(double);
917 __ASM_GLOBAL_FUNC( x87_asin
,
932 double CDECL
asin( double x
)
934 static const double pio2_hi
= 1.57079632679489655800e+00,
935 pio2_lo
= 6.12323399573676603587e-17;
941 unsigned int x87_cw
, sse2_cw
;
944 hx
= *(ULONGLONG
*)&x
>> 32;
945 ix
= hx
& 0x7fffffff;
946 /* |x| >= 1 or nan */
947 if (ix
>= 0x3ff00000) {
949 lx
= *(ULONGLONG
*)&x
;
950 if (((ix
- 0x3ff00000) | lx
) == 0)
951 /* asin(1) = +-pi/2 with inexact */
952 return x
* pio2_hi
+ 7.5231638452626401e-37;
956 return math_error(_DOMAIN
, "asin", x
, 0, x
);
961 return math_error(_DOMAIN
, "asin", x
, 0, 0 / (x
- x
));
965 __control87_2(0, 0, &x87_cw
, &sse2_cw
);
966 if (!sse2_enabled
|| (x87_cw
& _MCW_EM
) != _MCW_EM
967 || (sse2_cw
& (_MCW_EM
| _MCW_RC
)) != _MCW_EM
)
972 if (ix
< 0x3fe00000) {
973 /* if 0x1p-1022 <= |x| < 0x1p-26, avoid raising underflow */
974 if (ix
< 0x3e500000 && ix
>= 0x00100000)
976 return x
+ x
* asin_R(x
* x
);
979 z
= (1 - fabs(x
)) * 0.5;
982 if (ix
>= 0x3fef3333) { /* if |x| > 0.975 */
983 x
= pio2_hi
- (2 * (s
+ s
* r
) - pio2_lo
);
988 llx
= (*(ULONGLONG
*)&f
>> 32) << 32;
990 c
= (z
- f
* f
) / (s
+ f
);
991 x
= 0.5 * pio2_hi
- (2 * s
* r
- (pio2_lo
- 2 * c
) - (0.5 * pio2_hi
- 2 * f
));
998 /*********************************************************************
1001 * Copied from musl: src/math/atan.c
1003 double CDECL
atan( double x
)
1005 static const double atanhi
[] = {
1006 4.63647609000806093515e-01,
1007 7.85398163397448278999e-01,
1008 9.82793723247329054082e-01,
1009 1.57079632679489655800e+00,
1011 static const double atanlo
[] = {
1012 2.26987774529616870924e-17,
1013 3.06161699786838301793e-17,
1014 1.39033110312309984516e-17,
1015 6.12323399573676603587e-17,
1017 static const double aT
[] = {
1018 3.33333333333329318027e-01,
1019 -1.99999999998764832476e-01,
1020 1.42857142725034663711e-01,
1021 -1.11111104054623557880e-01,
1022 9.09088713343650656196e-02,
1023 -7.69187620504482999495e-02,
1024 6.66107313738753120669e-02,
1025 -5.83357013379057348645e-02,
1026 4.97687799461593236017e-02,
1027 -3.65315727442169155270e-02,
1028 1.62858201153657823623e-02,
1031 double w
, s1
, s2
, z
;
1032 unsigned int ix
, sign
;
1036 if (isnan(x
)) return math_error(_DOMAIN
, "atan", x
, 0, x
);
1039 ix
= *(ULONGLONG
*)&x
>> 32;
1042 if (ix
>= 0x44100000) { /* if |x| >= 2^66 */
1045 z
= atanhi
[3] + 7.5231638452626401e-37;
1046 return sign
? -z
: z
;
1048 if (ix
< 0x3fdc0000) { /* |x| < 0.4375 */
1049 if (ix
< 0x3e400000) { /* |x| < 2^-27 */
1050 if (ix
< 0x00100000)
1051 /* raise underflow for subnormal x */
1052 fp_barrierf((float)x
);
1058 if (ix
< 0x3ff30000) { /* |x| < 1.1875 */
1059 if (ix
< 0x3fe60000) { /* 7/16 <= |x| < 11/16 */
1061 x
= (2.0 * x
- 1.0) / (2.0 + x
);
1062 } else { /* 11/16 <= |x| < 19/16 */
1064 x
= (x
- 1.0) / (x
+ 1.0);
1067 if (ix
< 0x40038000) { /* |x| < 2.4375 */
1069 x
= (x
- 1.5) / (1.0 + 1.5 * x
);
1070 } else { /* 2.4375 <= |x| < 2^66 */
1076 /* end of argument reduction */
1079 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
1080 s1
= z
* (aT
[0] + w
* (aT
[2] + w
* (aT
[4] + w
* (aT
[6] + w
* (aT
[8] + w
* aT
[10])))));
1081 s2
= w
* (aT
[1] + w
* (aT
[3] + w
* (aT
[5] + w
* (aT
[7] + w
* aT
[9]))));
1083 return x
- x
* (s1
+ s2
);
1084 z
= atanhi
[id
] - (x
* (s1
+ s2
) - atanlo
[id
] - x
);
1085 return sign
? -z
: z
;
1088 /*********************************************************************
1091 * Copied from musl: src/math/atan2.c
1093 double CDECL
atan2( double y
, double x
)
1095 static const double pi
= 3.1415926535897931160E+00,
1096 pi_lo
= 1.2246467991473531772E-16;
1099 unsigned int m
, lx
, ly
, ix
, iy
;
1101 if (isnan(x
) || isnan(y
))
1103 ix
= *(ULONGLONG
*)&x
>> 32;
1104 lx
= *(ULONGLONG
*)&x
;
1105 iy
= *(ULONGLONG
*)&y
>> 32;
1106 ly
= *(ULONGLONG
*)&y
;
1107 if (((ix
- 0x3ff00000) | lx
) == 0) /* x = 1.0 */
1109 m
= ((iy
>> 31) & 1) | ((ix
>> 30) & 2); /* 2*sign(x)+sign(y) */
1110 ix
= ix
& 0x7fffffff;
1111 iy
= iy
& 0x7fffffff;
1114 if ((iy
| ly
) == 0) {
1117 case 1: return y
; /* atan(+-0,+anything)=+-0 */
1118 case 2: return pi
; /* atan(+0,-anything) = pi */
1119 case 3: return -pi
; /* atan(-0,-anything) =-pi */
1124 return m
& 1 ? -pi
/ 2 : pi
/ 2;
1126 if (ix
== 0x7ff00000) {
1127 if (iy
== 0x7ff00000) {
1129 case 0: return pi
/ 4; /* atan(+INF,+INF) */
1130 case 1: return -pi
/ 4; /* atan(-INF,+INF) */
1131 case 2: return 3 * pi
/ 4; /* atan(+INF,-INF) */
1132 case 3: return -3 * pi
/ 4; /* atan(-INF,-INF) */
1136 case 0: return 0.0; /* atan(+...,+INF) */
1137 case 1: return -0.0; /* atan(-...,+INF) */
1138 case 2: return pi
; /* atan(+...,-INF) */
1139 case 3: return -pi
; /* atan(-...,-INF) */
1143 /* |y/x| > 0x1p64 */
1144 if (ix
+ (64 << 20) < iy
|| iy
== 0x7ff00000)
1145 return m
& 1 ? -pi
/ 2 : pi
/ 2;
1147 /* z = atan(|y/x|) without spurious underflow */
1148 if ((m
& 2) && iy
+ (64 << 20) < ix
) /* |y/x| < 0x1p-64, x<0 */
1151 z
= atan(fabs(y
/ x
));
1153 case 0: return z
; /* atan(+,+) */
1154 case 1: return -z
; /* atan(-,+) */
1155 case 2: return pi
- (z
- pi_lo
); /* atan(+,-) */
1156 default: /* case 3 */
1157 return (z
- pi_lo
) - pi
; /* atan(-,-) */
1161 /*********************************************************************
1164 double CDECL
cos( double x
)
1166 double ret
= unix_funcs
->cos( x
);
1167 if (!isfinite(x
)) return math_error(_DOMAIN
, "cos", x
, 0, ret
);
1171 /*********************************************************************
1174 double CDECL
cosh( double x
)
1176 double ret
= unix_funcs
->cosh( x
);
1177 if (isnan(x
)) return math_error(_DOMAIN
, "cosh", x
, 0, ret
);
1181 /*********************************************************************
1184 double CDECL
exp( double x
)
1186 double ret
= unix_funcs
->exp( x
);
1187 if (isnan(x
)) return math_error(_DOMAIN
, "exp", x
, 0, ret
);
1188 if (isfinite(x
) && !ret
) return math_error(_UNDERFLOW
, "exp", x
, 0, ret
);
1189 if (isfinite(x
) && !isfinite(ret
)) return math_error(_OVERFLOW
, "exp", x
, 0, ret
);
1193 /*********************************************************************
1196 double CDECL
fmod( double x
, double y
)
1198 double ret
= unix_funcs
->fmod( x
, y
);
1199 if (!isfinite(x
) || !isfinite(y
)) return math_error(_DOMAIN
, "fmod", x
, y
, ret
);
1203 /*********************************************************************
1206 double CDECL
log( double x
)
1208 double ret
= unix_funcs
->log( x
);
1209 if (x
< 0.0) return math_error(_DOMAIN
, "log", x
, 0, ret
);
1210 if (x
== 0.0) return math_error(_SING
, "log", x
, 0, ret
);
1214 /*********************************************************************
1217 double CDECL
log10( double x
)
1219 double ret
= unix_funcs
->log10( x
);
1220 if (x
< 0.0) return math_error(_DOMAIN
, "log10", x
, 0, ret
);
1221 if (x
== 0.0) return math_error(_SING
, "log10", x
, 0, ret
);
1225 /*********************************************************************
1228 double CDECL
pow( double x
, double y
)
1230 double z
= unix_funcs
->pow(x
,y
);
1231 if (x
< 0 && y
!= floor(y
))
1232 return math_error(_DOMAIN
, "pow", x
, y
, z
);
1233 if (!x
&& isfinite(y
) && y
< 0)
1234 return math_error(_SING
, "pow", x
, y
, z
);
1235 if (isfinite(x
) && isfinite(y
) && !isfinite(z
))
1236 return math_error(_OVERFLOW
, "pow", x
, y
, z
);
1237 if (x
&& isfinite(x
) && isfinite(y
) && !z
)
1238 return math_error(_UNDERFLOW
, "pow", x
, y
, z
);
1242 /*********************************************************************
1245 double CDECL
sin( double x
)
1247 double ret
= unix_funcs
->sin( x
);
1248 if (!isfinite(x
)) return math_error(_DOMAIN
, "sin", x
, 0, ret
);
1252 /*********************************************************************
1255 double CDECL
sinh( double x
)
1257 double ret
= unix_funcs
->sinh( x
);
1258 if (isnan(x
)) return math_error(_DOMAIN
, "sinh", x
, 0, ret
);
1262 static BOOL
sqrt_validate( double *x
, BOOL update_sw
)
1264 short c
= _dclass(*x
);
1266 if (c
== FP_ZERO
) return FALSE
;
1271 *x
= math_error(_DOMAIN
, "sqrt", *x
, 0, *x
);
1273 /* set signaling bit */
1274 *(ULONGLONG
*)x
|= 0x8000000000000ULL
;
1280 *x
= math_error(_DOMAIN
, "sqrt", *x
, 0, ret_nan(update_sw
));
1283 if (c
== FP_INFINITE
) return FALSE
;
1287 #if defined(__x86_64__) || defined(__i386__)
1288 double CDECL
sse2_sqrt(double);
1289 __ASM_GLOBAL_FUNC( sse2_sqrt
,
1290 "sqrtsd %xmm0, %xmm0\n\t"
1295 double CDECL
x87_sqrt(double);
1296 __ASM_GLOBAL_FUNC( x87_sqrt
,
1304 /*********************************************************************
1307 * Copied from musl: src/math/sqrt.c
1309 double CDECL
sqrt( double x
)
1312 if (!sqrt_validate(&x
, TRUE
))
1315 return sse2_sqrt(x
);
1316 #elif defined( __i386__ )
1317 if (!sqrt_validate(&x
, TRUE
))
1322 static const double tiny
= 1.0e-300;
1325 int sign
= 0x80000000;
1327 unsigned int r
,t1
,s1
,ix1
,q1
;
1330 if (!sqrt_validate(&x
, TRUE
))
1333 ix
= *(ULONGLONG
*)&x
;
1339 if (m
== 0) { /* subnormal x */
1345 for (i
=0; (ix0
& 0x00100000) == 0; i
++)
1348 ix0
|= ix1
>> (32 - i
);
1351 m
-= 1023; /* unbias exponent */
1352 ix0
= (ix0
& 0x000fffff) | 0x00100000;
1353 if (m
& 1) { /* odd m, double x to make it even */
1354 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1357 m
>>= 1; /* m = [m/2] */
1359 /* generate sqrt(x) bit by bit */
1360 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1362 q
= q1
= s0
= s1
= 0; /* [q,q1] = sqrt(x) */
1363 r
= 0x00200000; /* r = moving bit from right to left */
1372 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1381 if (t
< ix0
|| (t
== ix0
&& t1
<= ix1
)) {
1383 if ((t1
&sign
) == sign
&& (s1
& sign
) == 0)
1391 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1396 /* use floating add to find out rounding direction */
1397 if ((ix0
| ix1
) != 0) {
1398 z
= 1.0 - tiny
; /* raise inexact flag */
1401 if (q1
== (unsigned int)0xffffffff) {
1404 } else if (z
> 1.0) {
1405 if (q1
== (unsigned int)0xfffffffe)
1412 ix0
= (q
>> 1) + 0x3fe00000;
1416 ix
= ix0
+ ((unsigned int)m
<< 20);
1419 return *(double*)&ix
;
1423 /*********************************************************************
1426 double CDECL
tan( double x
)
1428 double ret
= unix_funcs
->tan(x
);
1429 if (!isfinite(x
)) return math_error(_DOMAIN
, "tan", x
, 0, ret
);
1433 /*********************************************************************
1436 double CDECL
tanh( double x
)
1438 double ret
= unix_funcs
->tanh(x
);
1439 if (isnan(x
)) return math_error(_DOMAIN
, "tanh", x
, 0, ret
);
1444 #if (defined(__GNUC__) || defined(__clang__)) && defined(__i386__)
1446 #define CREATE_FPU_FUNC1(name, call) \
1447 __ASM_GLOBAL_FUNC(name, \
1449 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1450 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1451 "movl %esp, %ebp\n\t" \
1452 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1453 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1454 "fstpl (%esp)\n\t" /* store function argument */ \
1456 "movl $1, %ecx\n\t" /* empty FPU stack */ \
1460 "and $0x4500, %ax\n\t" \
1461 "cmp $0x4100, %ax\n\t" \
1463 "fstpl (%esp,%ecx,8)\n\t" \
1468 "movl %ecx, -4(%ebp)\n\t" \
1469 "call " __ASM_NAME( #call ) "\n\t" \
1470 "movl -4(%ebp), %ecx\n\t" \
1471 "fstpl (%esp)\n\t" /* save result */ \
1472 "3:\n\t" /* restore FPU stack */ \
1474 "fldl (%esp,%ecx,8)\n\t" \
1475 "cmpl $0, %ecx\n\t" \
1478 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1479 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1482 #define CREATE_FPU_FUNC2(name, call) \
1483 __ASM_GLOBAL_FUNC(name, \
1485 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1486 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1487 "movl %esp, %ebp\n\t" \
1488 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1489 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1490 "fstpl 8(%esp)\n\t" /* store function argument */ \
1492 "fstpl (%esp)\n\t" \
1494 "movl $2, %ecx\n\t" /* empty FPU stack */ \
1498 "and $0x4500, %ax\n\t" \
1499 "cmp $0x4100, %ax\n\t" \
1501 "fstpl (%esp,%ecx,8)\n\t" \
1506 "movl %ecx, -4(%ebp)\n\t" \
1507 "call " __ASM_NAME( #call ) "\n\t" \
1508 "movl -4(%ebp), %ecx\n\t" \
1509 "fstpl 8(%esp)\n\t" /* save result */ \
1510 "3:\n\t" /* restore FPU stack */ \
1512 "fldl (%esp,%ecx,8)\n\t" \
1513 "cmpl $1, %ecx\n\t" \
1516 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1517 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1520 CREATE_FPU_FUNC1(_CIacos
, acos
)
1521 CREATE_FPU_FUNC1(_CIasin
, asin
)
1522 CREATE_FPU_FUNC1(_CIatan
, atan
)
1523 CREATE_FPU_FUNC2(_CIatan2
, atan2
)
1524 CREATE_FPU_FUNC1(_CIcos
, cos
)
1525 CREATE_FPU_FUNC1(_CIcosh
, cosh
)
1526 CREATE_FPU_FUNC1(_CIexp
, exp
)
1527 CREATE_FPU_FUNC2(_CIfmod
, fmod
)
1528 CREATE_FPU_FUNC1(_CIlog
, log
)
1529 CREATE_FPU_FUNC1(_CIlog10
, log10
)
1530 CREATE_FPU_FUNC2(_CIpow
, pow
)
1531 CREATE_FPU_FUNC1(_CIsin
, sin
)
1532 CREATE_FPU_FUNC1(_CIsinh
, sinh
)
1533 CREATE_FPU_FUNC1(_CIsqrt
, sqrt
)
1534 CREATE_FPU_FUNC1(_CItan
, tan
)
1535 CREATE_FPU_FUNC1(_CItanh
, tanh
)
1537 __ASM_GLOBAL_FUNC(_ftol
,
1539 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1540 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1541 "movl %esp, %ebp\n\t"
1542 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1543 "subl $12, %esp\n\t" /* sizeof(LONGLONG) + 2*sizeof(WORD) */
1545 "mov (%esp), %ax\n\t"
1546 "or $0xc00, %ax\n\t"
1547 "mov %ax, 2(%esp)\n\t"
1549 "fistpq 4(%esp)\n\t"
1551 "movl 4(%esp), %eax\n\t"
1552 "movl 8(%esp), %edx\n\t"
1554 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1555 __ASM_CFI(".cfi_same_value %ebp\n\t")
1558 #endif /* (defined(__GNUC__) || defined(__clang__)) && defined(__i386__) */
1560 /*********************************************************************
1561 * _fpclass (MSVCRT.@)
1563 int CDECL
_fpclass(double num
)
1565 union { double f
; UINT64 i
; } u
= { num
};
1566 int e
= u
.i
>> 52 & 0x7ff;
1572 if (u
.i
<< 1) return s
? _FPCLASS_ND
: _FPCLASS_PD
;
1573 return s
? _FPCLASS_NZ
: _FPCLASS_PZ
;
1575 if (u
.i
<< 12) return ((u
.i
>> 51) & 1) ? _FPCLASS_QNAN
: _FPCLASS_SNAN
;
1576 return s
? _FPCLASS_NINF
: _FPCLASS_PINF
;
1578 return s
? _FPCLASS_NN
: _FPCLASS_PN
;
1582 /*********************************************************************
1585 unsigned int CDECL
MSVCRT__rotl(unsigned int num
, int shift
)
1588 return (num
<< shift
) | (num
>> (32-shift
));
1591 /*********************************************************************
1594 __msvcrt_ulong CDECL
MSVCRT__lrotl(__msvcrt_ulong num
, int shift
)
1597 return (num
<< shift
) | (num
>> (32-shift
));
1600 /*********************************************************************
1603 __msvcrt_ulong CDECL
MSVCRT__lrotr(__msvcrt_ulong num
, int shift
)
1606 return (num
>> shift
) | (num
<< (32-shift
));
1609 /*********************************************************************
1612 unsigned int CDECL
MSVCRT__rotr(unsigned int num
, int shift
)
1615 return (num
>> shift
) | (num
<< (32-shift
));
1618 /*********************************************************************
1619 * _rotl64 (MSVCRT.@)
1621 unsigned __int64 CDECL
MSVCRT__rotl64(unsigned __int64 num
, int shift
)
1624 return (num
<< shift
) | (num
>> (64-shift
));
1627 /*********************************************************************
1628 * _rotr64 (MSVCRT.@)
1630 unsigned __int64 CDECL
MSVCRT__rotr64(unsigned __int64 num
, int shift
)
1633 return (num
>> shift
) | (num
<< (64-shift
));
1636 /*********************************************************************
1639 int CDECL
abs( int n
)
1641 return n
>= 0 ? n
: -n
;
1644 /*********************************************************************
1647 __msvcrt_long CDECL
labs( __msvcrt_long n
)
1649 return n
>= 0 ? n
: -n
;
1653 /*********************************************************************
1654 * llabs (MSVCR100.@)
1656 __int64 CDECL
llabs( __int64 n
)
1658 return n
>= 0 ? n
: -n
;
1663 /*********************************************************************
1664 * imaxabs (MSVCR120.@)
1666 intmax_t CDECL
imaxabs( intmax_t n
)
1668 return n
>= 0 ? n
: -n
;
1672 /*********************************************************************
1675 __int64 CDECL
_abs64( __int64 n
)
1677 return n
>= 0 ? n
: -n
;
1680 /*********************************************************************
1683 double CDECL
_logb(double num
)
1685 double ret
= unix_funcs
->logb(num
);
1686 if (isnan(num
)) return math_error(_DOMAIN
, "_logb", num
, 0, ret
);
1687 if (!num
) return math_error(_SING
, "_logb", num
, 0, ret
);
1691 /*********************************************************************
1694 double CDECL
_hypot(double x
, double y
)
1696 /* FIXME: errno handling */
1697 return unix_funcs
->hypot( x
, y
);
1700 /*********************************************************************
1701 * _hypotf (MSVCRT.@)
1703 float CDECL
_hypotf(float x
, float y
)
1705 /* FIXME: errno handling */
1706 return unix_funcs
->hypotf( x
, y
);
1709 /*********************************************************************
1712 double CDECL
ceil( double x
)
1714 return unix_funcs
->ceil(x
);
1717 /*********************************************************************
1720 double CDECL
floor( double x
)
1722 return unix_funcs
->floor(x
);
1725 /*********************************************************************
1728 double CDECL
fma( double x
, double y
, double z
)
1730 double w
= unix_funcs
->fma(x
, y
, z
);
1731 if ((isinf(x
) && y
== 0) || (x
== 0 && isinf(y
))) *_errno() = EDOM
;
1732 else if (isinf(x
) && isinf(z
) && x
!= z
) *_errno() = EDOM
;
1733 else if (isinf(y
) && isinf(z
) && y
!= z
) *_errno() = EDOM
;
1737 /*********************************************************************
1740 float CDECL
fmaf( float x
, float y
, float z
)
1742 float w
= unix_funcs
->fmaf(x
, y
, z
);
1743 if ((isinf(x
) && y
== 0) || (x
== 0 && isinf(y
))) *_errno() = EDOM
;
1744 else if (isinf(x
) && isinf(z
) && x
!= z
) *_errno() = EDOM
;
1745 else if (isinf(y
) && isinf(z
) && y
!= z
) *_errno() = EDOM
;
1749 /*********************************************************************
1752 * Copied from musl: src/math/fabsf.c
1754 double CDECL
fabs( double x
)
1756 union { double f
; UINT64 i
; } u
= { x
};
1761 /*********************************************************************
1764 double CDECL
frexp( double x
, int *exp
)
1766 return unix_funcs
->frexp( x
, exp
);
1769 /*********************************************************************
1772 double CDECL
modf( double x
, double *iptr
)
1774 return unix_funcs
->modf( x
, iptr
);
1777 /**********************************************************************
1778 * _statusfp2 (MSVCRT.@)
1780 * Not exported by native msvcrt, added in msvcr80.
1782 #if defined(__i386__) || defined(__x86_64__)
1783 void CDECL
_statusfp2( unsigned int *x86_sw
, unsigned int *sse2_sw
)
1785 #if defined(__GNUC__) || defined(__clang__)
1787 unsigned long fpword
;
1791 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) );
1793 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1794 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1795 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1796 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1797 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1798 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1802 if (!sse2_sw
) return;
1806 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1808 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1809 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1810 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1811 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1812 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1813 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1818 FIXME( "not implemented\n" );
1823 /**********************************************************************
1824 * _statusfp (MSVCRT.@)
1826 unsigned int CDECL
_statusfp(void)
1828 unsigned int flags
= 0;
1829 #if defined(__i386__) || defined(__x86_64__)
1830 unsigned int x86_sw
, sse2_sw
;
1832 _statusfp2( &x86_sw
, &sse2_sw
);
1833 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
1834 flags
= x86_sw
| sse2_sw
;
1835 #elif defined(__aarch64__)
1838 __asm__
__volatile__( "mrs %0, fpsr" : "=r" (fpsr
) );
1839 if (fpsr
& 0x1) flags
|= _SW_INVALID
;
1840 if (fpsr
& 0x2) flags
|= _SW_ZERODIVIDE
;
1841 if (fpsr
& 0x4) flags
|= _SW_OVERFLOW
;
1842 if (fpsr
& 0x8) flags
|= _SW_UNDERFLOW
;
1843 if (fpsr
& 0x10) flags
|= _SW_INEXACT
;
1844 if (fpsr
& 0x80) flags
|= _SW_DENORMAL
;
1846 FIXME( "not implemented\n" );
1851 /*********************************************************************
1852 * _clearfp (MSVCRT.@)
1854 unsigned int CDECL
_clearfp(void)
1856 unsigned int flags
= 0;
1857 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
1858 unsigned long fpword
;
1860 __asm__
__volatile__( "fnstsw %0; fnclex" : "=m" (fpword
) );
1861 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1862 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1863 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1864 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1865 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1866 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1870 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1871 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1872 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1873 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1874 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1875 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1876 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1878 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1880 #elif defined(__aarch64__)
1883 __asm__
__volatile__( "mrs %0, fpsr" : "=r" (fpsr
) );
1884 if (fpsr
& 0x1) flags
|= _SW_INVALID
;
1885 if (fpsr
& 0x2) flags
|= _SW_ZERODIVIDE
;
1886 if (fpsr
& 0x4) flags
|= _SW_OVERFLOW
;
1887 if (fpsr
& 0x8) flags
|= _SW_UNDERFLOW
;
1888 if (fpsr
& 0x10) flags
|= _SW_INEXACT
;
1889 if (fpsr
& 0x80) flags
|= _SW_DENORMAL
;
1891 __asm__
__volatile__( "msr fpsr, %0" :: "r" (fpsr
) );
1893 FIXME( "not implemented\n" );
1898 /*********************************************************************
1899 * __fpecode (MSVCRT.@)
1901 int * CDECL
__fpecode(void)
1903 return &msvcrt_get_thread_data()->fpecode
;
1906 /*********************************************************************
1909 double CDECL
ldexp(double num
, int exp
)
1911 double z
= unix_funcs
->ldexp(num
,exp
);
1913 if (isfinite(num
) && !isfinite(z
))
1914 return math_error(_OVERFLOW
, "ldexp", num
, exp
, z
);
1915 if (num
&& isfinite(num
) && !z
)
1916 return math_error(_UNDERFLOW
, "ldexp", num
, exp
, z
);
1917 if (z
== 0 && signbit(z
))
1918 z
= 0.0; /* Convert -0 -> +0 */
1922 /*********************************************************************
1925 double CDECL
_cabs(struct _complex num
)
1927 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
1930 /*********************************************************************
1931 * _chgsign (MSVCRT.@)
1933 double CDECL
_chgsign(double num
)
1935 union { double f
; UINT64 i
; } u
= { num
};
1940 /*********************************************************************
1941 * __control87_2 (MSVCR80.@)
1943 * Not exported by native msvcrt, added in msvcr80.
1946 int CDECL
__control87_2( unsigned int newval
, unsigned int mask
,
1947 unsigned int *x86_cw
, unsigned int *sse2_cw
)
1949 #if defined(__GNUC__) || defined(__clang__)
1950 unsigned long fpword
;
1952 unsigned int old_flags
;
1956 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
1958 /* Convert into mask constants */
1960 if (fpword
& 0x1) flags
|= _EM_INVALID
;
1961 if (fpword
& 0x2) flags
|= _EM_DENORMAL
;
1962 if (fpword
& 0x4) flags
|= _EM_ZERODIVIDE
;
1963 if (fpword
& 0x8) flags
|= _EM_OVERFLOW
;
1964 if (fpword
& 0x10) flags
|= _EM_UNDERFLOW
;
1965 if (fpword
& 0x20) flags
|= _EM_INEXACT
;
1966 switch (fpword
& 0xc00)
1968 case 0xc00: flags
|= _RC_UP
|_RC_DOWN
; break;
1969 case 0x800: flags
|= _RC_UP
; break;
1970 case 0x400: flags
|= _RC_DOWN
; break;
1972 switch (fpword
& 0x300)
1974 case 0x0: flags
|= _PC_24
; break;
1975 case 0x200: flags
|= _PC_53
; break;
1976 case 0x300: flags
|= _PC_64
; break;
1978 if (fpword
& 0x1000) flags
|= _IC_AFFINE
;
1980 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1983 flags
= (flags
& ~mask
) | (newval
& mask
);
1985 /* Convert (masked) value back to fp word */
1987 if (flags
& _EM_INVALID
) fpword
|= 0x1;
1988 if (flags
& _EM_DENORMAL
) fpword
|= 0x2;
1989 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x4;
1990 if (flags
& _EM_OVERFLOW
) fpword
|= 0x8;
1991 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x10;
1992 if (flags
& _EM_INEXACT
) fpword
|= 0x20;
1993 switch (flags
& _MCW_RC
)
1995 case _RC_UP
|_RC_DOWN
: fpword
|= 0xc00; break;
1996 case _RC_UP
: fpword
|= 0x800; break;
1997 case _RC_DOWN
: fpword
|= 0x400; break;
1999 switch (flags
& _MCW_PC
)
2001 case _PC_64
: fpword
|= 0x300; break;
2002 case _PC_53
: fpword
|= 0x200; break;
2003 case _PC_24
: fpword
|= 0x0; break;
2005 if (flags
& _IC_AFFINE
) fpword
|= 0x1000;
2007 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
2012 if (!sse2_cw
) return 1;
2016 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2018 /* Convert into mask constants */
2020 if (fpword
& 0x80) flags
|= _EM_INVALID
;
2021 if (fpword
& 0x100) flags
|= _EM_DENORMAL
;
2022 if (fpword
& 0x200) flags
|= _EM_ZERODIVIDE
;
2023 if (fpword
& 0x400) flags
|= _EM_OVERFLOW
;
2024 if (fpword
& 0x800) flags
|= _EM_UNDERFLOW
;
2025 if (fpword
& 0x1000) flags
|= _EM_INEXACT
;
2026 switch (fpword
& 0x6000)
2028 case 0x6000: flags
|= _RC_UP
|_RC_DOWN
; break;
2029 case 0x4000: flags
|= _RC_UP
; break;
2030 case 0x2000: flags
|= _RC_DOWN
; break;
2032 switch (fpword
& 0x8040)
2034 case 0x0040: flags
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
2035 case 0x8000: flags
|= _DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
2036 case 0x8040: flags
|= _DN_FLUSH
; break;
2039 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
2043 mask
&= _MCW_EM
| _MCW_RC
| _MCW_DN
;
2044 flags
= (flags
& ~mask
) | (newval
& mask
);
2046 if (flags
!= old_flags
)
2048 /* Convert (masked) value back to fp word */
2050 if (flags
& _EM_INVALID
) fpword
|= 0x80;
2051 if (flags
& _EM_DENORMAL
) fpword
|= 0x100;
2052 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2053 if (flags
& _EM_OVERFLOW
) fpword
|= 0x400;
2054 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x800;
2055 if (flags
& _EM_INEXACT
) fpword
|= 0x1000;
2056 switch (flags
& _MCW_RC
)
2058 case _RC_UP
|_RC_DOWN
: fpword
|= 0x6000; break;
2059 case _RC_UP
: fpword
|= 0x4000; break;
2060 case _RC_DOWN
: fpword
|= 0x2000; break;
2062 switch (flags
& _MCW_DN
)
2064 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
2065 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
2066 case _DN_FLUSH
: fpword
|= 0x8040; break;
2068 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
2077 FIXME( "not implemented\n" );
2083 /*********************************************************************
2084 * _control87 (MSVCRT.@)
2086 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
2088 unsigned int flags
= 0;
2090 unsigned int sse2_cw
;
2092 __control87_2( newval
, mask
, &flags
, &sse2_cw
);
2094 if ((flags
^ sse2_cw
) & (_MCW_EM
| _MCW_RC
)) flags
|= _EM_AMBIGUOUS
;
2096 #elif defined(__x86_64__)
2097 unsigned long fpword
;
2098 unsigned int old_flags
;
2100 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2101 if (fpword
& 0x80) flags
|= _EM_INVALID
;
2102 if (fpword
& 0x100) flags
|= _EM_DENORMAL
;
2103 if (fpword
& 0x200) flags
|= _EM_ZERODIVIDE
;
2104 if (fpword
& 0x400) flags
|= _EM_OVERFLOW
;
2105 if (fpword
& 0x800) flags
|= _EM_UNDERFLOW
;
2106 if (fpword
& 0x1000) flags
|= _EM_INEXACT
;
2107 switch (fpword
& 0x6000)
2109 case 0x6000: flags
|= _RC_CHOP
; break;
2110 case 0x4000: flags
|= _RC_UP
; break;
2111 case 0x2000: flags
|= _RC_DOWN
; break;
2113 switch (fpword
& 0x8040)
2115 case 0x0040: flags
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
2116 case 0x8000: flags
|= _DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
2117 case 0x8040: flags
|= _DN_FLUSH
; break;
2120 mask
&= _MCW_EM
| _MCW_RC
| _MCW_DN
;
2121 flags
= (flags
& ~mask
) | (newval
& mask
);
2122 if (flags
!= old_flags
)
2125 if (flags
& _EM_INVALID
) fpword
|= 0x80;
2126 if (flags
& _EM_DENORMAL
) fpword
|= 0x100;
2127 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2128 if (flags
& _EM_OVERFLOW
) fpword
|= 0x400;
2129 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x800;
2130 if (flags
& _EM_INEXACT
) fpword
|= 0x1000;
2131 switch (flags
& _MCW_RC
)
2133 case _RC_CHOP
: fpword
|= 0x6000; break;
2134 case _RC_UP
: fpword
|= 0x4000; break;
2135 case _RC_DOWN
: fpword
|= 0x2000; break;
2137 switch (flags
& _MCW_DN
)
2139 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
2140 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
2141 case _DN_FLUSH
: fpword
|= 0x8040; break;
2143 __asm__
__volatile__( "ldmxcsr %0" :: "m" (fpword
) );
2145 #elif defined(__aarch64__)
2148 __asm__
__volatile__( "mrs %0, fpcr" : "=r" (fpcr
) );
2149 if (!(fpcr
& 0x100)) flags
|= _EM_INVALID
;
2150 if (!(fpcr
& 0x200)) flags
|= _EM_ZERODIVIDE
;
2151 if (!(fpcr
& 0x400)) flags
|= _EM_OVERFLOW
;
2152 if (!(fpcr
& 0x800)) flags
|= _EM_UNDERFLOW
;
2153 if (!(fpcr
& 0x1000)) flags
|= _EM_INEXACT
;
2154 if (!(fpcr
& 0x8000)) flags
|= _EM_DENORMAL
;
2155 switch (fpcr
& 0xc00000)
2157 case 0x400000: flags
|= _RC_UP
; break;
2158 case 0x800000: flags
|= _RC_DOWN
; break;
2159 case 0xc00000: flags
|= _RC_CHOP
; break;
2161 flags
= (flags
& ~mask
) | (newval
& mask
);
2162 fpcr
&= ~0xc09f00ul
;
2163 if (!(flags
& _EM_INVALID
)) fpcr
|= 0x100;
2164 if (!(flags
& _EM_ZERODIVIDE
)) fpcr
|= 0x200;
2165 if (!(flags
& _EM_OVERFLOW
)) fpcr
|= 0x400;
2166 if (!(flags
& _EM_UNDERFLOW
)) fpcr
|= 0x800;
2167 if (!(flags
& _EM_INEXACT
)) fpcr
|= 0x1000;
2168 if (!(flags
& _EM_DENORMAL
)) fpcr
|= 0x8000;
2169 switch (flags
& _MCW_RC
)
2171 case _RC_CHOP
: fpcr
|= 0xc00000; break;
2172 case _RC_UP
: fpcr
|= 0x400000; break;
2173 case _RC_DOWN
: fpcr
|= 0x800000; break;
2175 __asm__
__volatile__( "msr fpcr, %0" :: "r" (fpcr
) );
2177 FIXME( "not implemented\n" );
2182 /*********************************************************************
2183 * _controlfp (MSVCRT.@)
2185 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
2187 return _control87( newval
, mask
& ~_EM_DENORMAL
);
2190 /*********************************************************************
2191 * _set_controlfp (MSVCRT.@)
2193 void CDECL
_set_controlfp( unsigned int newval
, unsigned int mask
)
2195 _controlfp( newval
, mask
);
2198 /*********************************************************************
2199 * _controlfp_s (MSVCRT.@)
2201 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
2203 static const unsigned int all_flags
= (_MCW_EM
| _MCW_IC
| _MCW_RC
|
2207 if (!MSVCRT_CHECK_PMT( !(newval
& mask
& ~all_flags
) ))
2209 if (cur
) *cur
= _controlfp( 0, 0 ); /* retrieve it anyway */
2212 val
= _controlfp( newval
, mask
);
2213 if (cur
) *cur
= val
;
2218 /*********************************************************************
2219 * fegetenv (MSVCR120.@)
2221 int CDECL
fegetenv(fenv_t
*env
)
2223 env
->_Fe_ctl
= _controlfp(0, 0) & (_EM_INEXACT
| _EM_UNDERFLOW
|
2224 _EM_OVERFLOW
| _EM_ZERODIVIDE
| _EM_INVALID
| _RC_CHOP
);
2225 env
->_Fe_stat
= _statusfp();
2229 /*********************************************************************
2230 * feupdateenv (MSVCR120.@)
2232 int CDECL
feupdateenv(const fenv_t
*env
)
2235 set
._Fe_stat
|= _statusfp();
2236 return fesetenv(&set
);
2239 /*********************************************************************
2240 * fetestexcept (MSVCR120.@)
2242 int CDECL
fetestexcept(int flags
)
2244 return _statusfp() & flags
;
2247 /*********************************************************************
2248 * fesetexceptflag (MSVCR120.@)
2250 int CDECL
fesetexceptflag(const fexcept_t
*status
, int excepts
)
2258 excepts
&= FE_ALL_EXCEPT
;
2259 env
._Fe_stat
&= ~excepts
;
2260 env
._Fe_stat
|= (*status
& excepts
);
2261 return fesetenv(&env
);
2264 /*********************************************************************
2265 * feraiseexcept (MSVCR120.@)
2267 int CDECL
feraiseexcept(int flags
)
2272 env
._Fe_stat
|= (flags
& FE_ALL_EXCEPT
);
2273 return fesetenv(&env
);
2276 /*********************************************************************
2277 * feclearexcept (MSVCR120.@)
2279 int CDECL
feclearexcept(int flags
)
2284 env
._Fe_stat
&= ~(flags
& FE_ALL_EXCEPT
);
2285 return fesetenv(&env
);
2288 /*********************************************************************
2289 * fegetexceptflag (MSVCR120.@)
2291 int CDECL
fegetexceptflag(fexcept_t
*status
, int excepts
)
2293 *status
= _statusfp() & excepts
;
2299 /*********************************************************************
2300 * __fpe_flt_rounds (UCRTBASE.@)
2302 int CDECL
__fpe_flt_rounds(void)
2304 unsigned int fpc
= _controlfp(0, 0) & _RC_CHOP
;
2309 case _RC_CHOP
: return 0;
2310 case _RC_NEAR
: return 1;
2311 case _RC_UP
: return 2;
2319 /*********************************************************************
2320 * fegetround (MSVCR120.@)
2322 int CDECL
fegetround(void)
2324 return _controlfp(0, 0) & _RC_CHOP
;
2327 /*********************************************************************
2328 * fesetround (MSVCR120.@)
2330 int CDECL
fesetround(int round_mode
)
2332 if (round_mode
& (~_RC_CHOP
))
2334 _controlfp(round_mode
, _RC_CHOP
);
2338 #endif /* _MSVCR_VER>=120 */
2340 /*********************************************************************
2341 * _copysign (MSVCRT.@)
2343 * Copied from musl: src/math/copysign.c
2345 double CDECL
_copysign( double x
, double y
)
2347 union { double f
; UINT64 i
; } ux
= { x
}, uy
= { y
};
2349 ux
.i
|= uy
.i
& 1ull << 63;
2353 /*********************************************************************
2354 * _finite (MSVCRT.@)
2356 int CDECL
_finite(double num
)
2358 union { double f
; UINT64 i
; } u
= { num
};
2359 return (u
.i
& ~0ull >> 1) < 0x7ffull
<< 52;
2362 /*********************************************************************
2363 * _fpreset (MSVCRT.@)
2365 void CDECL
_fpreset(void)
2367 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2368 const unsigned int x86_cw
= 0x27f;
2369 __asm__
__volatile__( "fninit; fldcw %0" : : "m" (x86_cw
) );
2372 const unsigned long sse2_cw
= 0x1f80;
2373 __asm__
__volatile__( "ldmxcsr %0" : : "m" (sse2_cw
) );
2376 FIXME( "not implemented\n" );
2381 /*********************************************************************
2382 * fesetenv (MSVCR120.@)
2384 int CDECL
fesetenv(const fenv_t
*env
)
2386 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2394 DWORD instruction_pointer
;
2402 TRACE( "(%p)\n", env
);
2404 if (!env
->_Fe_ctl
&& !env
->_Fe_stat
) {
2409 __asm__
__volatile__( "fnstenv %0" : "=m" (fenv
) );
2411 fenv
.control_word
&= ~0xc3d;
2412 if (env
->_Fe_ctl
& _EM_INVALID
) fenv
.control_word
|= 0x1;
2413 if (env
->_Fe_ctl
& _EM_ZERODIVIDE
) fenv
.control_word
|= 0x4;
2414 if (env
->_Fe_ctl
& _EM_OVERFLOW
) fenv
.control_word
|= 0x8;
2415 if (env
->_Fe_ctl
& _EM_UNDERFLOW
) fenv
.control_word
|= 0x10;
2416 if (env
->_Fe_ctl
& _EM_INEXACT
) fenv
.control_word
|= 0x20;
2417 switch (env
->_Fe_ctl
& _MCW_RC
)
2419 case _RC_UP
|_RC_DOWN
: fenv
.control_word
|= 0xc00; break;
2420 case _RC_UP
: fenv
.control_word
|= 0x800; break;
2421 case _RC_DOWN
: fenv
.control_word
|= 0x400; break;
2424 fenv
.status_word
&= ~0x3f;
2425 if (env
->_Fe_stat
& _SW_INVALID
) fenv
.status_word
|= 0x1;
2426 if (env
->_Fe_stat
& _SW_DENORMAL
) fenv
.status_word
|= 0x2;
2427 if (env
->_Fe_stat
& _SW_ZERODIVIDE
) fenv
.status_word
|= 0x4;
2428 if (env
->_Fe_stat
& _SW_OVERFLOW
) fenv
.status_word
|= 0x8;
2429 if (env
->_Fe_stat
& _SW_UNDERFLOW
) fenv
.status_word
|= 0x10;
2430 if (env
->_Fe_stat
& _SW_INEXACT
) fenv
.status_word
|= 0x20;
2432 __asm__
__volatile__( "fldenv %0" : : "m" (fenv
) : "st", "st(1)",
2433 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
2438 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2440 if (env
->_Fe_ctl
& _EM_INVALID
) fpword
|= 0x80;
2441 if (env
->_Fe_ctl
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2442 if (env
->_Fe_ctl
& _EM_OVERFLOW
) fpword
|= 0x400;
2443 if (env
->_Fe_ctl
& _EM_UNDERFLOW
) fpword
|= 0x800;
2444 if (env
->_Fe_ctl
& _EM_INEXACT
) fpword
|= 0x1000;
2445 switch (env
->_Fe_ctl
& _MCW_RC
)
2447 case _RC_CHOP
: fpword
|= 0x6000; break;
2448 case _RC_UP
: fpword
|= 0x4000; break;
2449 case _RC_DOWN
: fpword
|= 0x2000; break;
2451 if (env
->_Fe_stat
& _SW_INVALID
) fpword
|= 0x1;
2452 if (env
->_Fe_stat
& _SW_DENORMAL
) fpword
|= 0x2;
2453 if (env
->_Fe_stat
& _SW_ZERODIVIDE
) fpword
|= 0x4;
2454 if (env
->_Fe_stat
& _SW_OVERFLOW
) fpword
|= 0x8;
2455 if (env
->_Fe_stat
& _SW_UNDERFLOW
) fpword
|= 0x10;
2456 if (env
->_Fe_stat
& _SW_INEXACT
) fpword
|= 0x20;
2457 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
2462 FIXME( "not implemented\n" );
2468 /*********************************************************************
2471 int CDECL
_isnan(double num
)
2473 union { double f
; UINT64 i
; } u
= { num
};
2474 return (u
.i
& ~0ull >> 1) > 0x7ffull
<< 52;
2477 static double pzero(double x
)
2479 static const double pR8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
2480 0.00000000000000000000e+00,
2481 -7.03124999999900357484e-02,
2482 -8.08167041275349795626e+00,
2483 -2.57063105679704847262e+02,
2484 -2.48521641009428822144e+03,
2485 -5.25304380490729545272e+03,
2487 1.16534364619668181717e+02,
2488 3.83374475364121826715e+03,
2489 4.05978572648472545552e+04,
2490 1.16752972564375915681e+05,
2491 4.76277284146730962675e+04,
2492 }, pR5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
2493 -1.14125464691894502584e-11,
2494 -7.03124940873599280078e-02,
2495 -4.15961064470587782438e+00,
2496 -6.76747652265167261021e+01,
2497 -3.31231299649172967747e+02,
2498 -3.46433388365604912451e+02,
2500 6.07539382692300335975e+01,
2501 1.05125230595704579173e+03,
2502 5.97897094333855784498e+03,
2503 9.62544514357774460223e+03,
2504 2.40605815922939109441e+03,
2505 }, pR3
[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
2506 -2.54704601771951915620e-09,
2507 -7.03119616381481654654e-02,
2508 -2.40903221549529611423e+00,
2509 -2.19659774734883086467e+01,
2510 -5.80791704701737572236e+01,
2511 -3.14479470594888503854e+01,
2513 3.58560338055209726349e+01,
2514 3.61513983050303863820e+02,
2515 1.19360783792111533330e+03,
2516 1.12799679856907414432e+03,
2517 1.73580930813335754692e+02,
2518 }, pR2
[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
2519 -8.87534333032526411254e-08,
2520 -7.03030995483624743247e-02,
2521 -1.45073846780952986357e+00,
2522 -7.63569613823527770791e+00,
2523 -1.11931668860356747786e+01,
2524 -3.23364579351335335033e+00,
2526 2.22202997532088808441e+01,
2527 1.36206794218215208048e+02,
2528 2.70470278658083486789e+02,
2529 1.53875394208320329881e+02,
2530 1.46576176948256193810e+01,
2533 const double *p
, *q
;
2537 ix
= *(ULONGLONG
*)&x
>> 32;
2539 if (ix
>= 0x40200000) {
2542 } else if (ix
>= 0x40122E8B) {
2545 } else if (ix
>= 0x4006DB6D) {
2548 } else /*ix >= 0x40000000*/ {
2554 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
2555 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* q
[4]))));
2559 static double qzero(double x
)
2561 static const double qR8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
2562 0.00000000000000000000e+00,
2563 7.32421874999935051953e-02,
2564 1.17682064682252693899e+01,
2565 5.57673380256401856059e+02,
2566 8.85919720756468632317e+03,
2567 3.70146267776887834771e+04,
2569 1.63776026895689824414e+02,
2570 8.09834494656449805916e+03,
2571 1.42538291419120476348e+05,
2572 8.03309257119514397345e+05,
2573 8.40501579819060512818e+05,
2574 -3.43899293537866615225e+05,
2575 }, qR5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
2576 1.84085963594515531381e-11,
2577 7.32421766612684765896e-02,
2578 5.83563508962056953777e+00,
2579 1.35111577286449829671e+02,
2580 1.02724376596164097464e+03,
2581 1.98997785864605384631e+03,
2583 8.27766102236537761883e+01,
2584 2.07781416421392987104e+03,
2585 1.88472887785718085070e+04,
2586 5.67511122894947329769e+04,
2587 3.59767538425114471465e+04,
2588 -5.35434275601944773371e+03,
2589 }, qR3
[6] = {/* for x in [4.547,2.8571]=1/[0.2199,0.35001] */
2590 4.37741014089738620906e-09,
2591 7.32411180042911447163e-02,
2592 3.34423137516170720929e+00,
2593 4.26218440745412650017e+01,
2594 1.70808091340565596283e+02,
2595 1.66733948696651168575e+02,
2597 4.87588729724587182091e+01,
2598 7.09689221056606015736e+02,
2599 3.70414822620111362994e+03,
2600 6.46042516752568917582e+03,
2601 2.51633368920368957333e+03,
2602 -1.49247451836156386662e+02,
2603 }, qR2
[6] = {/* for x in [2.8570,2]=1/[0.3499,0.5] */
2604 1.50444444886983272379e-07,
2605 7.32234265963079278272e-02,
2606 1.99819174093815998816e+00,
2607 1.44956029347885735348e+01,
2608 3.16662317504781540833e+01,
2609 1.62527075710929267416e+01,
2611 3.03655848355219184498e+01,
2612 2.69348118608049844624e+02,
2613 8.44783757595320139444e+02,
2614 8.82935845112488550512e+02,
2615 2.12666388511798828631e+02,
2616 -5.31095493882666946917e+00,
2619 const double *p
, *q
;
2623 ix
= *(ULONGLONG
*)&x
>> 32;
2625 if (ix
>= 0x40200000) {
2628 } else if (ix
>= 0x40122E8B) {
2631 } else if (ix
>= 0x4006DB6D) {
2634 } else /*ix >= 0x40000000*/ {
2640 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
2641 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* (q
[4] + z
* q
[5])))));
2642 return (-0.125 + r
/ s
) / x
;
2645 /* j0 and y0 approximation for |x|>=2 */
2646 static double j0_y0_approx(unsigned int ix
, double x
, BOOL y0
)
2648 static const double invsqrtpi
= 5.64189583547756279280e-01;
2650 double s
, c
, ss
, cc
, z
;
2656 /* avoid overflow in 2*x, big ulp error when x>=0x1p1023 */
2657 if (ix
< 0x7fe00000) {
2660 if (s
* c
< 0) cc
= z
/ ss
;
2662 if (ix
< 0x48000000) {
2664 cc
= pzero(x
) * cc
- qzero(x
) * ss
;
2667 return invsqrtpi
* cc
/ sqrt(x
);
2670 /*********************************************************************
2673 * Copied from musl: src/math/j0.c
2675 double CDECL
_j0(double x
)
2677 static const double R02
= 1.56249999999999947958e-02,
2678 R03
= -1.89979294238854721751e-04,
2679 R04
= 1.82954049532700665670e-06,
2680 R05
= -4.61832688532103189199e-09,
2681 S01
= 1.56191029464890010492e-02,
2682 S02
= 1.16926784663337450260e-04,
2683 S03
= 5.13546550207318111446e-07,
2684 S04
= 1.16614003333790000205e-09;
2689 ix
= *(ULONGLONG
*)&x
>> 32;
2692 /* j0(+-inf)=0, j0(nan)=nan */
2693 if (ix
>= 0x7ff00000)
2694 return math_error(_DOMAIN
, "_j0", x
, 0, 1 / (x
* x
));
2697 if (ix
>= 0x40000000) { /* |x| >= 2 */
2698 /* large ulp error near zeros: 2.4, 5.52, 8.6537,.. */
2699 return j0_y0_approx(ix
, x
, FALSE
);
2702 if (ix
>= 0x3f200000) { /* |x| >= 2**-13 */
2703 /* up to 4ulp error close to 2 */
2705 r
= z
* (R02
+ z
* (R03
+ z
* (R04
+ z
* R05
)));
2706 s
= 1 + z
* (S01
+ z
* (S02
+ z
* (S03
+ z
* S04
)));
2707 return (1 + x
/ 2) * (1 - x
/ 2) + z
* (r
/ s
);
2711 /* prevent underflow */
2712 /* inexact should be raised when x!=0, this is not done correctly */
2713 if (ix
>= 0x38000000) /* |x| >= 2**-127 */
2718 static double pone(double x
)
2720 static const double pr8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
2721 0.00000000000000000000e+00,
2722 1.17187499999988647970e-01,
2723 1.32394806593073575129e+01,
2724 4.12051854307378562225e+02,
2725 3.87474538913960532227e+03,
2726 7.91447954031891731574e+03,
2728 1.14207370375678408436e+02,
2729 3.65093083420853463394e+03,
2730 3.69562060269033463555e+04,
2731 9.76027935934950801311e+04,
2732 3.08042720627888811578e+04,
2733 }, pr5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
2734 1.31990519556243522749e-11,
2735 1.17187493190614097638e-01,
2736 6.80275127868432871736e+00,
2737 1.08308182990189109773e+02,
2738 5.17636139533199752805e+02,
2739 5.28715201363337541807e+02,
2741 5.92805987221131331921e+01,
2742 9.91401418733614377743e+02,
2743 5.35326695291487976647e+03,
2744 7.84469031749551231769e+03,
2745 1.50404688810361062679e+03,
2747 3.02503916137373618024e-09,
2748 1.17186865567253592491e-01,
2749 3.93297750033315640650e+00,
2750 3.51194035591636932736e+01,
2751 9.10550110750781271918e+01,
2752 4.85590685197364919645e+01,
2754 3.47913095001251519989e+01,
2755 3.36762458747825746741e+02,
2756 1.04687139975775130551e+03,
2757 8.90811346398256432622e+02,
2758 1.03787932439639277504e+02,
2759 }, pr2
[6] = { /* for x in [2.8570,2]=1/[0.3499,0.5] */
2760 1.07710830106873743082e-07,
2761 1.17176219462683348094e-01,
2762 2.36851496667608785174e+00,
2763 1.22426109148261232917e+01,
2764 1.76939711271687727390e+01,
2765 5.07352312588818499250e+00,
2767 2.14364859363821409488e+01,
2768 1.25290227168402751090e+02,
2769 2.32276469057162813669e+02,
2770 1.17679373287147100768e+02,
2771 8.36463893371618283368e+00,
2774 const double *p
, *q
;
2778 ix
= *(ULONGLONG
*)&x
>> 32;
2780 if (ix
>= 0x40200000) {
2783 } else if (ix
>= 0x40122E8B) {
2786 } else if (ix
>= 0x4006DB6D) {
2789 } else /*ix >= 0x40000000*/ {
2794 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
2795 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* q
[4]))));
2799 static double qone(double x
)
2801 static const double qr8
[6] = { /* for x in [inf, 8]=1/[0,0.125] */
2802 0.00000000000000000000e+00,
2803 -1.02539062499992714161e-01,
2804 -1.62717534544589987888e+01,
2805 -7.59601722513950107896e+02,
2806 -1.18498066702429587167e+04,
2807 -4.84385124285750353010e+04,
2809 1.61395369700722909556e+02,
2810 7.82538599923348465381e+03,
2811 1.33875336287249578163e+05,
2812 7.19657723683240939863e+05,
2813 6.66601232617776375264e+05,
2814 -2.94490264303834643215e+05,
2815 }, qr5
[6] = { /* for x in [8,4.5454]=1/[0.125,0.22001] */
2816 -2.08979931141764104297e-11,
2817 -1.02539050241375426231e-01,
2818 -8.05644828123936029840e+00,
2819 -1.83669607474888380239e+02,
2820 -1.37319376065508163265e+03,
2821 -2.61244440453215656817e+03,
2823 8.12765501384335777857e+01,
2824 1.99179873460485964642e+03,
2825 1.74684851924908907677e+04,
2826 4.98514270910352279316e+04,
2827 2.79480751638918118260e+04,
2828 -4.71918354795128470869e+03,
2830 -5.07831226461766561369e-09,
2831 -1.02537829820837089745e-01,
2832 -4.61011581139473403113e+00,
2833 -5.78472216562783643212e+01,
2834 -2.28244540737631695038e+02,
2835 -2.19210128478909325622e+02,
2837 4.76651550323729509273e+01,
2838 6.73865112676699709482e+02,
2839 3.38015286679526343505e+03,
2840 5.54772909720722782367e+03,
2841 1.90311919338810798763e+03,
2842 -1.35201191444307340817e+02,
2843 }, qr2
[6] = { /* for x in [2.8570,2]=1/[0.3499,0.5] */
2844 -1.78381727510958865572e-07,
2845 -1.02517042607985553460e-01,
2846 -2.75220568278187460720e+00,
2847 -1.96636162643703720221e+01,
2848 -4.23253133372830490089e+01,
2849 -2.13719211703704061733e+01,
2851 2.95333629060523854548e+01,
2852 2.52981549982190529136e+02,
2853 7.57502834868645436472e+02,
2854 7.39393205320467245656e+02,
2855 1.55949003336666123687e+02,
2856 -4.95949898822628210127e+00,
2859 const double *p
, *q
;
2863 ix
= *(ULONGLONG
*)&x
>> 32;
2865 if (ix
>= 0x40200000) {
2868 } else if (ix
>= 0x40122E8B) {
2871 } else if (ix
>= 0x4006DB6D) {
2874 } else /*ix >= 0x40000000*/ {
2879 r
= p
[0] + z
* (p
[1] + z
* (p
[2] + z
* (p
[3] + z
* (p
[4] + z
* p
[5]))));
2880 s
= 1.0 + z
* (q
[0] + z
* (q
[1] + z
* (q
[2] + z
* (q
[3] + z
* (q
[4] + z
* q
[5])))));
2881 return (0.375 + r
/ s
) / x
;
2884 static double j1_y1_approx(unsigned int ix
, double x
, BOOL y1
, int sign
)
2886 static const double invsqrtpi
= 5.64189583547756279280e-01;
2888 double z
, s
, c
, ss
, cc
;
2894 if (ix
< 0x7fe00000) {
2897 if (s
* c
> 0) cc
= z
/ ss
;
2899 if (ix
< 0x48000000) {
2902 cc
= pone(x
) * cc
- qone(x
) * ss
;
2907 return invsqrtpi
* cc
/ sqrt(x
);
2910 /*********************************************************************
2913 * Copied from musl: src/math/j1.c
2915 double CDECL
_j1(double x
)
2917 static const double r00
= -6.25000000000000000000e-02,
2918 r01
= 1.40705666955189706048e-03,
2919 r02
= -1.59955631084035597520e-05,
2920 r03
= 4.96727999609584448412e-08,
2921 s01
= 1.91537599538363460805e-02,
2922 s02
= 1.85946785588630915560e-04,
2923 s03
= 1.17718464042623683263e-06,
2924 s04
= 5.04636257076217042715e-09,
2925 s05
= 1.23542274426137913908e-11;
2931 ix
= *(ULONGLONG
*)&x
>> 32;
2934 if (ix
>= 0x7ff00000)
2935 return math_error(isnan(x
) ? 0 : _DOMAIN
, "_j1", x
, 0, 1 / (x
* x
));
2936 if (ix
>= 0x40000000) /* |x| >= 2 */
2937 return j1_y1_approx(ix
, fabs(x
), FALSE
, sign
);
2938 if (ix
>= 0x38000000) { /* |x| >= 2**-127 */
2940 r
= z
* (r00
+ z
* (r01
+ z
* (r02
+ z
* r03
)));
2941 s
= 1 + z
* (s01
+ z
* (s02
+ z
* (s03
+ z
* (s04
+ z
* s05
))));
2944 /* avoid underflow, raise inexact if x!=0 */
2947 return (0.5 + z
) * x
;
2950 /*********************************************************************
2953 * Copied from musl: src/math/jn.c
2955 double CDECL
_jn(int n
, double x
)
2957 static const double invsqrtpi
= 5.64189583547756279280e-01;
2959 unsigned int ix
, lx
;
2963 ix
= *(ULONGLONG
*)&x
>> 32;
2964 lx
= *(ULONGLONG
*)&x
;
2968 if ((ix
| (lx
| -lx
) >> 31) > 0x7ff00000) /* nan */
2983 sign
&= n
; /* even n: 0, odd n: signbit(x) */
2985 if ((ix
| lx
) == 0 || ix
== 0x7ff00000) /* if x is 0 or inf */
2988 if (ix
>= 0x52d00000) { /* x > 2**302 */
2991 temp
= -cos(x
) + sin(x
);
2994 temp
= -cos(x
) - sin(x
);
2997 temp
= cos(x
) - sin(x
);
3000 temp
= cos(x
) + sin(x
);
3003 b
= invsqrtpi
* temp
/ sqrt(x
);
3007 for (i
= 0; i
< nm1
; ) {
3010 b
= b
* (2.0 * i
/ x
) - a
; /* avoid underflow */
3015 if (ix
< 0x3e100000) { /* x < 2**-29 */
3016 if (nm1
> 32) /* underflow */
3022 for (i
= 2; i
<= nm1
+ 1; i
++) {
3023 a
*= (double)i
; /* a = n! */
3024 b
*= temp
; /* b = (x/2)^n */
3029 double t
, q0
, q1
, w
, h
, z
, tmp
, nf
;
3039 while (q1
< 1.0e9
) {
3046 for (t
= 0.0, i
= k
; i
>= 0; i
--)
3047 t
= 1 / (2 * (i
+ nf
) / x
- t
);
3050 tmp
= nf
* log(fabs(w
));
3051 if (tmp
< 7.09782712893383973096e+02) {
3052 for (i
= nm1
; i
> 0; i
--) {
3054 b
= b
* (2.0 * i
) / x
- a
;
3058 for (i
= nm1
; i
> 0; i
--) {
3060 b
= b
* (2.0 * i
) / x
- a
;
3062 /* scale b to avoid spurious overflow */
3072 if (fabs(z
) >= fabs(w
))
3078 return sign
? -b
: b
;
3081 /*********************************************************************
3084 double CDECL
_y0(double x
)
3086 static const double tpi
= 6.36619772367581382433e-01,
3087 u00
= -7.38042951086872317523e-02,
3088 u01
= 1.76666452509181115538e-01,
3089 u02
= -1.38185671945596898896e-02,
3090 u03
= 3.47453432093683650238e-04,
3091 u04
= -3.81407053724364161125e-06,
3092 u05
= 1.95590137035022920206e-08,
3093 u06
= -3.98205194132103398453e-11,
3094 v01
= 1.27304834834123699328e-02,
3095 v02
= 7.60068627350353253702e-05,
3096 v03
= 2.59150851840457805467e-07,
3097 v04
= 4.41110311332675467403e-10;
3100 unsigned int ix
, lx
;
3102 ix
= *(ULONGLONG
*)&x
>> 32;
3103 lx
= *(ULONGLONG
*)&x
;
3105 /* y0(nan)=nan, y0(<0)=nan, y0(0)=-inf, y0(inf)=0 */
3106 if ((ix
<< 1 | lx
) == 0)
3107 return math_error(_OVERFLOW
, "_y0", x
, 0, -INFINITY
);
3111 return math_error(_DOMAIN
, "_y0", x
, 0, 0 / (x
- x
));
3112 if (ix
>= 0x7ff00000)
3115 if (ix
>= 0x40000000) { /* x >= 2 */
3116 /* large ulp errors near zeros: 3.958, 7.086,.. */
3117 return j0_y0_approx(ix
, x
, TRUE
);
3120 if (ix
>= 0x3e400000) { /* x >= 2**-27 */
3121 /* large ulp error near the first zero, x ~= 0.89 */
3123 u
= u00
+ z
* (u01
+ z
* (u02
+ z
* (u03
+ z
* (u04
+ z
* (u05
+ z
* u06
)))));
3124 v
= 1.0 + z
* (v01
+ z
* (v02
+ z
* (v03
+ z
* v04
)));
3125 return u
/ v
+ tpi
* (j0(x
) * log(x
));
3127 return u00
+ tpi
* log(x
);
3130 /*********************************************************************
3133 double CDECL
_y1(double x
)
3135 static const double tpi
= 6.36619772367581382433e-01,
3136 u00
= -1.96057090646238940668e-01,
3137 u01
= 5.04438716639811282616e-02,
3138 u02
= -1.91256895875763547298e-03,
3139 u03
= 2.35252600561610495928e-05,
3140 u04
= -9.19099158039878874504e-08,
3141 v00
= 1.99167318236649903973e-02,
3142 v01
= 2.02552581025135171496e-04,
3143 v02
= 1.35608801097516229404e-06,
3144 v03
= 6.22741452364621501295e-09,
3145 v04
= 1.66559246207992079114e-11;
3148 unsigned int ix
, lx
;
3150 ix
= *(ULONGLONG
*)&x
>> 32;
3151 lx
= *(ULONGLONG
*)&x
;
3153 /* y1(nan)=nan, y1(<0)=nan, y1(0)=-inf, y1(inf)=0 */
3154 if ((ix
<< 1 | lx
) == 0)
3155 return math_error(_OVERFLOW
, "_y1", x
, 0, -INFINITY
);
3159 return math_error(_DOMAIN
, "_y1", x
, 0, 0 / (x
- x
));
3160 if (ix
>= 0x7ff00000)
3163 if (ix
>= 0x40000000) /* x >= 2 */
3164 return j1_y1_approx(ix
, x
, TRUE
, 0);
3165 if (ix
< 0x3c900000) /* x < 2**-54 */
3168 u
= u00
+ z
* (u01
+ z
* (u02
+ z
* (u03
+ z
* u04
)));
3169 v
= 1 + z
* (v00
+ z
* (v01
+ z
* (v02
+ z
* (v03
+ z
* v04
))));
3170 return x
* (u
/ v
) + tpi
* (j1(x
) * log(x
) - 1 / x
);
3173 /*********************************************************************
3176 * Copied from musl: src/math/jn.c
3178 double CDECL
_yn(int n
, double x
)
3180 static const double invsqrtpi
= 5.64189583547756279280e-01;
3182 unsigned int ix
, lx
, ib
;
3186 ix
= *(ULONGLONG
*)&x
>> 32;
3187 lx
= *(ULONGLONG
*)&x
;
3191 if ((ix
| (lx
| -lx
) >> 31) > 0x7ff00000) /* nan */
3193 if (sign
&& (ix
| lx
) != 0) /* x < 0 */
3194 return math_error(_DOMAIN
, "_y1", x
, 0, 0 / (x
- x
));
3195 if (ix
== 0x7ff00000)
3208 return sign
? -y1(x
) : y1(x
);
3210 if (ix
>= 0x52d00000) { /* x > 2**302 */
3213 temp
= -sin(x
) - cos(x
);
3216 temp
= -sin(x
) + cos(x
);
3219 temp
= sin(x
) + cos(x
);
3222 temp
= sin(x
) - cos(x
);
3225 b
= invsqrtpi
* temp
/ sqrt(x
);
3229 /* quit if b is -inf */
3230 ib
= *(ULONGLONG
*)&b
>> 32;
3231 for (i
= 0; i
< nm1
&& ib
!= 0xfff00000;) {
3234 b
= (2.0 * i
/ x
) * b
- a
;
3235 ib
= *(ULONGLONG
*)&b
>> 32;
3239 return sign
? -b
: b
;
3244 /*********************************************************************
3245 * _nearbyint (MSVCR120.@)
3247 double CDECL
nearbyint(double num
)
3249 return unix_funcs
->nearbyint( num
);
3252 /*********************************************************************
3253 * _nearbyintf (MSVCR120.@)
3255 float CDECL
nearbyintf(float num
)
3257 return unix_funcs
->nearbyintf( num
);
3260 /*********************************************************************
3261 * nexttoward (MSVCR120.@)
3263 double CDECL
MSVCRT_nexttoward(double num
, double next
)
3265 double ret
= unix_funcs
->nexttoward(num
, next
);
3266 if (!(_fpclass(ret
) & (_FPCLASS_PN
| _FPCLASS_NN
3267 | _FPCLASS_SNAN
| _FPCLASS_QNAN
)) && !isinf(num
))
3274 /*********************************************************************
3275 * nexttowardf (MSVCR120.@)
3277 float CDECL
MSVCRT_nexttowardf(float num
, double next
)
3279 float ret
= unix_funcs
->nexttowardf( num
, next
);
3280 if (!(_fpclass(ret
) & (_FPCLASS_PN
| _FPCLASS_NN
3281 | _FPCLASS_SNAN
| _FPCLASS_QNAN
)) && !isinf(num
))
3288 #endif /* _MSVCR_VER>=120 */
3290 /*********************************************************************
3291 * _nextafter (MSVCRT.@)
3293 double CDECL
_nextafter(double num
, double next
)
3296 if (!isfinite(num
) || !isfinite(next
)) *_errno() = EDOM
;
3297 retval
= unix_funcs
->nextafter(num
,next
);
3301 /*********************************************************************
3304 char * CDECL
_ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
3307 thread_data_t
*data
= msvcrt_get_thread_data();
3308 /* FIXME: check better for overflow (native supports over 300 chars) */
3309 ndigits
= min( ndigits
, 80 - 8); /* 8 : space for sign, dec point, "e",
3310 * 4 for exponent and one for
3311 * terminating '\0' */
3312 if (!data
->efcvt_buffer
)
3313 data
->efcvt_buffer
= malloc( 80 ); /* ought to be enough */
3315 /* handle cases with zero ndigits or less */
3317 if( prec
< 1) prec
= 2;
3318 len
= _snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
3320 if (data
->efcvt_buffer
[0] == '-') {
3321 memmove( data
->efcvt_buffer
, data
->efcvt_buffer
+ 1, len
-- );
3325 /* take the decimal "point away */
3327 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
3328 /* take the exponential "e" out */
3329 data
->efcvt_buffer
[ prec
] = '\0';
3330 /* read the exponent */
3331 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
3333 /* adjust for some border cases */
3334 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
3336 /* handle cases with zero ndigits or less */
3338 if( data
->efcvt_buffer
[ 0] >= '5')
3340 data
->efcvt_buffer
[ 0] = '\0';
3342 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
3343 return data
->efcvt_buffer
;
3346 /*********************************************************************
3347 * _ecvt_s (MSVCRT.@)
3349 int CDECL
_ecvt_s( char *buffer
, size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
3354 if (!MSVCRT_CHECK_PMT(buffer
!= NULL
)) return EINVAL
;
3355 if (!MSVCRT_CHECK_PMT(decpt
!= NULL
)) return EINVAL
;
3356 if (!MSVCRT_CHECK_PMT(sign
!= NULL
)) return EINVAL
;
3357 if (!MSVCRT_CHECK_PMT_ERR( length
> 2, ERANGE
)) return ERANGE
;
3358 if (!MSVCRT_CHECK_PMT_ERR(ndigits
< (int)length
- 1, ERANGE
)) return ERANGE
;
3360 /* handle cases with zero ndigits or less */
3362 if( prec
< 1) prec
= 2;
3363 result
= malloc(prec
+ 8);
3365 len
= _snprintf(result
, prec
+ 8, "%.*le", prec
- 1, number
);
3366 if (result
[0] == '-') {
3367 memmove( result
, result
+ 1, len
-- );
3371 /* take the decimal "point away */
3373 memmove( result
+ 1, result
+ 2, len
- 1 );
3374 /* take the exponential "e" out */
3375 result
[ prec
] = '\0';
3376 /* read the exponent */
3377 sscanf( result
+ prec
+ 1, "%d", decpt
);
3379 /* adjust for some border cases */
3380 if( result
[0] == '0')/* value is zero */
3382 /* handle cases with zero ndigits or less */
3384 if( result
[ 0] >= '5')
3388 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
3393 /***********************************************************************
3396 char * CDECL
_fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
3398 thread_data_t
*data
= msvcrt_get_thread_data();
3399 int stop
, dec1
, dec2
;
3400 char *ptr1
, *ptr2
, *first
;
3401 char buf
[80]; /* ought to be enough */
3402 char decimal_separator
= get_locinfo()->lconv
->decimal_point
[0];
3404 if (!data
->efcvt_buffer
)
3405 data
->efcvt_buffer
= malloc( 80 ); /* ought to be enough */
3407 stop
= _snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
3409 ptr2
= data
->efcvt_buffer
;
3419 /* For numbers below the requested resolution, work out where
3420 the decimal point will be rather than finding it in the string */
3421 if (number
< 1.0 && number
> 0.0) {
3422 dec2
= log10(number
+ 1e-10);
3423 if (-dec2
<= ndigits
) dec2
= 0;
3426 /* If requested digits is zero or less, we will need to truncate
3427 * the returned string */
3432 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
3433 while (*ptr1
!= '\0' && *ptr1
!= decimal_separator
) {
3434 if (!first
) first
= ptr2
;
3435 if ((ptr1
- buf
) < stop
) {
3446 while (*ptr1
== '0') { /* Process leading zeroes */
3451 while (*ptr1
!= '\0') {
3452 if (!first
) first
= ptr2
;
3459 /* We never found a non-zero digit, then our number is either
3460 * smaller than the requested precision, or 0.0 */
3465 first
= data
->efcvt_buffer
;
3470 *decpt
= dec2
? dec2
: dec1
;
3474 /***********************************************************************
3475 * _fcvt_s (MSVCRT.@)
3477 int CDECL
_fcvt_s(char* outbuffer
, size_t size
, double number
, int ndigits
, int *decpt
, int *sign
)
3479 int stop
, dec1
, dec2
;
3480 char *ptr1
, *ptr2
, *first
;
3481 char buf
[80]; /* ought to be enough */
3482 char decimal_separator
= get_locinfo()->lconv
->decimal_point
[0];
3484 if (!outbuffer
|| !decpt
|| !sign
|| size
== 0)
3490 stop
= _snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
3502 /* For numbers below the requested resolution, work out where
3503 the decimal point will be rather than finding it in the string */
3504 if (number
< 1.0 && number
> 0.0) {
3505 dec2
= log10(number
+ 1e-10);
3506 if (-dec2
<= ndigits
) dec2
= 0;
3509 /* If requested digits is zero or less, we will need to truncate
3510 * the returned string */
3515 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
3516 while (*ptr1
!= '\0' && *ptr1
!= decimal_separator
) {
3517 if (!first
) first
= ptr2
;
3518 if ((ptr1
- buf
) < stop
) {
3532 while (*ptr1
== '0') { /* Process leading zeroes */
3533 if (number
== 0.0 && size
> 1) {
3541 while (*ptr1
!= '\0') {
3542 if (!first
) first
= ptr2
;
3552 /* We never found a non-zero digit, then our number is either
3553 * smaller than the requested precision, or 0.0 */
3554 if (!first
&& (number
<= 0.0))
3557 *decpt
= dec2
? dec2
: dec1
;
3561 /***********************************************************************
3564 char * CDECL
_gcvt( double number
, int ndigit
, char *buff
)
3576 sprintf(buff
, "%.*g", ndigit
, number
);
3580 /***********************************************************************
3581 * _gcvt_s (MSVCRT.@)
3583 int CDECL
_gcvt_s(char *buff
, size_t size
, double number
, int digits
)
3592 if( digits
<0 || digits
>=size
) {
3600 len
= _scprintf("%.*g", digits
, number
);
3607 sprintf(buff
, "%.*g", digits
, number
);
3611 #include <stdlib.h> /* div_t, ldiv_t */
3613 /*********************************************************************
3616 * [i386] Windows binary compatible - returns the struct in eax/edx.
3619 unsigned __int64 CDECL
div(int num
, int denom
)
3623 unsigned __int64 uint64
;
3626 ret
.div
.quot
= num
/ denom
;
3627 ret
.div
.rem
= num
% denom
;
3631 /*********************************************************************
3634 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
3636 div_t CDECL
div(int num
, int denom
)
3640 ret
.quot
= num
/ denom
;
3641 ret
.rem
= num
% denom
;
3644 #endif /* ifdef __i386__ */
3647 /*********************************************************************
3650 * [i386] Windows binary compatible - returns the struct in eax/edx.
3653 unsigned __int64 CDECL
ldiv(__msvcrt_long num
, __msvcrt_long denom
)
3657 unsigned __int64 uint64
;
3660 ret
.ldiv
.quot
= num
/ denom
;
3661 ret
.ldiv
.rem
= num
% denom
;
3665 /*********************************************************************
3668 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
3670 ldiv_t CDECL
ldiv(__msvcrt_long num
, __msvcrt_long denom
)
3674 ret
.quot
= num
/ denom
;
3675 ret
.rem
= num
% denom
;
3678 #endif /* ifdef __i386__ */
3681 /*********************************************************************
3682 * lldiv (MSVCR100.@)
3684 lldiv_t CDECL
lldiv(__int64 num
, __int64 denom
)
3688 ret
.quot
= num
/ denom
;
3689 ret
.rem
= num
% denom
;
3697 /*********************************************************************
3698 * _adjust_fdiv (MSVCRT.@)
3699 * Used by the MSVC compiler to work around the Pentium FDIV bug.
3701 int MSVCRT__adjust_fdiv
= 0;
3703 /***********************************************************************
3704 * _adj_fdiv_m16i (MSVCRT.@)
3707 * I _think_ this function is intended to work around the Pentium
3710 void __stdcall
_adj_fdiv_m16i( short arg
)
3712 TRACE("(): stub\n");
3715 /***********************************************************************
3716 * _adj_fdiv_m32 (MSVCRT.@)
3719 * I _think_ this function is intended to work around the Pentium
3722 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
3724 TRACE("(): stub\n");
3727 /***********************************************************************
3728 * _adj_fdiv_m32i (MSVCRT.@)
3731 * I _think_ this function is intended to work around the Pentium
3734 void __stdcall
_adj_fdiv_m32i( int arg
)
3736 TRACE("(): stub\n");
3739 /***********************************************************************
3740 * _adj_fdiv_m64 (MSVCRT.@)
3743 * I _think_ this function is intended to work around the Pentium
3746 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
3748 TRACE("(): stub\n");
3751 /***********************************************************************
3752 * _adj_fdiv_r (MSVCRT.@)
3754 * This function is likely to have the wrong number of arguments.
3757 * I _think_ this function is intended to work around the Pentium
3760 void _adj_fdiv_r(void)
3762 TRACE("(): stub\n");
3765 /***********************************************************************
3766 * _adj_fdivr_m16i (MSVCRT.@)
3769 * I _think_ this function is intended to work around the Pentium
3772 void __stdcall
_adj_fdivr_m16i( short arg
)
3774 TRACE("(): stub\n");
3777 /***********************************************************************
3778 * _adj_fdivr_m32 (MSVCRT.@)
3781 * I _think_ this function is intended to work around the Pentium
3784 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
3786 TRACE("(): stub\n");
3789 /***********************************************************************
3790 * _adj_fdivr_m32i (MSVCRT.@)
3793 * I _think_ this function is intended to work around the Pentium
3796 void __stdcall
_adj_fdivr_m32i( int arg
)
3798 TRACE("(): stub\n");
3801 /***********************************************************************
3802 * _adj_fdivr_m64 (MSVCRT.@)
3805 * I _think_ this function is intended to work around the Pentium
3808 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
3810 TRACE("(): stub\n");
3813 /***********************************************************************
3814 * _adj_fpatan (MSVCRT.@)
3816 * This function is likely to have the wrong number of arguments.
3819 * I _think_ this function is intended to work around the Pentium
3822 void _adj_fpatan(void)
3824 TRACE("(): stub\n");
3827 /***********************************************************************
3828 * _adj_fprem (MSVCRT.@)
3830 * This function is likely to have the wrong number of arguments.
3833 * I _think_ this function is intended to work around the Pentium
3836 void _adj_fprem(void)
3838 TRACE("(): stub\n");
3841 /***********************************************************************
3842 * _adj_fprem1 (MSVCRT.@)
3844 * This function is likely to have the wrong number of arguments.
3847 * I _think_ this function is intended to work around the Pentium
3850 void _adj_fprem1(void)
3852 TRACE("(): stub\n");
3855 /***********************************************************************
3856 * _adj_fptan (MSVCRT.@)
3858 * This function is likely to have the wrong number of arguments.
3861 * I _think_ this function is intended to work around the Pentium
3864 void _adj_fptan(void)
3866 TRACE("(): stub\n");
3869 /***********************************************************************
3870 * _safe_fdiv (MSVCRT.@)
3872 * This function is likely to have the wrong number of arguments.
3875 * I _think_ this function is intended to work around the Pentium
3878 void _safe_fdiv(void)
3880 TRACE("(): stub\n");
3883 /***********************************************************************
3884 * _safe_fdivr (MSVCRT.@)
3886 * This function is likely to have the wrong number of arguments.
3889 * I _think_ this function is intended to work around the Pentium
3892 void _safe_fdivr(void)
3894 TRACE("(): stub\n");
3897 /***********************************************************************
3898 * _safe_fprem (MSVCRT.@)
3900 * This function is likely to have the wrong number of arguments.
3903 * I _think_ this function is intended to work around the Pentium
3906 void _safe_fprem(void)
3908 TRACE("(): stub\n");
3911 /***********************************************************************
3912 * _safe_fprem1 (MSVCRT.@)
3915 * This function is likely to have the wrong number of arguments.
3918 * I _think_ this function is intended to work around the Pentium
3921 void _safe_fprem1(void)
3923 TRACE("(): stub\n");
3926 /***********************************************************************
3927 * __libm_sse2_acos (MSVCRT.@)
3929 void __cdecl
__libm_sse2_acos(void)
3932 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3934 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3937 /***********************************************************************
3938 * __libm_sse2_acosf (MSVCRT.@)
3940 void __cdecl
__libm_sse2_acosf(void)
3943 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3945 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3948 /***********************************************************************
3949 * __libm_sse2_asin (MSVCRT.@)
3951 void __cdecl
__libm_sse2_asin(void)
3954 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3956 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3959 /***********************************************************************
3960 * __libm_sse2_asinf (MSVCRT.@)
3962 void __cdecl
__libm_sse2_asinf(void)
3965 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3967 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3970 /***********************************************************************
3971 * __libm_sse2_atan (MSVCRT.@)
3973 void __cdecl
__libm_sse2_atan(void)
3976 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3978 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3981 /***********************************************************************
3982 * __libm_sse2_atan2 (MSVCRT.@)
3984 void __cdecl
__libm_sse2_atan2(void)
3987 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
3988 d1
= atan2( d1
, d2
);
3989 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
3992 /***********************************************************************
3993 * __libm_sse2_atanf (MSVCRT.@)
3995 void __cdecl
__libm_sse2_atanf(void)
3998 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4000 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4003 /***********************************************************************
4004 * __libm_sse2_cos (MSVCRT.@)
4006 void __cdecl
__libm_sse2_cos(void)
4009 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4011 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4014 /***********************************************************************
4015 * __libm_sse2_cosf (MSVCRT.@)
4017 void __cdecl
__libm_sse2_cosf(void)
4020 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4022 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4025 /***********************************************************************
4026 * __libm_sse2_exp (MSVCRT.@)
4028 void __cdecl
__libm_sse2_exp(void)
4031 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4033 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4036 /***********************************************************************
4037 * __libm_sse2_expf (MSVCRT.@)
4039 void __cdecl
__libm_sse2_expf(void)
4042 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4044 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4047 /***********************************************************************
4048 * __libm_sse2_log (MSVCRT.@)
4050 void __cdecl
__libm_sse2_log(void)
4053 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4055 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4058 /***********************************************************************
4059 * __libm_sse2_log10 (MSVCRT.@)
4061 void __cdecl
__libm_sse2_log10(void)
4064 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4066 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4069 /***********************************************************************
4070 * __libm_sse2_log10f (MSVCRT.@)
4072 void __cdecl
__libm_sse2_log10f(void)
4075 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4077 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4080 /***********************************************************************
4081 * __libm_sse2_logf (MSVCRT.@)
4083 void __cdecl
__libm_sse2_logf(void)
4086 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4088 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4091 /***********************************************************************
4092 * __libm_sse2_pow (MSVCRT.@)
4094 void __cdecl
__libm_sse2_pow(void)
4097 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
4099 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
4102 /***********************************************************************
4103 * __libm_sse2_powf (MSVCRT.@)
4105 void __cdecl
__libm_sse2_powf(void)
4108 __asm__
__volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1
), "=g" (f2
) );
4109 f1
= powf( f1
, f2
);
4110 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f1
) );
4113 /***********************************************************************
4114 * __libm_sse2_sin (MSVCRT.@)
4116 void __cdecl
__libm_sse2_sin(void)
4119 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4121 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4124 /***********************************************************************
4125 * __libm_sse2_sinf (MSVCRT.@)
4127 void __cdecl
__libm_sse2_sinf(void)
4130 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4132 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4135 /***********************************************************************
4136 * __libm_sse2_tan (MSVCRT.@)
4138 void __cdecl
__libm_sse2_tan(void)
4141 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4143 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4146 /***********************************************************************
4147 * __libm_sse2_tanf (MSVCRT.@)
4149 void __cdecl
__libm_sse2_tanf(void)
4152 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
4154 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
4157 /***********************************************************************
4158 * __libm_sse2_sqrt_precise (MSVCR110.@)
4160 void __cdecl
__libm_sse2_sqrt_precise(void)
4165 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
4166 __control87_2(0, 0, NULL
, &cw
);
4170 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4174 if (!sqrt_validate(&d
, FALSE
))
4176 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
4179 __asm__
__volatile__( "call " __ASM_NAME( "sse2_sqrt" ) );
4181 #endif /* __i386__ */
4183 /*********************************************************************
4184 * _fdclass (MSVCR120.@)
4186 * Copied from musl: src/math/__fpclassifyf.c
4188 short CDECL
_fdclass(float x
)
4190 union { float f
; UINT32 i
; } u
= { x
};
4191 int e
= u
.i
>> 23 & 0xff;
4193 if (!e
) return u
.i
<< 1 ? FP_SUBNORMAL
: FP_ZERO
;
4194 if (e
== 0xff) return u
.i
<< 9 ? FP_NAN
: FP_INFINITE
;
4198 /*********************************************************************
4199 * _dclass (MSVCR120.@)
4201 * Copied from musl: src/math/__fpclassify.c
4203 short CDECL
_dclass(double x
)
4205 union { double f
; UINT64 i
; } u
= { x
};
4206 int e
= u
.i
>> 52 & 0x7ff;
4208 if (!e
) return u
.i
<< 1 ? FP_SUBNORMAL
: FP_ZERO
;
4209 if (e
== 0x7ff) return (u
.i
<< 12) ? FP_NAN
: FP_INFINITE
;
4215 /*********************************************************************
4218 double CDECL
cbrt(double x
)
4220 return unix_funcs
->cbrt( x
);
4223 /*********************************************************************
4224 * cbrtf (MSVCR120.@)
4226 float CDECL
cbrtf(float x
)
4228 return unix_funcs
->cbrtf( x
);
4231 /*********************************************************************
4234 double CDECL
exp2(double x
)
4236 double ret
= unix_funcs
->exp2( x
);
4237 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4241 /*********************************************************************
4242 * exp2f (MSVCR120.@)
4244 float CDECL
exp2f(float x
)
4246 float ret
= unix_funcs
->exp2f( x
);
4247 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4251 /*********************************************************************
4252 * expm1 (MSVCR120.@)
4254 double CDECL
expm1(double x
)
4256 double ret
= unix_funcs
->expm1( x
);
4257 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4261 /*********************************************************************
4262 * expm1f (MSVCR120.@)
4264 float CDECL
expm1f(float x
)
4266 float ret
= unix_funcs
->expm1f( x
);
4267 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
4271 /*********************************************************************
4272 * log1p (MSVCR120.@)
4274 double CDECL
log1p(double x
)
4276 if (x
< -1) *_errno() = EDOM
;
4277 else if (x
== -1) *_errno() = ERANGE
;
4278 return unix_funcs
->log1p( x
);
4281 /*********************************************************************
4282 * log1pf (MSVCR120.@)
4284 float CDECL
log1pf(float x
)
4286 if (x
< -1) *_errno() = EDOM
;
4287 else if (x
== -1) *_errno() = ERANGE
;
4288 return unix_funcs
->log1pf( x
);
4291 /*********************************************************************
4294 double CDECL
log2(double x
)
4296 if (x
< 0) *_errno() = EDOM
;
4297 else if (x
== 0) *_errno() = ERANGE
;
4298 return unix_funcs
->log2( x
);
4301 /*********************************************************************
4302 * log2f (MSVCR120.@)
4304 float CDECL
log2f(float x
)
4306 if (x
< 0) *_errno() = EDOM
;
4307 else if (x
== 0) *_errno() = ERANGE
;
4308 return unix_funcs
->log2f( x
);
4311 /*********************************************************************
4314 * Copied from musl: src/math/rint.c
4316 double CDECL
rint(double x
)
4318 static const double toint
= 1 / DBL_EPSILON
;
4320 ULONGLONG llx
= *(ULONGLONG
*)&x
;
4321 int e
= llx
>> 52 & 0x7ff;
4328 cw
= _controlfp(0, 0);
4329 if ((cw
& _MCW_PC
) != _PC_53
)
4330 _controlfp(_PC_53
, _MCW_PC
);
4332 y
= fp_barrier(x
- toint
) + toint
;
4334 y
= fp_barrier(x
+ toint
) - toint
;
4335 if ((cw
& _MCW_PC
) != _PC_53
)
4336 _controlfp(cw
, _MCW_PC
);
4338 return s
? -0.0 : 0;
4342 /*********************************************************************
4343 * rintf (MSVCR120.@)
4345 * Copied from musl: src/math/rintf.c
4347 float CDECL
rintf(float x
)
4349 static const float toint
= 1 / FLT_EPSILON
;
4351 unsigned int ix
= *(unsigned int*)&x
;
4352 int e
= ix
>> 23 & 0xff;
4359 y
= fp_barrierf(x
- toint
) + toint
;
4361 y
= fp_barrierf(x
+ toint
) - toint
;
4363 return s
? -0.0f
: 0.0f
;
4367 /*********************************************************************
4368 * lrint (MSVCR120.@)
4370 __msvcrt_long CDECL
lrint(double x
)
4375 if ((d
< 0 && d
!= (double)(__msvcrt_long
)d
)
4376 || (d
>= 0 && d
!= (double)(__msvcrt_ulong
)d
)) {
4383 /*********************************************************************
4384 * lrintf (MSVCR120.@)
4386 __msvcrt_long CDECL
lrintf(float x
)
4391 if ((f
< 0 && f
!= (float)(__msvcrt_long
)f
)
4392 || (f
>= 0 && f
!= (float)(__msvcrt_ulong
)f
)) {
4399 /*********************************************************************
4400 * llrint (MSVCR120.@)
4402 __int64 CDECL
llrint(double x
)
4407 if ((d
< 0 && d
!= (double)(__int64
)d
)
4408 || (d
>= 0 && d
!= (double)(unsigned __int64
)d
)) {
4415 /*********************************************************************
4416 * llrintf (MSVCR120.@)
4418 __int64 CDECL
llrintf(float x
)
4423 if ((f
< 0 && f
!= (float)(__int64
)f
)
4424 || (f
>= 0 && f
!= (float)(unsigned __int64
)f
)) {
4431 /*********************************************************************
4432 * round (MSVCR120.@)
4434 * Based on musl implementation: src/math/round.c
4436 double CDECL
round(double x
)
4438 ULONGLONG llx
= *(ULONGLONG
*)&x
, tmp
;
4439 int e
= (llx
>> 52 & 0x7ff) - 0x3ff;
4446 return signbit(x
) ? -1 : 1;
4448 tmp
= 0x000fffffffffffffULL
>> e
;
4451 llx
+= 0x0008000000000000ULL
>> e
;
4453 return *(double*)&llx
;
4456 /*********************************************************************
4457 * roundf (MSVCR120.@)
4459 * Copied from musl: src/math/roundf.c
4461 float CDECL
roundf(float x
)
4463 static const float toint
= 1 / FLT_EPSILON
;
4465 unsigned int ix
= *(unsigned int*)&x
;
4466 int e
= ix
>> 23 & 0xff;
4474 return 0 * *(float*)&ix
;
4475 y
= fp_barrierf(x
+ toint
) - toint
- x
;
4478 else if (y
<= -0.5f
)
4487 /*********************************************************************
4488 * lround (MSVCR120.@)
4490 * Copied from musl: src/math/lround.c
4492 __msvcrt_long CDECL
lround(double x
)
4494 double d
= round(x
);
4495 if (d
!= (double)(__msvcrt_long
)d
) {
4502 /*********************************************************************
4503 * lroundf (MSVCR120.@)
4505 * Copied from musl: src/math/lroundf.c
4507 __msvcrt_long CDECL
lroundf(float x
)
4509 float f
= roundf(x
);
4510 if (f
!= (float)(__msvcrt_long
)f
) {
4517 /*********************************************************************
4518 * llround (MSVCR120.@)
4520 * Copied from musl: src/math/llround.c
4522 __int64 CDECL
llround(double x
)
4524 double d
= round(x
);
4525 if (d
!= (double)(__int64
)d
) {
4532 /*********************************************************************
4533 * llroundf (MSVCR120.@)
4535 * Copied from musl: src/math/llroundf.c
4537 __int64 CDECL
llroundf(float x
)
4539 float f
= roundf(x
);
4540 if (f
!= (float)(__int64
)f
) {
4547 /*********************************************************************
4548 * trunc (MSVCR120.@)
4550 double CDECL
trunc(double x
)
4552 return unix_funcs
->trunc(x
);
4555 /*********************************************************************
4556 * truncf (MSVCR120.@)
4558 float CDECL
truncf(float x
)
4560 return unix_funcs
->truncf(x
);
4563 /*********************************************************************
4564 * _dtest (MSVCR120.@)
4566 short CDECL
_dtest(double *x
)
4571 /*********************************************************************
4572 * _fdtest (MSVCR120.@)
4574 short CDECL
_fdtest(float *x
)
4576 return _fdclass(*x
);
4579 /*********************************************************************
4582 double CDECL
erf(double x
)
4584 return unix_funcs
->erf( x
);
4587 /*********************************************************************
4590 float CDECL
erff(float x
)
4592 return unix_funcs
->erff( x
);
4595 /*********************************************************************
4598 double CDECL
erfc(double x
)
4600 return unix_funcs
->erfc( x
);
4603 /*********************************************************************
4604 * erfcf (MSVCR120.@)
4606 float CDECL
erfcf(float x
)
4608 return unix_funcs
->erfcf( x
);
4611 /*********************************************************************
4612 * fmaxf (MSVCR120.@)
4614 float CDECL
fmaxf(float x
, float y
)
4621 return signbit(x
) ? y
: x
;
4625 /*********************************************************************
4628 double CDECL
fmax(double x
, double y
)
4635 return signbit(x
) ? y
: x
;
4639 /*********************************************************************
4640 * fdimf (MSVCR120.@)
4642 float CDECL
fdimf(float x
, float y
)
4648 return x
>y
? x
-y
: 0;
4651 /*********************************************************************
4654 double CDECL
fdim(double x
, double y
)
4660 return x
>y
? x
-y
: 0;
4663 /*********************************************************************
4664 * _fdsign (MSVCR120.@)
4666 int CDECL
_fdsign(float x
)
4668 union { float f
; UINT32 i
; } u
= { x
};
4669 return (u
.i
>> 16) & 0x8000;
4672 /*********************************************************************
4673 * _dsign (MSVCR120.@)
4675 int CDECL
_dsign(double x
)
4677 union { double f
; UINT64 i
; } u
= { x
};
4678 return (u
.i
>> 48) & 0x8000;
4682 /*********************************************************************
4683 * _dpcomp (MSVCR120.@)
4685 int CDECL
_dpcomp(double x
, double y
)
4687 if(isnan(x
) || isnan(y
))
4690 if(x
== y
) return 2;
4691 return x
< y
? 1 : 4;
4694 /*********************************************************************
4695 * _fdpcomp (MSVCR120.@)
4697 int CDECL
_fdpcomp(float x
, float y
)
4699 return _dpcomp(x
, y
);
4702 /*********************************************************************
4703 * fminf (MSVCR120.@)
4705 float CDECL
fminf(float x
, float y
)
4712 return signbit(x
) ? x
: y
;
4716 /*********************************************************************
4719 double CDECL
fmin(double x
, double y
)
4726 return signbit(x
) ? x
: y
;
4730 /*********************************************************************
4731 * asinh (MSVCR120.@)
4733 double CDECL
asinh(double x
)
4735 return unix_funcs
->asinh( x
);
4738 /*********************************************************************
4739 * asinhf (MSVCR120.@)
4741 float CDECL
asinhf(float x
)
4743 return unix_funcs
->asinhf( x
);
4746 /*********************************************************************
4747 * acosh (MSVCR120.@)
4749 double CDECL
acosh(double x
)
4754 feraiseexcept(FE_INVALID
);
4757 return unix_funcs
->acosh( x
);
4760 /*********************************************************************
4761 * acoshf (MSVCR120.@)
4763 float CDECL
acoshf(float x
)
4768 feraiseexcept(FE_INVALID
);
4771 return unix_funcs
->acoshf( x
);
4774 /*********************************************************************
4775 * atanh (MSVCR120.@)
4777 double CDECL
atanh(double x
)
4781 if (x
> 1 || x
< -1) {
4783 /* on Linux atanh returns -NAN in this case */
4784 feraiseexcept(FE_INVALID
);
4787 ret
= unix_funcs
->atanh( x
);
4789 if (!isfinite(ret
)) *_errno() = ERANGE
;
4793 /*********************************************************************
4794 * atanhf (MSVCR120.@)
4796 float CDECL
atanhf(float x
)
4800 if (x
> 1 || x
< -1) {
4802 feraiseexcept(FE_INVALID
);
4806 ret
= unix_funcs
->atanh( x
);
4808 if (!isfinite(ret
)) *_errno() = ERANGE
;
4812 #endif /* _MSVCR_VER>=120 */
4814 /*********************************************************************
4816 * scalbn (MSVCR120.@)
4817 * scalbln (MSVCR120.@)
4819 double CDECL
_scalb(double num
, __msvcrt_long power
)
4821 return ldexp(num
, power
);
4824 /*********************************************************************
4825 * _scalbf (MSVCRT.@)
4826 * scalbnf (MSVCR120.@)
4827 * scalblnf (MSVCR120.@)
4829 float CDECL
_scalbf(float num
, __msvcrt_long power
)
4831 return ldexp(num
, power
);
4836 /*********************************************************************
4837 * remainder (MSVCR120.@)
4839 double CDECL
remainder(double x
, double y
)
4841 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
4842 if(!isfinite(x
)) *_errno() = EDOM
;
4843 if(isnan(y
) || y
==0.0) *_errno() = EDOM
;
4844 return unix_funcs
->remainder( x
, y
);
4847 /*********************************************************************
4848 * remainderf (MSVCR120.@)
4850 float CDECL
remainderf(float x
, float y
)
4852 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
4853 if(!isfinite(x
)) *_errno() = EDOM
;
4854 if(isnan(y
) || y
==0.0f
) *_errno() = EDOM
;
4855 return unix_funcs
->remainderf( x
, y
);
4858 /*********************************************************************
4859 * remquo (MSVCR120.@)
4861 double CDECL
remquo(double x
, double y
, int *quo
)
4863 if(!isfinite(x
)) *_errno() = EDOM
;
4864 if(isnan(y
) || y
==0.0) *_errno() = EDOM
;
4865 return unix_funcs
->remquo( x
, y
, quo
);
4868 /*********************************************************************
4869 * remquof (MSVCR120.@)
4871 float CDECL
remquof(float x
, float y
, int *quo
)
4873 if(!isfinite(x
)) *_errno() = EDOM
;
4874 if(isnan(y
) || y
==0.0f
) *_errno() = EDOM
;
4875 return unix_funcs
->remquof( x
, y
, quo
);
4878 /*********************************************************************
4879 * lgamma (MSVCR120.@)
4881 double CDECL
lgamma(double x
)
4883 return unix_funcs
->lgamma( x
);
4886 /*********************************************************************
4887 * lgammaf (MSVCR120.@)
4889 float CDECL
lgammaf(float x
)
4891 return unix_funcs
->lgammaf( x
);
4894 /*********************************************************************
4895 * tgamma (MSVCR120.@)
4897 double CDECL
tgamma(double x
)
4899 return unix_funcs
->tgamma( x
);
4902 /*********************************************************************
4903 * tgammaf (MSVCR120.@)
4905 float CDECL
tgammaf(float x
)
4907 return unix_funcs
->tgammaf( x
);
4910 /*********************************************************************
4913 double CDECL
nan(const char *tagp
)
4915 /* Windows ignores input (MSDN) */
4919 /*********************************************************************
4922 float CDECL
nanf(const char *tagp
)
4927 /*********************************************************************
4928 * _except1 (MSVCR120.@)
4930 * - find meaning of ignored cw and operation bits
4933 double CDECL
_except1(DWORD fpe
, _FP_OPERATION_CODE op
, double arg
, double res
, DWORD cw
, void *unk
)
4935 ULONG_PTR exception_arg
;
4936 DWORD exception
= 0;
4941 TRACE("(%x %x %lf %lf %x %p)\n", fpe
, op
, arg
, res
, cw
, unk
);
4944 cw
= ((cw
>> 7) & 0x3f) | ((cw
>> 3) & 0xc00);
4946 operation
= op
<< 5;
4947 exception_arg
= (ULONG_PTR
)&operation
;
4951 if (fpe
& 0x1) { /* overflow */
4952 if ((fpe
== 0x1 && (cw
& 0x8)) || (fpe
==0x11 && (cw
& 0x28))) {
4953 /* 32-bit version also sets SW_INEXACT here */
4954 env
._Fe_stat
|= FE_OVERFLOW
;
4955 if (fpe
& 0x10) env
._Fe_stat
|= FE_INEXACT
;
4956 res
= signbit(res
) ? -INFINITY
: INFINITY
;
4958 exception
= EXCEPTION_FLT_OVERFLOW
;
4960 } else if (fpe
& 0x2) { /* underflow */
4961 if ((fpe
== 0x2 && (cw
& 0x10)) || (fpe
==0x12 && (cw
& 0x30))) {
4962 env
._Fe_stat
|= FE_UNDERFLOW
;
4963 if (fpe
& 0x10) env
._Fe_stat
|= FE_INEXACT
;
4964 res
= signbit(res
) ? -0.0 : 0.0;
4966 exception
= EXCEPTION_FLT_UNDERFLOW
;
4968 } else if (fpe
& 0x4) { /* zerodivide */
4969 if ((fpe
== 0x4 && (cw
& 0x4)) || (fpe
==0x14 && (cw
& 0x24))) {
4970 env
._Fe_stat
|= FE_DIVBYZERO
;
4971 if (fpe
& 0x10) env
._Fe_stat
|= FE_INEXACT
;
4973 exception
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
4975 } else if (fpe
& 0x8) { /* invalid */
4976 if (fpe
== 0x8 && (cw
& 0x1)) {
4977 env
._Fe_stat
|= FE_INVALID
;
4979 exception
= EXCEPTION_FLT_INVALID_OPERATION
;
4981 } else if (fpe
& 0x10) { /* inexact */
4982 if (fpe
== 0x10 && (cw
& 0x20)) {
4983 env
._Fe_stat
|= FE_INEXACT
;
4985 exception
= EXCEPTION_FLT_INEXACT_RESULT
;
4993 RaiseException(exception
, 0, 1, &exception_arg
);
4995 if (cw
& 0x1) fpword
|= _EM_INVALID
;
4996 if (cw
& 0x2) fpword
|= _EM_DENORMAL
;
4997 if (cw
& 0x4) fpword
|= _EM_ZERODIVIDE
;
4998 if (cw
& 0x8) fpword
|= _EM_OVERFLOW
;
4999 if (cw
& 0x10) fpword
|= _EM_UNDERFLOW
;
5000 if (cw
& 0x20) fpword
|= _EM_INEXACT
;
5003 case 0xc00: fpword
|= _RC_UP
|_RC_DOWN
; break;
5004 case 0x800: fpword
|= _RC_UP
; break;
5005 case 0x400: fpword
|= _RC_DOWN
; break;
5009 case 0x0: fpword
|= _PC_24
; break;
5010 case 0x200: fpword
|= _PC_53
; break;
5011 case 0x300: fpword
|= _PC_64
; break;
5013 if (cw
& 0x1000) fpword
|= _IC_AFFINE
;
5014 _control87(fpword
, 0xffffffff);
5019 _Dcomplex
* CDECL
_Cbuild(_Dcomplex
*ret
, double r
, double i
)
5026 double CDECL
MSVCR120_creal(_Dcomplex z
)
5031 /*********************************************************************
5032 * ilogb (MSVCR120.@)
5034 * Copied from musl: src/math/ilogb.c
5036 int CDECL
ilogb(double x
)
5038 union { double f
; UINT64 i
; } u
= { x
};
5039 int e
= u
.i
>> 52 & 0x7ff;
5044 if (u
.i
== 0) return FP_ILOGB0
;
5046 for (e
= -0x3ff; u
.i
>> 63 == 0; e
--, u
.i
<<= 1);
5049 if (e
== 0x7ff) return u
.i
<< 12 ? FP_ILOGBNAN
: INT_MAX
;
5053 /*********************************************************************
5054 * ilogbf (MSVCR120.@)
5056 * Copied from musl: src/math/ilogbf.c
5058 int CDECL
ilogbf(float x
)
5060 union { float f
; UINT32 i
; } u
= { x
};
5061 int e
= u
.i
>> 23 & 0xff;
5066 if (u
.i
== 0) return FP_ILOGB0
;
5068 for (e
= -0x7f; u
.i
>> 31 == 0; e
--, u
.i
<<= 1);
5071 if (e
== 0xff) return u
.i
<< 9 ? FP_ILOGBNAN
: INT_MAX
;
5074 #endif /* _MSVCR_VER>=120 */