On Wednesday 21 October 2026 23:30:56 Pali Rohár wrote:
> On Wednesday 21 October 2026 23:26:48 Peter0x44 wrote:
> > On 2026-10-21 21:41, Pali Rohár wrote:
> > > On Tuesday 20 October 2026 01:40:17 Peter0x44 wrote:
> > > > On 2026-10-19 12:01, Pali Rohár wrote:
> > > > > The msvcrt/UCRT _fpreset function is not only the x87 fninit 
> > > > > instruction
> > > > > but is doing also some other work, like setting SSE registers or
> > > > > recovering
> > > > > from state when the function was called from the SIGFPE signal handler
> > > > > (which is begin called from SEH exception filter dispatcher).
> > > > 
> > > > I was curious what it does with SSE, so I tested it. It resets MXCSR
> > > > to
> > > > 0x1f80. But on x86_64 it doesn't touch the x87 unit at all. Only on
> > > > i686 does it also reset the x87 control word (to 0x027f). That's the
> > > > same for ucrtbase.dll and msvcrt.dll.
> > > 
> > > So, we do not need to change MXCSR in fesetenv (which you added
> > > recently), right?
> > 
> > For msvcrt and ucrt, it's probably not needed. Not sure I would jump the gun
> > to just remove it though.
> > Maybe crtdll.dll, msvcrt20, msvcrt40 are not aware of SSE and could need
> > this anyway?
> > I don't think it hurts to keep it. I will leave the decision up to you. Send
> > a patch if you want it removed.
> 
> Ok. I will investigate how older msvcr* version behaves and will do
> cleanup (if it is possible) based on my results of those older libs.
> At least we would have documentation how libs are working.

I checked different 32-bit x86 crtdll and msvcr* versions of their
_fpreset function and the results are quite surprising.

32-bit x86                          x87     SSE
crtdll                            64-bit  nothing
msvcrt10                          64-bit  nothing
msvcrt20                          64-bit  nothing
msvcrt40                          53-bit  nothing
msvcrt (4.2/5.0/6.0)              53-bit  nothing
msvcrt (nt4/98/me/2k/xp/2003sp1)  53-bit  nothing
msvcrt (2003sp2,vista,7,8,8.1,10) 53-bit   reset
msvcr70                           53-bit  nothing
msvcr71                           53-bit  nothing
msvcr80                           53-bit   reset
msvcr90                           53-bit   reset
msvcr100                          53-bit   reset
msvcr110                          53-bit   reset
msvcr120                          53-bit   reset
ucrt                              53-bit   reset

64-bit x86
everything                       nothing   reset

So it is needed to fix _fpreset in CRT_fp8.c for crtdll, msvcrt10 and
msvcrt20 builds because it does not set x87 precision to 53-bit mantisa.
And it is also needed to fix all CRT_fp*.c for pre-msvcr80 builds
because it does not reset SSE MXCSR.

Because the CRT_fp*.c files are not part of libmsvcr*.a import libs, the
fix needs to be conditional. I have few alternative ideas how to address
these fixes:
* in every 32-bit x86 import library provide 3 reset functions (4/8/10)
  and call the appropriate from every file CRT_fp*.c
* call _control87() in every file CRT_fp*.c unconditionally
* call ldmxcsr 0x1F80 in every file CRT_fp*.c unconditionally
* provide helper function for resetting in only affected import library
  and call it conditionally in CRT_fp*.c based on symbol presence

What do you think that is the best approach?

Note that this is independent of fesetenv. The _fpreset function is
also needed to work correctly and it is called during the process startup.


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

Reply via email to