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 /*********************************************************************
1421 double CDECL
MSVCRT__j1(double num
)
1423 /* FIXME: errno handling */
1427 /*********************************************************************
1430 double CDECL
MSVCRT__jn(int n
, double num
)
1432 /* FIXME: errno handling */
1436 /*********************************************************************
1439 double CDECL
MSVCRT__y0(double num
)
1442 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1444 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1446 *MSVCRT__errno() = MSVCRT_EDOM
;
1452 /*********************************************************************
1455 double CDECL
MSVCRT__y1(double num
)
1458 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1460 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1462 *MSVCRT__errno() = MSVCRT_EDOM
;
1468 /*********************************************************************
1471 double CDECL
MSVCRT__yn(int order
, double num
)
1474 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1475 retval
= yn(order
,num
);
1476 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1478 *MSVCRT__errno() = MSVCRT_EDOM
;
1486 /*********************************************************************
1487 * _nearbyint (MSVCR120.@)
1489 double CDECL
MSVCRT_nearbyint(double num
)
1491 #ifdef HAVE_NEARBYINT
1492 return nearbyint(num
);
1494 return num
>= 0 ? floor(num
+ 0.5) : ceil(num
- 0.5);
1498 /*********************************************************************
1499 * _nearbyintf (MSVCR120.@)
1501 float CDECL
MSVCRT_nearbyintf(float num
)
1503 #ifdef HAVE_NEARBYINTF
1504 return nearbyintf(num
);
1506 return MSVCRT_nearbyint(num
);
1510 #endif /* _MSVCR_VER>=120 */
1512 /*********************************************************************
1513 * _nextafter (MSVCRT.@)
1515 double CDECL
MSVCRT__nextafter(double num
, double next
)
1518 if (!isfinite(num
) || !isfinite(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1519 retval
= nextafter(num
,next
);
1523 /*********************************************************************
1526 char * CDECL
MSVCRT__ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
1529 thread_data_t
*data
= msvcrt_get_thread_data();
1530 /* FIXME: check better for overflow (native supports over 300 chars) */
1531 ndigits
= min( ndigits
, 80 - 7); /* 7 : space for dec point, 1 for "e",
1532 * 4 for exponent and one for
1533 * terminating '\0' */
1534 if (!data
->efcvt_buffer
)
1535 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1542 /* handle cases with zero ndigits or less */
1544 if( prec
< 1) prec
= 2;
1545 len
= snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
1546 /* take the decimal "point away */
1548 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
1549 /* take the exponential "e" out */
1550 data
->efcvt_buffer
[ prec
] = '\0';
1551 /* read the exponent */
1552 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
1554 /* adjust for some border cases */
1555 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
1557 /* handle cases with zero ndigits or less */
1559 if( data
->efcvt_buffer
[ 0] >= '5')
1561 data
->efcvt_buffer
[ 0] = '\0';
1563 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
1564 return data
->efcvt_buffer
;
1567 /*********************************************************************
1568 * _ecvt_s (MSVCRT.@)
1570 int CDECL
MSVCRT__ecvt_s( char *buffer
, MSVCRT_size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
1574 const char infret
[] = "1#INF";
1576 if (!MSVCRT_CHECK_PMT(buffer
!= NULL
)) return MSVCRT_EINVAL
;
1577 if (!MSVCRT_CHECK_PMT(decpt
!= NULL
)) return MSVCRT_EINVAL
;
1578 if (!MSVCRT_CHECK_PMT(sign
!= NULL
)) return MSVCRT_EINVAL
;
1579 if (!MSVCRT_CHECK_PMT_ERR( length
> 2, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
1580 if (!MSVCRT_CHECK_PMT_ERR(ndigits
< (int)length
- 1, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
1582 /* special case - inf */
1583 if(number
== HUGE_VAL
|| number
== -HUGE_VAL
)
1585 memset(buffer
, '0', ndigits
);
1586 memcpy(buffer
, infret
, min(ndigits
, sizeof(infret
) - 1 ) );
1587 buffer
[ndigits
] = '\0';
1589 if(number
== -HUGE_VAL
)
1595 /* handle cases with zero ndigits or less */
1597 if( prec
< 1) prec
= 2;
1598 result
= MSVCRT_malloc(prec
+ 7);
1605 len
= snprintf(result
, prec
+ 7, "%.*le", prec
- 1, number
);
1606 /* take the decimal "point away */
1608 memmove( result
+ 1, result
+ 2, len
- 1 );
1609 /* take the exponential "e" out */
1610 result
[ prec
] = '\0';
1611 /* read the exponent */
1612 sscanf( result
+ prec
+ 1, "%d", decpt
);
1614 /* adjust for some border cases */
1615 if( result
[0] == '0')/* value is zero */
1617 /* handle cases with zero ndigits or less */
1619 if( result
[ 0] >= '5')
1623 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
1624 MSVCRT_free( result
);
1628 /***********************************************************************
1631 char * CDECL
MSVCRT__fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
1633 thread_data_t
*data
= msvcrt_get_thread_data();
1634 int stop
, dec1
, dec2
;
1635 char *ptr1
, *ptr2
, *first
;
1636 char buf
[80]; /* ought to be enough */
1638 if (!data
->efcvt_buffer
)
1639 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1647 stop
= snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1649 ptr2
= data
->efcvt_buffer
;
1654 /* For numbers below the requested resolution, work out where
1655 the decimal point will be rather than finding it in the string */
1656 if (number
< 1.0 && number
> 0.0) {
1657 dec2
= log10(number
+ 1e-10);
1658 if (-dec2
<= ndigits
) dec2
= 0;
1661 /* If requested digits is zero or less, we will need to truncate
1662 * the returned string */
1667 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1668 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1669 if (!first
) first
= ptr2
;
1670 if ((ptr1
- buf
) < stop
) {
1681 while (*ptr1
== '0') { /* Process leading zeroes */
1686 while (*ptr1
!= '\0') {
1687 if (!first
) first
= ptr2
;
1694 /* We never found a non-zero digit, then our number is either
1695 * smaller than the requested precision, or 0.0 */
1700 first
= data
->efcvt_buffer
;
1705 *decpt
= dec2
? dec2
: dec1
;
1709 /***********************************************************************
1710 * _fcvt_s (MSVCRT.@)
1712 int CDECL
MSVCRT__fcvt_s(char* outbuffer
, MSVCRT_size_t size
, double number
, int ndigits
, int *decpt
, int *sign
)
1714 int stop
, dec1
, dec2
;
1715 char *ptr1
, *ptr2
, *first
;
1716 char buf
[80]; /* ought to be enough */
1718 if (!outbuffer
|| !decpt
|| !sign
|| size
== 0)
1720 *MSVCRT__errno() = MSVCRT_EINVAL
;
1721 return MSVCRT_EINVAL
;
1730 stop
= snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1737 /* For numbers below the requested resolution, work out where
1738 the decimal point will be rather than finding it in the string */
1739 if (number
< 1.0 && number
> 0.0) {
1740 dec2
= log10(number
+ 1e-10);
1741 if (-dec2
<= ndigits
) dec2
= 0;
1744 /* If requested digits is zero or less, we will need to truncate
1745 * the returned string */
1750 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1751 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1752 if (!first
) first
= ptr2
;
1753 if ((ptr1
- buf
) < stop
) {
1767 while (*ptr1
== '0') { /* Process leading zeroes */
1768 if (number
== 0.0 && size
> 1) {
1776 while (*ptr1
!= '\0') {
1777 if (!first
) first
= ptr2
;
1787 /* We never found a non-zero digit, then our number is either
1788 * smaller than the requested precision, or 0.0 */
1789 if (!first
&& (number
<= 0.0))
1792 *decpt
= dec2
? dec2
: dec1
;
1796 /***********************************************************************
1799 char * CDECL
MSVCRT__gcvt( double number
, int ndigit
, char *buff
)
1802 *MSVCRT__errno() = MSVCRT_EINVAL
;
1807 *MSVCRT__errno() = MSVCRT_ERANGE
;
1811 MSVCRT_sprintf(buff
, "%.*g", ndigit
, number
);
1815 /***********************************************************************
1816 * _gcvt_s (MSVCRT.@)
1818 int CDECL
MSVCRT__gcvt_s(char *buff
, MSVCRT_size_t size
, double number
, int digits
)
1823 *MSVCRT__errno() = MSVCRT_EINVAL
;
1824 return MSVCRT_EINVAL
;
1827 if( digits
<0 || digits
>=size
) {
1831 *MSVCRT__errno() = MSVCRT_ERANGE
;
1832 return MSVCRT_ERANGE
;
1835 len
= MSVCRT__scprintf("%.*g", digits
, number
);
1838 *MSVCRT__errno() = MSVCRT_ERANGE
;
1839 return MSVCRT_ERANGE
;
1842 MSVCRT_sprintf(buff
, "%.*g", digits
, number
);
1846 #include <stdlib.h> /* div_t, ldiv_t */
1848 /*********************************************************************
1851 * [i386] Windows binary compatible - returns the struct in eax/edx.
1854 unsigned __int64 CDECL
MSVCRT_div(int num
, int denom
)
1856 div_t dt
= div(num
,denom
);
1857 return ((unsigned __int64
)dt
.rem
<< 32) | (unsigned int)dt
.quot
;
1860 /*********************************************************************
1863 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1865 MSVCRT_div_t CDECL
MSVCRT_div(int num
, int denom
)
1867 div_t dt
= div(num
,denom
);
1875 #endif /* ifdef __i386__ */
1878 /*********************************************************************
1881 * [i386] Windows binary compatible - returns the struct in eax/edx.
1884 unsigned __int64 CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1886 ldiv_t ldt
= ldiv(num
,denom
);
1887 return ((unsigned __int64
)ldt
.rem
<< 32) | (MSVCRT_ulong
)ldt
.quot
;
1890 /*********************************************************************
1893 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1895 MSVCRT_ldiv_t CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1897 ldiv_t result
= ldiv(num
,denom
);
1900 ret
.quot
= result
.quot
;
1901 ret
.rem
= result
.rem
;
1905 #endif /* ifdef __i386__ */
1908 /*********************************************************************
1909 * lldiv (MSVCR100.@)
1911 MSVCRT_lldiv_t CDECL
MSVCRT_lldiv(MSVCRT_longlong num
, MSVCRT_longlong denom
)
1915 ret
.quot
= num
/ denom
;
1916 ret
.rem
= num
% denom
;
1924 /*********************************************************************
1925 * _adjust_fdiv (MSVCRT.@)
1926 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1928 int MSVCRT__adjust_fdiv
= 0;
1930 /***********************************************************************
1931 * _adj_fdiv_m16i (MSVCRT.@)
1934 * I _think_ this function is intended to work around the Pentium
1937 void __stdcall
_adj_fdiv_m16i( short arg
)
1939 TRACE("(): stub\n");
1942 /***********************************************************************
1943 * _adj_fdiv_m32 (MSVCRT.@)
1946 * I _think_ this function is intended to work around the Pentium
1949 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
1951 TRACE("(): stub\n");
1954 /***********************************************************************
1955 * _adj_fdiv_m32i (MSVCRT.@)
1958 * I _think_ this function is intended to work around the Pentium
1961 void __stdcall
_adj_fdiv_m32i( int arg
)
1963 TRACE("(): stub\n");
1966 /***********************************************************************
1967 * _adj_fdiv_m64 (MSVCRT.@)
1970 * I _think_ this function is intended to work around the Pentium
1973 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
1975 TRACE("(): stub\n");
1978 /***********************************************************************
1979 * _adj_fdiv_r (MSVCRT.@)
1981 * This function is likely to have the wrong number of arguments.
1984 * I _think_ this function is intended to work around the Pentium
1987 void _adj_fdiv_r(void)
1989 TRACE("(): stub\n");
1992 /***********************************************************************
1993 * _adj_fdivr_m16i (MSVCRT.@)
1996 * I _think_ this function is intended to work around the Pentium
1999 void __stdcall
_adj_fdivr_m16i( short arg
)
2001 TRACE("(): stub\n");
2004 /***********************************************************************
2005 * _adj_fdivr_m32 (MSVCRT.@)
2008 * I _think_ this function is intended to work around the Pentium
2011 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
2013 TRACE("(): stub\n");
2016 /***********************************************************************
2017 * _adj_fdivr_m32i (MSVCRT.@)
2020 * I _think_ this function is intended to work around the Pentium
2023 void __stdcall
_adj_fdivr_m32i( int arg
)
2025 TRACE("(): stub\n");
2028 /***********************************************************************
2029 * _adj_fdivr_m64 (MSVCRT.@)
2032 * I _think_ this function is intended to work around the Pentium
2035 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
2037 TRACE("(): stub\n");
2040 /***********************************************************************
2041 * _adj_fpatan (MSVCRT.@)
2043 * This function is likely to have the wrong number of arguments.
2046 * I _think_ this function is intended to work around the Pentium
2049 void _adj_fpatan(void)
2051 TRACE("(): stub\n");
2054 /***********************************************************************
2055 * _adj_fprem (MSVCRT.@)
2057 * This function is likely to have the wrong number of arguments.
2060 * I _think_ this function is intended to work around the Pentium
2063 void _adj_fprem(void)
2065 TRACE("(): stub\n");
2068 /***********************************************************************
2069 * _adj_fprem1 (MSVCRT.@)
2071 * This function is likely to have the wrong number of arguments.
2074 * I _think_ this function is intended to work around the Pentium
2077 void _adj_fprem1(void)
2079 TRACE("(): stub\n");
2082 /***********************************************************************
2083 * _adj_fptan (MSVCRT.@)
2085 * This function is likely to have the wrong number of arguments.
2088 * I _think_ this function is intended to work around the Pentium
2091 void _adj_fptan(void)
2093 TRACE("(): stub\n");
2096 /***********************************************************************
2097 * _safe_fdiv (MSVCRT.@)
2099 * This function is likely to have the wrong number of arguments.
2102 * I _think_ this function is intended to work around the Pentium
2105 void _safe_fdiv(void)
2107 TRACE("(): stub\n");
2110 /***********************************************************************
2111 * _safe_fdivr (MSVCRT.@)
2113 * This function is likely to have the wrong number of arguments.
2116 * I _think_ this function is intended to work around the Pentium
2119 void _safe_fdivr(void)
2121 TRACE("(): stub\n");
2124 /***********************************************************************
2125 * _safe_fprem (MSVCRT.@)
2127 * This function is likely to have the wrong number of arguments.
2130 * I _think_ this function is intended to work around the Pentium
2133 void _safe_fprem(void)
2135 TRACE("(): stub\n");
2138 /***********************************************************************
2139 * _safe_fprem1 (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_fprem1(void)
2150 TRACE("(): stub\n");
2153 /***********************************************************************
2154 * __libm_sse2_acos (MSVCRT.@)
2156 void __cdecl
MSVCRT___libm_sse2_acos(void)
2159 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2161 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2164 /***********************************************************************
2165 * __libm_sse2_acosf (MSVCRT.@)
2167 void __cdecl
MSVCRT___libm_sse2_acosf(void)
2170 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2172 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2175 /***********************************************************************
2176 * __libm_sse2_asin (MSVCRT.@)
2178 void __cdecl
MSVCRT___libm_sse2_asin(void)
2181 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2183 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2186 /***********************************************************************
2187 * __libm_sse2_asinf (MSVCRT.@)
2189 void __cdecl
MSVCRT___libm_sse2_asinf(void)
2192 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2194 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2197 /***********************************************************************
2198 * __libm_sse2_atan (MSVCRT.@)
2200 void __cdecl
MSVCRT___libm_sse2_atan(void)
2203 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2205 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2208 /***********************************************************************
2209 * __libm_sse2_atan2 (MSVCRT.@)
2211 void __cdecl
MSVCRT___libm_sse2_atan2(void)
2214 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
2215 d1
= atan2( d1
, d2
);
2216 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
2219 /***********************************************************************
2220 * __libm_sse2_atanf (MSVCRT.@)
2222 void __cdecl
MSVCRT___libm_sse2_atanf(void)
2225 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2227 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2230 /***********************************************************************
2231 * __libm_sse2_cos (MSVCRT.@)
2233 void __cdecl
MSVCRT___libm_sse2_cos(void)
2236 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2238 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2241 /***********************************************************************
2242 * __libm_sse2_cosf (MSVCRT.@)
2244 void __cdecl
MSVCRT___libm_sse2_cosf(void)
2247 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2249 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2252 /***********************************************************************
2253 * __libm_sse2_exp (MSVCRT.@)
2255 void __cdecl
MSVCRT___libm_sse2_exp(void)
2258 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2260 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2263 /***********************************************************************
2264 * __libm_sse2_expf (MSVCRT.@)
2266 void __cdecl
MSVCRT___libm_sse2_expf(void)
2269 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2271 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2274 /***********************************************************************
2275 * __libm_sse2_log (MSVCRT.@)
2277 void __cdecl
MSVCRT___libm_sse2_log(void)
2280 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2282 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2285 /***********************************************************************
2286 * __libm_sse2_log10 (MSVCRT.@)
2288 void __cdecl
MSVCRT___libm_sse2_log10(void)
2291 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2293 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2296 /***********************************************************************
2297 * __libm_sse2_log10f (MSVCRT.@)
2299 void __cdecl
MSVCRT___libm_sse2_log10f(void)
2302 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2304 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2307 /***********************************************************************
2308 * __libm_sse2_logf (MSVCRT.@)
2310 void __cdecl
MSVCRT___libm_sse2_logf(void)
2313 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2315 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2318 /***********************************************************************
2319 * __libm_sse2_pow (MSVCRT.@)
2321 void __cdecl
MSVCRT___libm_sse2_pow(void)
2324 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
2326 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
2329 /***********************************************************************
2330 * __libm_sse2_powf (MSVCRT.@)
2332 void __cdecl
MSVCRT___libm_sse2_powf(void)
2335 __asm__
__volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1
), "=g" (f2
) );
2336 f1
= powf( f1
, f2
);
2337 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f1
) );
2340 /***********************************************************************
2341 * __libm_sse2_sin (MSVCRT.@)
2343 void __cdecl
MSVCRT___libm_sse2_sin(void)
2346 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2348 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2351 /***********************************************************************
2352 * __libm_sse2_sinf (MSVCRT.@)
2354 void __cdecl
MSVCRT___libm_sse2_sinf(void)
2357 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2359 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2362 /***********************************************************************
2363 * __libm_sse2_tan (MSVCRT.@)
2365 void __cdecl
MSVCRT___libm_sse2_tan(void)
2368 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2370 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2373 /***********************************************************************
2374 * __libm_sse2_tanf (MSVCRT.@)
2376 void __cdecl
MSVCRT___libm_sse2_tanf(void)
2379 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2381 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2384 /***********************************************************************
2385 * __libm_sse2_sqrt_precise (MSVCR110.@)
2387 void __cdecl
MSVCRT___libm_sse2_sqrt_precise(void)
2390 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2392 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2395 #endif /* __i386__ */
2397 /*********************************************************************
2400 double CDECL
MSVCR120_cbrt(double x
)
2405 return x
< 0 ? -pow(-x
, 1.0 / 3.0) : pow(x
, 1.0 / 3.0);
2409 /*********************************************************************
2410 * cbrtf (MSVCR120.@)
2412 float CDECL
MSVCR120_cbrtf(float x
)
2417 return MSVCR120_cbrt(x
);
2421 /*********************************************************************
2422 * cbrtl (MSVCR120.@)
2424 LDOUBLE CDECL
MSVCR120_cbrtl(LDOUBLE x
)
2426 return MSVCR120_cbrt(x
);
2429 /*********************************************************************
2432 double CDECL
MSVCR120_exp2(double x
)
2435 double ret
= exp2(x
);
2437 double ret
= pow(2, x
);
2439 if (isfinite(x
) && !isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2443 /*********************************************************************
2444 * exp2f (MSVCR120.@)
2446 float CDECL
MSVCR120_exp2f(float x
)
2449 float ret
= exp2f(x
);
2450 if (finitef(x
) && !finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2453 return MSVCR120_exp2(x
);
2457 /*********************************************************************
2458 * exp2l (MSVCR120.@)
2460 LDOUBLE CDECL
MSVCR120_exp2l(LDOUBLE x
)
2462 return MSVCR120_exp2(x
);
2465 /*********************************************************************
2466 * expm1 (MSVCR120.@)
2468 double CDECL
MSVCR120_expm1(double x
)
2471 double ret
= expm1(x
);
2473 double ret
= exp(x
) - 1;
2475 if (isfinite(x
) && !isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2479 /*********************************************************************
2480 * expm1f (MSVCR120.@)
2482 float CDECL
MSVCR120_expm1f(float x
)
2485 float ret
= expm1f(x
);
2487 float ret
= exp(x
) - 1;
2489 if (finitef(x
) && !finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2493 /*********************************************************************
2494 * expm1l (MSVCR120.@)
2496 LDOUBLE CDECL
MSVCR120_expm1l(LDOUBLE x
)
2498 return MSVCR120_expm1(x
);
2501 /*********************************************************************
2502 * log1p (MSVCR120.@)
2504 double CDECL
MSVCR120_log1p(double x
)
2506 if (x
< -1) *MSVCRT__errno() = MSVCRT_EDOM
;
2507 else if (x
== -1) *MSVCRT__errno() = MSVCRT_ERANGE
;
2515 /*********************************************************************
2516 * log1pf (MSVCR120.@)
2518 float CDECL
MSVCR120_log1pf(float x
)
2520 if (x
< -1) *MSVCRT__errno() = MSVCRT_EDOM
;
2521 else if (x
== -1) *MSVCRT__errno() = MSVCRT_ERANGE
;
2529 /*********************************************************************
2530 * log1pl (MSVCR120.@)
2532 LDOUBLE CDECL
MSVCR120_log1pl(LDOUBLE x
)
2534 return MSVCR120_log1p(x
);
2537 /*********************************************************************
2540 double CDECL
MSVCR120_log2(double x
)
2542 if (x
< 0) *MSVCRT__errno() = MSVCRT_EDOM
;
2543 else if (x
== 0) *MSVCRT__errno() = MSVCRT_ERANGE
;
2547 return log(x
) / log(2);
2551 /*********************************************************************
2552 * log2f (MSVCR120.@)
2554 float CDECL
MSVCR120_log2f(float x
)
2557 if (x
< 0) *MSVCRT__errno() = MSVCRT_EDOM
;
2558 else if (x
== 0) *MSVCRT__errno() = MSVCRT_ERANGE
;
2561 return MSVCR120_log2(x
);
2565 /*********************************************************************
2566 * log2l (MSVCR120.@)
2568 LDOUBLE CDECL
MSVCR120_log2l(LDOUBLE x
)
2570 return MSVCR120_log2(x
);
2573 /*********************************************************************
2576 double CDECL
MSVCR120_rint(double x
)
2581 /*********************************************************************
2582 * rintf (MSVCR120.@)
2584 float CDECL
MSVCR120_rintf(float x
)
2589 /*********************************************************************
2590 * rintl (MSVCR120.@)
2592 LDOUBLE CDECL
MSVCR120_rintl(LDOUBLE x
)
2594 return MSVCR120_rint(x
);
2597 /*********************************************************************
2598 * lrint (MSVCR120.@)
2600 MSVCRT_long CDECL
MSVCR120_lrint(double x
)
2605 /*********************************************************************
2606 * lrintf (MSVCR120.@)
2608 MSVCRT_long CDECL
MSVCR120_lrintf(float x
)
2613 /*********************************************************************
2614 * lrintl (MSVCR120.@)
2616 MSVCRT_long CDECL
MSVCR120_lrintl(LDOUBLE x
)
2618 return MSVCR120_lrint(x
);
2621 /*********************************************************************
2622 * llrint (MSVCR120.@)
2624 MSVCRT_longlong CDECL
MSVCR120_llrint(double x
)
2629 /*********************************************************************
2630 * llrintf (MSVCR120.@)
2632 MSVCRT_longlong CDECL
MSVCR120_llrintf(float x
)
2637 /*********************************************************************
2638 * rintl (MSVCR120.@)
2640 MSVCRT_longlong CDECL
MSVCR120_llrintl(LDOUBLE x
)
2642 return MSVCR120_llrint(x
);
2647 /*********************************************************************
2648 * round (MSVCR120.@)
2650 double CDECL
MSVCR120_round(double x
)
2655 return MSVCR120_rint(x
);
2659 /*********************************************************************
2660 * roundf (MSVCR120.@)
2662 float CDECL
MSVCR120_roundf(float x
)
2667 return MSVCR120_round(x
);
2671 /*********************************************************************
2672 * roundl (MSVCR120.@)
2674 LDOUBLE CDECL
MSVCR120_roundl(LDOUBLE x
)
2676 return MSVCR120_round(x
);
2679 /*********************************************************************
2680 * lround (MSVCR120.@)
2682 MSVCRT_long CDECL
MSVCR120_lround(double x
)
2687 return MSVCR120_round(x
);
2691 /*********************************************************************
2692 * lroundf (MSVCR120.@)
2694 MSVCRT_long CDECL
MSVCR120_lroundf(float x
)
2699 return MSVCR120_lround(x
);
2703 /*********************************************************************
2704 * lroundl (MSVCR120.@)
2706 MSVCRT_long CDECL
MSVCR120_lroundl(LDOUBLE x
)
2708 return MSVCR120_lround(x
);
2711 /*********************************************************************
2712 * llround (MSVCR120.@)
2714 MSVCRT_longlong CDECL
MSVCR120_llround(double x
)
2719 return MSVCR120_round(x
);
2723 /*********************************************************************
2724 * llroundf (MSVCR120.@)
2726 MSVCRT_longlong CDECL
MSVCR120_llroundf(float x
)
2728 #ifdef HAVE_LLROUNDF
2731 return MSVCR120_llround(x
);
2735 /*********************************************************************
2736 * roundl (MSVCR120.@)
2738 MSVCRT_longlong CDECL
MSVCR120_llroundl(LDOUBLE x
)
2740 return MSVCR120_llround(x
);
2743 /*********************************************************************
2744 * trunc (MSVCR120.@)
2746 double CDECL
MSVCR120_trunc(double x
)
2751 return (x
> 0) ? floor(x
) : ceil(x
);
2755 /*********************************************************************
2756 * truncf (MSVCR120.@)
2758 float CDECL
MSVCR120_truncf(float x
)
2763 return MSVCR120_trunc(x
);
2767 /*********************************************************************
2768 * truncl (MSVCR120.@)
2770 LDOUBLE CDECL
MSVCR120_truncl(LDOUBLE x
)
2772 return MSVCR120_trunc(x
);
2775 /*********************************************************************
2776 * _dclass (MSVCR120.@)
2778 short CDECL
MSVCR120__dclass(double x
)
2780 switch (MSVCRT__fpclass(x
)) {
2781 case MSVCRT__FPCLASS_QNAN
:
2782 case MSVCRT__FPCLASS_SNAN
:
2783 return MSVCRT_FP_NAN
;
2784 case MSVCRT__FPCLASS_NINF
:
2785 case MSVCRT__FPCLASS_PINF
:
2786 return MSVCRT_FP_INFINITE
;
2787 case MSVCRT__FPCLASS_ND
:
2788 case MSVCRT__FPCLASS_PD
:
2789 return MSVCRT_FP_SUBNORMAL
;
2790 case MSVCRT__FPCLASS_NN
:
2791 case MSVCRT__FPCLASS_PN
:
2793 return MSVCRT_FP_NORMAL
;
2794 case MSVCRT__FPCLASS_NZ
:
2795 case MSVCRT__FPCLASS_PZ
:
2796 return MSVCRT_FP_ZERO
;
2800 /*********************************************************************
2801 * _fdclass (MSVCR120.@)
2803 short CDECL
MSVCR120__fdclass(float x
)
2805 return MSVCR120__dclass(x
);
2808 /*********************************************************************
2809 * _ldclass (MSVCR120.@)
2811 short CDECL
MSVCR120__ldclass(LDOUBLE x
)
2813 return MSVCR120__dclass(x
);
2816 /*********************************************************************
2817 * _dtest (MSVCR120.@)
2819 short CDECL
MSVCR120__dtest(double *x
)
2821 return MSVCR120__dclass(*x
);
2824 /*********************************************************************
2825 * _fdtest (MSVCR120.@)
2827 short CDECL
MSVCR120__fdtest(float *x
)
2829 return MSVCR120__dclass(*x
);
2832 /*********************************************************************
2833 * _ldtest (MSVCR120.@)
2835 short CDECL
MSVCR120__ldtest(LDOUBLE
*x
)
2837 return MSVCR120__dclass(*x
);
2840 /*********************************************************************
2843 double CDECL
MSVCR120_erf(double x
)
2848 /* Abramowitz and Stegun approximation, maximum error: 1.5*10^-7 */
2850 int sign
= signbit(x
);
2853 t
= 1 / (1 + 0.3275911 * x
);
2854 y
= ((((1.061405429*t
- 1.453152027)*t
+ 1.421413741)*t
- 0.284496736)*t
+ 0.254829592)*t
;
2855 y
= 1.0 - y
*exp(-x
*x
);
2856 return sign
? -y
: y
;
2860 /*********************************************************************
2863 float CDECL
MSVCR120_erff(float x
)
2868 return MSVCR120_erf(x
);
2872 /*********************************************************************
2875 LDOUBLE CDECL
MSVCR120_erfl(LDOUBLE x
)
2877 return MSVCR120_erf(x
);
2880 /*********************************************************************
2883 double CDECL
MSVCR120_erfc(double x
)
2888 return 1 - MSVCR120_erf(x
);
2892 /*********************************************************************
2893 * erfcf (MSVCR120.@)
2895 float CDECL
MSVCR120_erfcf(float x
)
2900 return MSVCR120_erfc(x
);
2904 /*********************************************************************
2905 * erfcl (MSVCR120.@)
2907 LDOUBLE CDECL
MSVCR120_erfcl(LDOUBLE x
)
2909 return MSVCR120_erfc(x
);
2912 /*********************************************************************
2913 * fmaxf (MSVCR120.@)
2915 float CDECL
MSVCR120_fmaxf(float x
, float y
)
2922 return signbit(x
) ? y
: x
;
2926 /*********************************************************************
2929 double CDECL
MSVCR120_fmax(double x
, double y
)
2936 return signbit(x
) ? y
: x
;
2940 /*********************************************************************
2941 * _fdsign (MSVCR120.@)
2943 int CDECL
MSVCR120__fdsign(float x
)
2945 return signbit(x
) ? 0x8000 : 0;
2948 /*********************************************************************
2949 * _dsign (MSVCR120.@)
2951 int CDECL
MSVCR120__dsign(double x
)
2953 return signbit(x
) ? 0x8000 : 0;
2957 /*********************************************************************
2958 * _dpcomp (MSVCR120.@)
2960 int CDECL
MSVCR120__dpcomp(double x
, double y
)
2962 if(isnan(x
) || isnan(y
))
2965 if(x
== y
) return 2;
2966 return x
< y
? 1 : 4;
2969 /*********************************************************************
2970 * _fdpcomp (MSVCR120.@)
2972 int CDECL
MSVCR120__fdpcomp(float x
, float y
)
2974 return MSVCR120__dpcomp(x
, y
);
2977 /*********************************************************************
2978 * fminf (MSVCR120.@)
2980 float CDECL
MSVCR120_fminf(float x
, float y
)
2987 return signbit(x
) ? x
: y
;
2991 /*********************************************************************
2994 double CDECL
MSVCR120_fmin(double x
, double y
)
3001 return signbit(x
) ? x
: y
;
3005 /*********************************************************************
3006 * asinh (MSVCR120.@)
3008 double CDECL
MSVCR120_asinh(double x
)
3013 if (!isfinite(x
*x
+1)) return log(2) + log(x
);
3014 return log(x
+ sqrt(x
*x
+1));
3018 /*********************************************************************
3019 * asinhf (MSVCR120.@)
3021 float CDECL
MSVCR120_asinhf(float x
)
3026 return MSVCR120_asinh(x
);
3030 /*********************************************************************
3031 * asinhl (MSVCR120.@)
3033 LDOUBLE CDECL
MSVCR120_asinhl(LDOUBLE x
)
3035 return MSVCR120_asinh(x
);
3038 /*********************************************************************
3039 * acosh (MSVCR120.@)
3041 double CDECL
MSVCR120_acosh(double x
)
3043 if (x
< 1) *MSVCRT__errno() = MSVCRT_EDOM
;
3051 MSVCRT_fegetenv(&env
);
3052 env
.status
|= MSVCRT__SW_INVALID
;
3053 MSVCRT_fesetenv(&env
);
3056 if (!isfinite(x
*x
)) return log(2) + log(x
);
3057 return log(x
+ sqrt(x
*x
-1));
3061 /*********************************************************************
3062 * acoshf (MSVCR120.@)
3064 float CDECL
MSVCR120_acoshf(float x
)
3067 if (x
< 1) *MSVCRT__errno() = MSVCRT_EDOM
;
3071 return MSVCR120_acosh(x
);
3075 /*********************************************************************
3076 * acoshl (MSVCR120.@)
3078 LDOUBLE CDECL
MSVCR120_acoshl(LDOUBLE x
)
3080 return MSVCR120_acosh(x
);
3083 /*********************************************************************
3084 * atanh (MSVCR120.@)
3086 double CDECL
MSVCR120_atanh(double x
)
3090 if (x
> 1 || x
< -1) {
3093 *MSVCRT__errno() = MSVCRT_EDOM
;
3095 /* on Linux atanh returns -NAN in this case */
3096 MSVCRT_fegetenv(&env
);
3097 env
.status
|= MSVCRT__SW_INVALID
;
3098 MSVCRT_fesetenv(&env
);
3105 if (-1e-6 < x
&& x
< 1e-6) ret
= x
+ x
*x
*x
/3;
3106 else ret
= (log(1+x
) - log(1-x
)) / 2;
3109 if (!isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
3113 /*********************************************************************
3114 * atanhf (MSVCR120.@)
3116 float CDECL
MSVCR120_atanhf(float x
)
3121 if (x
> 1 || x
< -1) {
3124 *MSVCRT__errno() = MSVCRT_EDOM
;
3126 MSVCRT_fegetenv(&env
);
3127 env
.status
|= MSVCRT__SW_INVALID
;
3128 MSVCRT_fesetenv(&env
);
3134 if (!finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
3137 return MSVCR120_atanh(x
);
3141 /*********************************************************************
3142 * atanhl (MSVCR120.@)
3144 LDOUBLE CDECL
MSVCR120_atanhl(LDOUBLE x
)
3146 return MSVCR120_atanh(x
);
3149 #endif /* _MSVCR_VER>=120 */
3151 /*********************************************************************
3153 * scalbn (MSVCR120.@)
3154 * scalbln (MSVCR120.@)
3156 double CDECL
MSVCRT__scalb(double num
, MSVCRT_long power
)
3158 return MSVCRT_ldexp(num
, power
);
3161 /*********************************************************************
3162 * _scalbf (MSVCRT.@)
3163 * scalbnf (MSVCR120.@)
3164 * scalblnf (MSVCR120.@)
3166 float CDECL
MSVCRT__scalbf(float num
, MSVCRT_long power
)
3168 return MSVCRT_ldexp(num
, power
);
3173 /*********************************************************************
3174 * scalbnl (MSVCR120.@)
3175 * scalblnl (MSVCR120.@)
3177 LDOUBLE CDECL
MSVCR120_scalbnl(LDOUBLE num
, MSVCRT_long power
)
3179 return MSVCRT__scalb(num
, power
);
3182 /*********************************************************************
3183 * remainder (MSVCR120.@)
3185 double CDECL
MSVCR120_remainder(double x
, double y
)
3187 #ifdef HAVE_REMAINDER
3188 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3189 if(!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
3190 if(isnan(y
) || y
==0.0) *MSVCRT__errno() = MSVCRT_EDOM
;
3191 return remainder(x
, y
);
3193 FIXME( "not implemented\n" );
3198 /*********************************************************************
3199 * remainderf (MSVCR120.@)
3201 float CDECL
MSVCR120_remainderf(float x
, float y
)
3203 #ifdef HAVE_REMAINDERF
3204 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3205 if(!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
3206 if(isnanf(y
) || y
==0.0f
) *MSVCRT__errno() = MSVCRT_EDOM
;
3207 return remainderf(x
, y
);
3209 FIXME( "not implemented\n" );
3214 /*********************************************************************
3215 * remainderl (MSVCR120.@)
3217 LDOUBLE CDECL
MSVCR120_remainderl(LDOUBLE x
, LDOUBLE y
)
3219 return MSVCR120_remainder(x
, y
);
3222 /*********************************************************************
3223 * lgamma (MSVCR120.@)
3225 double CDECL
MSVCR120_lgamma(double x
)
3230 FIXME( "not implemented\n" );
3235 /*********************************************************************
3236 * lgammaf (MSVCR120.@)
3238 float CDECL
MSVCR120_lgammaf(float x
)
3243 FIXME( "not implemented\n" );
3248 /*********************************************************************
3249 * lgammal (MSVCR120.@)
3251 LDOUBLE CDECL
MSVCR120_lgammal(LDOUBLE x
)
3253 return MSVCR120_lgamma(x
);
3256 /*********************************************************************
3259 double CDECL
MSVCR120_nan(const char *tagp
)
3261 /* Windows ignores input (MSDN) */
3265 /*********************************************************************
3268 float CDECL
MSVCR120_nanf(const char *tagp
)
3273 /*********************************************************************
3274 * _except1 (MSVCR120.@)
3276 * - find meaning of ignored cw and operation bits
3279 double CDECL
_except1(DWORD fpe
, _FP_OPERATION_CODE op
, double arg
, double res
, DWORD cw
, void *unk
)
3281 ULONG_PTR exception_arg
;
3282 DWORD exception
= 0;
3287 TRACE("(%x %x %lf %lf %x %p)\n", fpe
, op
, arg
, res
, cw
, unk
);
3290 cw
= ((cw
>> 7) & 0x3f) | ((cw
>> 3) & 0xc00);
3292 operation
= op
<< 5;
3293 exception_arg
= (ULONG_PTR
)&operation
;
3295 MSVCRT_fegetenv(&env
);
3297 if (fpe
& 0x1) { /* overflow */
3298 if ((fpe
== 0x1 && (cw
& 0x8)) || (fpe
==0x11 && (cw
& 0x28))) {
3299 /* 32-bit version also sets SW_INEXACT here */
3300 env
.status
|= MSVCRT__SW_OVERFLOW
;
3301 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3302 res
= signbit(res
) ? -INFINITY
: INFINITY
;
3304 exception
= EXCEPTION_FLT_OVERFLOW
;
3306 } else if (fpe
& 0x2) { /* underflow */
3307 if ((fpe
== 0x2 && (cw
& 0x10)) || (fpe
==0x12 && (cw
& 0x30))) {
3308 env
.status
|= MSVCRT__SW_UNDERFLOW
;
3309 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3310 res
= signbit(res
) ? -0.0 : 0.0;
3312 exception
= EXCEPTION_FLT_UNDERFLOW
;
3314 } else if (fpe
& 0x4) { /* zerodivide */
3315 if ((fpe
== 0x4 && (cw
& 0x4)) || (fpe
==0x14 && (cw
& 0x24))) {
3316 env
.status
|= MSVCRT__SW_ZERODIVIDE
;
3317 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3319 exception
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
3321 } else if (fpe
& 0x8) { /* invalid */
3322 if (fpe
== 0x8 && (cw
& 0x1)) {
3323 env
.status
|= MSVCRT__SW_INVALID
;
3325 exception
= EXCEPTION_FLT_INVALID_OPERATION
;
3327 } else if (fpe
& 0x10) { /* inexact */
3328 if (fpe
== 0x10 && (cw
& 0x20)) {
3329 env
.status
|= MSVCRT__SW_INEXACT
;
3331 exception
= EXCEPTION_FLT_INEXACT_RESULT
;
3337 MSVCRT_fesetenv(&env
);
3339 RaiseException(exception
, 0, 1, &exception_arg
);
3341 if (cw
& 0x1) fpword
|= MSVCRT__EM_INVALID
;
3342 if (cw
& 0x2) fpword
|= MSVCRT__EM_DENORMAL
;
3343 if (cw
& 0x4) fpword
|= MSVCRT__EM_ZERODIVIDE
;
3344 if (cw
& 0x8) fpword
|= MSVCRT__EM_OVERFLOW
;
3345 if (cw
& 0x10) fpword
|= MSVCRT__EM_UNDERFLOW
;
3346 if (cw
& 0x20) fpword
|= MSVCRT__EM_INEXACT
;
3349 case 0xc00: fpword
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
3350 case 0x800: fpword
|= MSVCRT__RC_UP
; break;
3351 case 0x400: fpword
|= MSVCRT__RC_DOWN
; break;
3355 case 0x0: fpword
|= MSVCRT__PC_24
; break;
3356 case 0x200: fpword
|= MSVCRT__PC_53
; break;
3357 case 0x300: fpword
|= MSVCRT__PC_64
; break;
3359 if (cw
& 0x1000) fpword
|= MSVCRT__IC_AFFINE
;
3360 _control87(fpword
, 0xffffffff);
3365 #endif /* _MSVCR_VER>=120 */