MWCC/compiler_and_linker/unsorted/BitVectors.c

123 lines
2.8 KiB
C
Raw Normal View History

#include "compiler/BitVectors.h"
void bitvectorcopy(UInt32 *dst, UInt32 *src, int len) {
int i = (len + 31) >> 5;
while (i--)
*(dst++) = *(src++);
}
int bitvectorchanged(UInt32 *dst, UInt32 *src, int len) {
int i = (len + 31) >> 5;
int flag = 0;
UInt32 v;
while (i--) {
v = *src;
if (*dst != v)
flag = 1;
*dst = v;
dst++;
src++;
}
return flag;
}
void bitvectorinitialize(UInt32 *vec, int len, UInt32 initval) {
int i = (len + 31) >> 5;
while (i--)
*(vec++) = initval;
}
void bitvectorintersect(UInt32 *dst, UInt32 *src, int len) {
int i = (len + 31) >> 5;
while (i--)
*(dst++) &= *(src++);
}
void bitvectorunion(UInt32 *dst, UInt32 *src, int len) {
int i = (len + 31) >> 5;
while (i--)
*(dst++) |= *(src++);
}
void bitvectordifference(UInt32 *dst, UInt32 *src, int len) {
int i = (len + 31) >> 5;
while (i--)
*(dst++) &= ~*(src++);
}
void bitvectorcomplement(UInt32 *dst, UInt32 *src, int len) {
int i = (len + 31) >> 5;
while (i--)
*(dst++) = ~*(src++);
}
int bitvectorcount(UInt32 *vec, int len) {
static unsigned char nbits[] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
};
UInt32 v;
int i = (len + 31) >> 5;
int total = 0;
while (i--) {
if ((v = *vec)) {
total += nbits[v & 0xFF];
total += nbits[(v >> 8) & 0xFF];
total += nbits[(v >> 16) & 0xFF];
total += nbits[(v >> 24) & 0xFF];
}
vec++;
}
return total;
}
int bitvectorisempty(UInt32 *vec, int len) {
int i = (len + 31) >> 5;
while (i--) {
if (*vec)
return 0;
vec++;
}
return 1;
}
int bitvectorintersectionisempty(UInt32 *a, UInt32 *b, int len) {
int i = (len + 31) >> 5;
while (i--) {
if (*a & *b)
return 0;
a++;
b++;
}
return 1;
}