util: New function bitwise_scan().
authorBen Pfaff <blp@nicira.com>
Wed, 3 Sep 2014 00:16:51 +0000 (17:16 -0700)
committerBen Pfaff <blp@nicira.com>
Tue, 7 Oct 2014 22:34:33 +0000 (15:34 -0700)
This will acquire its first user in an upcoming commit.

This implementation is not optimized at all but it doesn't matter for the
purpose for which I intend to initially use it.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
lib/util.c
lib/util.h

index 01fe7dc..707c652 100644 (file)
@@ -1273,6 +1273,35 @@ bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs,
     return true;
 }
 
+/* Scans the bits in 'p' that have bit offsets 'start' through 'end'
+ * (inclusive) for the first bit with value 'target'.  If one is found, returns
+ * its offset, otherwise 'end'.  'p' is 'len' bytes long.
+ *
+ * If you consider all of 'p' to be a single unsigned integer in network byte
+ * order, then bit N is the bit with value 2**N.  That is, bit 0 is the bit
+ * with value 1 in p[len - 1], bit 1 is the bit with value 2, bit 2 is the bit
+ * with value 4, ..., bit 8 is the bit with value 1 in p[len - 2], and so on.
+ *
+ * Required invariant:
+ *   start <= end
+ */
+unsigned int
+bitwise_scan(const void *p_, unsigned int len, bool target, unsigned int start,
+             unsigned int end)
+{
+    const uint8_t *p = p_;
+    unsigned int ofs;
+
+    for (ofs = start; ofs < end; ofs++) {
+        bool bit = (p[len - (ofs / 8 + 1)] & (1u << (ofs % 8))) != 0;
+        if (bit == target) {
+            break;
+        }
+    }
+    return ofs;
+}
+
+
 /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits
  * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long.
  *
index af2273d..d3e6469 100644 (file)
@@ -539,6 +539,8 @@ void bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs,
                  unsigned int n_bits);
 bool bitwise_is_all_zeros(const void *, unsigned int len, unsigned int ofs,
                           unsigned int n_bits);
+unsigned int bitwise_scan(const void *, unsigned int len,
+                          bool target, unsigned int start, unsigned int end);
 void bitwise_put(uint64_t value,
                  void *dst, unsigned int dst_len, unsigned int dst_ofs,
                  unsigned int n_bits);