Skip to content

Commit

Permalink
Add -ffp-contract=off compile flag.
Browse files Browse the repository at this point in the history
GCC per default enables one optimization for x86_64 that can change the
result of floating-point operations: -ffp-contract=fast.

This allows the compiler to do floating-point expression contraction
such as combining multiplication and addition instructions with an FMA
instruction. However, the FMA instruction omits the rounding done in the
multiplication instruction, making the calculations produce different
results for some input values.

Disabling floating-point contraction with -ffp-contract=off makes GCC
generate code that produces a consistent result for x86_64.

See https://kristerw.github.io/2021/11/09/fp-contract/

The `fft_test` checks that this is working correctly.

Signed-off-by: Tim 'mithro' Ansell <tansell@google.com>
  • Loading branch information
mithro committed Jan 10, 2024
1 parent 537d81c commit c5984d7
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ add_compile_options(
-Wno-sign-compare
-Wp,-D_GLIBCXX_ASSERTIONS
$<$<CXX_COMPILER_ID:Clang>:-Wno-gnu-zero-variadic-macro-arguments>
# Needed for floating point stability in FFT (fft_test will check this).
# See also https://kristerw.github.io/2021/11/09/fp-contract/
-ffp-contract=off
# Apple clang 14.0.0 deprecates sprintf, which generates 900 warnings.
$<$<CXX_COMPILER_ID:AppleClang>:-Wno-deprecated-declarations>
$<$<BOOL:${ASAN}>:-fsanitize=address>
Expand Down

0 comments on commit c5984d7

Please sign in to comment.