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 #include "wine/port.h"
24 #define __USE_ISOC9X 1
25 #define __USE_ISOC99 1
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
38 #define finitef(x) isfinite(x)
43 #define isnanf(x) isnan(x)
49 /* FIXME: Does not work with -NAN and -0. */
51 #define signbit(x) ((x) < 0)
54 #define _DOMAIN 1 /* domain error in argument */
55 #define _SING 2 /* singularity */
56 #define _OVERFLOW 3 /* range overflow */
57 #define _UNDERFLOW 4 /* range underflow */
59 typedef int (CDECL
*MSVCRT_matherr_func
)(struct MSVCRT__exception
*);
60 typedef double LDOUBLE
; /* long double is just a double */
62 static MSVCRT_matherr_func MSVCRT_default_matherr_func
= NULL
;
64 static BOOL sse2_supported
;
65 static BOOL sse2_enabled
;
67 void msvcrt_init_math(void)
69 sse2_supported
= sse2_enabled
= IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE
);
72 /*********************************************************************
75 int CDECL
MSVCRT__matherr(struct MSVCRT__exception
*e
)
80 TRACE("(%p = {%d, \"%s\", %g, %g, %g})\n", e
, e
->type
, e
->name
, e
->arg1
, e
->arg2
, e
->retval
);
84 if (MSVCRT_default_matherr_func
)
86 ret
= MSVCRT_default_matherr_func(e
);
93 *MSVCRT__errno() = MSVCRT_EDOM
;
97 *MSVCRT__errno() = MSVCRT_ERANGE
;
100 /* don't set errno */
103 ERR("Unhandled math error!\n");
109 /*********************************************************************
110 * __setusermatherr (MSVCRT.@)
112 void CDECL
MSVCRT___setusermatherr(MSVCRT_matherr_func func
)
114 MSVCRT_default_matherr_func
= func
;
115 TRACE("new matherr handler %p\n", func
);
118 static inline void math_error(int type
, const char *name
, double arg1
, double arg2
, double retval
)
120 struct MSVCRT__exception exception
= {type
, (char *)name
, arg1
, arg2
, retval
};
121 MSVCRT__matherr(&exception
);
124 /*********************************************************************
125 * _set_SSE2_enable (MSVCRT.@)
127 int CDECL
MSVCRT__set_SSE2_enable(int flag
)
129 sse2_enabled
= flag
&& sse2_supported
;
133 #if defined(_WIN64) && _MSVCR_VER>=120
134 /*********************************************************************
135 * _set_FMA3_enable (MSVCR120.@)
137 int CDECL
MSVCRT__set_FMA3_enable(int flag
)
139 FIXME("(%x) stub\n", flag
);
144 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || _MSVCR_VER>=120
146 /*********************************************************************
147 * _chgsignf (MSVCRT.@)
149 float CDECL
MSVCRT__chgsignf( float num
)
151 /* FIXME: +-infinity,Nan not tested */
155 /*********************************************************************
156 * _copysignf (MSVCRT.@)
158 float CDECL
MSVCRT__copysignf( float num
, float sign
)
161 return signbit(num
) ? num
: -num
;
162 return signbit(num
) ? -num
: num
;
165 /*********************************************************************
166 * _nextafterf (MSVCRT.@)
168 float CDECL
MSVCRT__nextafterf( float num
, float next
)
170 if (!finitef(num
) || !finitef(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
171 return nextafterf( num
, next
);
175 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
177 /*********************************************************************
178 * _finitef (MSVCRT.@)
180 int CDECL
MSVCRT__finitef( float num
)
182 return finitef(num
) != 0; /* See comment for _isnan() */
185 /*********************************************************************
188 INT CDECL
MSVCRT__isnanf( float num
)
190 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
191 * Do the same, as the result may be used in calculations
193 return isnanf(num
) != 0;
196 /*********************************************************************
199 float CDECL
MSVCRT__logbf( float num
)
201 float ret
= logbf(num
);
202 if (isnanf(num
)) math_error(_DOMAIN
, "_logbf", num
, 0, ret
);
203 else if (!num
) math_error(_SING
, "_logbf", num
, 0, ret
);
207 /*********************************************************************
208 * MSVCRT_acosf (MSVCRT.@)
210 float CDECL
MSVCRT_acosf( float x
)
212 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
213 * asin() uses a similar construction. This is bad because as x gets nearer to
214 * 1 the error in the expression "1 - x^2" can get relatively large due to
215 * cancellation. The sqrt() makes things worse. A safer way to calculate
216 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
217 float ret
= atan2f(sqrtf((1 - x
) * (1 + x
)), x
);
218 if (x
< -1.0 || x
> 1.0 || !finitef(x
)) math_error(_DOMAIN
, "acosf", x
, 0, ret
);
222 /*********************************************************************
223 * MSVCRT_asinf (MSVCRT.@)
225 float CDECL
MSVCRT_asinf( float x
)
227 float ret
= atan2f(x
, sqrtf((1 - x
) * (1 + x
)));
228 if (x
< -1.0 || x
> 1.0 || !finitef(x
)) math_error(_DOMAIN
, "asinf", x
, 0, ret
);
232 /*********************************************************************
233 * MSVCRT_atanf (MSVCRT.@)
235 float CDECL
MSVCRT_atanf( float x
)
237 float ret
= atanf(x
);
238 if (!finitef(x
)) math_error(_DOMAIN
, "atanf", x
, 0, ret
);
242 /*********************************************************************
243 * MSVCRT_atan2f (MSVCRT.@)
245 float CDECL
MSVCRT_atan2f( float x
, float y
)
247 float ret
= atan2f(x
, y
);
248 if (isnanf(x
)) math_error(_DOMAIN
, "atan2f", x
, y
, ret
);
252 /*********************************************************************
253 * MSVCRT_cosf (MSVCRT.@)
255 float CDECL
MSVCRT_cosf( float x
)
258 if (!finitef(x
)) math_error(_DOMAIN
, "cosf", x
, 0, ret
);
262 /*********************************************************************
263 * MSVCRT_coshf (MSVCRT.@)
265 float CDECL
MSVCRT_coshf( float x
)
267 float ret
= coshf(x
);
268 if (isnanf(x
)) math_error(_DOMAIN
, "coshf", x
, 0, ret
);
272 /*********************************************************************
273 * MSVCRT_expf (MSVCRT.@)
275 float CDECL
MSVCRT_expf( float x
)
278 if (isnanf(x
)) math_error(_DOMAIN
, "expf", x
, 0, ret
);
279 else if (finitef(x
) && !ret
) math_error(_UNDERFLOW
, "expf", x
, 0, ret
);
280 else if (finitef(x
) && !finitef(ret
)) math_error(_OVERFLOW
, "expf", x
, 0, ret
);
284 /*********************************************************************
285 * MSVCRT_fmodf (MSVCRT.@)
287 float CDECL
MSVCRT_fmodf( float x
, float y
)
289 float ret
= fmodf(x
, y
);
290 if (!finitef(x
) || !finitef(y
)) math_error(_DOMAIN
, "fmodf", x
, 0, ret
);
294 /*********************************************************************
295 * MSVCRT_logf (MSVCRT.@)
297 float CDECL
MSVCRT_logf( float x
)
300 if (x
< 0.0) math_error(_DOMAIN
, "logf", x
, 0, ret
);
301 else if (x
== 0.0) math_error(_SING
, "logf", x
, 0, ret
);
305 /*********************************************************************
306 * MSVCRT_log10f (MSVCRT.@)
308 float CDECL
MSVCRT_log10f( float x
)
310 float ret
= log10f(x
);
311 if (x
< 0.0) math_error(_DOMAIN
, "log10f", x
, 0, ret
);
312 else if (x
== 0.0) math_error(_SING
, "log10f", x
, 0, ret
);
316 /*********************************************************************
317 * MSVCRT_powf (MSVCRT.@)
319 float CDECL
MSVCRT_powf( float x
, float y
)
322 if (x
< 0 && y
!= floorf(y
)) math_error(_DOMAIN
, "powf", x
, y
, z
);
323 else if (!x
&& finitef(y
) && y
< 0) math_error(_SING
, "powf", x
, y
, z
);
324 else if (finitef(x
) && finitef(y
) && !finitef(z
)) math_error(_OVERFLOW
, "powf", x
, y
, z
);
325 else if (x
&& finitef(x
) && finitef(y
) && !z
) math_error(_UNDERFLOW
, "powf", x
, y
, z
);
329 /*********************************************************************
330 * MSVCRT_sinf (MSVCRT.@)
332 float CDECL
MSVCRT_sinf( float x
)
335 if (!finitef(x
)) math_error(_DOMAIN
, "sinf", x
, 0, ret
);
339 /*********************************************************************
340 * MSVCRT_sinhf (MSVCRT.@)
342 float CDECL
MSVCRT_sinhf( float x
)
344 float ret
= sinhf(x
);
345 if (isnanf(x
)) math_error(_DOMAIN
, "sinhf", x
, 0, ret
);
349 /*********************************************************************
350 * MSVCRT_sqrtf (MSVCRT.@)
352 float CDECL
MSVCRT_sqrtf( float x
)
354 float ret
= sqrtf(x
);
355 if (x
< 0.0) math_error(_DOMAIN
, "sqrtf", x
, 0, ret
);
359 /*********************************************************************
360 * MSVCRT_tanf (MSVCRT.@)
362 float CDECL
MSVCRT_tanf( float x
)
365 if (!finitef(x
)) math_error(_DOMAIN
, "tanf", x
, 0, ret
);
369 /*********************************************************************
370 * MSVCRT_tanhf (MSVCRT.@)
372 float CDECL
MSVCRT_tanhf( float x
)
374 float ret
= tanhf(x
);
375 if (!finitef(x
)) math_error(_DOMAIN
, "tanhf", x
, 0, ret
);
379 /*********************************************************************
382 float CDECL
MSVCRT_ceilf( float x
)
387 /*********************************************************************
390 float CDECL
MSVCRT_fabsf( float x
)
395 /*********************************************************************
398 float CDECL
MSVCRT_floorf( float x
)
403 /*********************************************************************
406 float CDECL
MSVCRT_frexpf( float x
, int *exp
)
408 return frexpf( x
, exp
);
411 /*********************************************************************
414 float CDECL
MSVCRT_modff( float x
, float *iptr
)
416 return modff( x
, iptr
);
421 /*********************************************************************
422 * MSVCRT_acos (MSVCRT.@)
424 double CDECL
MSVCRT_acos( double x
)
426 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
427 * asin() uses a similar construction. This is bad because as x gets nearer to
428 * 1 the error in the expression "1 - x^2" can get relatively large due to
429 * cancellation. The sqrt() makes things worse. A safer way to calculate
430 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
431 double ret
= atan2(sqrt((1 - x
) * (1 + x
)), x
);
432 if (x
< -1.0 || x
> 1.0 || !isfinite(x
)) math_error(_DOMAIN
, "acos", x
, 0, ret
);
436 /*********************************************************************
437 * MSVCRT_asin (MSVCRT.@)
439 double CDECL
MSVCRT_asin( double x
)
441 double ret
= atan2(x
, sqrt((1 - x
) * (1 + x
)));
442 if (x
< -1.0 || x
> 1.0 || !isfinite(x
)) math_error(_DOMAIN
, "asin", x
, 0, ret
);
446 /*********************************************************************
447 * MSVCRT_atan (MSVCRT.@)
449 double CDECL
MSVCRT_atan( double x
)
451 double ret
= atan(x
);
452 if (isnan(x
)) math_error(_DOMAIN
, "atan", x
, 0, ret
);
456 /*********************************************************************
457 * MSVCRT_atan2 (MSVCRT.@)
459 double CDECL
MSVCRT_atan2( double x
, double y
)
461 double ret
= atan2(x
, y
);
462 if (isnan(x
)) math_error(_DOMAIN
, "atan2", x
, y
, ret
);
466 /*********************************************************************
467 * MSVCRT_cos (MSVCRT.@)
469 double CDECL
MSVCRT_cos( double x
)
472 if (!isfinite(x
)) math_error(_DOMAIN
, "cos", x
, 0, ret
);
476 /*********************************************************************
477 * MSVCRT_cosh (MSVCRT.@)
479 double CDECL
MSVCRT_cosh( double x
)
481 double ret
= cosh(x
);
482 if (isnan(x
)) math_error(_DOMAIN
, "cosh", x
, 0, ret
);
486 /*********************************************************************
487 * MSVCRT_exp (MSVCRT.@)
489 double CDECL
MSVCRT_exp( double x
)
492 if (isnan(x
)) math_error(_DOMAIN
, "exp", x
, 0, ret
);
493 else if (isfinite(x
) && !ret
) math_error(_UNDERFLOW
, "exp", x
, 0, ret
);
494 else if (isfinite(x
) && !isfinite(ret
)) math_error(_OVERFLOW
, "exp", x
, 0, ret
);
498 /*********************************************************************
499 * MSVCRT_fmod (MSVCRT.@)
501 double CDECL
MSVCRT_fmod( double x
, double y
)
503 double ret
= fmod(x
, y
);
504 if (!isfinite(x
) || !isfinite(y
)) math_error(_DOMAIN
, "fmod", x
, y
, ret
);
508 /*********************************************************************
509 * MSVCRT_log (MSVCRT.@)
511 double CDECL
MSVCRT_log( double x
)
514 if (x
< 0.0) math_error(_DOMAIN
, "log", x
, 0, ret
);
515 else if (x
== 0.0) math_error(_SING
, "log", x
, 0, ret
);
519 /*********************************************************************
520 * MSVCRT_log10 (MSVCRT.@)
522 double CDECL
MSVCRT_log10( double x
)
524 double ret
= log10(x
);
525 if (x
< 0.0) math_error(_DOMAIN
, "log10", x
, 0, ret
);
526 else if (x
== 0.0) math_error(_SING
, "log10", x
, 0, ret
);
530 /*********************************************************************
531 * MSVCRT_pow (MSVCRT.@)
533 double CDECL
MSVCRT_pow( double x
, double y
)
536 if (x
< 0 && y
!= floor(y
)) math_error(_DOMAIN
, "pow", x
, y
, z
);
537 else if (!x
&& isfinite(y
) && y
< 0) math_error(_SING
, "pow", x
, y
, z
);
538 else if (isfinite(x
) && isfinite(y
) && !isfinite(z
)) math_error(_OVERFLOW
, "pow", x
, y
, z
);
539 else if (x
&& isfinite(x
) && isfinite(y
) && !z
) math_error(_UNDERFLOW
, "pow", x
, y
, z
);
543 /*********************************************************************
544 * MSVCRT_sin (MSVCRT.@)
546 double CDECL
MSVCRT_sin( double x
)
549 if (!isfinite(x
)) math_error(_DOMAIN
, "sin", x
, 0, ret
);
553 /*********************************************************************
554 * MSVCRT_sinh (MSVCRT.@)
556 double CDECL
MSVCRT_sinh( double x
)
558 double ret
= sinh(x
);
559 if (isnan(x
)) math_error(_DOMAIN
, "sinh", x
, 0, ret
);
563 /*********************************************************************
564 * MSVCRT_sqrt (MSVCRT.@)
566 double CDECL
MSVCRT_sqrt( double x
)
568 double ret
= sqrt(x
);
569 if (x
< 0.0) math_error(_DOMAIN
, "sqrt", x
, 0, ret
);
573 /*********************************************************************
574 * MSVCRT_tan (MSVCRT.@)
576 double CDECL
MSVCRT_tan( double x
)
579 if (!isfinite(x
)) math_error(_DOMAIN
, "tan", x
, 0, ret
);
583 /*********************************************************************
584 * MSVCRT_tanh (MSVCRT.@)
586 double CDECL
MSVCRT_tanh( double x
)
588 double ret
= tanh(x
);
589 if (isnan(x
)) math_error(_DOMAIN
, "tanh", x
, 0, ret
);
594 #if defined(__GNUC__) && defined(__i386__)
596 #define CREATE_FPU_FUNC1(name, call) \
597 __ASM_GLOBAL_FUNC(name, \
599 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
600 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
601 "movl %esp, %ebp\n\t" \
602 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
603 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
604 "fstpl (%esp)\n\t" /* store function argument */ \
606 "movl $1, %ecx\n\t" /* empty FPU stack */ \
610 "and $0x4500, %ax\n\t" \
611 "cmp $0x4100, %ax\n\t" \
613 "fstpl (%esp,%ecx,8)\n\t" \
618 "movl %ecx, -4(%ebp)\n\t" \
619 "call " __ASM_NAME( #call ) "\n\t" \
620 "movl -4(%ebp), %ecx\n\t" \
621 "fstpl (%esp)\n\t" /* save result */ \
622 "3:\n\t" /* restore FPU stack */ \
624 "fldl (%esp,%ecx,8)\n\t" \
625 "cmpl $0, %ecx\n\t" \
628 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
629 __ASM_CFI(".cfi_same_value %ebp\n\t") \
632 #define CREATE_FPU_FUNC2(name, call) \
633 __ASM_GLOBAL_FUNC(name, \
635 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
636 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
637 "movl %esp, %ebp\n\t" \
638 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
639 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
640 "fstpl 8(%esp)\n\t" /* store function argument */ \
644 "movl $2, %ecx\n\t" /* empty FPU stack */ \
648 "and $0x4500, %ax\n\t" \
649 "cmp $0x4100, %ax\n\t" \
651 "fstpl (%esp,%ecx,8)\n\t" \
656 "movl %ecx, -4(%ebp)\n\t" \
657 "call " __ASM_NAME( #call ) "\n\t" \
658 "movl -4(%ebp), %ecx\n\t" \
659 "fstpl 8(%esp)\n\t" /* save result */ \
660 "3:\n\t" /* restore FPU stack */ \
662 "fldl (%esp,%ecx,8)\n\t" \
663 "cmpl $1, %ecx\n\t" \
666 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
667 __ASM_CFI(".cfi_same_value %ebp\n\t") \
670 CREATE_FPU_FUNC1(_CIacos
, MSVCRT_acos
)
671 CREATE_FPU_FUNC1(_CIasin
, MSVCRT_asin
)
672 CREATE_FPU_FUNC1(_CIatan
, MSVCRT_atan
)
673 CREATE_FPU_FUNC2(_CIatan2
, MSVCRT_atan2
)
674 CREATE_FPU_FUNC1(_CIcos
, MSVCRT_cos
)
675 CREATE_FPU_FUNC1(_CIcosh
, MSVCRT_cosh
)
676 CREATE_FPU_FUNC1(_CIexp
, MSVCRT_exp
)
677 CREATE_FPU_FUNC2(_CIfmod
, MSVCRT_fmod
)
678 CREATE_FPU_FUNC1(_CIlog
, MSVCRT_log
)
679 CREATE_FPU_FUNC1(_CIlog10
, MSVCRT_log10
)
680 CREATE_FPU_FUNC2(_CIpow
, MSVCRT_pow
)
681 CREATE_FPU_FUNC1(_CIsin
, MSVCRT_sin
)
682 CREATE_FPU_FUNC1(_CIsinh
, MSVCRT_sinh
)
683 CREATE_FPU_FUNC1(_CIsqrt
, MSVCRT_sqrt
)
684 CREATE_FPU_FUNC1(_CItan
, MSVCRT_tan
)
685 CREATE_FPU_FUNC1(_CItanh
, MSVCRT_tanh
)
687 __ASM_GLOBAL_FUNC(MSVCRT__ftol
,
689 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
690 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
691 "movl %esp, %ebp\n\t"
692 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
693 "subl $12, %esp\n\t" /* sizeof(LONGLONG) + 2*sizeof(WORD) */
695 "mov (%esp), %ax\n\t"
697 "mov %ax, 2(%esp)\n\t"
701 "movl 4(%esp), %eax\n\t"
702 "movl 8(%esp), %edx\n\t"
704 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
705 __ASM_CFI(".cfi_same_value %ebp\n\t")
708 #endif /* defined(__GNUC__) && defined(__i386__) */
710 /*********************************************************************
711 * _fpclass (MSVCRT.@)
713 int CDECL
MSVCRT__fpclass(double num
)
715 #if defined(HAVE_FPCLASS) || defined(fpclass)
716 switch (fpclass( num
))
718 case FP_SNAN
: return MSVCRT__FPCLASS_SNAN
;
719 case FP_QNAN
: return MSVCRT__FPCLASS_QNAN
;
720 case FP_NINF
: return MSVCRT__FPCLASS_NINF
;
721 case FP_PINF
: return MSVCRT__FPCLASS_PINF
;
722 case FP_NDENORM
: return MSVCRT__FPCLASS_ND
;
723 case FP_PDENORM
: return MSVCRT__FPCLASS_PD
;
724 case FP_NZERO
: return MSVCRT__FPCLASS_NZ
;
725 case FP_PZERO
: return MSVCRT__FPCLASS_PZ
;
726 case FP_NNORM
: return MSVCRT__FPCLASS_NN
;
727 case FP_PNORM
: return MSVCRT__FPCLASS_PN
;
728 default: return MSVCRT__FPCLASS_PN
;
730 #elif defined (fpclassify)
731 switch (fpclassify( num
))
733 case FP_NAN
: return MSVCRT__FPCLASS_QNAN
;
734 case FP_INFINITE
: return signbit(num
) ? MSVCRT__FPCLASS_NINF
: MSVCRT__FPCLASS_PINF
;
735 case FP_SUBNORMAL
: return signbit(num
) ?MSVCRT__FPCLASS_ND
: MSVCRT__FPCLASS_PD
;
736 case FP_ZERO
: return signbit(num
) ? MSVCRT__FPCLASS_NZ
: MSVCRT__FPCLASS_PZ
;
738 return signbit(num
) ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
;
741 return MSVCRT__FPCLASS_QNAN
;
742 return num
== 0.0 ? MSVCRT__FPCLASS_PZ
: (num
< 0 ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
);
746 /*********************************************************************
749 unsigned int CDECL
_rotl(unsigned int num
, int shift
)
752 return (num
<< shift
) | (num
>> (32-shift
));
755 /*********************************************************************
758 MSVCRT_ulong CDECL
MSVCRT__lrotl(MSVCRT_ulong num
, int shift
)
761 return (num
<< shift
) | (num
>> (32-shift
));
764 /*********************************************************************
767 MSVCRT_ulong CDECL
MSVCRT__lrotr(MSVCRT_ulong num
, int shift
)
770 return (num
>> shift
) | (num
<< (32-shift
));
773 /*********************************************************************
776 unsigned int CDECL
_rotr(unsigned int num
, int shift
)
779 return (num
>> shift
) | (num
<< (32-shift
));
782 /*********************************************************************
785 unsigned __int64 CDECL
_rotl64(unsigned __int64 num
, int shift
)
788 return (num
<< shift
) | (num
>> (64-shift
));
791 /*********************************************************************
794 unsigned __int64 CDECL
_rotr64(unsigned __int64 num
, int shift
)
797 return (num
>> shift
) | (num
<< (64-shift
));
800 /*********************************************************************
803 int CDECL
MSVCRT_abs( int n
)
805 return n
>= 0 ? n
: -n
;
808 /*********************************************************************
811 MSVCRT_long CDECL
MSVCRT_labs( MSVCRT_long n
)
813 return n
>= 0 ? n
: -n
;
817 /*********************************************************************
820 MSVCRT_longlong CDECL
MSVCRT_llabs( MSVCRT_longlong n
)
822 return n
>= 0 ? n
: -n
;
826 /*********************************************************************
829 __int64 CDECL
_abs64( __int64 n
)
831 return n
>= 0 ? n
: -n
;
834 /*********************************************************************
837 double CDECL
MSVCRT__logb(double num
)
839 double ret
= logb(num
);
840 if (isnan(num
)) math_error(_DOMAIN
, "_logb", num
, 0, ret
);
841 else if (!num
) math_error(_SING
, "_logb", num
, 0, ret
);
845 /*********************************************************************
848 double CDECL
_hypot(double x
, double y
)
850 /* FIXME: errno handling */
851 return hypot( x
, y
);
854 /*********************************************************************
857 float CDECL
MSVCRT__hypotf(float x
, float y
)
859 /* FIXME: errno handling */
860 return hypotf( x
, y
);
863 /*********************************************************************
866 double CDECL
MSVCRT_ceil( double x
)
871 /*********************************************************************
874 double CDECL
MSVCRT_floor( double x
)
879 /*********************************************************************
882 double CDECL
MSVCRT_fabs( double x
)
887 /*********************************************************************
890 double CDECL
MSVCRT_frexp( double x
, int *exp
)
892 return frexp( x
, exp
);
895 /*********************************************************************
898 double CDECL
MSVCRT_modf( double x
, double *iptr
)
900 return modf( x
, iptr
);
903 /**********************************************************************
904 * _statusfp2 (MSVCRT.@)
906 * Not exported by native msvcrt, added in msvcr80.
908 #if defined(__i386__) || defined(__x86_64__)
909 void CDECL
_statusfp2( unsigned int *x86_sw
, unsigned int *sse2_sw
)
913 unsigned long fpword
;
917 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) );
919 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
920 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
921 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
922 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
923 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
924 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
928 if (!sse2_sw
) return;
932 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
934 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
935 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
936 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
937 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
938 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
939 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
944 FIXME( "not implemented\n" );
949 /**********************************************************************
950 * _statusfp (MSVCRT.@)
952 unsigned int CDECL
_statusfp(void)
954 #if defined(__i386__) || defined(__x86_64__)
955 unsigned int x86_sw
, sse2_sw
;
957 _statusfp2( &x86_sw
, &sse2_sw
);
958 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
959 return x86_sw
| sse2_sw
;
961 FIXME( "not implemented\n" );
966 /*********************************************************************
967 * _clearfp (MSVCRT.@)
969 unsigned int CDECL
_clearfp(void)
971 unsigned int flags
= 0;
972 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
973 unsigned long fpword
;
975 __asm__
__volatile__( "fnstsw %0; fnclex" : "=m" (fpword
) );
976 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
977 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
978 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
979 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
980 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
981 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
985 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
986 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
987 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
988 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
989 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
990 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
991 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
993 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
996 FIXME( "not implemented\n" );
1001 /*********************************************************************
1002 * __fpecode (MSVCRT.@)
1004 int * CDECL
__fpecode(void)
1006 return &msvcrt_get_thread_data()->fpecode
;
1009 /*********************************************************************
1012 double CDECL
MSVCRT_ldexp(double num
, MSVCRT_long exp
)
1014 double z
= ldexp(num
,exp
);
1016 if (isfinite(num
) && !isfinite(z
))
1017 math_error(_OVERFLOW
, "ldexp", num
, exp
, z
);
1018 else if (isfinite(num
) && !z
)
1019 math_error(_UNDERFLOW
, "ldexp", num
, exp
, z
);
1020 else if (z
== 0 && signbit(z
))
1021 z
= 0.0; /* Convert -0 -> +0 */
1025 /*********************************************************************
1028 double CDECL
MSVCRT__cabs(struct MSVCRT__complex num
)
1030 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
1033 /*********************************************************************
1034 * _chgsign (MSVCRT.@)
1036 double CDECL
MSVCRT__chgsign(double num
)
1038 /* FIXME: +-infinity,Nan not tested */
1042 /*********************************************************************
1043 * __control87_2 (MSVCR80.@)
1045 * Not exported by native msvcrt, added in msvcr80.
1047 #if defined(__i386__) || defined(__x86_64__)
1048 int CDECL
__control87_2( unsigned int newval
, unsigned int mask
,
1049 unsigned int *x86_cw
, unsigned int *sse2_cw
)
1052 unsigned long fpword
;
1057 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
1059 /* Convert into mask constants */
1061 if (fpword
& 0x1) flags
|= MSVCRT__EM_INVALID
;
1062 if (fpword
& 0x2) flags
|= MSVCRT__EM_DENORMAL
;
1063 if (fpword
& 0x4) flags
|= MSVCRT__EM_ZERODIVIDE
;
1064 if (fpword
& 0x8) flags
|= MSVCRT__EM_OVERFLOW
;
1065 if (fpword
& 0x10) flags
|= MSVCRT__EM_UNDERFLOW
;
1066 if (fpword
& 0x20) flags
|= MSVCRT__EM_INEXACT
;
1067 switch (fpword
& 0xc00)
1069 case 0xc00: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
1070 case 0x800: flags
|= MSVCRT__RC_UP
; break;
1071 case 0x400: flags
|= MSVCRT__RC_DOWN
; break;
1073 switch (fpword
& 0x300)
1075 case 0x0: flags
|= MSVCRT__PC_24
; break;
1076 case 0x200: flags
|= MSVCRT__PC_53
; break;
1077 case 0x300: flags
|= MSVCRT__PC_64
; break;
1079 if (fpword
& 0x1000) flags
|= MSVCRT__IC_AFFINE
;
1081 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1084 flags
= (flags
& ~mask
) | (newval
& mask
);
1086 /* Convert (masked) value back to fp word */
1088 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x1;
1089 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x2;
1090 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x4;
1091 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x8;
1092 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x10;
1093 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x20;
1094 switch (flags
& MSVCRT__MCW_RC
)
1096 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0xc00; break;
1097 case MSVCRT__RC_UP
: fpword
|= 0x800; break;
1098 case MSVCRT__RC_DOWN
: fpword
|= 0x400; break;
1100 switch (flags
& MSVCRT__MCW_PC
)
1102 case MSVCRT__PC_64
: fpword
|= 0x300; break;
1103 case MSVCRT__PC_53
: fpword
|= 0x200; break;
1104 case MSVCRT__PC_24
: fpword
|= 0x0; break;
1106 if (flags
& MSVCRT__IC_AFFINE
) fpword
|= 0x1000;
1108 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
1113 if (!sse2_cw
) return 1;
1117 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1119 /* Convert into mask constants */
1121 if (fpword
& 0x80) flags
|= MSVCRT__EM_INVALID
;
1122 if (fpword
& 0x100) flags
|= MSVCRT__EM_DENORMAL
;
1123 if (fpword
& 0x200) flags
|= MSVCRT__EM_ZERODIVIDE
;
1124 if (fpword
& 0x400) flags
|= MSVCRT__EM_OVERFLOW
;
1125 if (fpword
& 0x800) flags
|= MSVCRT__EM_UNDERFLOW
;
1126 if (fpword
& 0x1000) flags
|= MSVCRT__EM_INEXACT
;
1127 switch (fpword
& 0x6000)
1129 case 0x6000: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
1130 case 0x4000: flags
|= MSVCRT__RC_UP
; break;
1131 case 0x2000: flags
|= MSVCRT__RC_DOWN
; break;
1133 switch (fpword
& 0x8040)
1135 case 0x0040: flags
|= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
1136 case 0x8000: flags
|= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
1137 case 0x8040: flags
|= MSVCRT__DN_FLUSH
; break;
1140 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1143 flags
= (flags
& ~mask
) | (newval
& mask
);
1145 /* Convert (masked) value back to fp word */
1147 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x80;
1148 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x100;
1149 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x200;
1150 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x400;
1151 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x800;
1152 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x1000;
1153 switch (flags
& MSVCRT__MCW_RC
)
1155 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0x6000; break;
1156 case MSVCRT__RC_UP
: fpword
|= 0x4000; break;
1157 case MSVCRT__RC_DOWN
: fpword
|= 0x2000; break;
1159 switch (flags
& MSVCRT__MCW_DN
)
1161 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
1162 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
1163 case MSVCRT__DN_FLUSH
: fpword
|= 0x8040; break;
1165 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1173 FIXME( "not implemented\n" );
1179 /*********************************************************************
1180 * _control87 (MSVCRT.@)
1182 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
1184 #if defined(__i386__) || defined(__x86_64__)
1185 unsigned int x86_cw
, sse2_cw
;
1187 __control87_2( newval
, mask
, &x86_cw
, &sse2_cw
);
1189 if ((x86_cw
^ sse2_cw
) & (MSVCRT__MCW_EM
| MSVCRT__MCW_RC
)) x86_cw
|= MSVCRT__EM_AMBIGUOUS
;
1192 FIXME( "not implemented\n" );
1197 /*********************************************************************
1198 * _controlfp (MSVCRT.@)
1200 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
1202 return _control87( newval
, mask
& ~MSVCRT__EM_DENORMAL
);
1205 /*********************************************************************
1206 * _set_controlfp (MSVCRT.@)
1208 void CDECL
_set_controlfp( unsigned int newval
, unsigned int mask
)
1210 _controlfp( newval
, mask
);
1213 /*********************************************************************
1214 * _controlfp_s (MSVCRT.@)
1216 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
1218 static const unsigned int all_flags
= (MSVCRT__MCW_EM
| MSVCRT__MCW_IC
| MSVCRT__MCW_RC
|
1219 MSVCRT__MCW_PC
| MSVCRT__MCW_DN
);
1222 if (!MSVCRT_CHECK_PMT( !(newval
& mask
& ~all_flags
) ))
1224 if (cur
) *cur
= _controlfp( 0, 0 ); /* retrieve it anyway */
1225 return MSVCRT_EINVAL
;
1227 val
= _controlfp( newval
, mask
);
1228 if (cur
) *cur
= val
;
1233 /*********************************************************************
1234 * fegetenv (MSVCR120.@)
1236 int CDECL
MSVCRT_fegetenv(MSVCRT_fenv_t
*env
)
1238 env
->control
= _controlfp(0, 0) & (MSVCRT__EM_INEXACT
| MSVCRT__EM_UNDERFLOW
|
1239 MSVCRT__EM_OVERFLOW
| MSVCRT__EM_ZERODIVIDE
| MSVCRT__EM_INVALID
);
1240 env
->status
= _statusfp();
1246 /*********************************************************************
1247 * __fpe_flt_rounds (UCRTBASE.@)
1249 int CDECL
__fpe_flt_rounds(void)
1251 unsigned int fpc
= _controlfp(0, 0) & MSVCRT__RC_CHOP
;
1256 case MSVCRT__RC_CHOP
: return 0;
1257 case MSVCRT__RC_NEAR
: return 1;
1259 case MSVCRT__RC_UP
: return 3;
1262 case MSVCRT__RC_UP
: return 2;
1271 /*********************************************************************
1272 * fegetround (MSVCR120.@)
1274 int CDECL
MSVCRT_fegetround(void)
1276 return _controlfp(0, 0) & MSVCRT__RC_CHOP
;
1279 /*********************************************************************
1280 * fesetround (MSVCR120.@)
1282 int CDECL
MSVCRT_fesetround(int round_mode
)
1284 if (round_mode
& (~MSVCRT__RC_CHOP
))
1286 _controlfp(round_mode
, MSVCRT__RC_CHOP
);
1290 #endif /* _MSVCR_VER>=120 */
1292 /*********************************************************************
1293 * _copysign (MSVCRT.@)
1295 double CDECL
MSVCRT__copysign(double num
, double sign
)
1298 return signbit(num
) ? num
: -num
;
1299 return signbit(num
) ? -num
: num
;
1302 /*********************************************************************
1303 * _finite (MSVCRT.@)
1305 int CDECL
MSVCRT__finite(double num
)
1307 return isfinite(num
) != 0; /* See comment for _isnan() */
1310 /*********************************************************************
1311 * _fpreset (MSVCRT.@)
1313 void CDECL
_fpreset(void)
1315 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1316 const unsigned int x86_cw
= 0x27f;
1317 __asm__
__volatile__( "fninit; fldcw %0" : : "m" (x86_cw
) );
1320 const unsigned long sse2_cw
= 0x1f80;
1321 __asm__
__volatile__( "ldmxcsr %0" : : "m" (sse2_cw
) );
1324 FIXME( "not implemented\n" );
1329 /*********************************************************************
1330 * fesetenv (MSVCR120.@)
1332 int CDECL
MSVCRT_fesetenv(const MSVCRT_fenv_t
*env
)
1334 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1342 DWORD instruction_pointer
;
1350 TRACE( "(%p)\n", env
);
1352 if (!env
->control
&& !env
->status
) {
1357 __asm__
__volatile__( "fnstenv %0" : "=m" (fenv
) );
1359 fenv
.control_word
&= ~0x3d;
1360 if (env
->control
& MSVCRT__EM_INVALID
) fenv
.control_word
|= 0x1;
1361 if (env
->control
& MSVCRT__EM_ZERODIVIDE
) fenv
.control_word
|= 0x4;
1362 if (env
->control
& MSVCRT__EM_OVERFLOW
) fenv
.control_word
|= 0x8;
1363 if (env
->control
& MSVCRT__EM_UNDERFLOW
) fenv
.control_word
|= 0x10;
1364 if (env
->control
& MSVCRT__EM_INEXACT
) fenv
.control_word
|= 0x20;
1366 fenv
.status_word
&= ~0x3d;
1367 if (env
->status
& MSVCRT__SW_INVALID
) fenv
.status_word
|= 0x1;
1368 if (env
->status
& MSVCRT__SW_ZERODIVIDE
) fenv
.status_word
|= 0x4;
1369 if (env
->status
& MSVCRT__SW_OVERFLOW
) fenv
.status_word
|= 0x8;
1370 if (env
->status
& MSVCRT__SW_UNDERFLOW
) fenv
.status_word
|= 0x10;
1371 if (env
->status
& MSVCRT__SW_INEXACT
) fenv
.status_word
|= 0x20;
1373 __asm__
__volatile__( "fldenv %0" : : "m" (fenv
) : "st", "st(1)",
1374 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
1380 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1382 if (env
->control
& MSVCRT__EM_INVALID
) fpword
|= 0x80;
1383 if (env
->control
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x200;
1384 if (env
->control
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x400;
1385 if (env
->control
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x800;
1386 if (env
->control
& MSVCRT__EM_INEXACT
) fpword
|= 0x1000;
1387 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1392 FIXME( "not implemented\n" );
1398 /*********************************************************************
1401 INT CDECL
MSVCRT__isnan(double num
)
1403 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1404 * Do the same, as the result may be used in calculations
1406 return isnan(num
) != 0;
1409 /*********************************************************************
1412 double CDECL
MSVCRT__j0(double num
)
1414 /* FIXME: errno handling */
1418 FIXME("not implemented\n");
1423 /*********************************************************************
1426 double CDECL
MSVCRT__j1(double num
)
1428 /* FIXME: errno handling */
1432 FIXME("not implemented\n");
1437 /*********************************************************************
1440 double CDECL
MSVCRT__jn(int n
, double num
)
1442 /* FIXME: errno handling */
1446 FIXME("not implemented\n");
1451 /*********************************************************************
1454 double CDECL
MSVCRT__y0(double num
)
1457 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1460 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1462 *MSVCRT__errno() = MSVCRT_EDOM
;
1466 FIXME("not implemented\n");
1472 /*********************************************************************
1475 double CDECL
MSVCRT__y1(double num
)
1478 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1481 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1483 *MSVCRT__errno() = MSVCRT_EDOM
;
1487 FIXME("not implemented\n");
1493 /*********************************************************************
1496 double CDECL
MSVCRT__yn(int order
, double num
)
1499 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1501 retval
= yn(order
,num
);
1502 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1504 *MSVCRT__errno() = MSVCRT_EDOM
;
1508 FIXME("not implemented\n");
1516 /*********************************************************************
1517 * _nearbyint (MSVCR120.@)
1519 double CDECL
MSVCRT_nearbyint(double num
)
1521 #ifdef HAVE_NEARBYINT
1522 return nearbyint(num
);
1524 return num
>= 0 ? floor(num
+ 0.5) : ceil(num
- 0.5);
1528 /*********************************************************************
1529 * _nearbyintf (MSVCR120.@)
1531 float CDECL
MSVCRT_nearbyintf(float num
)
1533 #ifdef HAVE_NEARBYINTF
1534 return nearbyintf(num
);
1536 return MSVCRT_nearbyint(num
);
1540 #endif /* _MSVCR_VER>=120 */
1542 /*********************************************************************
1543 * _nextafter (MSVCRT.@)
1545 double CDECL
MSVCRT__nextafter(double num
, double next
)
1548 if (!isfinite(num
) || !isfinite(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1549 retval
= nextafter(num
,next
);
1553 /*********************************************************************
1556 char * CDECL
MSVCRT__ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
1559 thread_data_t
*data
= msvcrt_get_thread_data();
1560 /* FIXME: check better for overflow (native supports over 300 chars) */
1561 ndigits
= min( ndigits
, 80 - 7); /* 7 : space for dec point, 1 for "e",
1562 * 4 for exponent and one for
1563 * terminating '\0' */
1564 if (!data
->efcvt_buffer
)
1565 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1572 /* handle cases with zero ndigits or less */
1574 if( prec
< 1) prec
= 2;
1575 len
= snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
1576 /* take the decimal "point away */
1578 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
1579 /* take the exponential "e" out */
1580 data
->efcvt_buffer
[ prec
] = '\0';
1581 /* read the exponent */
1582 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
1584 /* adjust for some border cases */
1585 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
1587 /* handle cases with zero ndigits or less */
1589 if( data
->efcvt_buffer
[ 0] >= '5')
1591 data
->efcvt_buffer
[ 0] = '\0';
1593 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
1594 return data
->efcvt_buffer
;
1597 /*********************************************************************
1598 * _ecvt_s (MSVCRT.@)
1600 int CDECL
MSVCRT__ecvt_s( char *buffer
, MSVCRT_size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
1604 const char infret
[] = "1#INF";
1606 if (!MSVCRT_CHECK_PMT(buffer
!= NULL
)) return MSVCRT_EINVAL
;
1607 if (!MSVCRT_CHECK_PMT(decpt
!= NULL
)) return MSVCRT_EINVAL
;
1608 if (!MSVCRT_CHECK_PMT(sign
!= NULL
)) return MSVCRT_EINVAL
;
1609 if (!MSVCRT_CHECK_PMT_ERR( length
> 2, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
1610 if (!MSVCRT_CHECK_PMT_ERR(ndigits
< (int)length
- 1, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
1612 /* special case - inf */
1613 if(number
== HUGE_VAL
|| number
== -HUGE_VAL
)
1615 memset(buffer
, '0', ndigits
);
1616 memcpy(buffer
, infret
, min(ndigits
, sizeof(infret
) - 1 ) );
1617 buffer
[ndigits
] = '\0';
1619 if(number
== -HUGE_VAL
)
1625 /* handle cases with zero ndigits or less */
1627 if( prec
< 1) prec
= 2;
1628 result
= MSVCRT_malloc(prec
+ 7);
1635 len
= snprintf(result
, prec
+ 7, "%.*le", prec
- 1, number
);
1636 /* take the decimal "point away */
1638 memmove( result
+ 1, result
+ 2, len
- 1 );
1639 /* take the exponential "e" out */
1640 result
[ prec
] = '\0';
1641 /* read the exponent */
1642 sscanf( result
+ prec
+ 1, "%d", decpt
);
1644 /* adjust for some border cases */
1645 if( result
[0] == '0')/* value is zero */
1647 /* handle cases with zero ndigits or less */
1649 if( result
[ 0] >= '5')
1653 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
1654 MSVCRT_free( result
);
1658 /***********************************************************************
1661 char * CDECL
MSVCRT__fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
1663 thread_data_t
*data
= msvcrt_get_thread_data();
1664 int stop
, dec1
, dec2
;
1665 char *ptr1
, *ptr2
, *first
;
1666 char buf
[80]; /* ought to be enough */
1668 if (!data
->efcvt_buffer
)
1669 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1677 stop
= snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1679 ptr2
= data
->efcvt_buffer
;
1684 /* For numbers below the requested resolution, work out where
1685 the decimal point will be rather than finding it in the string */
1686 if (number
< 1.0 && number
> 0.0) {
1687 dec2
= log10(number
+ 1e-10);
1688 if (-dec2
<= ndigits
) dec2
= 0;
1691 /* If requested digits is zero or less, we will need to truncate
1692 * the returned string */
1697 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1698 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1699 if (!first
) first
= ptr2
;
1700 if ((ptr1
- buf
) < stop
) {
1711 while (*ptr1
== '0') { /* Process leading zeroes */
1716 while (*ptr1
!= '\0') {
1717 if (!first
) first
= ptr2
;
1724 /* We never found a non-zero digit, then our number is either
1725 * smaller than the requested precision, or 0.0 */
1730 first
= data
->efcvt_buffer
;
1735 *decpt
= dec2
? dec2
: dec1
;
1739 /***********************************************************************
1740 * _fcvt_s (MSVCRT.@)
1742 int CDECL
MSVCRT__fcvt_s(char* outbuffer
, MSVCRT_size_t size
, double number
, int ndigits
, int *decpt
, int *sign
)
1744 int stop
, dec1
, dec2
;
1745 char *ptr1
, *ptr2
, *first
;
1746 char buf
[80]; /* ought to be enough */
1748 if (!outbuffer
|| !decpt
|| !sign
|| size
== 0)
1750 *MSVCRT__errno() = MSVCRT_EINVAL
;
1751 return MSVCRT_EINVAL
;
1760 stop
= snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1767 /* For numbers below the requested resolution, work out where
1768 the decimal point will be rather than finding it in the string */
1769 if (number
< 1.0 && number
> 0.0) {
1770 dec2
= log10(number
+ 1e-10);
1771 if (-dec2
<= ndigits
) dec2
= 0;
1774 /* If requested digits is zero or less, we will need to truncate
1775 * the returned string */
1780 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1781 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1782 if (!first
) first
= ptr2
;
1783 if ((ptr1
- buf
) < stop
) {
1797 while (*ptr1
== '0') { /* Process leading zeroes */
1798 if (number
== 0.0 && size
> 1) {
1806 while (*ptr1
!= '\0') {
1807 if (!first
) first
= ptr2
;
1817 /* We never found a non-zero digit, then our number is either
1818 * smaller than the requested precision, or 0.0 */
1819 if (!first
&& (number
<= 0.0))
1822 *decpt
= dec2
? dec2
: dec1
;
1826 /***********************************************************************
1829 char * CDECL
MSVCRT__gcvt( double number
, int ndigit
, char *buff
)
1832 *MSVCRT__errno() = MSVCRT_EINVAL
;
1837 *MSVCRT__errno() = MSVCRT_ERANGE
;
1841 MSVCRT_sprintf(buff
, "%.*g", ndigit
, number
);
1845 /***********************************************************************
1846 * _gcvt_s (MSVCRT.@)
1848 int CDECL
MSVCRT__gcvt_s(char *buff
, MSVCRT_size_t size
, double number
, int digits
)
1853 *MSVCRT__errno() = MSVCRT_EINVAL
;
1854 return MSVCRT_EINVAL
;
1857 if( digits
<0 || digits
>=size
) {
1861 *MSVCRT__errno() = MSVCRT_ERANGE
;
1862 return MSVCRT_ERANGE
;
1865 len
= MSVCRT__scprintf("%.*g", digits
, number
);
1868 *MSVCRT__errno() = MSVCRT_ERANGE
;
1869 return MSVCRT_ERANGE
;
1872 MSVCRT_sprintf(buff
, "%.*g", digits
, number
);
1876 #include <stdlib.h> /* div_t, ldiv_t */
1878 /*********************************************************************
1881 * [i386] Windows binary compatible - returns the struct in eax/edx.
1884 unsigned __int64 CDECL
MSVCRT_div(int num
, int denom
)
1886 div_t dt
= div(num
,denom
);
1887 return ((unsigned __int64
)dt
.rem
<< 32) | (unsigned int)dt
.quot
;
1890 /*********************************************************************
1893 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1895 MSVCRT_div_t CDECL
MSVCRT_div(int num
, int denom
)
1897 div_t dt
= div(num
,denom
);
1905 #endif /* ifdef __i386__ */
1908 /*********************************************************************
1911 * [i386] Windows binary compatible - returns the struct in eax/edx.
1914 unsigned __int64 CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1916 ldiv_t ldt
= ldiv(num
,denom
);
1917 return ((unsigned __int64
)ldt
.rem
<< 32) | (MSVCRT_ulong
)ldt
.quot
;
1920 /*********************************************************************
1923 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1925 MSVCRT_ldiv_t CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1927 ldiv_t result
= ldiv(num
,denom
);
1930 ret
.quot
= result
.quot
;
1931 ret
.rem
= result
.rem
;
1935 #endif /* ifdef __i386__ */
1938 /*********************************************************************
1939 * lldiv (MSVCR100.@)
1941 MSVCRT_lldiv_t
* CDECL
MSVCRT_lldiv(MSVCRT_lldiv_t
*ret
,
1942 MSVCRT_longlong num
, MSVCRT_longlong denom
)
1944 ret
->quot
= num
/ denom
;
1945 ret
->rem
= num
% denom
;
1953 /*********************************************************************
1954 * _adjust_fdiv (MSVCRT.@)
1955 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1957 int MSVCRT__adjust_fdiv
= 0;
1959 /***********************************************************************
1960 * _adj_fdiv_m16i (MSVCRT.@)
1963 * I _think_ this function is intended to work around the Pentium
1966 void __stdcall
_adj_fdiv_m16i( short arg
)
1968 TRACE("(): stub\n");
1971 /***********************************************************************
1972 * _adj_fdiv_m32 (MSVCRT.@)
1975 * I _think_ this function is intended to work around the Pentium
1978 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
1980 TRACE("(): stub\n");
1983 /***********************************************************************
1984 * _adj_fdiv_m32i (MSVCRT.@)
1987 * I _think_ this function is intended to work around the Pentium
1990 void __stdcall
_adj_fdiv_m32i( int arg
)
1992 TRACE("(): stub\n");
1995 /***********************************************************************
1996 * _adj_fdiv_m64 (MSVCRT.@)
1999 * I _think_ this function is intended to work around the Pentium
2002 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
2004 TRACE("(): stub\n");
2007 /***********************************************************************
2008 * _adj_fdiv_r (MSVCRT.@)
2010 * This function is likely to have the wrong number of arguments.
2013 * I _think_ this function is intended to work around the Pentium
2016 void _adj_fdiv_r(void)
2018 TRACE("(): stub\n");
2021 /***********************************************************************
2022 * _adj_fdivr_m16i (MSVCRT.@)
2025 * I _think_ this function is intended to work around the Pentium
2028 void __stdcall
_adj_fdivr_m16i( short arg
)
2030 TRACE("(): stub\n");
2033 /***********************************************************************
2034 * _adj_fdivr_m32 (MSVCRT.@)
2037 * I _think_ this function is intended to work around the Pentium
2040 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
2042 TRACE("(): stub\n");
2045 /***********************************************************************
2046 * _adj_fdivr_m32i (MSVCRT.@)
2049 * I _think_ this function is intended to work around the Pentium
2052 void __stdcall
_adj_fdivr_m32i( int arg
)
2054 TRACE("(): stub\n");
2057 /***********************************************************************
2058 * _adj_fdivr_m64 (MSVCRT.@)
2061 * I _think_ this function is intended to work around the Pentium
2064 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
2066 TRACE("(): stub\n");
2069 /***********************************************************************
2070 * _adj_fpatan (MSVCRT.@)
2072 * This function is likely to have the wrong number of arguments.
2075 * I _think_ this function is intended to work around the Pentium
2078 void _adj_fpatan(void)
2080 TRACE("(): stub\n");
2083 /***********************************************************************
2084 * _adj_fprem (MSVCRT.@)
2086 * This function is likely to have the wrong number of arguments.
2089 * I _think_ this function is intended to work around the Pentium
2092 void _adj_fprem(void)
2094 TRACE("(): stub\n");
2097 /***********************************************************************
2098 * _adj_fprem1 (MSVCRT.@)
2100 * This function is likely to have the wrong number of arguments.
2103 * I _think_ this function is intended to work around the Pentium
2106 void _adj_fprem1(void)
2108 TRACE("(): stub\n");
2111 /***********************************************************************
2112 * _adj_fptan (MSVCRT.@)
2114 * This function is likely to have the wrong number of arguments.
2117 * I _think_ this function is intended to work around the Pentium
2120 void _adj_fptan(void)
2122 TRACE("(): stub\n");
2125 /***********************************************************************
2126 * _safe_fdiv (MSVCRT.@)
2128 * This function is likely to have the wrong number of arguments.
2131 * I _think_ this function is intended to work around the Pentium
2134 void _safe_fdiv(void)
2136 TRACE("(): stub\n");
2139 /***********************************************************************
2140 * _safe_fdivr (MSVCRT.@)
2142 * This function is likely to have the wrong number of arguments.
2145 * I _think_ this function is intended to work around the Pentium
2148 void _safe_fdivr(void)
2150 TRACE("(): stub\n");
2153 /***********************************************************************
2154 * _safe_fprem (MSVCRT.@)
2156 * This function is likely to have the wrong number of arguments.
2159 * I _think_ this function is intended to work around the Pentium
2162 void _safe_fprem(void)
2164 TRACE("(): stub\n");
2167 /***********************************************************************
2168 * _safe_fprem1 (MSVCRT.@)
2171 * This function is likely to have the wrong number of arguments.
2174 * I _think_ this function is intended to work around the Pentium
2177 void _safe_fprem1(void)
2179 TRACE("(): stub\n");
2182 /***********************************************************************
2183 * __libm_sse2_acos (MSVCRT.@)
2185 void __cdecl
MSVCRT___libm_sse2_acos(void)
2188 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2190 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2193 /***********************************************************************
2194 * __libm_sse2_acosf (MSVCRT.@)
2196 void __cdecl
MSVCRT___libm_sse2_acosf(void)
2199 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2201 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2204 /***********************************************************************
2205 * __libm_sse2_asin (MSVCRT.@)
2207 void __cdecl
MSVCRT___libm_sse2_asin(void)
2210 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2212 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2215 /***********************************************************************
2216 * __libm_sse2_asinf (MSVCRT.@)
2218 void __cdecl
MSVCRT___libm_sse2_asinf(void)
2221 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2223 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2226 /***********************************************************************
2227 * __libm_sse2_atan (MSVCRT.@)
2229 void __cdecl
MSVCRT___libm_sse2_atan(void)
2232 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2234 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2237 /***********************************************************************
2238 * __libm_sse2_atan2 (MSVCRT.@)
2240 void __cdecl
MSVCRT___libm_sse2_atan2(void)
2243 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
2244 d1
= atan2( d1
, d2
);
2245 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
2248 /***********************************************************************
2249 * __libm_sse2_atanf (MSVCRT.@)
2251 void __cdecl
MSVCRT___libm_sse2_atanf(void)
2254 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2256 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2259 /***********************************************************************
2260 * __libm_sse2_cos (MSVCRT.@)
2262 void __cdecl
MSVCRT___libm_sse2_cos(void)
2265 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2267 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2270 /***********************************************************************
2271 * __libm_sse2_cosf (MSVCRT.@)
2273 void __cdecl
MSVCRT___libm_sse2_cosf(void)
2276 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2278 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2281 /***********************************************************************
2282 * __libm_sse2_exp (MSVCRT.@)
2284 void __cdecl
MSVCRT___libm_sse2_exp(void)
2287 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2289 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2292 /***********************************************************************
2293 * __libm_sse2_expf (MSVCRT.@)
2295 void __cdecl
MSVCRT___libm_sse2_expf(void)
2298 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2300 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2303 /***********************************************************************
2304 * __libm_sse2_log (MSVCRT.@)
2306 void __cdecl
MSVCRT___libm_sse2_log(void)
2309 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2311 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2314 /***********************************************************************
2315 * __libm_sse2_log10 (MSVCRT.@)
2317 void __cdecl
MSVCRT___libm_sse2_log10(void)
2320 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2322 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2325 /***********************************************************************
2326 * __libm_sse2_log10f (MSVCRT.@)
2328 void __cdecl
MSVCRT___libm_sse2_log10f(void)
2331 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2333 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2336 /***********************************************************************
2337 * __libm_sse2_logf (MSVCRT.@)
2339 void __cdecl
MSVCRT___libm_sse2_logf(void)
2342 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2344 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2347 /***********************************************************************
2348 * __libm_sse2_pow (MSVCRT.@)
2350 void __cdecl
MSVCRT___libm_sse2_pow(void)
2353 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
2355 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
2358 /***********************************************************************
2359 * __libm_sse2_powf (MSVCRT.@)
2361 void __cdecl
MSVCRT___libm_sse2_powf(void)
2364 __asm__
__volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1
), "=g" (f2
) );
2365 f1
= powf( f1
, f2
);
2366 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f1
) );
2369 /***********************************************************************
2370 * __libm_sse2_sin (MSVCRT.@)
2372 void __cdecl
MSVCRT___libm_sse2_sin(void)
2375 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2377 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2380 /***********************************************************************
2381 * __libm_sse2_sinf (MSVCRT.@)
2383 void __cdecl
MSVCRT___libm_sse2_sinf(void)
2386 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2388 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2391 /***********************************************************************
2392 * __libm_sse2_tan (MSVCRT.@)
2394 void __cdecl
MSVCRT___libm_sse2_tan(void)
2397 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2399 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2402 /***********************************************************************
2403 * __libm_sse2_tanf (MSVCRT.@)
2405 void __cdecl
MSVCRT___libm_sse2_tanf(void)
2408 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2410 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2413 /***********************************************************************
2414 * __libm_sse2_sqrt_precise (MSVCR110.@)
2416 void __cdecl
MSVCRT___libm_sse2_sqrt_precise(void)
2419 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2421 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2424 #endif /* __i386__ */
2426 /*********************************************************************
2429 double CDECL
MSVCR120_cbrt(double x
)
2434 return x
< 0 ? -pow(-x
, 1.0 / 3.0) : pow(x
, 1.0 / 3.0);
2438 /*********************************************************************
2439 * cbrtf (MSVCR120.@)
2441 float CDECL
MSVCR120_cbrtf(float x
)
2446 return MSVCR120_cbrt(x
);
2450 /*********************************************************************
2451 * cbrtl (MSVCR120.@)
2453 LDOUBLE CDECL
MSVCR120_cbrtl(LDOUBLE x
)
2455 return MSVCR120_cbrt(x
);
2458 /*********************************************************************
2461 double CDECL
MSVCR120_exp2(double x
)
2464 double ret
= exp2(x
);
2466 double ret
= pow(2, x
);
2468 if (isfinite(x
) && !isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2472 /*********************************************************************
2473 * exp2f (MSVCR120.@)
2475 float CDECL
MSVCR120_exp2f(float x
)
2478 float ret
= exp2f(x
);
2479 if (finitef(x
) && !finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2482 return MSVCR120_exp2(x
);
2486 /*********************************************************************
2487 * exp2l (MSVCR120.@)
2489 LDOUBLE CDECL
MSVCR120_exp2l(LDOUBLE x
)
2491 return MSVCR120_exp2(x
);
2494 /*********************************************************************
2495 * expm1 (MSVCR120.@)
2497 double CDECL
MSVCR120_expm1(double x
)
2500 double ret
= expm1(x
);
2502 double ret
= exp(x
) - 1;
2504 if (isfinite(x
) && !isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2508 /*********************************************************************
2509 * expm1f (MSVCR120.@)
2511 float CDECL
MSVCR120_expm1f(float x
)
2514 float ret
= expm1f(x
);
2516 float ret
= exp(x
) - 1;
2518 if (finitef(x
) && !finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2522 /*********************************************************************
2523 * expm1l (MSVCR120.@)
2525 LDOUBLE CDECL
MSVCR120_expm1l(LDOUBLE x
)
2527 return MSVCR120_expm1(x
);
2530 /*********************************************************************
2531 * log1p (MSVCR120.@)
2533 double CDECL
MSVCR120_log1p(double x
)
2535 if (x
< -1) *MSVCRT__errno() = MSVCRT_EDOM
;
2536 else if (x
== -1) *MSVCRT__errno() = MSVCRT_ERANGE
;
2544 /*********************************************************************
2545 * log1pf (MSVCR120.@)
2547 float CDECL
MSVCR120_log1pf(float x
)
2549 if (x
< -1) *MSVCRT__errno() = MSVCRT_EDOM
;
2550 else if (x
== -1) *MSVCRT__errno() = MSVCRT_ERANGE
;
2558 /*********************************************************************
2559 * log1pl (MSVCR120.@)
2561 LDOUBLE CDECL
MSVCR120_log1pl(LDOUBLE x
)
2563 return MSVCR120_log1p(x
);
2566 /*********************************************************************
2569 double CDECL
MSVCR120_log2(double x
)
2571 if (x
< 0) *MSVCRT__errno() = MSVCRT_EDOM
;
2572 else if (x
== 0) *MSVCRT__errno() = MSVCRT_ERANGE
;
2576 return log(x
) / log(2);
2580 /*********************************************************************
2581 * log2f (MSVCR120.@)
2583 float CDECL
MSVCR120_log2f(float x
)
2586 if (x
< 0) *MSVCRT__errno() = MSVCRT_EDOM
;
2587 else if (x
== 0) *MSVCRT__errno() = MSVCRT_ERANGE
;
2590 return MSVCR120_log2(x
);
2594 /*********************************************************************
2595 * log2l (MSVCR120.@)
2597 LDOUBLE CDECL
MSVCR120_log2l(LDOUBLE x
)
2599 return MSVCR120_log2(x
);
2602 /*********************************************************************
2605 double CDECL
MSVCR120_rint(double x
)
2610 /*********************************************************************
2611 * rintf (MSVCR120.@)
2613 float CDECL
MSVCR120_rintf(float x
)
2618 /*********************************************************************
2619 * rintl (MSVCR120.@)
2621 LDOUBLE CDECL
MSVCR120_rintl(LDOUBLE x
)
2623 return MSVCR120_rint(x
);
2626 /*********************************************************************
2627 * lrint (MSVCR120.@)
2629 MSVCRT_long CDECL
MSVCR120_lrint(double x
)
2634 /*********************************************************************
2635 * lrintf (MSVCR120.@)
2637 MSVCRT_long CDECL
MSVCR120_lrintf(float x
)
2642 /*********************************************************************
2643 * lrintl (MSVCR120.@)
2645 MSVCRT_long CDECL
MSVCR120_lrintl(LDOUBLE x
)
2647 return MSVCR120_lrint(x
);
2650 /*********************************************************************
2651 * llrint (MSVCR120.@)
2653 MSVCRT_longlong CDECL
MSVCR120_llrint(double x
)
2658 /*********************************************************************
2659 * llrintf (MSVCR120.@)
2661 MSVCRT_longlong CDECL
MSVCR120_llrintf(float x
)
2666 /*********************************************************************
2667 * rintl (MSVCR120.@)
2669 MSVCRT_longlong CDECL
MSVCR120_llrintl(LDOUBLE x
)
2671 return MSVCR120_llrint(x
);
2676 /*********************************************************************
2677 * round (MSVCR120.@)
2679 double CDECL
MSVCR120_round(double x
)
2684 return MSVCR120_rint(x
);
2688 /*********************************************************************
2689 * roundf (MSVCR120.@)
2691 float CDECL
MSVCR120_roundf(float x
)
2696 return MSVCR120_round(x
);
2700 /*********************************************************************
2701 * roundl (MSVCR120.@)
2703 LDOUBLE CDECL
MSVCR120_roundl(LDOUBLE x
)
2705 return MSVCR120_round(x
);
2708 /*********************************************************************
2709 * lround (MSVCR120.@)
2711 MSVCRT_long CDECL
MSVCR120_lround(double x
)
2716 return MSVCR120_round(x
);
2720 /*********************************************************************
2721 * lroundf (MSVCR120.@)
2723 MSVCRT_long CDECL
MSVCR120_lroundf(float x
)
2728 return MSVCR120_lround(x
);
2732 /*********************************************************************
2733 * lroundl (MSVCR120.@)
2735 MSVCRT_long CDECL
MSVCR120_lroundl(LDOUBLE x
)
2737 return MSVCR120_lround(x
);
2740 /*********************************************************************
2741 * llround (MSVCR120.@)
2743 MSVCRT_longlong CDECL
MSVCR120_llround(double x
)
2748 return MSVCR120_round(x
);
2752 /*********************************************************************
2753 * llroundf (MSVCR120.@)
2755 MSVCRT_longlong CDECL
MSVCR120_llroundf(float x
)
2757 #ifdef HAVE_LLROUNDF
2760 return MSVCR120_llround(x
);
2764 /*********************************************************************
2765 * roundl (MSVCR120.@)
2767 MSVCRT_longlong CDECL
MSVCR120_llroundl(LDOUBLE x
)
2769 return MSVCR120_llround(x
);
2772 /*********************************************************************
2773 * trunc (MSVCR120.@)
2775 double CDECL
MSVCR120_trunc(double x
)
2780 return (x
> 0) ? floor(x
) : ceil(x
);
2784 /*********************************************************************
2785 * truncf (MSVCR120.@)
2787 float CDECL
MSVCR120_truncf(float x
)
2792 return MSVCR120_trunc(x
);
2796 /*********************************************************************
2797 * truncl (MSVCR120.@)
2799 LDOUBLE CDECL
MSVCR120_truncl(LDOUBLE x
)
2801 return MSVCR120_trunc(x
);
2804 /*********************************************************************
2805 * _dclass (MSVCR120.@)
2807 short CDECL
MSVCR120__dclass(double x
)
2809 switch (MSVCRT__fpclass(x
)) {
2810 case MSVCRT__FPCLASS_QNAN
:
2811 case MSVCRT__FPCLASS_SNAN
:
2812 return MSVCRT_FP_NAN
;
2813 case MSVCRT__FPCLASS_NINF
:
2814 case MSVCRT__FPCLASS_PINF
:
2815 return MSVCRT_FP_INFINITE
;
2816 case MSVCRT__FPCLASS_ND
:
2817 case MSVCRT__FPCLASS_PD
:
2818 return MSVCRT_FP_SUBNORMAL
;
2819 case MSVCRT__FPCLASS_NN
:
2820 case MSVCRT__FPCLASS_PN
:
2822 return MSVCRT_FP_NORMAL
;
2823 case MSVCRT__FPCLASS_NZ
:
2824 case MSVCRT__FPCLASS_PZ
:
2825 return MSVCRT_FP_ZERO
;
2829 /*********************************************************************
2830 * _fdclass (MSVCR120.@)
2832 short CDECL
MSVCR120__fdclass(float x
)
2834 return MSVCR120__dclass(x
);
2837 /*********************************************************************
2838 * _ldclass (MSVCR120.@)
2840 short CDECL
MSVCR120__ldclass(LDOUBLE x
)
2842 return MSVCR120__dclass(x
);
2845 /*********************************************************************
2846 * _dtest (MSVCR120.@)
2848 short CDECL
MSVCR120__dtest(double *x
)
2850 return MSVCR120__dclass(*x
);
2853 /*********************************************************************
2854 * _fdtest (MSVCR120.@)
2856 short CDECL
MSVCR120__fdtest(float *x
)
2858 return MSVCR120__dclass(*x
);
2861 /*********************************************************************
2862 * _ldtest (MSVCR120.@)
2864 short CDECL
MSVCR120__ldtest(LDOUBLE
*x
)
2866 return MSVCR120__dclass(*x
);
2869 /*********************************************************************
2872 double CDECL
MSVCR120_erf(double x
)
2877 /* Abramowitz and Stegun approximation, maximum error: 1.5*10^-7 */
2879 int sign
= signbit(x
);
2882 t
= 1 / (1 + 0.3275911 * x
);
2883 y
= ((((1.061405429*t
- 1.453152027)*t
+ 1.421413741)*t
- 0.284496736)*t
+ 0.254829592)*t
;
2884 y
= 1.0 - y
*exp(-x
*x
);
2885 return sign
? -y
: y
;
2889 /*********************************************************************
2892 float CDECL
MSVCR120_erff(float x
)
2897 return MSVCR120_erf(x
);
2901 /*********************************************************************
2904 LDOUBLE CDECL
MSVCR120_erfl(LDOUBLE x
)
2906 return MSVCR120_erf(x
);
2909 /*********************************************************************
2912 double CDECL
MSVCR120_erfc(double x
)
2917 return 1 - MSVCR120_erf(x
);
2921 /*********************************************************************
2922 * erfcf (MSVCR120.@)
2924 float CDECL
MSVCR120_erfcf(float x
)
2929 return MSVCR120_erfc(x
);
2933 /*********************************************************************
2934 * erfcl (MSVCR120.@)
2936 LDOUBLE CDECL
MSVCR120_erfcl(LDOUBLE x
)
2938 return MSVCR120_erfc(x
);
2941 /*********************************************************************
2942 * fmaxf (MSVCR120.@)
2944 float CDECL
MSVCR120_fmaxf(float x
, float y
)
2951 return signbit(x
) ? y
: x
;
2955 /*********************************************************************
2958 double CDECL
MSVCR120_fmax(double x
, double y
)
2965 return signbit(x
) ? y
: x
;
2969 /*********************************************************************
2970 * _fdsign (MSVCR120.@)
2972 int CDECL
MSVCR120__fdsign(float x
)
2974 return signbit(x
) ? 0x8000 : 0;
2977 /*********************************************************************
2978 * _dsign (MSVCR120.@)
2980 int CDECL
MSVCR120__dsign(double x
)
2982 return signbit(x
) ? 0x8000 : 0;
2986 /*********************************************************************
2987 * _dpcomp (MSVCR120.@)
2989 int CDECL
MSVCR120__dpcomp(double x
, double y
)
2991 if(isnan(x
) || isnan(y
))
2994 if(x
== y
) return 2;
2995 return x
< y
? 1 : 4;
2998 /*********************************************************************
2999 * _fdpcomp (MSVCR120.@)
3001 int CDECL
MSVCR120__fdpcomp(float x
, float y
)
3003 return MSVCR120__dpcomp(x
, y
);
3006 /*********************************************************************
3007 * fminf (MSVCR120.@)
3009 float CDECL
MSVCR120_fminf(float x
, float y
)
3016 return signbit(x
) ? x
: y
;
3020 /*********************************************************************
3023 double CDECL
MSVCR120_fmin(double x
, double y
)
3030 return signbit(x
) ? x
: y
;
3034 /*********************************************************************
3035 * asinh (MSVCR120.@)
3037 double CDECL
MSVCR120_asinh(double x
)
3042 if (!isfinite(x
*x
+1)) return log(2) + log(x
);
3043 return log(x
+ sqrt(x
*x
+1));
3047 /*********************************************************************
3048 * asinhf (MSVCR120.@)
3050 float CDECL
MSVCR120_asinhf(float x
)
3055 return MSVCR120_asinh(x
);
3059 /*********************************************************************
3060 * asinhl (MSVCR120.@)
3062 LDOUBLE CDECL
MSVCR120_asinhl(LDOUBLE x
)
3064 return MSVCR120_asinh(x
);
3067 /*********************************************************************
3068 * acosh (MSVCR120.@)
3070 double CDECL
MSVCR120_acosh(double x
)
3072 if (x
< 1) *MSVCRT__errno() = MSVCRT_EDOM
;
3080 MSVCRT_fegetenv(&env
);
3081 env
.status
|= MSVCRT__SW_INVALID
;
3082 MSVCRT_fesetenv(&env
);
3085 if (!isfinite(x
*x
)) return log(2) + log(x
);
3086 return log(x
+ sqrt(x
*x
-1));
3090 /*********************************************************************
3091 * acoshf (MSVCR120.@)
3093 float CDECL
MSVCR120_acoshf(float x
)
3096 if (x
< 1) *MSVCRT__errno() = MSVCRT_EDOM
;
3100 return MSVCR120_acosh(x
);
3104 /*********************************************************************
3105 * acoshl (MSVCR120.@)
3107 LDOUBLE CDECL
MSVCR120_acoshl(LDOUBLE x
)
3109 return MSVCR120_acosh(x
);
3112 /*********************************************************************
3113 * atanh (MSVCR120.@)
3115 double CDECL
MSVCR120_atanh(double x
)
3119 if (x
> 1 || x
< -1) {
3122 *MSVCRT__errno() = MSVCRT_EDOM
;
3124 /* on Linux atanh returns -NAN in this case */
3125 MSVCRT_fegetenv(&env
);
3126 env
.status
|= MSVCRT__SW_INVALID
;
3127 MSVCRT_fesetenv(&env
);
3134 if (-1e-6 < x
&& x
< 1e-6) ret
= x
+ x
*x
*x
/3;
3135 else ret
= (log(1+x
) - log(1-x
)) / 2;
3138 if (!isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
3142 /*********************************************************************
3143 * atanhf (MSVCR120.@)
3145 float CDECL
MSVCR120_atanhf(float x
)
3150 if (x
> 1 || x
< -1) {
3153 *MSVCRT__errno() = MSVCRT_EDOM
;
3155 MSVCRT_fegetenv(&env
);
3156 env
.status
|= MSVCRT__SW_INVALID
;
3157 MSVCRT_fesetenv(&env
);
3163 if (!finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
3166 return MSVCR120_atanh(x
);
3170 /*********************************************************************
3171 * atanhl (MSVCR120.@)
3173 LDOUBLE CDECL
MSVCR120_atanhl(LDOUBLE x
)
3175 return MSVCR120_atanh(x
);
3178 #endif /* _MSVCR_VER>=120 */
3180 /*********************************************************************
3182 * scalbn (MSVCR120.@)
3183 * scalbln (MSVCR120.@)
3185 double CDECL
MSVCRT__scalb(double num
, MSVCRT_long power
)
3187 return MSVCRT_ldexp(num
, power
);
3190 /*********************************************************************
3191 * _scalbf (MSVCRT.@)
3192 * scalbnf (MSVCR120.@)
3193 * scalblnf (MSVCR120.@)
3195 float CDECL
MSVCRT__scalbf(float num
, MSVCRT_long power
)
3197 return MSVCRT_ldexp(num
, power
);
3202 /*********************************************************************
3203 * scalbnl (MSVCR120.@)
3204 * scalblnl (MSVCR120.@)
3206 LDOUBLE CDECL
MSVCR120_scalbnl(LDOUBLE num
, MSVCRT_long power
)
3208 return MSVCRT__scalb(num
, power
);
3211 /*********************************************************************
3212 * remainder (MSVCR120.@)
3214 double CDECL
MSVCR120_remainder(double x
, double y
)
3216 #ifdef HAVE_REMAINDER
3217 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3218 if(!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
3219 if(isnan(y
) || y
==0.0) *MSVCRT__errno() = MSVCRT_EDOM
;
3220 return remainder(x
, y
);
3222 FIXME( "not implemented\n" );
3227 /*********************************************************************
3228 * remainderf (MSVCR120.@)
3230 float CDECL
MSVCR120_remainderf(float x
, float y
)
3232 #ifdef HAVE_REMAINDERF
3233 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3234 if(!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
3235 if(isnanf(y
) || y
==0.0f
) *MSVCRT__errno() = MSVCRT_EDOM
;
3236 return remainderf(x
, y
);
3238 FIXME( "not implemented\n" );
3243 /*********************************************************************
3244 * remainderl (MSVCR120.@)
3246 LDOUBLE CDECL
MSVCR120_remainderl(LDOUBLE x
, LDOUBLE y
)
3248 return MSVCR120_remainder(x
, y
);
3251 /*********************************************************************
3252 * lgamma (MSVCR120.@)
3254 double CDECL
MSVCR120_lgamma(double x
)
3259 FIXME( "not implemented\n" );
3264 /*********************************************************************
3265 * lgammaf (MSVCR120.@)
3267 float CDECL
MSVCR120_lgammaf(float x
)
3272 FIXME( "not implemented\n" );
3277 /*********************************************************************
3278 * lgammal (MSVCR120.@)
3280 LDOUBLE CDECL
MSVCR120_lgammal(LDOUBLE x
)
3282 return MSVCR120_lgamma(x
);
3285 /*********************************************************************
3288 double CDECL
MSVCR120_nan(const char *tagp
)
3290 /* Windows ignores input (MSDN) */
3294 /*********************************************************************
3297 float CDECL
MSVCR120_nanf(const char *tagp
)
3302 /*********************************************************************
3303 * _except1 (MSVCR120.@)
3305 * - find meaning of ignored cw and operation bits
3308 double CDECL
_except1(DWORD fpe
, _FP_OPERATION_CODE op
, double arg
, double res
, DWORD cw
, void *unk
)
3310 ULONG_PTR exception_arg
;
3311 DWORD exception
= 0;
3316 TRACE("(%x %x %lf %lf %x %p)\n", fpe
, op
, arg
, res
, cw
, unk
);
3319 cw
= ((cw
>> 7) & 0x3f) | ((cw
>> 3) & 0xc00);
3321 operation
= op
<< 5;
3322 exception_arg
= (ULONG_PTR
)&operation
;
3324 MSVCRT_fegetenv(&env
);
3326 if (fpe
& 0x1) { /* overflow */
3327 if ((fpe
== 0x1 && (cw
& 0x8)) || (fpe
==0x11 && (cw
& 0x28))) {
3328 /* 32-bit version also sets SW_INEXACT here */
3329 env
.status
|= MSVCRT__SW_OVERFLOW
;
3330 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3331 res
= signbit(res
) ? -INFINITY
: INFINITY
;
3333 exception
= EXCEPTION_FLT_OVERFLOW
;
3335 } else if (fpe
& 0x2) { /* underflow */
3336 if ((fpe
== 0x2 && (cw
& 0x10)) || (fpe
==0x12 && (cw
& 0x30))) {
3337 env
.status
|= MSVCRT__SW_UNDERFLOW
;
3338 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3339 res
= signbit(res
) ? -0.0 : 0.0;
3341 exception
= EXCEPTION_FLT_UNDERFLOW
;
3343 } else if (fpe
& 0x4) { /* zerodivide */
3344 if ((fpe
== 0x4 && (cw
& 0x4)) || (fpe
==0x14 && (cw
& 0x24))) {
3345 env
.status
|= MSVCRT__SW_ZERODIVIDE
;
3346 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3348 exception
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
3350 } else if (fpe
& 0x8) { /* invalid */
3351 if (fpe
== 0x8 && (cw
& 0x1)) {
3352 env
.status
|= MSVCRT__SW_INVALID
;
3354 exception
= EXCEPTION_FLT_INVALID_OPERATION
;
3356 } else if (fpe
& 0x10) { /* inexact */
3357 if (fpe
== 0x10 && (cw
& 0x20)) {
3358 env
.status
|= MSVCRT__SW_INEXACT
;
3360 exception
= EXCEPTION_FLT_INEXACT_RESULT
;
3366 MSVCRT_fesetenv(&env
);
3368 RaiseException(exception
, 0, 1, &exception_arg
);
3370 if (cw
& 0x1) fpword
|= MSVCRT__EM_INVALID
;
3371 if (cw
& 0x2) fpword
|= MSVCRT__EM_DENORMAL
;
3372 if (cw
& 0x4) fpword
|= MSVCRT__EM_ZERODIVIDE
;
3373 if (cw
& 0x8) fpword
|= MSVCRT__EM_OVERFLOW
;
3374 if (cw
& 0x10) fpword
|= MSVCRT__EM_UNDERFLOW
;
3375 if (cw
& 0x20) fpword
|= MSVCRT__EM_INEXACT
;
3378 case 0xc00: fpword
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
3379 case 0x800: fpword
|= MSVCRT__RC_UP
; break;
3380 case 0x400: fpword
|= MSVCRT__RC_DOWN
; break;
3384 case 0x0: fpword
|= MSVCRT__PC_24
; break;
3385 case 0x200: fpword
|= MSVCRT__PC_53
; break;
3386 case 0x300: fpword
|= MSVCRT__PC_64
; break;
3388 if (cw
& 0x1000) fpword
|= MSVCRT__IC_AFFINE
;
3389 _control87(fpword
, 0xffffffff);
3394 #endif /* _MSVCR_VER>=120 */