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
;
67 static BOOL sse2_supported
;
68 static BOOL sse2_enabled
;
70 static const struct unix_funcs
*unix_funcs
;
72 void msvcrt_init_math( void *module
)
74 sse2_supported
= sse2_enabled
= IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE
);
75 __wine_init_unix_lib( module
, DLL_PROCESS_ATTACH
, NULL
, &unix_funcs
);
78 /* Copied from musl: src/internal/libm.h */
79 static inline float fp_barrierf(float x
)
85 /*********************************************************************
88 int CDECL
_matherr(struct _exception
*e
)
94 static double math_error(int type
, const char *name
, double arg1
, double arg2
, double retval
)
96 struct _exception exception
= {type
, (char *)name
, arg1
, arg2
, retval
};
98 TRACE("(%d, %s, %g, %g, %g)\n", type
, debugstr_a(name
), arg1
, arg2
, retval
);
100 if (MSVCRT_default_matherr_func
&& MSVCRT_default_matherr_func(&exception
))
101 return exception
.retval
;
113 /* don't set errno */
116 ERR("Unhandled math error!\n");
119 return exception
.retval
;
122 /*********************************************************************
123 * __setusermatherr (MSVCRT.@)
125 void CDECL
__setusermatherr(MSVCRT_matherr_func func
)
127 MSVCRT_default_matherr_func
= func
;
128 TRACE("new matherr handler %p\n", func
);
131 /*********************************************************************
132 * _set_SSE2_enable (MSVCRT.@)
134 int CDECL
_set_SSE2_enable(int flag
)
136 sse2_enabled
= flag
&& sse2_supported
;
142 /*********************************************************************
143 * _get_FMA3_enable (UCRTBASE.@)
145 int CDECL
_get_FMA3_enable(void)
153 /*********************************************************************
154 * _set_FMA3_enable (MSVCR120.@)
156 int CDECL
_set_FMA3_enable(int flag
)
158 FIXME("(%x) stub\n", flag
);
164 #if !defined(__i386__) || _MSVCR_VER>=120
166 /*********************************************************************
167 * _chgsignf (MSVCRT.@)
169 float CDECL
_chgsignf( float num
)
171 union { float f
; UINT32 i
; } u
= { num
};
176 /*********************************************************************
177 * _copysignf (MSVCRT.@)
179 * Copied from musl: src/math/copysignf.c
181 float CDECL
_copysignf( float x
, float y
)
183 union { float f
; UINT32 i
; } ux
= { x
}, uy
= { y
};
185 ux
.i
|= uy
.i
& 0x80000000;
189 /*********************************************************************
190 * _nextafterf (MSVCRT.@)
192 float CDECL
_nextafterf( float num
, float next
)
194 if (!isfinite(num
) || !isfinite(next
)) *_errno() = EDOM
;
195 return unix_funcs
->nextafterf( num
, next
);
198 /*********************************************************************
201 float CDECL
_logbf( float num
)
203 float ret
= unix_funcs
->logbf(num
);
204 if (isnan(num
)) return math_error(_DOMAIN
, "_logbf", num
, 0, ret
);
205 if (!num
) return math_error(_SING
, "_logbf", num
, 0, ret
);
213 /*********************************************************************
214 * _fpclassf (MSVCRT.@)
216 int CDECL
_fpclassf( float num
)
218 union { float f
; UINT32 i
; } u
= { num
};
219 int e
= u
.i
>> 23 & 0xff;
225 if (u
.i
<< 1) return s
? _FPCLASS_ND
: _FPCLASS_PD
;
226 return s
? _FPCLASS_NZ
: _FPCLASS_PZ
;
228 if (u
.i
<< 9) return ((u
.i
>> 22) & 1) ? _FPCLASS_QNAN
: _FPCLASS_SNAN
;
229 return s
? _FPCLASS_NINF
: _FPCLASS_PINF
;
231 return s
? _FPCLASS_NN
: _FPCLASS_PN
;
235 /*********************************************************************
236 * _finitef (MSVCRT.@)
238 int CDECL
_finitef( float num
)
240 union { float f
; UINT32 i
; } u
= { num
};
241 return (u
.i
& 0x7fffffff) < 0x7f800000;
244 /*********************************************************************
247 int CDECL
_isnanf( float num
)
249 union { float f
; UINT32 i
; } u
= { num
};
250 return (u
.i
& 0x7fffffff) > 0x7f800000;
253 /*********************************************************************
256 * Copied from musl: src/math/acosf.c
258 static float acosf_R(float z
)
260 static const float pS0
= 1.6666586697e-01,
261 pS1
= -4.2743422091e-02,
262 pS2
= -8.6563630030e-03,
263 qS1
= -7.0662963390e-01;
266 p
= z
* (pS0
+ z
* (pS1
+ z
* pS2
));
271 float CDECL
acosf( float x
)
273 static const float pio2_hi
= 1.5707962513e+00,
274 pio2_lo
= 7.5497894159e-08;
276 float z
, w
, s
, c
, df
;
279 hx
= *(unsigned int*)&x
;
280 ix
= hx
& 0x7fffffff;
281 /* |x| >= 1 or nan */
282 if (ix
>= 0x3f800000) {
283 if (ix
== 0x3f800000) {
285 return 2 * pio2_lo
+ 2 * pio2_hi
+ 7.5231638453e-37;
288 if (isnan(x
)) return x
;
289 return math_error(_DOMAIN
, "acosf", x
, 0, 0 / (x
- x
));
292 if (ix
< 0x3f000000) {
293 if (ix
<= 0x32800000) /* |x| < 2**-26 */
294 return pio2_lo
+ pio2_hi
+ 7.5231638453e-37;
295 return pio2_hi
- (x
- (pio2_lo
- x
* acosf_R(x
* x
)));
301 w
= acosf_R(z
) * s
- pio2_lo
;
302 return 2 * (pio2_hi
- (s
+ w
));
307 hx
= *(unsigned int*)&s
& 0xfffff000;
309 c
= (z
- df
* df
) / (s
+ df
);
310 w
= acosf_R(z
) * s
+ c
;
314 /*********************************************************************
317 * Copied from musl: src/math/asinf.c
319 static float asinf_R(float z
)
321 /* coefficients for R(x^2) */
322 static const float pS0
= 1.6666586697e-01,
323 pS1
= -4.2743422091e-02,
324 pS2
= -8.6563630030e-03,
325 qS1
= -7.0662963390e-01;
328 p
= z
* (pS0
+ z
* (pS1
+ z
* pS2
));
333 float CDECL
asinf( float x
)
335 static const double pio2
= 1.570796326794896558e+00;
341 hx
= *(unsigned int*)&x
;
342 ix
= hx
& 0x7fffffff;
343 if (ix
>= 0x3f800000) { /* |x| >= 1 */
344 if (ix
== 0x3f800000) /* |x| == 1 */
345 return x
* pio2
+ 7.5231638453e-37; /* asin(+-1) = +-pi/2 with inexact */
346 if (isnan(x
)) return x
;
347 return math_error(_DOMAIN
, "asinf", x
, 0, 0 / (x
- x
));
349 if (ix
< 0x3f000000) { /* |x| < 0.5 */
350 /* if 0x1p-126 <= |x| < 0x1p-12, avoid raising underflow */
351 if (ix
< 0x39800000 && ix
>= 0x00800000)
353 return x
+ x
* asinf_R(x
* x
);
356 z
= (1 - fabsf(x
)) * 0.5f
;
358 x
= pio2
- 2 * (s
+ s
* asinf_R(z
));
364 /*********************************************************************
367 * Copied from musl: src/math/atanf.c
369 float CDECL
atanf( float x
)
371 static const float atanhi
[] = {
377 static const float atanlo
[] = {
383 static const float aT
[] = {
392 unsigned int ix
, sign
;
396 if (isnan(x
)) return math_error(_DOMAIN
, "atanf", x
, 0, x
);
399 ix
= *(unsigned int*)&x
;
402 if (ix
>= 0x4c800000) { /* if |x| >= 2**26 */
405 z
= atanhi
[3] + 7.5231638453e-37;
406 return sign
? -z
: z
;
408 if (ix
< 0x3ee00000) { /* |x| < 0.4375 */
409 if (ix
< 0x39800000) { /* |x| < 2**-12 */
411 /* raise underflow for subnormal x */
418 if (ix
< 0x3f980000) { /* |x| < 1.1875 */
419 if (ix
< 0x3f300000) { /* 7/16 <= |x| < 11/16 */
421 x
= (2.0f
* x
- 1.0f
) / (2.0f
+ x
);
422 } else { /* 11/16 <= |x| < 19/16 */
424 x
= (x
- 1.0f
) / (x
+ 1.0f
);
427 if (ix
< 0x401c0000) { /* |x| < 2.4375 */
429 x
= (x
- 1.5f
) / (1.0f
+ 1.5f
* x
);
430 } else { /* 2.4375 <= |x| < 2**26 */
436 /* end of argument reduction */
439 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
440 s1
= z
* (aT
[0] + w
* (aT
[2] + w
* aT
[4]));
441 s2
= w
* (aT
[1] + w
* aT
[3]);
443 return x
- x
* (s1
+ s2
);
444 z
= atanhi
[id
] - ((x
* (s1
+ s2
) - atanlo
[id
]) - x
);
445 return sign
? -z
: z
;
448 /*********************************************************************
451 * Copied from musl: src/math/atan2f.c
453 float CDECL
atan2f( float y
, float x
)
455 static const float pi
= 3.1415927410e+00,
456 pi_lo
= -8.7422776573e-08;
459 unsigned int m
, ix
, iy
;
461 if (isnan(x
) || isnan(y
))
463 ix
= *(unsigned int*)&x
;
464 iy
= *(unsigned int*)&y
;
465 if (ix
== 0x3f800000) /* x=1.0 */
467 m
= ((iy
>> 31) & 1) | ((ix
>> 30) & 2); /* 2*sign(x)+sign(y) */
475 case 1: return y
; /* atan(+-0,+anything)=+-0 */
476 case 2: return pi
; /* atan(+0,-anything) = pi */
477 case 3: return -pi
; /* atan(-0,-anything) =-pi */
482 return m
& 1 ? -pi
/ 2 : pi
/ 2;
484 if (ix
== 0x7f800000) {
485 if (iy
== 0x7f800000) {
487 case 0: return pi
/ 4; /* atan(+INF,+INF) */
488 case 1: return -pi
/ 4; /* atan(-INF,+INF) */
489 case 2: return 3 * pi
/ 4; /*atan(+INF,-INF)*/
490 case 3: return -3 * pi
/ 4; /*atan(-INF,-INF)*/
494 case 0: return 0.0f
; /* atan(+...,+INF) */
495 case 1: return -0.0f
; /* atan(-...,+INF) */
496 case 2: return pi
; /* atan(+...,-INF) */
497 case 3: return -pi
; /* atan(-...,-INF) */
502 if (ix
+ (26 << 23) < iy
|| iy
== 0x7f800000)
503 return m
& 1 ? -pi
/ 2 : pi
/ 2;
505 /* z = atan(|y/x|) with correct underflow */
506 if ((m
& 2) && iy
+ (26 << 23) < ix
) /*|y/x| < 0x1p-26, x < 0 */
509 z
= atanf(fabsf(y
/ x
));
511 case 0: return z
; /* atan(+,+) */
512 case 1: return -z
; /* atan(-,+) */
513 case 2: return pi
- (z
- pi_lo
); /* atan(+,-) */
514 default: /* case 3 */
515 return (z
- pi_lo
) - pi
; /* atan(-,-) */
519 /*********************************************************************
522 float CDECL
cosf( float x
)
524 float ret
= unix_funcs
->cosf( x
);
525 if (!isfinite(x
)) return math_error(_DOMAIN
, "cosf", x
, 0, ret
);
529 /*********************************************************************
532 float CDECL
coshf( float x
)
534 float ret
= unix_funcs
->coshf( x
);
535 if (isnan(x
)) return math_error(_DOMAIN
, "coshf", x
, 0, ret
);
539 /*********************************************************************
542 float CDECL
expf( float x
)
544 float ret
= unix_funcs
->expf( x
);
545 if (isnan(x
)) return math_error(_DOMAIN
, "expf", x
, 0, ret
);
546 if (isfinite(x
) && !ret
) return math_error(_UNDERFLOW
, "expf", x
, 0, ret
);
547 if (isfinite(x
) && !isfinite(ret
)) return math_error(_OVERFLOW
, "expf", x
, 0, ret
);
551 /*********************************************************************
554 float CDECL
fmodf( float x
, float y
)
556 float ret
= unix_funcs
->fmodf( x
, y
);
557 if (!isfinite(x
) || !isfinite(y
)) return math_error(_DOMAIN
, "fmodf", x
, 0, ret
);
561 /*********************************************************************
564 float CDECL
logf( float x
)
566 float ret
= unix_funcs
->logf( x
);
567 if (x
< 0.0) return math_error(_DOMAIN
, "logf", x
, 0, ret
);
568 if (x
== 0.0) return math_error(_SING
, "logf", x
, 0, ret
);
572 /*********************************************************************
575 float CDECL
log10f( float x
)
577 float ret
= unix_funcs
->log10f( x
);
578 if (x
< 0.0) return math_error(_DOMAIN
, "log10f", x
, 0, ret
);
579 if (x
== 0.0) return math_error(_SING
, "log10f", x
, 0, ret
);
583 /*********************************************************************
586 float CDECL
powf( float x
, float y
)
588 float z
= unix_funcs
->powf(x
,y
);
589 if (x
< 0 && y
!= floorf(y
)) return math_error(_DOMAIN
, "powf", x
, y
, z
);
590 if (!x
&& isfinite(y
) && y
< 0) return math_error(_SING
, "powf", x
, y
, z
);
591 if (isfinite(x
) && isfinite(y
) && !isfinite(z
)) return math_error(_OVERFLOW
, "powf", x
, y
, z
);
592 if (x
&& isfinite(x
) && isfinite(y
) && !z
) return math_error(_UNDERFLOW
, "powf", x
, y
, z
);
596 /*********************************************************************
599 float CDECL
sinf( float x
)
601 float ret
= unix_funcs
->sinf( x
);
602 if (!isfinite(x
)) return math_error(_DOMAIN
, "sinf", x
, 0, ret
);
606 /*********************************************************************
609 float CDECL
sinhf( float x
)
611 float ret
= unix_funcs
->sinhf( x
);
612 if (isnan(x
)) return math_error(_DOMAIN
, "sinhf", x
, 0, ret
);
616 /*********************************************************************
619 * Copied from musl: src/math/sqrtf.c
621 float CDECL
sqrtf( float x
)
623 static const float tiny
= 1.0e-30;
626 int sign
= 0x80000000;
632 /* take care of Inf and NaN */
633 if ((ix
& 0x7f800000) == 0x7f800000 && (ix
== 0x7f800000 || ix
& 0x7fffff))
636 /* take care of zero */
638 if ((ix
& ~sign
) == 0)
639 return x
; /* sqrt(+-0) = +-0 */
640 return math_error(_DOMAIN
, "sqrtf", x
, 0, (x
- x
) / (x
- x
)); /* sqrt(-ve) = sNaN */
644 if (m
== 0) { /* subnormal x */
645 for (i
= 0; (ix
& 0x00800000) == 0; i
++)
649 m
-= 127; /* unbias exponent */
650 ix
= (ix
& 0x007fffff) | 0x00800000;
651 if (m
& 1) /* odd m, double x to make it even */
653 m
>>= 1; /* m = [m/2] */
655 /* generate sqrt(x) bit by bit */
657 q
= s
= 0; /* q = sqrt(x) */
658 r
= 0x01000000; /* r = moving bit from right to left */
671 /* use floating add to find out rounding direction */
673 z
= 1.0f
- tiny
; /* raise inexact flag */
682 ix
= (q
>> 1) + 0x3f000000;
683 r
= ix
+ ((unsigned int)m
<< 23);
688 /*********************************************************************
691 float CDECL
tanf( float x
)
693 float ret
= unix_funcs
->tanf(x
);
694 if (!isfinite(x
)) return math_error(_DOMAIN
, "tanf", x
, 0, ret
);
698 /*********************************************************************
701 float CDECL
tanhf( float x
)
703 float ret
= unix_funcs
->tanhf(x
);
704 if (!isfinite(x
)) return math_error(_DOMAIN
, "tanhf", x
, 0, ret
);
708 /*********************************************************************
711 float CDECL
ceilf( float x
)
713 return unix_funcs
->ceilf(x
);
716 /*********************************************************************
719 * Copied from musl: src/math/fabsf.c
721 float CDECL
fabsf( float x
)
723 union { float f
; UINT32 i
; } u
= { x
};
728 /*********************************************************************
731 float CDECL
floorf( float x
)
733 return unix_funcs
->floorf(x
);
736 /*********************************************************************
739 float CDECL
frexpf( float x
, int *exp
)
741 return unix_funcs
->frexpf( x
, exp
);
744 /*********************************************************************
747 float CDECL
modff( float x
, float *iptr
)
749 return unix_funcs
->modff( x
, iptr
);
754 /*********************************************************************
757 * Copied from musl: src/math/acos.c
759 static double acos_R(double z
)
761 static const double pS0
= 1.66666666666666657415e-01,
762 pS1
= -3.25565818622400915405e-01,
763 pS2
= 2.01212532134862925881e-01,
764 pS3
= -4.00555345006794114027e-02,
765 pS4
= 7.91534994289814532176e-04,
766 pS5
= 3.47933107596021167570e-05,
767 qS1
= -2.40339491173441421878e+00,
768 qS2
= 2.02094576023350569471e+00,
769 qS3
= -6.88283971605453293030e-01,
770 qS4
= 7.70381505559019352791e-02;
773 p
= z
* (pS0
+ z
* (pS1
+ z
* (pS2
+ z
* (pS3
+ z
* (pS4
+ z
* pS5
)))));
774 q
= 1.0 + z
* (qS1
+ z
* (qS2
+ z
* (qS3
+ z
* qS4
)));
778 double CDECL
acos( double x
)
780 static const double pio2_hi
= 1.57079632679489655800e+00,
781 pio2_lo
= 6.12323399573676603587e-17;
783 double z
, w
, s
, c
, df
;
787 hx
= *(ULONGLONG
*)&x
>> 32;
788 ix
= hx
& 0x7fffffff;
789 /* |x| >= 1 or nan */
790 if (ix
>= 0x3ff00000) {
793 lx
= *(ULONGLONG
*)&x
;
794 if (((ix
- 0x3ff00000) | lx
) == 0) {
795 /* acos(1)=0, acos(-1)=pi */
797 return 2 * pio2_hi
+ 7.5231638452626401e-37;
800 if (isnan(x
)) return x
;
801 return math_error(_DOMAIN
, "acos", x
, 0, 0 / (x
- x
));
804 if (ix
< 0x3fe00000) {
805 if (ix
<= 0x3c600000) /* |x| < 2**-57 */
806 return pio2_hi
+ 7.5231638452626401e-37;
807 return pio2_hi
- (x
- (pio2_lo
- x
* acos_R(x
* x
)));
813 w
= acos_R(z
) * s
- pio2_lo
;
814 return 2 * (pio2_hi
- (s
+ w
));
820 llx
= (*(ULONGLONG
*)&df
>> 32) << 32;
822 c
= (z
- df
* df
) / (s
+ df
);
823 w
= acos_R(z
) * s
+ c
;
827 /*********************************************************************
830 * Copied from musl: src/math/asin.c
832 static double asin_R(double z
)
834 /* coefficients for R(x^2) */
835 static const double pS0
= 1.66666666666666657415e-01,
836 pS1
= -3.25565818622400915405e-01,
837 pS2
= 2.01212532134862925881e-01,
838 pS3
= -4.00555345006794114027e-02,
839 pS4
= 7.91534994289814532176e-04,
840 pS5
= 3.47933107596021167570e-05,
841 qS1
= -2.40339491173441421878e+00,
842 qS2
= 2.02094576023350569471e+00,
843 qS3
= -6.88283971605453293030e-01,
844 qS4
= 7.70381505559019352791e-02;
847 p
= z
* (pS0
+ z
* (pS1
+ z
* (pS2
+ z
* (pS3
+ z
* (pS4
+ z
* pS5
)))));
848 q
= 1.0 + z
* (qS1
+ z
* (qS2
+ z
* (qS3
+ z
* qS4
)));
852 double CDECL
asin( double x
)
854 static const double pio2_hi
= 1.57079632679489655800e+00,
855 pio2_lo
= 6.12323399573676603587e-17;
861 hx
= *(ULONGLONG
*)&x
>> 32;
862 ix
= hx
& 0x7fffffff;
863 /* |x| >= 1 or nan */
864 if (ix
>= 0x3ff00000) {
866 lx
= *(ULONGLONG
*)&x
;
867 if (((ix
- 0x3ff00000) | lx
) == 0)
868 /* asin(1) = +-pi/2 with inexact */
869 return x
* pio2_hi
+ 7.5231638452626401e-37;
870 if (isnan(x
)) return x
;
871 return math_error(_DOMAIN
, "asin", x
, 0, 0 / (x
- x
));
874 if (ix
< 0x3fe00000) {
875 /* if 0x1p-1022 <= |x| < 0x1p-26, avoid raising underflow */
876 if (ix
< 0x3e500000 && ix
>= 0x00100000)
878 return x
+ x
* asin_R(x
* x
);
881 z
= (1 - fabs(x
)) * 0.5;
884 if (ix
>= 0x3fef3333) { /* if |x| > 0.975 */
885 x
= pio2_hi
- (2 * (s
+ s
* r
) - pio2_lo
);
890 llx
= (*(ULONGLONG
*)&f
>> 32) << 32;
892 c
= (z
- f
* f
) / (s
+ f
);
893 x
= 0.5 * pio2_hi
- (2 * s
* r
- (pio2_lo
- 2 * c
) - (0.5 * pio2_hi
- 2 * f
));
900 /*********************************************************************
903 * Copied from musl: src/math/atan.c
905 double CDECL
atan( double x
)
907 static const double atanhi
[] = {
908 4.63647609000806093515e-01,
909 7.85398163397448278999e-01,
910 9.82793723247329054082e-01,
911 1.57079632679489655800e+00,
913 static const double atanlo
[] = {
914 2.26987774529616870924e-17,
915 3.06161699786838301793e-17,
916 1.39033110312309984516e-17,
917 6.12323399573676603587e-17,
919 static const double aT
[] = {
920 3.33333333333329318027e-01,
921 -1.99999999998764832476e-01,
922 1.42857142725034663711e-01,
923 -1.11111104054623557880e-01,
924 9.09088713343650656196e-02,
925 -7.69187620504482999495e-02,
926 6.66107313738753120669e-02,
927 -5.83357013379057348645e-02,
928 4.97687799461593236017e-02,
929 -3.65315727442169155270e-02,
930 1.62858201153657823623e-02,
934 unsigned int ix
, sign
;
938 if (isnan(x
)) return math_error(_DOMAIN
, "atan", x
, 0, x
);
941 ix
= *(ULONGLONG
*)&x
>> 32;
944 if (ix
>= 0x44100000) { /* if |x| >= 2^66 */
947 z
= atanhi
[3] + 7.5231638452626401e-37;
948 return sign
? -z
: z
;
950 if (ix
< 0x3fdc0000) { /* |x| < 0.4375 */
951 if (ix
< 0x3e400000) { /* |x| < 2^-27 */
953 /* raise underflow for subnormal x */
954 fp_barrierf((float)x
);
960 if (ix
< 0x3ff30000) { /* |x| < 1.1875 */
961 if (ix
< 0x3fe60000) { /* 7/16 <= |x| < 11/16 */
963 x
= (2.0 * x
- 1.0) / (2.0 + x
);
964 } else { /* 11/16 <= |x| < 19/16 */
966 x
= (x
- 1.0) / (x
+ 1.0);
969 if (ix
< 0x40038000) { /* |x| < 2.4375 */
971 x
= (x
- 1.5) / (1.0 + 1.5 * x
);
972 } else { /* 2.4375 <= |x| < 2^66 */
978 /* end of argument reduction */
981 /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
982 s1
= z
* (aT
[0] + w
* (aT
[2] + w
* (aT
[4] + w
* (aT
[6] + w
* (aT
[8] + w
* aT
[10])))));
983 s2
= w
* (aT
[1] + w
* (aT
[3] + w
* (aT
[5] + w
* (aT
[7] + w
* aT
[9]))));
985 return x
- x
* (s1
+ s2
);
986 z
= atanhi
[id
] - (x
* (s1
+ s2
) - atanlo
[id
] - x
);
987 return sign
? -z
: z
;
990 /*********************************************************************
993 * Copied from musl: src/math/atan2.c
995 double CDECL
atan2( double y
, double x
)
997 static const double pi
= 3.1415926535897931160E+00,
998 pi_lo
= 1.2246467991473531772E-16;
1001 unsigned int m
, lx
, ly
, ix
, iy
;
1003 if (isnan(x
) || isnan(y
))
1005 ix
= *(ULONGLONG
*)&x
>> 32;
1006 lx
= *(ULONGLONG
*)&x
;
1007 iy
= *(ULONGLONG
*)&y
>> 32;
1008 ly
= *(ULONGLONG
*)&y
;
1009 if (((ix
- 0x3ff00000) | lx
) == 0) /* x = 1.0 */
1011 m
= ((iy
>> 31) & 1) | ((ix
>> 30) & 2); /* 2*sign(x)+sign(y) */
1012 ix
= ix
& 0x7fffffff;
1013 iy
= iy
& 0x7fffffff;
1016 if ((iy
| ly
) == 0) {
1019 case 1: return y
; /* atan(+-0,+anything)=+-0 */
1020 case 2: return pi
; /* atan(+0,-anything) = pi */
1021 case 3: return -pi
; /* atan(-0,-anything) =-pi */
1026 return m
& 1 ? -pi
/ 2 : pi
/ 2;
1028 if (ix
== 0x7ff00000) {
1029 if (iy
== 0x7ff00000) {
1031 case 0: return pi
/ 4; /* atan(+INF,+INF) */
1032 case 1: return -pi
/ 4; /* atan(-INF,+INF) */
1033 case 2: return 3 * pi
/ 4; /* atan(+INF,-INF) */
1034 case 3: return -3 * pi
/ 4; /* atan(-INF,-INF) */
1038 case 0: return 0.0; /* atan(+...,+INF) */
1039 case 1: return -0.0; /* atan(-...,+INF) */
1040 case 2: return pi
; /* atan(+...,-INF) */
1041 case 3: return -pi
; /* atan(-...,-INF) */
1045 /* |y/x| > 0x1p64 */
1046 if (ix
+ (64 << 20) < iy
|| iy
== 0x7ff00000)
1047 return m
& 1 ? -pi
/ 2 : pi
/ 2;
1049 /* z = atan(|y/x|) without spurious underflow */
1050 if ((m
& 2) && iy
+ (64 << 20) < ix
) /* |y/x| < 0x1p-64, x<0 */
1053 z
= atan(fabs(y
/ x
));
1055 case 0: return z
; /* atan(+,+) */
1056 case 1: return -z
; /* atan(-,+) */
1057 case 2: return pi
- (z
- pi_lo
); /* atan(+,-) */
1058 default: /* case 3 */
1059 return (z
- pi_lo
) - pi
; /* atan(-,-) */
1063 /*********************************************************************
1066 double CDECL
cos( double x
)
1068 double ret
= unix_funcs
->cos( x
);
1069 if (!isfinite(x
)) return math_error(_DOMAIN
, "cos", x
, 0, ret
);
1073 /*********************************************************************
1076 double CDECL
cosh( double x
)
1078 double ret
= unix_funcs
->cosh( x
);
1079 if (isnan(x
)) return math_error(_DOMAIN
, "cosh", x
, 0, ret
);
1083 /*********************************************************************
1086 double CDECL
exp( double x
)
1088 double ret
= unix_funcs
->exp( x
);
1089 if (isnan(x
)) return math_error(_DOMAIN
, "exp", x
, 0, ret
);
1090 if (isfinite(x
) && !ret
) return math_error(_UNDERFLOW
, "exp", x
, 0, ret
);
1091 if (isfinite(x
) && !isfinite(ret
)) return math_error(_OVERFLOW
, "exp", x
, 0, ret
);
1095 /*********************************************************************
1098 double CDECL
fmod( double x
, double y
)
1100 double ret
= unix_funcs
->fmod( x
, y
);
1101 if (!isfinite(x
) || !isfinite(y
)) return math_error(_DOMAIN
, "fmod", x
, y
, ret
);
1105 /*********************************************************************
1108 double CDECL
log( double x
)
1110 double ret
= unix_funcs
->log( x
);
1111 if (x
< 0.0) return math_error(_DOMAIN
, "log", x
, 0, ret
);
1112 if (x
== 0.0) return math_error(_SING
, "log", x
, 0, ret
);
1116 /*********************************************************************
1119 double CDECL
log10( double x
)
1121 double ret
= unix_funcs
->log10( x
);
1122 if (x
< 0.0) return math_error(_DOMAIN
, "log10", x
, 0, ret
);
1123 if (x
== 0.0) return math_error(_SING
, "log10", x
, 0, ret
);
1127 /*********************************************************************
1130 double CDECL
pow( double x
, double y
)
1132 double z
= unix_funcs
->pow(x
,y
);
1133 if (x
< 0 && y
!= floor(y
))
1134 return math_error(_DOMAIN
, "pow", x
, y
, z
);
1135 if (!x
&& isfinite(y
) && y
< 0)
1136 return math_error(_SING
, "pow", x
, y
, z
);
1137 if (isfinite(x
) && isfinite(y
) && !isfinite(z
))
1138 return math_error(_OVERFLOW
, "pow", x
, y
, z
);
1139 if (x
&& isfinite(x
) && isfinite(y
) && !z
)
1140 return math_error(_UNDERFLOW
, "pow", x
, y
, z
);
1144 /*********************************************************************
1147 double CDECL
sin( double x
)
1149 double ret
= unix_funcs
->sin( x
);
1150 if (!isfinite(x
)) return math_error(_DOMAIN
, "sin", x
, 0, ret
);
1154 /*********************************************************************
1157 double CDECL
sinh( double x
)
1159 double ret
= unix_funcs
->sinh( x
);
1160 if (isnan(x
)) return math_error(_DOMAIN
, "sinh", x
, 0, ret
);
1164 /*********************************************************************
1167 * Copied from musl: src/math/sqrt.c
1169 double CDECL
sqrt( double x
)
1171 static const double tiny
= 1.0e-300;
1174 int sign
= 0x80000000;
1176 unsigned int r
,t1
,s1
,ix1
,q1
;
1179 ix
= *(ULONGLONG
*)&x
;
1183 /* take care of Inf and NaN */
1184 if (isnan(x
) || (isinf(x
) && x
> 0))
1187 /* take care of zero */
1189 if (((ix0
& ~sign
) | ix1
) == 0)
1190 return x
; /* sqrt(+-0) = +-0 */
1192 return math_error(_DOMAIN
, "sqrt", x
, 0, (x
- x
) / (x
- x
));
1196 if (m
== 0) { /* subnormal x */
1202 for (i
=0; (ix0
& 0x00100000) == 0; i
++)
1205 ix0
|= ix1
>> (32 - i
);
1208 m
-= 1023; /* unbias exponent */
1209 ix0
= (ix0
& 0x000fffff) | 0x00100000;
1210 if (m
& 1) { /* odd m, double x to make it even */
1211 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1214 m
>>= 1; /* m = [m/2] */
1216 /* generate sqrt(x) bit by bit */
1217 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1219 q
= q1
= s0
= s1
= 0; /* [q,q1] = sqrt(x) */
1220 r
= 0x00200000; /* r = moving bit from right to left */
1229 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1238 if (t
< ix0
|| (t
== ix0
&& t1
<= ix1
)) {
1240 if ((t1
&sign
) == sign
&& (s1
& sign
) == 0)
1248 ix0
+= ix0
+ ((ix1
& sign
) >> 31);
1253 /* use floating add to find out rounding direction */
1254 if ((ix0
| ix1
) != 0) {
1255 z
= 1.0 - tiny
; /* raise inexact flag */
1258 if (q1
== (unsigned int)0xffffffff) {
1261 } else if (z
> 1.0) {
1262 if (q1
== (unsigned int)0xfffffffe)
1269 ix0
= (q
>> 1) + 0x3fe00000;
1273 ix
= ix0
+ ((unsigned int)m
<< 20);
1276 return *(double*)&ix
;
1279 /*********************************************************************
1282 double CDECL
tan( double x
)
1284 double ret
= unix_funcs
->tan(x
);
1285 if (!isfinite(x
)) return math_error(_DOMAIN
, "tan", x
, 0, ret
);
1289 /*********************************************************************
1292 double CDECL
tanh( double x
)
1294 double ret
= unix_funcs
->tanh(x
);
1295 if (isnan(x
)) return math_error(_DOMAIN
, "tanh", x
, 0, ret
);
1300 #if (defined(__GNUC__) || defined(__clang__)) && defined(__i386__)
1302 #define CREATE_FPU_FUNC1(name, call) \
1303 __ASM_GLOBAL_FUNC(name, \
1305 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1306 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1307 "movl %esp, %ebp\n\t" \
1308 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1309 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1310 "fstpl (%esp)\n\t" /* store function argument */ \
1312 "movl $1, %ecx\n\t" /* empty FPU stack */ \
1316 "and $0x4500, %ax\n\t" \
1317 "cmp $0x4100, %ax\n\t" \
1319 "fstpl (%esp,%ecx,8)\n\t" \
1324 "movl %ecx, -4(%ebp)\n\t" \
1325 "call " __ASM_NAME( #call ) "\n\t" \
1326 "movl -4(%ebp), %ecx\n\t" \
1327 "fstpl (%esp)\n\t" /* save result */ \
1328 "3:\n\t" /* restore FPU stack */ \
1330 "fldl (%esp,%ecx,8)\n\t" \
1331 "cmpl $0, %ecx\n\t" \
1334 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1335 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1338 #define CREATE_FPU_FUNC2(name, call) \
1339 __ASM_GLOBAL_FUNC(name, \
1341 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
1342 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
1343 "movl %esp, %ebp\n\t" \
1344 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
1345 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
1346 "fstpl 8(%esp)\n\t" /* store function argument */ \
1348 "fstpl (%esp)\n\t" \
1350 "movl $2, %ecx\n\t" /* empty FPU stack */ \
1354 "and $0x4500, %ax\n\t" \
1355 "cmp $0x4100, %ax\n\t" \
1357 "fstpl (%esp,%ecx,8)\n\t" \
1362 "movl %ecx, -4(%ebp)\n\t" \
1363 "call " __ASM_NAME( #call ) "\n\t" \
1364 "movl -4(%ebp), %ecx\n\t" \
1365 "fstpl 8(%esp)\n\t" /* save result */ \
1366 "3:\n\t" /* restore FPU stack */ \
1368 "fldl (%esp,%ecx,8)\n\t" \
1369 "cmpl $1, %ecx\n\t" \
1372 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
1373 __ASM_CFI(".cfi_same_value %ebp\n\t") \
1376 CREATE_FPU_FUNC1(_CIacos
, acos
)
1377 CREATE_FPU_FUNC1(_CIasin
, asin
)
1378 CREATE_FPU_FUNC1(_CIatan
, atan
)
1379 CREATE_FPU_FUNC2(_CIatan2
, atan2
)
1380 CREATE_FPU_FUNC1(_CIcos
, cos
)
1381 CREATE_FPU_FUNC1(_CIcosh
, cosh
)
1382 CREATE_FPU_FUNC1(_CIexp
, exp
)
1383 CREATE_FPU_FUNC2(_CIfmod
, fmod
)
1384 CREATE_FPU_FUNC1(_CIlog
, log
)
1385 CREATE_FPU_FUNC1(_CIlog10
, log10
)
1386 CREATE_FPU_FUNC2(_CIpow
, pow
)
1387 CREATE_FPU_FUNC1(_CIsin
, sin
)
1388 CREATE_FPU_FUNC1(_CIsinh
, sinh
)
1389 CREATE_FPU_FUNC1(_CIsqrt
, sqrt
)
1390 CREATE_FPU_FUNC1(_CItan
, tan
)
1391 CREATE_FPU_FUNC1(_CItanh
, tanh
)
1393 __ASM_GLOBAL_FUNC(_ftol
,
1395 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
1396 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
1397 "movl %esp, %ebp\n\t"
1398 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
1399 "subl $12, %esp\n\t" /* sizeof(LONGLONG) + 2*sizeof(WORD) */
1401 "mov (%esp), %ax\n\t"
1402 "or $0xc00, %ax\n\t"
1403 "mov %ax, 2(%esp)\n\t"
1405 "fistpq 4(%esp)\n\t"
1407 "movl 4(%esp), %eax\n\t"
1408 "movl 8(%esp), %edx\n\t"
1410 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
1411 __ASM_CFI(".cfi_same_value %ebp\n\t")
1414 #endif /* (defined(__GNUC__) || defined(__clang__)) && defined(__i386__) */
1416 /*********************************************************************
1417 * _fpclass (MSVCRT.@)
1419 int CDECL
_fpclass(double num
)
1421 union { double f
; UINT64 i
; } u
= { num
};
1422 int e
= u
.i
>> 52 & 0x7ff;
1428 if (u
.i
<< 1) return s
? _FPCLASS_ND
: _FPCLASS_PD
;
1429 return s
? _FPCLASS_NZ
: _FPCLASS_PZ
;
1431 if (u
.i
<< 12) return ((u
.i
>> 51) & 1) ? _FPCLASS_QNAN
: _FPCLASS_SNAN
;
1432 return s
? _FPCLASS_NINF
: _FPCLASS_PINF
;
1434 return s
? _FPCLASS_NN
: _FPCLASS_PN
;
1438 /*********************************************************************
1441 unsigned int CDECL
MSVCRT__rotl(unsigned int num
, int shift
)
1444 return (num
<< shift
) | (num
>> (32-shift
));
1447 /*********************************************************************
1450 __msvcrt_ulong CDECL
MSVCRT__lrotl(__msvcrt_ulong num
, int shift
)
1453 return (num
<< shift
) | (num
>> (32-shift
));
1456 /*********************************************************************
1459 __msvcrt_ulong CDECL
MSVCRT__lrotr(__msvcrt_ulong num
, int shift
)
1462 return (num
>> shift
) | (num
<< (32-shift
));
1465 /*********************************************************************
1468 unsigned int CDECL
MSVCRT__rotr(unsigned int num
, int shift
)
1471 return (num
>> shift
) | (num
<< (32-shift
));
1474 /*********************************************************************
1475 * _rotl64 (MSVCRT.@)
1477 unsigned __int64 CDECL
MSVCRT__rotl64(unsigned __int64 num
, int shift
)
1480 return (num
<< shift
) | (num
>> (64-shift
));
1483 /*********************************************************************
1484 * _rotr64 (MSVCRT.@)
1486 unsigned __int64 CDECL
MSVCRT__rotr64(unsigned __int64 num
, int shift
)
1489 return (num
>> shift
) | (num
<< (64-shift
));
1492 /*********************************************************************
1495 int CDECL
abs( int n
)
1497 return n
>= 0 ? n
: -n
;
1500 /*********************************************************************
1503 __msvcrt_long CDECL
labs( __msvcrt_long n
)
1505 return n
>= 0 ? n
: -n
;
1509 /*********************************************************************
1510 * llabs (MSVCR100.@)
1512 __int64 CDECL
llabs( __int64 n
)
1514 return n
>= 0 ? n
: -n
;
1519 /*********************************************************************
1520 * imaxabs (MSVCR120.@)
1522 intmax_t CDECL
imaxabs( intmax_t n
)
1524 return n
>= 0 ? n
: -n
;
1528 /*********************************************************************
1531 __int64 CDECL
_abs64( __int64 n
)
1533 return n
>= 0 ? n
: -n
;
1536 /*********************************************************************
1539 double CDECL
_logb(double num
)
1541 double ret
= unix_funcs
->logb(num
);
1542 if (isnan(num
)) return math_error(_DOMAIN
, "_logb", num
, 0, ret
);
1543 if (!num
) return math_error(_SING
, "_logb", num
, 0, ret
);
1547 /*********************************************************************
1550 double CDECL
_hypot(double x
, double y
)
1552 /* FIXME: errno handling */
1553 return unix_funcs
->hypot( x
, y
);
1556 /*********************************************************************
1557 * _hypotf (MSVCRT.@)
1559 float CDECL
_hypotf(float x
, float y
)
1561 /* FIXME: errno handling */
1562 return unix_funcs
->hypotf( x
, y
);
1565 /*********************************************************************
1568 double CDECL
ceil( double x
)
1570 return unix_funcs
->ceil(x
);
1573 /*********************************************************************
1576 double CDECL
floor( double x
)
1578 return unix_funcs
->floor(x
);
1581 /*********************************************************************
1584 double CDECL
fma( double x
, double y
, double z
)
1586 double w
= unix_funcs
->fma(x
, y
, z
);
1587 if ((isinf(x
) && y
== 0) || (x
== 0 && isinf(y
))) *_errno() = EDOM
;
1588 else if (isinf(x
) && isinf(z
) && x
!= z
) *_errno() = EDOM
;
1589 else if (isinf(y
) && isinf(z
) && y
!= z
) *_errno() = EDOM
;
1593 /*********************************************************************
1596 float CDECL
fmaf( float x
, float y
, float z
)
1598 float w
= unix_funcs
->fmaf(x
, y
, z
);
1599 if ((isinf(x
) && y
== 0) || (x
== 0 && isinf(y
))) *_errno() = EDOM
;
1600 else if (isinf(x
) && isinf(z
) && x
!= z
) *_errno() = EDOM
;
1601 else if (isinf(y
) && isinf(z
) && y
!= z
) *_errno() = EDOM
;
1605 /*********************************************************************
1608 * Copied from musl: src/math/fabsf.c
1610 double CDECL
fabs( double x
)
1612 union { double f
; UINT64 i
; } u
= { x
};
1617 /*********************************************************************
1620 double CDECL
frexp( double x
, int *exp
)
1622 return unix_funcs
->frexp( x
, exp
);
1625 /*********************************************************************
1628 double CDECL
modf( double x
, double *iptr
)
1630 return unix_funcs
->modf( x
, iptr
);
1633 /**********************************************************************
1634 * _statusfp2 (MSVCRT.@)
1636 * Not exported by native msvcrt, added in msvcr80.
1638 #if defined(__i386__) || defined(__x86_64__)
1639 void CDECL
_statusfp2( unsigned int *x86_sw
, unsigned int *sse2_sw
)
1641 #if defined(__GNUC__) || defined(__clang__)
1643 unsigned long fpword
;
1647 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) );
1649 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1650 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1651 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1652 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1653 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1654 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1658 if (!sse2_sw
) return;
1662 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1664 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1665 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1666 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1667 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1668 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1669 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1674 FIXME( "not implemented\n" );
1679 /**********************************************************************
1680 * _statusfp (MSVCRT.@)
1682 unsigned int CDECL
_statusfp(void)
1684 unsigned int flags
= 0;
1685 #if defined(__i386__) || defined(__x86_64__)
1686 unsigned int x86_sw
, sse2_sw
;
1688 _statusfp2( &x86_sw
, &sse2_sw
);
1689 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
1690 flags
= x86_sw
| sse2_sw
;
1691 #elif defined(__aarch64__)
1694 __asm__
__volatile__( "mrs %0, fpsr" : "=r" (fpsr
) );
1695 if (fpsr
& 0x1) flags
|= _SW_INVALID
;
1696 if (fpsr
& 0x2) flags
|= _SW_ZERODIVIDE
;
1697 if (fpsr
& 0x4) flags
|= _SW_OVERFLOW
;
1698 if (fpsr
& 0x8) flags
|= _SW_UNDERFLOW
;
1699 if (fpsr
& 0x10) flags
|= _SW_INEXACT
;
1700 if (fpsr
& 0x80) flags
|= _SW_DENORMAL
;
1702 FIXME( "not implemented\n" );
1707 /*********************************************************************
1708 * _clearfp (MSVCRT.@)
1710 unsigned int CDECL
_clearfp(void)
1712 unsigned int flags
= 0;
1713 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
1714 unsigned long fpword
;
1716 __asm__
__volatile__( "fnstsw %0; fnclex" : "=m" (fpword
) );
1717 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1718 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1719 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1720 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1721 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1722 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1726 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1727 if (fpword
& 0x1) flags
|= _SW_INVALID
;
1728 if (fpword
& 0x2) flags
|= _SW_DENORMAL
;
1729 if (fpword
& 0x4) flags
|= _SW_ZERODIVIDE
;
1730 if (fpword
& 0x8) flags
|= _SW_OVERFLOW
;
1731 if (fpword
& 0x10) flags
|= _SW_UNDERFLOW
;
1732 if (fpword
& 0x20) flags
|= _SW_INEXACT
;
1734 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1736 #elif defined(__aarch64__)
1739 __asm__
__volatile__( "mrs %0, fpsr" : "=r" (fpsr
) );
1740 if (fpsr
& 0x1) flags
|= _SW_INVALID
;
1741 if (fpsr
& 0x2) flags
|= _SW_ZERODIVIDE
;
1742 if (fpsr
& 0x4) flags
|= _SW_OVERFLOW
;
1743 if (fpsr
& 0x8) flags
|= _SW_UNDERFLOW
;
1744 if (fpsr
& 0x10) flags
|= _SW_INEXACT
;
1745 if (fpsr
& 0x80) flags
|= _SW_DENORMAL
;
1747 __asm__
__volatile__( "msr fpsr, %0" :: "r" (fpsr
) );
1749 FIXME( "not implemented\n" );
1754 /*********************************************************************
1755 * __fpecode (MSVCRT.@)
1757 int * CDECL
__fpecode(void)
1759 return &msvcrt_get_thread_data()->fpecode
;
1762 /*********************************************************************
1765 double CDECL
ldexp(double num
, int exp
)
1767 double z
= unix_funcs
->ldexp(num
,exp
);
1769 if (isfinite(num
) && !isfinite(z
))
1770 return math_error(_OVERFLOW
, "ldexp", num
, exp
, z
);
1771 if (num
&& isfinite(num
) && !z
)
1772 return math_error(_UNDERFLOW
, "ldexp", num
, exp
, z
);
1773 if (z
== 0 && signbit(z
))
1774 z
= 0.0; /* Convert -0 -> +0 */
1778 /*********************************************************************
1781 double CDECL
_cabs(struct _complex num
)
1783 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
1786 /*********************************************************************
1787 * _chgsign (MSVCRT.@)
1789 double CDECL
_chgsign(double num
)
1791 union { double f
; UINT64 i
; } u
= { num
};
1796 /*********************************************************************
1797 * __control87_2 (MSVCR80.@)
1799 * Not exported by native msvcrt, added in msvcr80.
1802 int CDECL
__control87_2( unsigned int newval
, unsigned int mask
,
1803 unsigned int *x86_cw
, unsigned int *sse2_cw
)
1805 #if defined(__GNUC__) || defined(__clang__)
1806 unsigned long fpword
;
1808 unsigned int old_flags
;
1812 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
1814 /* Convert into mask constants */
1816 if (fpword
& 0x1) flags
|= _EM_INVALID
;
1817 if (fpword
& 0x2) flags
|= _EM_DENORMAL
;
1818 if (fpword
& 0x4) flags
|= _EM_ZERODIVIDE
;
1819 if (fpword
& 0x8) flags
|= _EM_OVERFLOW
;
1820 if (fpword
& 0x10) flags
|= _EM_UNDERFLOW
;
1821 if (fpword
& 0x20) flags
|= _EM_INEXACT
;
1822 switch (fpword
& 0xc00)
1824 case 0xc00: flags
|= _RC_UP
|_RC_DOWN
; break;
1825 case 0x800: flags
|= _RC_UP
; break;
1826 case 0x400: flags
|= _RC_DOWN
; break;
1828 switch (fpword
& 0x300)
1830 case 0x0: flags
|= _PC_24
; break;
1831 case 0x200: flags
|= _PC_53
; break;
1832 case 0x300: flags
|= _PC_64
; break;
1834 if (fpword
& 0x1000) flags
|= _IC_AFFINE
;
1836 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1839 flags
= (flags
& ~mask
) | (newval
& mask
);
1841 /* Convert (masked) value back to fp word */
1843 if (flags
& _EM_INVALID
) fpword
|= 0x1;
1844 if (flags
& _EM_DENORMAL
) fpword
|= 0x2;
1845 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x4;
1846 if (flags
& _EM_OVERFLOW
) fpword
|= 0x8;
1847 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x10;
1848 if (flags
& _EM_INEXACT
) fpword
|= 0x20;
1849 switch (flags
& _MCW_RC
)
1851 case _RC_UP
|_RC_DOWN
: fpword
|= 0xc00; break;
1852 case _RC_UP
: fpword
|= 0x800; break;
1853 case _RC_DOWN
: fpword
|= 0x400; break;
1855 switch (flags
& _MCW_PC
)
1857 case _PC_64
: fpword
|= 0x300; break;
1858 case _PC_53
: fpword
|= 0x200; break;
1859 case _PC_24
: fpword
|= 0x0; break;
1861 if (flags
& _IC_AFFINE
) fpword
|= 0x1000;
1863 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
1868 if (!sse2_cw
) return 1;
1872 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1874 /* Convert into mask constants */
1876 if (fpword
& 0x80) flags
|= _EM_INVALID
;
1877 if (fpword
& 0x100) flags
|= _EM_DENORMAL
;
1878 if (fpword
& 0x200) flags
|= _EM_ZERODIVIDE
;
1879 if (fpword
& 0x400) flags
|= _EM_OVERFLOW
;
1880 if (fpword
& 0x800) flags
|= _EM_UNDERFLOW
;
1881 if (fpword
& 0x1000) flags
|= _EM_INEXACT
;
1882 switch (fpword
& 0x6000)
1884 case 0x6000: flags
|= _RC_UP
|_RC_DOWN
; break;
1885 case 0x4000: flags
|= _RC_UP
; break;
1886 case 0x2000: flags
|= _RC_DOWN
; break;
1888 switch (fpword
& 0x8040)
1890 case 0x0040: flags
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
1891 case 0x8000: flags
|= _DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
1892 case 0x8040: flags
|= _DN_FLUSH
; break;
1895 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1899 mask
&= _MCW_EM
| _MCW_RC
| _MCW_DN
;
1900 flags
= (flags
& ~mask
) | (newval
& mask
);
1902 if (flags
!= old_flags
)
1904 /* Convert (masked) value back to fp word */
1906 if (flags
& _EM_INVALID
) fpword
|= 0x80;
1907 if (flags
& _EM_DENORMAL
) fpword
|= 0x100;
1908 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x200;
1909 if (flags
& _EM_OVERFLOW
) fpword
|= 0x400;
1910 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x800;
1911 if (flags
& _EM_INEXACT
) fpword
|= 0x1000;
1912 switch (flags
& _MCW_RC
)
1914 case _RC_UP
|_RC_DOWN
: fpword
|= 0x6000; break;
1915 case _RC_UP
: fpword
|= 0x4000; break;
1916 case _RC_DOWN
: fpword
|= 0x2000; break;
1918 switch (flags
& _MCW_DN
)
1920 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
1921 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
1922 case _DN_FLUSH
: fpword
|= 0x8040; break;
1924 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1933 FIXME( "not implemented\n" );
1939 /*********************************************************************
1940 * _control87 (MSVCRT.@)
1942 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
1944 unsigned int flags
= 0;
1946 unsigned int sse2_cw
;
1948 __control87_2( newval
, mask
, &flags
, &sse2_cw
);
1950 if ((flags
^ sse2_cw
) & (_MCW_EM
| _MCW_RC
)) flags
|= _EM_AMBIGUOUS
;
1952 #elif defined(__x86_64__)
1953 unsigned long fpword
;
1954 unsigned int old_flags
;
1956 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1957 if (fpword
& 0x80) flags
|= _EM_INVALID
;
1958 if (fpword
& 0x100) flags
|= _EM_DENORMAL
;
1959 if (fpword
& 0x200) flags
|= _EM_ZERODIVIDE
;
1960 if (fpword
& 0x400) flags
|= _EM_OVERFLOW
;
1961 if (fpword
& 0x800) flags
|= _EM_UNDERFLOW
;
1962 if (fpword
& 0x1000) flags
|= _EM_INEXACT
;
1963 switch (fpword
& 0x6000)
1965 case 0x6000: flags
|= _RC_CHOP
; break;
1966 case 0x4000: flags
|= _RC_UP
; break;
1967 case 0x2000: flags
|= _RC_DOWN
; break;
1969 switch (fpword
& 0x8040)
1971 case 0x0040: flags
|= _DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
1972 case 0x8000: flags
|= _DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
1973 case 0x8040: flags
|= _DN_FLUSH
; break;
1976 mask
&= _MCW_EM
| _MCW_RC
| _MCW_DN
;
1977 flags
= (flags
& ~mask
) | (newval
& mask
);
1978 if (flags
!= old_flags
)
1981 if (flags
& _EM_INVALID
) fpword
|= 0x80;
1982 if (flags
& _EM_DENORMAL
) fpword
|= 0x100;
1983 if (flags
& _EM_ZERODIVIDE
) fpword
|= 0x200;
1984 if (flags
& _EM_OVERFLOW
) fpword
|= 0x400;
1985 if (flags
& _EM_UNDERFLOW
) fpword
|= 0x800;
1986 if (flags
& _EM_INEXACT
) fpword
|= 0x1000;
1987 switch (flags
& _MCW_RC
)
1989 case _RC_CHOP
: fpword
|= 0x6000; break;
1990 case _RC_UP
: fpword
|= 0x4000; break;
1991 case _RC_DOWN
: fpword
|= 0x2000; break;
1993 switch (flags
& _MCW_DN
)
1995 case _DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
1996 case _DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
1997 case _DN_FLUSH
: fpword
|= 0x8040; break;
1999 __asm__
__volatile__( "ldmxcsr %0" :: "m" (fpword
) );
2001 #elif defined(__aarch64__)
2004 __asm__
__volatile__( "mrs %0, fpcr" : "=r" (fpcr
) );
2005 if (!(fpcr
& 0x100)) flags
|= _EM_INVALID
;
2006 if (!(fpcr
& 0x200)) flags
|= _EM_ZERODIVIDE
;
2007 if (!(fpcr
& 0x400)) flags
|= _EM_OVERFLOW
;
2008 if (!(fpcr
& 0x800)) flags
|= _EM_UNDERFLOW
;
2009 if (!(fpcr
& 0x1000)) flags
|= _EM_INEXACT
;
2010 if (!(fpcr
& 0x8000)) flags
|= _EM_DENORMAL
;
2011 switch (fpcr
& 0xc00000)
2013 case 0x400000: flags
|= _RC_UP
; break;
2014 case 0x800000: flags
|= _RC_DOWN
; break;
2015 case 0xc00000: flags
|= _RC_CHOP
; break;
2017 flags
= (flags
& ~mask
) | (newval
& mask
);
2018 fpcr
&= ~0xc09f00ul
;
2019 if (!(flags
& _EM_INVALID
)) fpcr
|= 0x100;
2020 if (!(flags
& _EM_ZERODIVIDE
)) fpcr
|= 0x200;
2021 if (!(flags
& _EM_OVERFLOW
)) fpcr
|= 0x400;
2022 if (!(flags
& _EM_UNDERFLOW
)) fpcr
|= 0x800;
2023 if (!(flags
& _EM_INEXACT
)) fpcr
|= 0x1000;
2024 if (!(flags
& _EM_DENORMAL
)) fpcr
|= 0x8000;
2025 switch (flags
& _MCW_RC
)
2027 case _RC_CHOP
: fpcr
|= 0xc00000; break;
2028 case _RC_UP
: fpcr
|= 0x400000; break;
2029 case _RC_DOWN
: fpcr
|= 0x800000; break;
2031 __asm__
__volatile__( "msr fpcr, %0" :: "r" (fpcr
) );
2033 FIXME( "not implemented\n" );
2038 /*********************************************************************
2039 * _controlfp (MSVCRT.@)
2041 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
2043 return _control87( newval
, mask
& ~_EM_DENORMAL
);
2046 /*********************************************************************
2047 * _set_controlfp (MSVCRT.@)
2049 void CDECL
_set_controlfp( unsigned int newval
, unsigned int mask
)
2051 _controlfp( newval
, mask
);
2054 /*********************************************************************
2055 * _controlfp_s (MSVCRT.@)
2057 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
2059 static const unsigned int all_flags
= (_MCW_EM
| _MCW_IC
| _MCW_RC
|
2063 if (!MSVCRT_CHECK_PMT( !(newval
& mask
& ~all_flags
) ))
2065 if (cur
) *cur
= _controlfp( 0, 0 ); /* retrieve it anyway */
2068 val
= _controlfp( newval
, mask
);
2069 if (cur
) *cur
= val
;
2074 /*********************************************************************
2075 * fegetenv (MSVCR120.@)
2077 int CDECL
fegetenv(fenv_t
*env
)
2079 env
->_Fe_ctl
= _controlfp(0, 0) & (_EM_INEXACT
| _EM_UNDERFLOW
|
2080 _EM_OVERFLOW
| _EM_ZERODIVIDE
| _EM_INVALID
| _RC_CHOP
);
2081 env
->_Fe_stat
= _statusfp();
2087 /*********************************************************************
2088 * __fpe_flt_rounds (UCRTBASE.@)
2090 int CDECL
__fpe_flt_rounds(void)
2092 unsigned int fpc
= _controlfp(0, 0) & _RC_CHOP
;
2097 case _RC_CHOP
: return 0;
2098 case _RC_NEAR
: return 1;
2099 case _RC_UP
: return 2;
2107 /*********************************************************************
2108 * fegetround (MSVCR120.@)
2110 int CDECL
fegetround(void)
2112 return _controlfp(0, 0) & _RC_CHOP
;
2115 /*********************************************************************
2116 * fesetround (MSVCR120.@)
2118 int CDECL
fesetround(int round_mode
)
2120 if (round_mode
& (~_RC_CHOP
))
2122 _controlfp(round_mode
, _RC_CHOP
);
2126 #endif /* _MSVCR_VER>=120 */
2128 /*********************************************************************
2129 * _copysign (MSVCRT.@)
2131 * Copied from musl: src/math/copysign.c
2133 double CDECL
_copysign( double x
, double y
)
2135 union { double f
; UINT64 i
; } ux
= { x
}, uy
= { y
};
2137 ux
.i
|= uy
.i
& 1ull << 63;
2141 /*********************************************************************
2142 * _finite (MSVCRT.@)
2144 int CDECL
_finite(double num
)
2146 union { double f
; UINT64 i
; } u
= { num
};
2147 return (u
.i
& ~0ull >> 1) < 0x7ffull
<< 52;
2150 /*********************************************************************
2151 * _fpreset (MSVCRT.@)
2153 void CDECL
_fpreset(void)
2155 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2156 const unsigned int x86_cw
= 0x27f;
2157 __asm__
__volatile__( "fninit; fldcw %0" : : "m" (x86_cw
) );
2160 const unsigned long sse2_cw
= 0x1f80;
2161 __asm__
__volatile__( "ldmxcsr %0" : : "m" (sse2_cw
) );
2164 FIXME( "not implemented\n" );
2169 /*********************************************************************
2170 * fesetenv (MSVCR120.@)
2172 int CDECL
fesetenv(const fenv_t
*env
)
2174 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
2182 DWORD instruction_pointer
;
2190 TRACE( "(%p)\n", env
);
2192 if (!env
->_Fe_ctl
&& !env
->_Fe_stat
) {
2197 __asm__
__volatile__( "fnstenv %0" : "=m" (fenv
) );
2199 fenv
.control_word
&= ~0xc3d;
2200 if (env
->_Fe_ctl
& _EM_INVALID
) fenv
.control_word
|= 0x1;
2201 if (env
->_Fe_ctl
& _EM_ZERODIVIDE
) fenv
.control_word
|= 0x4;
2202 if (env
->_Fe_ctl
& _EM_OVERFLOW
) fenv
.control_word
|= 0x8;
2203 if (env
->_Fe_ctl
& _EM_UNDERFLOW
) fenv
.control_word
|= 0x10;
2204 if (env
->_Fe_ctl
& _EM_INEXACT
) fenv
.control_word
|= 0x20;
2205 switch (env
->_Fe_ctl
& _MCW_RC
)
2207 case _RC_UP
|_RC_DOWN
: fenv
.control_word
|= 0xc00; break;
2208 case _RC_UP
: fenv
.control_word
|= 0x800; break;
2209 case _RC_DOWN
: fenv
.control_word
|= 0x400; break;
2212 fenv
.status_word
&= ~0x3d;
2213 if (env
->_Fe_stat
& FE_INVALID
) fenv
.status_word
|= 0x1;
2214 if (env
->_Fe_stat
& FE_DIVBYZERO
) fenv
.status_word
|= 0x4;
2215 if (env
->_Fe_stat
& FE_OVERFLOW
) fenv
.status_word
|= 0x8;
2216 if (env
->_Fe_stat
& FE_UNDERFLOW
) fenv
.status_word
|= 0x10;
2217 if (env
->_Fe_stat
& FE_INEXACT
) fenv
.status_word
|= 0x20;
2219 __asm__
__volatile__( "fldenv %0" : : "m" (fenv
) : "st", "st(1)",
2220 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
2225 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
2227 if (env
->_Fe_ctl
& _EM_INVALID
) fpword
|= 0x80;
2228 if (env
->_Fe_ctl
& _EM_ZERODIVIDE
) fpword
|= 0x200;
2229 if (env
->_Fe_ctl
& _EM_OVERFLOW
) fpword
|= 0x400;
2230 if (env
->_Fe_ctl
& _EM_UNDERFLOW
) fpword
|= 0x800;
2231 if (env
->_Fe_ctl
& _EM_INEXACT
) fpword
|= 0x1000;
2232 switch (env
->_Fe_ctl
& _MCW_RC
)
2234 case _RC_CHOP
: fpword
|= 0x6000; break;
2235 case _RC_UP
: fpword
|= 0x4000; break;
2236 case _RC_DOWN
: fpword
|= 0x2000; break;
2238 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
2243 FIXME( "not implemented\n" );
2249 /*********************************************************************
2252 int CDECL
_isnan(double num
)
2254 union { double f
; UINT64 i
; } u
= { num
};
2255 return (u
.i
& ~0ull >> 1) > 0x7ffull
<< 52;
2258 /*********************************************************************
2261 double CDECL
_j0(double num
)
2263 /* FIXME: errno handling */
2264 return unix_funcs
->j0( num
);
2267 /*********************************************************************
2270 double CDECL
_j1(double num
)
2272 /* FIXME: errno handling */
2273 return unix_funcs
->j1( num
);
2276 /*********************************************************************
2279 double CDECL
_jn(int n
, double num
)
2281 /* FIXME: errno handling */
2282 return unix_funcs
->jn( n
, num
);
2285 /*********************************************************************
2288 double CDECL
_y0(double num
)
2292 if (!isfinite(num
)) *_errno() = EDOM
;
2293 retval
= unix_funcs
->y0( num
);
2294 if (_fpclass(retval
) == _FPCLASS_NINF
)
2302 /*********************************************************************
2305 double CDECL
_y1(double num
)
2309 if (!isfinite(num
)) *_errno() = EDOM
;
2310 retval
= unix_funcs
->y1( num
);
2311 if (_fpclass(retval
) == _FPCLASS_NINF
)
2319 /*********************************************************************
2322 double CDECL
_yn(int order
, double num
)
2326 if (!isfinite(num
)) *_errno() = EDOM
;
2327 retval
= unix_funcs
->yn( order
, num
);
2328 if (_fpclass(retval
) == _FPCLASS_NINF
)
2338 /*********************************************************************
2339 * _nearbyint (MSVCR120.@)
2341 double CDECL
nearbyint(double num
)
2343 return unix_funcs
->nearbyint( num
);
2346 /*********************************************************************
2347 * _nearbyintf (MSVCR120.@)
2349 float CDECL
nearbyintf(float num
)
2351 return unix_funcs
->nearbyintf( num
);
2354 /*********************************************************************
2355 * nexttoward (MSVCR120.@)
2357 double CDECL
MSVCRT_nexttoward(double num
, double next
)
2359 double ret
= unix_funcs
->nexttoward(num
, next
);
2360 if (!(_fpclass(ret
) & (_FPCLASS_PN
| _FPCLASS_NN
2361 | _FPCLASS_SNAN
| _FPCLASS_QNAN
)) && !isinf(num
))
2368 /*********************************************************************
2369 * nexttowardf (MSVCR120.@)
2371 float CDECL
MSVCRT_nexttowardf(float num
, double next
)
2373 float ret
= unix_funcs
->nexttowardf( num
, next
);
2374 if (!(_fpclass(ret
) & (_FPCLASS_PN
| _FPCLASS_NN
2375 | _FPCLASS_SNAN
| _FPCLASS_QNAN
)) && !isinf(num
))
2382 #endif /* _MSVCR_VER>=120 */
2384 /*********************************************************************
2385 * _nextafter (MSVCRT.@)
2387 double CDECL
_nextafter(double num
, double next
)
2390 if (!isfinite(num
) || !isfinite(next
)) *_errno() = EDOM
;
2391 retval
= unix_funcs
->nextafter(num
,next
);
2395 /*********************************************************************
2398 char * CDECL
_ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
2401 thread_data_t
*data
= msvcrt_get_thread_data();
2402 /* FIXME: check better for overflow (native supports over 300 chars) */
2403 ndigits
= min( ndigits
, 80 - 7); /* 7 : space for dec point, 1 for "e",
2404 * 4 for exponent and one for
2405 * terminating '\0' */
2406 if (!data
->efcvt_buffer
)
2407 data
->efcvt_buffer
= malloc( 80 ); /* ought to be enough */
2414 /* handle cases with zero ndigits or less */
2416 if( prec
< 1) prec
= 2;
2417 len
= _snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
2418 /* take the decimal "point away */
2420 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
2421 /* take the exponential "e" out */
2422 data
->efcvt_buffer
[ prec
] = '\0';
2423 /* read the exponent */
2424 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
2426 /* adjust for some border cases */
2427 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
2429 /* handle cases with zero ndigits or less */
2431 if( data
->efcvt_buffer
[ 0] >= '5')
2433 data
->efcvt_buffer
[ 0] = '\0';
2435 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
2436 return data
->efcvt_buffer
;
2439 /*********************************************************************
2440 * _ecvt_s (MSVCRT.@)
2442 int CDECL
_ecvt_s( char *buffer
, size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
2446 const char infret
[] = "1#INF";
2448 if (!MSVCRT_CHECK_PMT(buffer
!= NULL
)) return EINVAL
;
2449 if (!MSVCRT_CHECK_PMT(decpt
!= NULL
)) return EINVAL
;
2450 if (!MSVCRT_CHECK_PMT(sign
!= NULL
)) return EINVAL
;
2451 if (!MSVCRT_CHECK_PMT_ERR( length
> 2, ERANGE
)) return ERANGE
;
2452 if (!MSVCRT_CHECK_PMT_ERR(ndigits
< (int)length
- 1, ERANGE
)) return ERANGE
;
2454 /* special case - inf */
2455 if(number
== HUGE_VAL
|| number
== -HUGE_VAL
)
2457 memset(buffer
, '0', ndigits
);
2458 memcpy(buffer
, infret
, min(ndigits
, sizeof(infret
) - 1 ) );
2459 buffer
[ndigits
] = '\0';
2461 if(number
== -HUGE_VAL
)
2467 /* handle cases with zero ndigits or less */
2469 if( prec
< 1) prec
= 2;
2470 result
= malloc(prec
+ 7);
2477 len
= _snprintf(result
, prec
+ 7, "%.*le", prec
- 1, number
);
2478 /* take the decimal "point away */
2480 memmove( result
+ 1, result
+ 2, len
- 1 );
2481 /* take the exponential "e" out */
2482 result
[ prec
] = '\0';
2483 /* read the exponent */
2484 sscanf( result
+ prec
+ 1, "%d", decpt
);
2486 /* adjust for some border cases */
2487 if( result
[0] == '0')/* value is zero */
2489 /* handle cases with zero ndigits or less */
2491 if( result
[ 0] >= '5')
2495 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
2500 /***********************************************************************
2503 char * CDECL
_fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
2505 thread_data_t
*data
= msvcrt_get_thread_data();
2506 int stop
, dec1
, dec2
;
2507 char *ptr1
, *ptr2
, *first
;
2508 char buf
[80]; /* ought to be enough */
2509 char decimal_separator
= get_locinfo()->lconv
->decimal_point
[0];
2511 if (!data
->efcvt_buffer
)
2512 data
->efcvt_buffer
= malloc( 80 ); /* ought to be enough */
2520 stop
= _snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
2522 ptr2
= data
->efcvt_buffer
;
2527 /* For numbers below the requested resolution, work out where
2528 the decimal point will be rather than finding it in the string */
2529 if (number
< 1.0 && number
> 0.0) {
2530 dec2
= log10(number
+ 1e-10);
2531 if (-dec2
<= ndigits
) dec2
= 0;
2534 /* If requested digits is zero or less, we will need to truncate
2535 * the returned string */
2540 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
2541 while (*ptr1
!= '\0' && *ptr1
!= decimal_separator
) {
2542 if (!first
) first
= ptr2
;
2543 if ((ptr1
- buf
) < stop
) {
2554 while (*ptr1
== '0') { /* Process leading zeroes */
2559 while (*ptr1
!= '\0') {
2560 if (!first
) first
= ptr2
;
2567 /* We never found a non-zero digit, then our number is either
2568 * smaller than the requested precision, or 0.0 */
2573 first
= data
->efcvt_buffer
;
2578 *decpt
= dec2
? dec2
: dec1
;
2582 /***********************************************************************
2583 * _fcvt_s (MSVCRT.@)
2585 int CDECL
_fcvt_s(char* outbuffer
, size_t size
, double number
, int ndigits
, int *decpt
, int *sign
)
2587 int stop
, dec1
, dec2
;
2588 char *ptr1
, *ptr2
, *first
;
2589 char buf
[80]; /* ought to be enough */
2590 char decimal_separator
= get_locinfo()->lconv
->decimal_point
[0];
2592 if (!outbuffer
|| !decpt
|| !sign
|| size
== 0)
2604 stop
= _snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
2611 /* For numbers below the requested resolution, work out where
2612 the decimal point will be rather than finding it in the string */
2613 if (number
< 1.0 && number
> 0.0) {
2614 dec2
= log10(number
+ 1e-10);
2615 if (-dec2
<= ndigits
) dec2
= 0;
2618 /* If requested digits is zero or less, we will need to truncate
2619 * the returned string */
2624 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
2625 while (*ptr1
!= '\0' && *ptr1
!= decimal_separator
) {
2626 if (!first
) first
= ptr2
;
2627 if ((ptr1
- buf
) < stop
) {
2641 while (*ptr1
== '0') { /* Process leading zeroes */
2642 if (number
== 0.0 && size
> 1) {
2650 while (*ptr1
!= '\0') {
2651 if (!first
) first
= ptr2
;
2661 /* We never found a non-zero digit, then our number is either
2662 * smaller than the requested precision, or 0.0 */
2663 if (!first
&& (number
<= 0.0))
2666 *decpt
= dec2
? dec2
: dec1
;
2670 /***********************************************************************
2673 char * CDECL
_gcvt( double number
, int ndigit
, char *buff
)
2685 sprintf(buff
, "%.*g", ndigit
, number
);
2689 /***********************************************************************
2690 * _gcvt_s (MSVCRT.@)
2692 int CDECL
_gcvt_s(char *buff
, size_t size
, double number
, int digits
)
2701 if( digits
<0 || digits
>=size
) {
2709 len
= _scprintf("%.*g", digits
, number
);
2716 sprintf(buff
, "%.*g", digits
, number
);
2720 #include <stdlib.h> /* div_t, ldiv_t */
2722 /*********************************************************************
2725 * [i386] Windows binary compatible - returns the struct in eax/edx.
2728 unsigned __int64 CDECL
div(int num
, int denom
)
2732 unsigned __int64 uint64
;
2735 ret
.div
.quot
= num
/ denom
;
2736 ret
.div
.rem
= num
% denom
;
2740 /*********************************************************************
2743 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
2745 div_t CDECL
div(int num
, int denom
)
2749 ret
.quot
= num
/ denom
;
2750 ret
.rem
= num
% denom
;
2753 #endif /* ifdef __i386__ */
2756 /*********************************************************************
2759 * [i386] Windows binary compatible - returns the struct in eax/edx.
2762 unsigned __int64 CDECL
ldiv(__msvcrt_long num
, __msvcrt_long denom
)
2766 unsigned __int64 uint64
;
2769 ret
.ldiv
.quot
= num
/ denom
;
2770 ret
.ldiv
.rem
= num
% denom
;
2774 /*********************************************************************
2777 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
2779 ldiv_t CDECL
ldiv(__msvcrt_long num
, __msvcrt_long denom
)
2783 ret
.quot
= num
/ denom
;
2784 ret
.rem
= num
% denom
;
2787 #endif /* ifdef __i386__ */
2790 /*********************************************************************
2791 * lldiv (MSVCR100.@)
2793 lldiv_t CDECL
lldiv(__int64 num
, __int64 denom
)
2797 ret
.quot
= num
/ denom
;
2798 ret
.rem
= num
% denom
;
2806 /*********************************************************************
2807 * _adjust_fdiv (MSVCRT.@)
2808 * Used by the MSVC compiler to work around the Pentium FDIV bug.
2810 int MSVCRT__adjust_fdiv
= 0;
2812 /***********************************************************************
2813 * _adj_fdiv_m16i (MSVCRT.@)
2816 * I _think_ this function is intended to work around the Pentium
2819 void __stdcall
_adj_fdiv_m16i( short arg
)
2821 TRACE("(): stub\n");
2824 /***********************************************************************
2825 * _adj_fdiv_m32 (MSVCRT.@)
2828 * I _think_ this function is intended to work around the Pentium
2831 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
2833 TRACE("(): stub\n");
2836 /***********************************************************************
2837 * _adj_fdiv_m32i (MSVCRT.@)
2840 * I _think_ this function is intended to work around the Pentium
2843 void __stdcall
_adj_fdiv_m32i( int arg
)
2845 TRACE("(): stub\n");
2848 /***********************************************************************
2849 * _adj_fdiv_m64 (MSVCRT.@)
2852 * I _think_ this function is intended to work around the Pentium
2855 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
2857 TRACE("(): stub\n");
2860 /***********************************************************************
2861 * _adj_fdiv_r (MSVCRT.@)
2863 * This function is likely to have the wrong number of arguments.
2866 * I _think_ this function is intended to work around the Pentium
2869 void _adj_fdiv_r(void)
2871 TRACE("(): stub\n");
2874 /***********************************************************************
2875 * _adj_fdivr_m16i (MSVCRT.@)
2878 * I _think_ this function is intended to work around the Pentium
2881 void __stdcall
_adj_fdivr_m16i( short arg
)
2883 TRACE("(): stub\n");
2886 /***********************************************************************
2887 * _adj_fdivr_m32 (MSVCRT.@)
2890 * I _think_ this function is intended to work around the Pentium
2893 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
2895 TRACE("(): stub\n");
2898 /***********************************************************************
2899 * _adj_fdivr_m32i (MSVCRT.@)
2902 * I _think_ this function is intended to work around the Pentium
2905 void __stdcall
_adj_fdivr_m32i( int arg
)
2907 TRACE("(): stub\n");
2910 /***********************************************************************
2911 * _adj_fdivr_m64 (MSVCRT.@)
2914 * I _think_ this function is intended to work around the Pentium
2917 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
2919 TRACE("(): stub\n");
2922 /***********************************************************************
2923 * _adj_fpatan (MSVCRT.@)
2925 * This function is likely to have the wrong number of arguments.
2928 * I _think_ this function is intended to work around the Pentium
2931 void _adj_fpatan(void)
2933 TRACE("(): stub\n");
2936 /***********************************************************************
2937 * _adj_fprem (MSVCRT.@)
2939 * This function is likely to have the wrong number of arguments.
2942 * I _think_ this function is intended to work around the Pentium
2945 void _adj_fprem(void)
2947 TRACE("(): stub\n");
2950 /***********************************************************************
2951 * _adj_fprem1 (MSVCRT.@)
2953 * This function is likely to have the wrong number of arguments.
2956 * I _think_ this function is intended to work around the Pentium
2959 void _adj_fprem1(void)
2961 TRACE("(): stub\n");
2964 /***********************************************************************
2965 * _adj_fptan (MSVCRT.@)
2967 * This function is likely to have the wrong number of arguments.
2970 * I _think_ this function is intended to work around the Pentium
2973 void _adj_fptan(void)
2975 TRACE("(): stub\n");
2978 /***********************************************************************
2979 * _safe_fdiv (MSVCRT.@)
2981 * This function is likely to have the wrong number of arguments.
2984 * I _think_ this function is intended to work around the Pentium
2987 void _safe_fdiv(void)
2989 TRACE("(): stub\n");
2992 /***********************************************************************
2993 * _safe_fdivr (MSVCRT.@)
2995 * This function is likely to have the wrong number of arguments.
2998 * I _think_ this function is intended to work around the Pentium
3001 void _safe_fdivr(void)
3003 TRACE("(): stub\n");
3006 /***********************************************************************
3007 * _safe_fprem (MSVCRT.@)
3009 * This function is likely to have the wrong number of arguments.
3012 * I _think_ this function is intended to work around the Pentium
3015 void _safe_fprem(void)
3017 TRACE("(): stub\n");
3020 /***********************************************************************
3021 * _safe_fprem1 (MSVCRT.@)
3024 * This function is likely to have the wrong number of arguments.
3027 * I _think_ this function is intended to work around the Pentium
3030 void _safe_fprem1(void)
3032 TRACE("(): stub\n");
3035 /***********************************************************************
3036 * __libm_sse2_acos (MSVCRT.@)
3038 void __cdecl
__libm_sse2_acos(void)
3041 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3043 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3046 /***********************************************************************
3047 * __libm_sse2_acosf (MSVCRT.@)
3049 void __cdecl
__libm_sse2_acosf(void)
3052 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3054 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3057 /***********************************************************************
3058 * __libm_sse2_asin (MSVCRT.@)
3060 void __cdecl
__libm_sse2_asin(void)
3063 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3065 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3068 /***********************************************************************
3069 * __libm_sse2_asinf (MSVCRT.@)
3071 void __cdecl
__libm_sse2_asinf(void)
3074 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3076 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3079 /***********************************************************************
3080 * __libm_sse2_atan (MSVCRT.@)
3082 void __cdecl
__libm_sse2_atan(void)
3085 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3087 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3090 /***********************************************************************
3091 * __libm_sse2_atan2 (MSVCRT.@)
3093 void __cdecl
__libm_sse2_atan2(void)
3096 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
3097 d1
= atan2( d1
, d2
);
3098 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
3101 /***********************************************************************
3102 * __libm_sse2_atanf (MSVCRT.@)
3104 void __cdecl
__libm_sse2_atanf(void)
3107 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3109 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3112 /***********************************************************************
3113 * __libm_sse2_cos (MSVCRT.@)
3115 void __cdecl
__libm_sse2_cos(void)
3118 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3120 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3123 /***********************************************************************
3124 * __libm_sse2_cosf (MSVCRT.@)
3126 void __cdecl
__libm_sse2_cosf(void)
3129 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3131 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3134 /***********************************************************************
3135 * __libm_sse2_exp (MSVCRT.@)
3137 void __cdecl
__libm_sse2_exp(void)
3140 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3142 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3145 /***********************************************************************
3146 * __libm_sse2_expf (MSVCRT.@)
3148 void __cdecl
__libm_sse2_expf(void)
3151 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3153 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3156 /***********************************************************************
3157 * __libm_sse2_log (MSVCRT.@)
3159 void __cdecl
__libm_sse2_log(void)
3162 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3164 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3167 /***********************************************************************
3168 * __libm_sse2_log10 (MSVCRT.@)
3170 void __cdecl
__libm_sse2_log10(void)
3173 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3175 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3178 /***********************************************************************
3179 * __libm_sse2_log10f (MSVCRT.@)
3181 void __cdecl
__libm_sse2_log10f(void)
3184 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3186 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3189 /***********************************************************************
3190 * __libm_sse2_logf (MSVCRT.@)
3192 void __cdecl
__libm_sse2_logf(void)
3195 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3197 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3200 /***********************************************************************
3201 * __libm_sse2_pow (MSVCRT.@)
3203 void __cdecl
__libm_sse2_pow(void)
3206 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
3208 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
3211 /***********************************************************************
3212 * __libm_sse2_powf (MSVCRT.@)
3214 void __cdecl
__libm_sse2_powf(void)
3217 __asm__
__volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1
), "=g" (f2
) );
3218 f1
= powf( f1
, f2
);
3219 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f1
) );
3222 /***********************************************************************
3223 * __libm_sse2_sin (MSVCRT.@)
3225 void __cdecl
__libm_sse2_sin(void)
3228 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3230 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3233 /***********************************************************************
3234 * __libm_sse2_sinf (MSVCRT.@)
3236 void __cdecl
__libm_sse2_sinf(void)
3239 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3241 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3244 /***********************************************************************
3245 * __libm_sse2_tan (MSVCRT.@)
3247 void __cdecl
__libm_sse2_tan(void)
3250 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3252 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3255 /***********************************************************************
3256 * __libm_sse2_tanf (MSVCRT.@)
3258 void __cdecl
__libm_sse2_tanf(void)
3261 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
3263 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
3266 /***********************************************************************
3267 * __libm_sse2_sqrt_precise (MSVCR110.@)
3269 void __cdecl
__libm_sse2_sqrt_precise(void)
3272 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
3274 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
3277 #endif /* __i386__ */
3279 /*********************************************************************
3282 double CDECL
cbrt(double x
)
3284 return unix_funcs
->cbrt( x
);
3287 /*********************************************************************
3288 * cbrtf (MSVCR120.@)
3290 float CDECL
cbrtf(float x
)
3292 return unix_funcs
->cbrtf( x
);
3295 /*********************************************************************
3298 double CDECL
exp2(double x
)
3300 double ret
= unix_funcs
->exp2( x
);
3301 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
3305 /*********************************************************************
3306 * exp2f (MSVCR120.@)
3308 float CDECL
exp2f(float x
)
3310 float ret
= unix_funcs
->exp2f( x
);
3311 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
3315 /*********************************************************************
3316 * expm1 (MSVCR120.@)
3318 double CDECL
expm1(double x
)
3320 double ret
= unix_funcs
->expm1( x
);
3321 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
3325 /*********************************************************************
3326 * expm1f (MSVCR120.@)
3328 float CDECL
expm1f(float x
)
3330 float ret
= unix_funcs
->expm1f( x
);
3331 if (isfinite(x
) && !isfinite(ret
)) *_errno() = ERANGE
;
3335 /*********************************************************************
3336 * log1p (MSVCR120.@)
3338 double CDECL
log1p(double x
)
3340 if (x
< -1) *_errno() = EDOM
;
3341 else if (x
== -1) *_errno() = ERANGE
;
3342 return unix_funcs
->log1p( x
);
3345 /*********************************************************************
3346 * log1pf (MSVCR120.@)
3348 float CDECL
log1pf(float x
)
3350 if (x
< -1) *_errno() = EDOM
;
3351 else if (x
== -1) *_errno() = ERANGE
;
3352 return unix_funcs
->log1pf( x
);
3355 /*********************************************************************
3358 double CDECL
log2(double x
)
3360 if (x
< 0) *_errno() = EDOM
;
3361 else if (x
== 0) *_errno() = ERANGE
;
3362 return unix_funcs
->log2( x
);
3365 /*********************************************************************
3366 * log2f (MSVCR120.@)
3368 float CDECL
log2f(float x
)
3370 if (x
< 0) *_errno() = EDOM
;
3371 else if (x
== 0) *_errno() = ERANGE
;
3372 return unix_funcs
->log2f( x
);
3375 /*********************************************************************
3378 double CDECL
rint(double x
)
3380 return unix_funcs
->rint(x
);
3383 /*********************************************************************
3384 * rintf (MSVCR120.@)
3386 float CDECL
rintf(float x
)
3388 return unix_funcs
->rintf(x
);
3391 /*********************************************************************
3392 * lrint (MSVCR120.@)
3394 __msvcrt_long CDECL
lrint(double x
)
3396 return unix_funcs
->lrint( x
);
3399 /*********************************************************************
3400 * lrintf (MSVCR120.@)
3402 __msvcrt_long CDECL
lrintf(float x
)
3404 return unix_funcs
->lrintf( x
);
3407 /*********************************************************************
3408 * llrint (MSVCR120.@)
3410 __int64 CDECL
llrint(double x
)
3412 return unix_funcs
->llrint( x
);
3415 /*********************************************************************
3416 * llrintf (MSVCR120.@)
3418 __int64 CDECL
llrintf(float x
)
3420 return unix_funcs
->llrintf( x
);
3425 /*********************************************************************
3426 * round (MSVCR120.@)
3428 double CDECL
round(double x
)
3430 return unix_funcs
->round(x
);
3433 /*********************************************************************
3434 * roundf (MSVCR120.@)
3436 float CDECL
roundf(float x
)
3438 return unix_funcs
->roundf(x
);
3441 /*********************************************************************
3442 * lround (MSVCR120.@)
3444 __msvcrt_long CDECL
lround(double x
)
3446 return unix_funcs
->lround( x
);
3449 /*********************************************************************
3450 * lroundf (MSVCR120.@)
3452 __msvcrt_long CDECL
lroundf(float x
)
3454 return unix_funcs
->lroundf( x
);
3457 /*********************************************************************
3458 * llround (MSVCR120.@)
3460 __int64 CDECL
llround(double x
)
3462 return unix_funcs
->llround( x
);
3465 /*********************************************************************
3466 * llroundf (MSVCR120.@)
3468 __int64 CDECL
llroundf(float x
)
3470 return unix_funcs
->llroundf( x
);
3473 /*********************************************************************
3474 * trunc (MSVCR120.@)
3476 double CDECL
trunc(double x
)
3478 return unix_funcs
->trunc(x
);
3481 /*********************************************************************
3482 * truncf (MSVCR120.@)
3484 float CDECL
truncf(float x
)
3486 return unix_funcs
->truncf(x
);
3489 /*********************************************************************
3490 * _dclass (MSVCR120.@)
3492 * Copied from musl: src/math/__fpclassify.c
3494 short CDECL
_dclass(double x
)
3496 union { double f
; UINT64 i
; } u
= { x
};
3497 int e
= u
.i
>> 52 & 0x7ff;
3499 if (!e
) return u
.i
<< 1 ? FP_SUBNORMAL
: FP_ZERO
;
3500 if (e
== 0x7ff) return (u
.i
<< 12) ? FP_NAN
: FP_INFINITE
;
3504 /*********************************************************************
3505 * _fdclass (MSVCR120.@)
3507 * Copied from musl: src/math/__fpclassifyf.c
3509 short CDECL
_fdclass(float x
)
3511 union { float f
; UINT32 i
; } u
= { x
};
3512 int e
= u
.i
>> 23 & 0xff;
3514 if (!e
) return u
.i
<< 1 ? FP_SUBNORMAL
: FP_ZERO
;
3515 if (e
== 0xff) return u
.i
<< 9 ? FP_NAN
: FP_INFINITE
;
3519 /*********************************************************************
3520 * _dtest (MSVCR120.@)
3522 short CDECL
_dtest(double *x
)
3527 /*********************************************************************
3528 * _fdtest (MSVCR120.@)
3530 short CDECL
_fdtest(float *x
)
3532 return _fdclass(*x
);
3535 /*********************************************************************
3538 double CDECL
erf(double x
)
3540 return unix_funcs
->erf( x
);
3543 /*********************************************************************
3546 float CDECL
erff(float x
)
3548 return unix_funcs
->erff( x
);
3551 /*********************************************************************
3554 double CDECL
erfc(double x
)
3556 return unix_funcs
->erfc( x
);
3559 /*********************************************************************
3560 * erfcf (MSVCR120.@)
3562 float CDECL
erfcf(float x
)
3564 return unix_funcs
->erfcf( x
);
3567 /*********************************************************************
3568 * fmaxf (MSVCR120.@)
3570 float CDECL
fmaxf(float x
, float y
)
3577 return signbit(x
) ? y
: x
;
3581 /*********************************************************************
3584 double CDECL
fmax(double x
, double y
)
3591 return signbit(x
) ? y
: x
;
3595 /*********************************************************************
3596 * fdimf (MSVCR120.@)
3598 float CDECL
fdimf(float x
, float y
)
3604 return x
>y
? x
-y
: 0;
3607 /*********************************************************************
3610 double CDECL
fdim(double x
, double y
)
3616 return x
>y
? x
-y
: 0;
3619 /*********************************************************************
3620 * _fdsign (MSVCR120.@)
3622 int CDECL
_fdsign(float x
)
3624 union { float f
; UINT32 i
; } u
= { x
};
3625 return (u
.i
>> 16) & 0x8000;
3628 /*********************************************************************
3629 * _dsign (MSVCR120.@)
3631 int CDECL
_dsign(double x
)
3633 union { double f
; UINT64 i
; } u
= { x
};
3634 return (u
.i
>> 48) & 0x8000;
3638 /*********************************************************************
3639 * _dpcomp (MSVCR120.@)
3641 int CDECL
_dpcomp(double x
, double y
)
3643 if(isnan(x
) || isnan(y
))
3646 if(x
== y
) return 2;
3647 return x
< y
? 1 : 4;
3650 /*********************************************************************
3651 * _fdpcomp (MSVCR120.@)
3653 int CDECL
_fdpcomp(float x
, float y
)
3655 return _dpcomp(x
, y
);
3658 /*********************************************************************
3659 * fminf (MSVCR120.@)
3661 float CDECL
fminf(float x
, float y
)
3668 return signbit(x
) ? x
: y
;
3672 /*********************************************************************
3675 double CDECL
fmin(double x
, double y
)
3682 return signbit(x
) ? x
: y
;
3686 /*********************************************************************
3687 * asinh (MSVCR120.@)
3689 double CDECL
asinh(double x
)
3691 return unix_funcs
->asinh( x
);
3694 /*********************************************************************
3695 * asinhf (MSVCR120.@)
3697 float CDECL
asinhf(float x
)
3699 return unix_funcs
->asinhf( x
);
3702 /*********************************************************************
3703 * acosh (MSVCR120.@)
3705 double CDECL
acosh(double x
)
3713 env
._Fe_stat
|= FE_INVALID
;
3717 return unix_funcs
->acosh( x
);
3720 /*********************************************************************
3721 * acoshf (MSVCR120.@)
3723 float CDECL
acoshf(float x
)
3731 env
._Fe_stat
|= FE_INVALID
;
3735 return unix_funcs
->acoshf( x
);
3738 /*********************************************************************
3739 * atanh (MSVCR120.@)
3741 double CDECL
atanh(double x
)
3745 if (x
> 1 || x
< -1) {
3750 /* on Linux atanh returns -NAN in this case */
3752 env
._Fe_stat
|= FE_INVALID
;
3756 ret
= unix_funcs
->atanh( x
);
3758 if (!isfinite(ret
)) *_errno() = ERANGE
;
3762 /*********************************************************************
3763 * atanhf (MSVCR120.@)
3765 float CDECL
atanhf(float x
)
3769 if (x
> 1 || x
< -1) {
3775 env
._Fe_stat
|= FE_INVALID
;
3780 ret
= unix_funcs
->atanh( x
);
3782 if (!isfinite(ret
)) *_errno() = ERANGE
;
3786 #endif /* _MSVCR_VER>=120 */
3788 /*********************************************************************
3790 * scalbn (MSVCR120.@)
3791 * scalbln (MSVCR120.@)
3793 double CDECL
_scalb(double num
, __msvcrt_long power
)
3795 return ldexp(num
, power
);
3798 /*********************************************************************
3799 * _scalbf (MSVCRT.@)
3800 * scalbnf (MSVCR120.@)
3801 * scalblnf (MSVCR120.@)
3803 float CDECL
_scalbf(float num
, __msvcrt_long power
)
3805 return ldexp(num
, power
);
3810 /*********************************************************************
3811 * remainder (MSVCR120.@)
3813 double CDECL
remainder(double x
, double y
)
3815 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3816 if(!isfinite(x
)) *_errno() = EDOM
;
3817 if(isnan(y
) || y
==0.0) *_errno() = EDOM
;
3818 return unix_funcs
->remainder( x
, y
);
3821 /*********************************************************************
3822 * remainderf (MSVCR120.@)
3824 float CDECL
remainderf(float x
, float y
)
3826 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3827 if(!isfinite(x
)) *_errno() = EDOM
;
3828 if(isnan(y
) || y
==0.0f
) *_errno() = EDOM
;
3829 return unix_funcs
->remainderf( x
, y
);
3832 /*********************************************************************
3833 * remquo (MSVCR120.@)
3835 double CDECL
remquo(double x
, double y
, int *quo
)
3837 if(!isfinite(x
)) *_errno() = EDOM
;
3838 if(isnan(y
) || y
==0.0) *_errno() = EDOM
;
3839 return unix_funcs
->remquo( x
, y
, quo
);
3842 /*********************************************************************
3843 * remquof (MSVCR120.@)
3845 float CDECL
remquof(float x
, float y
, int *quo
)
3847 if(!isfinite(x
)) *_errno() = EDOM
;
3848 if(isnan(y
) || y
==0.0f
) *_errno() = EDOM
;
3849 return unix_funcs
->remquof( x
, y
, quo
);
3852 /*********************************************************************
3853 * lgamma (MSVCR120.@)
3855 double CDECL
lgamma(double x
)
3857 return unix_funcs
->lgamma( x
);
3860 /*********************************************************************
3861 * lgammaf (MSVCR120.@)
3863 float CDECL
lgammaf(float x
)
3865 return unix_funcs
->lgammaf( x
);
3868 /*********************************************************************
3869 * tgamma (MSVCR120.@)
3871 double CDECL
tgamma(double x
)
3873 return unix_funcs
->tgamma( x
);
3876 /*********************************************************************
3877 * tgammaf (MSVCR120.@)
3879 float CDECL
tgammaf(float x
)
3881 return unix_funcs
->tgammaf( x
);
3884 /*********************************************************************
3887 double CDECL
nan(const char *tagp
)
3889 /* Windows ignores input (MSDN) */
3893 /*********************************************************************
3896 float CDECL
nanf(const char *tagp
)
3901 /*********************************************************************
3902 * _except1 (MSVCR120.@)
3904 * - find meaning of ignored cw and operation bits
3907 double CDECL
_except1(DWORD fpe
, _FP_OPERATION_CODE op
, double arg
, double res
, DWORD cw
, void *unk
)
3909 ULONG_PTR exception_arg
;
3910 DWORD exception
= 0;
3915 TRACE("(%x %x %lf %lf %x %p)\n", fpe
, op
, arg
, res
, cw
, unk
);
3918 cw
= ((cw
>> 7) & 0x3f) | ((cw
>> 3) & 0xc00);
3920 operation
= op
<< 5;
3921 exception_arg
= (ULONG_PTR
)&operation
;
3925 if (fpe
& 0x1) { /* overflow */
3926 if ((fpe
== 0x1 && (cw
& 0x8)) || (fpe
==0x11 && (cw
& 0x28))) {
3927 /* 32-bit version also sets SW_INEXACT here */
3928 env
._Fe_stat
|= FE_OVERFLOW
;
3929 if (fpe
& 0x10) env
._Fe_stat
|= FE_INEXACT
;
3930 res
= signbit(res
) ? -INFINITY
: INFINITY
;
3932 exception
= EXCEPTION_FLT_OVERFLOW
;
3934 } else if (fpe
& 0x2) { /* underflow */
3935 if ((fpe
== 0x2 && (cw
& 0x10)) || (fpe
==0x12 && (cw
& 0x30))) {
3936 env
._Fe_stat
|= FE_UNDERFLOW
;
3937 if (fpe
& 0x10) env
._Fe_stat
|= FE_INEXACT
;
3938 res
= signbit(res
) ? -0.0 : 0.0;
3940 exception
= EXCEPTION_FLT_UNDERFLOW
;
3942 } else if (fpe
& 0x4) { /* zerodivide */
3943 if ((fpe
== 0x4 && (cw
& 0x4)) || (fpe
==0x14 && (cw
& 0x24))) {
3944 env
._Fe_stat
|= FE_DIVBYZERO
;
3945 if (fpe
& 0x10) env
._Fe_stat
|= FE_INEXACT
;
3947 exception
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
3949 } else if (fpe
& 0x8) { /* invalid */
3950 if (fpe
== 0x8 && (cw
& 0x1)) {
3951 env
._Fe_stat
|= FE_INVALID
;
3953 exception
= EXCEPTION_FLT_INVALID_OPERATION
;
3955 } else if (fpe
& 0x10) { /* inexact */
3956 if (fpe
== 0x10 && (cw
& 0x20)) {
3957 env
._Fe_stat
|= FE_INEXACT
;
3959 exception
= EXCEPTION_FLT_INEXACT_RESULT
;
3967 RaiseException(exception
, 0, 1, &exception_arg
);
3969 if (cw
& 0x1) fpword
|= _EM_INVALID
;
3970 if (cw
& 0x2) fpword
|= _EM_DENORMAL
;
3971 if (cw
& 0x4) fpword
|= _EM_ZERODIVIDE
;
3972 if (cw
& 0x8) fpword
|= _EM_OVERFLOW
;
3973 if (cw
& 0x10) fpword
|= _EM_UNDERFLOW
;
3974 if (cw
& 0x20) fpword
|= _EM_INEXACT
;
3977 case 0xc00: fpword
|= _RC_UP
|_RC_DOWN
; break;
3978 case 0x800: fpword
|= _RC_UP
; break;
3979 case 0x400: fpword
|= _RC_DOWN
; break;
3983 case 0x0: fpword
|= _PC_24
; break;
3984 case 0x200: fpword
|= _PC_53
; break;
3985 case 0x300: fpword
|= _PC_64
; break;
3987 if (cw
& 0x1000) fpword
|= _IC_AFFINE
;
3988 _control87(fpword
, 0xffffffff);
3993 _Dcomplex
* CDECL
_Cbuild(_Dcomplex
*ret
, double r
, double i
)
4000 double CDECL
MSVCR120_creal(_Dcomplex z
)
4005 /*********************************************************************
4006 * ilogb (MSVCR120.@)
4008 * Copied from musl: src/math/ilogb.c
4010 int CDECL
ilogb(double x
)
4012 union { double f
; UINT64 i
; } u
= { x
};
4013 int e
= u
.i
>> 52 & 0x7ff;
4018 if (u
.i
== 0) return FP_ILOGB0
;
4020 for (e
= -0x3ff; u
.i
>> 63 == 0; e
--, u
.i
<<= 1);
4023 if (e
== 0x7ff) return u
.i
<< 12 ? FP_ILOGBNAN
: INT_MAX
;
4027 /*********************************************************************
4028 * ilogbf (MSVCR120.@)
4030 * Copied from musl: src/math/ilogbf.c
4032 int CDECL
ilogbf(float x
)
4034 union { float f
; UINT32 i
; } u
= { x
};
4035 int e
= u
.i
>> 23 & 0xff;
4040 if (u
.i
== 0) return FP_ILOGB0
;
4042 for (e
= -0x7f; u
.i
>> 31 == 0; e
--, u
.i
<<= 1);
4045 if (e
== 0xff) return u
.i
<< 9 ? FP_ILOGBNAN
: INT_MAX
;
4048 #endif /* _MSVCR_VER>=120 */