Follow @Openwall on Twitter for new release announcements and other news
[<prev] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20260813043856.GC3542221@port70.net>
Date: Thu, 13 Aug 2026 06:38:56 +0200
From: Szabolcs Nagy <nsz@...t70.net>
To: Sergey Davidoff <shnatsel@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: Bug in fmaf: subnormals are rounded incorrectly

* Sergey Davidoff <shnatsel@...il.com> [2026-08-10 18:24:55 +0100]:
> Rich wrote:
> > If this test is the only thing causing it to be wrong, it could easily
> > be adjusted to work with subnormals that are halfway cases, or to just
> > handle all subnormals in this code path if that works.
> 
> Incorrect rounding of subnormals is the only bug I am aware of, but I do not
> think that adjusting that condition alone is sufficient.

the halfway mask is exponent dependent in the subnormal range,
that can be fixed, it's patch 1, attached.

> 
> I am hesitant to invent a novel algorithm here because the input space is large
> (2 to the 96th power) and the failure cases are sparse, so exhaustive
> verification is intractable and random search won't find this kind of bug.
> 
> I suggest following this paper which contains a formally proven algorithm:
> https://guillaume.melquiond.fr/doc/08-tc.pdf

interesting, thanks, but i don't think it helps much.

i think correct rounding of fmaf is tracktable, the paper can
help with that but there are error prone implementation issues
the paper does not deal with.

e.g. there is still a known bug in fmaf: on architectures
where underflow is raised before rounding (arm, ppc) we can
fail to raise it if the rounded result is not subnormal:

  fmaf(-0x1p-100f,0x1p-100f,0x1p-126f)

this can be fixed with a few extra checks, i did that for fma,
but piling up such cornercase cheks is what leads to subtle
bugs (target specifc, hard to optimize, etc).

we had many issues with the fma* code from freebsd, i rewrote
fma eventually.

i cleaned up fmaf now, but not with int code like in fma, so
issues like the uflow bug would be hard to fix. this is patch 2.

> 
> I've originally encountered this bug in the Rust standard library, which
> derives its software f32 FMA from musl (which derives from FreeBSD).
> Here is my reading of the algorithm from the paper applied to it:
> https://github.com/rust-lang/compiler-builtins/pull/1270/changes
> 
> The diff is not large, but it's not as simple as changing one condition either.

View attachment "0001-math-fix-fmaf-subnormal-double-rounding.patch" of type "text/x-diff" (1618 bytes)

View attachment "0002-math-fmaf-optimization-and-cleanup.patch" of type "text/x-diff" (5367 bytes)

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.