more vst parameter related stuff
[ardour.git] / libs / pbd / pbd / floating.h
1 /* Taken from
2  * http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
3  *
4  * Code assumed to be in the public domain.
5  */
6
7 #ifndef __libpbd__floating_h__
8 #define __libpbd__floating_h__
9
10 #include <cmath>
11
12 namespace PBD {
13
14 union Float_t
15 {
16     Float_t (float num = 0.0f) : f(num) {}
17
18     // Portable extraction of components.
19     bool    negative() const { return (i >> 31) != 0; }
20     int32_t raw_mantissa() const { return i & ((1 << 23) - 1); }
21     int32_t raw_exponent() const { return (i >> 23) & 0xFF; }
22  
23     int32_t i;
24     float f;
25 };
26  
27 /* Note: ULPS = Units in the Last Place */
28
29 static inline bool floateq (float a, float b, int max_ulps_diff)
30 {
31     Float_t ua (a);
32     Float_t ub (b);
33  
34     if (a == b) {
35             return true;
36     }
37
38     // Different signs means they do not match.
39     if (ua.negative() != ub.negative()) {
40             return false;
41     }
42
43     // Find the difference in ULPs.
44     int ulps_diff = abs (ua.i - ub.i);
45
46     if (ulps_diff <= max_ulps_diff) {
47         return true;
48     }
49  
50     return false;
51 }
52
53 } /* namespace */
54
55 #endif /* __libpbd__floating_h__ */