Version: SMASH-3.1
fpenvironment.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2015,2017-2018,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__
34 inline bool enable_float_traps(int mask) { return -1 != feenableexcept(mask); }
35 #elif defined __SSE__ && !defined __clang__
37 bool enable_float_traps(int mask);
38 #else
40 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_
Guard type that safely disables floating point traps for the scope in which it is placed.
Definition: fpenvironment.h:79
~DisableFloatTraps()
When the guard goes out of scope the floating point environment is restored.
Definition: fpenvironment.h:98
DisableFloatTraps(int mask=0)
Constructs the guard object.
Definition: fpenvironment.h:87
void reenable_traps(int mask)
Reenables the given traps.
std::fenv_t environment_
The stored environment that the destructor will restore.
Definition: action.h:24
bool enable_float_traps(int)
Fallback that fails to set the trap.
Definition: fpenvironment.h:40
void without_float_traps(F &&f)
Convenience function to create a scope where all floating point traps are disabled.
void setup_default_float_traps()
Setup the floating-point traps used throughout SMASH.