Skip to content

test: Avoid -ffinite-math-only on floating point comparisons - #1373

Open
iv-m wants to merge 2 commits into
simd-everywhere:masterfrom
iv-m:tests-no-finite-math-only
Open

test: Avoid -ffinite-math-only on floating point comparisons#1373
iv-m wants to merge 2 commits into
simd-everywhere:masterfrom
iv-m:tests-no-finite-math-only

Conversation

@iv-m

@iv-m iv-m commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

With -ffinite-math-only (implied by -ffast-math and -Ofast), GCC considers all comparisons against infinities to be false and can optimize them away. This breaks several tests that rely on infinities to be compared correctly. To avoid this, we add GCC-specific optimize attribute that disables -ffinite-math-only optimization for floating point comarisions used in assertions.

@mr-c
mr-c force-pushed the tests-no-finite-math-only branch from 30e03ea to cea5dd8 Compare January 22, 2026 14:46
@mr-c
mr-c force-pushed the tests-no-finite-math-only branch from cea5dd8 to 7afd6ea Compare August 9, 2026 07:54
@mr-c
mr-c force-pushed the tests-no-finite-math-only branch from 7afd6ea to c68fff9 Compare August 20, 2026 11:07
@mr-c
mr-c enabled auto-merge (rebase) August 20, 2026 11:08
@mr-c
mr-c force-pushed the tests-no-finite-math-only branch from c68fff9 to 6e7c5b5 Compare August 20, 2026 11:54
@mr-c
mr-c force-pushed the tests-no-finite-math-only branch from 6e7c5b5 to 9f3bd66 Compare August 20, 2026 11:58
iv-m and others added 2 commits August 20, 2026 15:04
With `-ffinite-math-only` (implied by `-ffast-math` and -Ofast),
GCC considers all comparisons against infinities to be false
and can optimize them away. This breaks several tests that rely
on infinities to be compared correctly. To avoid this, we add
GCC-specific optimize attribute that disables `-ffinite-math-only`
optimization for floating point comarisions used in assertions.

Signed-off-by: Ivan A. Melnikov <iv@altlinux.org>
From the GCC manual page:

> Disregard strict standards compliance. -Ofast enables all -O3 optimizations.
> It also enables optimizations that are not valid for all standard-compliant
> programs. It turns on -ffast-math, -fallow-store-data-races.
> It turns off -fsemantic-interposition.

On recent clang, keep "-ffast-math" due to
> error: argument '-Ofast' is deprecated; use '-O3 -ffast-math' for the same
> behavior, or '-O3' to enable only conforming optimizations
> [-Werror,-Wdeprecated-ofast]
skipping "-O3" due to clang/llvm compiler bugs
@mr-c
mr-c force-pushed the tests-no-finite-math-only branch from 9f3bd66 to 8cf787c Compare August 20, 2026 12:04

@mr-c mr-c left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Ofast produces errors with various GCC versions; perhaps they are compiler errors, perhaps they are issues that SIMDe can workaround.

Regardless, they need addressing before we merge; unless -ffast-math is sufficient?

@iv-m

iv-m commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

The idea of commit test: Avoid -ffinite-math-only on floating point comparisons was to fix a very specific case of failing tests: when an assertion needs to compare two infinities (one returned from the function under test, and one from the expected response), but GCC assumed such comparison to be false because of -ffinite-math-only.

Since -ffast-math implies -ffinite-math-only, -ffast-math is enough to trigger this failure mode. Also, since this commit addresses a specific issue triggered by -ffinite-math-only, it probably won't help with any tests that are passing with -ffast-math but failing with -Ofast or when -O3 is added.

What it can do is to allow to unskip some tests that are currently skipped when SIMDE_FAST_MATH is set, which seems to be quite a lot of tests.

@mr-c

mr-c commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

@iv-m Great! Can you revert the change to use -Ofast where that failed, and then adjust the test skips where we can benefit?

@iv-m

iv-m commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

@iv-m Great! Can you revert the change to use -Ofast where that failed, and then adjust the test skips where we can benefit?

Well, I won't promise I'll review every entrance of SIMDE_FAST_MATH in the tests, but I think I can easily enable several tests (a couple of hundred #ifs from my rough initial estimate) to demonstrate that this approach works.

And I think since this change affects only some GCC-like compilers, these tests should stay disabled for all the rest of the compilers, and so another feature-test macro is needed for use in place of SIMDE_FAST_MATH. For example:

#if defined(SIMDE_FAST_MATH)
#define SIMDE_TEST_EQUALTITY_WORKS_FOR_INFINITIES
#endif
#if defined(SIMDE_FAST_MATH) && defined(HEDLEY_GCC_VERSION) && HEDLEY_GCC_VERSION_CHECK(4,4,0)
/* For these GCC versions, `-ffinite-math-only` is disabled for
 * the functions that are used in assertions */
#define SIMDE_TEST_EQUALTITY_WORKS_FOR_INFINITIES
#endif

SIMDE_TEST_EQUALTITY_WORKS_FOR_INFINITIES is somewhat long name. Maybe a negative macro, e.g. SIMDE_TEST_INFINITE_EQ_BROKEN, is better? Maybe you have better naming suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants