00001
00002
00003
00004
00005
00006
00013 #include "common.h"
00014 #include "bits.h"
00015
00016
00017 static Bits oneBit[8] = { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1};
00018 static Bits leftMask[8] = {0xFF, 0x7F, 0x3F, 0x1F, 0xF, 0x7, 0x3, 0x1,};
00019 static Bits rightMask[8] = {0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF,};
00020 static int inittedBitsInByte = 0;
00021 int bitsInByte[256];
00022
00023
00027 void bitsInByteInit(void)
00028 {
00029 int i;
00030
00031 if (!inittedBitsInByte)
00032 {
00033 inittedBitsInByte = 1;
00034 for (i=0; i<256; ++i)
00035 {
00036 int count = 0;
00037 if (i&1)
00038 count = 1;
00039 if (i&2)
00040 ++count;
00041 if (i&4)
00042 ++count;
00043 if (i&8)
00044 ++count;
00045 if (i&0x10)
00046 ++count;
00047 if (i&0x20)
00048 ++count;
00049 if (i&0x40)
00050 ++count;
00051 if (i&0x80)
00052 ++count;
00053 bitsInByte[i] = count;
00054 }
00055 }
00056 }
00057
00058
00059
00063 Bits *bitAlloc(int bitCount)
00064 {
00065 int byteCount = ((bitCount+7)>>3);
00066 return needLargeZeroedMem(byteCount);
00067 }
00068
00069
00070
00074 Bits *bitRealloc(Bits *b, int bitCount, int newBitCount)
00075 {
00076 int byteCount = ((bitCount+7)>>3);
00077 int newByteCount = ((newBitCount+7)>>3);
00078 return needLargeZeroedMemResize(b, byteCount, newByteCount);
00079 }
00080
00081
00082
00086 Bits *bitClone(Bits* orig, int bitCount)
00087 {
00088 int byteCount = ((bitCount+7)>>3);
00089 Bits* bits = needLargeZeroedMem(byteCount);
00090 memcpy(bits, orig, byteCount);
00091 return bits;
00092 }
00093
00094
00095
00099 void bitFree(Bits **pB)
00100 {
00101 freez(pB);
00102 }
00103
00104
00105
00109 void bitSetOne(Bits *b, int bitIx)
00110 {
00111 b[bitIx>>3] |= oneBit[bitIx&7];
00112 }
00113
00114
00115
00119 void bitClearOne(Bits *b, int bitIx)
00120 {
00121 b[bitIx>>3] &= ~oneBit[bitIx&7];
00122 }
00123
00124
00125
00129 void bitSetRange(Bits *b, int startIx, int bitCount)
00130 {
00131 int endIx = (startIx + bitCount - 1);
00132 int startByte = (startIx>>3);
00133 int endByte = (endIx>>3);
00134 int startBits = (startIx&7);
00135 int endBits = (endIx&7);
00136 int i;
00137
00138 if (startByte == endByte)
00139 {
00140 b[startByte] |= (leftMask[startBits] & rightMask[endBits]);
00141 return;
00142 }
00143 b[startByte] |= leftMask[startBits];
00144 for (i = startByte+1; i<endByte; ++i)
00145 b[i] = 0xff;
00146 b[endByte] |= rightMask[endBits];
00147 }
00148
00149
00150
00154 int bitReadOne(Bits *b, int bitIx)
00155 {
00156 return (b[bitIx>>3] & oneBit[bitIx&7]) != 0;
00157 }
00158
00159
00160
00164 int bitCountRange(Bits *b, int startIx, int bitCount)
00165 {
00166 int endIx = (startIx + bitCount - 1);
00167 int startByte = (startIx>>3);
00168 int endByte = (endIx>>3);
00169 int startBits = (startIx&7);
00170 int endBits = (endIx&7);
00171 int i;
00172 int count = 0;
00173
00174 if (!inittedBitsInByte)
00175 bitsInByteInit();
00176 if (startByte == endByte)
00177 return bitsInByte[b[startByte] & leftMask[startBits] & rightMask[endBits]];
00178 count = bitsInByte[b[startByte] & leftMask[startBits]];
00179 for (i = startByte+1; i<endByte; ++i)
00180 count += bitsInByte[b[i]];
00181 count += bitsInByte[b[endByte] & rightMask[endBits]];
00182 return count;
00183 }
00184
00185
00186
00187
00188
00189 static int bitFind(Bits *b, int startIx, int val, int bitCount)
00190 {
00191 unsigned char notByteVal = val ? 0 : 0xff;
00192 int iBit = startIx;
00193 int endByte = ((bitCount-1)>>3);
00194 int iByte;
00195
00196
00197 while (((iBit & 7) != 0) && (iBit < bitCount))
00198 {
00199 if (bitReadOne(b, iBit) == val)
00200 return iBit;
00201 iBit++;
00202 }
00203
00204
00205 iByte = (iBit >> 3);
00206 if (iByte < endByte)
00207 {
00208 while ((iByte < endByte) && (b[iByte] == notByteVal))
00209 iByte++;
00210 iBit = iByte << 3;
00211 }
00212
00213
00214 while (iBit < bitCount)
00215 {
00216 if (bitReadOne(b, iBit) == val)
00217 return iBit;
00218 iBit++;
00219 }
00220 return bitCount;
00221 }
00222
00223
00224
00228 int bitFindSet(Bits *b, int startIx, int bitCount)
00229 {
00230 return bitFind(b, startIx, 1, bitCount);
00231 }
00232
00233
00234
00238 int bitFindClear(Bits *b, int startIx, int bitCount)
00239 {
00240 return bitFind(b, startIx, 0, bitCount);
00241 }
00242
00243
00244
00248 void bitClear(Bits *b, int bitCount)
00249 {
00250 int byteCount = ((bitCount+7)>>3);
00251 zeroBytes(b, byteCount);
00252 }
00253
00254
00255
00259 void bitClearRange(Bits *b, int startIx, int bitCount)
00260 {
00261 int endIx = (startIx + bitCount - 1);
00262 int startByte = (startIx>>3);
00263 int endByte = (endIx>>3);
00264 int startBits = (startIx&7);
00265 int endBits = (endIx&7);
00266 int i;
00267
00268 if (startByte == endByte)
00269 {
00270 b[startByte] &= ~(leftMask[startBits] & rightMask[endBits]);
00271 return;
00272 }
00273 b[startByte] &= ~leftMask[startBits];
00274 for (i = startByte+1; i<endByte; ++i)
00275 b[i] = 0x00;
00276 b[endByte] &= ~rightMask[endBits];
00277 }
00278
00279
00280
00284 void bitAnd(Bits *a, Bits *b, int bitCount)
00285 {
00286 int byteCount = ((bitCount+7)>>3);
00287 while (--byteCount >= 0)
00288 {
00289 *a = (*a & *b++);
00290 a++;
00291 }
00292 }
00293
00294
00295
00299 void bitOr(Bits *a, Bits *b, int bitCount)
00300 {
00301 int byteCount = ((bitCount+7)>>3);
00302 while (--byteCount >= 0)
00303 {
00304 *a = (*a | *b++);
00305 a++;
00306 }
00307 }
00308
00309
00310
00314 void bitXor(Bits *a, Bits *b, int bitCount)
00315 {
00316 int byteCount = ((bitCount+7)>>3);
00317 while (--byteCount >= 0)
00318 {
00319 *a = (*a ^ *b++);
00320 a++;
00321 }
00322 }
00323
00324
00325
00329 void bitNot(Bits *a, int bitCount)
00330 {
00331 int byteCount = ((bitCount+7)>>3);
00332 while (--byteCount >= 0)
00333 {
00334 *a = ~*a;
00335 a++;
00336 }
00337 }
00338
00339
00340
00345 void bitPrint(Bits *a, int startIx, int bitCount)
00346 {
00347 int i;
00348 for (i = startIx; i < bitCount; i++)
00349 {
00350 if (bitReadOne(a, i))
00351 putchar('1');
00352 else
00353 putchar('0');
00354 }
00355 putchar('\n');
00356 }
00357