forked from triblerteam/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.cpp
169 lines (129 loc) · 2.69 KB
/
bin.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
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
/*
* bin.cpp
* swift
*
* Created by Victor Grishchenko on 10/10/09.
* Reimplemented by Alexander G. Pronchenkov on 05/05/10
*
* Copyright 2010 Delft University of Technology. All rights reserved.
*
*/
#include "bin.h"
#include <ostream>
const bin_t bin_t::NONE(8 * sizeof(bin_t::uint_t), 0);
const bin_t bin_t::ALL(8 * sizeof(bin_t::uint_t) - 1, 0);
/* Methods */
/**
* Gets the layer value of a bin
*/
int bin_t::layer(void) const
{
if (is_none()) {
return -1;
}
int r = 0;
#ifdef _MSC_VER
# pragma warning (push)
# pragma warning (disable:4146)
#endif
register uint_t tail;
tail = v_ + 1;
tail = tail & (-tail);
#ifdef _MSC_VER
# pragma warning (pop)
#endif
if (tail > 0x80000000U) {
r = 32;
tail >>= 16; // FIXME: hide warning
tail >>= 16;
}
// courtesy of Sean Eron Anderson
// http://graphics.stanford.edu/~seander/bithacks.html
static const char DeBRUIJN[32] = { 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 };
return r + DeBRUIJN[ 0x1f & ((tail * 0x077CB531U) >> 27) ];
}
/* String operations */
namespace {
char* append(char* buf, int x)
{
char* l = buf;
char* r = buf;
if (x < 0) {
*r++ = '-';
x = -x;
}
do {
*r++ = '0' + x % 10;
x /= 10;
} while (x);
char* e = r--;
while (l < r) {
const char t = *l;
*l++ = *r;
*r-- = t;
}
*e = '\0';
return e;
}
char* append(char* buf, bin_t::uint_t x)
{
char* l = buf;
char* r = buf;
do {
*r++ = '0' + x % 10;
x /= 10;
} while (x);
char* e = r--;
while (l < r) {
const char t = *l;
*l++ = *r;
*r-- = t;
}
*e = '\0';
return e;
}
char* append(char* buf, const char* s)
{
char* e = buf;
while (*s) {
*e++ = *s++;
}
*e = '\0';
return e;
}
char* append(char* buf, char c)
{
char* e = buf;
*e++ = c;
*e = '\0';
return e;
}
} /* namespace */
/**
* Get the standard-form of this bin, e.g. "(2,1)".
* (buffer should have enough of space)
*/
const char* bin_t::str(char* buf) const
{
char* e = buf;
if (is_all()) {
e = append(e, "(ALL)");
} else if (is_none()) {
e = append(e, "(NONE)");
} else {
e = append(e, '(');
e = append(e, layer());
e = append(e, ',');
e = append(e, layer_offset());
e = append(e, ')');
}
return buf;
}
/**
* Output operator
*/
std::ostream & operator << (std::ostream & ostream, const bin_t & bin)
{
char bin_name_buf[64];
return ostream << bin.str(bin_name_buf);
}