POSIX requires a thread created by pthread_create() to inherit the floating-point environment of the creating thread. New threads instead called _fpreset(), which always resets the x87 control word to 0x037F and leaves MXCSR at the Windows default, discarding whatever the creating thread had set (rounding mode, exception masks, x87 precision, FTZ/DAZ).
Save the environment with fegetenv() in pthread_create() and install it with fesetenv() at the start of pthread_create_wrapper(). This still fixes what the _fpreset() call was added for in 295fafcf: the main thread runs at 64-bit precision after CRT startup, so its threads now inherit that rather than the 53-bit Windows default, and long double keeps working. It also carries over settings such as -mpc32/-mpc64 or the FTZ/DAZ bits set for -ffast-math, which were previously lost in every pthread. As 295fafcf notes, this cannot be done from a TLS callback on x64, because the thread's initial context, which includes the FPU state, is loaded after the callbacks run. pthread_create_wrapper runs as the thread procedure, after that point. Add a pthread_create/fenv-inherit test that checks the rounding mode, enabled exceptions, raised flags, and on x86 the x87 precision and the MXCSR FTZ/DAZ bits are inherited by a thread and by its child. Signed-off-by: Peter Damianov <[email protected]> --- 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 ++++++++++++++++++ 4 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 mingw-w64-libraries/winpthreads/tests/pthread_create/fenv-inherit.c diff --git a/mingw-w64-libraries/winpthreads/src/thread.c b/mingw-w64-libraries/winpthreads/src/thread.c index fa99b3055..29b8af66a 100644 --- a/mingw-w64-libraries/winpthreads/src/thread.c +++ b/mingw-w64-libraries/winpthreads/src/thread.c @@ -1557,8 +1557,6 @@ pthread_setcanceltype (int type, int *oldtype) return 0; } -void _fpreset (void); - #if defined(__i386__) /* Align ESP on 16-byte boundaries. */ __attribute__((force_align_arg_pointer)) @@ -1569,7 +1567,12 @@ pthread_create_wrapper (void *args) unsigned rslt = 0; struct _pthread_v *tv = (struct _pthread_v *)args; - _fpreset(); + /* POSIX requires the floating-point environment to be inherited from the + creating thread. Windows starts every thread with its own defaults + (x87 precision set to 53 bits, which breaks long double), and a TLS + callback cannot change that on x64 because the initial context is + loaded after the callbacks run, so set it here. */ + fesetenv (&tv->fenv); pthread_mutex_lock (&mtx_pthr_locked); pthread_mutex_lock (&tv->p_clock); @@ -1686,6 +1689,8 @@ pthread_create (pthread_t *th, const pthread_attr_t *attr, void *(* func)(void * tv->sched.sched_priority = attr->param.sched_priority; } + fegetenv (&tv->fenv); + /* Make sure tv->h has value of INVALID_HANDLE_VALUE */ _ReadWriteBarrier(); diff --git a/mingw-w64-libraries/winpthreads/src/thread.h b/mingw-w64-libraries/winpthreads/src/thread.h index 90f9cd39a..a53eca1ca 100644 --- a/mingw-w64-libraries/winpthreads/src/thread.h +++ b/mingw-w64-libraries/winpthreads/src/thread.h @@ -23,6 +23,7 @@ #ifndef WIN_PTHREAD_H #define WIN_PTHREAD_H +#include <fenv.h> #include <setjmp.h> /* internal header files */ #include "rwlock.h" @@ -57,6 +58,7 @@ struct _pthread_v int sched_pol; int ended; struct sched_param sched; + fenv_t fenv; /* Floating-point environment of the creating thread. */ jmp_buf jb; struct _pthread_v *next; pthread_t x; /* Internal posix handle. */ diff --git a/mingw-w64-libraries/winpthreads/tests/Makefile.am b/mingw-w64-libraries/winpthreads/tests/Makefile.am index e1934a68c..24d6395e4 100644 --- a/mingw-w64-libraries/winpthreads/tests/Makefile.am +++ b/mingw-w64-libraries/winpthreads/tests/Makefile.am @@ -101,6 +101,7 @@ check_PROGRAMS = \ pthread_create/create1 \ pthread_create/create2 \ pthread_create/create3 \ + pthread_create/fenv-inherit \ pthread_exit/exit2 \ pthread_exit/exit3 \ pthread_exit/exit4 \ diff --git a/mingw-w64-libraries/winpthreads/tests/pthread_create/fenv-inherit.c b/mingw-w64-libraries/winpthreads/tests/pthread_create/fenv-inherit.c new file mode 100644 index 000000000..d66827982 --- /dev/null +++ b/mingw-w64-libraries/winpthreads/tests/pthread_create/fenv-inherit.c @@ -0,0 +1,193 @@ +/* + Copyright (c) 2026 mingw-w64 project + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#define _GNU_SOURCE /* for feenableexcept, fedisableexcept and fegetexcept */ +#include <fenv.h> +#include <string.h> + +#include "test.h" + +/** + * Test Summary: + * + * POSIX requires a thread created by `pthread_create` to inherit the + * floating-point environment of the creating thread. Windows starts every + * thread with its own defaults, so check that the rounding mode, enabled + * exceptions and raised exception flags reach the new thread, and on x86 + * also the x87 precision control and the MXCSR flush-to-zero and + * denormals-are-zero bits. + */ + +#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 + +typedef struct { + int round; + int enabled; + int raised; +#ifdef TEST_X86 + unsigned short x87_cw; + unsigned int mxcsr; +#endif +} fp_state; + +static void get_state (fp_state *s) { + s->round = fegetround (); + s->enabled = fegetexcept (); + s->raised = fetestexcept (FE_ALL_EXCEPT); +#ifdef TEST_X86 + s->x87_cw = get_x87_cw (); + s->mxcsr = has_sse () ? get_mxcsr () : 0; +#endif +} + +static void *child (void *arg) { + get_state ((fp_state *) arg); + return NULL; +} + +static void *parent (void *arg) { + pthread_t t; + assert (pthread_create (&t, NULL, child, arg) == 0); + assert (pthread_join (t, NULL) == 0); + return NULL; +} + +/* Check that a thread started from the current state, and a thread started by + that thread, both see the current state. */ +static void check_inherited (void) { + fp_state expected, got; + pthread_t t; + + get_state (&expected); + + memset (&got, 0xff, sizeof (got)); + assert (pthread_create (&t, NULL, child, &got) == 0); + assert (pthread_join (t, NULL) == 0); + assert (got.round == expected.round); + assert (got.enabled == expected.enabled); + assert (got.raised == expected.raised); +#ifdef TEST_X86 + assert (got.x87_cw == expected.x87_cw); + assert (got.mxcsr == expected.mxcsr); +#endif + + memset (&got, 0xff, sizeof (got)); + assert (pthread_create (&t, NULL, parent, &got) == 0); + assert (pthread_join (t, NULL) == 0); + assert (got.round == expected.round); + assert (got.enabled == expected.enabled); + assert (got.raised == expected.raised); +#ifdef TEST_X86 + assert (got.x87_cw == expected.x87_cw); + assert (got.mxcsr == expected.mxcsr); +#endif +} + +int main (void) { + fenv_t saved; + static const int rounding[] = { FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO }; + size_t i; + + assert (fegetenv (&saved) == 0); + + /** + * The default environment. + */ + check_inherited (); + + /** + * Each rounding mode. + */ + for (i = 0; i < sizeof (rounding) / sizeof (rounding[0]); i++) { + assert (fesetround (rounding[i]) == 0); + check_inherited (); + } + assert (fesetround (FE_TONEAREST) == 0); + + /** + * An enabled (unmasked) exception. No floating-point operations are done + * while it is enabled. + */ + assert (feenableexcept (FE_DIVBYZERO) != -1); + check_inherited (); + assert (fedisableexcept (FE_DIVBYZERO) != -1); + + /** + * Raised exception flags. + */ + assert (feraiseexcept (FE_INEXACT | FE_OVERFLOW) == 0); + check_inherited (); + assert (feclearexcept (FE_ALL_EXCEPT) == 0); + +#ifdef TEST_X86 + /** + * x87 precision control: 24-bit and 53-bit. + */ + set_x87_cw ((get_x87_cw () & ~0x0300) | 0x0000); + check_inherited (); + set_x87_cw ((get_x87_cw () & ~0x0300) | 0x0200); + check_inherited (); + set_x87_cw ((get_x87_cw () & ~0x0300) | 0x0300); + + /** + * MXCSR flush-to-zero and denormals-are-zero. + */ + if (has_sse ()) { + set_mxcsr (get_mxcsr () | 0x8040); + check_inherited (); + set_mxcsr (get_mxcsr () & ~0x8040); + } +#endif + + assert (fesetenv (&saved) == 0); + + return 0; +} -- 2.54.0.windows.1 _______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
