[PATCH] math: Add optimization barrier to ensure a1 + u.d is not reused [BZ #30664]

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Feb 25 19:27:37 GMT 2025



On 07/02/25 14:00, John David Anglin wrote:
> This patch fixes the test fma fails seen on hppa with gcc-13 and
> later.
> 
> There may be more instances of this problem still lurking in
> sysdeps/ieee754/dbl-64/s_fma.c and other similar files.  Whether
> they occur in practice is very hardware dependent.  I'm not able
> to test changes to any other files on hppa.
> 
> Okay?
> 
> Dave
> ---
> 
> math: Add optimization barrier to ensure a1 + u.d is not reused [BZ #30664]
> 
> A number of fma tests started to fail on hppa when gcc was changed to
> use Ranger rather than EVRP.  Eventually I found that the value of
> a1 + u.d in this is block of code was being computed in FE_TOWARDZERO
> mode and not the original rounding mode:
> 
>     if (TININESS_AFTER_ROUNDING)
>       {
>         w.d = a1 + u.d;
>         if (w.ieee.exponent == 109)
>           return w.d * 0x1p-108;
>       }
> 
> This caused the exponent value to be wrong and the wrong return path
> to be used.

Is there some compiler annotation/attribute to mark libc_fesetround and
any other function as compiler barrier to avoid such issue?

Another possibility would to rewrite it to avoid FP operations.

> 
> Here we add an optimization barrier after the rounding mode is reset
> to ensure that the previous value of a1 + u.d is not reused.
> 
> Signed-off-by: John David Anglin <dave.anglin@bell.net>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> 
> diff --git a/sysdeps/ieee754/dbl-64/s_fma.c b/sysdeps/ieee754/dbl-64/s_fma.c
> index 20f617b996..42351c6b34 100644
> --- a/sysdeps/ieee754/dbl-64/s_fma.c
> +++ b/sysdeps/ieee754/dbl-64/s_fma.c
> @@ -244,6 +244,9 @@ __fma (double x, double y, double z)
>    /* Reset rounding mode and test for inexact simultaneously.  */
>    int j = libc_feupdateenv_test (&env, FE_INEXACT) != 0;
>  
> +  /* Ensure value of a1 + u.d is not reused.  */
> +  a1 = math_opt_barrier (a1);
> +
>    if (__glibc_likely (adjust == 0))
>      {
>        if ((u.ieee.mantissa1 & 1) == 0 && u.ieee.exponent != 0x7ff)



More information about the Libc-alpha mailing list