From 13a18906ef320546d0aff2bbaa794c896462a640 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tyge=20L=C3=B8vset?= Date: Wed, 25 Nov 2020 14:29:42 +0100 Subject: [PATCH] win32 tcc_libm.h: Implemented overrides for msvcrt.dll's frexp(), ldexp(), and fabs() with speedups 4.5X, 7.3X and 6.3X correspondingly. This is the last commit on this for now. --- win32/include/tcc/tcc_libm.h | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/win32/include/tcc/tcc_libm.h b/win32/include/tcc/tcc_libm.h index 19453320..cb256fe1 100644 --- a/win32/include/tcc/tcc_libm.h +++ b/win32/include/tcc/tcc_libm.h @@ -232,6 +232,23 @@ __CRT_INLINE double __cdecl scalbn(double x, int n) { return x * u.f; } +/* MUSL: Override msvcrt frexp(): 4.5x speedup! */ + +__CRT_INLINE double __cdecl frexp(double x, int *e) { + union {double f; uint64_t i;} u = {.f = x}; + int ee = u.i>>52 & 0x7ff; + if (!ee) { + if (x) x = frexp(x*0x1p64, e), *e -= 64; + else *e = 0; + return x; + } else if (ee == 0x7ff) + return x; + *e = ee - 0x3fe; + u.i &= 0x800fffffffffffffull; + u.i |= 0x3fe0000000000000ull; + return u.f; +} + /* MUSL nan */ __CRT_INLINE double __cdecl nan(const char* s) { @@ -273,7 +290,11 @@ __CRT_INLINE long double __cdecl scalblnl(long double x, long n) { return scalbn(x, n); } +/* Override msvcrt ldexp(): 7.3x speedup! */ +__CRT_INLINE double __cdecl ldexp(double x, int expn) { + return scalbn(x, expn); +} __CRT_INLINE float __cdecl ldexpf(float x, int expn) { return scalbn(x, expn); } @@ -391,14 +412,16 @@ __CRT_INLINE long double __cdecl nexttowardl(long double x, long double to) { return nextafter(x, to); } +/* Override msvcrt fabs(): 6.3x speedup! */ -__CRT_INLINE float __cdecl fabsf (float x) { - union {float f; uint32_t i;} u = {.f = x}; - u.i &= 0x7fffffff; return u.f; +__CRT_INLINE double __cdecl fabs(double x) { + return x < 0 ? -x : x; } -__CRT_INLINE long double __cdecl fabsl (long double x) { - union {double f; uint64_t i;} u = {.f = x}; - u.i &= 0x7fffffffffffffff; return u.f; +__CRT_INLINE float __cdecl fabsf(float x) { + return x < 0 ? -x : x; +} +__CRT_INLINE long double __cdecl fabsl(long double x) { + return x < 0 ? -x : x; }