Something like this?

#include <stdio.h>
#include <fenv.h>

int main(void)
{
    /* Compute 1.0/3.0 in default FPU mode */
    volatile float somefloat = 1.0;
    somefloat /= 3.0;

    /* Output it in float and hex formats */
    printf("%.9g\thex: %x\n", somefloat, *(int *)&somefloat);

    fenv_t a, b;

    fegetenv(&a); /* Save fpu env */

    fesetround(FE_TOWARDZERO);  /* Set round mode */

    fesetenv(&a); /* Restore fpu env (round mode must also be restored) */

    fegetenv(&b); /* Get current fpu env */

    /* Compute 1.0/3.0 after changing and restoring fpu env */
    volatile float somefloat2 = 1.0;
    somefloat2 /= 3.0;

    /* Output it in float and hex formats */
    printf("%.9g\thex: %x\n", somefloat2, *(int *)&somefloat2);

    /* Output FPU control words */
    printf ("FPU control words: %x, %x\n", a.__control_word, b.__control_word);

    return 0;
}


Output for incorrect work of fesetenv:
0.333333343 hex: 3eaaaaab
0.333333313 hex: 3eaaaaaa
FPU control words: 37f, f7f

For correct work output :
0.333333343 hex: 3eaaaaab
0.333333343 hex: 3eaaaaab
FPU control words: 37f, 37f

чт, 7 мар. 2019 г. в 21:50, Liu Hao <lh_mo...@126.com>:
>
> 在 2019/2/25 22:41, Liu Hao 写道:
> > 在 2019/2/25 19:27, Zidane Sama 写道:
> >> After this commit:
> >>
> >> [e98d80] (2.0 kB) by  Kai Tietz
> >> Add a fix for working fesetenv in libquadmath-library
> >>
> >> fesetenv was broken and do not sets new fpu state because it's now
> >> overwrites new env state by fnstenv.
> >>
> >> And this patch must fix it by store only mxcsr and load its afterwards:
> >>
> >> diff --git a/mingw-w64-crt/misc/fesetenv.c b/mingw-w64-crt/misc/fesetenv.c
> >> index f998017b..07035c66 100644
> >> --- a/mingw-w64-crt/misc/fesetenv.c
> >> +++ b/mingw-w64-crt/misc/fesetenv.c
> >> @@ -66,8 +66,7 @@ int fesetenv (const fenv_t * envp)
> >>      {
> >>        fenv_t env = *envp;
> >>        int _mxcsr;
> >> -      __asm__ ("fnstenv %0\n"
> >> -           "stmxcsr %1" : "=m" (*&env), "=m" (*&_mxcsr));
> >> +      __asm__ ("stmxcsr %0" : "=m" (*&_mxcsr));
> >>        /*_mxcsr = ((int)envp->__unused0 << 16) | (int)envp->__unused1;
> >> *//* mxcsr low and high */
> >>        env.__unused0 = 0xffff;
> >>        env.__unused1 = 0xffff;
> >>
> >>
> >>
> >
> > This seems a good catch.
> >
> > However I think the `STMXCSR` instruction should follow
> > `__mingw_has_sse()`, although CPUs without SSE are rare. I propose
> > another patch to address this issue.
> >
> >
> >
>
> Ping?  Do you have any testcases related to this issue? Please notice
> that without any response we will not act on this.
>
>
> --
> Best regards,
> LH_Mouse
>


_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to