ATLAS Offline Software
Loading...
Searching...
No Matches
FPControl.cxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration.
3 */
10
11
12#include "CxxUtils/FPControl.h"
13#include "CxxUtils/features.h"
14
15
16namespace CxxUtils {
17
18
24FPControl::FPControl()
25{
26 fegetenv (&m_env);
27}
28
29
38FPControl::~FPControl()
39{
40 if (m_masked) {
41 feclearexcept (m_masked);
42 }
43 feupdateenv (&m_env);
44}
45
46
50void FPControl::holdExceptions()
51{
52 fenv_t tmp;
53 feholdexcept (&tmp);
54 m_masked |= FE_ALL_EXCEPT;
55}
56
57
62void FPControl::enable ([[maybe_unused]] Exc exc)
63{
64#if HAVE_FEENABLEEXCEPT
65 int mask = excToMask (exc);
66 feenableexcept (mask);
67 m_masked &= ~mask;
68#endif
69}
70
71
76void FPControl::disable ([[maybe_unused]] Exc exc)
77{
78#if HAVE_FEENABLEEXCEPT
79 int mask = excToMask (exc);
80 fedisableexcept (mask);
81 m_masked |= mask;
82#endif
83}
84
85
90int FPControl::excToMask (Exc exc)
91{
92 // A platform isn't required to define all of these...
93#ifdef FE_ALL_EXCEPT
94 if (CxxUtils::test (exc, Exc::all))
95 return FE_ALL_EXCEPT;
96#endif
97 int mask = 0;
98#ifdef FE_DIVBYZERO
99 if (CxxUtils::test (exc, Exc::divbyzero)) mask |= FE_DIVBYZERO;
100#endif
101#ifdef FE_INEXACT
102 if (CxxUtils::test (exc, Exc::inexact)) mask |= FE_INEXACT;
103#endif
104#ifdef FE_INVALID
105 if (CxxUtils::test (exc, Exc::invalid)) mask |= FE_INVALID;
106#endif
107#ifdef FE_OVERFLOW
108 if (CxxUtils::test (exc, Exc::overflow)) mask |= FE_OVERFLOW;
109#endif
110#ifdef FE_UNDERFLOW
111 if (CxxUtils::test (exc, Exc::underflow)) mask |= FE_UNDERFLOW;
112#endif
113 return mask;
114}
115
116
117} // namespace CxxUtils
118
Helper to control FP exceptions.
Some additional feature test macros.
constexpr std::enable_if_t< is_bitmask_v< E >, bool > test(E lhs, E rhs)
Convenience function to test bits in a class enum bitmask.
Definition bitmask.h:270