From 8472a3cecc6c370e227dd477470e4a8cf760ddb5 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 20 Jul 2012 11:45:33 -0700 Subject: [PATCH] util: New function zero_rightmost_1bit(). It's probably easier to understand x = zero_rightmost_1bit(x); than x &= x - 1; Signed-off-by: Ben Pfaff --- lib/util.h | 8 ++++++++ ofproto/ofproto-dpif.c | 4 ++-- tests/test-classifier.c | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/util.h b/lib/util.h index 57527fcc..8c9103c6 100644 --- a/lib/util.h +++ b/lib/util.h @@ -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 diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c index 3aa29a83..b7e36abc 100644 --- a/ofproto/ofproto-dpif.c +++ b/ofproto/ofproto-dpif.c @@ -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]; diff --git a/tests/test-classifier.c b/tests/test-classifier.c index d4a524d3..3ee6ddb6 100644 --- a/tests/test-classifier.c +++ b/tests/test-classifier.c @@ -769,7 +769,7 @@ count_ones(unsigned long int x) int n = 0; while (x) { - x &= x - 1; + x = zero_rightmost_1bit(x); n++; } -- 2.30.2