diff --git a/docs/examples/numerics/cfenv.md b/docs/examples/numerics/cfenv.md index f908865..e57a88c 100644 --- a/docs/examples/numerics/cfenv.md +++ b/docs/examples/numerics/cfenv.md @@ -195,7 +195,11 @@ after `std::log(0.0)` is therefore not portable. `fetestexcept`: in the `quiet_reciprocal` example above, with a plain `double r`, GCC 15 at `-O2` performed the division after the test and the caller saw `FE_DIVBYZERO`. Use `volatile` operands and results (as these - examples do) when the code must work on GCC. + examples do) when the code must work on GCC. Even that isn't enough for + `std::rint` on x86-64: at `-O2` GCC 16 expands it inline as the rounded + magnitude with the sign copied back, so a volatile `-2.5` under `FE_UPWARD` + gave `-3.0`, not `-2.0`. With `-frounding-math` it gave `-2.0`. Code that + changes the rounding mode SHOULD be built with `-frounding-math` on GCC. ```cpp run #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1d443af..22f2b3c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -294,6 +294,13 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_G PROPERTIES COMPILE_OPTIONS -freflection) endif() +# Code that changes the rounding mode needs -frounding-math on GCC; without it an +# optimized x86-64 build expands std::rint as if rounding to nearest (#371). +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set_source_files_properties(numerics/cfenv_test.cpp + PROPERTIES COMPILE_OPTIONS -frounding-math) +endif() + add_custom_target(examples_tests) add_dependencies(examples_tests cppdocs_support_tests) diff --git a/tests/numerics/cfenv_test.cpp b/tests/numerics/cfenv_test.cpp index c0da284..d07eaa0 100644 --- a/tests/numerics/cfenv_test.cpp +++ b/tests/numerics/cfenv_test.cpp @@ -1,8 +1,9 @@ // Checks claims made by docs/examples/numerics/cfenv.md. // // Operands are volatile (as on the page) so nothing folds at compile time; -// the harness builds at -O0 and doesn't use #pragma STDC FENV_ACCESS at file -// scope (GCC warns that it ignores it). Tests that call a function +// GCC builds this file with -frounding-math (tests/CMakeLists.txt), which the +// hardened presets' -O2 needs for std::rint on x86-64 (#371). It doesn't use +// #pragma STDC FENV_ACCESS at file scope (GCC warns that it ignores it). Tests that call a function // Clang may expand inline under a non-default mode use it at block scope, // for Clang only, as the page's examples do. #include