From 9d33388b290d12676246eba4f03b28dbba342402 Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Sat, 7 Nov 2015 22:24:31 +0200 Subject: [PATCH] win: libm: add implementation for round/fmin/fmax and variants round and fmin/fmax are relatively commonly used functions but were not implemented anywhere in the tcc Windows distribution package. Newer mingw(64) math.h stil doesn't include these implementations. Add C implementations for these functions and place it as inline functions at win32/include/tcc/tcc_libm.h - which is already included from math.h . The code is mostly taken from musl-libc rs-1.0 (MIT) [*], musl-libc: http://git.musl-libc.org/cgit/musl/tree/src/math?h=rs-1.0 license: http://git.musl-libc.org/cgit/musl/tree/COPYRIGHT?h=rs-1.0 Potential enhancements: - Check how many useful libm implementations are still missing and consider adding them (some of them already work via the MS runtime). - Consider putting libm implementations in an actual libm.a file, or add a dummy one such that build processes which try to link with libm will not fail. --- win32/include/tcc/tcc_libm.h | 113 +++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/win32/include/tcc/tcc_libm.h b/win32/include/tcc/tcc_libm.h index 096b01b..c9a6ad1 100644 --- a/win32/include/tcc/tcc_libm.h +++ b/win32/include/tcc/tcc_libm.h @@ -65,6 +65,7 @@ __CRT_INLINE int __cdecl __fpclassifyl (long double x) { return __fpclassify(x); } + /* signbit */ __CRT_INLINE int __cdecl __signbit (double x) { @@ -81,6 +82,118 @@ __CRT_INLINE int __cdecl __signbitl (long double x) { return __signbit(x); } + +/* fmin*, fmax* */ + +#define TCCFP_FMIN_EVAL (isnan(x) ? y : \ + isnan(y) ? x : \ + (signbit(x) != signbit(y)) ? (signbit(x) ? x : y) : \ + x < y ? x : y) + +__CRT_INLINE double __cdecl fmin (double x, double y) { + return TCCFP_FMIN_EVAL; +} + +__CRT_INLINE float __cdecl fminf (float x, float y) { + return TCCFP_FMIN_EVAL; +} + +__CRT_INLINE long double __cdecl fminl (long double x, long double y) { + return TCCFP_FMIN_EVAL; +} + +#define TCCFP_FMAX_EVAL (isnan(x) ? y : \ + isnan(y) ? x : \ + (signbit(x) != signbit(y)) ? (signbit(x) ? y : x) : \ + x < y ? y : x) + +__CRT_INLINE double __cdecl fmax (double x, double y) { + return TCCFP_FMAX_EVAL; +} + +__CRT_INLINE float __cdecl fmaxf (float x, float y) { + return TCCFP_FMAX_EVAL; +} + +__CRT_INLINE long double __cdecl fmaxl (long double x, long double y) { + return TCCFP_FMAX_EVAL; +} + + +/* *round* */ + +#define TCCFP_FORCE_EVAL(x) do { \ +if (sizeof(x) == sizeof(float)) { \ + volatile float __x; \ + __x = (x); \ +} else if (sizeof(x) == sizeof(double)) { \ + volatile double __x; \ + __x = (x); \ +} else { \ + volatile long double __x; \ + __x = (x); \ +} \ +} while(0) + +__CRT_INLINE double __cdecl round (double x) { + union {double f; uint64_t i;} u = {x}; + int e = u.i >> 52 & 0x7ff; + double y; + + if (e >= 0x3ff+52) + return x; + if (u.i >> 63) + x = -x; + if (e < 0x3ff-1) { + /* raise inexact if x!=0 */ + TCCFP_FORCE_EVAL(x + 0x1p52); + return 0*u.f; + } + y = (double)(x + 0x1p52) - 0x1p52 - x; + if (y > 0.5) + y = y + x - 1; + else if (y <= -0.5) + y = y + x + 1; + else + y = y + x; + if (u.i >> 63) + y = -y; + return y; +} + +__CRT_INLINE long __cdecl lround (double x) { + return round(x); +} + +__CRT_INLINE long long __cdecl llround (double x) { + return round(x); +} + +__CRT_INLINE float __cdecl roundf (float x) { + return round(x); +} + +__CRT_INLINE long __cdecl lroundf (float x) { + return round(x); +} + +__CRT_INLINE long long __cdecl llroundf (float x) { + return round(x); +} + +__CRT_INLINE long double __cdecl roundl (long double x) { + return round(x); +} + +__CRT_INLINE long __cdecl lroundl (long double x) { + return round(x); +} + +__CRT_INLINE long long __cdecl llroundl (long double x) { + return round(x); +} + + /******************************************************************************* End of code based on MUSL *******************************************************************************/