[Bug 277783] libc fma() doesn't not return the correct zero sign

From: <bugzilla-noreply_at_freebsd.org>
Date: Wed, 12 Jun 2024 18:58:03 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=277783

--- Comment #11 from Steve Kargl <kargl@FreeBSD.org> ---
Someone smarter than me may need to look at this!  With the original
src/s_fma.c, I see

dble (x,y,z): -0x1p+0  0x1p+0  0x1p+0
mpfr (x,y,z): -0x1p+0  0x1p+0  0x1p+0
        mpfr    libm
RNDN:  0x0p+0  0x0p+0
RNDU:  0x0p+0  0x0p+0
RNDD: -0x0p+0 -0x0p+0
RNDZ:  0x0p+0  0x0p+0

dble (x,y,z):  0x1p+0 -0x1p+0  0x1p+0
mpfr (x,y,z):  0x1p+0 -0x1p+0  0x1p+0
        mpfr    libm
RNDN:  0x0p+0  0x0p+0
RNDU:  0x0p+0  0x0p+0
RNDD: -0x0p+0 -0x0p+0
RNDZ:  0x0p+0  0x0p+0

dble (x,y,z): -0x1p+0 -0x1p+0 -0x1p+0
mpfr (x,y,z): -0x1p+0 -0x1p+0 -0x1p+0
        mpfr    libm
RNDN:  0x0p+0  0x0p+0
RNDU:  0x0p+0  0x0p+0
RNDD: -0x0p+0 -0x0p+0
RNDZ:  0x0p+0  0x0p+0

dble (x,y,z):  0x1.ffffffffffff8p-501  0x1.0000000000004p-500 -0x1p-1000
mpfr (x,y,z):  0xf.fffffffffffcp-504  0x1.0000000000004p-500 -0x1p-1000
        mpfr    libm
RNDN: -0x0p+0  0x0p+0
RNDU: -0x0p+0  0x0p+0
RNDD: -0x1p-1074 -0x1p-1074
RNDZ: -0x0p+0  0x0p+0

The first groups of three are fma(-1.,1.,1.), fma(1.,-1.,1.), and
fma(-1.,-1.,-1.) with the four rounding modes.  The group is Victor's example
where the rounding is wrong.

If one looks in src/s_fma.c, there is a special case for addends that sum to
zero.
This is the 'if (r.hi == 0.0) {}' block of code (lines 263-274).  If I comment 
out this block, I get wrong results for fma(-1.,1.,1.), fma(1.,-1.,1.), and
fma(-1.,-1.,-1.), but correct results for Victor's input. :(

dble (x,y,z): -0x1p+0  0x1p+0  0x1p+0
mpfr (x,y,z): -0x1p+0  0x1p+0  0x1p+0
        mpfr    libm
RNDN:  0x0p+0  0x0p+0
RNDU:  0x0p+0  0x0p+0
RNDD: -0x0p+0  0x0p+0
RNDZ:  0x0p+0  0x0p+0

dble (x,y,z):  0x1p+0 -0x1p+0  0x1p+0
mpfr (x,y,z):  0x1p+0 -0x1p+0  0x1p+0
        mpfr    libm
RNDN:  0x0p+0  0x0p+0
RNDU:  0x0p+0  0x0p+0
RNDD: -0x0p+0  0x0p+0
RNDZ:  0x0p+0  0x0p+0

dble (x,y,z): -0x1p+0 -0x1p+0 -0x1p+0
mpfr (x,y,z): -0x1p+0 -0x1p+0 -0x1p+0
        mpfr    libm
RNDN:  0x0p+0  0x0p+0
RNDU:  0x0p+0  0x0p+0
RNDD: -0x0p+0  0x0p+0
RNDZ:  0x0p+0  0x0p+0

dble (x,y,z):  0x1.ffffffffffff8p-501  0x1.0000000000004p-500 -0x1p-1000
mpfr (x,y,z):  0xf.fffffffffffcp-504  0x1.0000000000004p-500 -0x1p-1000
        mpfr    libm
RNDN: -0x0p+0 -0x0p+0
RNDU: -0x0p+0 -0x0p+0
RNDD: -0x1p-1074 -0x1p-1074
RNDZ: -0x0p+0 -0x0p+0

-- 
You are receiving this mail because:
You are the assignee for the bug.