util: New function zero_rightmost_1bit().
authorBen Pfaff <blp@nicira.com>
Fri, 20 Jul 2012 18:45:33 +0000 (11:45 -0700)
committerBen Pfaff <blp@nicira.com>
Tue, 4 Sep 2012 18:19:17 +0000 (11:19 -0700)
It's probably easier to understand
x = zero_rightmost_1bit(x);
than
x &= x - 1;

Signed-off-by: Ben Pfaff <blp@nicira.com>
lib/util.h
ofproto/ofproto-dpif.c
tests/test-classifier.c

index 57527fc..8c9103c 100644 (file)
@@ -103,6 +103,14 @@ rightmost_1bit(uintmax_t x)
     return x & -x;
 }
 
+/* Returns 'x' with its rightmost 1-bit changed to a zero (e.g. 01011000 =>
+ * 01010000), or 0 if 'x' is 0. */
+static inline uintmax_t
+zero_rightmost_1bit(uintmax_t x)
+{
+    return x & (x - 1);
+}
+
 #ifndef MIN
 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
 #endif
index 3aa29a8..b7e36ab 100644 (file)
@@ -6001,7 +6001,7 @@ add_mirror_actions(struct action_xlate_ctx *ctx, const struct flow *orig_flow)
         m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
 
         if (!vlan_is_mirrored(m, vlan)) {
-            mirrors &= mirrors - 1;
+            mirrors = zero_rightmost_1bit(mirrors);
             continue;
         }
 
@@ -6031,7 +6031,7 @@ update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
         return;
     }
 
-    for (; mirrors; mirrors &= mirrors - 1) {
+    for (; mirrors; mirrors = zero_rightmost_1bit(mirrors)) {
         struct ofmirror *m;
 
         m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
index d4a524d..3ee6ddb 100644 (file)
@@ -769,7 +769,7 @@ count_ones(unsigned long int x)
     int n = 0;
 
     while (x) {
-        x &= x - 1;
+        x = zero_rightmost_1bit(x);
         n++;
     }