ATLAS Offline Software
fpcompare.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 
3 /*
4  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 */
79 #include <cmath>
80 #include <functional>
81 
82 
83 #ifndef CXXUTILS_FPCOMPARE_H
84 #define CXXUTILS_FPCOMPARE_H
85 
86 
87 // Decide whether we need to use volatile or not.
88 #if defined(__FLT_EVAL_METHOD__) && \
89  (__FLT_EVAL_METHOD__ == 2 || __FLT_EVAL_METHOD__ < 0)
90  // __FLT_EVAL_METHOD__ < 0 means unspecified.
91  // Be pessimistic in that case.
92 # define CXXUTILS_FPCOMPARE_VOLATILE volatile
93 #elif defined(__i386__) && !defined(__SSE2__)
94  // On x86, gcc -msse -mfpmath=sse is observed to _not_ generate
95  // sse fp instructions, but does set __FLT_EVAL_METHOD__ to 0.
96  // -msse2 -mfpmath=sse does seem to work as expected.
97  // Special-case this for now; should follow up with a gcc bug report
98  // if this still happens in current releases.
99 # define CXXUTILS_FPCOMPARE_VOLATILE volatile
100 #else
101 # define CXXUTILS_FPCOMPARE_VOLATILE
102 #endif
103 
104 
105 namespace CxxUtils {
106 namespace fpcompare {
107 
108 
113 inline
114 bool equal (double a, double b)
115 {
116  CXXUTILS_FPCOMPARE_VOLATILE double va = a;
117  CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
118  return va == vb;
119 }
120 
121 
126 inline
127 bool equal (float a, float b)
128 {
129  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
130  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
131  return va == vb;
132 }
133 
134 
139 inline
140 bool greater (double a, double b)
141 {
142  CXXUTILS_FPCOMPARE_VOLATILE double va = a;
143  CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
144  return va > vb;
145 }
146 
147 
152 inline
153 bool greater (float a, float b)
154 {
155  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
156  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
157  return va > vb;
158 }
159 
160 
165 inline
166 bool less (double a, double b)
167 {
168  CXXUTILS_FPCOMPARE_VOLATILE double va = a;
169  CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
170  return va < vb;
171 }
172 
173 
178 inline
179 bool less (float a, float b)
180 {
181  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
182  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
183  return va < vb;
184 }
185 
186 
191 inline
192 bool greater_equal (double a, double b)
193 {
194  CXXUTILS_FPCOMPARE_VOLATILE double va = a;
195  CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
196  return va >= vb;
197 }
198 
199 
204 inline
205 bool greater_equal (float a, float b)
206 {
207  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
208  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
209  return va >= vb;
210 }
211 
212 
217 inline
218 bool less_equal (double a, double b)
219 {
220  CXXUTILS_FPCOMPARE_VOLATILE double va = a;
221  CXXUTILS_FPCOMPARE_VOLATILE double vb = b;
222  return va <= vb;
223 }
224 
225 
230 inline
231 bool less_equal (float a, float b)
232 {
233  CXXUTILS_FPCOMPARE_VOLATILE float va = a;
234  CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
235  return va <= vb;
236 }
237 
238 
239 } // namespace fpcompare
240 
241 
242 namespace fpcompare_fn {
243 
244 
248 struct equal
249 {
250  bool
251  operator()(double a, double b) const
252  { return fpcompare::equal (a, b); }
253 };
254 
255 
259 struct equalf
260 {
261  bool
262  operator()(float a, float b) const
263  { return fpcompare::equal (a, b); }
264 };
265 
266 
270 struct greater
271 {
272  bool
273  operator()(double a, double b) const
274  { return fpcompare::greater (a, b); }
275 };
276 
277 
281 struct greaterf
282 {
283  bool
284  operator()(float a, float b) const
285  { return fpcompare::greater (a, b); }
286 };
287 
288 
292 struct less
293 {
294  bool
295  operator()(double a, double b) const
296  { return fpcompare::less (a, b); }
297 };
298 
299 
303 struct lessf
304 {
305  bool
306  operator()(float a, float b) const
307  { return fpcompare::less (a, b); }
308 };
309 
310 
315 {
316  bool
317  operator()(double a, double b) const
318  { return fpcompare::greater_equal (a, b); }
319 };
320 
321 
326 {
327  bool
328  operator()(float a, float b) const
329  { return fpcompare::greater_equal (a, b); }
330 };
331 
332 
337 {
338  bool
339  operator()(double a, double b) const
340  { return fpcompare::less_equal (a, b); }
341 };
342 
343 
348 {
349  bool
350  operator()(float a, float b) const
351  { return fpcompare::less_equal (a, b); }
352 };
353 
354 
355 } // namespace fpcompare_fn
356 } // namespace CxxUtils
357 
358 
359 #endif // not CXXUTILS_FPCOMPARE_H
CxxUtils::fpcompare_fn::equal
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:249
CxxUtils::fpcompare_fn::greater_equal
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:315
CxxUtils::fpcompare_fn::less_equalf::operator()
bool operator()(float a, float b) const
Definition: fpcompare.h:350
CxxUtils::fpcompare_fn::less_equal
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:337
CxxUtils::fpcompare_fn::greater::operator()
bool operator()(double a, double b) const
Definition: fpcompare.h:273
CxxUtils::fpcompare_fn::less
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:293
CxxUtils::fpcompare_fn::less_equal::operator()
bool operator()(double a, double b) const
Definition: fpcompare.h:339
CxxUtils::fpcompare::greater
bool greater(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:140
CxxUtils::fpcompare::equal
bool equal(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:114
CxxUtils::fpcompare_fn::greaterf::operator()
bool operator()(float a, float b) const
Definition: fpcompare.h:284
CxxUtils
Definition: aligned_vector.h:29
CxxUtils::fpcompare_fn::greater_equalf::operator()
bool operator()(float a, float b) const
Definition: fpcompare.h:328
CxxUtils::fpcompare_fn::lessf::operator()
bool operator()(float a, float b) const
Definition: fpcompare.h:306
CxxUtils::fpcompare_fn::greater_equalf
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:326
CxxUtils::fpcompare_fn::lessf
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:304
CxxUtils::fpcompare_fn::greaterf
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:282
CxxUtils::fpcompare::greater_equal
bool greater_equal(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:192
CxxUtils::fpcompare_fn::equalf
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:260
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
CxxUtils::fpcompare_fn::greater
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:271
CxxUtils::fpcompare_fn::equalf::operator()
bool operator()(float a, float b) const
Definition: fpcompare.h:262
CxxUtils::fpcompare::less
bool less(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:166
CxxUtils::fpcompare_fn::greater_equal::operator()
bool operator()(double a, double b) const
Definition: fpcompare.h:317
CxxUtils::fpcompare::less_equal
bool less_equal(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:218
CxxUtils::fpcompare_fn::less_equalf
Compare two FP numbers, working around x87 precision issues.
Definition: fpcompare.h:348
CxxUtils::fpcompare_fn::equal::operator()
bool operator()(double a, double b) const
Definition: fpcompare.h:251
a
TList * a
Definition: liststreamerinfos.cxx:10
CXXUTILS_FPCOMPARE_VOLATILE
#define CXXUTILS_FPCOMPARE_VOLATILE
Definition: fpcompare.h:101
CxxUtils::fpcompare_fn::less::operator()
bool operator()(double a, double b) const
Definition: fpcompare.h:295