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