]> mj.ucw.cz Git - libucw.git/commitdiff
Added functions for manipulating bit arrays. One day, an optimized
authorMartin Mares <mj@ucw.cz>
Mon, 13 Jan 2003 21:28:04 +0000 (21:28 +0000)
committerMartin Mares <mj@ucw.cz>
Mon, 13 Jan 2003 21:28:04 +0000 (21:28 +0000)
version for i386 using bts instruction et al. will appear.

lib/bitarray.h [new file with mode: 0644]

diff --git a/lib/bitarray.h b/lib/bitarray.h
new file mode 100644 (file)
index 0000000..39340d1
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ *     Bit Array Operations
+ *
+ *     (c) 2003 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include <string.h>
+
+#define BIT_ARRAY_WORDS(n) (((n)+31)/32)
+#define BIT_ARRAY(name,size) u32 name[BIT_ARRAY_WORDS(size)]
+
+static inline void
+bit_array_zero(u32 *a, uns n)
+{
+  bzero(a, 4*BIT_ARRAY_WORDS(n));
+}
+
+static inline void
+bit_array_set(u32 *a, uns i)
+{
+  a[i/32] |= (1 << (i%32));
+}
+
+static inline void
+bit_array_clear(u32 *a, uns i)
+{
+  a[i/32] &= ~(1 << (i%32));
+}
+
+static inline uns
+bit_array_isset(u32 *a, uns i)
+{
+  return a[i/32] & (1 << (i%32));
+}
+
+static inline uns
+bit_array_get(u32 *a, uns i)
+{
+  return !! bit_array_isset(a, i);
+}
+
+static inline uns
+bit_array_test_and_set(u32 *a, uns i)
+{
+  uns t = bit_array_isset(a, i);
+  bit_array_set(a, i);
+  return t;
+}
+
+static inline uns
+bit_array_test_and_clear(u32 *a, uns i)
+{
+  uns t = bit_array_isset(a, i);
+  bit_array_clear(a, i);
+  return t;
+}
+
+/* Iterate over all set bits, possibly destructively */
+#define BIT_ARRAY_FISH_BITS_BEGIN(var,ary,size)                                        \
+  for (uns var##_hi=0; var##_hi < BIT_ARRAY_WORDS(size); var##_hi++)           \
+    for (uns var##_lo=0; ary[var##_hi]; var##_lo++)                            \
+      if (ary[var##_hi] & (1 << var##_lo))                                     \
+        {                                                                      \
+         uns var = 32*var##_hi + var##_lo;                                     \
+         ary[var##_hi] &= ~(1 << var##_lo);                                    \
+         do
+
+#define BIT_ARRAY_FISH_BITS_END                                                        \
+         while (0);                                                            \
+       }