Page MenuHomeFreeBSD

Avoid raising unexpected floating point exceptions in libm
ClosedPublic

Authored by dim on Apr 9 2021, 11:09 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Mar 31, 10:07 PM
Unknown Object (File)
Jan 31 2024, 9:32 PM
Unknown Object (File)
Dec 29 2023, 3:37 AM
Unknown Object (File)
Dec 20 2023, 4:13 AM
Unknown Object (File)
Dec 18 2023, 9:13 PM
Unknown Object (File)
Dec 13 2023, 8:39 AM
Unknown Object (File)
Dec 7 2023, 10:58 AM
Unknown Object (File)
Nov 6 2023, 4:55 AM
Subscribers

Details

Summary

When using clang with x86_64 CPUs that support AVX, some floating point
transformations may raise exceptions that would not have been raised by
the original code. To avoid this, use the -fp-exception-behavior=maytrap
flag, introduced in clang 10.0.0.

In particular, this fixes a number of test failures with ctanhf(3) and
ctanf(3), when libm is compiled with -mavx. An unexpected FE_INVALID
exception is then raised, because clang emits vdivps instructions to
perform certain divides. (The vdivps instruction operates on multiple
single-precision float operands simultaneously, but the exceptions may
be influenced by unused parts of the XMM registers. In this particular
case, it was calculating 0 / 0, which results in FE_INVALID.)

If -fp-exception-behavior=maytrap is specified however, clang uses
vdivss instructions instead, which work on one operand, and should not
raise unexpected exceptions.

Reported by: olivier
PR: 254911
MFC after: 1 week

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

dim requested review of this revision.Apr 9 2021, 11:09 PM

Note that we could also apply this flag *only* to lib/msun/src/s_ctanhf.c optionally, as this is the only case we know for certain that has issues with these spurious exceptions.

However, I guess that the performance gain from using e.g. vdivps and friends is not that shocking in libm. It would be nice if we also had performance tests next to functional tests... :)

I had a similar patch locally using =strict, but it seems like maytrap should be sufficient.

This revision is now accepted and ready to land.Apr 9 2021, 11:16 PM