From: Peter Damianov <[email protected]>

Since fesetenv recognizes FE_DFL_ENV by address, __mingw_fe_dfl_env is
meant to hold the encoding of the startup environment, so that a copy
of *FE_DFL_ENV selects it too.  But on x86 the startup environment
depends on which _fpreset is linked: CRT_fp10.o (the default) sets x87
precision to 64 bits, CRT_fp8.o to 53 bits.  With CRT_fp8.o, a copy of
*FE_DFL_ENV selected 64-bit precision.

On x86, define __mingw_fe_dfl_env in CRT_fp10.c and CRT_fp8.c, next to
_fpreset, with the encoding that fegetenv returns after it.

On arm and arm64ec, __mingw_fe_dfl_env was still { 0, 0 }.  That only
worked while fesetenv treated those contents as FE_DFL_ENV; now a copy
of it is decoded as an environment with every exception unmasked.  Give
it the encoding of the startup environment there as well: all
exceptions masked and round to nearest.

Extend t_fesetenv to check that fegetenv at startup returns the
contents of *FE_DFL_ENV, and run the copy test on all targets.  Add
t_fesetenv_fp8 and t_fesetenv_fp10, which run the same test linked
explicitly with CRT_fp8.o and CRT_fp10.o.  As those are only built for
x86, add a HOST_X86 conditional for them.

Signed-off-by: Peter Damianov <[email protected]>
---
 mingw-w64-crt/configure.ac                |  2 ++
 mingw-w64-crt/crt/CRT_fp10.c              |  5 +++++
 mingw-w64-crt/crt/CRT_fp8.c               |  5 +++++
 mingw-w64-crt/misc/fesetenv.c             | 14 ++++++++------
 mingw-w64-crt/testcases/Makefile.am       | 10 ++++++++++
 mingw-w64-crt/testcases/t_fesetenv.c      | 23 +++++++++++++++++++++--
 mingw-w64-crt/testcases/t_fesetenv_fp10.c |  2 ++
 mingw-w64-crt/testcases/t_fesetenv_fp8.c  |  3 +++
 8 files changed, 56 insertions(+), 8 deletions(-)
 create mode 100644 mingw-w64-crt/testcases/t_fesetenv_fp10.c
 create mode 100644 mingw-w64-crt/testcases/t_fesetenv_fp8.c

diff --git a/mingw-w64-crt/configure.ac b/mingw-w64-crt/configure.ac
index dd73c3306..c97db444d 100644
--- a/mingw-w64-crt/configure.ac
+++ b/mingw-w64-crt/configure.ac
@@ -227,6 +227,8 @@ AC_SUBST([LIBARM64SUFFIXDIR],[$libarm64suffx])
 AC_SUBST([LIBARM32SUFFIXDIR],[$libarm32suffx])
 
 AM_CONDITIONAL([ARM64EC],[test $host_cpu = arm64ec -o x$enable_arm64x = xyes])
+AS_CASE([$host_cpu],[x86_64 | i*86],[host_x86=yes],[host_x86=no])
+AM_CONDITIONAL([HOST_X86],[test $host_x86 = yes])
 
 # Checks for features.
 
diff --git a/mingw-w64-crt/crt/CRT_fp10.c b/mingw-w64-crt/crt/CRT_fp10.c
index 9ca9fb93e..94b0ca26f 100644
--- a/mingw-w64-crt/crt/CRT_fp10.c
+++ b/mingw-w64-crt/crt/CRT_fp10.c
@@ -5,8 +5,13 @@
  */
 
 #include <_mingw.h>
+#include <fenv.h>
 #include <float.h>
 
+/* FE_DFL_ENV as fegetenv encodes the state after this _fpreset (x87
+   control word 0x037f, MXCSR 0x1f80).  */
+const fenv_t __mingw_fe_dfl_env = { 0x3f3f003f, 0 };
+
 extern void (* __MINGW_IMP_SYMBOL(_fpreset))(void);
 void _fpreset (void);
 
diff --git a/mingw-w64-crt/crt/CRT_fp8.c b/mingw-w64-crt/crt/CRT_fp8.c
index 8e30beadb..17e5c4fcb 100644
--- a/mingw-w64-crt/crt/CRT_fp8.c
+++ b/mingw-w64-crt/crt/CRT_fp8.c
@@ -5,6 +5,11 @@
  */
 
 #include <_mingw.h>
+#include <fenv.h>
+
+/* FE_DFL_ENV as fegetenv encodes the state after this _fpreset (x87
+   control word 0x027f, MXCSR 0x1f80).  */
+const fenv_t __mingw_fe_dfl_env = { 0x3f3f103f, 0 };
 
 extern void (* __MINGW_IMP_SYMBOL(_fpreset))(void);
 void _fpreset (void);
