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
;
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
;
816 /*********************************************************************
819 MSVCRT_longlong CDECL
MSVCRT_llabs( MSVCRT_longlong n
)
821 return n
>= 0 ? n
: -n
;
824 /*********************************************************************
827 __int64 CDECL
_abs64( __int64 n
)
829 return n
>= 0 ? n
: -n
;
832 /*********************************************************************
835 double CDECL
MSVCRT__logb(double num
)
837 double ret
= logb(num
);
838 if (isnan(num
)) math_error(_DOMAIN
, "_logb", num
, 0, ret
);
839 else if (!num
) math_error(_SING
, "_logb", num
, 0, ret
);
843 /*********************************************************************
846 double CDECL
_hypot(double x
, double y
)
848 /* FIXME: errno handling */
849 return hypot( x
, y
);
852 /*********************************************************************
855 float CDECL
MSVCRT__hypotf(float x
, float y
)
857 /* FIXME: errno handling */
858 return hypotf( x
, y
);
861 /*********************************************************************
864 double CDECL
MSVCRT_ceil( double x
)
869 /*********************************************************************
872 double CDECL
MSVCRT_floor( double x
)
877 /*********************************************************************
880 double CDECL
MSVCRT_fabs( double x
)
885 /*********************************************************************
888 double CDECL
MSVCRT_frexp( double x
, int *exp
)
890 return frexp( x
, exp
);
893 /*********************************************************************
896 double CDECL
MSVCRT_modf( double x
, double *iptr
)
898 return modf( x
, iptr
);
901 /**********************************************************************
902 * _statusfp2 (MSVCRT.@)
904 * Not exported by native msvcrt, added in msvcr80.
906 #if defined(__i386__) || defined(__x86_64__)
907 void CDECL
_statusfp2( unsigned int *x86_sw
, unsigned int *sse2_sw
)
911 unsigned long fpword
;
915 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) );
917 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
918 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
919 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
920 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
921 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
922 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
926 if (!sse2_sw
) return;
930 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
932 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
933 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
934 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
935 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
936 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
937 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
942 FIXME( "not implemented\n" );
947 /**********************************************************************
948 * _statusfp (MSVCRT.@)
950 unsigned int CDECL
_statusfp(void)
952 #if defined(__i386__) || defined(__x86_64__)
953 unsigned int x86_sw
, sse2_sw
;
955 _statusfp2( &x86_sw
, &sse2_sw
);
956 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
957 return x86_sw
| sse2_sw
;
959 FIXME( "not implemented\n" );
964 /*********************************************************************
965 * _clearfp (MSVCRT.@)
967 unsigned int CDECL
_clearfp(void)
969 unsigned int flags
= 0;
970 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
971 unsigned long fpword
;
973 __asm__
__volatile__( "fnstsw %0; fnclex" : "=m" (fpword
) );
974 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
975 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
976 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
977 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
978 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
979 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
983 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
984 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
985 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
986 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
987 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
988 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
989 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
991 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
994 FIXME( "not implemented\n" );
999 /*********************************************************************
1000 * __fpecode (MSVCRT.@)
1002 int * CDECL
__fpecode(void)
1004 return &msvcrt_get_thread_data()->fpecode
;
1007 /*********************************************************************
1010 double CDECL
MSVCRT_ldexp(double num
, MSVCRT_long exp
)
1012 double z
= ldexp(num
,exp
);
1014 if (isfinite(num
) && !isfinite(z
))
1015 math_error(_OVERFLOW
, "ldexp", num
, exp
, z
);
1016 else if (isfinite(num
) && !z
)
1017 math_error(_UNDERFLOW
, "ldexp", num
, exp
, z
);
1018 else if (z
== 0 && signbit(z
))
1019 z
= 0.0; /* Convert -0 -> +0 */
1023 /*********************************************************************
1026 double CDECL
MSVCRT__cabs(struct MSVCRT__complex num
)
1028 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
1031 /*********************************************************************
1032 * _chgsign (MSVCRT.@)
1034 double CDECL
MSVCRT__chgsign(double num
)
1036 /* FIXME: +-infinity,Nan not tested */
1040 /*********************************************************************
1041 * __control87_2 (MSVCRT.@)
1043 * Not exported by native msvcrt, added in msvcr80.
1045 #if defined(__i386__) || defined(__x86_64__)
1046 int CDECL
__control87_2( unsigned int newval
, unsigned int mask
,
1047 unsigned int *x86_cw
, unsigned int *sse2_cw
)
1050 unsigned long fpword
;
1055 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
1057 /* Convert into mask constants */
1059 if (fpword
& 0x1) flags
|= MSVCRT__EM_INVALID
;
1060 if (fpword
& 0x2) flags
|= MSVCRT__EM_DENORMAL
;
1061 if (fpword
& 0x4) flags
|= MSVCRT__EM_ZERODIVIDE
;
1062 if (fpword
& 0x8) flags
|= MSVCRT__EM_OVERFLOW
;
1063 if (fpword
& 0x10) flags
|= MSVCRT__EM_UNDERFLOW
;
1064 if (fpword
& 0x20) flags
|= MSVCRT__EM_INEXACT
;
1065 switch (fpword
& 0xc00)
1067 case 0xc00: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
1068 case 0x800: flags
|= MSVCRT__RC_UP
; break;
1069 case 0x400: flags
|= MSVCRT__RC_DOWN
; break;
1071 switch (fpword
& 0x300)
1073 case 0x0: flags
|= MSVCRT__PC_24
; break;
1074 case 0x200: flags
|= MSVCRT__PC_53
; break;
1075 case 0x300: flags
|= MSVCRT__PC_64
; break;
1077 if (fpword
& 0x1000) flags
|= MSVCRT__IC_AFFINE
;
1079 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1082 flags
= (flags
& ~mask
) | (newval
& mask
);
1084 /* Convert (masked) value back to fp word */
1086 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x1;
1087 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x2;
1088 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x4;
1089 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x8;
1090 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x10;
1091 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x20;
1092 switch (flags
& MSVCRT__MCW_RC
)
1094 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0xc00; break;
1095 case MSVCRT__RC_UP
: fpword
|= 0x800; break;
1096 case MSVCRT__RC_DOWN
: fpword
|= 0x400; break;
1098 switch (flags
& MSVCRT__MCW_PC
)
1100 case MSVCRT__PC_64
: fpword
|= 0x300; break;
1101 case MSVCRT__PC_53
: fpword
|= 0x200; break;
1102 case MSVCRT__PC_24
: fpword
|= 0x0; break;
1104 if (flags
& MSVCRT__IC_AFFINE
) fpword
|= 0x1000;
1106 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
1111 if (!sse2_cw
) return 1;
1115 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1117 /* Convert into mask constants */
1119 if (fpword
& 0x80) flags
|= MSVCRT__EM_INVALID
;
1120 if (fpword
& 0x100) flags
|= MSVCRT__EM_DENORMAL
;
1121 if (fpword
& 0x200) flags
|= MSVCRT__EM_ZERODIVIDE
;
1122 if (fpword
& 0x400) flags
|= MSVCRT__EM_OVERFLOW
;
1123 if (fpword
& 0x800) flags
|= MSVCRT__EM_UNDERFLOW
;
1124 if (fpword
& 0x1000) flags
|= MSVCRT__EM_INEXACT
;
1125 switch (fpword
& 0x6000)
1127 case 0x6000: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
1128 case 0x4000: flags
|= MSVCRT__RC_UP
; break;
1129 case 0x2000: flags
|= MSVCRT__RC_DOWN
; break;
1131 switch (fpword
& 0x8040)
1133 case 0x0040: flags
|= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
1134 case 0x8000: flags
|= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
1135 case 0x8040: flags
|= MSVCRT__DN_FLUSH
; break;
1138 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1141 flags
= (flags
& ~mask
) | (newval
& mask
);
1143 /* Convert (masked) value back to fp word */
1145 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x80;
1146 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x100;
1147 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x200;
1148 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x400;
1149 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x800;
1150 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x1000;
1151 switch (flags
& MSVCRT__MCW_RC
)
1153 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0x6000; break;
1154 case MSVCRT__RC_UP
: fpword
|= 0x4000; break;
1155 case MSVCRT__RC_DOWN
: fpword
|= 0x2000; break;
1157 switch (flags
& MSVCRT__MCW_DN
)
1159 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
1160 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
1161 case MSVCRT__DN_FLUSH
: fpword
|= 0x8040; break;
1163 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1171 FIXME( "not implemented\n" );
1177 /*********************************************************************
1178 * _control87 (MSVCRT.@)
1180 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
1182 #if defined(__i386__) || defined(__x86_64__)
1183 unsigned int x86_cw
, sse2_cw
;
1185 __control87_2( newval
, mask
, &x86_cw
, &sse2_cw
);
1187 if ((x86_cw
^ sse2_cw
) & (MSVCRT__MCW_EM
| MSVCRT__MCW_RC
)) x86_cw
|= MSVCRT__EM_AMBIGUOUS
;
1190 FIXME( "not implemented\n" );
1195 /*********************************************************************
1196 * _controlfp (MSVCRT.@)
1198 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
1200 return _control87( newval
, mask
& ~MSVCRT__EM_DENORMAL
);
1203 /*********************************************************************
1204 * _set_controlfp (MSVCRT.@)
1206 void CDECL
_set_controlfp( unsigned int newval
, unsigned int mask
)
1208 _controlfp( newval
, mask
);
1211 /*********************************************************************
1212 * _controlfp_s (MSVCRT.@)
1214 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
1216 static const unsigned int all_flags
= (MSVCRT__MCW_EM
| MSVCRT__MCW_IC
| MSVCRT__MCW_RC
|
1217 MSVCRT__MCW_PC
| MSVCRT__MCW_DN
);
1220 if (!MSVCRT_CHECK_PMT( !(newval
& mask
& ~all_flags
) ))
1222 if (cur
) *cur
= _controlfp( 0, 0 ); /* retrieve it anyway */
1223 return MSVCRT_EINVAL
;
1225 val
= _controlfp( newval
, mask
);
1226 if (cur
) *cur
= val
;
1230 /*********************************************************************
1231 * fegetenv (MSVCR120.@)
1233 int CDECL
MSVCRT_fegetenv(MSVCRT_fenv_t
*env
)
1235 env
->control
= _controlfp(0, 0) & (MSVCRT__EM_INEXACT
| MSVCRT__EM_UNDERFLOW
|
1236 MSVCRT__EM_OVERFLOW
| MSVCRT__EM_ZERODIVIDE
| MSVCRT__EM_INVALID
);
1237 env
->status
= _statusfp();
1241 /*********************************************************************
1242 * __fpe_flt_rounds (UCRTBASE.@)
1244 int CDECL
__fpe_flt_rounds(void)
1246 unsigned int fpc
= _controlfp(0, 0) & MSVCRT__RC_CHOP
;
1251 case MSVCRT__RC_CHOP
: return 0;
1252 case MSVCRT__RC_NEAR
: return 1;
1254 case MSVCRT__RC_UP
: return 3;
1257 case MSVCRT__RC_UP
: return 2;
1263 /*********************************************************************
1264 * fegetround (MSVCR120.@)
1266 int CDECL
MSVCRT_fegetround(void)
1268 return _controlfp(0, 0) & MSVCRT__RC_CHOP
;
1271 /*********************************************************************
1272 * fesetround (MSVCR120.@)
1274 int CDECL
MSVCRT_fesetround(int round_mode
)
1276 if (round_mode
& (~MSVCRT__RC_CHOP
))
1278 _controlfp(round_mode
, MSVCRT__RC_CHOP
);
1282 /*********************************************************************
1283 * _copysign (MSVCRT.@)
1285 double CDECL
MSVCRT__copysign(double num
, double sign
)
1288 return signbit(num
) ? num
: -num
;
1289 return signbit(num
) ? -num
: num
;
1292 /*********************************************************************
1293 * _finite (MSVCRT.@)
1295 int CDECL
MSVCRT__finite(double num
)
1297 return isfinite(num
) != 0; /* See comment for _isnan() */
1300 /*********************************************************************
1301 * _fpreset (MSVCRT.@)
1303 void CDECL
_fpreset(void)
1305 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1306 const unsigned int x86_cw
= 0x27f;
1307 __asm__
__volatile__( "fninit; fldcw %0" : : "m" (x86_cw
) );
1310 const unsigned long sse2_cw
= 0x1f80;
1311 __asm__
__volatile__( "ldmxcsr %0" : : "m" (sse2_cw
) );
1314 FIXME( "not implemented\n" );
1318 /*********************************************************************
1319 * fesetenv (MSVCR120.@)
1321 int CDECL
MSVCRT_fesetenv(const MSVCRT_fenv_t
*env
)
1323 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1331 DWORD instruction_pointer
;
1339 TRACE( "(%p)\n", env
);
1341 if (!env
->control
&& !env
->status
) {
1346 __asm__
__volatile__( "fnstenv %0" : "=m" (fenv
) );
1348 fenv
.control_word
&= ~0x3d;
1349 if (env
->control
& MSVCRT__EM_INVALID
) fenv
.control_word
|= 0x1;
1350 if (env
->control
& MSVCRT__EM_ZERODIVIDE
) fenv
.control_word
|= 0x4;
1351 if (env
->control
& MSVCRT__EM_OVERFLOW
) fenv
.control_word
|= 0x8;
1352 if (env
->control
& MSVCRT__EM_UNDERFLOW
) fenv
.control_word
|= 0x10;
1353 if (env
->control
& MSVCRT__EM_INEXACT
) fenv
.control_word
|= 0x20;
1355 fenv
.status_word
&= ~0x3d;
1356 if (env
->status
& MSVCRT__SW_INVALID
) fenv
.status_word
|= 0x1;
1357 if (env
->status
& MSVCRT__SW_ZERODIVIDE
) fenv
.status_word
|= 0x4;
1358 if (env
->status
& MSVCRT__SW_OVERFLOW
) fenv
.status_word
|= 0x8;
1359 if (env
->status
& MSVCRT__SW_UNDERFLOW
) fenv
.status_word
|= 0x10;
1360 if (env
->status
& MSVCRT__SW_INEXACT
) fenv
.status_word
|= 0x20;
1362 __asm__
__volatile__( "fldenv %0" : : "m" (fenv
) : "st", "st(1)",
1363 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
1369 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1371 if (env
->control
& MSVCRT__EM_INVALID
) fpword
|= 0x80;
1372 if (env
->control
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x200;
1373 if (env
->control
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x400;
1374 if (env
->control
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x800;
1375 if (env
->control
& MSVCRT__EM_INEXACT
) fpword
|= 0x1000;
1376 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1381 FIXME( "not implemented\n" );
1386 /*********************************************************************
1389 INT CDECL
MSVCRT__isnan(double num
)
1391 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1392 * Do the same, as the result may be used in calculations
1394 return isnan(num
) != 0;
1397 /*********************************************************************
1400 double CDECL
MSVCRT__j0(double num
)
1402 /* FIXME: errno handling */
1406 /*********************************************************************
1409 double CDECL
MSVCRT__j1(double num
)
1411 /* FIXME: errno handling */
1415 /*********************************************************************
1418 double CDECL
MSVCRT__jn(int n
, double num
)
1420 /* FIXME: errno handling */
1424 /*********************************************************************
1427 double CDECL
MSVCRT__y0(double num
)
1430 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1432 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1434 *MSVCRT__errno() = MSVCRT_EDOM
;
1440 /*********************************************************************
1443 double CDECL
MSVCRT__y1(double num
)
1446 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1448 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1450 *MSVCRT__errno() = MSVCRT_EDOM
;
1456 /*********************************************************************
1459 double CDECL
MSVCRT__yn(int order
, double num
)
1462 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1463 retval
= yn(order
,num
);
1464 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1466 *MSVCRT__errno() = MSVCRT_EDOM
;
1472 /*********************************************************************
1473 * _nearbyint (MSVCRT.@)
1475 double CDECL
MSVCRT_nearbyint(double num
)
1477 #ifdef HAVE_NEARBYINT
1478 return nearbyint(num
);
1480 return num
>= 0 ? floor(num
+ 0.5) : ceil(num
- 0.5);
1484 /*********************************************************************
1485 * _nearbyintf (MSVCRT.@)
1487 float CDECL
MSVCRT_nearbyintf(float num
)
1489 #ifdef HAVE_NEARBYINTF
1490 return nearbyintf(num
);
1492 return MSVCRT_nearbyint(num
);
1496 /*********************************************************************
1497 * _nextafter (MSVCRT.@)
1499 double CDECL
MSVCRT__nextafter(double num
, double next
)
1502 if (!isfinite(num
) || !isfinite(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1503 retval
= nextafter(num
,next
);
1507 /*********************************************************************
1510 char * CDECL
MSVCRT__ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
1513 thread_data_t
*data
= msvcrt_get_thread_data();
1514 /* FIXME: check better for overflow (native supports over 300 chars) */
1515 ndigits
= min( ndigits
, 80 - 7); /* 7 : space for dec point, 1 for "e",
1516 * 4 for exponent and one for
1517 * terminating '\0' */
1518 if (!data
->efcvt_buffer
)
1519 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1526 /* handle cases with zero ndigits or less */
1528 if( prec
< 1) prec
= 2;
1529 len
= snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
1530 /* take the decimal "point away */
1532 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
1533 /* take the exponential "e" out */
1534 data
->efcvt_buffer
[ prec
] = '\0';
1535 /* read the exponent */
1536 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
1538 /* adjust for some border cases */
1539 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
1541 /* handle cases with zero ndigits or less */
1543 if( data
->efcvt_buffer
[ 0] >= '5')
1545 data
->efcvt_buffer
[ 0] = '\0';
1547 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
1548 return data
->efcvt_buffer
;
1551 /*********************************************************************
1552 * _ecvt_s (MSVCRT.@)
1554 int CDECL
MSVCRT__ecvt_s( char *buffer
, MSVCRT_size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
1558 const char infret
[] = "1#INF";
1560 if (!MSVCRT_CHECK_PMT(buffer
!= NULL
)) return MSVCRT_EINVAL
;
1561 if (!MSVCRT_CHECK_PMT(decpt
!= NULL
)) return MSVCRT_EINVAL
;
1562 if (!MSVCRT_CHECK_PMT(sign
!= NULL
)) return MSVCRT_EINVAL
;
1563 if (!MSVCRT_CHECK_PMT_ERR( length
> 2, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
1564 if (!MSVCRT_CHECK_PMT_ERR(ndigits
< (int)length
- 1, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
1566 /* special case - inf */
1567 if(number
== HUGE_VAL
|| number
== -HUGE_VAL
)
1569 memset(buffer
, '0', ndigits
);
1570 memcpy(buffer
, infret
, min(ndigits
, sizeof(infret
) - 1 ) );
1571 buffer
[ndigits
] = '\0';
1573 if(number
== -HUGE_VAL
)
1579 /* handle cases with zero ndigits or less */
1581 if( prec
< 1) prec
= 2;
1582 result
= MSVCRT_malloc(prec
+ 7);
1589 len
= snprintf(result
, prec
+ 7, "%.*le", prec
- 1, number
);
1590 /* take the decimal "point away */
1592 memmove( result
+ 1, result
+ 2, len
- 1 );
1593 /* take the exponential "e" out */
1594 result
[ prec
] = '\0';
1595 /* read the exponent */
1596 sscanf( result
+ prec
+ 1, "%d", decpt
);
1598 /* adjust for some border cases */
1599 if( result
[0] == '0')/* value is zero */
1601 /* handle cases with zero ndigits or less */
1603 if( result
[ 0] >= '5')
1607 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
1608 MSVCRT_free( result
);
1612 /***********************************************************************
1615 char * CDECL
MSVCRT__fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
1617 thread_data_t
*data
= msvcrt_get_thread_data();
1618 int stop
, dec1
, dec2
;
1619 char *ptr1
, *ptr2
, *first
;
1620 char buf
[80]; /* ought to be enough */
1622 if (!data
->efcvt_buffer
)
1623 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1631 stop
= snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1633 ptr2
= data
->efcvt_buffer
;
1638 /* For numbers below the requested resolution, work out where
1639 the decimal point will be rather than finding it in the string */
1640 if (number
< 1.0 && number
> 0.0) {
1641 dec2
= log10(number
+ 1e-10);
1642 if (-dec2
<= ndigits
) dec2
= 0;
1645 /* If requested digits is zero or less, we will need to truncate
1646 * the returned string */
1651 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1652 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1653 if (!first
) first
= ptr2
;
1654 if ((ptr1
- buf
) < stop
) {
1665 while (*ptr1
== '0') { /* Process leading zeroes */
1670 while (*ptr1
!= '\0') {
1671 if (!first
) first
= ptr2
;
1678 /* We never found a non-zero digit, then our number is either
1679 * smaller than the requested precision, or 0.0 */
1684 first
= data
->efcvt_buffer
;
1689 *decpt
= dec2
? dec2
: dec1
;
1693 /***********************************************************************
1694 * _fcvt_s (MSVCRT.@)
1696 int CDECL
MSVCRT__fcvt_s(char* outbuffer
, MSVCRT_size_t size
, double number
, int ndigits
, int *decpt
, int *sign
)
1698 int stop
, dec1
, dec2
;
1699 char *ptr1
, *ptr2
, *first
;
1700 char buf
[80]; /* ought to be enough */
1702 if (!outbuffer
|| !decpt
|| !sign
|| size
== 0)
1704 *MSVCRT__errno() = MSVCRT_EINVAL
;
1705 return MSVCRT_EINVAL
;
1714 stop
= snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1721 /* For numbers below the requested resolution, work out where
1722 the decimal point will be rather than finding it in the string */
1723 if (number
< 1.0 && number
> 0.0) {
1724 dec2
= log10(number
+ 1e-10);
1725 if (-dec2
<= ndigits
) dec2
= 0;
1728 /* If requested digits is zero or less, we will need to truncate
1729 * the returned string */
1734 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1735 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1736 if (!first
) first
= ptr2
;
1737 if ((ptr1
- buf
) < stop
) {
1751 while (*ptr1
== '0') { /* Process leading zeroes */
1752 if (number
== 0.0 && size
> 1) {
1760 while (*ptr1
!= '\0') {
1761 if (!first
) first
= ptr2
;
1771 /* We never found a non-zero digit, then our number is either
1772 * smaller than the requested precision, or 0.0 */
1773 if (!first
&& (number
<= 0.0))
1776 *decpt
= dec2
? dec2
: dec1
;
1780 /***********************************************************************
1783 char * CDECL
MSVCRT__gcvt( double number
, int ndigit
, char *buff
)
1786 *MSVCRT__errno() = MSVCRT_EINVAL
;
1791 *MSVCRT__errno() = MSVCRT_ERANGE
;
1795 MSVCRT_sprintf(buff
, "%.*g", ndigit
, number
);
1799 /***********************************************************************
1800 * _gcvt_s (MSVCRT.@)
1802 int CDECL
MSVCRT__gcvt_s(char *buff
, MSVCRT_size_t size
, double number
, int digits
)
1807 *MSVCRT__errno() = MSVCRT_EINVAL
;
1808 return MSVCRT_EINVAL
;
1811 if( digits
<0 || digits
>=size
) {
1815 *MSVCRT__errno() = MSVCRT_ERANGE
;
1816 return MSVCRT_ERANGE
;
1819 len
= MSVCRT__scprintf("%.*g", digits
, number
);
1822 *MSVCRT__errno() = MSVCRT_ERANGE
;
1823 return MSVCRT_ERANGE
;
1826 MSVCRT_sprintf(buff
, "%.*g", digits
, number
);
1830 #include <stdlib.h> /* div_t, ldiv_t */
1832 /*********************************************************************
1835 * [i386] Windows binary compatible - returns the struct in eax/edx.
1838 unsigned __int64 CDECL
MSVCRT_div(int num
, int denom
)
1840 div_t dt
= div(num
,denom
);
1841 return ((unsigned __int64
)dt
.rem
<< 32) | (unsigned int)dt
.quot
;
1844 /*********************************************************************
1847 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1849 MSVCRT_div_t CDECL
MSVCRT_div(int num
, int denom
)
1851 div_t dt
= div(num
,denom
);
1859 #endif /* ifdef __i386__ */
1862 /*********************************************************************
1865 * [i386] Windows binary compatible - returns the struct in eax/edx.
1868 unsigned __int64 CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1870 ldiv_t ldt
= ldiv(num
,denom
);
1871 return ((unsigned __int64
)ldt
.rem
<< 32) | (MSVCRT_ulong
)ldt
.quot
;
1874 /*********************************************************************
1877 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1879 MSVCRT_ldiv_t CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1881 ldiv_t result
= ldiv(num
,denom
);
1884 ret
.quot
= result
.quot
;
1885 ret
.rem
= result
.rem
;
1889 #endif /* ifdef __i386__ */
1891 /*********************************************************************
1894 MSVCRT_lldiv_t CDECL
MSVCRT_lldiv(MSVCRT_longlong num
, MSVCRT_longlong denom
)
1898 ret
.quot
= num
/ denom
;
1899 ret
.rem
= num
% denom
;
1906 /*********************************************************************
1907 * _adjust_fdiv (MSVCRT.@)
1908 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1910 int MSVCRT__adjust_fdiv
= 0;
1912 /***********************************************************************
1913 * _adj_fdiv_m16i (MSVCRT.@)
1916 * I _think_ this function is intended to work around the Pentium
1919 void __stdcall
_adj_fdiv_m16i( short arg
)
1921 TRACE("(): stub\n");
1924 /***********************************************************************
1925 * _adj_fdiv_m32 (MSVCRT.@)
1928 * I _think_ this function is intended to work around the Pentium
1931 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
1933 TRACE("(): stub\n");
1936 /***********************************************************************
1937 * _adj_fdiv_m32i (MSVCRT.@)
1940 * I _think_ this function is intended to work around the Pentium
1943 void __stdcall
_adj_fdiv_m32i( int arg
)
1945 TRACE("(): stub\n");
1948 /***********************************************************************
1949 * _adj_fdiv_m64 (MSVCRT.@)
1952 * I _think_ this function is intended to work around the Pentium
1955 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
1957 TRACE("(): stub\n");
1960 /***********************************************************************
1961 * _adj_fdiv_r (MSVCRT.@)
1963 * This function is likely to have the wrong number of arguments.
1966 * I _think_ this function is intended to work around the Pentium
1969 void _adj_fdiv_r(void)
1971 TRACE("(): stub\n");
1974 /***********************************************************************
1975 * _adj_fdivr_m16i (MSVCRT.@)
1978 * I _think_ this function is intended to work around the Pentium
1981 void __stdcall
_adj_fdivr_m16i( short arg
)
1983 TRACE("(): stub\n");
1986 /***********************************************************************
1987 * _adj_fdivr_m32 (MSVCRT.@)
1990 * I _think_ this function is intended to work around the Pentium
1993 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
1995 TRACE("(): stub\n");
1998 /***********************************************************************
1999 * _adj_fdivr_m32i (MSVCRT.@)
2002 * I _think_ this function is intended to work around the Pentium
2005 void __stdcall
_adj_fdivr_m32i( int arg
)
2007 TRACE("(): stub\n");
2010 /***********************************************************************
2011 * _adj_fdivr_m64 (MSVCRT.@)
2014 * I _think_ this function is intended to work around the Pentium
2017 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
2019 TRACE("(): stub\n");
2022 /***********************************************************************
2023 * _adj_fpatan (MSVCRT.@)
2025 * This function is likely to have the wrong number of arguments.
2028 * I _think_ this function is intended to work around the Pentium
2031 void _adj_fpatan(void)
2033 TRACE("(): stub\n");
2036 /***********************************************************************
2037 * _adj_fprem (MSVCRT.@)
2039 * This function is likely to have the wrong number of arguments.
2042 * I _think_ this function is intended to work around the Pentium
2045 void _adj_fprem(void)
2047 TRACE("(): stub\n");
2050 /***********************************************************************
2051 * _adj_fprem1 (MSVCRT.@)
2053 * This function is likely to have the wrong number of arguments.
2056 * I _think_ this function is intended to work around the Pentium
2059 void _adj_fprem1(void)
2061 TRACE("(): stub\n");
2064 /***********************************************************************
2065 * _adj_fptan (MSVCRT.@)
2067 * This function is likely to have the wrong number of arguments.
2070 * I _think_ this function is intended to work around the Pentium
2073 void _adj_fptan(void)
2075 TRACE("(): stub\n");
2078 /***********************************************************************
2079 * _safe_fdiv (MSVCRT.@)
2081 * This function is likely to have the wrong number of arguments.
2084 * I _think_ this function is intended to work around the Pentium
2087 void _safe_fdiv(void)
2089 TRACE("(): stub\n");
2092 /***********************************************************************
2093 * _safe_fdivr (MSVCRT.@)
2095 * This function is likely to have the wrong number of arguments.
2098 * I _think_ this function is intended to work around the Pentium
2101 void _safe_fdivr(void)
2103 TRACE("(): stub\n");
2106 /***********************************************************************
2107 * _safe_fprem (MSVCRT.@)
2109 * This function is likely to have the wrong number of arguments.
2112 * I _think_ this function is intended to work around the Pentium
2115 void _safe_fprem(void)
2117 TRACE("(): stub\n");
2120 /***********************************************************************
2121 * _safe_fprem1 (MSVCRT.@)
2124 * This function is likely to have the wrong number of arguments.
2127 * I _think_ this function is intended to work around the Pentium
2130 void _safe_fprem1(void)
2132 TRACE("(): stub\n");
2135 /***********************************************************************
2136 * __libm_sse2_acos (MSVCRT.@)
2138 void __cdecl
MSVCRT___libm_sse2_acos(void)
2141 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2143 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2146 /***********************************************************************
2147 * __libm_sse2_acosf (MSVCRT.@)
2149 void __cdecl
MSVCRT___libm_sse2_acosf(void)
2152 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2154 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2157 /***********************************************************************
2158 * __libm_sse2_asin (MSVCRT.@)
2160 void __cdecl
MSVCRT___libm_sse2_asin(void)
2163 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2165 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2168 /***********************************************************************
2169 * __libm_sse2_asinf (MSVCRT.@)
2171 void __cdecl
MSVCRT___libm_sse2_asinf(void)
2174 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2176 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2179 /***********************************************************************
2180 * __libm_sse2_atan (MSVCRT.@)
2182 void __cdecl
MSVCRT___libm_sse2_atan(void)
2185 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2187 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2190 /***********************************************************************
2191 * __libm_sse2_atan2 (MSVCRT.@)
2193 void __cdecl
MSVCRT___libm_sse2_atan2(void)
2196 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
2197 d1
= atan2( d1
, d2
);
2198 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
2201 /***********************************************************************
2202 * __libm_sse2_atanf (MSVCRT.@)
2204 void __cdecl
MSVCRT___libm_sse2_atanf(void)
2207 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2209 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2212 /***********************************************************************
2213 * __libm_sse2_cos (MSVCRT.@)
2215 void __cdecl
MSVCRT___libm_sse2_cos(void)
2218 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2220 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2223 /***********************************************************************
2224 * __libm_sse2_cosf (MSVCRT.@)
2226 void __cdecl
MSVCRT___libm_sse2_cosf(void)
2229 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2231 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2234 /***********************************************************************
2235 * __libm_sse2_exp (MSVCRT.@)
2237 void __cdecl
MSVCRT___libm_sse2_exp(void)
2240 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2242 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2245 /***********************************************************************
2246 * __libm_sse2_expf (MSVCRT.@)
2248 void __cdecl
MSVCRT___libm_sse2_expf(void)
2251 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2253 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2256 /***********************************************************************
2257 * __libm_sse2_log (MSVCRT.@)
2259 void __cdecl
MSVCRT___libm_sse2_log(void)
2262 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2264 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2267 /***********************************************************************
2268 * __libm_sse2_log10 (MSVCRT.@)
2270 void __cdecl
MSVCRT___libm_sse2_log10(void)
2273 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2275 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2278 /***********************************************************************
2279 * __libm_sse2_log10f (MSVCRT.@)
2281 void __cdecl
MSVCRT___libm_sse2_log10f(void)
2284 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2286 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2289 /***********************************************************************
2290 * __libm_sse2_logf (MSVCRT.@)
2292 void __cdecl
MSVCRT___libm_sse2_logf(void)
2295 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2297 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2300 /***********************************************************************
2301 * __libm_sse2_pow (MSVCRT.@)
2303 void __cdecl
MSVCRT___libm_sse2_pow(void)
2306 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
2308 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
2311 /***********************************************************************
2312 * __libm_sse2_powf (MSVCRT.@)
2314 void __cdecl
MSVCRT___libm_sse2_powf(void)
2317 __asm__
__volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1
), "=g" (f2
) );
2318 f1
= powf( f1
, f2
);
2319 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f1
) );
2322 /***********************************************************************
2323 * __libm_sse2_sin (MSVCRT.@)
2325 void __cdecl
MSVCRT___libm_sse2_sin(void)
2328 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2330 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2333 /***********************************************************************
2334 * __libm_sse2_sinf (MSVCRT.@)
2336 void __cdecl
MSVCRT___libm_sse2_sinf(void)
2339 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2341 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2344 /***********************************************************************
2345 * __libm_sse2_tan (MSVCRT.@)
2347 void __cdecl
MSVCRT___libm_sse2_tan(void)
2350 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2352 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2355 /***********************************************************************
2356 * __libm_sse2_tanf (MSVCRT.@)
2358 void __cdecl
MSVCRT___libm_sse2_tanf(void)
2361 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2363 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2366 /***********************************************************************
2367 * __libm_sse2_sqrt_precise (MSVCR110.@)
2369 void __cdecl
MSVCRT___libm_sse2_sqrt_precise(void)
2372 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2374 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2377 #endif /* __i386__ */
2379 /*********************************************************************
2382 double CDECL
MSVCR120_cbrt(double x
)
2387 return x
< 0 ? -pow(-x
, 1.0 / 3.0) : pow(x
, 1.0 / 3.0);
2391 /*********************************************************************
2392 * cbrtf (MSVCR120.@)
2394 float CDECL
MSVCR120_cbrtf(float x
)
2399 return MSVCR120_cbrt(x
);
2403 /*********************************************************************
2404 * cbrtl (MSVCR120.@)
2406 LDOUBLE CDECL
MSVCR120_cbrtl(LDOUBLE x
)
2408 return MSVCR120_cbrt(x
);
2411 /*********************************************************************
2414 double CDECL
MSVCR120_exp2(double x
)
2417 double ret
= exp2(x
);
2419 double ret
= pow(2, x
);
2421 if (isfinite(x
) && !isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2425 /*********************************************************************
2426 * exp2f (MSVCR120.@)
2428 float CDECL
MSVCR120_exp2f(float x
)
2431 float ret
= exp2f(x
);
2432 if (finitef(x
) && !finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2435 return MSVCR120_exp2(x
);
2439 /*********************************************************************
2440 * exp2l (MSVCR120.@)
2442 LDOUBLE CDECL
MSVCR120_exp2l(LDOUBLE x
)
2444 return MSVCR120_exp2(x
);
2447 /*********************************************************************
2448 * expm1 (MSVCR120.@)
2450 double CDECL
MSVCR120_expm1(double x
)
2453 double ret
= expm1(x
);
2455 double ret
= exp(x
) - 1;
2457 if (isfinite(x
) && !isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2461 /*********************************************************************
2462 * expm1f (MSVCR120.@)
2464 float CDECL
MSVCR120_expm1f(float x
)
2467 float ret
= expm1f(x
);
2469 float ret
= exp(x
) - 1;
2471 if (finitef(x
) && !finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2475 /*********************************************************************
2476 * expm1l (MSVCR120.@)
2478 LDOUBLE CDECL
MSVCR120_expm1l(LDOUBLE x
)
2480 return MSVCR120_expm1(x
);
2483 /*********************************************************************
2484 * log1p (MSVCR120.@)
2486 double CDECL
MSVCR120_log1p(double x
)
2488 if (x
< -1) *MSVCRT__errno() = MSVCRT_EDOM
;
2489 else if (x
== -1) *MSVCRT__errno() = MSVCRT_ERANGE
;
2497 /*********************************************************************
2498 * log1pf (MSVCR120.@)
2500 float CDECL
MSVCR120_log1pf(float x
)
2502 if (x
< -1) *MSVCRT__errno() = MSVCRT_EDOM
;
2503 else if (x
== -1) *MSVCRT__errno() = MSVCRT_ERANGE
;
2511 /*********************************************************************
2512 * log1pl (MSVCR120.@)
2514 LDOUBLE CDECL
MSVCR120_log1pl(LDOUBLE x
)
2516 return MSVCR120_log1p(x
);
2519 /*********************************************************************
2522 double CDECL
MSVCR120_log2(double x
)
2524 if (x
< 0) *MSVCRT__errno() = MSVCRT_EDOM
;
2525 else if (x
== 0) *MSVCRT__errno() = MSVCRT_ERANGE
;
2529 return log(x
) / log(2);
2533 /*********************************************************************
2534 * log2f (MSVCR120.@)
2536 float CDECL
MSVCR120_log2f(float x
)
2539 if (x
< 0) *MSVCRT__errno() = MSVCRT_EDOM
;
2540 else if (x
== 0) *MSVCRT__errno() = MSVCRT_ERANGE
;
2543 return MSVCR120_log2(x
);
2547 /*********************************************************************
2548 * log2l (MSVCR120.@)
2550 LDOUBLE CDECL
MSVCR120_log2l(LDOUBLE x
)
2552 return MSVCR120_log2(x
);
2555 /*********************************************************************
2558 double CDECL
MSVCR120_rint(double x
)
2563 /*********************************************************************
2564 * rintf (MSVCR120.@)
2566 float CDECL
MSVCR120_rintf(float x
)
2571 /*********************************************************************
2572 * rintl (MSVCR120.@)
2574 LDOUBLE CDECL
MSVCR120_rintl(LDOUBLE x
)
2576 return MSVCR120_rint(x
);
2579 /*********************************************************************
2580 * lrint (MSVCR120.@)
2582 MSVCRT_long CDECL
MSVCR120_lrint(double x
)
2587 /*********************************************************************
2588 * lrintf (MSVCR120.@)
2590 MSVCRT_long CDECL
MSVCR120_lrintf(float x
)
2595 /*********************************************************************
2596 * lrintl (MSVCR120.@)
2598 MSVCRT_long CDECL
MSVCR120_lrintl(LDOUBLE x
)
2600 return MSVCR120_lrint(x
);
2603 /*********************************************************************
2604 * llrint (MSVCR120.@)
2606 MSVCRT_longlong CDECL
MSVCR120_llrint(double x
)
2611 /*********************************************************************
2612 * llrintf (MSVCR120.@)
2614 MSVCRT_longlong CDECL
MSVCR120_llrintf(float x
)
2619 /*********************************************************************
2620 * rintl (MSVCR120.@)
2622 MSVCRT_longlong CDECL
MSVCR120_llrintl(LDOUBLE x
)
2624 return MSVCR120_llrint(x
);
2627 /*********************************************************************
2628 * round (MSVCR120.@)
2630 double CDECL
MSVCR120_round(double x
)
2635 return MSVCR120_rint(x
);
2639 /*********************************************************************
2640 * roundf (MSVCR120.@)
2642 float CDECL
MSVCR120_roundf(float x
)
2647 return MSVCR120_round(x
);
2651 /*********************************************************************
2652 * roundl (MSVCR120.@)
2654 LDOUBLE CDECL
MSVCR120_roundl(LDOUBLE x
)
2656 return MSVCR120_round(x
);
2659 /*********************************************************************
2660 * lround (MSVCR120.@)
2662 MSVCRT_long CDECL
MSVCR120_lround(double x
)
2667 return MSVCR120_round(x
);
2671 /*********************************************************************
2672 * lroundf (MSVCR120.@)
2674 MSVCRT_long CDECL
MSVCR120_lroundf(float x
)
2679 return MSVCR120_lround(x
);
2683 /*********************************************************************
2684 * lroundl (MSVCR120.@)
2686 MSVCRT_long CDECL
MSVCR120_lroundl(LDOUBLE x
)
2688 return MSVCR120_lround(x
);
2691 /*********************************************************************
2692 * llround (MSVCR120.@)
2694 MSVCRT_longlong CDECL
MSVCR120_llround(double x
)
2699 return MSVCR120_round(x
);
2703 /*********************************************************************
2704 * llroundf (MSVCR120.@)
2706 MSVCRT_longlong CDECL
MSVCR120_llroundf(float x
)
2708 #ifdef HAVE_LLROUNDF
2711 return MSVCR120_llround(x
);
2715 /*********************************************************************
2716 * roundl (MSVCR120.@)
2718 MSVCRT_longlong CDECL
MSVCR120_llroundl(LDOUBLE x
)
2720 return MSVCR120_llround(x
);
2723 /*********************************************************************
2724 * trunc (MSVCR120.@)
2726 double CDECL
MSVCR120_trunc(double x
)
2731 return (x
> 0) ? floor(x
) : ceil(x
);
2735 /*********************************************************************
2736 * truncf (MSVCR120.@)
2738 float CDECL
MSVCR120_truncf(float x
)
2743 return MSVCR120_trunc(x
);
2747 /*********************************************************************
2748 * truncl (MSVCR120.@)
2750 LDOUBLE CDECL
MSVCR120_truncl(LDOUBLE x
)
2752 return MSVCR120_trunc(x
);
2755 /*********************************************************************
2756 * _dclass (MSVCR120.@)
2758 short CDECL
MSVCR120__dclass(double x
)
2760 switch (MSVCRT__fpclass(x
)) {
2761 case MSVCRT__FPCLASS_QNAN
:
2762 case MSVCRT__FPCLASS_SNAN
:
2763 return MSVCRT_FP_NAN
;
2764 case MSVCRT__FPCLASS_NINF
:
2765 case MSVCRT__FPCLASS_PINF
:
2766 return MSVCRT_FP_INFINITE
;
2767 case MSVCRT__FPCLASS_ND
:
2768 case MSVCRT__FPCLASS_PD
:
2769 return MSVCRT_FP_SUBNORMAL
;
2770 case MSVCRT__FPCLASS_NN
:
2771 case MSVCRT__FPCLASS_PN
:
2773 return MSVCRT_FP_NORMAL
;
2774 case MSVCRT__FPCLASS_NZ
:
2775 case MSVCRT__FPCLASS_PZ
:
2776 return MSVCRT_FP_ZERO
;
2780 /*********************************************************************
2781 * _fdclass (MSVCR120.@)
2783 short CDECL
MSVCR120__fdclass(float x
)
2785 return MSVCR120__dclass(x
);
2788 /*********************************************************************
2789 * _ldclass (MSVCR120.@)
2791 short CDECL
MSVCR120__ldclass(LDOUBLE x
)
2793 return MSVCR120__dclass(x
);
2796 /*********************************************************************
2797 * _dtest (MSVCR120.@)
2799 short CDECL
MSVCR120__dtest(double *x
)
2801 return MSVCR120__dclass(*x
);
2804 /*********************************************************************
2805 * _fdtest (MSVCR120.@)
2807 short CDECL
MSVCR120__fdtest(float *x
)
2809 return MSVCR120__dclass(*x
);
2812 /*********************************************************************
2813 * _ldtest (MSVCR120.@)
2815 short CDECL
MSVCR120__ldtest(LDOUBLE
*x
)
2817 return MSVCR120__dclass(*x
);
2820 /*********************************************************************
2823 double CDECL
MSVCR120_erf(double x
)
2828 /* Abramowitz and Stegun approximation, maximum error: 1.5*10^-7 */
2830 int sign
= signbit(x
);
2833 t
= 1 / (1 + 0.3275911 * x
);
2834 y
= ((((1.061405429*t
- 1.453152027)*t
+ 1.421413741)*t
- 0.284496736)*t
+ 0.254829592)*t
;
2835 y
= 1.0 - y
*exp(-x
*x
);
2836 return sign
? -y
: y
;
2840 /*********************************************************************
2843 float CDECL
MSVCR120_erff(float x
)
2848 return MSVCR120_erf(x
);
2852 /*********************************************************************
2855 LDOUBLE CDECL
MSVCR120_erfl(LDOUBLE x
)
2857 return MSVCR120_erf(x
);
2860 /*********************************************************************
2863 double CDECL
MSVCR120_erfc(double x
)
2868 return 1 - MSVCR120_erf(x
);
2872 /*********************************************************************
2873 * erfcf (MSVCR120.@)
2875 float CDECL
MSVCR120_erfcf(float x
)
2880 return MSVCR120_erfc(x
);
2884 /*********************************************************************
2885 * erfcl (MSVCR120.@)
2887 LDOUBLE CDECL
MSVCR120_erfcl(LDOUBLE x
)
2889 return MSVCR120_erfc(x
);
2892 /*********************************************************************
2893 * fmaxf (MSVCR120.@)
2895 float CDECL
MSVCR120_fmaxf(float x
, float y
)
2902 return signbit(x
) ? y
: x
;
2906 /*********************************************************************
2909 double CDECL
MSVCR120_fmax(double x
, double y
)
2916 return signbit(x
) ? y
: x
;
2920 /*********************************************************************
2921 * _fdsign (MSVCR120.@)
2923 int CDECL
MSVCR120__fdsign(float x
)
2925 return signbit(x
) ? 0x8000 : 0;
2928 /*********************************************************************
2929 * _dsign (MSVCR120.@)
2931 int CDECL
MSVCR120__dsign(double x
)
2933 return signbit(x
) ? 0x8000 : 0;
2937 /*********************************************************************
2938 * _dpcomp (MSVCR120.@)
2940 int CDECL
MSVCR120__dpcomp(double x
, double y
)
2942 if(isnan(x
) || isnan(y
))
2945 if(x
== y
) return 2;
2946 return x
< y
? 1 : 4;
2949 /*********************************************************************
2950 * _fdpcomp (MSVCR120.@)
2952 int CDECL
MSVCR120__fdpcomp(float x
, float y
)
2954 return MSVCR120__dpcomp(x
, y
);
2957 /*********************************************************************
2958 * fminf (MSVCR120.@)
2960 float CDECL
MSVCR120_fminf(float x
, float y
)
2967 return signbit(x
) ? x
: y
;
2971 /*********************************************************************
2974 double CDECL
MSVCR120_fmin(double x
, double y
)
2981 return signbit(x
) ? x
: y
;
2985 /*********************************************************************
2986 * asinh (MSVCR120.@)
2988 double CDECL
MSVCR120_asinh(double x
)
2993 if (!isfinite(x
*x
+1)) return log(2) + log(x
);
2994 return log(x
+ sqrt(x
*x
+1));
2998 /*********************************************************************
2999 * asinhf (MSVCR120.@)
3001 float CDECL
MSVCR120_asinhf(float x
)
3006 return MSVCR120_asinh(x
);
3010 /*********************************************************************
3011 * asinhl (MSVCR120.@)
3013 LDOUBLE CDECL
MSVCR120_asinhl(LDOUBLE x
)
3015 return MSVCR120_asinh(x
);
3018 /*********************************************************************
3019 * acosh (MSVCR120.@)
3021 double CDECL
MSVCR120_acosh(double x
)
3023 if (x
< 1) *MSVCRT__errno() = MSVCRT_EDOM
;
3031 MSVCRT_fegetenv(&env
);
3032 env
.status
|= MSVCRT__SW_INVALID
;
3033 MSVCRT_fesetenv(&env
);
3036 if (!isfinite(x
*x
)) return log(2) + log(x
);
3037 return log(x
+ sqrt(x
*x
-1));
3041 /*********************************************************************
3042 * acoshf (MSVCR120.@)
3044 float CDECL
MSVCR120_acoshf(float x
)
3047 if (x
< 1) *MSVCRT__errno() = MSVCRT_EDOM
;
3051 return MSVCR120_acosh(x
);
3055 /*********************************************************************
3056 * acoshl (MSVCR120.@)
3058 LDOUBLE CDECL
MSVCR120_acoshl(LDOUBLE x
)
3060 return MSVCR120_acosh(x
);
3063 /*********************************************************************
3064 * atanh (MSVCR120.@)
3066 double CDECL
MSVCR120_atanh(double x
)
3070 if (x
> 1 || x
< -1) {
3073 *MSVCRT__errno() = MSVCRT_EDOM
;
3075 /* on Linux atanh returns -NAN in this case */
3076 MSVCRT_fegetenv(&env
);
3077 env
.status
|= MSVCRT__SW_INVALID
;
3078 MSVCRT_fesetenv(&env
);
3085 if (-1e-6 < x
&& x
< 1e-6) ret
= x
+ x
*x
*x
/3;
3086 else ret
= (log(1+x
) - log(1-x
)) / 2;
3089 if (!isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
3093 /*********************************************************************
3094 * atanhf (MSVCR120.@)
3096 float CDECL
MSVCR120_atanhf(float x
)
3101 if (x
> 1 || x
< -1) {
3104 *MSVCRT__errno() = MSVCRT_EDOM
;
3106 MSVCRT_fegetenv(&env
);
3107 env
.status
|= MSVCRT__SW_INVALID
;
3108 MSVCRT_fesetenv(&env
);
3114 if (!finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
3117 return MSVCR120_atanh(x
);
3121 /*********************************************************************
3122 * atanhl (MSVCR120.@)
3124 LDOUBLE CDECL
MSVCR120_atanhl(LDOUBLE x
)
3126 return MSVCR120_atanh(x
);
3129 /*********************************************************************
3131 * scalbn (MSVCR120.@)
3132 * scalbln (MSVCR120.@)
3134 double CDECL
MSVCRT__scalb(double num
, MSVCRT_long power
)
3136 return MSVCRT_ldexp(num
, power
);
3139 /*********************************************************************
3140 * _scalbf (MSVCRT.@)
3141 * scalbnf (MSVCR120.@)
3142 * scalblnf (MSVCR120.@)
3144 float CDECL
MSVCRT__scalbf(float num
, MSVCRT_long power
)
3146 return MSVCRT_ldexp(num
, power
);
3149 /*********************************************************************
3150 * scalbnl (MSVCR120.@)
3151 * scalblnl (MSVCR120.@)
3153 LDOUBLE CDECL
MSVCR120_scalbnl(LDOUBLE num
, MSVCRT_long power
)
3155 return MSVCRT__scalb(num
, power
);
3158 /*********************************************************************
3159 * remainder (MSVCR120.@)
3161 double CDECL
MSVCR120_remainder(double x
, double y
)
3163 #ifdef HAVE_REMAINDER
3164 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3165 if(!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
3166 if(isnan(y
) || y
==0.0) *MSVCRT__errno() = MSVCRT_EDOM
;
3167 return remainder(x
, y
);
3169 FIXME( "not implemented\n" );
3174 /*********************************************************************
3175 * remainderf (MSVCR120.@)
3177 float CDECL
MSVCR120_remainderf(float x
, float y
)
3179 #ifdef HAVE_REMAINDERF
3180 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3181 if(!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
3182 if(isnanf(y
) || y
==0.0f
) *MSVCRT__errno() = MSVCRT_EDOM
;
3183 return remainderf(x
, y
);
3185 FIXME( "not implemented\n" );
3190 /*********************************************************************
3191 * remainderl (MSVCR120.@)
3193 LDOUBLE CDECL
MSVCR120_remainderl(LDOUBLE x
, LDOUBLE y
)
3195 return MSVCR120_remainder(x
, y
);
3198 /*********************************************************************
3199 * lgamma (MSVCR120.@)
3201 double CDECL
MSVCR120_lgamma(double x
)
3206 FIXME( "not implemented\n" );
3211 /*********************************************************************
3212 * lgammaf (MSVCR120.@)
3214 float CDECL
MSVCR120_lgammaf(float x
)
3219 FIXME( "not implemented\n" );
3224 /*********************************************************************
3225 * lgammal (MSVCR120.@)
3227 LDOUBLE CDECL
MSVCR120_lgammal(LDOUBLE x
)
3229 return MSVCR120_lgamma(x
);
3232 /*********************************************************************
3235 double CDECL
MSVCR120_nan(const char *tagp
)
3237 /* Windows ignores input (MSDN) */
3241 /*********************************************************************
3244 float CDECL
MSVCR120_nanf(const char *tagp
)
3249 /*********************************************************************
3250 * _except1 (MSVCR120.@)
3252 * - find meaning of ignored cw and operation bits
3255 double CDECL
_except1(DWORD fpe
, _FP_OPERATION_CODE op
, double arg
, double res
, DWORD cw
, void *unk
)
3257 ULONG_PTR exception_arg
;
3258 DWORD exception
= 0;
3263 TRACE("(%x %x %lf %lf %x %p)\n", fpe
, op
, arg
, res
, cw
, unk
);
3266 cw
= ((cw
>> 7) & 0x3f) | ((cw
>> 3) & 0xc00);
3268 operation
= op
<< 5;
3269 exception_arg
= (ULONG_PTR
)&operation
;
3271 MSVCRT_fegetenv(&env
);
3273 if (fpe
& 0x1) { /* overflow */
3274 if ((fpe
== 0x1 && (cw
& 0x8)) || (fpe
==0x11 && (cw
& 0x28))) {
3275 /* 32-bit version also sets SW_INEXACT here */
3276 env
.status
|= MSVCRT__SW_OVERFLOW
;
3277 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3278 res
= signbit(res
) ? -INFINITY
: INFINITY
;
3280 exception
= EXCEPTION_FLT_OVERFLOW
;
3282 } else if (fpe
& 0x2) { /* underflow */
3283 if ((fpe
== 0x2 && (cw
& 0x10)) || (fpe
==0x12 && (cw
& 0x30))) {
3284 env
.status
|= MSVCRT__SW_UNDERFLOW
;
3285 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3286 res
= signbit(res
) ? -0.0 : 0.0;
3288 exception
= EXCEPTION_FLT_UNDERFLOW
;
3290 } else if (fpe
& 0x4) { /* zerodivide */
3291 if ((fpe
== 0x4 && (cw
& 0x4)) || (fpe
==0x14 && (cw
& 0x24))) {
3292 env
.status
|= MSVCRT__SW_ZERODIVIDE
;
3293 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3295 exception
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
3297 } else if (fpe
& 0x8) { /* invalid */
3298 if (fpe
== 0x8 && (cw
& 0x1)) {
3299 env
.status
|= MSVCRT__SW_INVALID
;
3301 exception
= EXCEPTION_FLT_INVALID_OPERATION
;
3303 } else if (fpe
& 0x10) { /* inexact */
3304 if (fpe
== 0x10 && (cw
& 0x20)) {
3305 env
.status
|= MSVCRT__SW_INEXACT
;
3307 exception
= EXCEPTION_FLT_INEXACT_RESULT
;
3313 MSVCRT_fesetenv(&env
);
3315 RaiseException(exception
, 0, 1, &exception_arg
);
3317 if (cw
& 0x1) fpword
|= MSVCRT__EM_INVALID
;
3318 if (cw
& 0x2) fpword
|= MSVCRT__EM_DENORMAL
;
3319 if (cw
& 0x4) fpword
|= MSVCRT__EM_ZERODIVIDE
;
3320 if (cw
& 0x8) fpword
|= MSVCRT__EM_OVERFLOW
;
3321 if (cw
& 0x10) fpword
|= MSVCRT__EM_UNDERFLOW
;
3322 if (cw
& 0x20) fpword
|= MSVCRT__EM_INEXACT
;
3325 case 0xc00: fpword
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
3326 case 0x800: fpword
|= MSVCRT__RC_UP
; break;
3327 case 0x400: fpword
|= MSVCRT__RC_DOWN
; break;
3331 case 0x0: fpword
|= MSVCRT__PC_24
; break;
3332 case 0x200: fpword
|= MSVCRT__PC_53
; break;
3333 case 0x300: fpword
|= MSVCRT__PC_64
; break;
3335 if (cw
& 0x1000) fpword
|= MSVCRT__IC_AFFINE
;
3336 _control87(fpword
, 0xffffffff);