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)
41 /* FIXME: Does not work with -NAN and -0. */
43 #define signbit(x) ((x) < 0)
46 #define _DOMAIN 1 /* domain error in argument */
47 #define _SING 2 /* singularity */
48 #define _OVERFLOW 3 /* range overflow */
49 #define _UNDERFLOW 4 /* range underflow */
51 typedef int (CDECL
*MSVCRT_matherr_func
)(struct MSVCRT__exception
*);
52 typedef double LDOUBLE
; /* long double is just a double */
54 static MSVCRT_matherr_func MSVCRT_default_matherr_func
= NULL
;
56 static BOOL sse2_supported
;
57 static BOOL sse2_enabled
;
59 void msvcrt_init_math(void)
61 sse2_supported
= sse2_enabled
= IsProcessorFeaturePresent( PF_XMMI64_INSTRUCTIONS_AVAILABLE
);
64 /*********************************************************************
67 int CDECL
MSVCRT__matherr(struct MSVCRT__exception
*e
)
73 static void math_error(int type
, const char *name
, double arg1
, double arg2
, double retval
)
75 TRACE("(%d, %s, %g, %g, %g)\n", type
, debugstr_a(name
), arg1
, arg2
, retval
);
77 if (MSVCRT_default_matherr_func
)
79 struct MSVCRT__exception exception
= {type
, (char *)name
, arg1
, arg2
, retval
};
81 if (MSVCRT_default_matherr_func(&exception
)) return;
87 *MSVCRT__errno() = MSVCRT_EDOM
;
91 *MSVCRT__errno() = MSVCRT_ERANGE
;
97 ERR("Unhandled math error!\n");
101 /*********************************************************************
102 * __setusermatherr (MSVCRT.@)
104 void CDECL
MSVCRT___setusermatherr(MSVCRT_matherr_func func
)
106 MSVCRT_default_matherr_func
= func
;
107 TRACE("new matherr handler %p\n", func
);
110 /*********************************************************************
111 * _set_SSE2_enable (MSVCRT.@)
113 int CDECL
MSVCRT__set_SSE2_enable(int flag
)
115 sse2_enabled
= flag
&& sse2_supported
;
119 #if defined(_WIN64) && _MSVCR_VER>=120
120 /*********************************************************************
121 * _set_FMA3_enable (MSVCR120.@)
123 int CDECL
MSVCRT__set_FMA3_enable(int flag
)
125 FIXME("(%x) stub\n", flag
);
130 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) || _MSVCR_VER>=120
132 /*********************************************************************
133 * _chgsignf (MSVCRT.@)
135 float CDECL
MSVCRT__chgsignf( float num
)
137 /* FIXME: +-infinity,Nan not tested */
141 /*********************************************************************
142 * _copysignf (MSVCRT.@)
144 float CDECL
MSVCRT__copysignf( float num
, float sign
)
147 return signbit(num
) ? num
: -num
;
148 return signbit(num
) ? -num
: num
;
151 /*********************************************************************
152 * _nextafterf (MSVCRT.@)
154 float CDECL
MSVCRT__nextafterf( float num
, float next
)
156 if (!finitef(num
) || !finitef(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
157 return nextafterf( num
, next
);
161 #if defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
163 /*********************************************************************
164 * _finitef (MSVCRT.@)
166 int CDECL
MSVCRT__finitef( float num
)
168 return finitef(num
) != 0; /* See comment for _isnan() */
171 /*********************************************************************
174 INT CDECL
MSVCRT__isnanf( float num
)
176 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
177 * Do the same, as the result may be used in calculations
179 return isnan(num
) != 0;
182 /*********************************************************************
185 float CDECL
MSVCRT__logbf( float num
)
187 float ret
= logbf(num
);
188 if (isnan(num
)) math_error(_DOMAIN
, "_logbf", num
, 0, ret
);
189 else if (!num
) math_error(_SING
, "_logbf", num
, 0, ret
);
193 /*********************************************************************
194 * MSVCRT_acosf (MSVCRT.@)
196 float CDECL
MSVCRT_acosf( float x
)
198 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
199 * asin() uses a similar construction. This is bad because as x gets nearer to
200 * 1 the error in the expression "1 - x^2" can get relatively large due to
201 * cancellation. The sqrt() makes things worse. A safer way to calculate
202 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
203 float ret
= atan2f(sqrtf((1 - x
) * (1 + x
)), x
);
204 if (x
< -1.0 || x
> 1.0 || !finitef(x
)) math_error(_DOMAIN
, "acosf", x
, 0, ret
);
208 /*********************************************************************
209 * MSVCRT_asinf (MSVCRT.@)
211 float CDECL
MSVCRT_asinf( float x
)
213 float ret
= atan2f(x
, sqrtf((1 - x
) * (1 + x
)));
214 if (x
< -1.0 || x
> 1.0 || !finitef(x
)) math_error(_DOMAIN
, "asinf", x
, 0, ret
);
218 /*********************************************************************
219 * MSVCRT_atanf (MSVCRT.@)
221 float CDECL
MSVCRT_atanf( float x
)
223 float ret
= atanf(x
);
224 if (!finitef(x
)) math_error(_DOMAIN
, "atanf", x
, 0, ret
);
228 /*********************************************************************
229 * MSVCRT_atan2f (MSVCRT.@)
231 float CDECL
MSVCRT_atan2f( float x
, float y
)
233 float ret
= atan2f(x
, y
);
234 if (isnan(x
)) math_error(_DOMAIN
, "atan2f", x
, y
, ret
);
238 /*********************************************************************
239 * MSVCRT_cosf (MSVCRT.@)
241 float CDECL
MSVCRT_cosf( float x
)
244 if (!finitef(x
)) math_error(_DOMAIN
, "cosf", x
, 0, ret
);
248 /*********************************************************************
249 * MSVCRT_coshf (MSVCRT.@)
251 float CDECL
MSVCRT_coshf( float x
)
253 float ret
= coshf(x
);
254 if (isnan(x
)) math_error(_DOMAIN
, "coshf", x
, 0, ret
);
258 /*********************************************************************
259 * MSVCRT_expf (MSVCRT.@)
261 float CDECL
MSVCRT_expf( float x
)
264 if (isnan(x
)) math_error(_DOMAIN
, "expf", x
, 0, ret
);
265 else if (finitef(x
) && !ret
) math_error(_UNDERFLOW
, "expf", x
, 0, ret
);
266 else if (finitef(x
) && !finitef(ret
)) math_error(_OVERFLOW
, "expf", x
, 0, ret
);
270 /*********************************************************************
271 * MSVCRT_fmodf (MSVCRT.@)
273 float CDECL
MSVCRT_fmodf( float x
, float y
)
275 float ret
= fmodf(x
, y
);
276 if (!finitef(x
) || !finitef(y
)) math_error(_DOMAIN
, "fmodf", x
, 0, ret
);
280 /*********************************************************************
281 * MSVCRT_logf (MSVCRT.@)
283 float CDECL
MSVCRT_logf( float x
)
286 if (x
< 0.0) math_error(_DOMAIN
, "logf", x
, 0, ret
);
287 else if (x
== 0.0) math_error(_SING
, "logf", x
, 0, ret
);
291 /*********************************************************************
292 * MSVCRT_log10f (MSVCRT.@)
294 float CDECL
MSVCRT_log10f( float x
)
296 float ret
= log10f(x
);
297 if (x
< 0.0) math_error(_DOMAIN
, "log10f", x
, 0, ret
);
298 else if (x
== 0.0) math_error(_SING
, "log10f", x
, 0, ret
);
302 /*********************************************************************
303 * MSVCRT_powf (MSVCRT.@)
305 float CDECL
MSVCRT_powf( float x
, float y
)
308 if (x
< 0 && y
!= floorf(y
)) math_error(_DOMAIN
, "powf", x
, y
, z
);
309 else if (!x
&& finitef(y
) && y
< 0) math_error(_SING
, "powf", x
, y
, z
);
310 else if (finitef(x
) && finitef(y
) && !finitef(z
)) math_error(_OVERFLOW
, "powf", x
, y
, z
);
311 else if (x
&& finitef(x
) && finitef(y
) && !z
) math_error(_UNDERFLOW
, "powf", x
, y
, z
);
315 /*********************************************************************
316 * MSVCRT_sinf (MSVCRT.@)
318 float CDECL
MSVCRT_sinf( float x
)
321 if (!finitef(x
)) math_error(_DOMAIN
, "sinf", x
, 0, ret
);
325 /*********************************************************************
326 * MSVCRT_sinhf (MSVCRT.@)
328 float CDECL
MSVCRT_sinhf( float x
)
330 float ret
= sinhf(x
);
331 if (isnan(x
)) math_error(_DOMAIN
, "sinhf", x
, 0, ret
);
335 /*********************************************************************
336 * MSVCRT_sqrtf (MSVCRT.@)
338 float CDECL
MSVCRT_sqrtf( float x
)
340 float ret
= sqrtf(x
);
341 if (x
< 0.0) math_error(_DOMAIN
, "sqrtf", x
, 0, ret
);
345 /*********************************************************************
346 * MSVCRT_tanf (MSVCRT.@)
348 float CDECL
MSVCRT_tanf( float x
)
351 if (!finitef(x
)) math_error(_DOMAIN
, "tanf", x
, 0, ret
);
355 /*********************************************************************
356 * MSVCRT_tanhf (MSVCRT.@)
358 float CDECL
MSVCRT_tanhf( float x
)
360 float ret
= tanhf(x
);
361 if (!finitef(x
)) math_error(_DOMAIN
, "tanhf", x
, 0, ret
);
365 /*********************************************************************
368 float CDECL
MSVCRT_ceilf( float x
)
373 /*********************************************************************
376 float CDECL
MSVCRT_fabsf( float x
)
381 /*********************************************************************
384 float CDECL
MSVCRT_floorf( float x
)
389 /*********************************************************************
392 float CDECL
MSVCRT_frexpf( float x
, int *exp
)
394 return frexpf( x
, exp
);
397 /*********************************************************************
400 float CDECL
MSVCRT_modff( float x
, float *iptr
)
402 return modff( x
, iptr
);
407 /*********************************************************************
408 * MSVCRT_acos (MSVCRT.@)
410 double CDECL
MSVCRT_acos( double x
)
412 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
413 * asin() uses a similar construction. This is bad because as x gets nearer to
414 * 1 the error in the expression "1 - x^2" can get relatively large due to
415 * cancellation. The sqrt() makes things worse. A safer way to calculate
416 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
417 double ret
= atan2(sqrt((1 - x
) * (1 + x
)), x
);
418 if (x
< -1.0 || x
> 1.0 || !isfinite(x
)) math_error(_DOMAIN
, "acos", x
, 0, ret
);
422 /*********************************************************************
423 * MSVCRT_asin (MSVCRT.@)
425 double CDECL
MSVCRT_asin( double x
)
427 double ret
= atan2(x
, sqrt((1 - x
) * (1 + x
)));
428 if (x
< -1.0 || x
> 1.0 || !isfinite(x
)) math_error(_DOMAIN
, "asin", x
, 0, ret
);
432 /*********************************************************************
433 * MSVCRT_atan (MSVCRT.@)
435 double CDECL
MSVCRT_atan( double x
)
437 double ret
= atan(x
);
438 if (isnan(x
)) math_error(_DOMAIN
, "atan", x
, 0, ret
);
442 /*********************************************************************
443 * MSVCRT_atan2 (MSVCRT.@)
445 double CDECL
MSVCRT_atan2( double x
, double y
)
447 double ret
= atan2(x
, y
);
448 if (isnan(x
)) math_error(_DOMAIN
, "atan2", x
, y
, ret
);
452 /*********************************************************************
453 * MSVCRT_cos (MSVCRT.@)
455 double CDECL
MSVCRT_cos( double x
)
458 if (!isfinite(x
)) math_error(_DOMAIN
, "cos", x
, 0, ret
);
462 /*********************************************************************
463 * MSVCRT_cosh (MSVCRT.@)
465 double CDECL
MSVCRT_cosh( double x
)
467 double ret
= cosh(x
);
468 if (isnan(x
)) math_error(_DOMAIN
, "cosh", x
, 0, ret
);
472 /*********************************************************************
473 * MSVCRT_exp (MSVCRT.@)
475 double CDECL
MSVCRT_exp( double x
)
478 if (isnan(x
)) math_error(_DOMAIN
, "exp", x
, 0, ret
);
479 else if (isfinite(x
) && !ret
) math_error(_UNDERFLOW
, "exp", x
, 0, ret
);
480 else if (isfinite(x
) && !isfinite(ret
)) math_error(_OVERFLOW
, "exp", x
, 0, ret
);
484 /*********************************************************************
485 * MSVCRT_fmod (MSVCRT.@)
487 double CDECL
MSVCRT_fmod( double x
, double y
)
489 double ret
= fmod(x
, y
);
490 if (!isfinite(x
) || !isfinite(y
)) math_error(_DOMAIN
, "fmod", x
, y
, ret
);
494 /*********************************************************************
495 * MSVCRT_log (MSVCRT.@)
497 double CDECL
MSVCRT_log( double x
)
500 if (x
< 0.0) math_error(_DOMAIN
, "log", x
, 0, ret
);
501 else if (x
== 0.0) math_error(_SING
, "log", x
, 0, ret
);
505 /*********************************************************************
506 * MSVCRT_log10 (MSVCRT.@)
508 double CDECL
MSVCRT_log10( double x
)
510 double ret
= log10(x
);
511 if (x
< 0.0) math_error(_DOMAIN
, "log10", x
, 0, ret
);
512 else if (x
== 0.0) math_error(_SING
, "log10", x
, 0, ret
);
516 /*********************************************************************
517 * MSVCRT_pow (MSVCRT.@)
519 double CDECL
MSVCRT_pow( double x
, double y
)
522 if (x
< 0 && y
!= floor(y
)) math_error(_DOMAIN
, "pow", x
, y
, z
);
523 else if (!x
&& isfinite(y
) && y
< 0) math_error(_SING
, "pow", x
, y
, z
);
524 else if (isfinite(x
) && isfinite(y
) && !isfinite(z
)) math_error(_OVERFLOW
, "pow", x
, y
, z
);
525 else if (x
&& isfinite(x
) && isfinite(y
) && !z
) math_error(_UNDERFLOW
, "pow", x
, y
, z
);
529 /*********************************************************************
530 * MSVCRT_sin (MSVCRT.@)
532 double CDECL
MSVCRT_sin( double x
)
535 if (!isfinite(x
)) math_error(_DOMAIN
, "sin", x
, 0, ret
);
539 /*********************************************************************
540 * MSVCRT_sinh (MSVCRT.@)
542 double CDECL
MSVCRT_sinh( double x
)
544 double ret
= sinh(x
);
545 if (isnan(x
)) math_error(_DOMAIN
, "sinh", x
, 0, ret
);
549 /*********************************************************************
550 * MSVCRT_sqrt (MSVCRT.@)
552 double CDECL
MSVCRT_sqrt( double x
)
554 double ret
= sqrt(x
);
555 if (x
< 0.0) math_error(_DOMAIN
, "sqrt", x
, 0, ret
);
559 /*********************************************************************
560 * MSVCRT_tan (MSVCRT.@)
562 double CDECL
MSVCRT_tan( double x
)
565 if (!isfinite(x
)) math_error(_DOMAIN
, "tan", x
, 0, ret
);
569 /*********************************************************************
570 * MSVCRT_tanh (MSVCRT.@)
572 double CDECL
MSVCRT_tanh( double x
)
574 double ret
= tanh(x
);
575 if (isnan(x
)) math_error(_DOMAIN
, "tanh", x
, 0, ret
);
580 #if defined(__GNUC__) && defined(__i386__)
582 #define CREATE_FPU_FUNC1(name, call) \
583 __ASM_GLOBAL_FUNC(name, \
585 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
586 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
587 "movl %esp, %ebp\n\t" \
588 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
589 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
590 "fstpl (%esp)\n\t" /* store function argument */ \
592 "movl $1, %ecx\n\t" /* empty FPU stack */ \
596 "and $0x4500, %ax\n\t" \
597 "cmp $0x4100, %ax\n\t" \
599 "fstpl (%esp,%ecx,8)\n\t" \
604 "movl %ecx, -4(%ebp)\n\t" \
605 "call " __ASM_NAME( #call ) "\n\t" \
606 "movl -4(%ebp), %ecx\n\t" \
607 "fstpl (%esp)\n\t" /* save result */ \
608 "3:\n\t" /* restore FPU stack */ \
610 "fldl (%esp,%ecx,8)\n\t" \
611 "cmpl $0, %ecx\n\t" \
614 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
615 __ASM_CFI(".cfi_same_value %ebp\n\t") \
618 #define CREATE_FPU_FUNC2(name, call) \
619 __ASM_GLOBAL_FUNC(name, \
621 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t") \
622 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t") \
623 "movl %esp, %ebp\n\t" \
624 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t") \
625 "subl $68, %esp\n\t" /* sizeof(double)*8 + sizeof(int) */ \
626 "fstpl 8(%esp)\n\t" /* store function argument */ \
630 "movl $2, %ecx\n\t" /* empty FPU stack */ \
634 "and $0x4500, %ax\n\t" \
635 "cmp $0x4100, %ax\n\t" \
637 "fstpl (%esp,%ecx,8)\n\t" \
642 "movl %ecx, -4(%ebp)\n\t" \
643 "call " __ASM_NAME( #call ) "\n\t" \
644 "movl -4(%ebp), %ecx\n\t" \
645 "fstpl 8(%esp)\n\t" /* save result */ \
646 "3:\n\t" /* restore FPU stack */ \
648 "fldl (%esp,%ecx,8)\n\t" \
649 "cmpl $1, %ecx\n\t" \
652 __ASM_CFI(".cfi_def_cfa %esp,4\n\t") \
653 __ASM_CFI(".cfi_same_value %ebp\n\t") \
656 CREATE_FPU_FUNC1(_CIacos
, MSVCRT_acos
)
657 CREATE_FPU_FUNC1(_CIasin
, MSVCRT_asin
)
658 CREATE_FPU_FUNC1(_CIatan
, MSVCRT_atan
)
659 CREATE_FPU_FUNC2(_CIatan2
, MSVCRT_atan2
)
660 CREATE_FPU_FUNC1(_CIcos
, MSVCRT_cos
)
661 CREATE_FPU_FUNC1(_CIcosh
, MSVCRT_cosh
)
662 CREATE_FPU_FUNC1(_CIexp
, MSVCRT_exp
)
663 CREATE_FPU_FUNC2(_CIfmod
, MSVCRT_fmod
)
664 CREATE_FPU_FUNC1(_CIlog
, MSVCRT_log
)
665 CREATE_FPU_FUNC1(_CIlog10
, MSVCRT_log10
)
666 CREATE_FPU_FUNC2(_CIpow
, MSVCRT_pow
)
667 CREATE_FPU_FUNC1(_CIsin
, MSVCRT_sin
)
668 CREATE_FPU_FUNC1(_CIsinh
, MSVCRT_sinh
)
669 CREATE_FPU_FUNC1(_CIsqrt
, MSVCRT_sqrt
)
670 CREATE_FPU_FUNC1(_CItan
, MSVCRT_tan
)
671 CREATE_FPU_FUNC1(_CItanh
, MSVCRT_tanh
)
673 __ASM_GLOBAL_FUNC(MSVCRT__ftol
,
675 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
676 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
677 "movl %esp, %ebp\n\t"
678 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
679 "subl $12, %esp\n\t" /* sizeof(LONGLONG) + 2*sizeof(WORD) */
681 "mov (%esp), %ax\n\t"
683 "mov %ax, 2(%esp)\n\t"
687 "movl 4(%esp), %eax\n\t"
688 "movl 8(%esp), %edx\n\t"
690 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
691 __ASM_CFI(".cfi_same_value %ebp\n\t")
694 #endif /* defined(__GNUC__) && defined(__i386__) */
696 /*********************************************************************
697 * _fpclass (MSVCRT.@)
699 int CDECL
MSVCRT__fpclass(double num
)
701 #if defined(HAVE_FPCLASS) || defined(fpclass)
702 switch (fpclass( num
))
704 case FP_SNAN
: return MSVCRT__FPCLASS_SNAN
;
705 case FP_QNAN
: return MSVCRT__FPCLASS_QNAN
;
706 case FP_NINF
: return MSVCRT__FPCLASS_NINF
;
707 case FP_PINF
: return MSVCRT__FPCLASS_PINF
;
708 case FP_NDENORM
: return MSVCRT__FPCLASS_ND
;
709 case FP_PDENORM
: return MSVCRT__FPCLASS_PD
;
710 case FP_NZERO
: return MSVCRT__FPCLASS_NZ
;
711 case FP_PZERO
: return MSVCRT__FPCLASS_PZ
;
712 case FP_NNORM
: return MSVCRT__FPCLASS_NN
;
713 case FP_PNORM
: return MSVCRT__FPCLASS_PN
;
714 default: return MSVCRT__FPCLASS_PN
;
716 #elif defined (fpclassify)
717 switch (fpclassify( num
))
719 case FP_NAN
: return MSVCRT__FPCLASS_QNAN
;
720 case FP_INFINITE
: return signbit(num
) ? MSVCRT__FPCLASS_NINF
: MSVCRT__FPCLASS_PINF
;
721 case FP_SUBNORMAL
: return signbit(num
) ?MSVCRT__FPCLASS_ND
: MSVCRT__FPCLASS_PD
;
722 case FP_ZERO
: return signbit(num
) ? MSVCRT__FPCLASS_NZ
: MSVCRT__FPCLASS_PZ
;
724 return signbit(num
) ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
;
727 return MSVCRT__FPCLASS_QNAN
;
728 return num
== 0.0 ? MSVCRT__FPCLASS_PZ
: (num
< 0 ? MSVCRT__FPCLASS_NN
: MSVCRT__FPCLASS_PN
);
732 /*********************************************************************
735 unsigned int CDECL
_rotl(unsigned int num
, int shift
)
738 return (num
<< shift
) | (num
>> (32-shift
));
741 /*********************************************************************
744 MSVCRT_ulong CDECL
MSVCRT__lrotl(MSVCRT_ulong num
, int shift
)
747 return (num
<< shift
) | (num
>> (32-shift
));
750 /*********************************************************************
753 MSVCRT_ulong CDECL
MSVCRT__lrotr(MSVCRT_ulong num
, int shift
)
756 return (num
>> shift
) | (num
<< (32-shift
));
759 /*********************************************************************
762 unsigned int CDECL
_rotr(unsigned int num
, int shift
)
765 return (num
>> shift
) | (num
<< (32-shift
));
768 /*********************************************************************
771 unsigned __int64 CDECL
_rotl64(unsigned __int64 num
, int shift
)
774 return (num
<< shift
) | (num
>> (64-shift
));
777 /*********************************************************************
780 unsigned __int64 CDECL
_rotr64(unsigned __int64 num
, int shift
)
783 return (num
>> shift
) | (num
<< (64-shift
));
786 /*********************************************************************
789 int CDECL
MSVCRT_abs( int n
)
791 return n
>= 0 ? n
: -n
;
794 /*********************************************************************
797 MSVCRT_long CDECL
MSVCRT_labs( MSVCRT_long n
)
799 return n
>= 0 ? n
: -n
;
803 /*********************************************************************
806 MSVCRT_longlong CDECL
MSVCRT_llabs( MSVCRT_longlong n
)
808 return n
>= 0 ? n
: -n
;
812 /*********************************************************************
815 __int64 CDECL
_abs64( __int64 n
)
817 return n
>= 0 ? n
: -n
;
820 /*********************************************************************
823 double CDECL
MSVCRT__logb(double num
)
825 double ret
= logb(num
);
826 if (isnan(num
)) math_error(_DOMAIN
, "_logb", num
, 0, ret
);
827 else if (!num
) math_error(_SING
, "_logb", num
, 0, ret
);
831 /*********************************************************************
834 double CDECL
_hypot(double x
, double y
)
836 /* FIXME: errno handling */
837 return hypot( x
, y
);
840 /*********************************************************************
843 float CDECL
MSVCRT__hypotf(float x
, float y
)
845 /* FIXME: errno handling */
846 return hypotf( x
, y
);
849 /*********************************************************************
852 double CDECL
MSVCRT_ceil( double x
)
857 /*********************************************************************
860 double CDECL
MSVCRT_floor( double x
)
865 /*********************************************************************
868 double CDECL
MSVCRT_fabs( double x
)
873 /*********************************************************************
876 double CDECL
MSVCRT_frexp( double x
, int *exp
)
878 return frexp( x
, exp
);
881 /*********************************************************************
884 double CDECL
MSVCRT_modf( double x
, double *iptr
)
886 return modf( x
, iptr
);
889 /**********************************************************************
890 * _statusfp2 (MSVCRT.@)
892 * Not exported by native msvcrt, added in msvcr80.
894 #if defined(__i386__) || defined(__x86_64__)
895 void CDECL
_statusfp2( unsigned int *x86_sw
, unsigned int *sse2_sw
)
899 unsigned long fpword
;
903 __asm__
__volatile__( "fstsw %0" : "=m" (fpword
) );
905 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
906 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
907 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
908 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
909 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
910 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
914 if (!sse2_sw
) return;
918 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
920 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
921 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
922 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
923 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
924 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
925 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
930 FIXME( "not implemented\n" );
935 /**********************************************************************
936 * _statusfp (MSVCRT.@)
938 unsigned int CDECL
_statusfp(void)
940 #if defined(__i386__) || defined(__x86_64__)
941 unsigned int x86_sw
, sse2_sw
;
943 _statusfp2( &x86_sw
, &sse2_sw
);
944 /* FIXME: there's no definition for ambiguous status, just return all status bits for now */
945 return x86_sw
| sse2_sw
;
947 FIXME( "not implemented\n" );
952 /*********************************************************************
953 * _clearfp (MSVCRT.@)
955 unsigned int CDECL
_clearfp(void)
957 unsigned int flags
= 0;
958 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
959 unsigned long fpword
;
961 __asm__
__volatile__( "fnstsw %0; fnclex" : "=m" (fpword
) );
962 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
963 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
964 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
965 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
966 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
967 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
971 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
972 if (fpword
& 0x1) flags
|= MSVCRT__SW_INVALID
;
973 if (fpword
& 0x2) flags
|= MSVCRT__SW_DENORMAL
;
974 if (fpword
& 0x4) flags
|= MSVCRT__SW_ZERODIVIDE
;
975 if (fpword
& 0x8) flags
|= MSVCRT__SW_OVERFLOW
;
976 if (fpword
& 0x10) flags
|= MSVCRT__SW_UNDERFLOW
;
977 if (fpword
& 0x20) flags
|= MSVCRT__SW_INEXACT
;
979 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
982 FIXME( "not implemented\n" );
987 /*********************************************************************
988 * __fpecode (MSVCRT.@)
990 int * CDECL
__fpecode(void)
992 return &msvcrt_get_thread_data()->fpecode
;
995 /*********************************************************************
998 double CDECL
MSVCRT_ldexp(double num
, MSVCRT_long exp
)
1000 double z
= ldexp(num
,exp
);
1002 if (isfinite(num
) && !isfinite(z
))
1003 math_error(_OVERFLOW
, "ldexp", num
, exp
, z
);
1004 else if (num
&& isfinite(num
) && !z
)
1005 math_error(_UNDERFLOW
, "ldexp", num
, exp
, z
);
1006 else if (z
== 0 && signbit(z
))
1007 z
= 0.0; /* Convert -0 -> +0 */
1011 /*********************************************************************
1014 double CDECL
MSVCRT__cabs(struct MSVCRT__complex num
)
1016 return sqrt(num
.x
* num
.x
+ num
.y
* num
.y
);
1019 /*********************************************************************
1020 * _chgsign (MSVCRT.@)
1022 double CDECL
MSVCRT__chgsign(double num
)
1024 /* FIXME: +-infinity,Nan not tested */
1028 /*********************************************************************
1029 * __control87_2 (MSVCR80.@)
1031 * Not exported by native msvcrt, added in msvcr80.
1033 #if defined(__i386__) || defined(__x86_64__)
1034 int CDECL
__control87_2( unsigned int newval
, unsigned int mask
,
1035 unsigned int *x86_cw
, unsigned int *sse2_cw
)
1038 unsigned long fpword
;
1043 __asm__
__volatile__( "fstcw %0" : "=m" (fpword
) );
1045 /* Convert into mask constants */
1047 if (fpword
& 0x1) flags
|= MSVCRT__EM_INVALID
;
1048 if (fpword
& 0x2) flags
|= MSVCRT__EM_DENORMAL
;
1049 if (fpword
& 0x4) flags
|= MSVCRT__EM_ZERODIVIDE
;
1050 if (fpword
& 0x8) flags
|= MSVCRT__EM_OVERFLOW
;
1051 if (fpword
& 0x10) flags
|= MSVCRT__EM_UNDERFLOW
;
1052 if (fpword
& 0x20) flags
|= MSVCRT__EM_INEXACT
;
1053 switch (fpword
& 0xc00)
1055 case 0xc00: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
1056 case 0x800: flags
|= MSVCRT__RC_UP
; break;
1057 case 0x400: flags
|= MSVCRT__RC_DOWN
; break;
1059 switch (fpword
& 0x300)
1061 case 0x0: flags
|= MSVCRT__PC_24
; break;
1062 case 0x200: flags
|= MSVCRT__PC_53
; break;
1063 case 0x300: flags
|= MSVCRT__PC_64
; break;
1065 if (fpword
& 0x1000) flags
|= MSVCRT__IC_AFFINE
;
1067 TRACE( "x86 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1070 flags
= (flags
& ~mask
) | (newval
& mask
);
1072 /* Convert (masked) value back to fp word */
1074 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x1;
1075 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x2;
1076 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x4;
1077 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x8;
1078 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x10;
1079 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x20;
1080 switch (flags
& MSVCRT__MCW_RC
)
1082 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0xc00; break;
1083 case MSVCRT__RC_UP
: fpword
|= 0x800; break;
1084 case MSVCRT__RC_DOWN
: fpword
|= 0x400; break;
1086 switch (flags
& MSVCRT__MCW_PC
)
1088 case MSVCRT__PC_64
: fpword
|= 0x300; break;
1089 case MSVCRT__PC_53
: fpword
|= 0x200; break;
1090 case MSVCRT__PC_24
: fpword
|= 0x0; break;
1092 if (flags
& MSVCRT__IC_AFFINE
) fpword
|= 0x1000;
1094 __asm__
__volatile__( "fldcw %0" : : "m" (fpword
) );
1099 if (!sse2_cw
) return 1;
1103 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1105 /* Convert into mask constants */
1107 if (fpword
& 0x80) flags
|= MSVCRT__EM_INVALID
;
1108 if (fpword
& 0x100) flags
|= MSVCRT__EM_DENORMAL
;
1109 if (fpword
& 0x200) flags
|= MSVCRT__EM_ZERODIVIDE
;
1110 if (fpword
& 0x400) flags
|= MSVCRT__EM_OVERFLOW
;
1111 if (fpword
& 0x800) flags
|= MSVCRT__EM_UNDERFLOW
;
1112 if (fpword
& 0x1000) flags
|= MSVCRT__EM_INEXACT
;
1113 switch (fpword
& 0x6000)
1115 case 0x6000: flags
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
1116 case 0x4000: flags
|= MSVCRT__RC_UP
; break;
1117 case 0x2000: flags
|= MSVCRT__RC_DOWN
; break;
1119 switch (fpword
& 0x8040)
1121 case 0x0040: flags
|= MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS
; break;
1122 case 0x8000: flags
|= MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS
; break;
1123 case 0x8040: flags
|= MSVCRT__DN_FLUSH
; break;
1126 TRACE( "sse2 flags=%08x newval=%08x mask=%08x\n", flags
, newval
, mask
);
1129 flags
= (flags
& ~mask
) | (newval
& mask
);
1131 /* Convert (masked) value back to fp word */
1133 if (flags
& MSVCRT__EM_INVALID
) fpword
|= 0x80;
1134 if (flags
& MSVCRT__EM_DENORMAL
) fpword
|= 0x100;
1135 if (flags
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x200;
1136 if (flags
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x400;
1137 if (flags
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x800;
1138 if (flags
& MSVCRT__EM_INEXACT
) fpword
|= 0x1000;
1139 switch (flags
& MSVCRT__MCW_RC
)
1141 case MSVCRT__RC_UP
|MSVCRT__RC_DOWN
: fpword
|= 0x6000; break;
1142 case MSVCRT__RC_UP
: fpword
|= 0x4000; break;
1143 case MSVCRT__RC_DOWN
: fpword
|= 0x2000; break;
1145 switch (flags
& MSVCRT__MCW_DN
)
1147 case MSVCRT__DN_FLUSH_OPERANDS_SAVE_RESULTS
: fpword
|= 0x0040; break;
1148 case MSVCRT__DN_SAVE_OPERANDS_FLUSH_RESULTS
: fpword
|= 0x8000; break;
1149 case MSVCRT__DN_FLUSH
: fpword
|= 0x8040; break;
1151 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1159 FIXME( "not implemented\n" );
1165 /*********************************************************************
1166 * _control87 (MSVCRT.@)
1168 unsigned int CDECL
_control87(unsigned int newval
, unsigned int mask
)
1170 #if defined(__i386__) || defined(__x86_64__)
1171 unsigned int x86_cw
, sse2_cw
;
1173 __control87_2( newval
, mask
, &x86_cw
, &sse2_cw
);
1175 if ((x86_cw
^ sse2_cw
) & (MSVCRT__MCW_EM
| MSVCRT__MCW_RC
)) x86_cw
|= MSVCRT__EM_AMBIGUOUS
;
1178 FIXME( "not implemented\n" );
1183 /*********************************************************************
1184 * _controlfp (MSVCRT.@)
1186 unsigned int CDECL
_controlfp(unsigned int newval
, unsigned int mask
)
1188 return _control87( newval
, mask
& ~MSVCRT__EM_DENORMAL
);
1191 /*********************************************************************
1192 * _set_controlfp (MSVCRT.@)
1194 void CDECL
_set_controlfp( unsigned int newval
, unsigned int mask
)
1196 _controlfp( newval
, mask
);
1199 /*********************************************************************
1200 * _controlfp_s (MSVCRT.@)
1202 int CDECL
_controlfp_s(unsigned int *cur
, unsigned int newval
, unsigned int mask
)
1204 static const unsigned int all_flags
= (MSVCRT__MCW_EM
| MSVCRT__MCW_IC
| MSVCRT__MCW_RC
|
1205 MSVCRT__MCW_PC
| MSVCRT__MCW_DN
);
1208 if (!MSVCRT_CHECK_PMT( !(newval
& mask
& ~all_flags
) ))
1210 if (cur
) *cur
= _controlfp( 0, 0 ); /* retrieve it anyway */
1211 return MSVCRT_EINVAL
;
1213 val
= _controlfp( newval
, mask
);
1214 if (cur
) *cur
= val
;
1219 /*********************************************************************
1220 * fegetenv (MSVCR120.@)
1222 int CDECL
MSVCRT_fegetenv(MSVCRT_fenv_t
*env
)
1224 env
->control
= _controlfp(0, 0) & (MSVCRT__EM_INEXACT
| MSVCRT__EM_UNDERFLOW
|
1225 MSVCRT__EM_OVERFLOW
| MSVCRT__EM_ZERODIVIDE
| MSVCRT__EM_INVALID
);
1226 env
->status
= _statusfp();
1232 /*********************************************************************
1233 * __fpe_flt_rounds (UCRTBASE.@)
1235 int CDECL
__fpe_flt_rounds(void)
1237 unsigned int fpc
= _controlfp(0, 0) & MSVCRT__RC_CHOP
;
1242 case MSVCRT__RC_CHOP
: return 0;
1243 case MSVCRT__RC_NEAR
: return 1;
1245 case MSVCRT__RC_UP
: return 3;
1248 case MSVCRT__RC_UP
: return 2;
1257 /*********************************************************************
1258 * fegetround (MSVCR120.@)
1260 int CDECL
MSVCRT_fegetround(void)
1262 return _controlfp(0, 0) & MSVCRT__RC_CHOP
;
1265 /*********************************************************************
1266 * fesetround (MSVCR120.@)
1268 int CDECL
MSVCRT_fesetround(int round_mode
)
1270 if (round_mode
& (~MSVCRT__RC_CHOP
))
1272 _controlfp(round_mode
, MSVCRT__RC_CHOP
);
1276 #endif /* _MSVCR_VER>=120 */
1278 /*********************************************************************
1279 * _copysign (MSVCRT.@)
1281 double CDECL
MSVCRT__copysign(double num
, double sign
)
1284 return signbit(num
) ? num
: -num
;
1285 return signbit(num
) ? -num
: num
;
1288 /*********************************************************************
1289 * _finite (MSVCRT.@)
1291 int CDECL
MSVCRT__finite(double num
)
1293 return isfinite(num
) != 0; /* See comment for _isnan() */
1296 /*********************************************************************
1297 * _fpreset (MSVCRT.@)
1299 void CDECL
_fpreset(void)
1301 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1302 const unsigned int x86_cw
= 0x27f;
1303 __asm__
__volatile__( "fninit; fldcw %0" : : "m" (x86_cw
) );
1306 const unsigned long sse2_cw
= 0x1f80;
1307 __asm__
__volatile__( "ldmxcsr %0" : : "m" (sse2_cw
) );
1310 FIXME( "not implemented\n" );
1315 /*********************************************************************
1316 * fesetenv (MSVCR120.@)
1318 int CDECL
MSVCRT_fesetenv(const MSVCRT_fenv_t
*env
)
1320 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1328 DWORD instruction_pointer
;
1336 TRACE( "(%p)\n", env
);
1338 if (!env
->control
&& !env
->status
) {
1343 __asm__
__volatile__( "fnstenv %0" : "=m" (fenv
) );
1345 fenv
.control_word
&= ~0x3d;
1346 if (env
->control
& MSVCRT__EM_INVALID
) fenv
.control_word
|= 0x1;
1347 if (env
->control
& MSVCRT__EM_ZERODIVIDE
) fenv
.control_word
|= 0x4;
1348 if (env
->control
& MSVCRT__EM_OVERFLOW
) fenv
.control_word
|= 0x8;
1349 if (env
->control
& MSVCRT__EM_UNDERFLOW
) fenv
.control_word
|= 0x10;
1350 if (env
->control
& MSVCRT__EM_INEXACT
) fenv
.control_word
|= 0x20;
1352 fenv
.status_word
&= ~0x3d;
1353 if (env
->status
& MSVCRT__SW_INVALID
) fenv
.status_word
|= 0x1;
1354 if (env
->status
& MSVCRT__SW_ZERODIVIDE
) fenv
.status_word
|= 0x4;
1355 if (env
->status
& MSVCRT__SW_OVERFLOW
) fenv
.status_word
|= 0x8;
1356 if (env
->status
& MSVCRT__SW_UNDERFLOW
) fenv
.status_word
|= 0x10;
1357 if (env
->status
& MSVCRT__SW_INEXACT
) fenv
.status_word
|= 0x20;
1359 __asm__
__volatile__( "fldenv %0" : : "m" (fenv
) : "st", "st(1)",
1360 "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)" );
1366 __asm__
__volatile__( "stmxcsr %0" : "=m" (fpword
) );
1368 if (env
->control
& MSVCRT__EM_INVALID
) fpword
|= 0x80;
1369 if (env
->control
& MSVCRT__EM_ZERODIVIDE
) fpword
|= 0x200;
1370 if (env
->control
& MSVCRT__EM_OVERFLOW
) fpword
|= 0x400;
1371 if (env
->control
& MSVCRT__EM_UNDERFLOW
) fpword
|= 0x800;
1372 if (env
->control
& MSVCRT__EM_INEXACT
) fpword
|= 0x1000;
1373 __asm__
__volatile__( "ldmxcsr %0" : : "m" (fpword
) );
1378 FIXME( "not implemented\n" );
1384 /*********************************************************************
1387 INT CDECL
MSVCRT__isnan(double num
)
1389 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
1390 * Do the same, as the result may be used in calculations
1392 return isnan(num
) != 0;
1395 /*********************************************************************
1398 double CDECL
MSVCRT__j0(double num
)
1400 /* FIXME: errno handling */
1404 FIXME("not implemented\n");
1409 /*********************************************************************
1412 double CDECL
MSVCRT__j1(double num
)
1414 /* FIXME: errno handling */
1418 FIXME("not implemented\n");
1423 /*********************************************************************
1426 double CDECL
MSVCRT__jn(int n
, double num
)
1428 /* FIXME: errno handling */
1432 FIXME("not implemented\n");
1437 /*********************************************************************
1440 double CDECL
MSVCRT__y0(double num
)
1443 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1446 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1448 *MSVCRT__errno() = MSVCRT_EDOM
;
1452 FIXME("not implemented\n");
1458 /*********************************************************************
1461 double CDECL
MSVCRT__y1(double num
)
1464 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1467 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1469 *MSVCRT__errno() = MSVCRT_EDOM
;
1473 FIXME("not implemented\n");
1479 /*********************************************************************
1482 double CDECL
MSVCRT__yn(int order
, double num
)
1485 if (!isfinite(num
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1487 retval
= yn(order
,num
);
1488 if (MSVCRT__fpclass(retval
) == MSVCRT__FPCLASS_NINF
)
1490 *MSVCRT__errno() = MSVCRT_EDOM
;
1494 FIXME("not implemented\n");
1502 /*********************************************************************
1503 * _nearbyint (MSVCR120.@)
1505 double CDECL
MSVCRT_nearbyint(double num
)
1507 #ifdef HAVE_NEARBYINT
1508 return nearbyint(num
);
1510 return num
>= 0 ? floor(num
+ 0.5) : ceil(num
- 0.5);
1514 /*********************************************************************
1515 * _nearbyintf (MSVCR120.@)
1517 float CDECL
MSVCRT_nearbyintf(float num
)
1519 #ifdef HAVE_NEARBYINTF
1520 return nearbyintf(num
);
1522 return MSVCRT_nearbyint(num
);
1526 /*********************************************************************
1527 * nexttoward (MSVCR120.@)
1529 double CDECL
MSVCRT_nexttoward(double num
, double next
)
1531 #ifdef HAVE_NEXTTOWARD
1532 double ret
= nexttoward(num
, next
);
1533 if (!(MSVCRT__fpclass(ret
) & (MSVCRT__FPCLASS_PN
| MSVCRT__FPCLASS_NN
1534 | MSVCRT__FPCLASS_SNAN
| MSVCRT__FPCLASS_QNAN
)) && !isinf(num
))
1536 *MSVCRT__errno() = MSVCRT_ERANGE
;
1540 FIXME("not implemented\n");
1545 /*********************************************************************
1546 * nexttowardf (MSVCR120.@)
1548 float CDECL
MSVCRT_nexttowardf(float num
, double next
)
1550 #ifdef HAVE_NEXTTOWARDF
1551 float ret
= nexttowardf(num
, next
);
1552 if (!(MSVCRT__fpclass(ret
) & (MSVCRT__FPCLASS_PN
| MSVCRT__FPCLASS_NN
1553 | MSVCRT__FPCLASS_SNAN
| MSVCRT__FPCLASS_QNAN
)) && !isinf(num
))
1555 *MSVCRT__errno() = MSVCRT_ERANGE
;
1559 FIXME("not implemented\n");
1564 #endif /* _MSVCR_VER>=120 */
1566 /*********************************************************************
1567 * _nextafter (MSVCRT.@)
1569 double CDECL
MSVCRT__nextafter(double num
, double next
)
1572 if (!isfinite(num
) || !isfinite(next
)) *MSVCRT__errno() = MSVCRT_EDOM
;
1573 retval
= nextafter(num
,next
);
1577 /*********************************************************************
1580 char * CDECL
MSVCRT__ecvt( double number
, int ndigits
, int *decpt
, int *sign
)
1583 thread_data_t
*data
= msvcrt_get_thread_data();
1584 /* FIXME: check better for overflow (native supports over 300 chars) */
1585 ndigits
= min( ndigits
, 80 - 7); /* 7 : space for dec point, 1 for "e",
1586 * 4 for exponent and one for
1587 * terminating '\0' */
1588 if (!data
->efcvt_buffer
)
1589 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1596 /* handle cases with zero ndigits or less */
1598 if( prec
< 1) prec
= 2;
1599 len
= snprintf(data
->efcvt_buffer
, 80, "%.*le", prec
- 1, number
);
1600 /* take the decimal "point away */
1602 memmove( data
->efcvt_buffer
+ 1, data
->efcvt_buffer
+ 2, len
- 1 );
1603 /* take the exponential "e" out */
1604 data
->efcvt_buffer
[ prec
] = '\0';
1605 /* read the exponent */
1606 sscanf( data
->efcvt_buffer
+ prec
+ 1, "%d", decpt
);
1608 /* adjust for some border cases */
1609 if( data
->efcvt_buffer
[0] == '0')/* value is zero */
1611 /* handle cases with zero ndigits or less */
1613 if( data
->efcvt_buffer
[ 0] >= '5')
1615 data
->efcvt_buffer
[ 0] = '\0';
1617 TRACE("out=\"%s\"\n",data
->efcvt_buffer
);
1618 return data
->efcvt_buffer
;
1621 /*********************************************************************
1622 * _ecvt_s (MSVCRT.@)
1624 int CDECL
MSVCRT__ecvt_s( char *buffer
, MSVCRT_size_t length
, double number
, int ndigits
, int *decpt
, int *sign
)
1628 const char infret
[] = "1#INF";
1630 if (!MSVCRT_CHECK_PMT(buffer
!= NULL
)) return MSVCRT_EINVAL
;
1631 if (!MSVCRT_CHECK_PMT(decpt
!= NULL
)) return MSVCRT_EINVAL
;
1632 if (!MSVCRT_CHECK_PMT(sign
!= NULL
)) return MSVCRT_EINVAL
;
1633 if (!MSVCRT_CHECK_PMT_ERR( length
> 2, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
1634 if (!MSVCRT_CHECK_PMT_ERR(ndigits
< (int)length
- 1, MSVCRT_ERANGE
)) return MSVCRT_ERANGE
;
1636 /* special case - inf */
1637 if(number
== HUGE_VAL
|| number
== -HUGE_VAL
)
1639 memset(buffer
, '0', ndigits
);
1640 memcpy(buffer
, infret
, min(ndigits
, sizeof(infret
) - 1 ) );
1641 buffer
[ndigits
] = '\0';
1643 if(number
== -HUGE_VAL
)
1649 /* handle cases with zero ndigits or less */
1651 if( prec
< 1) prec
= 2;
1652 result
= MSVCRT_malloc(prec
+ 7);
1659 len
= snprintf(result
, prec
+ 7, "%.*le", prec
- 1, number
);
1660 /* take the decimal "point away */
1662 memmove( result
+ 1, result
+ 2, len
- 1 );
1663 /* take the exponential "e" out */
1664 result
[ prec
] = '\0';
1665 /* read the exponent */
1666 sscanf( result
+ prec
+ 1, "%d", decpt
);
1668 /* adjust for some border cases */
1669 if( result
[0] == '0')/* value is zero */
1671 /* handle cases with zero ndigits or less */
1673 if( result
[ 0] >= '5')
1677 memcpy( buffer
, result
, max(ndigits
+ 1, 1) );
1678 MSVCRT_free( result
);
1682 /***********************************************************************
1685 char * CDECL
MSVCRT__fcvt( double number
, int ndigits
, int *decpt
, int *sign
)
1687 thread_data_t
*data
= msvcrt_get_thread_data();
1688 int stop
, dec1
, dec2
;
1689 char *ptr1
, *ptr2
, *first
;
1690 char buf
[80]; /* ought to be enough */
1692 if (!data
->efcvt_buffer
)
1693 data
->efcvt_buffer
= MSVCRT_malloc( 80 ); /* ought to be enough */
1701 stop
= snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1703 ptr2
= data
->efcvt_buffer
;
1708 /* For numbers below the requested resolution, work out where
1709 the decimal point will be rather than finding it in the string */
1710 if (number
< 1.0 && number
> 0.0) {
1711 dec2
= log10(number
+ 1e-10);
1712 if (-dec2
<= ndigits
) dec2
= 0;
1715 /* If requested digits is zero or less, we will need to truncate
1716 * the returned string */
1721 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1722 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1723 if (!first
) first
= ptr2
;
1724 if ((ptr1
- buf
) < stop
) {
1735 while (*ptr1
== '0') { /* Process leading zeroes */
1740 while (*ptr1
!= '\0') {
1741 if (!first
) first
= ptr2
;
1748 /* We never found a non-zero digit, then our number is either
1749 * smaller than the requested precision, or 0.0 */
1754 first
= data
->efcvt_buffer
;
1759 *decpt
= dec2
? dec2
: dec1
;
1763 /***********************************************************************
1764 * _fcvt_s (MSVCRT.@)
1766 int CDECL
MSVCRT__fcvt_s(char* outbuffer
, MSVCRT_size_t size
, double number
, int ndigits
, int *decpt
, int *sign
)
1768 int stop
, dec1
, dec2
;
1769 char *ptr1
, *ptr2
, *first
;
1770 char buf
[80]; /* ought to be enough */
1772 if (!outbuffer
|| !decpt
|| !sign
|| size
== 0)
1774 *MSVCRT__errno() = MSVCRT_EINVAL
;
1775 return MSVCRT_EINVAL
;
1784 stop
= snprintf(buf
, 80, "%.*f", ndigits
< 0 ? 0 : ndigits
, number
);
1791 /* For numbers below the requested resolution, work out where
1792 the decimal point will be rather than finding it in the string */
1793 if (number
< 1.0 && number
> 0.0) {
1794 dec2
= log10(number
+ 1e-10);
1795 if (-dec2
<= ndigits
) dec2
= 0;
1798 /* If requested digits is zero or less, we will need to truncate
1799 * the returned string */
1804 while (*ptr1
== '0') ptr1
++; /* Skip leading zeroes */
1805 while (*ptr1
!= '\0' && *ptr1
!= '.') {
1806 if (!first
) first
= ptr2
;
1807 if ((ptr1
- buf
) < stop
) {
1821 while (*ptr1
== '0') { /* Process leading zeroes */
1822 if (number
== 0.0 && size
> 1) {
1830 while (*ptr1
!= '\0') {
1831 if (!first
) first
= ptr2
;
1841 /* We never found a non-zero digit, then our number is either
1842 * smaller than the requested precision, or 0.0 */
1843 if (!first
&& (number
<= 0.0))
1846 *decpt
= dec2
? dec2
: dec1
;
1850 /***********************************************************************
1853 char * CDECL
MSVCRT__gcvt( double number
, int ndigit
, char *buff
)
1856 *MSVCRT__errno() = MSVCRT_EINVAL
;
1861 *MSVCRT__errno() = MSVCRT_ERANGE
;
1865 MSVCRT_sprintf(buff
, "%.*g", ndigit
, number
);
1869 /***********************************************************************
1870 * _gcvt_s (MSVCRT.@)
1872 int CDECL
MSVCRT__gcvt_s(char *buff
, MSVCRT_size_t size
, double number
, int digits
)
1877 *MSVCRT__errno() = MSVCRT_EINVAL
;
1878 return MSVCRT_EINVAL
;
1881 if( digits
<0 || digits
>=size
) {
1885 *MSVCRT__errno() = MSVCRT_ERANGE
;
1886 return MSVCRT_ERANGE
;
1889 len
= MSVCRT__scprintf("%.*g", digits
, number
);
1892 *MSVCRT__errno() = MSVCRT_ERANGE
;
1893 return MSVCRT_ERANGE
;
1896 MSVCRT_sprintf(buff
, "%.*g", digits
, number
);
1900 #include <stdlib.h> /* div_t, ldiv_t */
1902 /*********************************************************************
1905 * [i386] Windows binary compatible - returns the struct in eax/edx.
1908 unsigned __int64 CDECL
MSVCRT_div(int num
, int denom
)
1910 div_t dt
= div(num
,denom
);
1911 return ((unsigned __int64
)dt
.rem
<< 32) | (unsigned int)dt
.quot
;
1914 /*********************************************************************
1917 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1919 MSVCRT_div_t CDECL
MSVCRT_div(int num
, int denom
)
1921 div_t dt
= div(num
,denom
);
1929 #endif /* ifdef __i386__ */
1932 /*********************************************************************
1935 * [i386] Windows binary compatible - returns the struct in eax/edx.
1938 unsigned __int64 CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1940 ldiv_t ldt
= ldiv(num
,denom
);
1941 return ((unsigned __int64
)ldt
.rem
<< 32) | (MSVCRT_ulong
)ldt
.quot
;
1944 /*********************************************************************
1947 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1949 MSVCRT_ldiv_t CDECL
MSVCRT_ldiv(MSVCRT_long num
, MSVCRT_long denom
)
1951 ldiv_t result
= ldiv(num
,denom
);
1954 ret
.quot
= result
.quot
;
1955 ret
.rem
= result
.rem
;
1959 #endif /* ifdef __i386__ */
1962 /*********************************************************************
1963 * lldiv (MSVCR100.@)
1965 MSVCRT_lldiv_t
* CDECL
MSVCRT_lldiv(MSVCRT_lldiv_t
*ret
,
1966 MSVCRT_longlong num
, MSVCRT_longlong denom
)
1968 ret
->quot
= num
/ denom
;
1969 ret
->rem
= num
% denom
;
1977 /*********************************************************************
1978 * _adjust_fdiv (MSVCRT.@)
1979 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1981 int MSVCRT__adjust_fdiv
= 0;
1983 /***********************************************************************
1984 * _adj_fdiv_m16i (MSVCRT.@)
1987 * I _think_ this function is intended to work around the Pentium
1990 void __stdcall
_adj_fdiv_m16i( short arg
)
1992 TRACE("(): stub\n");
1995 /***********************************************************************
1996 * _adj_fdiv_m32 (MSVCRT.@)
1999 * I _think_ this function is intended to work around the Pentium
2002 void __stdcall
_adj_fdiv_m32( unsigned int arg
)
2004 TRACE("(): stub\n");
2007 /***********************************************************************
2008 * _adj_fdiv_m32i (MSVCRT.@)
2011 * I _think_ this function is intended to work around the Pentium
2014 void __stdcall
_adj_fdiv_m32i( int arg
)
2016 TRACE("(): stub\n");
2019 /***********************************************************************
2020 * _adj_fdiv_m64 (MSVCRT.@)
2023 * I _think_ this function is intended to work around the Pentium
2026 void __stdcall
_adj_fdiv_m64( unsigned __int64 arg
)
2028 TRACE("(): stub\n");
2031 /***********************************************************************
2032 * _adj_fdiv_r (MSVCRT.@)
2034 * This function is likely to have the wrong number of arguments.
2037 * I _think_ this function is intended to work around the Pentium
2040 void _adj_fdiv_r(void)
2042 TRACE("(): stub\n");
2045 /***********************************************************************
2046 * _adj_fdivr_m16i (MSVCRT.@)
2049 * I _think_ this function is intended to work around the Pentium
2052 void __stdcall
_adj_fdivr_m16i( short arg
)
2054 TRACE("(): stub\n");
2057 /***********************************************************************
2058 * _adj_fdivr_m32 (MSVCRT.@)
2061 * I _think_ this function is intended to work around the Pentium
2064 void __stdcall
_adj_fdivr_m32( unsigned int arg
)
2066 TRACE("(): stub\n");
2069 /***********************************************************************
2070 * _adj_fdivr_m32i (MSVCRT.@)
2073 * I _think_ this function is intended to work around the Pentium
2076 void __stdcall
_adj_fdivr_m32i( int arg
)
2078 TRACE("(): stub\n");
2081 /***********************************************************************
2082 * _adj_fdivr_m64 (MSVCRT.@)
2085 * I _think_ this function is intended to work around the Pentium
2088 void __stdcall
_adj_fdivr_m64( unsigned __int64 arg
)
2090 TRACE("(): stub\n");
2093 /***********************************************************************
2094 * _adj_fpatan (MSVCRT.@)
2096 * This function is likely to have the wrong number of arguments.
2099 * I _think_ this function is intended to work around the Pentium
2102 void _adj_fpatan(void)
2104 TRACE("(): stub\n");
2107 /***********************************************************************
2108 * _adj_fprem (MSVCRT.@)
2110 * This function is likely to have the wrong number of arguments.
2113 * I _think_ this function is intended to work around the Pentium
2116 void _adj_fprem(void)
2118 TRACE("(): stub\n");
2121 /***********************************************************************
2122 * _adj_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 _adj_fprem1(void)
2132 TRACE("(): stub\n");
2135 /***********************************************************************
2136 * _adj_fptan (MSVCRT.@)
2138 * This function is likely to have the wrong number of arguments.
2141 * I _think_ this function is intended to work around the Pentium
2144 void _adj_fptan(void)
2146 TRACE("(): stub\n");
2149 /***********************************************************************
2150 * _safe_fdiv (MSVCRT.@)
2152 * This function is likely to have the wrong number of arguments.
2155 * I _think_ this function is intended to work around the Pentium
2158 void _safe_fdiv(void)
2160 TRACE("(): stub\n");
2163 /***********************************************************************
2164 * _safe_fdivr (MSVCRT.@)
2166 * This function is likely to have the wrong number of arguments.
2169 * I _think_ this function is intended to work around the Pentium
2172 void _safe_fdivr(void)
2174 TRACE("(): stub\n");
2177 /***********************************************************************
2178 * _safe_fprem (MSVCRT.@)
2180 * This function is likely to have the wrong number of arguments.
2183 * I _think_ this function is intended to work around the Pentium
2186 void _safe_fprem(void)
2188 TRACE("(): stub\n");
2191 /***********************************************************************
2192 * _safe_fprem1 (MSVCRT.@)
2195 * This function is likely to have the wrong number of arguments.
2198 * I _think_ this function is intended to work around the Pentium
2201 void _safe_fprem1(void)
2203 TRACE("(): stub\n");
2206 /***********************************************************************
2207 * __libm_sse2_acos (MSVCRT.@)
2209 void __cdecl
MSVCRT___libm_sse2_acos(void)
2212 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2214 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2217 /***********************************************************************
2218 * __libm_sse2_acosf (MSVCRT.@)
2220 void __cdecl
MSVCRT___libm_sse2_acosf(void)
2223 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2225 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2228 /***********************************************************************
2229 * __libm_sse2_asin (MSVCRT.@)
2231 void __cdecl
MSVCRT___libm_sse2_asin(void)
2234 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2236 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2239 /***********************************************************************
2240 * __libm_sse2_asinf (MSVCRT.@)
2242 void __cdecl
MSVCRT___libm_sse2_asinf(void)
2245 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2247 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2250 /***********************************************************************
2251 * __libm_sse2_atan (MSVCRT.@)
2253 void __cdecl
MSVCRT___libm_sse2_atan(void)
2256 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2258 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2261 /***********************************************************************
2262 * __libm_sse2_atan2 (MSVCRT.@)
2264 void __cdecl
MSVCRT___libm_sse2_atan2(void)
2267 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
2268 d1
= atan2( d1
, d2
);
2269 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
2272 /***********************************************************************
2273 * __libm_sse2_atanf (MSVCRT.@)
2275 void __cdecl
MSVCRT___libm_sse2_atanf(void)
2278 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2280 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2283 /***********************************************************************
2284 * __libm_sse2_cos (MSVCRT.@)
2286 void __cdecl
MSVCRT___libm_sse2_cos(void)
2289 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2291 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2294 /***********************************************************************
2295 * __libm_sse2_cosf (MSVCRT.@)
2297 void __cdecl
MSVCRT___libm_sse2_cosf(void)
2300 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2302 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2305 /***********************************************************************
2306 * __libm_sse2_exp (MSVCRT.@)
2308 void __cdecl
MSVCRT___libm_sse2_exp(void)
2311 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2313 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2316 /***********************************************************************
2317 * __libm_sse2_expf (MSVCRT.@)
2319 void __cdecl
MSVCRT___libm_sse2_expf(void)
2322 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2324 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2327 /***********************************************************************
2328 * __libm_sse2_log (MSVCRT.@)
2330 void __cdecl
MSVCRT___libm_sse2_log(void)
2333 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2335 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2338 /***********************************************************************
2339 * __libm_sse2_log10 (MSVCRT.@)
2341 void __cdecl
MSVCRT___libm_sse2_log10(void)
2344 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2346 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2349 /***********************************************************************
2350 * __libm_sse2_log10f (MSVCRT.@)
2352 void __cdecl
MSVCRT___libm_sse2_log10f(void)
2355 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2357 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2360 /***********************************************************************
2361 * __libm_sse2_logf (MSVCRT.@)
2363 void __cdecl
MSVCRT___libm_sse2_logf(void)
2366 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2368 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2371 /***********************************************************************
2372 * __libm_sse2_pow (MSVCRT.@)
2374 void __cdecl
MSVCRT___libm_sse2_pow(void)
2377 __asm__
__volatile__( "movq %%xmm0,%0; movq %%xmm1,%1 " : "=m" (d1
), "=m" (d2
) );
2379 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d1
) );
2382 /***********************************************************************
2383 * __libm_sse2_powf (MSVCRT.@)
2385 void __cdecl
MSVCRT___libm_sse2_powf(void)
2388 __asm__
__volatile__( "movd %%xmm0,%0; movd %%xmm1,%1" : "=g" (f1
), "=g" (f2
) );
2389 f1
= powf( f1
, f2
);
2390 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f1
) );
2393 /***********************************************************************
2394 * __libm_sse2_sin (MSVCRT.@)
2396 void __cdecl
MSVCRT___libm_sse2_sin(void)
2399 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2401 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2404 /***********************************************************************
2405 * __libm_sse2_sinf (MSVCRT.@)
2407 void __cdecl
MSVCRT___libm_sse2_sinf(void)
2410 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2412 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2415 /***********************************************************************
2416 * __libm_sse2_tan (MSVCRT.@)
2418 void __cdecl
MSVCRT___libm_sse2_tan(void)
2421 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2423 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2426 /***********************************************************************
2427 * __libm_sse2_tanf (MSVCRT.@)
2429 void __cdecl
MSVCRT___libm_sse2_tanf(void)
2432 __asm__
__volatile__( "movd %%xmm0,%0" : "=g" (f
) );
2434 __asm__
__volatile__( "movd %0,%%xmm0" : : "g" (f
) );
2437 /***********************************************************************
2438 * __libm_sse2_sqrt_precise (MSVCR110.@)
2440 void __cdecl
MSVCRT___libm_sse2_sqrt_precise(void)
2443 __asm__
__volatile__( "movq %%xmm0,%0" : "=m" (d
) );
2445 __asm__
__volatile__( "movq %0,%%xmm0" : : "m" (d
) );
2448 #endif /* __i386__ */
2450 /*********************************************************************
2453 double CDECL
MSVCR120_cbrt(double x
)
2458 return x
< 0 ? -pow(-x
, 1.0 / 3.0) : pow(x
, 1.0 / 3.0);
2462 /*********************************************************************
2463 * cbrtf (MSVCR120.@)
2465 float CDECL
MSVCR120_cbrtf(float x
)
2470 return MSVCR120_cbrt(x
);
2474 /*********************************************************************
2475 * cbrtl (MSVCR120.@)
2477 LDOUBLE CDECL
MSVCR120_cbrtl(LDOUBLE x
)
2479 return MSVCR120_cbrt(x
);
2482 /*********************************************************************
2485 double CDECL
MSVCR120_exp2(double x
)
2488 double ret
= exp2(x
);
2490 double ret
= pow(2, x
);
2492 if (isfinite(x
) && !isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2496 /*********************************************************************
2497 * exp2f (MSVCR120.@)
2499 float CDECL
MSVCR120_exp2f(float x
)
2502 float ret
= exp2f(x
);
2503 if (finitef(x
) && !finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2506 return MSVCR120_exp2(x
);
2510 /*********************************************************************
2511 * exp2l (MSVCR120.@)
2513 LDOUBLE CDECL
MSVCR120_exp2l(LDOUBLE x
)
2515 return MSVCR120_exp2(x
);
2518 /*********************************************************************
2519 * expm1 (MSVCR120.@)
2521 double CDECL
MSVCR120_expm1(double x
)
2524 double ret
= expm1(x
);
2526 double ret
= exp(x
) - 1;
2528 if (isfinite(x
) && !isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2532 /*********************************************************************
2533 * expm1f (MSVCR120.@)
2535 float CDECL
MSVCR120_expm1f(float x
)
2538 float ret
= expm1f(x
);
2540 float ret
= exp(x
) - 1;
2542 if (finitef(x
) && !finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
2546 /*********************************************************************
2547 * expm1l (MSVCR120.@)
2549 LDOUBLE CDECL
MSVCR120_expm1l(LDOUBLE x
)
2551 return MSVCR120_expm1(x
);
2554 /*********************************************************************
2555 * log1p (MSVCR120.@)
2557 double CDECL
MSVCR120_log1p(double x
)
2559 if (x
< -1) *MSVCRT__errno() = MSVCRT_EDOM
;
2560 else if (x
== -1) *MSVCRT__errno() = MSVCRT_ERANGE
;
2568 /*********************************************************************
2569 * log1pf (MSVCR120.@)
2571 float CDECL
MSVCR120_log1pf(float x
)
2573 if (x
< -1) *MSVCRT__errno() = MSVCRT_EDOM
;
2574 else if (x
== -1) *MSVCRT__errno() = MSVCRT_ERANGE
;
2582 /*********************************************************************
2583 * log1pl (MSVCR120.@)
2585 LDOUBLE CDECL
MSVCR120_log1pl(LDOUBLE x
)
2587 return MSVCR120_log1p(x
);
2590 /*********************************************************************
2593 double CDECL
MSVCR120_log2(double x
)
2595 if (x
< 0) *MSVCRT__errno() = MSVCRT_EDOM
;
2596 else if (x
== 0) *MSVCRT__errno() = MSVCRT_ERANGE
;
2600 return log(x
) / log(2);
2604 /*********************************************************************
2605 * log2f (MSVCR120.@)
2607 float CDECL
MSVCR120_log2f(float x
)
2610 if (x
< 0) *MSVCRT__errno() = MSVCRT_EDOM
;
2611 else if (x
== 0) *MSVCRT__errno() = MSVCRT_ERANGE
;
2614 return MSVCR120_log2(x
);
2618 /*********************************************************************
2619 * log2l (MSVCR120.@)
2621 LDOUBLE CDECL
MSVCR120_log2l(LDOUBLE x
)
2623 return MSVCR120_log2(x
);
2626 /*********************************************************************
2629 double CDECL
MSVCR120_rint(double x
)
2634 /*********************************************************************
2635 * rintf (MSVCR120.@)
2637 float CDECL
MSVCR120_rintf(float x
)
2642 /*********************************************************************
2643 * rintl (MSVCR120.@)
2645 LDOUBLE CDECL
MSVCR120_rintl(LDOUBLE x
)
2647 return MSVCR120_rint(x
);
2650 /*********************************************************************
2651 * lrint (MSVCR120.@)
2653 MSVCRT_long CDECL
MSVCR120_lrint(double x
)
2658 /*********************************************************************
2659 * lrintf (MSVCR120.@)
2661 MSVCRT_long CDECL
MSVCR120_lrintf(float x
)
2666 /*********************************************************************
2667 * lrintl (MSVCR120.@)
2669 MSVCRT_long CDECL
MSVCR120_lrintl(LDOUBLE x
)
2671 return MSVCR120_lrint(x
);
2674 /*********************************************************************
2675 * llrint (MSVCR120.@)
2677 MSVCRT_longlong CDECL
MSVCR120_llrint(double x
)
2682 /*********************************************************************
2683 * llrintf (MSVCR120.@)
2685 MSVCRT_longlong CDECL
MSVCR120_llrintf(float x
)
2690 /*********************************************************************
2691 * rintl (MSVCR120.@)
2693 MSVCRT_longlong CDECL
MSVCR120_llrintl(LDOUBLE x
)
2695 return MSVCR120_llrint(x
);
2700 /*********************************************************************
2701 * round (MSVCR120.@)
2703 double CDECL
MSVCR120_round(double x
)
2708 return MSVCR120_rint(x
);
2712 /*********************************************************************
2713 * roundf (MSVCR120.@)
2715 float CDECL
MSVCR120_roundf(float x
)
2720 return MSVCR120_round(x
);
2724 /*********************************************************************
2725 * roundl (MSVCR120.@)
2727 LDOUBLE CDECL
MSVCR120_roundl(LDOUBLE x
)
2729 return MSVCR120_round(x
);
2732 /*********************************************************************
2733 * lround (MSVCR120.@)
2735 MSVCRT_long CDECL
MSVCR120_lround(double x
)
2740 return MSVCR120_round(x
);
2744 /*********************************************************************
2745 * lroundf (MSVCR120.@)
2747 MSVCRT_long CDECL
MSVCR120_lroundf(float x
)
2752 return MSVCR120_lround(x
);
2756 /*********************************************************************
2757 * lroundl (MSVCR120.@)
2759 MSVCRT_long CDECL
MSVCR120_lroundl(LDOUBLE x
)
2761 return MSVCR120_lround(x
);
2764 /*********************************************************************
2765 * llround (MSVCR120.@)
2767 MSVCRT_longlong CDECL
MSVCR120_llround(double x
)
2772 return MSVCR120_round(x
);
2776 /*********************************************************************
2777 * llroundf (MSVCR120.@)
2779 MSVCRT_longlong CDECL
MSVCR120_llroundf(float x
)
2781 #ifdef HAVE_LLROUNDF
2784 return MSVCR120_llround(x
);
2788 /*********************************************************************
2789 * roundl (MSVCR120.@)
2791 MSVCRT_longlong CDECL
MSVCR120_llroundl(LDOUBLE x
)
2793 return MSVCR120_llround(x
);
2796 /*********************************************************************
2797 * trunc (MSVCR120.@)
2799 double CDECL
MSVCR120_trunc(double x
)
2804 return (x
> 0) ? floor(x
) : ceil(x
);
2808 /*********************************************************************
2809 * truncf (MSVCR120.@)
2811 float CDECL
MSVCR120_truncf(float x
)
2816 return MSVCR120_trunc(x
);
2820 /*********************************************************************
2821 * truncl (MSVCR120.@)
2823 LDOUBLE CDECL
MSVCR120_truncl(LDOUBLE x
)
2825 return MSVCR120_trunc(x
);
2828 /*********************************************************************
2829 * _dclass (MSVCR120.@)
2831 short CDECL
MSVCR120__dclass(double x
)
2833 switch (MSVCRT__fpclass(x
)) {
2834 case MSVCRT__FPCLASS_QNAN
:
2835 case MSVCRT__FPCLASS_SNAN
:
2836 return MSVCRT_FP_NAN
;
2837 case MSVCRT__FPCLASS_NINF
:
2838 case MSVCRT__FPCLASS_PINF
:
2839 return MSVCRT_FP_INFINITE
;
2840 case MSVCRT__FPCLASS_ND
:
2841 case MSVCRT__FPCLASS_PD
:
2842 return MSVCRT_FP_SUBNORMAL
;
2843 case MSVCRT__FPCLASS_NN
:
2844 case MSVCRT__FPCLASS_PN
:
2846 return MSVCRT_FP_NORMAL
;
2847 case MSVCRT__FPCLASS_NZ
:
2848 case MSVCRT__FPCLASS_PZ
:
2849 return MSVCRT_FP_ZERO
;
2853 /*********************************************************************
2854 * _fdclass (MSVCR120.@)
2856 short CDECL
MSVCR120__fdclass(float x
)
2858 return MSVCR120__dclass(x
);
2861 /*********************************************************************
2862 * _ldclass (MSVCR120.@)
2864 short CDECL
MSVCR120__ldclass(LDOUBLE x
)
2866 return MSVCR120__dclass(x
);
2869 /*********************************************************************
2870 * _dtest (MSVCR120.@)
2872 short CDECL
MSVCR120__dtest(double *x
)
2874 return MSVCR120__dclass(*x
);
2877 /*********************************************************************
2878 * _fdtest (MSVCR120.@)
2880 short CDECL
MSVCR120__fdtest(float *x
)
2882 return MSVCR120__dclass(*x
);
2885 /*********************************************************************
2886 * _ldtest (MSVCR120.@)
2888 short CDECL
MSVCR120__ldtest(LDOUBLE
*x
)
2890 return MSVCR120__dclass(*x
);
2893 /*********************************************************************
2896 double CDECL
MSVCR120_erf(double x
)
2901 /* Abramowitz and Stegun approximation, maximum error: 1.5*10^-7 */
2903 int sign
= signbit(x
);
2906 t
= 1 / (1 + 0.3275911 * x
);
2907 y
= ((((1.061405429*t
- 1.453152027)*t
+ 1.421413741)*t
- 0.284496736)*t
+ 0.254829592)*t
;
2908 y
= 1.0 - y
*exp(-x
*x
);
2909 return sign
? -y
: y
;
2913 /*********************************************************************
2916 float CDECL
MSVCR120_erff(float x
)
2921 return MSVCR120_erf(x
);
2925 /*********************************************************************
2928 LDOUBLE CDECL
MSVCR120_erfl(LDOUBLE x
)
2930 return MSVCR120_erf(x
);
2933 /*********************************************************************
2936 double CDECL
MSVCR120_erfc(double x
)
2941 return 1 - MSVCR120_erf(x
);
2945 /*********************************************************************
2946 * erfcf (MSVCR120.@)
2948 float CDECL
MSVCR120_erfcf(float x
)
2953 return MSVCR120_erfc(x
);
2957 /*********************************************************************
2958 * erfcl (MSVCR120.@)
2960 LDOUBLE CDECL
MSVCR120_erfcl(LDOUBLE x
)
2962 return MSVCR120_erfc(x
);
2965 /*********************************************************************
2966 * fmaxf (MSVCR120.@)
2968 float CDECL
MSVCR120_fmaxf(float x
, float y
)
2975 return signbit(x
) ? y
: x
;
2979 /*********************************************************************
2982 double CDECL
MSVCR120_fmax(double x
, double y
)
2989 return signbit(x
) ? y
: x
;
2993 /*********************************************************************
2994 * _fdsign (MSVCR120.@)
2996 int CDECL
MSVCR120__fdsign(float x
)
2998 return signbit(x
) ? 0x8000 : 0;
3001 /*********************************************************************
3002 * _dsign (MSVCR120.@)
3004 int CDECL
MSVCR120__dsign(double x
)
3006 return signbit(x
) ? 0x8000 : 0;
3010 /*********************************************************************
3011 * _dpcomp (MSVCR120.@)
3013 int CDECL
MSVCR120__dpcomp(double x
, double y
)
3015 if(isnan(x
) || isnan(y
))
3018 if(x
== y
) return 2;
3019 return x
< y
? 1 : 4;
3022 /*********************************************************************
3023 * _fdpcomp (MSVCR120.@)
3025 int CDECL
MSVCR120__fdpcomp(float x
, float y
)
3027 return MSVCR120__dpcomp(x
, y
);
3030 /*********************************************************************
3031 * fminf (MSVCR120.@)
3033 float CDECL
MSVCR120_fminf(float x
, float y
)
3040 return signbit(x
) ? x
: y
;
3044 /*********************************************************************
3047 double CDECL
MSVCR120_fmin(double x
, double y
)
3054 return signbit(x
) ? x
: y
;
3058 /*********************************************************************
3059 * asinh (MSVCR120.@)
3061 double CDECL
MSVCR120_asinh(double x
)
3066 if (!isfinite(x
*x
+1)) return log(2) + log(x
);
3067 return log(x
+ sqrt(x
*x
+1));
3071 /*********************************************************************
3072 * asinhf (MSVCR120.@)
3074 float CDECL
MSVCR120_asinhf(float x
)
3079 return MSVCR120_asinh(x
);
3083 /*********************************************************************
3084 * asinhl (MSVCR120.@)
3086 LDOUBLE CDECL
MSVCR120_asinhl(LDOUBLE x
)
3088 return MSVCR120_asinh(x
);
3091 /*********************************************************************
3092 * acosh (MSVCR120.@)
3094 double CDECL
MSVCR120_acosh(double x
)
3096 if (x
< 1) *MSVCRT__errno() = MSVCRT_EDOM
;
3104 MSVCRT_fegetenv(&env
);
3105 env
.status
|= MSVCRT__SW_INVALID
;
3106 MSVCRT_fesetenv(&env
);
3109 if (!isfinite(x
*x
)) return log(2) + log(x
);
3110 return log(x
+ sqrt(x
*x
-1));
3114 /*********************************************************************
3115 * acoshf (MSVCR120.@)
3117 float CDECL
MSVCR120_acoshf(float x
)
3120 if (x
< 1) *MSVCRT__errno() = MSVCRT_EDOM
;
3124 return MSVCR120_acosh(x
);
3128 /*********************************************************************
3129 * acoshl (MSVCR120.@)
3131 LDOUBLE CDECL
MSVCR120_acoshl(LDOUBLE x
)
3133 return MSVCR120_acosh(x
);
3136 /*********************************************************************
3137 * atanh (MSVCR120.@)
3139 double CDECL
MSVCR120_atanh(double x
)
3143 if (x
> 1 || x
< -1) {
3146 *MSVCRT__errno() = MSVCRT_EDOM
;
3148 /* on Linux atanh returns -NAN in this case */
3149 MSVCRT_fegetenv(&env
);
3150 env
.status
|= MSVCRT__SW_INVALID
;
3151 MSVCRT_fesetenv(&env
);
3158 if (-1e-6 < x
&& x
< 1e-6) ret
= x
+ x
*x
*x
/3;
3159 else ret
= (log(1+x
) - log(1-x
)) / 2;
3162 if (!isfinite(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
3166 /*********************************************************************
3167 * atanhf (MSVCR120.@)
3169 float CDECL
MSVCR120_atanhf(float x
)
3174 if (x
> 1 || x
< -1) {
3177 *MSVCRT__errno() = MSVCRT_EDOM
;
3179 MSVCRT_fegetenv(&env
);
3180 env
.status
|= MSVCRT__SW_INVALID
;
3181 MSVCRT_fesetenv(&env
);
3187 if (!finitef(ret
)) *MSVCRT__errno() = MSVCRT_ERANGE
;
3190 return MSVCR120_atanh(x
);
3194 /*********************************************************************
3195 * atanhl (MSVCR120.@)
3197 LDOUBLE CDECL
MSVCR120_atanhl(LDOUBLE x
)
3199 return MSVCR120_atanh(x
);
3202 #endif /* _MSVCR_VER>=120 */
3204 /*********************************************************************
3206 * scalbn (MSVCR120.@)
3207 * scalbln (MSVCR120.@)
3209 double CDECL
MSVCRT__scalb(double num
, MSVCRT_long power
)
3211 return MSVCRT_ldexp(num
, power
);
3214 /*********************************************************************
3215 * _scalbf (MSVCRT.@)
3216 * scalbnf (MSVCR120.@)
3217 * scalblnf (MSVCR120.@)
3219 float CDECL
MSVCRT__scalbf(float num
, MSVCRT_long power
)
3221 return MSVCRT_ldexp(num
, power
);
3226 /*********************************************************************
3227 * scalbnl (MSVCR120.@)
3228 * scalblnl (MSVCR120.@)
3230 LDOUBLE CDECL
MSVCR120_scalbnl(LDOUBLE num
, MSVCRT_long power
)
3232 return MSVCRT__scalb(num
, power
);
3235 /*********************************************************************
3236 * remainder (MSVCR120.@)
3238 double CDECL
MSVCR120_remainder(double x
, double y
)
3240 #ifdef HAVE_REMAINDER
3241 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3242 if(!finite(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
3243 if(isnan(y
) || y
==0.0) *MSVCRT__errno() = MSVCRT_EDOM
;
3244 return remainder(x
, y
);
3246 FIXME( "not implemented\n" );
3251 /*********************************************************************
3252 * remainderf (MSVCR120.@)
3254 float CDECL
MSVCR120_remainderf(float x
, float y
)
3256 #ifdef HAVE_REMAINDERF
3257 /* this matches 64-bit Windows. 32-bit Windows is slightly different */
3258 if(!finitef(x
)) *MSVCRT__errno() = MSVCRT_EDOM
;
3259 if(isnan(y
) || y
==0.0f
) *MSVCRT__errno() = MSVCRT_EDOM
;
3260 return remainderf(x
, y
);
3262 FIXME( "not implemented\n" );
3267 /*********************************************************************
3268 * remainderl (MSVCR120.@)
3270 LDOUBLE CDECL
MSVCR120_remainderl(LDOUBLE x
, LDOUBLE y
)
3272 return MSVCR120_remainder(x
, y
);
3275 /*********************************************************************
3276 * lgamma (MSVCR120.@)
3278 double CDECL
MSVCR120_lgamma(double x
)
3283 FIXME( "not implemented\n" );
3288 /*********************************************************************
3289 * lgammaf (MSVCR120.@)
3291 float CDECL
MSVCR120_lgammaf(float x
)
3296 FIXME( "not implemented\n" );
3301 /*********************************************************************
3302 * lgammal (MSVCR120.@)
3304 LDOUBLE CDECL
MSVCR120_lgammal(LDOUBLE x
)
3306 return MSVCR120_lgamma(x
);
3309 /*********************************************************************
3312 double CDECL
MSVCR120_nan(const char *tagp
)
3314 /* Windows ignores input (MSDN) */
3318 /*********************************************************************
3321 float CDECL
MSVCR120_nanf(const char *tagp
)
3326 /*********************************************************************
3327 * _except1 (MSVCR120.@)
3329 * - find meaning of ignored cw and operation bits
3332 double CDECL
_except1(DWORD fpe
, _FP_OPERATION_CODE op
, double arg
, double res
, DWORD cw
, void *unk
)
3334 ULONG_PTR exception_arg
;
3335 DWORD exception
= 0;
3340 TRACE("(%x %x %lf %lf %x %p)\n", fpe
, op
, arg
, res
, cw
, unk
);
3343 cw
= ((cw
>> 7) & 0x3f) | ((cw
>> 3) & 0xc00);
3345 operation
= op
<< 5;
3346 exception_arg
= (ULONG_PTR
)&operation
;
3348 MSVCRT_fegetenv(&env
);
3350 if (fpe
& 0x1) { /* overflow */
3351 if ((fpe
== 0x1 && (cw
& 0x8)) || (fpe
==0x11 && (cw
& 0x28))) {
3352 /* 32-bit version also sets SW_INEXACT here */
3353 env
.status
|= MSVCRT__SW_OVERFLOW
;
3354 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3355 res
= signbit(res
) ? -INFINITY
: INFINITY
;
3357 exception
= EXCEPTION_FLT_OVERFLOW
;
3359 } else if (fpe
& 0x2) { /* underflow */
3360 if ((fpe
== 0x2 && (cw
& 0x10)) || (fpe
==0x12 && (cw
& 0x30))) {
3361 env
.status
|= MSVCRT__SW_UNDERFLOW
;
3362 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3363 res
= signbit(res
) ? -0.0 : 0.0;
3365 exception
= EXCEPTION_FLT_UNDERFLOW
;
3367 } else if (fpe
& 0x4) { /* zerodivide */
3368 if ((fpe
== 0x4 && (cw
& 0x4)) || (fpe
==0x14 && (cw
& 0x24))) {
3369 env
.status
|= MSVCRT__SW_ZERODIVIDE
;
3370 if (fpe
& 0x10) env
.status
|= MSVCRT__SW_INEXACT
;
3372 exception
= EXCEPTION_FLT_DIVIDE_BY_ZERO
;
3374 } else if (fpe
& 0x8) { /* invalid */
3375 if (fpe
== 0x8 && (cw
& 0x1)) {
3376 env
.status
|= MSVCRT__SW_INVALID
;
3378 exception
= EXCEPTION_FLT_INVALID_OPERATION
;
3380 } else if (fpe
& 0x10) { /* inexact */
3381 if (fpe
== 0x10 && (cw
& 0x20)) {
3382 env
.status
|= MSVCRT__SW_INEXACT
;
3384 exception
= EXCEPTION_FLT_INEXACT_RESULT
;
3390 MSVCRT_fesetenv(&env
);
3392 RaiseException(exception
, 0, 1, &exception_arg
);
3394 if (cw
& 0x1) fpword
|= MSVCRT__EM_INVALID
;
3395 if (cw
& 0x2) fpword
|= MSVCRT__EM_DENORMAL
;
3396 if (cw
& 0x4) fpword
|= MSVCRT__EM_ZERODIVIDE
;
3397 if (cw
& 0x8) fpword
|= MSVCRT__EM_OVERFLOW
;
3398 if (cw
& 0x10) fpword
|= MSVCRT__EM_UNDERFLOW
;
3399 if (cw
& 0x20) fpword
|= MSVCRT__EM_INEXACT
;
3402 case 0xc00: fpword
|= MSVCRT__RC_UP
|MSVCRT__RC_DOWN
; break;
3403 case 0x800: fpword
|= MSVCRT__RC_UP
; break;
3404 case 0x400: fpword
|= MSVCRT__RC_DOWN
; break;
3408 case 0x0: fpword
|= MSVCRT__PC_24
; break;
3409 case 0x200: fpword
|= MSVCRT__PC_53
; break;
3410 case 0x300: fpword
|= MSVCRT__PC_64
; break;
3412 if (cw
& 0x1000) fpword
|= MSVCRT__IC_AFFINE
;
3413 _control87(fpword
, 0xffffffff);
3418 _Dcomplex
* CDECL
MSVCR120__Cbuild(_Dcomplex
*ret
, double r
, double i
)
3425 double CDECL
MSVCR120_creal(_Dcomplex z
)
3430 int CDECL
MSVCR120_ilogb(double x
)
3432 if (!x
) return MSVCRT_FP_ILOGB0
;
3433 if (isnan(x
)) return MSVCRT_FP_ILOGBNAN
;
3434 if (isinf(x
)) return MSVCRT_INT_MAX
;
3443 int CDECL
MSVCR120_ilogbf(float x
)
3445 if (!x
) return MSVCRT_FP_ILOGB0
;
3446 if (isnan(x
)) return MSVCRT_FP_ILOGBNAN
;
3447 if (isinf(x
)) return MSVCRT_INT_MAX
;
3456 int CDECL
MSVCR120_ilogbl(LDOUBLE x
)
3458 return MSVCR120_ilogb(x
);
3461 #endif /* _MSVCR_VER>=120 */