Re: [PATCH 07/10] bitops: Provide compile time HWEIGHT{8,16,32,64}

From: John Kacur
Date: Fri Jan 29 2010 - 05:32:11 EST


On Fri, Jan 22, 2010 at 4:50 PM, Peter Zijlstra <a.p.zijlstra@xxxxxxxxx> wrote:
> Provide compile time versions of hweight.
>
> Signed-off-by: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
> LKML-Reference: <new-submission>
> ---
>  include/linux/bitops.h |   14 ++++++++++++++
>  1 file changed, 14 insertions(+)
>
> Index: linux-2.6/include/linux/bitops.h
> ===================================================================
> --- linux-2.6.orig/include/linux/bitops.h
> +++ linux-2.6/include/linux/bitops.h
> @@ -45,6 +45,20 @@ static inline unsigned long hweight_long
>        return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
>  }

I like it. Maybe provide a comment that this provides the Hamming weight, it
took me a second to realize what this was.

>
> +#define HWEIGHT8(w)                    \
> +      (        (!!((w) & (1ULL << 0))) +       \
> +       (!!((w) & (1ULL << 1))) +       \
> +       (!!((w) & (1ULL << 2))) +       \
> +       (!!((w) & (1ULL << 3))) +       \
> +       (!!((w) & (1ULL << 4))) +       \
> +       (!!((w) & (1ULL << 5))) +       \
> +       (!!((w) & (1ULL << 6))) +       \
> +       (!!((w) & (1ULL << 7))) )
> +
> +#define HWEIGHT16(w) (HWEIGHT8(w)  + HWEIGHT8(w >> 8))
> +#define HWEIGHT32(w) (HWEIGHT16(w) + HWEIGHT16(w >> 16))
> +#define HWEIGHT64(w) (HWEIGHT32(w) + HWEIGHT32(w >> 32))
> +
>  /**
>  * rol32 - rotate a 32-bit value left
>  * @word: value to rotate
>
> --
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@xxxxxxxxxxxxxxx
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/