-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitwiseNot.cpp
57 lines (49 loc) · 1.27 KB
/
bitwiseNot.cpp
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
#include <limits>
#include "bitwiseOperations.hh"
namespace itv
{
// computes bitwise not on UNUMervals by brute force
UInterval bfUnsignedNot(const UInterval& a)
{
// r is isEmpty by default
UInterval r{std::numeric_limits<unsigned int>::max(), 0};
for (unsigned int i = a.lo; i <= a.hi; ++i) {
unsigned int ni = ~i;
if (ni < r.lo) r.lo = ni;
if (ni > r.hi) r.hi = ni;
}
return r;
}
// computes bitwise not on UNUMervals by brute force
SInterval bfSignedNot(const SInterval& a)
{
// r is isEmpty by default
SInterval r = SEMPTY;
for (int i = a.lo; i <= a.hi; ++i) {
int ni = ~i;
if (ni < r.lo) r.lo = ni;
if (ni > r.hi) r.hi = ni;
}
return r;
}
bool testUnsignedNot(const UInterval& a)
{
UInterval r1 = bfUnsignedNot(a);
UInterval r2 = bitwiseUnsignedNot(a);
if (r1 != r2) {
std::cout << "ERROR: " << a << " -> " << r1 << " != " << r2 << std::endl;
return false;
}
return true;
}
bool testSignedNot(const SInterval& a)
{
SInterval r1 = bfSignedNot(a);
SInterval r2 = bitwiseSignedNot(a);
if (r1 != r2) {
std::cout << "ERROR: " << a << " -> " << r1 << " != " << r2 << std::endl;
return false;
}
return true;
}
} // namespace itv