-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSafeFloat.hpp
More file actions
165 lines (147 loc) · 5.88 KB
/
Copy pathSafeFloat.hpp
File metadata and controls
165 lines (147 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//=============================================================================
// TimedPetriNetEditor: A timed Petri net editor.
// Copyright 2021 -- 2026 Quentin Quadrat <lecrapouille@gmail.com>
//
// This file is part of TimedPetriNetEditor.
//
// TimedPetriNetEditor is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
//=============================================================================
#ifndef SAFE_FLOAT_HPP
# define SAFE_FLOAT_HPP
# include <cstdint>
# include <cstring>
# include <limits>
// *****************************************************************************
//! \file SafeFloat.hpp
//! \brief Floating-point classification helpers that stay correct even when the
//! whole project is compiled with -ffast-math.
//!
//! The build flags (see .makefile/rules/Makefile, PERFORMANCE_FLAGS) enable
//! -ffast-math, which implies -ffinite-math-only. Under this assumption the
//! optimizer considers that NaN and +/-Inf never occur, so std::isnan() and
//! std::isinf() are constant-folded to false and direct comparisons such as
//! `x == -inf` become unreliable.
//!
//! The helpers below inspect the raw IEEE-754 bit pattern instead, which the
//! optimizer cannot reason away, so they keep working whatever the math flags.
//! Use them everywhere a special value must be detected (e.g. the (max,+) zero
//! %0 == -inf, or the "no duration yet" NaN sentinel of Place -> Transition
//! arcs).
// *****************************************************************************
namespace tpne {
//------------------------------------------------------------------------------
//! \brief Quiet NaN constant robust to -ffast-math / -Wnan-infinity-disabled (float).
inline float safeNaNF()
{
float value;
uint32_t const bits = 0x7FC00000u;
std::memcpy(&value, &bits, sizeof(value));
return value;
}
//------------------------------------------------------------------------------
//! \brief NaN test robust to -ffast-math (32-bit IEEE-754).
inline bool safeIsNaN(float value)
{
uint32_t bits;
static_assert(sizeof(bits) == sizeof(value), "float must be 32-bit IEEE-754");
std::memcpy(&bits, &value, sizeof(bits));
return ((bits & 0x7F800000u) == 0x7F800000u) && ((bits & 0x007FFFFFu) != 0u);
}
//------------------------------------------------------------------------------
//! \brief NaN test robust to -ffast-math (64-bit IEEE-754).
inline bool safeIsNaN(double value)
{
uint64_t bits;
static_assert(sizeof(bits) == sizeof(value), "double must be 64-bit IEEE-754");
std::memcpy(&bits, &value, sizeof(bits));
return ((bits & 0x7FF0000000000000ULL) == 0x7FF0000000000000ULL) &&
((bits & 0x000FFFFFFFFFFFFFULL) != 0ULL);
}
//------------------------------------------------------------------------------
//! \brief -Inf test robust to -ffast-math (32-bit IEEE-754).
inline bool safeIsNegInf(float value)
{
uint32_t bits;
std::memcpy(&bits, &value, sizeof(bits));
return bits == 0xFF800000u;
}
//------------------------------------------------------------------------------
//! \brief -Inf test robust to -ffast-math (64-bit IEEE-754).
inline bool safeIsNegInf(double value)
{
uint64_t bits;
std::memcpy(&bits, &value, sizeof(bits));
return bits == 0xFFF0000000000000ULL;
}
//------------------------------------------------------------------------------
//! \brief +Inf test robust to -ffast-math (32-bit IEEE-754).
inline bool safeIsPosInf(float value)
{
uint32_t bits;
std::memcpy(&bits, &value, sizeof(bits));
return bits == 0x7F800000u;
}
//------------------------------------------------------------------------------
//! \brief +Inf test robust to -ffast-math (64-bit IEEE-754).
inline bool safeIsPosInf(double value)
{
uint64_t bits;
std::memcpy(&bits, &value, sizeof(bits));
return bits == 0x7FF0000000000000ULL;
}
//------------------------------------------------------------------------------
//! \brief +/-Inf test robust to -ffast-math.
template<typename T>
inline bool safeIsInf(T value)
{
return safeIsNegInf(value) || safeIsPosInf(value);
}
//------------------------------------------------------------------------------
//! \brief -Inf constant robust to -ffast-math / -Wnan-infinity-disabled (float).
inline float safeNegInfF()
{
float value;
uint32_t const bits = 0xFF800000u;
std::memcpy(&value, &bits, sizeof(value));
return value;
}
//------------------------------------------------------------------------------
//! \brief +Inf constant robust to -ffast-math / -Wnan-infinity-disabled (float).
inline float safePosInfF()
{
float value;
uint32_t const bits = 0x7F800000u;
std::memcpy(&value, &bits, sizeof(value));
return value;
}
//------------------------------------------------------------------------------
//! \brief -Inf constant robust to -ffast-math / -Wnan-infinity-disabled (double).
inline double safeNegInf()
{
double value;
uint64_t const bits = 0xFFF0000000000000ULL;
std::memcpy(&value, &bits, sizeof(value));
return value;
}
//------------------------------------------------------------------------------
//! \brief +Inf constant robust to -ffast-math / -Wnan-infinity-disabled (double).
inline double safePosInf()
{
double value;
uint64_t const bits = 0x7FF0000000000000ULL;
std::memcpy(&value, &bits, sizeof(value));
return value;
}
} // namespace tpne
#endif // SAFE_FLOAT_HPP