These came out of trying to keep a 32-bit x87 build of a physics engine
bit-identical to its 64-bit build.  That needs the x87 precision set to
24 bits on every thread, and every thread started through winpthreads
was reset to 64-bit precision.

Patch 1 makes pthread_create() pass the creating thread's floating-point
environment to the new thread, as POSIX requires ("The floating-point
environment shall be inherited from the creating thread").  It replaces
the _fpreset() call added in 295fafcf.  The case that commit fixed
still works: the main thread is at 0x037f after CRT startup, so its
threads inherit that rather than the 53-bit Windows default.  The
environment is installed in pthread_create_wrapper because, as 295fafcf
found, a TLS callback can't do it on x64.  I checked that on Windows 11:
a control word set in a DLL_THREAD_ATTACH callback survives to the
thread procedure for 32-bit processes under WOW64, but is reset to
0x027f/0x1f80 for x64 processes.

Patches 2 and 3 fix fesetenv() bugs I found while testing patch 1:

 - fesetenv(FE_DFL_ENV) relied on _fpreset() to reset everything, but
   on x86 that only resets the x87 unit, so SSE masks, rounding, FTZ/DAZ
   and flags survived it.  With SSE exceptions unmasked, a division by
   zero after fesetenv(FE_DFL_ENV) still kills an x86_64 process.  The
   result now matches glibc: x87 control word 0x037f and MXCSR 0x1f80,
   with FZ and DAZ cleared.  The new test also showed that on aarch64,
   msvcrt.dll's _fpreset() leaves the FPSR exception flags set (UCRT's
   clears them), so patch 2 clears the status flags on ARM as well.

 - FE_DFL_ENV was recognized by content ({ 0, 0 }), which is also what
   fegetenv() returns when every exception is unmasked at 64-bit
   precision with round to nearest.  Such an environment couldn't be
   restored, and patch 1 would fail to pass it to a new thread.  Patch 3
   compares the pointer, as glibc does with its FE_DFL_ENV sentinel.  On
   x86 it also stores the real default encoding in __mingw_fe_dfl_env
   (the same value as __mingw_fe_pc64_env), so a copy of *FE_DFL_ENV
   still means "default".

Possible behaviour changes:

 - A thread now inherits non-default settings from its creator.  That
   is the intent, and matches POSIX and glibc.
 - Code that zero-fills an fenv_t and passes it to fesetenv() expecting
   the default environment now gets every exception unmasked.  That was
   never valid C, but it did work before.

Peter Damianov (3):
  winpthreads: Inherit the floating-point environment in new threads
  crt: fesetenv: Reset MXCSR and ARM status flags for FE_DFL_ENV
  crt: fesetenv: Recognize FE_DFL_ENV by address

 mingw-w64-crt/misc/fesetenv.c                 |  28 ++-
 mingw-w64-crt/testcases/Makefile.am           |   1 +
 mingw-w64-crt/testcases/t_fesetenv.c          | 129 ++++++++++++
 mingw-w64-libraries/winpthreads/src/thread.c  |  11 +-
 mingw-w64-libraries/winpthreads/src/thread.h  |   2 +
 .../winpthreads/tests/Makefile.am             |   1 +
 .../tests/pthread_create/fenv-inherit.c       | 193 ++++++++++++++++++
 7 files changed, 360 insertions(+), 5 deletions(-)
 create mode 100644 mingw-w64-crt/testcases/t_fesetenv.c
 create mode 100644 
mingw-w64-libraries/winpthreads/tests/pthread_create/fenv-inherit.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