Version: SMASH-3.1
smash::DisableFloatTraps Class Reference

#include <fpenvironment.h>

Guard type that safely disables floating point traps for the scope in which it is placed.

Example:

{
// some code where FPEs will trap
// all code up to the closing brace will not trap anymore.
}
DisableFloatTraps(int mask=0)
Constructs the guard object.
Definition: fpenvironment.h:87

You can also keep some traps enabled. E.g. FE_DIVBYZERO is a candidate you might want to keep enabled, whereas FE_UNDERFLOW and FE_OVERFLOW are the ones that you really need to get rid of:

DisableFloatTraps guard(FE_DIVBYZERO | FE_INVALID);
Note
There is no guarantee about the complexity of modifying the floating point environment. This could be very expensive. Therefore it is always preferable to fix your code to not require underflow or overflow.
See also
http://en.cppreference.com/w/cpp/numeric/fenv/FE_exceptions for a complete list of flags.

Definition at line 79 of file fpenvironment.h.

Public Member Functions

 DisableFloatTraps (int mask=0)
 Constructs the guard object. More...
 
 ~DisableFloatTraps ()
 When the guard goes out of scope the floating point environment is restored. More...
 

Private Member Functions

void reenable_traps (int mask)
 Reenables the given traps. More...
 

Private Attributes

std::fenv_t environment_
 The stored environment that the destructor will restore. More...
 

Constructor & Destructor Documentation

◆ DisableFloatTraps()

smash::DisableFloatTraps::DisableFloatTraps ( int  mask = 0)
inlineexplicit

Constructs the guard object.

Parameters
maskA bitwise of the traps you want to keep enabled.
Returns
The constructed object.

Definition at line 87 of file fpenvironment.h.

87  {
88  std::feholdexcept(&environment_);
89  if (mask != 0) {
90  reenable_traps(mask);
91  }
92  }
void reenable_traps(int mask)
Reenables the given traps.
std::fenv_t environment_
The stored environment that the destructor will restore.

◆ ~DisableFloatTraps()

smash::DisableFloatTraps::~DisableFloatTraps ( )
inline

When the guard goes out of scope the floating point environment is restored.

Definition at line 98 of file fpenvironment.h.

98 { std::fesetenv(&environment_); }

Member Function Documentation

◆ reenable_traps()

void smash::DisableFloatTraps::reenable_traps ( int  mask)
private

Reenables the given traps.

Parameters
maskA bitwise of the traps you want to keep enabled.

Definition at line 63 of file fpenvironment.cc.

63  {
64  if (!enable_float_traps(mask)) {
65  logg[LFpe].warn("Failed to setup traps on ", mask);
66  }
67 }
std::array< einhard::Logger<>, std::tuple_size< LogArea::AreaTuple >::value > logg
An array that stores all pre-configured Logger objects.
Definition: logging.cc:39
bool enable_float_traps(int)
Fallback that fails to set the trap.
Definition: fpenvironment.h:40
static constexpr int LFpe

Member Data Documentation

◆ environment_

std::fenv_t smash::DisableFloatTraps::environment_
private

The stored environment that the destructor will restore.

Definition at line 108 of file fpenvironment.h.


The documentation for this class was generated from the following files: