1 /*
2 * msvcrt.dll math functions
3 *
4 * Copyright 2000 Jon Griffiths
5 *
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.
10 *
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.
15 *
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
19 */
20 #include "config.h"
21
22 #include <stdio.h>
23 #define __USE_ISOC9X 1
24 #define __USE_ISOC99 1
25 #include <math.h>
26 #ifdef HAVE_IEEEFP_H
27 #include <ieeefp.h>
28 #endif
29
30 #include "msvcrt.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
35
36 #ifndef HAVE_FINITE
37 #ifndef finite /* Could be a macro */
38 #ifdef isfinite
39 #define finite(x) isfinite(x)
40 #else
41 #define finite(x) (!isnan(x)) /* At least catch some cases */
42 #endif
43 #endif
44 #endif
45
46 #ifndef signbit
47 #define signbit(x) 0
48 #endif
49
50 typedef int (*MSVCRT_matherr_func)(struct MSVCRT__exception *);
51
52 static MSVCRT_matherr_func MSVCRT_default_matherr_func = NULL;
53
54 /*********************************************************************
55 * MSVCRT_acos (MSVCRT.@)
56 */
57 double CDECL MSVCRT_acos( double x )
58 {
59 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
60 /* glibc implements acos() as the FPU equivalent of atan2(sqrt(1 - x ^ 2), x).
61 * asin() uses a similar construction. This is bad because as x gets nearer to
62 * 1 the error in the expression "1 - x^2" can get relatively large due to
63 * cancellation. The sqrt() makes things worse. A safer way to calculate
64 * acos() is to use atan2(sqrt((1 - x) * (1 + x)), x). */
65 return atan2(sqrt((1 - x) * (1 + x)), x);
66 }
67
68 /*********************************************************************
69 * MSVCRT_asin (MSVCRT.@)
70 */
71 double CDECL MSVCRT_asin( double x )
72 {
73 if (x < -1.0 || x > 1.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
74 return atan2(x, sqrt((1 - x) * (1 + x)));
75 }
76
77 /*********************************************************************
78 * MSVCRT_atan (MSVCRT.@)
79 */
80 double CDECL MSVCRT_atan( double x )
81 {
82 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
83 return atan(x);
84 }
85
86 /*********************************************************************
87 * MSVCRT_atan2 (MSVCRT.@)
88 */
89 double CDECL MSVCRT_atan2( double x, double y )
90 {
91 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
92 return atan2(x,y);
93 }
94
95 /*********************************************************************
96 * MSVCRT_cos (MSVCRT.@)
97 */
98 double CDECL MSVCRT_cos( double x )
99 {
100 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
101 return cos(x);
102 }
103
104 /*********************************************************************
105 * MSVCRT_cosh (MSVCRT.@)
106 */
107 double CDECL MSVCRT_cosh( double x )
108 {
109 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
110 return cosh(x);
111 }
112
113 /*********************************************************************
114 * MSVCRT_exp (MSVCRT.@)
115 */
116 double CDECL MSVCRT_exp( double x )
117 {
118 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
119 return exp(x);
120 }
121
122 /*********************************************************************
123 * MSVCRT_fmod (MSVCRT.@)
124 */
125 double CDECL MSVCRT_fmod( double x, double y )
126 {
127 if (!finite(x) || !finite(y)) *MSVCRT__errno() = MSVCRT_EDOM;
128 return fmod(x,y);
129 }
130
131 /*********************************************************************
132 * MSVCRT_log (MSVCRT.@)
133 */
134 double CDECL MSVCRT_log( double x)
135 {
136 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
137 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
138 return log(x);
139 }
140
141 /*********************************************************************
142 * MSVCRT_log10 (MSVCRT.@)
143 */
144 double CDECL MSVCRT_log10( double x )
145 {
146 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
147 if (x == 0.0) *MSVCRT__errno() = MSVCRT_ERANGE;
148 return log10(x);
149 }
150
151 /*********************************************************************
152 * MSVCRT_pow (MSVCRT.@)
153 */
154 double CDECL MSVCRT_pow( double x, double y )
155 {
156 /* FIXME: If x < 0 and y is not integral, set EDOM */
157 double z = pow(x,y);
158 if (!finite(z)) *MSVCRT__errno() = MSVCRT_EDOM;
159 return z;
160 }
161
162 /*********************************************************************
163 * MSVCRT_sin (MSVCRT.@)
164 */
165 double CDECL MSVCRT_sin( double x )
166 {
167 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
168 return sin(x);
169 }
170
171 /*********************************************************************
172 * MSVCRT_sinh (MSVCRT.@)
173 */
174 double CDECL MSVCRT_sinh( double x )
175 {
176 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
177 return sinh(x);
178 }
179
180 /*********************************************************************
181 * MSVCRT_sqrt (MSVCRT.@)
182 */
183 double CDECL MSVCRT_sqrt( double x )
184 {
185 if (x < 0.0 || !finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
186 return sqrt(x);
187 }
188
189 /*********************************************************************
190 * MSVCRT_tan (MSVCRT.@)
191 */
192 double CDECL MSVCRT_tan( double x )
193 {
194 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
195 return tan(x);
196 }
197
198 /*********************************************************************
199 * MSVCRT_tanh (MSVCRT.@)
200 */
201 double CDECL MSVCRT_tanh( double x )
202 {
203 if (!finite(x)) *MSVCRT__errno() = MSVCRT_EDOM;
204 return tanh(x);
205 }
206
207
208 #if defined(__GNUC__) && defined(__i386__)
209
210 #define FPU_DOUBLE(var) double var; \
211 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var) : )
212 #define FPU_DOUBLES(var1,var2) double var1,var2; \
213 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var2) : ); \
214 __asm__ __volatile__( "fstpl %0;fwait" : "=m" (var1) : )
215
216 /*********************************************************************
217 * _CIacos (MSVCRT.@)
218 */
219 double CDECL _CIacos(void)
220 {
221 FPU_DOUBLE(x);
222 return MSVCRT_acos(x);
223 }
224
225 /*********************************************************************
226 * _CIasin (MSVCRT.@)
227 */
228 double CDECL _CIasin(void)
229 {
230 FPU_DOUBLE(x);
231 return MSVCRT_asin(x);
232 }
233
234 /*********************************************************************
235 * _CIatan (MSVCRT.@)
236 */
237 double CDECL _CIatan(void)
238 {
239 FPU_DOUBLE(x);
240 return MSVCRT_atan(x);
241 }
242
243 /*********************************************************************
244 * _CIatan2 (MSVCRT.@)
245 */
246 double CDECL _CIatan2(void)
247 {
248 FPU_DOUBLES(x,y);
249 return MSVCRT_atan2(x,y);
250 }
251
252 /*********************************************************************
253 * _CIcos (MSVCRT.@)
254 */
255 double CDECL _CIcos(void)
256 {
257 FPU_DOUBLE(x);
258 return MSVCRT_cos(x);
259 }
260
261 /*********************************************************************
262 * _CIcosh (MSVCRT.@)
263 */
264 double CDECL _CIcosh(void)
265 {
266 FPU_DOUBLE(x);
267 return MSVCRT_cosh(x);
268 }
269
270 /*********************************************************************
271 * _CIexp (MSVCRT.@)
272 */
273 double CDECL _CIexp(void)
274 {
275 FPU_DOUBLE(x);
276 return MSVCRT_exp(x);
277 }
278
279 /*********************************************************************
280 * _CIfmod (MSVCRT.@)
281 */
282 double CDECL _CIfmod(void)
283 {
284 FPU_DOUBLES(x,y);
285 return MSVCRT_fmod(x,y);
286 }
287
288 /*********************************************************************
289 * _CIlog (MSVCRT.@)
290 */
291 double CDECL _CIlog(void)
292 {
293 FPU_DOUBLE(x);
294 return MSVCRT_log(x);
295 }
296
297 /*********************************************************************
298 * _CIlog10 (MSVCRT.@)
299 */
300 double CDECL _CIlog10(void)
301 {
302 FPU_DOUBLE(x);
303 return MSVCRT_log10(x);
304 }
305
306 /*********************************************************************
307 * _CIpow (MSVCRT.@)
308 */
309 double CDECL _CIpow(void)
310 {
311 FPU_DOUBLES(x,y);
312 return MSVCRT_pow(x,y);
313 }
314
315 /*********************************************************************
316 * _CIsin (MSVCRT.@)
317 */
318 double CDECL _CIsin(void)
319 {
320 FPU_DOUBLE(x);
321 return MSVCRT_sin(x);
322 }
323
324 /*********************************************************************
325 * _CIsinh (MSVCRT.@)
326 */
327 double CDECL _CIsinh(void)
328 {
329 FPU_DOUBLE(x);
330 return MSVCRT_sinh(x);
331 }
332
333 /*********************************************************************
334 * _CIsqrt (MSVCRT.@)
335 */
336 double CDECL _CIsqrt(void)
337 {
338 FPU_DOUBLE(x);
339 return MSVCRT_sqrt(x);
340 }
341
342 /*********************************************************************
343 * _CItan (MSVCRT.@)
344 */
345 double CDECL _CItan(void)
346 {
347 FPU_DOUBLE(x);
348 return MSVCRT_tan(x);
349 }
350
351 /*********************************************************************
352 * _CItanh (MSVCRT.@)
353 */
354 double CDECL _CItanh(void)
355 {
356 FPU_DOUBLE(x);
357 return MSVCRT_tanh(x);
358 }
359
360 #endif /* defined(__GNUC__) && defined(__i386__) */
361
362 /*********************************************************************
363 * _fpclass (MSVCRT.@)
364 */
365 int CDECL _fpclass(double num)
366 {
367 #if defined(HAVE_FPCLASS) || defined(fpclass)
368 switch (fpclass( num ))
369 {
370 #ifdef FP_SNAN
371 case FP_SNAN: return MSVCRT__FPCLASS_SNAN;
372 #endif
373 #ifdef FP_QNAN
374 case FP_QNAN: return MSVCRT__FPCLASS_QNAN;
375 #endif
376 #ifdef FP_NINF
377 case FP_NINF: return MSVCRT__FPCLASS_NINF;
378 #endif
379 #ifdef FP_PINF
380 case FP_PINF: return MSVCRT__FPCLASS_PINF;
381 #endif
382 #ifdef FP_NDENORM
383 case FP_NDENORM: return MSVCRT__FPCLASS_ND;
384 #endif
385 #ifdef FP_PDENORM
386 case FP_PDENORM: return MSVCRT__FPCLASS_PD;
387 #endif
388 #ifdef FP_NZERO
389 case FP_NZERO: return MSVCRT__FPCLASS_NZ;
390 #endif
391 #ifdef FP_PZERO
392 case FP_PZERO: return MSVCRT__FPCLASS_PZ;
393 #endif
394 #ifdef FP_NNORM
395 case FP_NNORM: return MSVCRT__FPCLASS_NN;
396 #endif
397 #ifdef FP_PNORM
398 case FP_PNORM: return MSVCRT__FPCLASS_PN;
399 #endif
400 default: return MSVCRT__FPCLASS_PN;
401 }
402 #elif defined (fpclassify)
403 switch (fpclassify( num ))
404 {
405 case FP_NAN: return MSVCRT__FPCLASS_QNAN;
406 case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
407 case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
408 case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
409 }
410 return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
411 #else
412 if (!finite(num))
413 return MSVCRT__FPCLASS_QNAN;
414 return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
415 #endif
416 }
417
418 /*********************************************************************
419 * _rotl (MSVCRT.@)
420 */
421 unsigned int CDECL _rotl(unsigned int num, int shift)
422 {
423 shift &= 31;
424 return (num << shift) | (num >> (32-shift));
425 }
426
427 /*********************************************************************
428 * _logb (MSVCRT.@)
429 */
430 double CDECL _logb(double num)
431 {
432 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
433 return logb(num);
434 }
435
436 /*********************************************************************
437 * _lrotl (MSVCRT.@)
438 */
439 MSVCRT_ulong CDECL _lrotl(MSVCRT_ulong num, int shift)
440 {
441 shift &= 0x1f;
442 return (num << shift) | (num >> (32-shift));
443 }
444
445 /*********************************************************************
446 * _lrotr (MSVCRT.@)
447 */
448 MSVCRT_ulong CDECL _lrotr(MSVCRT_ulong num, int shift)
449 {
450 shift &= 0x1f;
451 return (num >> shift) | (num << (32-shift));
452 }
453
454 /*********************************************************************
455 * _rotr (MSVCRT.@)
456 */
457 unsigned int CDECL _rotr(unsigned int num, int shift)
458 {
459 shift &= 0x1f;
460 return (num >> shift) | (num << (32-shift));
461 }
462
463 /*********************************************************************
464 * _scalb (MSVCRT.@)
465 */
466 double CDECL _scalb(double num, MSVCRT_long power)
467 {
468 /* Note - Can't forward directly as libc expects y as double */
469 double dblpower = (double)power;
470 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
471 return scalb(num, dblpower);
472 }
473
474 /*********************************************************************
475 * _hypot (MSVCRT.@)
476 */
477 double CDECL _hypot(double x, double y)
478 {
479 /* FIXME: errno handling */
480 return hypot( x, y );
481 }
482
483 /*********************************************************************
484 * ceil (MSVCRT.@)
485 */
486 double CDECL MSVCRT_ceil( double x )
487 {
488 return ceil(x);
489 }
490
491 /*********************************************************************
492 * floor (MSVCRT.@)
493 */
494 double CDECL MSVCRT_floor( double x )
495 {
496 return floor(x);
497 }
498
499 /*********************************************************************
500 * fabs (MSVCRT.@)
501 */
502 double CDECL MSVCRT_fabs( double x )
503 {
504 return fabs(x);
505 }
506
507 /*********************************************************************
508 * frexp (MSVCRT.@)
509 */
510 double CDECL MSVCRT_frexp( double x, int *exp )
511 {
512 return frexp( x, exp );
513 }
514
515 /*********************************************************************
516 * modf (MSVCRT.@)
517 */
518 double CDECL MSVCRT_modf( double x, double *iptr )
519 {
520 return modf( x, iptr );
521 }
522
523 /*********************************************************************
524 * _matherr (MSVCRT.@)
525 */
526 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
527 {
528 if (e)
529 TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
530 e->retval);
531 else
532 TRACE("(null)\n");
533 if (MSVCRT_default_matherr_func)
534 return MSVCRT_default_matherr_func(e);
535 ERR(":Unhandled math error!\n");
536 return 0;
537 }
538
539 /*********************************************************************
540 * __setusermatherr (MSVCRT.@)
541 */
542 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
543 {
544 MSVCRT_default_matherr_func = func;
545 TRACE(":new matherr handler %p\n", func);
546 }
547
548 /**********************************************************************
549 * _statusfp (MSVCRT.@)
550 */
551 unsigned int CDECL _statusfp(void)
552 {
553 unsigned int retVal = 0;
554 #if defined(__GNUC__) && defined(__i386__)
555 unsigned int fpword;
556
557 __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
558 if (fpword & 0x1) retVal |= MSVCRT__SW_INVALID;
559 if (fpword & 0x2) retVal |= MSVCRT__SW_DENORMAL;
560 if (fpword & 0x4) retVal |= MSVCRT__SW_ZERODIVIDE;
561 if (fpword & 0x8) retVal |= MSVCRT__SW_OVERFLOW;
562 if (fpword & 0x10) retVal |= MSVCRT__SW_UNDERFLOW;
563 if (fpword & 0x20) retVal |= MSVCRT__SW_INEXACT;
564 #else
565 FIXME(":Not implemented!\n");
566 #endif
567 return retVal;
568 }
569
570 /*********************************************************************
571 * _clearfp (MSVCRT.@)
572 */
573 unsigned int CDECL _clearfp(void)
574 {
575 unsigned int retVal = _statusfp();
576 #if defined(__GNUC__) && defined(__i386__)
577 __asm__ __volatile__( "fnclex" );
578 #else
579 FIXME(":Not Implemented\n");
580 #endif
581 return retVal;
582 }
583
584 /*********************************************************************
585 * __fpecode (MSVCRT.@)
586 */
587 int * CDECL __fpecode(void)
588 {
589 return &msvcrt_get_thread_data()->fpecode;
590 }
591
592 /*********************************************************************
593 * ldexp (MSVCRT.@)
594 */
595 double CDECL MSVCRT_ldexp(double num, MSVCRT_long exp)
596 {
597 double z = ldexp(num,exp);
598
599 if (!finite(z))
600 *MSVCRT__errno() = MSVCRT_ERANGE;
601 else if (z == 0 && signbit(z))
602 z = 0.0; /* Convert -0 -> +0 */
603 return z;
604 }
605
606 /*********************************************************************
607 * _cabs (MSVCRT.@)
608 */
609 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
610 {
611 return sqrt(num.x * num.x + num.y * num.y);
612 }
613
614 /*********************************************************************
615 * _chgsign (MSVCRT.@)
616 */
617 double CDECL _chgsign(double num)
618 {
619 /* FIXME: +-infinity,Nan not tested */
620 return -num;
621 }
622
623 /*********************************************************************
624 * _control87 (MSVCRT.@)
625 */
626 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
627 {
628 #if defined(__GNUC__) && defined(__i386__)
629 unsigned int fpword = 0;
630 unsigned int flags = 0;
631
632 TRACE("(%08x, %08x): Called\n", newval, mask);
633
634 /* Get fp control word */
635 __asm__ __volatile__( "fstcw %0" : "=m" (fpword) : );
636
637 TRACE("Control word before : %08x\n", fpword);
638
639 /* Convert into mask constants */
640 if (fpword & 0x1) flags |= MSVCRT__EM_INVALID;
641 if (fpword & 0x2) flags |= MSVCRT__EM_DENORMAL;
642 if (fpword & 0x4) flags |= MSVCRT__EM_ZERODIVIDE;
643 if (fpword & 0x8) flags |= MSVCRT__EM_OVERFLOW;
644 if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
645 if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
646 switch(fpword & 0xC00) {
647 case 0xC00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
648 case 0x800: flags |= MSVCRT__RC_UP; break;
649 case 0x400: flags |= MSVCRT__RC_DOWN; break;
650 }
651 switch(fpword & 0x300) {
652 case 0x0: flags |= MSVCRT__PC_24; break;
653 case 0x200: flags |= MSVCRT__PC_53; break;
654 case 0x300: flags |= MSVCRT__PC_64; break;
655 }
656 if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
657
658 /* Mask with parameters */
659 flags = (flags & ~mask) | (newval & mask);
660
661 /* Convert (masked) value back to fp word */
662 fpword = 0;
663 if (flags & MSVCRT__EM_INVALID) fpword |= 0x1;
664 if (flags & MSVCRT__EM_DENORMAL) fpword |= 0x2;
665 if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
666 if (flags & MSVCRT__EM_OVERFLOW) fpword |= 0x8;
667 if (flags & MSVCRT__EM_UNDERFLOW) fpword |= 0x10;
668 if (flags & MSVCRT__EM_INEXACT) fpword |= 0x20;
669 switch(flags & (MSVCRT__RC_UP | MSVCRT__RC_DOWN)) {
670 case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xC00; break;
671 case MSVCRT__RC_UP: fpword |= 0x800; break;
672 case MSVCRT__RC_DOWN: fpword |= 0x400; break;
673 }
674 switch (flags & (MSVCRT__PC_24 | MSVCRT__PC_53)) {
675 case MSVCRT__PC_64: fpword |= 0x300; break;
676 case MSVCRT__PC_53: fpword |= 0x200; break;
677 case MSVCRT__PC_24: fpword |= 0x0; break;
678 }
679 if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
680
681 TRACE("Control word after : %08x\n", fpword);
682
683 /* Put fp control word */
684 __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
685
686 return flags;
687 #else
688 FIXME(":Not Implemented!\n");
689 return 0;
690 #endif
691 }
692
693 /*********************************************************************
694 * _controlfp (MSVCRT.@)
695 */
696 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
697 {
698 #ifdef __i386__
699 return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
700 #else
701 FIXME(":Not Implemented!\n");
702 return 0;
703 #endif
704 }
705
706 /*********************************************************************
707 * _copysign (MSVCRT.@)
708 */
709 double CDECL _copysign(double num, double sign)
710 {
711 /* FIXME: Behaviour for Nan/Inf? */
712 if (sign < 0.0)
713 return num < 0.0 ? num : -num;
714 return num < 0.0 ? -num : num;
715 }
716
717 /*********************************************************************
718 * _finite (MSVCRT.@)
719 */
720 int CDECL _finite(double num)
721 {
722 return (finite(num)?1:0); /* See comment for _isnan() */
723 }
724
725 /*********************************************************************
726 * _fpreset (MSVCRT.@)
727 */
728 void CDECL _fpreset(void)
729 {
730 #if defined(__GNUC__) && defined(__i386__)
731 __asm__ __volatile__( "fninit" );
732 #else
733 FIXME(":Not Implemented!\n");
734 #endif
735 }
736
737 /*********************************************************************
738 * _isnan (MSVCRT.@)
739 */
740 INT CDECL _isnan(double num)
741 {
742 /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
743 * Do the same, as the result may be used in calculations
744 */
745 return isnan(num) ? 1 : 0;
746 }
747
748 /*********************************************************************
749 * _j0 (MSVCRT.@)
750 */
751 double CDECL _j0(double num)
752 {
753 /* FIXME: errno handling */
754 return j0(num);
755 }
756
757 /*********************************************************************
758 * _j1 (MSVCRT.@)
759 */
760 double CDECL _j1(double num)
761 {
762 /* FIXME: errno handling */
763 return j1(num);
764 }
765
766 /*********************************************************************
767 * jn (MSVCRT.@)
768 */
769 double CDECL _jn(int n, double num)
770 {
771 /* FIXME: errno handling */
772 return jn(n, num);
773 }
774
775 /*********************************************************************
776 * _y0 (MSVCRT.@)
777 */
778 double CDECL _y0(double num)
779 {
780 double retval;
781 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
782 retval = y0(num);
783 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
784 {
785 *MSVCRT__errno() = MSVCRT_EDOM;
786 retval = sqrt(-1);
787 }
788 return retval;
789 }
790
791 /*********************************************************************
792 * _y1 (MSVCRT.@)
793 */
794 double CDECL _y1(double num)
795 {
796 double retval;
797 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
798 retval = y1(num);
799 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
800 {
801 *MSVCRT__errno() = MSVCRT_EDOM;
802 retval = sqrt(-1);
803 }
804 return retval;
805 }
806
807 /*********************************************************************
808 * _yn (MSVCRT.@)
809 */
810 double CDECL _yn(int order, double num)
811 {
812 double retval;
813 if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
814 retval = yn(order,num);
815 if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
816 {
817 *MSVCRT__errno() = MSVCRT_EDOM;
818 retval = sqrt(-1);
819 }
820 return retval;
821 }
822
823 /*********************************************************************
824 * _nextafter (MSVCRT.@)
825 */
826 double CDECL _nextafter(double num, double next)
827 {
828 double retval;
829 if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
830 retval = nextafter(num,next);
831 return retval;
832 }
833
834 /*********************************************************************
835 * _ecvt (MSVCRT.@)
836 */
837 char * CDECL _ecvt( double number, int ndigits, int *decpt, int *sign )
838 {
839 int prec, len;
840 thread_data_t *data = msvcrt_get_thread_data();
841 /* FIXME: check better for overflow (native supports over 300 chars's) */
842 ndigits = min( ndigits, 80 - 7); /* 7 : space for dec point, 1 for "e",
843 * 4 for exponent and one for
844 * terminating '\0' */
845 if (!data->efcvt_buffer)
846 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
847
848 if( number < 0) {
849 *sign = TRUE;
850 number = -number;
851 } else
852 *sign = FALSE;
853 /* handle cases with zero ndigits or less */
854 prec = ndigits;
855 if( prec < 1) prec = 2;
856 len = snprintf(data->efcvt_buffer, 80, "%.*le", prec - 1, number);
857 /* take the decimal "point away */
858 if( prec != 1)
859 memmove( data->efcvt_buffer + 1, data->efcvt_buffer + 2, len - 1 );
860 /* take the exponential "e" out */
861 data->efcvt_buffer[ prec] = '\0';
862 /* read the exponent */
863 sscanf( data->efcvt_buffer + prec + 1, "%d", decpt);
864 (*decpt)++;
865 /* adjust for some border cases */
866 if( data->efcvt_buffer[0] == '')/* value is zero */
867 *decpt = 0;
868 /* handle cases with zero ndigits or less */
869 if( ndigits < 1){
870 if( data->efcvt_buffer[ 0] >= '5')
871 (*decpt)++;
872 data->efcvt_buffer[ 0] = '\0';
873 }
874 TRACE("out=\"%s\"\n",data->efcvt_buffer);
875 return data->efcvt_buffer;
876 }
877
878 /***********************************************************************
879 * _fcvt (MSVCRT.@)
880 */
881 char * CDECL _fcvt( double number, int ndigits, int *decpt, int *sign )
882 {
883 thread_data_t *data = msvcrt_get_thread_data();
884 int stop, dec1, dec2;
885 char *ptr1, *ptr2, *first;
886 char buf[80]; /* ought to be enough */
887
888 if (!data->efcvt_buffer)
889 data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
890
891 if (number < 0)
892 {
893 *sign = 1;
894 number = -number;
895 } else *sign = 0;
896
897 snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
898 ptr1 = buf;
899 ptr2 = data->efcvt_buffer;
900 first = NULL;
901 dec1 = 0;
902 dec2 = 0;
903
904 /* For numbers below the requested resolution, work out where
905 the decimal point will be rather than finding it in the string */
906 if (number < 1.0 && number > 0.0) {
907 dec2 = log10(number + 1e-10);
908 if (-dec2 <= ndigits) dec2 = 0;
909 }
910
911 /* If requested digits is zero or less, we will need to truncate
912 * the returned string */
913 if (ndigits < 1) {
914 stop = strlen(buf) + ndigits;
915 } else {
916 stop = strlen(buf);
917 }
918
919 while (*ptr1 == '') ptr1++; /* Skip leading zeroes */
920 while (*ptr1 != '\0' && *ptr1 != '.') {
921 if (!first) first = ptr2;
922 if ((ptr1 - buf) < stop) {
923 *ptr2++ = *ptr1++;
924 } else {
925 ptr1++;
926 }
927 dec1++;
928 }
929
930 if (ndigits > 0) {
931 ptr1++;
932 if (!first) {
933 while (*ptr1 == '') { /* Process leading zeroes */
934 *ptr2++ = *ptr1++;
935 dec1--;
936 }
937 }
938 while (*ptr1 != '\0') {
939 if (!first) first = ptr2;
940 *ptr2++ = *ptr1++;
941 }
942 }
943
944 *ptr2 = '\0';
945
946 /* We never found a non-zero digit, then our number is either
947 * smaller than the requested precision, or 0.0 */
948 if (!first) {
949 if (number > 0.0) {
950 first = ptr2;
951 } else {
952 first = data->efcvt_buffer;
953 dec1 = 0;
954 }
955 }
956
957 *decpt = dec2 ? dec2 : dec1;
958 return first;
959 }
960
961 /***********************************************************************
962 * _gcvt (MSVCRT.@)
963 *
964 * FIXME: uses both E and F.
965 */
966 char * CDECL _gcvt( double number, int ndigit, char *buff )
967 {
968 sprintf(buff, "%.*E", ndigit, number);
969 return buff;
970 }
971
972 #include <stdlib.h> /* div_t, ldiv_t */
973
974 /*********************************************************************
975 * div (MSVCRT.@)
976 * VERSION
977 * [i386] Windows binary compatible - returns the struct in eax/edx.
978 */
979 #ifdef __i386__
980 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
981 {
982 div_t dt = div(num,denom);
983 return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
984 }
985 #else
986 /*********************************************************************
987 * div (MSVCRT.@)
988 * VERSION
989 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
990 */
991 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
992 {
993 div_t dt = div(num,denom);
994 MSVCRT_div_t ret;
995 ret.quot = dt.quot;
996 ret.rem = dt.rem;
997
998 return ret;
999
1000 }
1001 #endif /* ifdef __i386__ */
1002
1003
1004 /*********************************************************************
1005 * ldiv (MSVCRT.@)
1006 * VERSION
1007 * [i386] Windows binary compatible - returns the struct in eax/edx.
1008 */
1009 #ifdef __i386__
1010 unsigned __int64 CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1011 {
1012 ldiv_t ldt = ldiv(num,denom);
1013 return ((unsigned __int64)ldt.rem << 32) | (MSVCRT_ulong)ldt.quot;
1014 }
1015 #else
1016 /*********************************************************************
1017 * ldiv (MSVCRT.@)
1018 * VERSION
1019 * [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1020 */
1021 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(MSVCRT_long num, MSVCRT_long denom)
1022 {
1023 ldiv_t result = ldiv(num,denom);
1024
1025 MSVCRT_ldiv_t ret;
1026 ret.quot = result.quot;
1027 ret.rem = result.rem;
1028
1029 return ret;
1030 }
1031 #endif /* ifdef __i386__ */
1032
1033 #ifdef __i386__
1034
1035 /*********************************************************************
1036 * _adjust_fdiv (MSVCRT.@)
1037 * Used by the MSVC compiler to work around the Pentium FDIV bug.
1038 */
1039 int MSVCRT__adjust_fdiv = 0;
1040
1041 /***********************************************************************
1042 * _adj_fdiv_m16i (MSVCRT.@)
1043 *
1044 * NOTE
1045 * I _think_ this function is intended to work around the Pentium
1046 * fdiv bug.
1047 */
1048 void __stdcall _adj_fdiv_m16i( short arg )
1049 {
1050 TRACE("(): stub\n");
1051 }
1052
1053 /***********************************************************************
1054 * _adj_fdiv_m32 (MSVCRT.@)
1055 *
1056 * NOTE
1057 * I _think_ this function is intended to work around the Pentium
1058 * fdiv bug.
1059 */
1060 void __stdcall _adj_fdiv_m32( unsigned int arg )
1061 {
1062 TRACE("(): stub\n");
1063 }
1064
1065 /***********************************************************************
1066 * _adj_fdiv_m32i (MSVCRT.@)
1067 *
1068 * NOTE
1069 * I _think_ this function is intended to work around the Pentium
1070 * fdiv bug.
1071 */
1072 void __stdcall _adj_fdiv_m32i( int arg )
1073 {
1074 TRACE("(): stub\n");
1075 }
1076
1077 /***********************************************************************
1078 * _adj_fdiv_m64 (MSVCRT.@)
1079 *
1080 * NOTE
1081 * I _think_ this function is intended to work around the Pentium
1082 * fdiv bug.
1083 */
1084 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1085 {
1086 TRACE("(): stub\n");
1087 }
1088
1089 /***********************************************************************
1090 * _adj_fdiv_r (MSVCRT.@)
1091 * FIXME
1092 * This function is likely to have the wrong number of arguments.
1093 *
1094 * NOTE
1095 * I _think_ this function is intended to work around the Pentium
1096 * fdiv bug.
1097 */
1098 void _adj_fdiv_r(void)
1099 {
1100 TRACE("(): stub\n");
1101 }
1102
1103 /***********************************************************************
1104 * _adj_fdivr_m16i (MSVCRT.@)
1105 *
1106 * NOTE
1107 * I _think_ this function is intended to work around the Pentium
1108 * fdiv bug.
1109 */
1110 void __stdcall _adj_fdivr_m16i( short arg )
1111 {
1112 TRACE("(): stub\n");
1113 }
1114
1115 /***********************************************************************
1116 * _adj_fdivr_m32 (MSVCRT.@)
1117 *
1118 * NOTE
1119 * I _think_ this function is intended to work around the Pentium
1120 * fdiv bug.
1121 */
1122 void __stdcall _adj_fdivr_m32( unsigned int arg )
1123 {
1124 TRACE("(): stub\n");
1125 }
1126
1127 /***********************************************************************
1128 * _adj_fdivr_m32i (MSVCRT.@)
1129 *
1130 * NOTE
1131 * I _think_ this function is intended to work around the Pentium
1132 * fdiv bug.
1133 */
1134 void __stdcall _adj_fdivr_m32i( int arg )
1135 {
1136 TRACE("(): stub\n");
1137 }
1138
1139 /***********************************************************************
1140 * _adj_fdivr_m64 (MSVCRT.@)
1141 *
1142 * NOTE
1143 * I _think_ this function is intended to work around the Pentium
1144 * fdiv bug.
1145 */
1146 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1147 {
1148 TRACE("(): stub\n");
1149 }
1150
1151 /***********************************************************************
1152 * _adj_fpatan (MSVCRT.@)
1153 * FIXME
1154 * This function is likely to have the wrong number of arguments.
1155 *
1156 * NOTE
1157 * I _think_ this function is intended to work around the Pentium
1158 * fdiv bug.
1159 */
1160 void _adj_fpatan(void)
1161 {
1162 TRACE("(): stub\n");
1163 }
1164
1165 /***********************************************************************
1166 * _adj_fprem (MSVCRT.@)
1167 * FIXME
1168 * This function is likely to have the wrong number of arguments.
1169 *
1170 * NOTE
1171 * I _think_ this function is intended to work around the Pentium
1172 * fdiv bug.
1173 */
1174 void _adj_fprem(void)
1175 {
1176 TRACE("(): stub\n");
1177 }
1178
1179 /***********************************************************************
1180 * _adj_fprem1 (MSVCRT.@)
1181 * FIXME
1182 * This function is likely to have the wrong number of arguments.
1183 *
1184 * NOTE
1185 * I _think_ this function is intended to work around the Pentium
1186 * fdiv bug.
1187 */
1188 void _adj_fprem1(void)
1189 {
1190 TRACE("(): stub\n");
1191 }
1192
1193 /***********************************************************************
1194 * _adj_fptan (MSVCRT.@)
1195 * FIXME
1196 * This function is likely to have the wrong number of arguments.
1197 *
1198 * NOTE
1199 * I _think_ this function is intended to work around the Pentium
1200 * fdiv bug.
1201 */
1202 void _adj_fptan(void)
1203 {
1204 TRACE("(): stub\n");
1205 }
1206
1207 /***********************************************************************
1208 * _safe_fdiv (MSVCRT.@)
1209 * FIXME
1210 * This function is likely to have the wrong number of arguments.
1211 *
1212 * NOTE
1213 * I _think_ this function is intended to work around the Pentium
1214 * fdiv bug.
1215 */
1216 void _safe_fdiv(void)
1217 {
1218 TRACE("(): stub\n");
1219 }
1220
1221 /***********************************************************************
1222 * _safe_fdivr (MSVCRT.@)
1223 * FIXME
1224 * This function is likely to have the wrong number of arguments.
1225 *
1226 * NOTE
1227 * I _think_ this function is intended to work around the Pentium
1228 * fdiv bug.
1229 */
1230 void _safe_fdivr(void)
1231 {
1232 TRACE("(): stub\n");
1233 }
1234
1235 /***********************************************************************
1236 * _safe_fprem (MSVCRT.@)
1237 * FIXME
1238 * This function is likely to have the wrong number of arguments.
1239 *
1240 * NOTE
1241 * I _think_ this function is intended to work around the Pentium
1242 * fdiv bug.
1243 */
1244 void _safe_fprem(void)
1245 {
1246 TRACE("(): stub\n");
1247 }
1248
1249 /***********************************************************************
1250 * _safe_fprem1 (MSVCRT.@)
1251 *
1252 * FIXME
1253 * This function is likely to have the wrong number of arguments.
1254 *
1255 * NOTE
1256 * I _think_ this function is intended to work around the Pentium
1257 * fdiv bug.
1258 */
1259 void _safe_fprem1(void)
1260 {
1261 TRACE("(): stub\n");
1262 }
1263
1264 #endif /* __i386__ */
1265
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.