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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
implement Sets;
include "sys.m";
sys: Sys;
include "sets32.m";
init()
{
sys = load Sys Sys->PATH;
}
set(): Set
{
return Set(0);
}
BITS: con 32;
MSB: con 1 << (BITS - 1);
Set.X(s1: self Set, o: int, s2: Set): Set
{
return Set(op(o, s1.s, s2.s));
}
Set.invert(s: self Set): Set
{
return Set(~s.s);
}
Set.add(s: self Set, n: int): Set
{
return Set(s.s | (1 << n));
}
Set.del(s: self Set, n: int): Set
{
return Set(s.s & ~(1 << n));
}
Set.addlist(s: self Set, ns: list of int): Set
{
for (; ns != nil; ns = tl ns)
s.s |= (1 << hd ns);
return s;
}
Set.holds(s: self Set, n: int): int
{
return s.s & (1 << n);
}
Set.str(s: self Set): string
{
msb := s.s >> (BITS - 1);
# discard all top bits that are the same as msb
t := 16rf << (BITS - 4);
sig := 8;
while (t != 0) {
if ((msb & t) != (s.s & t))
break;
sig--;
t = (t >> 4) & 16r0fffffff; # logical shift right
}
str: string;
if (sig > 0) {
top := ~MSB & s.s;
if (sig < 8) # shifting left by 32 bits is undefined.
top &= (1 << (sig << 2)) - 1;
str = sys->sprint("%.*ux", sig, top);
}
return str + ":" + string (msb & 1);
}
Set.bytes(s: self Set, n: int): array of byte
{
m := (s.limit() >> 3) + 1;
if(m > n)
n = m;
d := array[n] of byte;
case len d {
1 =>
d[0] = byte s.s;
2 =>
d[0] = byte s.s;
d[1] = byte (s.s >> 8);
3 =>
d[0] = byte s.s;
d[1] = byte (s.s >> 8);
d[2] = byte (s.s >> 16);
4 =>
d[0] = byte s.s;
d[1] = byte (s.s >> 8);
d[2] = byte (s.s >> 16);
d[3] = byte (s.s >> 24);
* =>
d[0] = byte s.s;
d[1] = byte (s.s >> 8);
d[2] = byte (s.s >> 16);
d[3] = byte (s.s >> 24);
msb := byte (s.s >> (BITS - 1)); # sign extension
for(i := 4; i < len d; i++)
d[i] = msb;
}
return d;
}
bytes2set(d: array of byte): Set
{
if(len d == 0)
return Set(0);
msb := ~(int (d[len d - 1] >> 7) - 1);
v: int;
case len d {
1 =>
v = int d[0] | (msb & int 16rffffff00);
2 =>
v = int d[0] | (int d[1] << 8) | (msb & int 16rffff0000);
3 =>
v = int d[0] | (int d[1] << 8) | (int d[2] << 16) | (msb & int 16rff000000);
* or # XXX could raise (or return) an error for len d > 4
4 =>
v = int d[0] | (int d[1] << 8) | (int d[2] << 16) | (int d[3] << 24);
}
return Set(v);
}
Set.debugstr(s: self Set): string
{
return sys->sprint("%ux", s.s);
}
Set.eq(s1: self Set, s2: Set): int
{
return s1.s == s2.s;
}
Set.isempty(s: self Set): int
{
return s.s == 0;
}
Set.msb(s: self Set): int
{
return (s.s & MSB) != 0;
}
Set.limit(s: self Set): int
{
m := s.s >> (BITS - 1); # sign extension
return topbit(s.s ^ m);
}
topbit(v: int): int
{
if (v == 0)
return 0;
(b, n, mask) := (1, 16, int 16rffff0000);
while (n != 0) {
if (v & mask) {
b += n;
v >>= n; # could return if v==0 here if we thought it worth it
}
n >>= 1;
mask >>= n;
}
return b;
}
str2set(str: string): Set
{
n := len str;
if (n < 2 || str[n - 2] != ':')
return Set(0);
c := str[n - 1];
if (c != '0' && c != '1')
return Set(0);
n -= 2;
msb := ~(c - '1');
# XXX should we give some sort of error if there
# are more bits than we can hold?
return Set((hex2int(str[0:n], msb) & ~MSB) | (msb & MSB));
}
hex2int(s: string, fill: int): int
{
n := fill;
for (i := 0; i < len s; i++) {
c := s[i];
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'a' && c <= 'f')
c -= 'a' - 10;
else if (c >= 'A' && c <= 'F')
c -= 'A' - 10;
else
c = 0;
n = (n << 4) | c;
}
return n;
}
op(o: int, a, b: int): int
{
case o & 2r1111 {
2r0000 => return 0;
2r0001 => return ~(a | b);
2r0010 => return a & ~b;
2r0011 => return ~b;
2r0100 => return ~a & b;
2r0101 => return ~a;
2r0110 => return a ^ b;
2r0111 => return ~(a & b);
2r1000 => return a & b;
2r1001 => return ~(a ^ b);
2r1010 => return a;
2r1011 => return a | ~b;
2r1100 => return b;
2r1101 => return ~a | b;
2r1110 => return a | b;
2r1111 => return ~0;
}
return 0;
}
|