From d019586378eb1a7adf033f0f5fd994278f903301 Mon Sep 17 00:00:00 2001 From: grischka Date: Mon, 24 Feb 2020 15:48:01 +0100 Subject: [PATCH] win32/include/math.h: rint/trunc: pop fp stack ... in order to avoid fp stack overflow (see test below). #include #include int main() { printf("%f %f %f %f\n", trunc(1.2), rint(1.2), trunc(1.2), rint(1.2)); printf("%f %f %f %f\n", trunc(1.2), rint(1.2), trunc(1.2), rint(1.2)); printf("%f %f %f %f\n", trunc(1.2), rint(1.2), trunc(1.2), rintl(1.2)); } Also in rintl: - 'long double' is not a ten-byte float on windows. --- win32/include/math.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/win32/include/math.h b/win32/include/math.h index 279a69bd..c270c57c 100644 --- a/win32/include/math.h +++ b/win32/include/math.h @@ -496,7 +496,7 @@ extern "C" { __asm__ ( "fldl %1\n" "frndint \n" - "fstl %0\n" : "=m" (retval) : "m" (x)); + "fstpl %0\n" : "=m" (retval) : "m" (x)); return retval; } @@ -506,18 +506,23 @@ extern "C" { __asm__ ( "flds %1\n" "frndint \n" - "fsts %0\n" : "=m" (retval) : "m" (x)); + "fstps %0\n" : "=m" (retval) : "m" (x)); return retval; } __CRT_INLINE long double __cdecl rintl (long double x) { +#ifdef _WIN32 + // on win32 'long double' is double internally + return rint(x); +#else long double retval; __asm__ ( "fldt %1\n" "frndint \n" - "fstt %0\n" : "=m" (retval) : "m" (x)); + "fstpt %0\n" : "=m" (retval) : "m" (x)); return retval; +#endif } /* 7.12.9.5 */ @@ -591,7 +596,7 @@ extern "C" { __asm__ ("fldcw %0;" : : "m" (tmp_cw)); __asm__ ("fldl %1;" "frndint;" - "fstl %0;" : "=m" (retval) : "m" (_x)); /* round towards zero */ + "fstpl %0;" : "=m" (retval) : "m" (_x)); /* round towards zero */ __asm__ ("fldcw %0;" : : "m" (saved_cw) ); /* restore saved control word */ return retval; }