History

Overhauled WIN32 math and added missing functions: 1) Cleanup: moved function implementations from win32/include/math.h to win32/include/tcc/tcc_libm.h 2) Added missing math functions: MUSL: asinh(), acosh(), atanh(), scalbn(). My impl: log1p(), expm1(), ilogb(), scalbln(), nexttoward() - now only a few are missing: remquo(), remainder(), fma(), nan(), erf(), erfc(), nearbyint(). 3) Added/defined all missing *f() and *l() math functions. 4) Added functions have short but accurate/fast implementations. 5) Added <tgmath.h> for all platforms. (not too useful, IMO, but is C99 standard).

Tyge Løvset committed 5 years ago

Commit b11144d1 parent 82611f53 files · +486 −323Browse files

Unified · Split

3 files
  1. include/tgmath.h +89 −0
  2. win32/include/math.h +21 −291
  3. win32/include/tcc/tcc_libm.h +376 −32
include/tgmath.h+89−0
@@ -0,0 +1,89 @@
1/*
2 * ISO C Standard: 7.22 Type-generic math <tgmath.h>
3 */
4
5#ifndef _TGMATH_H
6#define _TGMATH_H
7
8#include <math.h>
9
10#ifndef __cplusplus
11#define __tgmath_real(x, F) \
12 _Generic ((x), float: F##f, long double: F##l, default: F)(x)
13#define __tgmath_real_2_1(x, y, F) \
14 _Generic ((x), float: F##f, long double: F##l, default: F)(x, y)
15#define __tgmath_real_2(x, y, F) \
16 _Generic ((x)+(y), float: F##f, long double: F##l, default: F)(x, y)
17#define __tgmath_real_3_2(x, y, z, F) \
18 _Generic ((x)+(y), float: F##f, long double: F##l, default: F)(x, y, z)
19#define __tgmath_real_3(x, y, z, F) \
20 _Generic ((x)+(y)+(z), float: F##f, long double: F##l, default: F)(x, y, z)
21
22/* Functions defined in both <math.h> and <complex.h> (7.22p4) */
23#define acos(z) __tgmath_real(z, acos)
24#define asin(z) __tgmath_real(z, asin)
25#define atan(z) __tgmath_real(z, atan)
26#define acosh(z) __tgmath_real(z, acosh)
27#define asinh(z) __tgmath_real(z, asinh)
28#define atanh(z) __tgmath_real(z, atanh)
29#define cos(z) __tgmath_real(z, cos)
30#define sin(z) __tgmath_real(z, sin)
31#define tan(z) __tgmath_real(z, tan)
32#define cosh(z) __tgmath_real(z, cosh)
33#define sinh(z) __tgmath_real(z, sinh)
34#define tanh(z) __tgmath_real(z, tanh)
35#define exp(z) __tgmath_real(z, exp)
36#define log(z) __tgmath_real(z, log)
37#define pow(z1,z2) __tgmath_real_2(z1, z2, pow)
38#define sqrt(z) __tgmath_real(z, sqrt)
39#define fabs(z) __tgmath_real(z, fabs)
40
41/* Functions defined in <math.h> only (7.22p5) */
42#define atan2(x,y) __tgmath_real_2(x, y, atan2)
43#define cbrt(x) __tgmath_real(x, cbrt)
44#define ceil(x) __tgmath_real(x, ceil)
45#define copysign(x,y) __tgmath_real_2(x, y, copysign)
46#define erf(x) __tgmath_real(x, erf)
47#define erfc(x) __tgmath_real(x, erfc)
48#define exp2(x) __tgmath_real(x, exp2)
49#define expm1(x) __tgmath_real(x, expm1)
50#define fdim(x,y) __tgmath_real_2(x, y, fdim)
51#define floor(x) __tgmath_real(x, floor)
52#define fma(x,y,z) __tgmath_real_3(x, y, z, fma)
53#define fmax(x,y) __tgmath_real_2(x, y, fmax)
54#define fmin(x,y) __tgmath_real_2(x, y, fmin)
55#define fmod(x,y) __tgmath_real_2(x, y, fmod)
56#define frexp(x,y) __tgmath_real_2_1(x, y, frexp)
57#define hypot(x,y) __tgmath_real_2(x, y, hypot)
58#define ilogb(x) __tgmath_real(x, ilogb)
59#define ldexp(x,y) __tgmath_real_2_1(x, y, ldexp)
60#define lgamma(x) __tgmath_real(x, lgamma)
61#define llrint(x) __tgmath_real(x, llrint)
62#define llround(x) __tgmath_real(x, llround)
63#define log10(x) __tgmath_real(x, log10)
64#define log1p(x) __tgmath_real(x, log1p)
65#define log2(x) __tgmath_real(x, log2)
66#define logb(x) __tgmath_real(x, logb)
67#define lrint(x) __tgmath_real(x, lrint)
68#define lround(x) __tgmath_real(x, lround)
69#define nearbyint(x) __tgmath_real(x, nearbyint)
70#define nextafter(x,y) __tgmath_real_2(x, y, nextafter)
71#define nexttoward(x,y) __tgmath_real_2(x, y, nexttoward)
72#define remainder(x,y) __tgmath_real_2(x, y, remainder)
73#define remquo(x,y,z) __tgmath_real_3_2(x, y, z, remquo)
74#define rint(x) __tgmath_real(x, rint)
75#define round(x) __tgmath_real(x, round)
76#define scalbln(x,y) __tgmath_real_2_1(x, y, scalbln)
77#define scalbn(x,y) __tgmath_real_2_1(x, y, scalbn)
78#define tgamma(x) __tgmath_real(x, tgamma)
79#define trunc(x) __tgmath_real(x, trunc)
80
81/* Functions defined in <complex.h> only (7.22p6)
82#define carg(z) __tgmath_cplx_only(z, carg)
83#define cimag(z) __tgmath_cplx_only(z, cimag)
84#define conj(z) __tgmath_cplx_only(z, conj)
85#define cproj(z) __tgmath_cplx_only(z, cproj)
86#define creal(z) __tgmath_cplx_only(z, creal)
87*/
88#endif /* __cplusplus */
89#endif /* _TGMATH_H */
win32/include/math.h+21−291
@@ -197,105 +197,6 @@ extern "C" { · 196 unmodified lines
197 int __cdecl _fpclassf(float _X);197 int __cdecl _fpclassf(float _X);
198#endif198#endif
199 199
200#ifndef __cplusplus
201#define _hypotl(x,y) ((long double)_hypot((double)(x),(double)(y)))
202#define _matherrl _matherr
203 __CRT_INLINE long double _chgsignl(long double _Number) { return _chgsign((double)(_Number)); }
204 __CRT_INLINE long double _copysignl(long double _Number,long double _Sign) { return _copysign((double)(_Number),(double)(_Sign)); }
205 __CRT_INLINE float frexpf(float _X,int *_Y) { return ((float)frexp((double)_X,_Y)); }
206
207#if !defined (__ia64__)
208 __CRT_INLINE float __cdecl fabsf (float x)
209 {
210#ifdef _WIN64
211 *((int *) &x) &= 0x7fffffff;
212 return x;
213#else
214 float res;
215 __asm__ ("fabs;" : "=t" (res) : "0" (x));
216 return res;
217#endif
218 }
219
220 __CRT_INLINE float __cdecl ldexpf (float x, int expn) { return (float) ldexp (x, expn); }
221#endif
222#if defined (_WIN32) && !defined(_WIN64)
223 __CRT_INLINE float acosf(float x) { return (float) acos(x); }
224 __CRT_INLINE float asinf(float x) { return (float) asin(x); }
225 __CRT_INLINE float atanf(float x) { return (float) atan(x); }
226 __CRT_INLINE float atan2f(float x, float y) { return (float) atan2(x, y); }
227 __CRT_INLINE float ceilf(float x) { return (float) ceil(x); }
228 __CRT_INLINE float cosf(float x) { return (float) cos(x); }
229 __CRT_INLINE float coshf(float x) { return (float) cosh(x); }
230 __CRT_INLINE float expf(float x) { return (float) exp(x); }
231 __CRT_INLINE float floorf(float x) { return (float) floor(x); }
232 __CRT_INLINE float fmodf(float x, float y) { return (float) fmod(x, y); }
233 __CRT_INLINE float logf(float x) { return (float) log(x); }
234 __CRT_INLINE float logbf(float x) { return (float) logb(x); }
235 __CRT_INLINE float log10f(float x) { return (float) log10(x); }
236 __CRT_INLINE float modff(float x, float *y) {
237 double di, df = modf(x, &di);
238 *y = (float) di; return (float) df;
239 }
240 __CRT_INLINE float powf(float x, float y) { return (float) pow(x, y); }
241 __CRT_INLINE float sinf(float x) { return (float) sin(x); }
242 __CRT_INLINE float sinhf(float x) { return (float) sinh(x); }
243 __CRT_INLINE float sqrtf(float x) { return (float) sqrt(x); }
244 __CRT_INLINE float tanf(float x) { return (float) tan(x); }
245 __CRT_INLINE float tanhf(float x) { return (float) tanh(x); }
246#endif
247#else
248 // cplusplus
249 __CRT_INLINE long double __cdecl fabsl (long double x)
250 {
251 long double res;
252 __asm__ ("fabs;" : "=t" (res) : "0" (x));
253 return res;
254 }
255 __CRT_INLINE long double modfl(long double _X,long double *_Y) {
256 double _Di,_Df = modf((double)_X,&_Di);
257 *_Y = (long double)_Di;
258 return (_Df);
259 }
260 __CRT_INLINE long double _chgsignl(long double _Number) { return _chgsign(static_cast<double>(_Number)); }
261 __CRT_INLINE long double _copysignl(long double _Number,long double _Sign) { return _copysign(static_cast<double>(_Number),static_cast<double>(_Sign)); }
262 __CRT_INLINE float frexpf(float _X,int *_Y) { return ((float)frexp((double)_X,_Y)); }
263#ifndef __ia64__
264 __CRT_INLINE float __cdecl fabsf (float x)
265 {
266 float res;
267 __asm__ ("fabs;" : "=t" (res) : "0" (x));
268 return res;
269 }
270 __CRT_INLINE float __cdecl ldexpf (float x, int expn) { return (float) ldexp (x, expn); }
271#ifndef __x86_64
272 __CRT_INLINE float acosf(float _X) { return ((float)acos((double)_X)); }
273 __CRT_INLINE float asinf(float _X) { return ((float)asin((double)_X)); }
274 __CRT_INLINE float atanf(float _X) { return ((float)atan((double)_X)); }
275 __CRT_INLINE float atan2f(float _X,float _Y) { return ((float)atan2((double)_X,(double)_Y)); }
276 __CRT_INLINE float ceilf(float _X) { return ((float)ceil((double)_X)); }
277 __CRT_INLINE float cosf(float _X) { return ((float)cos((double)_X)); }
278 __CRT_INLINE float coshf(float _X) { return ((float)cosh((double)_X)); }
279 __CRT_INLINE float expf(float _X) { return ((float)exp((double)_X)); }
280 __CRT_INLINE float floorf(float _X) { return ((float)floor((double)_X)); }
281 __CRT_INLINE float fmodf(float _X,float _Y) { return ((float)fmod((double)_X,(double)_Y)); }
282 __CRT_INLINE float logf(float _X) { return ((float)log((double)_X)); }
283 __CRT_INLINE float log10f(float _X) { return ((float)log10((double)_X)); }
284 __CRT_INLINE float modff(float _X,float *_Y) {
285 double _Di,_Df = modf((double)_X,&_Di);
286 *_Y = (float)_Di;
287 return ((float)_Df);
288 }
289 __CRT_INLINE float powf(float _X,float _Y) { return ((float)pow((double)_X,(double)_Y)); }
290 __CRT_INLINE float sinf(float _X) { return ((float)sin((double)_X)); }
291 __CRT_INLINE float sinhf(float _X) { return ((float)sinh((double)_X)); }
292 __CRT_INLINE float sqrtf(float _X) { return ((float)sqrt((double)_X)); }
293 __CRT_INLINE float tanf(float _X) { return ((float)tan((double)_X)); }
294 __CRT_INLINE float tanhf(float _X) { return ((float)tanh((double)_X)); }
295#endif
296#endif
297#endif
298
299#ifndef NO_OLDNAMES200#ifndef NO_OLDNAMES
300#define matherr _matherr201#define matherr _matherr
301 202
@@ -339,10 +240,13 @@ extern "C" { · 37 unmodified lines
339 extern int __cdecl __fpclassify (double);240 extern int __cdecl __fpclassify (double);
340 extern int __cdecl __fpclassifyl (long double);241 extern int __cdecl __fpclassifyl (long double);
341 242
342/* Implemented at tcc/tcc_libm.h */243/* Implemented at tcc/tcc_libm.h
343#define fpclassify(x) (sizeof (x) == sizeof (float) ? __fpclassifyf (x) \244#define fpclassify(x) (sizeof (x) == sizeof (float) ? __fpclassifyf (x) \
344 : sizeof (x) == sizeof (double) ? __fpclassify (x) \245 : sizeof (x) == sizeof (double) ? __fpclassify (x) \
345 : __fpclassifyl (x))246 : __fpclassifyl (x))
247*/
248#define fpclassify(x) \
249 _Generic(x, float: __fpclassifyf, double: __fpclassify, long double: __fpclassifyl)(x)
346 250
347 /* 7.12.3.2 */251 /* 7.12.3.2 */
348#define isfinite(x) ((fpclassify(x) & FP_NAN) == 0)252#define isfinite(x) ((fpclassify(x) & FP_NAN) == 0)
@@ -364,10 +268,13 @@ extern "C" { · 15 unmodified lines
364 extern int __cdecl __signbit (double);268 extern int __cdecl __signbit (double);
365 extern int __cdecl __signbitl (long double);269 extern int __cdecl __signbitl (long double);
366 270
367/* Implemented at tcc/tcc_libm.h */271/* Implemented at tcc/tcc_libm.h
368#define signbit(x) (sizeof (x) == sizeof (float) ? __signbitf (x) \272#define signbit(x) (sizeof (x) == sizeof (float) ? __signbitf (x) \
369 : sizeof (x) == sizeof (double) ? __signbit (x) \273 : sizeof (x) == sizeof (double) ? __signbit (x) \
370 : __signbitl (x))274 : __signbitl (x))
275*/
276#define signbit(x) \
277 _Generic(x, float: __signbitf, double: __signbit, long double: signbitl)(x)
371 278
372 extern double __cdecl exp2(double);279 extern double __cdecl exp2(double);
373 extern float __cdecl exp2f(float);280 extern float __cdecl exp2f(float);
@@ -390,31 +297,6 @@ extern "C" { · 16 unmodified lines
390 extern double __cdecl logb (double);297 extern double __cdecl logb (double);
391 extern float __cdecl logbf (float);298 extern float __cdecl logbf (float);
392 extern long double __cdecl logbl (long double);299 extern long double __cdecl logbl (long double);
393#ifndef _WIN32
394 __CRT_INLINE double __cdecl logb (double x)
395 {
396 double res;
397 __asm__ ("fxtract\n\t"
398 "fstp %%st" : "=t" (res) : "0" (x));
399 return res;
400 }
401
402 __CRT_INLINE float __cdecl logbf (float x)
403 {
404 float res;
405 __asm__ ("fxtract\n\t"
406 "fstp %%st" : "=t" (res) : "0" (x));
407 return res;
408 }
409
410 __CRT_INLINE long double __cdecl logbl (long double x)
411 {
412 long double res;
413 __asm__ ("fxtract\n\t"
414 "fstp %%st" : "=t" (res) : "0" (x));
415 return res;
416 }
417#endif
418 300
419 extern long double __cdecl modfl (long double, long double*);301 extern long double __cdecl modfl (long double, long double*);
420 302
@@ -433,8 +315,8 @@ extern "C" { · 12 unmodified lines
433 extern float __cdecl cbrtf (float);315 extern float __cdecl cbrtf (float);
434 extern long double __cdecl cbrtl (long double);316 extern long double __cdecl cbrtl (long double);
435 317
436 __CRT_INLINE float __cdecl hypotf (float x, float y)318 extern double __cdecl hypot (double, double);
437 { return (float) hypot (x, y);}319 extern float __cdecl hypotf (float, float);
438 extern long double __cdecl hypotl (long double, long double);320 extern long double __cdecl hypotl (long double, long double);
439 321
440 extern long double __cdecl powl (long double, long double);322 extern long double __cdecl powl (long double, long double);
@@ -490,117 +372,23 @@ extern "C" { · 49 unmodified lines
490 372
491 /* 7.12.9.4 */373 /* 7.12.9.4 */
492 /* round, using fpu control word settings */374 /* round, using fpu control word settings */
493 __CRT_INLINE double __cdecl rint (double x)375 extern double __cdecl rint (double);
494 {376 extern float __cdecl rintf (float);
495 double retval;377 extern long double __cdecl rintl (long double);
496 __asm__ (378
497 "fldl %1\n"379 extern long __cdecl lrint (double);
498 "frndint \n"380 extern long __cdecl lrintf (float);
499 "fstpl %0\n" : "=m" (retval) : "m" (x));381 extern long __cdecl lrintl (long double);
500 return retval;382
501 }383 extern long long __cdecl llrint (double);
502 384 extern long long __cdecl llrintf (float);
503 __CRT_INLINE float __cdecl rintf (float x)385 extern long long __cdecl llrintl (long double);
504 {
505 float retval;
506 __asm__ (
507 "flds %1\n"
508 "frndint \n"
509 "fstps %0\n" : "=m" (retval) : "m" (x));
510 return retval;
511 }
512
513 __CRT_INLINE long double __cdecl rintl (long double x)
514 {
515#ifdef _WIN32
516 // on win32 'long double' is double internally
517 return rint(x);
518#else
519 long double retval;
520 __asm__ (
521 "fldt %1\n"
522 "frndint \n"
523 "fstpt %0\n" : "=m" (retval) : "m" (x));
524 return retval;
525#endif
526 }
527
528 /* 7.12.9.5 */
529 __CRT_INLINE long __cdecl lrint (double x)
530 {
531 long retval;
532 __asm__ __volatile__ \
533 ("fldl %1\n" \
534 "fistpl %0" : "=m" (retval) : "m" (x)); \
535 return retval;
536 }
537
538 __CRT_INLINE long __cdecl lrintf (float x)
539 {
540 long retval;
541 __asm__ __volatile__ \
542 ("flds %1\n" \
543 "fistpl %0" : "=m" (retval) : "m" (x)); \
544 return retval;
545 }
546
547 __CRT_INLINE long __cdecl lrintl (long double x)
548 {
549 long retval;
550 __asm__ __volatile__ \
551 ("fldt %1\n" \
552 "fistpl %0" : "=m" (retval) : "m" (x)); \
553 return retval;
554 }
555
556 __CRT_INLINE long long __cdecl llrint (double x)
557 {
558 long long retval;
559 __asm__ __volatile__ \
560 ("fldl %1\n" \
561 "fistpll %0" : "=m" (retval) : "m" (x)); \
562 return retval;
563 }
564
565 __CRT_INLINE long long __cdecl llrintf (float x)
566 {
567 long long retval;
568 __asm__ __volatile__ \
569 ("flds %1\n" \
570 "fistpll %0" : "=m" (retval) : "m" (x)); \
571 return retval;
572 }
573
574 __CRT_INLINE long long __cdecl llrintl (long double x)
575 {
576 long long retval;
577 __asm__ __volatile__ \
578 ("fldt %1\n" \
579 "fistpll %0" : "=m" (retval) : "m" (x)); \
580 return retval;
581 }
582 386
583 #define FE_TONEAREST 0x0000387 #define FE_TONEAREST 0x0000
584 #define FE_DOWNWARD 0x0400388 #define FE_DOWNWARD 0x0400
585 #define FE_UPWARD 0x0800389 #define FE_UPWARD 0x0800
586 #define FE_TOWARDZERO 0x0c00390 #define FE_TOWARDZERO 0x0c00
587 391
588 __CRT_INLINE double trunc (double _x)
589 {
590 double retval;
591 unsigned short saved_cw;
592 unsigned short tmp_cw;
593 __asm__ ("fnstcw %0;" : "=m" (saved_cw)); /* save FPU control word */
594 tmp_cw = (saved_cw & ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO))
595 | FE_TOWARDZERO;
596 __asm__ ("fldcw %0;" : : "m" (tmp_cw));
597 __asm__ ("fldl %1;"
598 "frndint;"
599 "fstpl %0;" : "=m" (retval) : "m" (_x)); /* round towards zero */
600 __asm__ ("fldcw %0;" : : "m" (saved_cw) ); /* restore saved control word */
601 return retval;
602 }
603
604 /* 7.12.9.6 */392 /* 7.12.9.6 */
605 /* round away from zero, regardless of fpu control word settings */393 /* round away from zero, regardless of fpu control word settings */
606 extern double __cdecl round (double);394 extern double __cdecl round (double);
@@ -685,70 +473,12 @@ extern "C" { · 78 unmodified lines
685 extern long double __cdecl fmal (long double, long double, long double);473 extern long double __cdecl fmal (long double, long double, long double);
686 474
687 475
688#if 0 // gr: duplicate, see below
689 /* 7.12.14 */
690 /*
691 * With these functions, comparisons involving quiet NaNs set the FP
692 * condition code to "unordered". The IEEE floating-point spec
693 * dictates that the result of floating-point comparisons should be
694 * false whenever a NaN is involved, with the exception of the != op,
695 * which always returns true: yes, (NaN != NaN) is true).
696 */
697
698#if __GNUC__ >= 3
699
700#define isgreater(x, y) __builtin_isgreater(x, y)
701#define isgreaterequal(x, y) __builtin_isgreaterequal(x, y)
702#define isless(x, y) __builtin_isless(x, y)
703#define islessequal(x, y) __builtin_islessequal(x, y)
704#define islessgreater(x, y) __builtin_islessgreater(x, y)
705#define isunordered(x, y) __builtin_isunordered(x, y)
706
707#else
708 /* helper */
709 __CRT_INLINE int __cdecl
710 __fp_unordered_compare (long double x, long double y){
711 unsigned short retval;
712 __asm__ ("fucom %%st(1);"
713 "fnstsw;": "=a" (retval) : "t" (x), "u" (y));
714 return retval;
715 }
716
717#define isgreater(x, y) ((__fp_unordered_compare(x, y) \
718 & 0x4500) == 0)
719#define isless(x, y) ((__fp_unordered_compare (y, x) \
720 & 0x4500) == 0)
721#define isgreaterequal(x, y) ((__fp_unordered_compare (x, y) \
722 & FP_INFINITE) == 0)
723#define islessequal(x, y) ((__fp_unordered_compare(y, x) \
724 & FP_INFINITE) == 0)
725#define islessgreater(x, y) ((__fp_unordered_compare(x, y) \
726 & FP_SUBNORMAL) == 0)
727#define isunordered(x, y) ((__fp_unordered_compare(x, y) \
728 & 0x4500) == 0x4500)
729
730#endif
731#endif //0
732
733
734#endif /* __STDC_VERSION__ >= 199901L */476#endif /* __STDC_VERSION__ >= 199901L */
735#endif /* __NO_ISOCEXT */477#endif /* __NO_ISOCEXT */
736 478
737#ifdef __cplusplus479#ifdef __cplusplus
738}480}
739extern "C++" {
740 template<class _Ty> inline _Ty _Pow_int(_Ty _X,int _Y) {
741 unsigned int _N;
742 if(_Y >= 0) _N = (unsigned int)_Y;
743 else _N = (unsigned int)(-_Y);
744 for(_Ty _Z = _Ty(1);;_X *= _X) {
745 if((_N & 1)!=0) _Z *= _X;
746 if((_N >>= 1)==0) return (_Y < 0 ? _Ty(1) / _Z : _Z);
747 }
748 }
749}
750#endif481#endif
751
752#pragma pack(pop)482#pragma pack(pop)
753 483
754/* 7.12.14 */484/* 7.12.14 */
win32/include/tcc/tcc_libm.h+376−32
@@ -122,17 +122,9 @@ __CRT_INLINE long double __cdecl fmaxl (long double x, long double y) { · 121 unmodified lines
122 122
123/* *round* */123/* *round* */
124 124
125#define TCCFP_FORCE_EVAL(x) do { \125#define TCCFP_FORCE_EVAL(x) do { \
126if (sizeof(x) == sizeof(float)) { \126 volatile typeof(x) __x; \
127 volatile float __x; \127 __x = (x); \
128 __x = (x); \
129} else if (sizeof(x) == sizeof(double)) { \
130 volatile double __x; \
131 __x = (x); \
132} else { \
133 volatile long double __x; \
134 __x = (x); \
135} \
136} while(0)128} while(0)
137 129
138__CRT_INLINE double __cdecl round (double x) {130__CRT_INLINE double __cdecl round (double x) {
@@ -150,15 +142,8 @@ __CRT_INLINE double __cdecl round (double x) { · 11 unmodified lines
150 return 0*u.f;142 return 0*u.f;
151 }143 }
152 y = (double)(x + 0x1p52) - 0x1p52 - x;144 y = (double)(x + 0x1p52) - 0x1p52 - x;
153 if (y > 0.5)145 y = y + x - (y > 0.5) + (y <= -0.5); /* branchless */
154 y = y + x - 1;146 return (u.i >> 63) ? -y : y;
155 else if (y <= -0.5)
156 y = y + x + 1;
157 else
158 y = y + x;
159 if (u.i >> 63)
160 y = -y;
161 return y;
162}147}
163 148
164__CRT_INLINE long __cdecl lround (double x) {149__CRT_INLINE long __cdecl lround (double x) {
@@ -194,24 +179,345 @@ __CRT_INLINE long long __cdecl llroundl (long double x) { · 29 unmodified lines
194}179}
195 180
196 181
182/* MUSL asinh, acosh, atanh */
183
184__CRT_INLINE double __cdecl asinh(double x) {
185 union {double f; uint64_t i;} u = {x};
186 unsigned e = u.i >> 52 & 0x7ff, s = u.i >> 63;
187 u.i &= -1ull / 2, x = u.f;
188 if (e >= 0x3ff + 26) x = log(x) + 0.69314718055994530941723212145818;
189 else if (e >= 0x3ff + 1) x = log(2*x + 1 / (sqrt(x*x + 1) + x));
190 else if (e >= 0x3ff - 26) x = log1p(x + x*x / (sqrt(x*x + 1) + 1));
191 else TCCFP_FORCE_EVAL(x + 0x1p120f);
192 return s ? -x : x;
193}
194
195__CRT_INLINE double __cdecl acosh(double x) {
196 union {double f; uint64_t i;} u = {x};
197 unsigned e = u.i >> 52 & 0x7ff;
198 if (e < 0x3ff + 1) return log1p(x - 1 + sqrt((x - 1)*(x - 1) + 2*(x - 1)));
199 if (e < 0x3ff + 26) return log(2*x - 1 / (x + sqrt(x*x - 1)));
200 return log(x) + 0.69314718055994530941723212145818;
201}
202
203__CRT_INLINE double __cdecl atanh(double x) {
204 union {double f; uint64_t i;} u = {x};
205 unsigned e = u.i >> 52 & 0x7ff, s = u.i >> 63;
206 u.i &= -1ull / 2, x = u.f;
207 if (e < 0x3ff - 1) {
208 if (e < 0x3ff - 32) { if (e == 0) TCCFP_FORCE_EVAL((float)x); }
209 else x = 0.5 * log1p(2*x + 2*x*x / (1 - x)); /* |x| < 0.5 */
210 } else x = 0.5 * log1p(2*(x / (1 - x))); /* avoid overflow */
211 return s ? -x : x;
212}
213
214/* MUSL scalbn */
215
216__CRT_INLINE double __cdecl scalbn(double x, int n) {
217 union {double f; uint64_t i;} u;
218 if (n > 1023) {
219 x *= 0x1p1023, n -= 1023;
220 if (n > 1023) {
221 x *= 0x1p1023, n -= 1023;
222 if (n > 1023) n = 1023;
223 }
224 } else if (n < -1022) {
225 x *= 0x1p-1022 * 0x1p53, n += 1022 - 53;
226 if (n < -1022) {
227 x *= 0x1p-1022 * 0x1p53, n += 1022 - 53;
228 if (n < -1022) n = -1022;
229 }
230 }
231 u.i = (0x3ffull + n) << 52;
232 return x * u.f;
233}
234
235
197/*******************************************************************************236/*******************************************************************************
198 End of code based on MUSL237 End of code based on MUSL
199*******************************************************************************/238*******************************************************************************/
200 239
201 240
241/* Following are math functions missing from msvcrt.dll, and not defined
242 * in math.h or above. Functions still remaining:
243 * remquo(), remainder(), fma(), nan(), erf(), erfc(), nearbyint().
244 * In <stdlib.h>: lldiv().
245 */
246
247__CRT_INLINE float __cdecl scalbnf(float x, int n) {
248 return scalbn(x, n);
249}
250__CRT_INLINE long double __cdecl scalbnl(long double x, int n) {
251 return scalbn(x, n);
252}
253
254__CRT_INLINE double __cdecl scalbln(double x, long n) {
255 int m = n > INT_MAX ? INT_MAX : (n < INT_MIN ? INT_MIN : n);
256 return scalbn(x, m);
257}
258__CRT_INLINE float __cdecl scalblnf(float x, long n) {
259 int m = n > INT_MAX ? INT_MAX : (n < INT_MIN ? INT_MIN : n);
260 return scalbn(x, m);
261}
262
263
264__CRT_INLINE float __cdecl ldexpf(float x, int expn) {
265 return scalbn(x, expn);
266}
267__CRT_INLINE long double __cdecl ldexpl(long double x, int expn) {
268 return scalbn(x, expn);
269}
270
271__CRT_INLINE float __cdecl frexpf(float x, int *y) {
272 return frexp(x, y);
273}
274__CRT_INLINE long double __cdecl frexpl (long double x, int* y) {
275 return frexp(x, y);
276}
277
278
279__CRT_INLINE double __cdecl rint(double x) {
280double retval;
281 __asm__ (
282 "fldl %1\n"
283 "frndint \n"
284 "fstpl %0\n" : "=m" (retval) : "m" (x));
285 return retval;
286}
287
288__CRT_INLINE float __cdecl rintf(float x) {
289 float retval;
290 __asm__ (
291 "flds %1\n"
292 "frndint \n"
293 "fstps %0\n" : "=m" (retval) : "m" (x));
294 return retval;
295}
296
297
298/* 7.12.9.5 */
299__CRT_INLINE long __cdecl lrint(double x) {
300 long retval;
301 __asm__ __volatile__
302 ("fldl %1\n"
303 "fistpl %0" : "=m" (retval) : "m" (x));
304 return retval;
305}
306
307__CRT_INLINE long __cdecl lrintf(float x) {
308 long retval;
309 __asm__ __volatile__
310 ("flds %1\n"
311 "fistpl %0" : "=m" (retval) : "m" (x));
312 return retval;
313}
314
315__CRT_INLINE long long __cdecl llrint(double x) {
316long long retval;
317 __asm__ __volatile__
318 ("fldl %1\n"
319 "fistpll %0" : "=m" (retval) : "m" (x));
320 return retval;
321}
322
323__CRT_INLINE long long __cdecl llrintf(float x) {
324 long long retval;
325 __asm__ __volatile__
326 ("flds %1\n"
327 "fistpll %0" : "=m" (retval) : "m" (x));
328 return retval;
329}
330
331/*
332 __CRT_INLINE long double __cdecl rintl (long double x) {
333 long double retval;
334 __asm__ (
335 "fldt %1\n"
336 "frndint \n"
337 "fstpt %0\n" : "=m" (retval) : "m" (x));
338 return retval;
339 }
340 __CRT_INLINE long __cdecl lrintl (long double x) {
341 long retval;
342 __asm__ __volatile__
343 ("fldt %1\n"
344 "fistpl %0" : "=m" (retval) : "m" (x));
345 return retval;
346 }
347 __CRT_INLINE long long __cdecl llrintl (long double x) {
348 long long retval;
349 __asm__ __volatile__
350 ("fldt %1\n"
351 "fistpll %0" : "=m" (retval) : "m" (x));
352 return retval;
353 }
354 __CRT_INLINE float __cdecl fabsf (float x) {
355 float res;
356 __asm__ ("fabs;" : "=t" (res) : "0" (x));
357 return res;
358 }
359*/
360
361__CRT_INLINE long double __cdecl rintl(long double x) {
362 return rint(x);
363}
364__CRT_INLINE long __cdecl lrintl(long double x) {
365 return lrint(x);
366}
367__CRT_INLINE long long __cdecl llrintl(long double x) {
368 return llrint(x);
369}
370
371
372__CRT_INLINE double trunc(double _x) {
373 double retval;
374 unsigned short saved_cw;
375 unsigned short tmp_cw;
376 __asm__ ("fnstcw %0;" : "=m" (saved_cw)); /* save FPU control word */
377 tmp_cw = (saved_cw & ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO))
378 | FE_TOWARDZERO;
379 __asm__ ("fldcw %0;" : : "m" (tmp_cw));
380 __asm__ ("fldl %1;"
381 "frndint;"
382 "fstpl %0;" : "=m" (retval) : "m" (_x)); /* round towards zero */
383 __asm__ ("fldcw %0;" : : "m" (saved_cw) ); /* restore saved control word */
384 return retval;
385}
386
387__CRT_INLINE float __cdecl truncf(float x) {
388 return (float) ((int) x);
389}
390__CRT_INLINE long double __cdecl truncl(long double x) {
391 return trunc(x);
392}
393
394
395__CRT_INLINE long double __cdecl nextafterl(long double x, long double to) {
396 return nextafter(x, to);
397}
398
399__CRT_INLINE double __cdecl nexttoward(double x, long double to) {
400 return nextafter(x, to);
401}
402__CRT_INLINE float __cdecl nexttowardf(float x, long double to) {
403 return nextafterf(x, to);
404}
405__CRT_INLINE long double __cdecl nexttowardl(long double x, long double to) {
406 return nextafter(x, to);
407}
408
409
410__CRT_INLINE float __cdecl fabsf (float x) {
411 union {float f; uint32_t i;} u = {x};
412 u.i &= 0x7fffffff;
413 return u.f;
414}
415__CRT_INLINE long double __cdecl fabsl (long double x) {
416 return fabs(x);
417}
418
419
420#if defined(_WIN32) && !defined(_WIN64) && !defined(__ia64__)
421 __CRT_INLINE float acosf(float x) { return acos(x); }
422 __CRT_INLINE float asinf(float x) { return asin(x); }
423 __CRT_INLINE float atanf(float x) { return atan(x); }
424 __CRT_INLINE float atan2f(float x, float y) { return atan2(x, y); }
425 __CRT_INLINE float ceilf(float x) { return ceil(x); }
426 __CRT_INLINE float cosf(float x) { return cos(x); }
427 __CRT_INLINE float coshf(float x) { return cosh(x); }
428 __CRT_INLINE float expf(float x) { return exp(x); }
429 __CRT_INLINE float floorf(float x) { return floor(x); }
430 __CRT_INLINE float fmodf(float x, float y) { return fmod(x, y); }
431 __CRT_INLINE float logf(float x) { return log(x); }
432 __CRT_INLINE float logbf(float x) { return logb(x); }
433 __CRT_INLINE float log10f(float x) { return log10(x); }
434 __CRT_INLINE float modff(float x, float *y) { double di, df = modf(x, &di); *y = di; return df; }
435 __CRT_INLINE float powf(float x, float y) { return pow(x, y); }
436 __CRT_INLINE float sinf(float x) { return sin(x); }
437 __CRT_INLINE float sinhf(float x) { return sinh(x); }
438 __CRT_INLINE float sqrtf(float x) { return sqrt(x); }
439 __CRT_INLINE float tanf(float x) { return tan(x); }
440 __CRT_INLINE float tanhf(float x) { return tanh(x); }
441#endif
442__CRT_INLINE float __cdecl asinhf(float x) { return asinh(x); }
443__CRT_INLINE float __cdecl acoshf(float x) { return acosh(x); }
444__CRT_INLINE float __cdecl atanhf(float x) { return atanh(x); }
445
446__CRT_INLINE long double __cdecl asinhl(long double x) { return asinh(x); }
447__CRT_INLINE long double __cdecl acoshl(long double x) { return acosh(x); }
448__CRT_INLINE long double __cdecl atanhl(long double x) { return atanh(x); }
449__CRT_INLINE long double __cdecl asinl(long double x) { return asin(x); }
450__CRT_INLINE long double __cdecl acosl(long double x) { return acos(x); }
451__CRT_INLINE long double __cdecl atanl(long double x) { return atan(x); }
452__CRT_INLINE long double __cdecl ceill(long double x) { return ceil(x); }
453__CRT_INLINE long double __cdecl coshl(long double x) { return cosh(x); }
454__CRT_INLINE long double __cdecl cosl(long double x) { return cos(x); }
455__CRT_INLINE long double __cdecl expl(long double x) { return exp(x); }
456__CRT_INLINE long double __cdecl floorl(long double x) { return floor(x); }
457__CRT_INLINE long double __cdecl fmodl(long double x, long double y) { return fmod(x, y); }
458__CRT_INLINE long double __cdecl hypotl(long double x, long double y) { return hypot(x, y); }
459__CRT_INLINE long double __cdecl logl(long double x) { return log(x); }
460__CRT_INLINE long double __cdecl logbl(long double x) { return logb(x); }
461__CRT_INLINE long double __cdecl log10l(long double x) { return log10(x); }
462__CRT_INLINE long double __cdecl modfl(long double x, long double* y) { return modf(x, y); }
463__CRT_INLINE long double __cdecl powl(long double x, long double y) { return pow(x, y); }
464__CRT_INLINE long double __cdecl sinhl(long double x) { return sinh(x); }
465__CRT_INLINE long double __cdecl sinl(long double x) { return sin(x); }
466__CRT_INLINE long double __cdecl sqrtl(long double x) { return sqrt(x); }
467__CRT_INLINE long double __cdecl tanhl(long double x) { return tanh(x); }
468__CRT_INLINE long double __cdecl tanl(long double x) { return tan(x); }
469
470/* Following are accurate, but much shorter implementations than MUSL lib. */
471
472__CRT_INLINE double __cdecl log1p(double x) {
473 double u = 1.0 + x;
474 return u == 1.0 ? x : log(u)*(x / (u - 1.0));
475}
476__CRT_INLINE float __cdecl log1pf(float x) {
477 float u = 1.0f + x;
478 return u == 1.0f ? x : logf(u)*(x / (u - 1.0f));
479}
480__CRT_INLINE long double __cdecl log1pl(long double x) {
481 return log1p(x);
482}
483
484
485__CRT_INLINE double __cdecl expm1(double x) {
486 if (fabs(x) > 0.0024) return exp(x) - 1.0;
487 double u, v = x*x, t = x + 0.5*v + 0.1666666666666666667*(u = v*x);
488 return t + 0.04166666666666666667*u*x;
489}
490__CRT_INLINE float __cdecl expm1f(float x) {
491 if (fabsf(x) > 0.085f) return expf(x) - 1.0f;
492 float u, v = x*x, t = x + 0.5f*v + 0.1666666666666666667f*(u = v*x);
493 return t + 0.04166666666666666667f*u*x;
494}
495__CRT_INLINE long double __cdecl expm1l(long double x) {
496 return expm1(x);
497}
498
499
202__CRT_INLINE double __cdecl cbrt(double x) {500__CRT_INLINE double __cdecl cbrt(double x) {
203 return (1.0 - ((x < 0.0) << 1)) * pow(fabs(x), 1.0 / 3.0);501 return (1 - ((x < 0.0) << 1)) * pow(fabs(x), 1/3.0);
204}502}
205__CRT_INLINE float __cdecl cbrtf(float x) {503__CRT_INLINE float __cdecl cbrtf(float x) {
206 return (1.0f - ((x < 0.0f) << 1)) * (float) pow(fabs(x), 1.0 / 3.0);504 return (1 - ((x < 0.0f) << 1)) * powf(fabsf((float) x), 1/3.0f);
505}
506__CRT_INLINE long double __cdecl cbrtl(long double x) {
507 return (1 - ((x < 0.0) << 1)) * pow(fabs(x), 1/3.0);
207}508}
208 509
510
209__CRT_INLINE double __cdecl log2(double x) {511__CRT_INLINE double __cdecl log2(double x) {
210 return log(x) * 1.4426950408889634073599246810019;512 return log(x) * 1.4426950408889634073599246810019;
211}513}
212__CRT_INLINE float __cdecl log2f(float x) {514__CRT_INLINE float __cdecl log2f(float x) {
213 return logf(x) * 1.4426950408889634073599246810019f;515 return logf(x) * 1.4426950408889634073599246810019f;
214}516}
517__CRT_INLINE long double __cdecl log2l(long double x) {
518 return log(x) * 1.4426950408889634073599246810019;
519}
520
215 521
216__CRT_INLINE double __cdecl exp2(double x) {522__CRT_INLINE double __cdecl exp2(double x) {
217 return exp(x * 0.69314718055994530941723212145818);523 return exp(x * 0.69314718055994530941723212145818);
@@ -219,48 +525,86 @@ __CRT_INLINE double __cdecl exp2(double x) { · 1 unmodified line
219__CRT_INLINE float __cdecl exp2f(float x) {525__CRT_INLINE float __cdecl exp2f(float x) {
220 return expf(x * 0.69314718055994530941723212145818f);526 return expf(x * 0.69314718055994530941723212145818f);
221}527}
528__CRT_INLINE long double __cdecl exp2l(long double x) {
529 return exp(x * 0.69314718055994530941723212145818);
530}
531
532
533__CRT_INLINE int __cdecl ilogb(double x) {
534 return (int) logb(x);
535}
536__CRT_INLINE int __cdecl ilogbf(float x) {
537 return (int) logbf(x);
538}
539__CRT_INLINE int __cdecl ilogbl(long double x) {
540 return (int) logb(x);
541}
542
543
544__CRT_INLINE double __cdecl fdim(double x, double y) {
545 if (isnan(x) || isnan(y)) return NAN;
546 return x > y ? x - y : 0;
547}
548__CRT_INLINE float __cdecl fdimf(float x, float y) {
549 if (isnan(x) || isnan(y)) return NAN;
550 return x > y ? x - y : 0;
551}
552__CRT_INLINE long double __cdecl fdiml(long double x, long double y) {
553 if (isnan(x) || isnan(y)) return NAN;
554 return x > y ? x - y : 0;
555}
556
222 557
223/* tgamma and lgamma: Lanczos approximation558/* tgamma and lgamma: Lanczos approximation
224 * https://rosettacode.org/wiki/Gamma_function559 * https://rosettacode.org/wiki/Gamma_function
225 * https://www.johndcook.com/blog/cpp_gamma560 * https://www.johndcook.com/blog/cpp_gamma
226 */561 */
227__CRT_INLINE double __cdecl lgamma(double x);
228 562
229__CRT_INLINE double __cdecl tgamma(double x) {563__CRT_INLINE double __cdecl tgamma(double x) {
230 double m = 1.0, t = 3.14159265358979323;564 double m = 1.0, t = 3.14159265358979323;
231 if (x == (int)x) {565 if (x > 172.0)
566 return INFINITY;
567 if (x == floor(x)) {
232 for (int k = 2; k < x; ++k) m *= k;568 for (int k = 2; k < x; ++k) m *= k;
233 return x > 0.0 ? m : (x == 0.0 ? INFINITY : NAN);569 return x > 0.0 ? m : (x == 0.0 ? INFINITY : NAN);
234 }570 }
235 if (x < 0.5)571 if (x < 0.5)
236 return t / (sin(t * x) * tgamma(1.0 - x));572 return t / (sin(t*x)*tgamma(1.0 - x));
237 if (x > 12.0)573 if (x > 12.0)
238 return exp(lgamma(x));574 return exp(lgamma(x));
575
239 static const double c[8] = {676.5203681218851, -1259.1392167224028,576 static const double c[8] = {676.5203681218851, -1259.1392167224028,
240 771.32342877765313, -176.61502916214059,577 771.32342877765313, -176.61502916214059,
241 12.507343278686905, -0.13857109526572012,578 12.507343278686905, -0.13857109526572012,
242 9.9843695780195716e-6, 1.5056327351493116e-7};579 9.9843695780195716e-6, 1.5056327351493116e-7};
243 m = 0.99999999999980993, t = x + (8 - 1.5);580 m = 0.99999999999980993, t = x + 6.5; /* x-1+8-.5 */
244 for (int k = 0; k < 8; ++k) m += c[k] / (x + k);581 for (int k = 0; k < 8; ++k) m += c[k] / (x + k);
245 return 2.50662827463100050 * pow(t, x - 0.5) * exp(-t) * m; /* sqrt(2pi) */582 return 2.50662827463100050 * pow(t, x - 0.5)*exp(-t)*m; /* C=sqrt(2pi) */
246}583}
247 584
585
248__CRT_INLINE double __cdecl lgamma(double x) {586__CRT_INLINE double __cdecl lgamma(double x) {
249 if (x < 12.0)587 if (x < 12.0)
250 return x <= 0.0 && x == (int)x ? INFINITY : log(fabs(tgamma(x)));588 return x > 0.0 || x != floor(x) ? log(fabs(tgamma(x))) : INFINITY;
589
251 static const double c[7] = {1.0/12.0, -1.0/360.0, 1.0/1260.0, -1.0/1680.0,590 static const double c[7] = {1.0/12.0, -1.0/360.0, 1.0/1260.0, -1.0/1680.0,
252 1.0/1188.0, -691.0/360360.0, 1.0/156.0};591 1.0/1188.0, -691.0/360360.0, 1.0/156.0};
253 double m = -3617.0/122400.0, z = 1.0 / (x * x);592 double m = -3617.0/122400.0, t = 1.0 / (x*x);
254 for (int k = 6; k >= 0; --k) m = m * z + c[k];593 for (int k = 6; k >= 0; --k) m = m*t + c[k];
255 return (x - 0.5) * log(x) - x + 0.918938533204672742 + m / x; /* log(2pi)/2 */594 return (x - 0.5)*log(x) - x + 0.918938533204672742 + m / x; /* C=log(2pi)/2 */
256}595}
257 596
258__CRT_INLINE float __cdecl tgammaf(float x) {597__CRT_INLINE float __cdecl tgammaf(float x) {
259 return tgamma(x);598 return tgamma(x);
260}599}
261
262__CRT_INLINE float __cdecl lgammaf(float x) {600__CRT_INLINE float __cdecl lgammaf(float x) {
263 return lgamma(x);601 return lgamma(x);
264}602}
603__CRT_INLINE long double __cdecl tgammal(long double x) {
604 return tgamma(x);
605}
606__CRT_INLINE long double __cdecl lgammal(long double x) {
607 return lgamma(x);
608}
265 609
266#endif /* _TCC_LIBM_H_ */610#endif /* _TCC_LIBM_H_ */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t