-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.cc
73 lines (65 loc) · 1.23 KB
/
analyzer.cc
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
#include "gen2.h"
#include "analyzer.h"
#include <sstream>
using std::stringstream;
Val Analyzer::shift(Val x, int s)
{
if (s > 63 || s < -63) return 0;
return s > 0 ? x << s : x >> (-s);
}
int Analyzer::distance(Val x, Val y)
{
int min_distance = 111;
for (int s = -64; s <= 64; s++) {
int d = not_distance(shift(x, s), y);
if (d < min_distance)
min_distance = d;
}
return min_distance;
}
int Analyzer::not_distance(Val x, Val y)
{
int d = base_distance(x, y);
int n = base_distance(~x, y);
return d < n ? d : n;
}
int Analyzer::base_distance(Val x, Val y)
{
Val v = x ^ y;
// do the bit count
int count;
for (count = 0; v; count++) {
v = (v - 1) & v;
}
return count;
}
string Analyzer::sdist(Val x, Val y)
{
stringstream ss;
int min_distance = 111;
for (int s = -64; s <= 64; s++) {
int d = not_distance(shift(x, s), y);
if (s == 0)
ss << '|';
if (d == 0)
ss << '.';
else if (d < 10)
ss << d;
else if (d <= 36)
ss << (char)('a' + d - 10);
else
ss << '*';
if (s == 0)
ss << '|';
}
return ss.str();
}
#ifdef AMAIN
int main()
{
Analyzer a;
int d = a.distance(0xb445fbb8cddcf9f8, 0xF0F0F0F0B445FBB8);
printf("min_distance: %d\n", d);
return 0;
}
#endif