fesetenv() treated any environment whose encoding is { 0, 0 } as
FE_DFL_ENV. That is also how fegetenv() encodes a real environment with
every exception unmasked, 64-bit x87 precision and round to nearest (for
example x87 control word 0x0340 with MXCSR 0), so saving and restoring
that environment silently reset it instead.
FE_DFL_ENV is defined as &__mingw_fe_dfl_env, so compare the pointer, as
glibc does with its own FE_DFL_ENV sentinel.
So that a copy of *FE_DFL_ENV still selects the default environment
rather than unmasking every exception, make __mingw_fe_dfl_env hold the
real encoding of the startup environment on x86 (x87 control word
0x037f, MXCSR 0x1f80), which is what fegetenv() returns in that state.
Extend t_fesetenv to save and restore such an environment, and to check
that a copy of *FE_DFL_ENV still restores the defaults.
Signed-off-by: Peter Damianov <[email protected]>
---
mingw-w64-crt/misc/fesetenv.c | 13 ++++++++--
mingw-w64-crt/testcases/t_fesetenv.c | 38 ++++++++++++++++++++++++++++
2 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/mingw-w64-crt/misc/fesetenv.c b/mingw-w64-crt/misc/fesetenv.c
index 14a99ce12..37ee530ea 100644
--- a/mingw-w64-crt/misc/fesetenv.c
+++ b/mingw-w64-crt/misc/fesetenv.c
@@ -7,8 +7,14 @@
#include <internal.h>
/* The FE_DFL_ENV macro is required by standard.
- fesetenv will use the environment set at app startup.*/
+ fesetenv recognizes it by address and installs the environment set at
+ app startup. On x86 its contents also encode that environment (x87
+ control word 0x037f, MXCSR 0x1f80), so a copy of it means the same. */
+#if defined(__i386__) || defined(__x86_64__)
+const fenv_t __mingw_fe_dfl_env = { 0x3f3f003f, 0 };
+#else
const fenv_t __mingw_fe_dfl_env = { 0, 0 };
+#endif
/* The C99 standard (7.6.9) allows us to define implementation-specific macros
for
different fp environments */
@@ -40,7 +46,10 @@ int fesetenv(const fenv_t *env)
unsigned int x87_cw, cw, x87_stat, stat;
unsigned int mask = ~0u;
- if (!env->_Fe_ctl && !env->_Fe_stat) {
+ /* Compare the pointer, not the contents: an environment saved by
+ fegetenv with every exception unmasked, 64-bit precision and round
+ to nearest encodes as { 0, 0 }. */
+ if (env == FE_DFL_ENV) {
_fpreset();
#if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
/* _fpreset only reinitializes the x87 unit. Also put MXCSR back to
diff --git a/mingw-w64-crt/testcases/t_fesetenv.c
b/mingw-w64-crt/testcases/t_fesetenv.c
index 3e647a407..6a4148358 100644
--- a/mingw-w64-crt/testcases/t_fesetenv.c
+++ b/mingw-w64-crt/testcases/t_fesetenv.c
@@ -87,5 +87,43 @@ int main()
CHECK(fesetenv(FE_DFL_ENV) == 0);
check_default_environment();
+#ifdef TEST_X86
+ /* An environment with every exception unmasked, 64-bit precision and round
+ to nearest encodes as { 0, 0 }. Saving and restoring it must not reset
+ it to FE_DFL_ENV. The results are read before checking them so that no
+ floating-point operation runs while exceptions are unmasked. */
+ {
+ fenv_t env;
+ unsigned short cw = (get_x87_cw() & ~0x0f3f) | 0x0300;
+ unsigned short got_cw;
+ unsigned int got_mxcsr = 0;
+ int ret1, ret2, ret3;
+
+ set_x87_cw(cw);
+ if (has_sse())
+ set_mxcsr(0);
+ ret1 = fegetenv(&env);
+ ret2 = fesetenv(FE_DFL_ENV);
+ ret3 = fesetenv(&env);
+ got_cw = get_x87_cw();
+ if (has_sse())
+ got_mxcsr = get_mxcsr();
+ fesetenv(FE_DFL_ENV);
+
+ CHECK(ret1 == 0 && ret2 == 0 && ret3 == 0);
+ CHECK(got_cw == cw);
+ CHECK(got_mxcsr == 0);
+ }
+
+ /* A copy of *FE_DFL_ENV selects the startup environment too. */
+ {
+ fenv_t env = *FE_DFL_ENV;
+
+ change_environment();
+ CHECK(fesetenv(&env) == 0);
+ check_default_environment();
+ }
+#endif
+
return failed;
}
--
2.54.0.windows.1
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public