fesetenv(FE_DFL_ENV) only called _fpreset(), which reinitializes the x87
unit and leaves MXCSR untouched.  SSE exception masks, rounding mode,
flush-to-zero, denormals-are-zero and status flags all survived the
reset.  For example, after unmasking SSE exceptions and then calling
fesetenv(FE_DFL_ENV), dividing by zero on x86_64 still terminates the
process with STATUS_FLOAT_DIVIDE_BY_ZERO.

Also set MXCSR back to its startup value, 0x1f80, when SSE is available.
This matches glibc, whose fesetenv(FE_DFL_ENV) masks all SSE exceptions,
clears the SSE flags, selects round to nearest and clears FZ and DAZ.

On aarch64, msvcrt.dll's _fpreset() leaves the exception flags in FPSR
set (UCRT's clears them), so clear the status flags explicitly on ARM.

Add a t_fesetenv test that moves the rounding mode, exception masks,
exception flags, x87 precision and MXCSR FTZ/DAZ away from their
defaults and checks that fesetenv(FE_DFL_ENV) restores all of them.

Signed-off-by: Peter Damianov <[email protected]>
---
 mingw-w64-crt/misc/fesetenv.c        | 15 +++++
 mingw-w64-crt/testcases/Makefile.am  |  1 +
 mingw-w64-crt/testcases/t_fesetenv.c | 91 ++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+)
 create mode 100644 mingw-w64-crt/testcases/t_fesetenv.c

diff --git a/mingw-w64-crt/misc/fesetenv.c b/mingw-w64-crt/misc/fesetenv.c
index 432192763..14a99ce12 100644
--- a/mingw-w64-crt/misc/fesetenv.c
+++ b/mingw-w64-crt/misc/fesetenv.c
@@ -42,6 +42,21 @@ int fesetenv(const fenv_t *env)
 
     if (!env->_Fe_ctl && !env->_Fe_stat) {
         _fpreset();
+#if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
+        /* _fpreset only reinitializes the x87 unit.  Also put MXCSR back to
+           its startup value, 0x1f80: all exceptions masked, round to nearest,
+           no flush to zero or denormals are zero, and no flags set.  */
+        if (__mingw_has_sse()) {
+            cw = _MCW_EM | _RC_NEAR | _DN_SAVE;
+            stat = 0;
+            __mingw_setfp_sse(&cw, mask, &stat, ~0);
+        }
+#elif defined(__arm__) || (defined(__aarch64__) && !defined(__arm64ec__))
+        /* msvcrt.dll's _fpreset on aarch64 leaves the exception flags in
+           FPSR set, so clear the status flags explicitly.  */
+        stat = 0;
+        __mingw_setfp(NULL, 0, &stat, ~0);
+#endif
         return 0;
     }
 
diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index 41ce4677b..e1d445eab 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -106,6 +106,7 @@ testcase_progs = \
   t_exp_denormal \
   t_expm1 \
   t_fctptr \
+  t_fesetenv \
   t_findfirst \
   t_float  \
   t_fmod \
diff --git a/mingw-w64-crt/testcases/t_fesetenv.c 
b/mingw-w64-crt/testcases/t_fesetenv.c
new file mode 100644
index 000000000..3e647a407
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_fesetenv.c
@@ -0,0 +1,91 @@
+#define _GNU_SOURCE /* for feenableexcept and fegetexcept */
+#include <fenv.h>
+#include <stdio.h>
+
+/* mingw-w64 headers */
+#include "libtest.h"
+
+#if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
+#define TEST_X86 1
+
+#if defined(__i386__) && !defined(__SSE__)
+int __mingw_has_sse(void);
+#define has_sse() __mingw_has_sse()
+#else
+#define has_sse() 1
+#endif
+
+static unsigned short get_x87_cw(void)
+{
+  unsigned short cw;
+  __asm__ __volatile__ ("fnstcw %0" : "=m" (cw));
+  return cw;
+}
+
+static void set_x87_cw(unsigned short cw)
+{
+  __asm__ __volatile__ ("fldcw %0" : : "m" (cw));
+}
+
+static unsigned int get_mxcsr(void)
+{
+  unsigned int mxcsr;
+  __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
+  return mxcsr;
+}
+
+static void set_mxcsr(unsigned int mxcsr)
+{
+  __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
+}
+#endif
+
+static int failed;
+
+#define CHECK(cond) do { \
+    if (!(cond)) { \
+      failed = 1; \
+      printf("%s:%d: check failed: %s\n", __FILE__, __LINE__, #cond); \
+    } \
+  } while (0)
+
+/* Move every part of the environment away from its default. */
+static void change_environment(void)
+{
+  CHECK(fesetround(FE_TOWARDZERO) == 0);
+  CHECK(feenableexcept(FE_DIVBYZERO | FE_INVALID) != -1);
+  CHECK(feraiseexcept(FE_INEXACT) == 0);
+#ifdef TEST_X86
+  /* 24-bit x87 precision. */
+  set_x87_cw(get_x87_cw() & ~0x0300);
+  /* SSE flush-to-zero, denormals-are-zero and a raised overflow flag. */
+  if (has_sse())
+    set_mxcsr(get_mxcsr() | 0x8040 | 0x0008);
+#endif
+}
+
+/* Check that the environment is the one set at program startup. */
+static void check_default_environment(void)
+{
+  CHECK(fegetround() == FE_TONEAREST);
+  CHECK(fegetexcept() == 0);
+  CHECK(fetestexcept(FE_ALL_EXCEPT) == 0);
+#ifdef TEST_X86
+  CHECK(get_x87_cw() == 0x037f);
+  if (has_sse())
+    CHECK(get_mxcsr() == 0x1f80);
+#endif
+}
+
+int main()
+{
+  mingw_test_init();
+
+  /* fesetenv(FE_DFL_ENV) restores the startup environment of both the x87
+     and the SSE unit. */
+  change_environment();
+  CHECK(fesetenv(FE_DFL_ENV) == 0);
+  check_default_environment();
+
+  return failed;
+}
-- 
2.54.0.windows.1



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to