forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBits.c
408 lines (328 loc) · 8.52 KB
/
Bits.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*ident "@(#)cls4:src/Bits.c 1.5" */
/*******************************************************************************
C++ source for the C++ Language System, Release 3.0. This product
is a new release of the original cfront developed in the computer
science research center of AT&T Bell Laboratories.
Copyright (c) 1993 UNIX System Laboratories, Inc.
Copyright (c) 1991, 1992 AT&T and UNIX System Laboratories, Inc.
Copyright (c) 1984, 1989, 1990 AT&T. All Rights Reserved.
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
Laboratories, Inc. The copyright notice above does not evidence
any actual or intended publication of such source code.
*******************************************************************************/
// Bits.c
// The numbering scheme in this library is consistently
// ``little-end'' -- bit 0 is the low-order bit of the word
// stored in the lowest location of a class.
#include "Bits.h"
Blockimplement(Bits_chunk)
Bits::Bits(register Bits_chunk val, register unsigned ct) {
if (ct < Bits_len_ATTLC) {
register Bits_chunk mask = ~Bits_chunk(0) << ct;
while (val & mask) {
ct++;
mask <<= 1;
}
}
if (size(ct))
*((Bits_chunk *) b) = val;
}
unsigned Bits::size(unsigned x) {
int newsize = bound(x);
if (b.size() != newsize)
b.size(newsize);
n = b.size() ? x : 0;
normalize();
return n;
}
Bits &Bits::operator&=(const Bits &x) {
if (size() < x.size())
size(x.size());
register Bits_chunk *p = b;
register const Bits_chunk *q = x.b;
register const Bits_chunk *lim = x.limit();
while (q < lim)
*p++ &= *q++;
lim = limit();
while (p < lim)
*p++ = 0;
return *this;
}
Bits &Bits::operator|=(const Bits &x) {
if (size() < x.size())
size(x.size());
register Bits_chunk *p = b;
register const Bits_chunk *q = x.b;
register const Bits_chunk *lim = x.limit();
while (q < lim)
*p++ |= *q++;
return *this;
}
Bits &Bits::operator^=(const Bits &x) {
if (size() < x.size())
size(x.size());
register Bits_chunk *p = b;
register const Bits_chunk *q = x.b;
register const Bits_chunk *lim = x.limit();
while (q < lim)
*p++ ^= *q++;
return *this;
}
Bits &Bits::compl() {
register Bits_chunk *p = b;
register const Bits_chunk *lim = limit();
while (p < lim) {
*p = ~*p;
p++;
}
normalize();
return *this;
}
unsigned Bits::count() const {
register const Bits_chunk *p = b;
register const Bits_chunk *lim = limit();
register unsigned r = 0;
while (p < lim) {
register unsigned long x = *p++;
register int i = Bits_len_ATTLC;
while (--i >= 0) {
if (x & 1)
r++;
x >>= 1;
}
}
return r;
}
#if 0
Bits::operator Bits_chunk() const
{
register const Bits_chunk* p = b;
register const Bits_chunk* lim = limit();
while (p < lim) {
if (*p++)
return p[-1];
}
return 0;
}
Bits
operator& (const Bits& x, const Bits& y)
{
Bits r = x;
r &= y;
return r;
}
Bits
operator| (const Bits& x, const Bits& y)
{
Bits r = x;
r |= y;
return r;
}
Bits
operator^ (const Bits& x, const Bits& y)
{
Bits r = x;
r ^= y;
return r;
}
#endif
Bits operator~(const Bits &x) {
Bits r = x;
r.compl();
return r;
}
#if 0
Bits
operator<< (const Bits& x, int n)
{
Bits r = x;
r <<= n;
return r;
}
Bits
operator>> (const Bits& x, int n)
{
Bits r = x;
r >>= n;
return r;
}
#endif
int Bits::compare(const Bits &x) const {
int w = bound(size());
int xw = bound(x.size());
int len, r;
register const Bits_chunk *p;
register const Bits_chunk *q;
register const Bits_chunk *lim;
// two null strings are equal
if (w == 0 && xw == 0)
return 0;
// a null string is the smallest string
if (w == 0)
return -1;
if (xw == 0)
return 1;
// if the lengths differ, check the high-order
// part of the longer string; leave shorter length
// in len if we get out of this
if (w != xw) {
if (w > xw) {
len = xw;
p = &b[len];
q = &b[w];
r = 1;
} else {
len = w;
p = &x.b[len];
q = &x.b[xw];
r = -1;
}
do {
if (*p++)
return r;
} while (p < q);
} else
len = w;
// now compare low-order parts, going from high-order
// end to low-order end (the direction is important!)
p = (const Bits_chunk *) b + len;
q = (const Bits_chunk *) x.b + len;
lim = b;
while (p > lim) {
if (*--p != *--q)
return *p < *q ? -1 : 1;
}
// the bits are equal -- length determines the result
return int(size()) - int(x.size());
}
// Are two bit strings identical?
int Bits::equal(const Bits &x) const {
// two strings of different sizes are not equal
if (size() != x.size())
return 0;
// two null strings are equal
if (size() == 0)
return 1;
// else go the long route
return compare(x) == 0;
}
// The following routines can surely be made more efficient.
// I have not done so for two reasons:
//
// 1. The function call and memory allocation overhead
// will probably dominate for all but the largest of
// bit strings.
//
// 2. This code is tricky. Further optimization would
// erode my confidence in getting it right.
Bits &Bits::operator<<=(int k) {
// Quick test for shift by 0 or negative
if (k <= 0) {
if (k < 0)
operator>>=(-k);
return *this;
}
// Enlarge the structure to contain the result; return on error
if (size(size() + k) == 0)
return *this;
register Bits_chunk *lim = b;
// If needed, shift left by chunks
int chunkoffset = k >> Bits_shift_ATTLC;
if (chunkoffset) {
register Bits_chunk *dest = limit();
register Bits_chunk *src = dest - chunkoffset;
do
*--dest = *--src;
while (src > lim);
do
*--dest = 0;
while (dest > lim);
}
// If needed, shift left by bits
register int bitoffset = k & Bits_mask_ATTLC;
if (bitoffset) {
register Bits_chunk *p = limit();
register int compoffset = Bits_len_ATTLC - bitoffset;
while (--p > lim)
*p = (*p << bitoffset) | (*(p - 1) >> compoffset);
// Shift low-order chunk
*lim <<= bitoffset;
}
normalize();
return *this;
}
Bits &Bits::operator>>=(int k) {
// Quick test for shift by 0 or negative
if (k <= 0) {
if (k < 0)
operator<<=(-k);
return *this;
}
// Check for shifting all significance out
int newsize = size() - k;
if (newsize <= 0) {
size(0);
return *this;
}
// If needed, shift right by chunks, leaving high-order
// garbage words that will be sized out later
int chunkoffset = k >> Bits_shift_ATTLC;
if (chunkoffset) {
register Bits_chunk *dest = b;
register Bits_chunk *src = dest + chunkoffset;
register const Bits_chunk *lim = limit();
do
*dest++ = *src++;
while (src < lim);
}
// If needed, shift right by bits
register int bitoffset = k & Bits_mask_ATTLC;
if (bitoffset) {
register Bits_chunk *p = b;
register Bits_chunk *lim = p + chunk(newsize - 1);
register int compoffset = Bits_len_ATTLC - bitoffset;
while (p < lim) {
*p = (*p >> bitoffset) | (*(p + 1) << compoffset);
p++;
}
// Shift high-order chunk right
*lim >>= bitoffset;
if (lim + 1 < limit())
*lim |= *(lim + 1) << compoffset;
}
// Finally, make the size right, discarding junk bits
size(newsize);
return *this;
}
// How many significant bits?
unsigned Bits::signif() const {
if (size() == 0)
return 0;
register const Bits_chunk *p = limit();
register const Bits_chunk *lim = b;
while (--p >= lim) {
if (*p) {
register Bits_chunk x = *p;
register int k = Bits_len_ATTLC;
while (--k >= 0) {
if (x & (Bits_chunk(1) << k))
return k + 1 + Bits_len_ATTLC * (p - lim);
}
}
}
return 0;
}
Bits &Bits::concat(const Bits &x) {
operator<<=(x.size());
operator|=(x);
return *this;
}
#if 0
Bits
concat(const Bits& x, const Bits& y)
{
Bits r = x;
r.concat(y);
return r;
}
#endif