From ff85981b6c79efada5b62b32422ba02b45a7d4fb Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Tue, 14 Jul 2026 09:28:27 +0300 Subject: [PATCH] win32: allow self-host with tcc 0.9.27 - part 2 of 2 Commit cb39bc4 ("Calculate hex floating point with 128 bits instead of 64 bits.", 2025-05-27) started using ldexpl in tccpp.c, however, older tcc like 0.9.27 (2017) don't have it on windows - ldexpl was added to win32/include/math.h (in tcc_libm.h) in commit b11144d (2020). So copy the current ldexpl definition from current tcc_libm.h into tccpp.c - but under a different name. --- tccpp.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tccpp.c b/tccpp.c index 214e9cd4..0752d530 100644 --- a/tccpp.c +++ b/tccpp.c @@ -21,6 +21,49 @@ #define USING_GLOBALS #include "tcc.h" +#if defined(_WIN32) && defined (__TINYC__) + /* allow self-host build with tcc 0.97 - doesn't have ldexpl in tcc_libm.h . + * + * we can't feature-test whether we have ldexpl, and our ./configure also + * can't perform such tests (and it's also not used by win32/build-tcc.bat), + * so define it unconditionally if we're compiling using tcc on windows + * (otherwise the platform's math.h should already have ldexpl). + * + * we could instead include win32/include/math.h (specifically tcc_libm.h), + * but this requires working around include guards and redefinitions, + * so KISS - copy the tcc_libm.h definitions here with a different name. + */ + + #undef ldexpl + #define ldexpl ldexpl_local + + /* MUSL scalbn - (renamed) copied from win32/include/tcc/tcc_libm.h */ + static double scalbn_local(double x, int n) { + union {double f; uint64_t i;} u; + if (n > 1023) { + x *= 0x1p1023, n -= 1023; + if (n > 1023) { + x *= 0x1p1023, n -= 1023; + if (n > 1023) n = 1023; + } + } else if (n < -1022) { + x *= 0x1p-1022 * 0x1p53, n += 1022 - 53; + if (n < -1022) { + x *= 0x1p-1022 * 0x1p53, n += 1022 - 53; + if (n < -1022) n = -1022; + } + } + u.i = (0x3ffull + n) << 52; + return x * u.f; + } + + /* (windows long double is the same type as double - 64bit) */ + /* (renamed) copied from win32/include/tcc/tcc_libm.h */ + static long double ldexpl_local(long double x, int exp) { + return scalbn_local(x, exp); + } +#endif + /* #define to 1 to enable (see parse_pp_string()) */ #define ACCEPT_LF_IN_STRINGS 0