1 /* Copyright (c) 2003-2006, 2008 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
16 
17 #ifndef NDB_BITMASK_H
18 #define NDB_BITMASK_H
19 
20 #include <ndb_global.h>
21 
22 /**
23  * Bitmask implementation.  Size is given explicitly
24  * (as first argument).  All methods are static.
25  */
26 class BitmaskImpl {
27 public:
28   STATIC_CONST( NotFound = (unsigned)-1 );
29 
30   /**
31    * get - Check if bit n is set.
32    */
33   static bool get(unsigned size, const Uint32 data[], unsigned n);
34 
35   /**
36    * set - Set bit n to given value (true/false).
37    */
38   static void set(unsigned size, Uint32 data[], unsigned n, bool value);
39 
40   /**
41    * set - Set bit n.
42    */
43   static void set(unsigned size, Uint32 data[], unsigned n);
44 
45   /**
46    * set - Set all bits.
47    */
48   static void set(unsigned size, Uint32 data[]);
49 
50   /**
51    * set bit from <em>start</em> to <em>last</em>
52    */
53   static void set_range(unsigned size, Uint32 data[], unsigned start, unsigned last);
54 
55   /**
56    * assign - Set all bits in <em>dst</em> to corresponding in <em>src/<em>
57    */
58   static void assign(unsigned size, Uint32 dst[], const Uint32 src[]);
59 
60   /**
61    * clear - Clear bit n.
62    */
63   static void clear(unsigned size, Uint32 data[], unsigned n);
64 
65   /**
66    * clear - Clear all bits.
67    */
68   static void clear(unsigned size, Uint32 data[]);
69 
70   /**
71    * clear bit from <em>start</em> to <em>last</em>
72    */
73   static void clear_range(unsigned size, Uint32 data[], unsigned start, unsigned last);
74 
75   static Uint32 getWord(unsigned size, Uint32 data[], unsigned word_pos);
76   static void setWord(unsigned size, Uint32 data[],
77                       unsigned word_pos, Uint32 new_word);
78   /**
79    * isclear -  Check if all bits are clear.  This is faster
80    * than checking count() == 0.
81    */
82   static bool isclear(unsigned size, const Uint32 data[]);
83 
84   /**
85    * count - Count number of set bits.
86    */
87   static unsigned count(unsigned size, const Uint32 data[]);
88 
89   /**
90    * find - Find first set bit, starting at given position.
91    * Returns NotFound when not found.
92    */
93   static unsigned find(unsigned size, const Uint32 data[], unsigned n);
94 
95   /**
96    * equal - Bitwise equal.
97    */
98   static bool equal(unsigned size, const Uint32 data[], const Uint32 data2[]);
99 
100   /**
101    * bitOR - Bitwise (x | y) into first operand.
102    */
103   static void bitOR(unsigned size, Uint32 data[], const Uint32 data2[]);
104 
105   /**
106    * bitAND - Bitwise (x & y) into first operand.
107    */
108   static void bitAND(unsigned size, Uint32 data[], const Uint32 data2[]);
109 
110   /**
111    * bitANDC - Bitwise (x & ~y) into first operand.
112    */
113   static void bitANDC(unsigned size, Uint32 data[], const Uint32 data2[]);
114 
115   /**
116    * bitXOR - Bitwise (x ^ y) into first operand.
117    */
118   static void bitXOR(unsigned size, Uint32 data[], const Uint32 data2[]);
119 
120   /**
121    * bitXORC - Bitwise (x ^ ~y) into first operand.
122    */
123   static void bitXORC(unsigned size, Uint32 data[], const Uint32 data2[]);
124 
125   /**
126    * contains - Check if all bits set in data2 are set in data
127    */
128   static bool contains(unsigned size, Uint32 data[], const Uint32 data2[]);
129 
130   /**
131    * overlaps - Check if any bit set in data is set in data2
132    */
133   static bool overlaps(unsigned size, Uint32 data[], const Uint32 data2[]);
134 
135   /**
136    * getField - Get bitfield at given position and length (max 32 bits)
137    */
138   static Uint32 getField(unsigned size, const Uint32 data[],
139       unsigned pos, unsigned len);
140 
141   /**
142    * setField - Set bitfield at given position and length (max 32 bits)
143    * Note : length == 0 not supported.
144    */
145   static void setField(unsigned size, Uint32 data[],
146       unsigned pos, unsigned len, Uint32 val);
147 
148 
149   /**
150    * getField - Get bitfield at given position and length
151    * Note : length == 0 not supported.
152    */
153   static void getField(unsigned size, const Uint32 data[],
154 		       unsigned pos, unsigned len, Uint32 dst[]);
155 
156   /**
157    * setField - Set bitfield at given position and length
158    */
159   static void setField(unsigned size, Uint32 data[],
160 		       unsigned pos, unsigned len, const Uint32 src[]);
161 
162   /**
163    * getText - Return as hex-digits (only for debug routines).
164    */
165   static char* getText(unsigned size, const Uint32 data[], char* buf);
166 private:
167   static void getFieldImpl(const Uint32 data[], unsigned, unsigned, Uint32 []);
168   static void setFieldImpl(Uint32 data[], unsigned, unsigned, const Uint32 []);
169 };
170 
171 inline bool
get(unsigned size,const Uint32 data[],unsigned n)172 BitmaskImpl::get(unsigned size, const Uint32 data[], unsigned n)
173 {
174   assert(n < (size << 5));
175   return (data[n >> 5] & (1 << (n & 31))) != 0;
176 }
177 
178 inline void
set(unsigned size,Uint32 data[],unsigned n,bool value)179 BitmaskImpl::set(unsigned size, Uint32 data[], unsigned n, bool value)
180 {
181   value ? set(size, data, n) : clear(size, data, n);
182 }
183 
184 inline void
set(unsigned size,Uint32 data[],unsigned n)185 BitmaskImpl::set(unsigned size, Uint32 data[], unsigned n)
186 {
187   assert(n < (size << 5));
188   data[n >> 5] |= (1 << (n & 31));
189 }
190 
191 inline void
set(unsigned size,Uint32 data[])192 BitmaskImpl::set(unsigned size, Uint32 data[])
193 {
194   for (unsigned i = 0; i < size; i++) {
195     data[i] = ~0;
196   }
197 }
198 
199 inline void
set_range(unsigned size,Uint32 data[],unsigned start,unsigned last)200 BitmaskImpl::set_range(unsigned size, Uint32 data[],
201 		       unsigned start, unsigned last)
202 {
203   Uint32 *ptr = data + (start >> 5);
204   Uint32 *end =  data + (last >> 5);
205   assert(start <= last);
206   assert(last < (size << 5));
207 
208   Uint32 tmp_word = ~(Uint32)0 << (start & 31);
209 
210   if (ptr < end)
211   {
212     * ptr ++ |= tmp_word;
213 
214     for(; ptr < end; )
215     {
216       * ptr ++ = ~(Uint32)0;
217     }
218 
219     tmp_word = ~(Uint32)0;
220   }
221 
222   tmp_word &= ~(~(Uint32)0 << (last & 31));
223 
224   * ptr |= tmp_word;
225 }
226 
227 inline void
assign(unsigned size,Uint32 dst[],const Uint32 src[])228 BitmaskImpl::assign(unsigned size, Uint32 dst[], const Uint32 src[])
229 {
230   for (unsigned i = 0; i < size; i++) {
231     dst[i] = src[i];
232   }
233 }
234 
235 inline void
clear(unsigned size,Uint32 data[],unsigned n)236 BitmaskImpl::clear(unsigned size, Uint32 data[], unsigned n)
237 {
238   assert(n < (size << 5));
239   data[n >> 5] &= ~(1 << (n & 31));
240 }
241 
242 inline void
clear(unsigned size,Uint32 data[])243 BitmaskImpl::clear(unsigned size, Uint32 data[])
244 {
245   for (unsigned i = 0; i < size; i++) {
246     data[i] = 0;
247   }
248 }
249 
250 inline void
clear_range(unsigned size,Uint32 data[],unsigned start,unsigned last)251 BitmaskImpl::clear_range(unsigned size, Uint32 data[],
252 			 unsigned start, unsigned last)
253 {
254   Uint32 *ptr = data + (start >> 5);
255   Uint32 *end =  data + (last >> 5);
256   assert(start <= last);
257   assert(last < (size << 5));
258 
259   Uint32 tmp_word = ~(Uint32)0 << (start & 31);
260 
261   if (ptr < end)
262   {
263     * ptr ++ &= ~tmp_word;
264 
265     for(; ptr < end; )
266     {
267       * ptr ++ = 0;
268     }
269 
270     tmp_word = ~(Uint32)0;
271   }
272 
273   tmp_word &= ~(~(Uint32)0 << (last & 31));
274 
275   * ptr &= ~tmp_word;
276 }
277 
278 inline
279 Uint32
getWord(unsigned size,Uint32 data[],unsigned word_pos)280 BitmaskImpl::getWord(unsigned size, Uint32 data[], unsigned word_pos)
281 {
282   return data[word_pos];
283 }
284 
285 inline void
setWord(unsigned size,Uint32 data[],unsigned word_pos,Uint32 new_word)286 BitmaskImpl::setWord(unsigned size, Uint32 data[],
287                      unsigned word_pos, Uint32 new_word)
288 {
289   data[word_pos] = new_word;
290   return;
291 }
292 
293 inline bool
isclear(unsigned size,const Uint32 data[])294 BitmaskImpl::isclear(unsigned size, const Uint32 data[])
295 {
296   for (unsigned i = 0; i < size; i++) {
297     if (data[i] != 0)
298       return false;
299   }
300   return true;
301 }
302 
303 inline unsigned
count(unsigned size,const Uint32 data[])304 BitmaskImpl::count(unsigned size, const Uint32 data[])
305 {
306   unsigned cnt = 0;
307   for (unsigned i = 0; i < size; i++) {
308     Uint32 x = data[i];
309     while (x) {
310       x &= (x - 1);
311       cnt++;
312     }
313   }
314   return cnt;
315 }
316 
317 inline unsigned
find(unsigned size,const Uint32 data[],unsigned n)318 BitmaskImpl::find(unsigned size, const Uint32 data[], unsigned n)
319 {
320   while (n < (size << 5)) {             // XXX make this smarter
321     if (get(size, data, n)) {
322       return n;
323     }
324     n++;
325   }
326   return NotFound;
327 }
328 
329 inline bool
equal(unsigned size,const Uint32 data[],const Uint32 data2[])330 BitmaskImpl::equal(unsigned size, const Uint32 data[], const Uint32 data2[])
331 {
332   for (unsigned i = 0; i < size; i++) {
333     if (data[i] != data2[i])
334       return false;
335   }
336   return true;
337 }
338 
339 inline void
bitOR(unsigned size,Uint32 data[],const Uint32 data2[])340 BitmaskImpl::bitOR(unsigned size, Uint32 data[], const Uint32 data2[])
341 {
342   for (unsigned i = 0; i < size; i++) {
343     data[i] |= data2[i];
344   }
345 }
346 
347 inline void
bitAND(unsigned size,Uint32 data[],const Uint32 data2[])348 BitmaskImpl::bitAND(unsigned size, Uint32 data[], const Uint32 data2[])
349 {
350   for (unsigned i = 0; i < size; i++) {
351     data[i] &= data2[i];
352   }
353 }
354 
355 inline void
bitANDC(unsigned size,Uint32 data[],const Uint32 data2[])356 BitmaskImpl::bitANDC(unsigned size, Uint32 data[], const Uint32 data2[])
357 {
358   for (unsigned i = 0; i < size; i++) {
359     data[i] &= ~data2[i];
360   }
361 }
362 
363 inline void
bitXOR(unsigned size,Uint32 data[],const Uint32 data2[])364 BitmaskImpl::bitXOR(unsigned size, Uint32 data[], const Uint32 data2[])
365 {
366   for (unsigned i = 0; i < size; i++) {
367     data[i] ^= data2[i];
368   }
369 }
370 
371 inline void
bitXORC(unsigned size,Uint32 data[],const Uint32 data2[])372 BitmaskImpl::bitXORC(unsigned size, Uint32 data[], const Uint32 data2[])
373 {
374   for (unsigned i = 0; i < size; i++) {
375     data[i] ^= ~data2[i];
376   }
377 }
378 
379 inline bool
contains(unsigned size,Uint32 data[],const Uint32 data2[])380 BitmaskImpl::contains(unsigned size, Uint32 data[], const Uint32 data2[])
381 {
382   for (unsigned int i = 0; i < size; i++)
383     if ((data[i] & data2[i]) != data2[i])
384       return false;
385   return true;
386 }
387 
388 inline bool
overlaps(unsigned size,Uint32 data[],const Uint32 data2[])389 BitmaskImpl::overlaps(unsigned size, Uint32 data[], const Uint32 data2[])
390 {
391   for (unsigned int i = 0; i < size; i++)
392     if ((data[i] & data2[i]) != 0)
393       return true;
394   return false;
395 }
396 
397 inline Uint32
getField(unsigned size,const Uint32 data[],unsigned pos,unsigned len)398 BitmaskImpl::getField(unsigned size, const Uint32 data[],
399     unsigned pos, unsigned len)
400 {
401   Uint32 val = 0;
402   for (unsigned i = 0; i < len; i++)
403     val |= get(size, data, pos + i) << i;
404   return val;
405 }
406 
407 inline void
setField(unsigned size,Uint32 data[],unsigned pos,unsigned len,Uint32 val)408 BitmaskImpl::setField(unsigned size, Uint32 data[],
409     unsigned pos, unsigned len, Uint32 val)
410 {
411   for (unsigned i = 0; i < len; i++)
412     set(size, data, pos + i, val & (1 << i));
413 }
414 
415 inline char *
getText(unsigned size,const Uint32 data[],char * buf)416 BitmaskImpl::getText(unsigned size, const Uint32 data[], char* buf)
417 {
418   char * org = buf;
419   const char* const hex = "0123456789abcdef";
420   for (int i = (size-1); i >= 0; i--) {
421     Uint32 x = data[i];
422     for (unsigned j = 0; j < 8; j++) {
423       buf[7-j] = hex[x & 0xf];
424       x >>= 4;
425     }
426     buf += 8;
427   }
428   *buf = 0;
429   return org;
430 }
431 
432 /**
433  * Bitmasks.  The size is number of 32-bit words (Uint32).
434  * Unused bits in the last word must be zero.
435  *
436  * XXX replace size by length in bits
437  */
438 template <unsigned size>
439 struct BitmaskPOD {
440 public:
441   /**
442    * POD data representation
443    */
444   struct Data {
445     Uint32 data[size];
446 #if 0
447     Data & operator=(const BitmaskPOD<size> & src) {
448       src.copyto(size, data);
449       return *this;
450     }
451 #endif
452   };
453 private:
454 
455   Data rep;
456 public:
457   STATIC_CONST( Size = size );
458   STATIC_CONST( NotFound = BitmaskImpl::NotFound );
459   STATIC_CONST( TextLength = size * 8 );
460 
461   /**
462    * assign - Set all bits in <em>dst</em> to corresponding in <em>src/<em>
463    */
464   void assign(const typename BitmaskPOD<size>::Data & src);
465 
466   /**
467    * assign - Set all bits in <em>dst</em> to corresponding in <em>src/<em>
468    */
469   static void assign(Uint32 dst[], const Uint32 src[]);
470   static void assign(Uint32 dst[], const BitmaskPOD<size> & src);
471   void assign(const BitmaskPOD<size> & src);
472 
473   /**
474    * copy this to <em>dst</em>
475    */
476   void copyto(unsigned sz, Uint32 dst[]) const;
477 
478   /**
479    * assign <em>this</em> according to <em>src/em>
480    */
481   void assign(unsigned sz, const Uint32 src[]);
482 
483   /**
484    * get - Check if bit n is set.
485    */
486   static bool get(const Uint32 data[], unsigned n);
487   bool get(unsigned n) const;
488 
489   /**
490    * set - Set bit n to given value (true/false).
491    */
492   static void set(Uint32 data[], unsigned n, bool value);
493   void set(unsigned n, bool value);
494 
495   /**
496    * set - Set bit n.
497    */
498   static void set(Uint32 data[], unsigned n);
499   void set(unsigned n);
500 
501   /**
502    * set - set all bits.
503    */
504   static void set(Uint32 data[]);
505   void set();
506 
507   /**
508    * clear - Clear bit n.
509    */
510   static void clear(Uint32 data[], unsigned n);
511   void clear(unsigned n);
512 
513   /**
514    * clear - Clear all bits.
515    */
516   static void clear(Uint32 data[]);
517   void clear();
518 
519   /**
520    * Get and set words of bits
521    */
522   Uint32 getWord(unsigned word_pos);
523   void setWord(unsigned word_pos, Uint32 new_word);
524 
525   /**
526    * isclear -  Check if all bits are clear.  This is faster
527    * than checking count() == 0.
528    */
529   static bool isclear(const Uint32 data[]);
530   bool isclear() const;
531 
532   /**
533    * count - Count number of set bits.
534    */
535   static unsigned count(const Uint32 data[]);
536   unsigned count() const;
537 
538   /**
539    * find - Find first set bit, starting at given position.
540    * Returns NotFound when not found.
541    */
542   static unsigned find(const Uint32 data[], unsigned n);
543   unsigned find(unsigned n) const;
544 
545   /**
546    * equal - Bitwise equal.
547    */
548   static bool equal(const Uint32 data[], const Uint32 data2[]);
549   bool equal(const BitmaskPOD<size>& mask2) const;
550 
551   /**
552    * bitOR - Bitwise (x | y) into first operand.
553    */
554   static void bitOR(Uint32 data[], const Uint32 data2[]);
555   BitmaskPOD<size>& bitOR(const BitmaskPOD<size>& mask2);
556 
557   /**
558    * bitAND - Bitwise (x & y) into first operand.
559    */
560   static void bitAND(Uint32 data[], const Uint32 data2[]);
561   BitmaskPOD<size>& bitAND(const BitmaskPOD<size>& mask2);
562 
563   /**
564    * bitANDC - Bitwise (x & ~y) into first operand.
565    */
566   static void bitANDC(Uint32 data[], const Uint32 data2[]);
567   BitmaskPOD<size>& bitANDC(const BitmaskPOD<size>& mask2);
568 
569   /**
570    * bitXOR - Bitwise (x ^ y) into first operand.
571    */
572   static void bitXOR(Uint32 data[], const Uint32 data2[]);
573   BitmaskPOD<size>& bitXOR(const BitmaskPOD<size>& mask2);
574 
575   /**
576    * bitXORC - Bitwise (x ^ ~y) into first operand.
577    */
578   static void bitXORC(Uint32 data[], const Uint32 data2[]);
579   BitmaskPOD<size>& bitXORC(const BitmaskPOD<size>& mask2);
580 
581   /**
582    * contains - Check if all bits set in data2 (that) are also set in data (this)
583    */
584   static bool contains(Uint32 data[], const Uint32 data2[]);
585   bool contains(BitmaskPOD<size> that);
586 
587   /**
588    * overlaps - Check if any bit set in this BitmaskPOD (data) is also set in that (data2)
589    */
590   static bool overlaps(Uint32 data[], const Uint32 data2[]);
591   bool overlaps(BitmaskPOD<size> that);
592 
593   /**
594    * getText - Return as hex-digits (only for debug routines).
595    */
596   static char* getText(const Uint32 data[], char* buf);
597   char* getText(char* buf) const;
598 };
599 
600 template <unsigned size>
601 inline void
assign(Uint32 dst[],const Uint32 src[])602 BitmaskPOD<size>::assign(Uint32 dst[], const Uint32 src[])
603 {
604   BitmaskImpl::assign(size, dst, src);
605 }
606 
607 template <unsigned size>
608 inline void
assign(Uint32 dst[],const BitmaskPOD<size> & src)609 BitmaskPOD<size>::assign(Uint32 dst[], const BitmaskPOD<size> & src)
610 {
611   BitmaskImpl::assign(size, dst, src.rep.data);
612 }
613 
614 template <unsigned size>
615 inline void
assign(const typename BitmaskPOD<size>::Data & src)616 BitmaskPOD<size>::assign(const typename BitmaskPOD<size>::Data & src)
617 {
618   BitmaskPOD<size>::assign(rep.data, src.data);
619 }
620 
621 template <unsigned size>
622 inline void
assign(const BitmaskPOD<size> & src)623 BitmaskPOD<size>::assign(const BitmaskPOD<size> & src)
624 {
625   BitmaskPOD<size>::assign(rep.data, src.rep.data);
626 }
627 
628 template <unsigned size>
629 inline void
copyto(unsigned sz,Uint32 dst[]) const630 BitmaskPOD<size>::copyto(unsigned sz, Uint32 dst[]) const
631 {
632   BitmaskImpl::assign(sz, dst, rep.data);
633 }
634 
635 template <unsigned size>
636 inline void
assign(unsigned sz,const Uint32 src[])637 BitmaskPOD<size>::assign(unsigned sz, const Uint32 src[])
638 {
639   BitmaskImpl::assign(sz, rep.data, src);
640 }
641 
642 template <unsigned size>
643 inline bool
get(const Uint32 data[],unsigned n)644 BitmaskPOD<size>::get(const Uint32 data[], unsigned n)
645 {
646   return BitmaskImpl::get(size, data, n);
647 }
648 
649 template <unsigned size>
650 inline bool
get(unsigned n) const651 BitmaskPOD<size>::get(unsigned n) const
652 {
653   return BitmaskPOD<size>::get(rep.data, n);
654 }
655 
656 template <unsigned size>
657 inline void
set(Uint32 data[],unsigned n,bool value)658 BitmaskPOD<size>::set(Uint32 data[], unsigned n, bool value)
659 {
660   BitmaskImpl::set(size, data, n, value);
661 }
662 
663 template <unsigned size>
664 inline void
set(unsigned n,bool value)665 BitmaskPOD<size>::set(unsigned n, bool value)
666 {
667   BitmaskPOD<size>::set(rep.data, n, value);
668 }
669 
670 template <unsigned size>
671 inline void
set(Uint32 data[],unsigned n)672 BitmaskPOD<size>::set(Uint32 data[], unsigned n)
673 {
674   BitmaskImpl::set(size, data, n);
675 }
676 
677 template <unsigned size>
678 inline void
set(unsigned n)679 BitmaskPOD<size>::set(unsigned n)
680 {
681   BitmaskPOD<size>::set(rep.data, n);
682 }
683 
684 template <unsigned size>
685 inline void
set(Uint32 data[])686 BitmaskPOD<size>::set(Uint32 data[])
687 {
688   BitmaskImpl::set(size, data);
689 }
690 
691 template <unsigned size>
692 inline void
set()693 BitmaskPOD<size>::set()
694 {
695   BitmaskPOD<size>::set(rep.data);
696 }
697 
698 template <unsigned size>
699 inline void
clear(Uint32 data[],unsigned n)700 BitmaskPOD<size>::clear(Uint32 data[], unsigned n)
701 {
702   BitmaskImpl::clear(size, data, n);
703 }
704 
705 template <unsigned size>
706 inline void
clear(unsigned n)707 BitmaskPOD<size>::clear(unsigned n)
708 {
709   BitmaskPOD<size>::clear(rep.data, n);
710 }
711 
712 template <unsigned size>
713 inline void
clear(Uint32 data[])714 BitmaskPOD<size>::clear(Uint32 data[])
715 {
716   BitmaskImpl::clear(size, data);
717 }
718 
719 template <unsigned size>
720 inline void
clear()721 BitmaskPOD<size>::clear()
722 {
723   BitmaskPOD<size>::clear(rep.data);
724 }
725 
726 template <unsigned size>
727 inline Uint32
getWord(unsigned word_pos)728 BitmaskPOD<size>::getWord(unsigned word_pos)
729 {
730   return BitmaskImpl::getWord(size, rep.data, word_pos);
731 }
732 
733 template <unsigned size>
734 inline void
setWord(unsigned word_pos,Uint32 new_word)735 BitmaskPOD<size>::setWord(unsigned word_pos, Uint32 new_word)
736 {
737   BitmaskImpl::setWord(size, rep.data, word_pos, new_word);
738 }
739 
740 template <unsigned size>
741 inline bool
isclear(const Uint32 data[])742 BitmaskPOD<size>::isclear(const Uint32 data[])
743 {
744   return BitmaskImpl::isclear(size, data);
745 }
746 
747 template <unsigned size>
748 inline bool
isclear() const749 BitmaskPOD<size>::isclear() const
750 {
751   return BitmaskPOD<size>::isclear(rep.data);
752 }
753 
754 template <unsigned size>
755 unsigned
count(const Uint32 data[])756 BitmaskPOD<size>::count(const Uint32 data[])
757 {
758   return BitmaskImpl::count(size, data);
759 }
760 
761 template <unsigned size>
762 inline unsigned
count() const763 BitmaskPOD<size>::count() const
764 {
765   return BitmaskPOD<size>::count(rep.data);
766 }
767 
768 template <unsigned size>
769 unsigned
find(const Uint32 data[],unsigned n)770 BitmaskPOD<size>::find(const Uint32 data[], unsigned n)
771 {
772   return BitmaskImpl::find(size, data, n);
773 }
774 
775 template <unsigned size>
776 inline unsigned
find(unsigned n) const777 BitmaskPOD<size>::find(unsigned n) const
778 {
779   return BitmaskPOD<size>::find(rep.data, n);
780 }
781 
782 template <unsigned size>
783 inline bool
equal(const Uint32 data[],const Uint32 data2[])784 BitmaskPOD<size>::equal(const Uint32 data[], const Uint32 data2[])
785 {
786   return BitmaskImpl::equal(size, data, data2);
787 }
788 
789 template <unsigned size>
790 inline bool
equal(const BitmaskPOD<size> & mask2) const791 BitmaskPOD<size>::equal(const BitmaskPOD<size>& mask2) const
792 {
793   return BitmaskPOD<size>::equal(rep.data, mask2.rep.data);
794 }
795 
796 template <unsigned size>
797 inline void
bitOR(Uint32 data[],const Uint32 data2[])798 BitmaskPOD<size>::bitOR(Uint32 data[], const Uint32 data2[])
799 {
800   BitmaskImpl::bitOR(size,data, data2);
801 }
802 
803 template <unsigned size>
804 inline BitmaskPOD<size>&
bitOR(const BitmaskPOD<size> & mask2)805 BitmaskPOD<size>::bitOR(const BitmaskPOD<size>& mask2)
806 {
807   BitmaskPOD<size>::bitOR(rep.data, mask2.rep.data);
808   return *this;
809 }
810 
811 template <unsigned size>
812 inline void
bitAND(Uint32 data[],const Uint32 data2[])813 BitmaskPOD<size>::bitAND(Uint32 data[], const Uint32 data2[])
814 {
815   BitmaskImpl::bitAND(size,data, data2);
816 }
817 
818 template <unsigned size>
819 inline BitmaskPOD<size>&
bitAND(const BitmaskPOD<size> & mask2)820 BitmaskPOD<size>::bitAND(const BitmaskPOD<size>& mask2)
821 {
822   BitmaskPOD<size>::bitAND(rep.data, mask2.rep.data);
823   return *this;
824 }
825 
826 template <unsigned size>
827 inline void
bitANDC(Uint32 data[],const Uint32 data2[])828 BitmaskPOD<size>::bitANDC(Uint32 data[], const Uint32 data2[])
829 {
830   BitmaskImpl::bitANDC(size,data, data2);
831 }
832 
833 template <unsigned size>
834 inline BitmaskPOD<size>&
bitANDC(const BitmaskPOD<size> & mask2)835 BitmaskPOD<size>::bitANDC(const BitmaskPOD<size>& mask2)
836 {
837   BitmaskPOD<size>::bitANDC(rep.data, mask2.rep.data);
838   return *this;
839 }
840 
841 template <unsigned size>
842 inline void
bitXOR(Uint32 data[],const Uint32 data2[])843 BitmaskPOD<size>::bitXOR(Uint32 data[], const Uint32 data2[])
844 {
845   BitmaskImpl::bitXOR(size,data, data2);
846 }
847 
848 template <unsigned size>
849 inline BitmaskPOD<size>&
bitXOR(const BitmaskPOD<size> & mask2)850 BitmaskPOD<size>::bitXOR(const BitmaskPOD<size>& mask2)
851 {
852   BitmaskPOD<size>::bitXOR(rep.data, mask2.rep.data);
853   return *this;
854 }
855 
856 template <unsigned size>
857 inline void
bitXORC(Uint32 data[],const Uint32 data2[])858 BitmaskPOD<size>::bitXORC(Uint32 data[], const Uint32 data2[])
859 {
860   BitmaskImpl::bitXORC(size,data, data2);
861 }
862 
863 template <unsigned size>
864 inline BitmaskPOD<size>&
bitXORC(const BitmaskPOD<size> & mask2)865 BitmaskPOD<size>::bitXORC(const BitmaskPOD<size>& mask2)
866 {
867   BitmaskPOD<size>::bitXORC(rep.data, mask2.rep.data);
868   return *this;
869 }
870 
871 template <unsigned size>
872 char *
getText(const Uint32 data[],char * buf)873 BitmaskPOD<size>::getText(const Uint32 data[], char* buf)
874 {
875   return BitmaskImpl::getText(size, data, buf);
876 }
877 
878 template <unsigned size>
879 inline char *
getText(char * buf) const880 BitmaskPOD<size>::getText(char* buf) const
881 {
882   return BitmaskPOD<size>::getText(rep.data, buf);
883 }
884 
885 template <unsigned size>
886 inline bool
contains(Uint32 data[],const Uint32 data2[])887 BitmaskPOD<size>::contains(Uint32 data[], const Uint32 data2[])
888 {
889   return BitmaskImpl::contains(size, data, data2);
890 }
891 
892 template <unsigned size>
893 inline bool
contains(BitmaskPOD<size> that)894 BitmaskPOD<size>::contains(BitmaskPOD<size> that)
895 {
896   return BitmaskPOD<size>::contains(this->rep.data, that.rep.data);
897 }
898 
899 template <unsigned size>
900 inline bool
overlaps(Uint32 data[],const Uint32 data2[])901 BitmaskPOD<size>::overlaps(Uint32 data[], const Uint32 data2[])
902 {
903   return BitmaskImpl::overlaps(size, data, data2);
904 }
905 
906 template <unsigned size>
907 inline bool
overlaps(BitmaskPOD<size> that)908 BitmaskPOD<size>::overlaps(BitmaskPOD<size> that)
909 {
910   return BitmaskPOD<size>::overlaps(this->rep.data, that.rep.data);
911 }
912 
913 template <unsigned size>
914 class Bitmask : public BitmaskPOD<size> {
915 public:
Bitmask()916   Bitmask() { this->clear();}
917 };
918 
919 inline void
getField(unsigned size,const Uint32 src[],unsigned pos,unsigned len,Uint32 dst[])920 BitmaskImpl::getField(unsigned size, const Uint32 src[],
921 		      unsigned pos, unsigned len, Uint32 dst[])
922 {
923   assert(pos + len <= (size << 5));
924   assert (len != 0);
925   if (len == 0)
926     return;
927 
928   src += (pos >> 5);
929   Uint32 offset = pos & 31;
930   * dst = (* src >> offset) & (len >= 32 ? ~0 : (1 << len) - 1);
931 
932   if(offset + len <= 32)
933   {
934     return;
935   }
936   Uint32 used = (32 - offset);
937   assert(len > used);
938   getFieldImpl(src+1, used & 31, len-used, dst+(used >> 5));
939 }
940 
941 inline void
setField(unsigned size,Uint32 dst[],unsigned pos,unsigned len,const Uint32 src[])942 BitmaskImpl::setField(unsigned size, Uint32 dst[],
943 		      unsigned pos, unsigned len, const Uint32 src[])
944 {
945   assert(pos + len <= (size << 5));
946   assert(len != 0);
947   if (len == 0)
948     return;
949 
950   dst += (pos >> 5);
951   Uint32 offset = pos & 31;
952   Uint32 mask = (len >= 32 ? ~0 : (1 << len) - 1) << offset;
953 
954   * dst = (* dst & ~mask) | ((*src << offset) & mask);
955 
956   if(offset + len <= 32)
957   {
958     return;
959   }
960   Uint32 used = (32 - offset);
961   assert(len > used);
962   setFieldImpl(dst+1, used & 31, len-used, src+(used >> 5));
963 }
964 
965 
966 #endif
967