|
|
Message-ID: <20260819230957.GG23438@brightrain.aerifal.cx>
Date: Wed, 19 Aug 2026 19:09:58 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com, Sergey Davidoff <shnatsel@...il.com>
Subject: Re: [PATCH v4 2/2] math: fmaf rewrite
On Wed, Aug 19, 2026 at 11:05:59PM +0200, Szabolcs Nagy wrote:
> * Rich Felker <dalias@...c.org> [2026-08-19 15:53:43 -0400]:
>
> > On Wed, Aug 19, 2026 at 05:28:28PM +0200, Szabolcs Nagy wrote:
> > > + double xy = (double)x * y;
> > > + union {double r; uint64_t i;} u = {xy + z};
> > > + int e = u.i>>52 & 0x7ff;
> > > + /* covers |r| > 0x1p-126 halfway cases (may round incorrectly) */
> > > + int halfway = (u.i & 0x1fffffff) == 0x10000000;
> > > + /* covers tiny inexact (may miss uflow) and tiny halfway cases */
> > > + int tiny = e <= 0x3ff-126 && e >= 0x3ff-149;
> > > + if (!halfway && !tiny)
> >
> > Shouldn't this just be:
> >
> > if ((!halfway && !tiny) || e == 0x7ff)
> >
> > so that the rest of the function doesn't have to be conditional? Or am
> > I missing something?
>
> yeah that works too.
>
> i was trying to keep the hot path clearly separate
> and it was easier to place comments
>
> and i think gcc is also more likely to move the
> e==0x7ff check out of the straight line hot path
I would expect that it first evaluates (!halfway && !tiny) and reaches
the normal hot path without even comparing e with 0x7ff. With CSE, I
wouldn't even be surprised if both versions of the code generate
identical asm. This is how I noticed it -- I was looking for "what's
different in the slow path where the value is inf/nan?" and found
"nothing".
And as written, !halfway is true for inf and most nan, and !tiny is
true for all inf and nan, so only very specific nans get to the
explicit check for e != 0x7ff.
> with my code, so hot code is tighter together.
> but it's a bit random what compilers do, so i'm
> fine either way.
I don't have a strong opinion on it, but I wonder if the version I
suggested makes more sense.
Rich
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.