Version: SMASH-2.0
fpenvironment.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2015-2020
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_FPENVIRONMENT_H_
11 #define SRC_INCLUDE_SMASH_FPENVIRONMENT_H_
12 
13 #include <cfenv>
14 
15 namespace smash {
16 
17 #if defined _GNU_SOURCE && !defined __clang__
18 
34 inline bool enable_float_traps(int mask) { return -1 != feenableexcept(mask); }
35 #elif defined __SSE__ && !defined __clang__
36 bool enable_float_traps(int mask);
38 #else
39 inline bool enable_float_traps(int) { return false; }
41 #endif
42 
51 
80  public:
87  explicit DisableFloatTraps(int mask = 0) {
88  std::feholdexcept(&environment_);
89  if (mask != 0) {
90  reenable_traps(mask);
91  }
92  }
93 
98  ~DisableFloatTraps() { std::fesetenv(&environment_); }
99 
100  private:
105  void reenable_traps(int mask);
106 
108  std::fenv_t environment_;
109 };
110 
128 template <typename F>
129 void without_float_traps(F &&f) {
130  DisableFloatTraps guard;
131  f();
132 }
133 
134 } // namespace smash
135 
136 #endif // SRC_INCLUDE_SMASH_FPENVIRONMENT_H_
smash
Definition: action.h:24
smash::DisableFloatTraps::~DisableFloatTraps
~DisableFloatTraps()
When the guard goes out of scope the floating point environment is restored.
Definition: fpenvironment.h:98
smash::DisableFloatTraps::environment_
std::fenv_t environment_
The stored environment that the destructor will restore.
Definition: fpenvironment.h:108
smash::DisableFloatTraps
Guard type that safely disables floating point traps for the scope in which it is placed.
Definition: fpenvironment.h:79
smash::enable_float_traps
bool enable_float_traps(int)
Fallback that fails to set the trap.
Definition: fpenvironment.h:40
smash::DisableFloatTraps::reenable_traps
void reenable_traps(int mask)
Reenables the given traps.
Definition: fpenvironment.cc:63
smash::setup_default_float_traps
void setup_default_float_traps()
Setup the floating-point traps used throughout SMASH.
Definition: fpenvironment.cc:69
smash::DisableFloatTraps::DisableFloatTraps
DisableFloatTraps(int mask=0)
Constructs the guard object.
Definition: fpenvironment.h:87
smash::without_float_traps
void without_float_traps(F &&f)
Convenience function to create a scope where all floating point traps are disabled.
Definition: fpenvironment.h:129