[PATCH 8/8] math: Use math_opt_barrier on underflow/overflow

H.J. Lu hjl.tools@gmail.com
Thu Dec 18 23:42:55 GMT 2025


On Thu, Dec 18, 2025 at 8:46 PM Adhemerval Zanella Netto
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 17/12/25 21:05, H.J. Lu wrote:
> > On Thu, Dec 18, 2025 at 1:56 AM Adhemerval Zanella
> > <adhemerval.zanella@linaro.org> wrote:
> >>
> >> This avoids compiler to move the operation before the 'iy' compare.
> >>
> >> It fixes math/test-float128-pow regreesions when building with clang:
> >>
> >> Failure: pow (-0x1.000002p+0, 0xf.ffffffffffff8p+1020): Exception "Underflow" set
> >> Failure: pow (-0x1.000002p+0, 0xf.ffffffffffffbffffffffffffcp+1020): Exception "Underflow" set
> >> Failure: pow (-0x1.000002p+0, 0xf.fffffffffffffffffffffffffff8p+16380): Exception "Underflow" set
> >> Failure: pow (-0x1.000002p+0, 0xf.fffffffffffffffp+16380): Exception "Underflow" set
> >> Failure: pow (-0x1.000002p+0, 0xf.fffffp+124): Exception "Underflow" set
> >> Failure: pow (-0x1.00000ep+0, 0xf.ffffffffffff8p+1020): Exception "Underflow" set
> >> Failure: pow (-0x1.00000ep+0, 0xf.ffffffffffffbffffffffffffcp+1020): Exception "Underflow" set
> >> Failure: pow (-0x1.00000ep+0, 0xf.fffffffffffffffffffffffffff8p+16380): Exception "Underflow" set
> >> Failure: pow (-0x1.00000ep+0, 0xf.fffffffffffffffp+16380): Exception "Underflow" set
> >> Failure: pow (-0x2p+0, -0xf.ffffffffffff8p+1020): Exception "Overflow" set
> >> Failure: pow (-0x2p+0, -0xf.ffffffffffffbffffffffffffcp+1020): Exception "Overflow" set
> >> Failure: pow (-0x2p+0, -0xf.fffffffffffffffffffffffffff8p+16380): Exception "Overflow" set
> >> Failure: pow (-0x2p+0, -0xf.fffffffffffffffp+16380): Exception "Overflow" set
> >> Failure: pow (-0x2p+0, -0xf.fffffp+124): Exception "Overflow" set
> >> Failure: pow (-0x2p+0, 0xf.ffffffffffff8p+1020): Exception "Underflow" set
> >> Failure: pow (-0x2p+0, 0xf.ffffffffffffbffffffffffffcp+1020): Exception "Underflow" set
> >> Failure: pow (-0x2p+0, 0xf.fffffffffffffffffffffffffff8p+16380): Exception "Underflow" set
> >> Failure: pow (-0x2p+0, 0xf.fffffffffffffffp+16380): Exception "Underflow" set
> >> Failure: pow (-0x2p+0, 0xf.fffffp+124): Exception "Underflow" set
> >> [...]
> >>
> >> Checked on x86_64-linux-gnu and aarch64-linux-gnu with gcc-15 and
> >> clang-18.
> >> ---
> >>  sysdeps/ieee754/ldbl-128/e_powl.c | 6 ++++--
> >>  1 file changed, 4 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/sysdeps/ieee754/ldbl-128/e_powl.c b/sysdeps/ieee754/ldbl-128/e_powl.c
> >> index 4e20616705..9ff5a43573 100644
> >> --- a/sysdeps/ieee754/ldbl-128/e_powl.c
> >> +++ b/sysdeps/ieee754/ldbl-128/e_powl.c
> >> @@ -279,9 +279,11 @@ __ieee754_powl (_Float128 x, _Float128 y)
> >>        if (iy > 0x407d654b)
> >>         {
> >>           if (ix <= 0x3ffeffff)
> >> -           return (hy < 0) ? huge * huge : tiny * tiny;
> >> +           return (hy < 0) ? math_opt_barrier (huge * huge)
> >> +             : math_opt_barrier (tiny * tiny);
> >>           if (ix >= 0x3fff0000)
> >> -           return (hy > 0) ? huge * huge : tiny * tiny;
> >> +           return (hy > 0) ? math_opt_barrier (huge * huge)
> >> +             : math_opt_barrier (tiny * tiny);
> >>         }
> >
> > Does it generate less optimized codes for GCC?
>
> I do not think this really matter here, these are for corner cases where
> the function underflow/overflow.  And I think this are really a issue
> with clang with -frounding-math, since it can not assume that the
> multiplication will not generate underflow/overflow exceptions (so it can
> move out the comparison).

Is this a clang bug? I think this info should be mentioned in the commit log.

> In any case, I think this is a quite common technique that we use on other
> math implementations to either force overflow/underflow exception or to avoid
> some compiler issues (like 2fe5e2af0995a6e6ee2c761e55e7596a3220d07c).
>
> >
> >>        /* over/underflow if x is not close to one */
> >>        if (ix < 0x3ffeffff)
> >> --
> >> 2.43.0
> >>
> >
> >
>


-- 
H.J.


More information about the Libc-alpha mailing list