21 #include <ndb_global.h>
23 #ifdef HAVE__BITSCANFORWARD
33 STATIC_CONST( NotFound = (
unsigned)-1 );
38 static bool get(
unsigned size,
const Uint32 data[],
unsigned n);
43 static void set(
unsigned size, Uint32 data[],
unsigned n,
bool value);
48 static void set(
unsigned size, Uint32 data[],
unsigned n);
53 static void set(
unsigned size, Uint32 data[]);
58 static void setRange(
unsigned size, Uint32 data[],
unsigned start,
64 static void assign(
unsigned size, Uint32 dst[],
const Uint32 src[]);
69 static void clear(
unsigned size, Uint32 data[],
unsigned n);
74 static void clear(
unsigned size, Uint32 data[]);
79 static void clear_range(
unsigned size, Uint32 data[],
unsigned start,
unsigned last);
81 static Uint32 getWord(
unsigned size, Uint32 data[],
unsigned word_pos);
82 static void setWord(
unsigned size, Uint32 data[],
83 unsigned word_pos, Uint32 new_word);
88 static bool isclear(
unsigned size,
const Uint32 data[]);
93 static unsigned count(
unsigned size,
const Uint32 data[]);
99 static unsigned ffs(Uint32 x);
105 static unsigned find_first(
unsigned size,
const Uint32 data[]);
111 static unsigned find_next(
unsigned size,
const Uint32 data[],
unsigned n);
117 static unsigned find(
unsigned size,
const Uint32 data[],
unsigned n);
122 static bool equal(
unsigned size,
const Uint32 data[],
const Uint32 data2[]);
127 static void bitOR(
unsigned size, Uint32 data[],
const Uint32 data2[]);
132 static void bitAND(
unsigned size, Uint32 data[],
const Uint32 data2[]);
137 static void bitANDC(
unsigned size, Uint32 data[],
const Uint32 data2[]);
142 static void bitXOR(
unsigned size, Uint32 data[],
const Uint32 data2[]);
147 static void bitXORC(
unsigned size, Uint32 data[],
const Uint32 data2[]);
152 static void bitNOT(
unsigned size, Uint32 data[]);
157 static bool contains(
unsigned size, Uint32 data[],
const Uint32 data2[]);
162 static bool overlaps(
unsigned size, Uint32 data[],
const Uint32 data2[]);
167 static Uint32
getField(
unsigned size,
const Uint32 data[],
168 unsigned pos,
unsigned len);
174 static void setField(
unsigned size, Uint32 data[],
175 unsigned pos,
unsigned len, Uint32 val);
182 static void getField(
unsigned size,
const Uint32 data[],
183 unsigned pos,
unsigned len, Uint32 dst[]);
188 static void setField(
unsigned size, Uint32 data[],
189 unsigned pos,
unsigned len,
const Uint32 src[]);
196 static void copyField(Uint32 dst[],
unsigned destPos,
197 const Uint32 src[],
unsigned srcPos,
unsigned len);
202 static char*
getText(
unsigned size,
const Uint32 data[],
char*
buf);
205 static inline Uint32 count_bits(Uint32 x);
210 static Uint32
toArray(Uint8* dst, Uint32 len,
211 unsigned size,
const Uint32 data[]);
213 static void getFieldImpl(
const Uint32 data[],
unsigned,
unsigned, Uint32 []);
214 static void setFieldImpl(Uint32 data[],
unsigned,
unsigned,
const Uint32 []);
220 assert(n < (size << 5));
221 return (data[n >> 5] & (1 << (n & 31))) != 0;
227 value ?
set(
size, data,
n) :
clear(size, data, n);
233 assert(n < (size << 5));
234 data[n >> 5] |= (1 << (n & 31));
240 for (
unsigned i = 0;
i <
size;
i++) {
247 unsigned start,
unsigned len)
249 Uint32 last = start + len - 1;
250 Uint32 *ptr = data + (start >> 5);
251 Uint32 *end = data + (last >> 5);
252 assert(start <= last);
253 assert(last < (size << 5));
255 Uint32 tmp_word = ~(Uint32)0 << (start & 31);
259 * ptr ++ |= tmp_word;
263 * ptr ++ = ~(Uint32)0;
266 tmp_word = ~(Uint32)0;
269 tmp_word &= ~(~(Uint32)0 << (last & 31));
277 for (
unsigned i = 0;
i <
size;
i++) {
285 assert(n < (size << 5));
286 data[n >> 5] &= ~(1 << (n & 31));
292 for (
unsigned i = 0;
i <
size;
i++) {
299 unsigned start,
unsigned last)
301 Uint32 *ptr = data + (start >> 5);
302 Uint32 *end = data + (last >> 5);
303 assert(start <= last);
304 assert(last < (size << 5));
306 Uint32 tmp_word = ~(Uint32)0 << (start & 31);
310 * ptr ++ &= ~tmp_word;
317 tmp_word = ~(Uint32)0;
320 tmp_word &= ~(~(Uint32)0 << (last & 31));
327 BitmaskImpl::getWord(
unsigned size, Uint32 data[],
unsigned word_pos)
329 return data[word_pos];
333 BitmaskImpl::setWord(
unsigned size, Uint32 data[],
334 unsigned word_pos, Uint32 new_word)
336 data[word_pos] = new_word;
343 for (
unsigned i = 0;
i <
size;
i++) {
354 for (
unsigned i = 0;
i <
size;
i++) {
355 cnt += count_bits(data[
i]);
368 #if defined(__GNUC__) && (defined(__x86_64__) || defined (__i386__))
373 #elif defined HAVE___BUILTIN_FFS
377 return __builtin_ffs(x) - 1;
378 #elif defined HAVE__BITSCANFORWARD
380 unsigned char res = _BitScanForward(&r, (
unsigned long)x);
383 #elif defined HAVE_FFS
420 while (n < (size << 5))
422 Uint32 val = data[n >> 5];
435 Uint32 val = data[n >> 5];
447 while (n < (size << 5))
468 for (
unsigned i = 0;
i <
size;
i++) {
469 if (data[
i] != data2[
i])
478 for (
unsigned i = 0;
i <
size;
i++) {
486 for (
unsigned i = 0;
i <
size;
i++) {
494 for (
unsigned i = 0;
i <
size;
i++) {
495 data[
i] &= ~data2[
i];
502 for (
unsigned i = 0;
i <
size;
i++) {
510 for (
unsigned i = 0;
i <
size;
i++) {
511 data[
i] ^= ~data2[
i];
518 for (
unsigned i = 0;
i <
size;
i++) {
526 for (
unsigned int i = 0;
i <
size;
i++)
527 if ((data[
i] & data2[
i]) != data2[i])
535 for (
unsigned int i = 0;
i <
size;
i++)
536 if ((data[
i] & data2[
i]) != 0)
543 unsigned pos,
unsigned len)
546 for (
unsigned i = 0;
i < len;
i++)
547 val |=
get(size, data, pos +
i) <<
i;
553 unsigned pos,
unsigned len, Uint32 val)
555 for (
unsigned i = 0;
i < len;
i++)
556 set(size, data, pos +
i, val & (1 <<
i));
563 const char*
const hex =
"0123456789abcdef";
564 for (
int i = (size-1);
i >= 0;
i--) {
566 for (
unsigned j = 0; j < 8; j++) {
567 buf[7-j] = hex[x & 0xf];
578 BitmaskImpl::count_bits(Uint32 x)
580 x= x - ((x>>1) & 0x55555555);
581 x= (x & 0x33333333) + ((x>>2) & 0x33333333);
582 x= (x + (x>>4)) & 0x0f0f0f0f;
583 x= (x*0x01010101) >> 24;
590 unsigned size,
const Uint32 * data)
592 assert(len >= size * 32);
593 assert(32 * size <= 256);
595 for (Uint32
i = 0;
i<
size;
i++)
597 Uint32 val = * data++;
601 if (val & (1 << bit))
603 * dst++ = 32 *
i + bit;
609 return (Uint32)(dst - save);
618 template <
unsigned size>
636 STATIC_CONST( Size =
size );
637 STATIC_CONST( NotFound = BitmaskImpl::NotFound );
638 STATIC_CONST( TextLength =
size * 8 );
640 Uint32 getSizeInWords()
const {
return Size;}
642 unsigned max_size()
const {
return (
size * 32) - 1; }
652 static void assign(Uint32 dst[],
const Uint32 src[]);
659 void copyto(
unsigned sz, Uint32 dst[])
const;
664 void assign(
unsigned sz,
const Uint32 src[]);
672 static bool get(
const Uint32 data[],
unsigned n);
673 bool get(
unsigned n)
const;
678 static void set(Uint32 data[],
unsigned n,
bool value);
679 void set(
unsigned n,
bool value);
684 static void set(Uint32 data[],
unsigned n);
685 void set(
unsigned n);
690 static void set(Uint32 data[]);
696 static void setRange(Uint32 data[], Uint32 pos, Uint32 len);
697 void setRange(Uint32 pos, Uint32 len);
702 static void clear(Uint32 data[],
unsigned n);
708 static void clear(Uint32 data[]);
714 Uint32
getWord(
unsigned word_pos);
715 void setWord(
unsigned word_pos, Uint32 new_word);
721 static bool isclear(
const Uint32 data[]);
727 static unsigned count(
const Uint32 data[]);
728 unsigned count()
const;
734 static unsigned find_first(
const Uint32 data[]);
741 static unsigned find_next(
const Uint32 data[],
unsigned n);
748 static unsigned find(
const Uint32 data[],
unsigned n);
749 unsigned find(
unsigned n)
const;
754 static bool equal(
const Uint32 data[],
const Uint32 data2[]);
760 static void bitOR(Uint32 data[],
const Uint32 data2[]);
766 static void bitAND(Uint32 data[],
const Uint32 data2[]);
772 static void bitANDC(Uint32 data[],
const Uint32 data2[]);
778 static void bitXOR(Uint32 data[],
const Uint32 data2[]);
784 static void bitXORC(Uint32 data[],
const Uint32 data2[]);
790 static void bitNOT(Uint32 data[]);
796 static bool contains(Uint32 data[],
const Uint32 data2[]);
802 static bool overlaps(Uint32 data[],
const Uint32 data2[]);
808 static char*
getText(
const Uint32 data[],
char*
buf);
811 static Uint32 toArray(Uint8 * dst, Uint32 len,
const Uint32 data[]);
812 Uint32 toArray(Uint8 * dst, Uint32 len)
const;
815 template <
unsigned size>
822 template <
unsigned size>
829 template <
unsigned size>
836 template <
unsigned size>
843 template <
unsigned size>
850 template <
unsigned size>
857 template <
unsigned size>
864 template <
unsigned size>
871 template <
unsigned size>
878 template <
unsigned size>
885 template <
unsigned size>
892 template <
unsigned size>
899 template <
unsigned size>
906 template <
unsigned size>
913 template <
unsigned size>
921 template <
unsigned size>
928 template <
unsigned size>
935 template <
unsigned size>
942 template <
unsigned size>
949 template <
unsigned size>
956 template <
unsigned size>
960 return BitmaskImpl::getWord(
size, rep.data, word_pos);
963 template <
unsigned size>
967 BitmaskImpl::setWord(
size, rep.data, word_pos, new_word);
970 template <
unsigned size>
977 template <
unsigned size>
984 template <
unsigned size>
991 template <
unsigned size>
998 template <
unsigned size>
1005 template <
unsigned size>
1012 template <
unsigned size>
1019 template <
unsigned size>
1026 template <
unsigned size>
1030 return find_next(data, n);
1033 template <
unsigned size>
1037 return find_next(n);
1040 template <
unsigned size>
1047 template <
unsigned size>
1054 template <
unsigned size>
1061 template <
unsigned size>
1069 template <
unsigned size>
1076 template <
unsigned size>
1084 template <
unsigned size>
1091 template <
unsigned size>
1099 template <
unsigned size>
1106 template <
unsigned size>
1114 template <
unsigned size>
1121 template <
unsigned size>
1129 template <
unsigned size>
1136 template <
unsigned size>
1144 template <
unsigned size>
1151 template <
unsigned size>
1158 template <
unsigned size>
1165 template <
unsigned size>
1172 template <
unsigned size>
1179 template <
unsigned size>
1186 template <
unsigned size>
1194 template <
unsigned size>
1202 template <
unsigned size>
1210 for (
unsigned i = 0;
i < sz2;
i++)
1212 this->rep.data[
i] = src.rep.data[
i];
1217 assert(src.
find(32*
size+1) == BitmaskImpl::NotFound);
1218 for (
unsigned i = 0;
i <
size;
i++)
1220 this->rep.data[
i] = src.rep.data[
i];
1229 for (
unsigned i = 0;
i < sz2;
i++)
1231 this->rep.data[
i] = src.rep.data[
i];
1236 assert(src.
find(32*
size+1) == BitmaskImpl::NotFound);
1237 for (
unsigned i = 0;
i <
size;
i++)
1239 this->rep.data[
i] = src.rep.data[
i];
1248 unsigned pos,
unsigned len, Uint32 dst[])
1250 assert(pos + len <= (size << 5));
1256 Uint32
offset = pos & 31;
1257 * dst = (* src >>
offset) & (len >= 32 ? ~0 : (1 << len) - 1);
1259 if(offset + len <= 32)
1263 Uint32 used = (32 -
offset);
1265 getFieldImpl(src+1, used & 31, len-used, dst+(used >> 5));
1270 unsigned pos,
unsigned len,
const Uint32 src[])
1272 assert(pos + len <= (size << 5));
1278 Uint32
offset = pos & 31;
1279 Uint32 mask = (len >= 32 ? ~0 : (1 << len) - 1) <<
offset;
1281 * dst = (* dst & ~mask) | ((*src << offset) & mask);
1283 if(offset + len <= 32)
1287 Uint32 used = (32 -
offset);
1289 setFieldImpl(dst+1, used & 31, len-used, src+(used >> 5));
1293 inline unsigned minLength(
unsigned a,
unsigned b,
unsigned c)
1302 const Uint32 _src[],
unsigned srcPos,
unsigned len)
1317 const Uint32* src= _src + (srcPos >> 5);
1318 Uint32* dst= _dst + (dstPos >> 5);
1319 unsigned srcOffset= srcPos & 31;
1320 unsigned dstOffset= dstPos & 31;
1321 unsigned srcBitsInWord= 32 - srcOffset;
1322 unsigned dstBitsInWord= 32 - dstOffset;
1325 unsigned bits= minLength(dstBitsInWord, srcBitsInWord, len);
1328 Uint32 destMask= (~(Uint32)0 >> (32-bits) << dstOffset);
1331 Uint32 data= ((*src) >> srcOffset) << dstOffset;
1336 *dst= (*dst & ~destMask) | (data & destMask);