diff --git a/mingw-w64-crt/misc/fesetenv.c b/mingw-w64-crt/misc/fesetenv.c
index 37ee530ea..e43c3f5ee 100644
--- a/mingw-w64-crt/misc/fesetenv.c
+++ b/mingw-w64-crt/misc/fesetenv.c
@@ -8,12 +8,14 @@
 
 /* The FE_DFL_ENV macro is required by standard.
    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 };
+   app startup.  Its contents encode that environment, so a copy works too.
+   On x86 it is defined next to _fpreset in CRT_fp*.c instead.  */
+#if defined(__arm64ec__)
+/* MXCSR 0x1f80.  */
+const fenv_t __mingw_fe_dfl_env = { 0x3f00003f, 0 };
+#elif !defined(__i386__) && !defined(__x86_64__)
+/* All exceptions masked, round to nearest.  */
+const fenv_t __mingw_fe_dfl_env = { 0x3f, 0 };
 #endif
 
 /* The C99 standard (7.6.9) allows us to define implementation-specific macros 
for
diff --git a/mingw-w64-crt/testcases/Makefile.am 
b/mingw-w64-crt/testcases/Makefile.am
index e1d445eab..46ff37509 100644
--- a/mingw-w64-crt/testcases/Makefile.am
+++ b/mingw-w64-crt/testcases/Makefile.am
@@ -210,6 +210,13 @@ testcase_progs += \
   t_wrongret
 endif
 
+# The CRT_fp*.o objects are only built for x86.
+if HOST_X86
+testcase_progs += \
+  t_fesetenv_fp10 \
+  t_fesetenv_fp8
+endif
+
 # Most of the tests are C programs, but there are some C++ tests as well.
 # For C++ targets, we have to explicitly specify source files so that
 # Automake does not assume .c extension.
@@ -251,6 +258,9 @@ endif
 t_global_ctors_LDFLAGS = $(AM_LDFLAGS) -static
 t_global_ctors_LDADD = @TARGET_LIBGCC_FILE_NAME@ $(LDADD)
 
+t_fesetenv_fp10_LDADD = -l:CRT_fp10.o $(LDADD)
+t_fesetenv_fp8_LDADD = -l:CRT_fp8.o $(LDADD)
+
 # Compile t_intrinc in C99 mode
 t_intrinc_CFLAGS = -std=c99 $(AM_CFLAGS)
 
diff --git a/mingw-w64-crt/testcases/t_fesetenv.c 
b/mingw-w64-crt/testcases/t_fesetenv.c
index 6a4148358..e374e7b62 100644
--- a/mingw-w64-crt/testcases/t_fesetenv.c
+++ b/mingw-w64-crt/testcases/t_fesetenv.c
@@ -1,6 +1,7 @@
 #define _GNU_SOURCE /* for feenableexcept and fegetexcept */
 #include <fenv.h>
 #include <stdio.h>
+#include <string.h>
 
 /* mingw-w64 headers */
 #include "libtest.h"
@@ -8,6 +9,11 @@
 #if defined(__i386__) || (defined(__x86_64__) && !defined(__arm64ec__))
 #define TEST_X86 1
 
+/* x87 control word set by _fpreset; t_fesetenv_fp*.c override it.  */
+#ifndef EXPECT_X87_CW
+#define EXPECT_X87_CW 0x037f
+#endif
+
 #if defined(__i386__) && !defined(__SSE__)
 int __mingw_has_sse(void);
 #define has_sse() __mingw_has_sse()
@@ -71,7 +77,7 @@ static void check_default_environment(void)
   CHECK(fegetexcept() == 0);
   CHECK(fetestexcept(FE_ALL_EXCEPT) == 0);
 #ifdef TEST_X86
-  CHECK(get_x87_cw() == 0x037f);
+  CHECK(get_x87_cw() == EXPECT_X87_CW);
   if (has_sse())
     CHECK(get_mxcsr() == 0x1f80);
 #endif
@@ -81,6 +87,19 @@ int main()
 {
   mingw_test_init();
 
+  /* fegetenv at startup matches *FE_DFL_ENV. */
+  {
+    fenv_t env;
+
+    CHECK(fegetenv(&env) == 0);
+    CHECK(memcmp(&env, FE_DFL_ENV, sizeof(env)) == 0);
+    if (memcmp(&env, FE_DFL_ENV, sizeof(env)) != 0)
+      printf("startup environment { %#lx, %#lx }, *FE_DFL_ENV { %#lx, %#lx 
}\n",
+             (unsigned long) env._Fe_ctl, (unsigned long) env._Fe_stat,
+             (unsigned long) FE_DFL_ENV->_Fe_ctl,
+             (unsigned long) FE_DFL_ENV->_Fe_stat);
+  }
+
   /* fesetenv(FE_DFL_ENV) restores the startup environment of both the x87
      and the SSE unit. */
   change_environment();
@@ -114,6 +133,7 @@ int main()
     CHECK(got_cw == cw);
     CHECK(got_mxcsr == 0);
   }
+#endif
 
   /* A copy of *FE_DFL_ENV selects the startup environment too. */
   {
@@ -123,7 +143,6 @@ int main()
     CHECK(fesetenv(&env) == 0);
     check_default_environment();
   }
-#endif
 
   return failed;
 }
diff --git a/mingw-w64-crt/testcases/t_fesetenv_fp10.c 
b/mingw-w64-crt/testcases/t_fesetenv_fp10.c
new file mode 100644
index 000000000..a7c1bd0f3
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_fesetenv_fp10.c
@@ -0,0 +1,2 @@
+/* t_fesetenv linked explicitly with CRT_fp10.o (64-bit x87 precision).  */
+#include "t_fesetenv.c"
diff --git a/mingw-w64-crt/testcases/t_fesetenv_fp8.c 
b/mingw-w64-crt/testcases/t_fesetenv_fp8.c
new file mode 100644
index 000000000..3f7c44b2e
--- /dev/null
+++ b/mingw-w64-crt/testcases/t_fesetenv_fp8.c
@@ -0,0 +1,3 @@
+/* t_fesetenv linked with CRT_fp8.o (53-bit x87 precision).  */
+#define EXPECT_X87_CW 0x027f
+#include "t_fesetenv.c"
-- 
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