reactos/sdk/lib/crt/math/i386/ldexp.c
Colin Finck bd0a28d1e4 Fix the MSVC version of ldexp filling up the FPU stack and bailing out with #IND after a few calls. Additionally, add it back to libcntpr.
MSVC was previously given a "result" variable to copy the fscale result from st(0). This led to another "fld" FPU stack push at the very end without popping the source value from the FPU stack.
Moreover, this copy isn't even needed: A simple "fstp st(1)" at the end pops an element from the FPU stack while effectively storing the result in st(0), the register used for returning a double value.
This problem didn't affect GCC, as it is only given the "fscale" instruction and does all necessary stack operations itself.

However, looking into the CRT sources, I found many other i386 implementations with inline assembly suffering from the same problem.
Fortunately, they have been replaced by pure assembly implementations a while ago, so it's time to finally remove them.
ldexp would have also been a candidate for a pure assembly implementation, but the required check for NaN and setting errno (verified on Win2003) already outweighs the benefits.
And we cannot just do a NaN check with FUCOMI as this is an i686/pentiumpro instruction while we're still targeting i586/pentium.

I'm also using this opportunity to clean up the ldexp.c header and only put in the remaining contributors as returned by "git blame".

Thanks to NightWolve1975 for reporting the problem! (https://twitter.com/nightwolve1975/status/1099042477531643912)
2019-03-03 16:11:27 +01:00

47 lines
1.2 KiB
C

/*
* PROJECT: ReactOS CRT
* LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
* PURPOSE: Implements the ldexp CRT function for IA-32 with Windows-compatible error codes.
* COPYRIGHT: Copyright 2010 Timo Kreuzer (timo.kreuzer@reactos.org)
* Copyright 2011 Pierre Schweitzer (pierre@reactos.org)
* Copyright 2019 Colin Finck (colin@reactos.org)
*/
#include <precomp.h>
double ldexp (double value, int exp)
{
#ifdef __GNUC__
register double result;
#endif
/* Check for value correctness
* and set errno if required
*/
if (_isnan(value))
{
errno = EDOM;
}
#ifdef __GNUC__
asm ("fscale"
: "=t" (result)
: "0" (value), "u" ((double)exp)
: "1");
return result;
#else /* !__GNUC__ */
__asm
{
fild exp
fld value
fscale
fstp st(1)
}
/* "fstp st(1)" has copied st(0) to st(1), then popped the FPU stack,
* so that the value is again in st(0) now. Effectively, we have reduced
* the FPU stack by one element while preserving st(0).
* st(0) is also the register used for returning a double value. */
#endif /* !__GNUC__ */
}