Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/examples/numerics/cfenv.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cfenv>
Expand Down
7 changes: 7 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions tests/numerics/cfenv_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cmath> 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 <cmath> 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 <cfenv>
Expand Down