Monado OpenXR Runtime
hg_numerics_checker.hpp
Go to the documentation of this file.
1// Copyright 2021-2022, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Simple util for setting floating-point exceptions and checking for NaNs.
6 * @author Moses Turner <moses@collabora.com>
7 * @ingroup drv_ht
8 */
9
10#undef PEDANTIC_NAN_CHECKS
11#undef NAN_EXCEPTIONS
12
13#ifdef NAN_EXCEPTIONS
14#include <cfenv>
15#endif
16
17namespace xrt::tracking::hand::mercury::numerics_checker {
18
19#ifdef PEDANTIC_NAN_CHECKS
20#define CHECK_NOT_NAN(val) \
21 do { \
22 if (val != val) { \
23 U_LOG_E(" was NAN at %d", __LINE__); \
24 assert(false); \
25 } \
26 \
27 } while (0)
28
29#else
30#define CHECK_NOT_NAN(val) (void)val;
31#endif
32
33#ifdef NAN_EXCEPTIONS
34static int ex = FE_DIVBYZERO | FE_OVERFLOW | FE_INVALID;
35
36
37static inline void
38set_floating_exceptions()
39{
40 // NO: FE_UNDERFLOW, FE_INEXACT
41 // https://stackoverflow.com/questions/60731382/c-setting-floating-point-exception-environment
42 feenableexcept(ex); // Uncomment this for version 2
43}
44
45static inline void
46remove_floating_exceptions()
47{
48 fedisableexcept(ex);
49}
50
51#else
52static inline void
53set_floating_exceptions()
54{}
55
56static inline void
57remove_floating_exceptions()
58{}
59#endif
60
61
62} // namespace xrt::tracking::hand::mercury::numerics_checker