crypt32: Fix several failing tests in win9x.
[wine] / dlls / msvcrt / math.c
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 #else /* defined(__GNUC__) && defined(__i386__) */
361
362 /* The above cannot be called on non x86 platforms, stub them for linking */
363
364 #define IX86_ONLY(func) double func(void) { return 0.0; }
365
366 IX86_ONLY(_CIacos)
367 IX86_ONLY(_CIasin)
368 IX86_ONLY(_CIatan)
369 IX86_ONLY(_CIatan2)
370 IX86_ONLY(_CIcos)
371 IX86_ONLY(_CIcosh)
372 IX86_ONLY(_CIexp)
373 IX86_ONLY(_CIfmod)
374 IX86_ONLY(_CIlog)
375 IX86_ONLY(_CIlog10)
376 IX86_ONLY(_CIpow)
377 IX86_ONLY(_CIsin)
378 IX86_ONLY(_CIsinh)
379 IX86_ONLY(_CIsqrt)
380 IX86_ONLY(_CItan)
381 IX86_ONLY(_CItanh)
382
383 #endif /* defined(__GNUC__) && defined(__i386__) */
384
385 /*********************************************************************
386  *              _fpclass (MSVCRT.@)
387  */
388 int CDECL _fpclass(double num)
389 {
390 #if defined(HAVE_FPCLASS) || defined(fpclass)
391   switch (fpclass( num ))
392   {
393 #ifdef FP_SNAN
394   case FP_SNAN:  return MSVCRT__FPCLASS_SNAN;
395 #endif
396 #ifdef FP_QNAN
397   case FP_QNAN:  return MSVCRT__FPCLASS_QNAN;
398 #endif
399 #ifdef FP_NINF
400   case FP_NINF:  return MSVCRT__FPCLASS_NINF;
401 #endif
402 #ifdef FP_PINF
403   case FP_PINF:  return MSVCRT__FPCLASS_PINF;
404 #endif
405 #ifdef FP_NDENORM
406   case FP_NDENORM: return MSVCRT__FPCLASS_ND;
407 #endif
408 #ifdef FP_PDENORM
409   case FP_PDENORM: return MSVCRT__FPCLASS_PD;
410 #endif
411 #ifdef FP_NZERO
412   case FP_NZERO: return MSVCRT__FPCLASS_NZ;
413 #endif
414 #ifdef FP_PZERO
415   case FP_PZERO: return MSVCRT__FPCLASS_PZ;
416 #endif
417 #ifdef FP_NNORM
418   case FP_NNORM: return MSVCRT__FPCLASS_NN;
419 #endif
420 #ifdef FP_PNORM
421   case FP_PNORM: return MSVCRT__FPCLASS_PN;
422 #endif
423   }
424   return MSVCRT__FPCLASS_PN;
425 #elif defined (fpclassify)
426   switch (fpclassify( num ))
427   {
428   case FP_NAN: return MSVCRT__FPCLASS_QNAN;
429   case FP_INFINITE: return signbit(num) ? MSVCRT__FPCLASS_NINF : MSVCRT__FPCLASS_PINF;
430   case FP_SUBNORMAL: return signbit(num) ?MSVCRT__FPCLASS_ND : MSVCRT__FPCLASS_PD;
431   case FP_ZERO: return signbit(num) ? MSVCRT__FPCLASS_NZ : MSVCRT__FPCLASS_PZ;
432   }
433   return signbit(num) ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN;
434 #else
435   if (!finite(num))
436     return MSVCRT__FPCLASS_QNAN;
437   return num == 0.0 ? MSVCRT__FPCLASS_PZ : (num < 0 ? MSVCRT__FPCLASS_NN : MSVCRT__FPCLASS_PN);
438 #endif
439 }
440
441 /*********************************************************************
442  *              _rotl (MSVCRT.@)
443  */
444 unsigned int CDECL _rotl(unsigned int num, int shift)
445 {
446   shift &= 31;
447   return (num << shift) | (num >> (32-shift));
448 }
449
450 /*********************************************************************
451  *              _logb (MSVCRT.@)
452  */
453 double CDECL _logb(double num)
454 {
455   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
456   return logb(num);
457 }
458
459 /*********************************************************************
460  *              _lrotl (MSVCRT.@)
461  */
462 unsigned long CDECL _lrotl(unsigned long num, int shift)
463 {
464   shift &= 0x1f;
465   return (num << shift) | (num >> (32-shift));
466 }
467
468 /*********************************************************************
469  *              _lrotr (MSVCRT.@)
470  */
471 unsigned long CDECL _lrotr(unsigned long num, int shift)
472 {
473   shift &= 0x1f;
474   return (num >> shift) | (num << (32-shift));
475 }
476
477 /*********************************************************************
478  *              _rotr (MSVCRT.@)
479  */
480 unsigned int CDECL _rotr(unsigned int num, int shift)
481 {
482     shift &= 0x1f;
483     return (num >> shift) | (num << (32-shift));
484 }
485
486 /*********************************************************************
487  *              _scalb (MSVCRT.@)
488  */
489 double CDECL _scalb(double num, long power)
490 {
491   /* Note - Can't forward directly as libc expects y as double */
492   double dblpower = (double)power;
493   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
494   return scalb(num, dblpower);
495 }
496
497 /*********************************************************************
498  *              _hypot (MSVCRT.@)
499  */
500 double CDECL _hypot(double x, double y)
501 {
502   /* FIXME: errno handling */
503   return hypot( x, y );
504 }
505
506 /*********************************************************************
507  *              ceil (MSVCRT.@)
508  */
509 double CDECL MSVCRT_ceil( double x )
510 {
511   return ceil(x);
512 }
513
514 /*********************************************************************
515  *              floor (MSVCRT.@)
516  */
517 double CDECL MSVCRT_floor( double x )
518 {
519   return floor(x);
520 }
521
522 /*********************************************************************
523  *              fabs (MSVCRT.@)
524  */
525 double CDECL MSVCRT_fabs( double x )
526 {
527   return fabs(x);
528 }
529
530 /*********************************************************************
531  *              frexp (MSVCRT.@)
532  */
533 double CDECL MSVCRT_frexp( double x, int *exp )
534 {
535   return frexp( x, exp );
536 }
537
538 /*********************************************************************
539  *              modf (MSVCRT.@)
540  */
541 double CDECL MSVCRT_modf( double x, double *iptr )
542 {
543   return modf( x, iptr );
544 }
545
546 /*********************************************************************
547  *              _matherr (MSVCRT.@)
548  */
549 int CDECL MSVCRT__matherr(struct MSVCRT__exception *e)
550 {
551   if (e)
552     TRACE("(%p = %d, %s, %g %g %g)\n",e, e->type, e->name, e->arg1, e->arg2,
553           e->retval);
554   else
555     TRACE("(null)\n");
556   if (MSVCRT_default_matherr_func)
557     return MSVCRT_default_matherr_func(e);
558   ERR(":Unhandled math error!\n");
559   return 0;
560 }
561
562 /*********************************************************************
563  *              __setusermatherr (MSVCRT.@)
564  */
565 void CDECL MSVCRT___setusermatherr(MSVCRT_matherr_func func)
566 {
567   MSVCRT_default_matherr_func = func;
568   TRACE(":new matherr handler %p\n", func);
569 }
570
571 /**********************************************************************
572  *              _statusfp (MSVCRT.@)
573  */
574 unsigned int CDECL _statusfp(void)
575 {
576    unsigned int retVal = 0;
577 #if defined(__GNUC__) && defined(__i386__)
578   unsigned int fpword;
579
580   __asm__ __volatile__( "fstsw %0" : "=m" (fpword) : );
581   if (fpword & 0x1)  retVal |= MSVCRT__SW_INVALID;
582   if (fpword & 0x2)  retVal |= MSVCRT__SW_DENORMAL;
583   if (fpword & 0x4)  retVal |= MSVCRT__SW_ZERODIVIDE;
584   if (fpword & 0x8)  retVal |= MSVCRT__SW_OVERFLOW;
585   if (fpword & 0x10) retVal |= MSVCRT__SW_UNDERFLOW;
586   if (fpword & 0x20) retVal |= MSVCRT__SW_INEXACT;
587 #else
588   FIXME(":Not implemented!\n");
589 #endif
590   return retVal;
591 }
592
593 /*********************************************************************
594  *              _clearfp (MSVCRT.@)
595  */
596 unsigned int CDECL _clearfp(void)
597 {
598   unsigned int retVal = _statusfp();
599 #if defined(__GNUC__) && defined(__i386__)
600   __asm__ __volatile__( "fnclex" );
601 #else
602   FIXME(":Not Implemented\n");
603 #endif
604   return retVal;
605 }
606
607 /*********************************************************************
608  *              __fpecode (MSVCRT.@)
609  */
610 int * CDECL __fpecode(void)
611 {
612     return &msvcrt_get_thread_data()->fpecode;
613 }
614
615 /*********************************************************************
616  *              ldexp (MSVCRT.@)
617  */
618 double CDECL MSVCRT_ldexp(double num, long exp)
619 {
620   double z = ldexp(num,exp);
621
622   if (!finite(z))
623     *MSVCRT__errno() = MSVCRT_ERANGE;
624   else if (z == 0 && signbit(z))
625     z = 0.0; /* Convert -0 -> +0 */
626   return z;
627 }
628
629 /*********************************************************************
630  *              _cabs (MSVCRT.@)
631  */
632 double CDECL MSVCRT__cabs(struct MSVCRT__complex num)
633 {
634   return sqrt(num.x * num.x + num.y * num.y);
635 }
636
637 /*********************************************************************
638  *              _chgsign (MSVCRT.@)
639  */
640 double CDECL _chgsign(double num)
641 {
642   /* FIXME: +-infinity,Nan not tested */
643   return -num;
644 }
645
646 /*********************************************************************
647  *              _control87 (MSVCRT.@)
648  */
649 unsigned int CDECL _control87(unsigned int newval, unsigned int mask)
650 {
651 #if defined(__GNUC__) && defined(__i386__)
652   unsigned int fpword = 0;
653   unsigned int flags = 0;
654
655   TRACE("(%08x, %08x): Called\n", newval, mask);
656
657   /* Get fp control word */
658   __asm__ __volatile__( "fstcw %0" : "=m" (fpword) : );
659
660   TRACE("Control word before : %08x\n", fpword);
661
662   /* Convert into mask constants */
663   if (fpword & 0x1)  flags |= MSVCRT__EM_INVALID;
664   if (fpword & 0x2)  flags |= MSVCRT__EM_DENORMAL;
665   if (fpword & 0x4)  flags |= MSVCRT__EM_ZERODIVIDE;
666   if (fpword & 0x8)  flags |= MSVCRT__EM_OVERFLOW;
667   if (fpword & 0x10) flags |= MSVCRT__EM_UNDERFLOW;
668   if (fpword & 0x20) flags |= MSVCRT__EM_INEXACT;
669   switch(fpword & 0xC00) {
670   case 0xC00: flags |= MSVCRT__RC_UP|MSVCRT__RC_DOWN; break;
671   case 0x800: flags |= MSVCRT__RC_UP; break;
672   case 0x400: flags |= MSVCRT__RC_DOWN; break;
673   }
674   switch(fpword & 0x300) {
675   case 0x0:   flags |= MSVCRT__PC_24; break;
676   case 0x200: flags |= MSVCRT__PC_53; break;
677   case 0x300: flags |= MSVCRT__PC_64; break;
678   }
679   if (fpword & 0x1000) flags |= MSVCRT__IC_AFFINE;
680
681   /* Mask with parameters */
682   flags = (flags & ~mask) | (newval & mask);
683
684   /* Convert (masked) value back to fp word */
685   fpword = 0;
686   if (flags & MSVCRT__EM_INVALID)    fpword |= 0x1;
687   if (flags & MSVCRT__EM_DENORMAL)   fpword |= 0x2;
688   if (flags & MSVCRT__EM_ZERODIVIDE) fpword |= 0x4;
689   if (flags & MSVCRT__EM_OVERFLOW)   fpword |= 0x8;
690   if (flags & MSVCRT__EM_UNDERFLOW)  fpword |= 0x10;
691   if (flags & MSVCRT__EM_INEXACT)    fpword |= 0x20;
692   switch(flags & (MSVCRT__RC_UP | MSVCRT__RC_DOWN)) {
693   case MSVCRT__RC_UP|MSVCRT__RC_DOWN: fpword |= 0xC00; break;
694   case MSVCRT__RC_UP:          fpword |= 0x800; break;
695   case MSVCRT__RC_DOWN:        fpword |= 0x400; break;
696   }
697   switch (flags & (MSVCRT__PC_24 | MSVCRT__PC_53)) {
698   case MSVCRT__PC_64: fpword |= 0x300; break;
699   case MSVCRT__PC_53: fpword |= 0x200; break;
700   case MSVCRT__PC_24: fpword |= 0x0; break;
701   }
702   if (flags & MSVCRT__IC_AFFINE) fpword |= 0x1000;
703
704   TRACE("Control word after  : %08x\n", fpword);
705
706   /* Put fp control word */
707   __asm__ __volatile__( "fldcw %0" : : "m" (fpword) );
708
709   return flags;
710 #else
711   FIXME(":Not Implemented!\n");
712   return 0;
713 #endif
714 }
715
716 /*********************************************************************
717  *              _controlfp (MSVCRT.@)
718  */
719 unsigned int CDECL _controlfp(unsigned int newval, unsigned int mask)
720 {
721 #ifdef __i386__
722   return _control87( newval, mask & ~MSVCRT__EM_DENORMAL );
723 #else
724   FIXME(":Not Implemented!\n");
725   return 0;
726 #endif
727 }
728
729 /*********************************************************************
730  *              _copysign (MSVCRT.@)
731  */
732 double CDECL _copysign(double num, double sign)
733 {
734   /* FIXME: Behaviour for Nan/Inf? */
735   if (sign < 0.0)
736     return num < 0.0 ? num : -num;
737   return num < 0.0 ? -num : num;
738 }
739
740 /*********************************************************************
741  *              _finite (MSVCRT.@)
742  */
743 int CDECL _finite(double num)
744 {
745   return (finite(num)?1:0); /* See comment for _isnan() */
746 }
747
748 /*********************************************************************
749  *              _fpreset (MSVCRT.@)
750  */
751 void CDECL _fpreset(void)
752 {
753 #if defined(__GNUC__) && defined(__i386__)
754   __asm__ __volatile__( "fninit" );
755 #else
756   FIXME(":Not Implemented!\n");
757 #endif
758 }
759
760 /*********************************************************************
761  *              _isnan (MSVCRT.@)
762  */
763 INT CDECL _isnan(double num)
764 {
765   /* Some implementations return -1 for true(glibc), msvcrt/crtdll return 1.
766    * Do the same, as the result may be used in calculations
767    */
768   return isnan(num) ? 1 : 0;
769 }
770
771 /*********************************************************************
772  *              _j0 (MSVCRT.@)
773  */
774 double CDECL _j0(double num)
775 {
776   /* FIXME: errno handling */
777   return j0(num);
778 }
779
780 /*********************************************************************
781  *              _j1 (MSVCRT.@)
782  */
783 double CDECL _j1(double num)
784 {
785   /* FIXME: errno handling */
786   return j1(num);
787 }
788
789 /*********************************************************************
790  *              jn (MSVCRT.@)
791  */
792 double CDECL _jn(int n, double num)
793 {
794   /* FIXME: errno handling */
795   return jn(n, num);
796 }
797
798 /*********************************************************************
799  *              _y0 (MSVCRT.@)
800  */
801 double CDECL _y0(double num)
802 {
803   double retval;
804   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
805   retval  = y0(num);
806   if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
807   {
808     *MSVCRT__errno() = MSVCRT_EDOM;
809     retval = sqrt(-1);
810   }
811   return retval;
812 }
813
814 /*********************************************************************
815  *              _y1 (MSVCRT.@)
816  */
817 double CDECL _y1(double num)
818 {
819   double retval;
820   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
821   retval  = y1(num);
822   if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
823   {
824     *MSVCRT__errno() = MSVCRT_EDOM;
825     retval = sqrt(-1);
826   }
827   return retval;
828 }
829
830 /*********************************************************************
831  *              _yn (MSVCRT.@)
832  */
833 double CDECL _yn(int order, double num)
834 {
835   double retval;
836   if (!finite(num)) *MSVCRT__errno() = MSVCRT_EDOM;
837   retval  = yn(order,num);
838   if (_fpclass(retval) == MSVCRT__FPCLASS_NINF)
839   {
840     *MSVCRT__errno() = MSVCRT_EDOM;
841     retval = sqrt(-1);
842   }
843   return retval;
844 }
845
846 /*********************************************************************
847  *              _nextafter (MSVCRT.@)
848  */
849 double CDECL _nextafter(double num, double next)
850 {
851   double retval;
852   if (!finite(num) || !finite(next)) *MSVCRT__errno() = MSVCRT_EDOM;
853   retval = nextafter(num,next);
854   return retval;
855 }
856
857 /*********************************************************************
858  *              _ecvt (MSVCRT.@)
859  */
860 char * CDECL _ecvt( double number, int ndigits, int *decpt, int *sign )
861 {
862     thread_data_t *data = msvcrt_get_thread_data();
863     char *dec;
864
865     if (!data->efcvt_buffer)
866         data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
867
868     snprintf(data->efcvt_buffer, 80, "%.*e", ndigits /* FIXME wrong */, number);
869     *sign = (number < 0);
870     dec = strchr(data->efcvt_buffer, '.');
871     *decpt = (dec) ? dec - data->efcvt_buffer : -1;
872     return data->efcvt_buffer;
873 }
874
875 /***********************************************************************
876  *              _fcvt  (MSVCRT.@)
877  */
878 char * CDECL _fcvt( double number, int ndigits, int *decpt, int *sign )
879 {
880     thread_data_t *data = msvcrt_get_thread_data();
881     int stop, dec1, dec2;
882     char *ptr1, *ptr2, *first;
883     char buf[80]; /* ought to be enough */
884
885     if (!data->efcvt_buffer)
886         data->efcvt_buffer = MSVCRT_malloc( 80 ); /* ought to be enough */
887
888     if (number < 0)
889     {
890         *sign = 1;
891         number = -number;
892     } else *sign = 0;
893
894     snprintf(buf, 80, "%.*f", ndigits < 0 ? 0 : ndigits, number);
895     ptr1 = buf;
896     ptr2 = data->efcvt_buffer;
897     first = NULL;
898     dec1 = 0;
899     dec2 = 0;
900
901     /* For numbers below the requested resolution, work out where
902        the decimal point will be rather than finding it in the string */
903     if (number < 1.0 && number > 0.0) {
904         dec2 = log10(number + 1e-10);
905         if (-dec2 <= ndigits) dec2 = 0;
906     }
907
908     /* If requested digits is zero or less, we will need to truncate
909      * the returned string */
910     if (ndigits < 1) {
911         stop = strlen(buf) + ndigits;
912     } else {
913         stop = strlen(buf);
914     }
915
916     while (*ptr1 == '0') ptr1++; /* Skip leading zeroes */
917     while (*ptr1 != '\0' && *ptr1 != '.') {
918         if (!first) first = ptr2;
919         if ((ptr1 - buf) < stop) {
920             *ptr2++ = *ptr1++;
921         } else {
922             ptr1++;
923         }
924         dec1++;
925     }
926
927     if (ndigits > 0) {
928         ptr1++;
929         if (!first) {
930             while (*ptr1 == '0') { /* Process leading zeroes */
931                 *ptr2++ = *ptr1++;
932                 dec1--;
933             }
934         }
935         while (*ptr1 != '\0') {
936             if (!first) first = ptr2;
937             *ptr2++ = *ptr1++;
938         }
939     }
940
941     *ptr2 = '\0';
942
943     /* We never found a non-zero digit, then our number is either
944      * smaller than the requested precision, or 0.0 */
945     if (!first) {
946         if (number > 0.0) {
947             first = ptr2;
948         } else {
949             first = data->efcvt_buffer;
950             dec1 = 0;
951         }
952     }
953
954     *decpt = dec2 ? dec2 : dec1;
955     return first;
956 }
957
958 /***********************************************************************
959  *              _gcvt  (MSVCRT.@)
960  *
961  * FIXME: uses both E and F.
962  */
963 char * CDECL _gcvt( double number, int ndigit, char *buff )
964 {
965     sprintf(buff, "%.*E", ndigit, number);
966     return buff;
967 }
968
969 #include <stdlib.h> /* div_t, ldiv_t */
970
971 /*********************************************************************
972  *              div (MSVCRT.@)
973  * VERSION
974  *      [i386] Windows binary compatible - returns the struct in eax/edx.
975  */
976 #ifdef __i386__
977 unsigned __int64 CDECL MSVCRT_div(int num, int denom)
978 {
979   div_t dt = div(num,denom);
980   return ((unsigned __int64)dt.rem << 32) | (unsigned int)dt.quot;
981 }
982 #else
983 /*********************************************************************
984  *              div (MSVCRT.@)
985  * VERSION
986  *      [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
987  */
988 MSVCRT_div_t CDECL MSVCRT_div(int num, int denom)
989 {
990   div_t dt = div(num,denom);
991   MSVCRT_div_t     ret;
992   ret.quot = dt.quot;
993   ret.rem = dt.rem;
994
995   return ret;
996
997 }
998 #endif /* ifdef __i386__ */
999
1000
1001 /*********************************************************************
1002  *              ldiv (MSVCRT.@)
1003  * VERSION
1004  *      [i386] Windows binary compatible - returns the struct in eax/edx.
1005  */
1006 #ifdef __i386__
1007 unsigned __int64 CDECL MSVCRT_ldiv(long num, long denom)
1008 {
1009   ldiv_t ldt = ldiv(num,denom);
1010   return ((unsigned __int64)ldt.rem << 32) | (unsigned long)ldt.quot;
1011 }
1012 #else
1013 /*********************************************************************
1014  *              ldiv (MSVCRT.@)
1015  * VERSION
1016  *      [!i386] Non-x86 can't run win32 apps so we don't need binary compatibility
1017  */
1018 MSVCRT_ldiv_t CDECL MSVCRT_ldiv(long num, long denom)
1019 {
1020   ldiv_t result = ldiv(num,denom);
1021
1022   MSVCRT_ldiv_t ret;
1023   ret.quot = result.quot;
1024   ret.rem = result.rem;
1025
1026   return ret;
1027 }
1028 #endif /* ifdef __i386__ */
1029
1030 /***********************************************************************
1031  *              _adj_fdiv_m16i (MSVCRT.@)
1032  *
1033  * NOTE
1034  *    I _think_ this function is intended to work around the Pentium
1035  *    fdiv bug.
1036  */
1037 void __stdcall _adj_fdiv_m16i( short arg )
1038 {
1039   TRACE("(): stub\n");
1040 }
1041
1042 /***********************************************************************
1043  *              _adj_fdiv_m32 (MSVCRT.@)
1044  *
1045  * NOTE
1046  *    I _think_ this function is intended to work around the Pentium
1047  *    fdiv bug.
1048  */
1049 void __stdcall _adj_fdiv_m32( unsigned int arg )
1050 {
1051   TRACE("(): stub\n");
1052 }
1053
1054 /***********************************************************************
1055  *              _adj_fdiv_m32i (MSVCRT.@)
1056  *
1057  * NOTE
1058  *    I _think_ this function is intended to work around the Pentium
1059  *    fdiv bug.
1060  */
1061 void __stdcall _adj_fdiv_m32i( int arg )
1062 {
1063   TRACE("(): stub\n");
1064 }
1065
1066 /***********************************************************************
1067  *              _adj_fdiv_m64 (MSVCRT.@)
1068  *
1069  * NOTE
1070  *    I _think_ this function is intended to work around the Pentium
1071  *    fdiv bug.
1072  */
1073 void __stdcall _adj_fdiv_m64( unsigned __int64 arg )
1074 {
1075   TRACE("(): stub\n");
1076 }
1077
1078 /***********************************************************************
1079  *              _adj_fdiv_r (MSVCRT.@)
1080  * FIXME
1081  *    This function is likely to have the wrong number of arguments.
1082  *
1083  * NOTE
1084  *    I _think_ this function is intended to work around the Pentium
1085  *    fdiv bug.
1086  */
1087 void _adj_fdiv_r(void)
1088 {
1089   TRACE("(): stub\n");
1090 }
1091
1092 /***********************************************************************
1093  *              _adj_fdivr_m16i (MSVCRT.@)
1094  *
1095  * NOTE
1096  *    I _think_ this function is intended to work around the Pentium
1097  *    fdiv bug.
1098  */
1099 void __stdcall _adj_fdivr_m16i( short arg )
1100 {
1101   TRACE("(): stub\n");
1102 }
1103
1104 /***********************************************************************
1105  *              _adj_fdivr_m32 (MSVCRT.@)
1106  *
1107  * NOTE
1108  *    I _think_ this function is intended to work around the Pentium
1109  *    fdiv bug.
1110  */
1111 void __stdcall _adj_fdivr_m32( unsigned int arg )
1112 {
1113   TRACE("(): stub\n");
1114 }
1115
1116 /***********************************************************************
1117  *              _adj_fdivr_m32i (MSVCRT.@)
1118  *
1119  * NOTE
1120  *    I _think_ this function is intended to work around the Pentium
1121  *    fdiv bug.
1122  */
1123 void __stdcall _adj_fdivr_m32i( int arg )
1124 {
1125   TRACE("(): stub\n");
1126 }
1127
1128 /***********************************************************************
1129  *              _adj_fdivr_m64 (MSVCRT.@)
1130  *
1131  * NOTE
1132  *    I _think_ this function is intended to work around the Pentium
1133  *    fdiv bug.
1134  */
1135 void __stdcall _adj_fdivr_m64( unsigned __int64 arg )
1136 {
1137   TRACE("(): stub\n");
1138 }
1139
1140 /***********************************************************************
1141  *              _adj_fpatan (MSVCRT.@)
1142  * FIXME
1143  *    This function is likely to have the wrong number of arguments.
1144  *
1145  * NOTE
1146  *    I _think_ this function is intended to work around the Pentium
1147  *    fdiv bug.
1148  */
1149 void _adj_fpatan(void)
1150 {
1151   TRACE("(): stub\n");
1152 }
1153
1154 /***********************************************************************
1155  *              _adj_fprem (MSVCRT.@)
1156  * FIXME
1157  *    This function is likely to have the wrong number of arguments.
1158  *
1159  * NOTE
1160  *    I _think_ this function is intended to work around the Pentium
1161  *    fdiv bug.
1162  */
1163 void _adj_fprem(void)
1164 {
1165   TRACE("(): stub\n");
1166 }
1167
1168 /***********************************************************************
1169  *              _adj_fprem1 (MSVCRT.@)
1170  * FIXME
1171  *    This function is likely to have the wrong number of arguments.
1172  *
1173  * NOTE
1174  *    I _think_ this function is intended to work around the Pentium
1175  *    fdiv bug.
1176  */
1177 void _adj_fprem1(void)
1178 {
1179   TRACE("(): stub\n");
1180 }
1181
1182 /***********************************************************************
1183  *              _adj_fptan (MSVCRT.@)
1184  * FIXME
1185  *    This function is likely to have the wrong number of arguments.
1186  *
1187  * NOTE
1188  *    I _think_ this function is intended to work around the Pentium
1189  *    fdiv bug.
1190  */
1191 void _adj_fptan(void)
1192 {
1193   TRACE("(): stub\n");
1194 }
1195
1196 /***********************************************************************
1197  *              _adjust_fdiv (MSVCRT.@)
1198  * FIXME
1199  *    I _think_ this function should be a variable indicating whether
1200  *    Pentium fdiv bug safe code should be used.
1201  */
1202 void _adjust_fdiv(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 }