summaryrefslogtreecommitdiff
path: root/libs/pbd/pbd/floating.h
blob: d7d6349a3478557fc6768c3bd8f89d053a683be4 (plain)
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
/*
    Copyright (C) 2012 Paul Davis

    This program 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 2 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/* Taken from
 * http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
 *
 * Code assumed to be in the public domain.
 */

#ifndef __libpbd__floating_h__
#define __libpbd__floating_h__

#include <stdint.h>

#include <cstdlib> // abs(int)
#include <cmath>

#include "pbd/libpbd_visibility.h"

namespace PBD {

union /*LIBPBD_API*/ Float_t
{
    Float_t (float num = 0.0f) : f(num) {}

    // Portable extraction of components.
    bool    negative() const { return (i >> 31) != 0; }
    int32_t raw_mantissa() const { return i & ((1 << 23) - 1); }
    int32_t raw_exponent() const { return (i >> 23) & 0xFF; }

    int32_t i;
    float f;
};

/* Note: ULPS = Units in the Last Place */

static inline bool floateq (float a, float b, int max_ulps_diff)
{
    Float_t ua (a);
    Float_t ub (b);

    if (a == b) {
            return true;
    }

    // Different signs means they do not match.
    if (ua.negative() != ub.negative()) {
	    return false;
    }

    // Find the difference in ULPs.
    int ulps_diff = abs (ua.i - ub.i);

    if (ulps_diff <= max_ulps_diff) {
        return true;
    }

    return false;
}

} /* namespace */

#endif /* __libpbd__floating_h__ */