[PATCH] Ensure calculations happen with desired rounding mode in y1lf128

Michael Hudson-Doyle michael.hudson@canonical.com
Wed Aug 17 04:23:11 GMT 2022


On Tue, 16 Aug 2022 at 09:00, Joseph Myers <joseph@codesourcery.com> wrote:

> On Fri, 12 Aug 2022, Wilco Dijkstra via Libc-alpha wrote:
>
> > All math functions using the SET_RESTORE_ROUND macros will need similar
> > barriers.


Sounds fun. Really, wrapping mode changes around a function call sounds
saner but also tedious in a different way.


> > Note that it is feasible to remove these macros altogether and fix
> > any issues (a slightly larger ULP is acceptable for non-nearest
> rounding).
>

Removing the rounding mode change entirely makes the errors quite a lot
worse, as the bug Joseph links to shows.


> > Given rounding mode changes are generally expensive, this also improves
> > performance (though that may not be important for 128-bit floats).


> This one is a case where SET_RESTORE_ROUND is used to reduce error
> accumulation to keep the errors within the bounds accepted by the
> testsuite (see bug 16824).  In such cases, it may indeed be possible to
> change the algorithm to one that has less total error accumulation
> possible in any rounding mode so the results are sufficiently accurate
> independent of rounding mode without needing SET_RESTORE_ROUND.
>
> In other cases, the manipulation of the floating-point environment is
> needed for correctness, e.g. to avoid spurious exceptions or to implement
> round-to-odd for functions that must be correctly rounding, or it's
> because algorithms for higher internal precision are used (Dekker / Knuth)
> that are only correct in round-to-nearest most and much larger errors
> might occur if those are used in the wrong rounding mode.
>

It does seem likely that an algorithm that does not require setting a
rounding mode would in general be better than one that does but also that
this is not very realistic. But I guess my point is that SET_RESTORE_ROUND
without barriers is a footgun. I guess I should commit my patch and perhaps
see about writing some more for other uses of the macro?

Cheers,
mwh


More information about the Libc-alpha mailing list