-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbComparator.h
187 lines (156 loc) · 4.34 KB
/
wbComparator.h
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef __WB_COMPARATOR_H__
#define __WB_COMPARATOR_H__
#include <wb.h>
template <typename T>
static inline T _abs(const T & a) {
return a < 0 ? -a : a;
}
static inline wbBool _almostEqual(double A, double B, double eps) {
if (A == 0) {
return _abs(B) < eps;
} else if (B == 0) {
return _abs(A) < eps;
} else {
#if 0
double d = max(_abs(A), _abs(B));
double g = (_abs(A - B)/d);
#else
double g = _abs(A - B);
#endif
if (g <= eps) {
return wbTrue;
} else {
return wbFalse;
}
}
}
static inline wbBool _almostEqual(float A, float B, float eps) {
if (A == 0) {
return _abs(B) < eps;
} else if (B == 0) {
return _abs(A) < eps;
} else {
#if 0
float d = max(_abs(A), _abs(B));
float g = (_abs(A - B)/d);
#else
float g = _abs(A - B);
#endif
if (g <= eps) {
return wbTrue;
} else {
return wbFalse;
}
}
}
static inline wbBool _almostEqual(double A, double B) {
return _almostEqual(A, B, 0.1);
}
static inline wbBool _almostEqual(float A, float B) {
return _almostEqual(A, B, 0.1f);
}
static inline wbBool _almostEqual2sComplement(float A, float B, int maxUlps) {
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
int aInt, bInt, intDiff;
wbAssert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
int * tmp = reinterpret_cast<int *>(&A);
aInt = *tmp;
// Make aInt lexicographically ordered as a twos-complement int
if (aInt < 0) {
aInt = 0x80000000 - aInt;
}
// Make bInt lexicographically ordered as a twos-complement int
tmp = reinterpret_cast<int *>(&B);
bInt = *tmp;
if (bInt < 0) {
bInt = 0x80000000 - bInt;
}
intDiff = _abs(aInt - bInt);
if (intDiff <= maxUlps) {
return wbTrue;
}
return wbFalse;
}
static inline wbBool _almostEqual2sComplement(float A, float B) {
return _almostEqual2sComplement(A, B, 4);
}
static inline wbBool _almostEqual2sComplement(double A, double B, int maxUlps) {
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
int64_t aInt, bInt, intDiff;
wbAssert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
int64_t * tmp = reinterpret_cast<int64_t *>(&A);
aInt = *tmp;
// Make aInt lexicographically ordered as a twos-complement int
if (aInt < 0) {
aInt = 0x80000000 - aInt;
}
// Make bInt lexicographically ordered as a twos-complement int
tmp = reinterpret_cast<int64_t *>(&B);
bInt = *tmp;
if (bInt < 0) {
bInt = 0x80000000 - bInt;
}
intDiff = _abs(aInt - bInt);
if (intDiff <= maxUlps) {
return wbTrue;
}
return wbFalse;
}
static inline wbBool _almostEqual2sComplement(double A, double B) {
return _almostEqual2sComplement(A, B, 4);
}
template <typename T>
static inline int wbCompare(const T & a, const T & b) {
if (a == b) {
return 0;
} else if (a < b) {
return -1;
} else {
return 1;
}
}
template <>
inline int wbCompare(const double & a, const double & b) {
if (_almostEqual(a, b)) {
return 0;
} else if (a < b) {
return -1;
} else {
return 1;
}
}
template <>
inline int wbCompare(const float & a, const float & b) {
if (_almostEqual(a, b)) {
return 0;
} else if (a < b) {
return -1;
} else {
return 1;
}
}
template <typename T>
static inline wbBool wbEqualQ(const T & a, const T & b) {
return wbCompare(a, b) == 0;
}
template <typename T>
static inline wbBool wbUnequalQ(const T & a, const T & b) {
return wbCompare(a, b) != 0;
}
template <typename T>
static inline wbBool wbEqualQ(const T * a, const T * b, size_t n) {
size_t ii;
for (ii = 0; ii < n; ii++) {
if (wbUnequalQ(a[ii], b[ii])) {
return wbFalse;
}
}
return wbTrue;
}
template <typename T>
static inline wbBool wbUnequalQ(const T * a, const T * b, size_t n) {
return !wbEqualQ(a, b, n);
}
#endif /* __WB_COMPARATOR_H__ */