ATLAS Offline Software
Loading...
Searching...
No Matches
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*/
77
78
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
105namespace CxxUtils {
106namespace fpcompare {
107
108
113inline
114bool 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
126inline
127bool equal (float a, float b)
128{
130 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
131 return va == vb;
132}
133
134
139inline
140bool 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
152inline
153bool greater (float a, float b)
154{
156 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
157 return va > vb;
158}
159
160
165inline
166bool 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
178inline
179bool less (float a, float b)
180{
182 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
183 return va < vb;
184}
185
186
191inline
192bool 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
204inline
205bool greater_equal (float a, float b)
206{
208 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
209 return va >= vb;
210}
211
212
217inline
218bool 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
230inline
231bool less_equal (float a, float b)
232{
234 CXXUTILS_FPCOMPARE_VOLATILE float vb = b;
235 return va <= vb;
236}
237
238
239} // namespace fpcompare
240
241
242namespace fpcompare_fn {
243
244
248struct equal
249{
250 bool
251 operator()(double a, double b) const
252 { return fpcompare::equal (a, b); }
253};
254
255
259struct equalf
260{
261 bool
262 operator()(float a, float b) const
263 { return fpcompare::equal (a, b); }
264};
265
266
271{
272 bool
273 operator()(double a, double b) const
274 { return fpcompare::greater (a, b); }
275};
276
277
282{
283 bool
284 operator()(float a, float b) const
285 { return fpcompare::greater (a, b); }
286};
287
288
292struct less
293{
294 bool
295 operator()(double a, double b) const
296 { return fpcompare::less (a, b); }
297};
298
299
303struct 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
static Double_t a
#define CXXUTILS_FPCOMPARE_VOLATILE
Definition fpcompare.h:101
bool equal(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:114
bool greater(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:140
bool less(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:166
bool less_equal(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:218
bool greater_equal(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:192
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:249
bool operator()(double a, double b) const
Definition fpcompare.h:251
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:260
bool operator()(float a, float b) const
Definition fpcompare.h:262
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:315
bool operator()(double a, double b) const
Definition fpcompare.h:317
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:326
bool operator()(float a, float b) const
Definition fpcompare.h:328
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:271
bool operator()(double a, double b) const
Definition fpcompare.h:273
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:282
bool operator()(float a, float b) const
Definition fpcompare.h:284
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:337
bool operator()(double a, double b) const
Definition fpcompare.h:339
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:348
bool operator()(float a, float b) const
Definition fpcompare.h:350
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:293
bool operator()(double a, double b) const
Definition fpcompare.h:295
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:304
bool operator()(float a, float b) const
Definition fpcompare.h:306