[ovs-dev] [classifier-opt 22/28] util: New function zero_rightmost_1bit().

Ethan Jackson ethan at nicira.com
Tue Jul 31 17:30:58 UTC 2012


Looks good, thanks.

Ethan

On Fri, Jul 20, 2012 at 4:25 PM, Ben Pfaff <blp at nicira.com> wrote:
> It's probably easier to understand
>         x = zero_rightmost_1bit(x);
> than
>         x &= x - 1;
>
> Signed-off-by: Ben Pfaff <blp at nicira.com>
> ---
>  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 ef464ea..ca1d566 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 44cbd17..e21a6a2 100644
> --- a/ofproto/ofproto-dpif.c
> +++ b/ofproto/ofproto-dpif.c
> @@ -5930,7 +5930,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;
>          }
>
> @@ -5960,7 +5960,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 d4a524d..3ee6ddb 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++;
>      }
>
> --
> 1.7.2.5
>



More information about the dev mailing list