On 2026/10/19 05:34, Peter Damianov wrote:
> On other x86 targets, -mpc32, -mpc64 and -mpc80 link crtprec32.o,
> crtprec64.o or crtprec80.o, whose constructor sets the x87 precision
> control.  The mingw-w64 targets accept the options, but libgcc does not
> build those objects for them and the driver does not link them, so the
> options have no effect.
> 
> Build the objects for mingw targets and link them for mingw-w64.  Unlike
> on Linux, a new thread on Windows does not inherit the x87 control word
> of the thread that created it.  Instead, every thread starts with 53-bit
> precision.  On 32-bit targets crtprec.c therefore also registers a TLS
> callback that sets the precision on each new thread.  It is placed in
> .CRT$XLB so that it runs before the CRT's callback that runs dynamic
> initializers of thread-local variables.
> 
> This is not possible on x86_64, where the thread's initial context,
> including its floating-point state, is loaded after TLS callbacks run.
> There is nothing crtprec.c can do about threads created with
> CreateThread or _beginthreadex there.  But winpthreads passes the
> creating thread's floating-point environment to new threads (mingw-w64
> patch "winpthreads: Inherit the floating-point environment in new
> threads"), so on x86_64 the setting still reaches the main thread and
> threads created with pthread_create.
> 
> The TLS callback refers to the CRT's TLS directory, _tls_used, so the
> objects are linked from STARTFILE_SPEC, ahead of the CRT libraries,
> rather than from ENDFILE_SPEC as on other targets.
> 
> gcc/ChangeLog:
> 
>       * config/i386/mingw-w64.h (STARTFILE_SPEC): Link crtprec32.o,
>       crtprec64.o or crtprec80.o for -mpc32, -mpc64 or -mpc80.
> 
> libgcc/ChangeLog:
> 
>       * config.host (i[34567]86-*-mingw*, x86_64-*-mingw*): Add
>       crtprec32.o, crtprec64.o and crtprec80.o to extra_parts and
>       i386/t-crtpc to tmake_file.
>       * config/i386/crtprec.c [_WIN32 && __i386__] (set_precision_tls):
>       New function.
>       (crtprec_tls_used, crtprec_tls_callback): New variables.
> 
> Signed-off-by: Peter Damianov <[email protected]>
> ---
> Testing: I built an i686-w64-mingw32 cross compiler (C only) from this
> commit and ran a small test program on Windows 11 (32-bit process under
> WOW64) and on native 32-bit Windows 2000 (in the v86 emulator).  With
> -mpc32, -mpc64 and -mpc80, the x87 control word has the requested
> precision on the main thread and on threads created with CreateThread,
> _beginthreadex and pthread_create, linked with and without -pthread,
> statically and against the winpthreads DLL.  A TLS callback placed in
> .CRT$XLC, where the CRT constructs thread-local objects, also sees the
> requested precision.  I have not built or tested an x86_64-w64-mingw32
> compiler with this change, and have not run the GCC testsuite.
> 
> Like on Linux, crtprec*.o is also linked into DLLs built with -shared, so
> such a DLL changes the precision of the process that loads it.  I can
> restrict it to executables with %{!shared:%{!mdll:...}} if preferred.
> 
> The winpthreads change mentioned above was posted to mingw-w64-public
> as Message-ID <[email protected]>.
> 
>  gcc/config/i386/mingw-w64.h  |  6 ++++++
>  libgcc/config.host           |  8 ++++----
>  libgcc/config/i386/crtprec.c | 31 +++++++++++++++++++++++++++++++
>  3 files changed, 41 insertions(+), 4 deletions(-)

Hi, I recently set up a GitHub repo [1] to automatically test builds on all
Windows targets (Cygwin, MSYS2, Cygwin-to-MinGW cross, MSYS2-to-MinGW cross,
MinGW native) in GitHub Actions. Hopefully, this can make testing GCC patches
regarding cygwin / mingw-w64 easier, and ensure patch to one target won't
break other Windows targets.

I've opened a PR to test your patch [2]. It'd be better if you could provide
some simple tests so that we can see whether this patch actually fix the issue
for all Windows targets.

[1]: https://github.com/dragon-archer/gcc-for-windows
[2]: https://github.com/dragon-archer/gcc-for-windows/pull/6

Reply via email to