Re: [PATCH 2/5] bitops: compile time optimization forhweight_long(CONSTANT)

From: Wu Fengguang
Date: Wed Feb 03 2010 - 08:42:24 EST


On Mon, Feb 01, 2010 at 01:48:25PM -0700, Andrew Morton wrote:
> > -static inline unsigned long hweight_long(unsigned long w)
> > -{
> > - return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
> > -}
> > +#define hweight_long(x) \
> > +( \
> > + __builtin_constant_p(x) ? \
> > + __builtin_popcountl(x) : \
> > + (sizeof(x) <= 4 ? \
> > + hweight32(x) : \
> > + hweight64(x)) \
> > +)
> >
> > /**
> > * rol32 - rotate a 32-bit value left
>
> Peter's been mucking with a compile-time HWEIGHT(). An outdated
> version of that is presently in linux-next.

Ah I find it. Thanks.

> (I wonder if it could have used __builtin_popcount)
>
> (I wonder which gcc versions support __builtin_popcount)

This is how Jamie Lokier first recommended it to me:

Checked GCC 3.4.3 / 4.1 / 4.4: It expands as a compile-time
constant if the argument is a compile-time constant, so can be
used in BUILD_BUG_ON() and even for array sizes etc.

Is there an official lowest GCC version that Linux supports?

> Anyway, I suspect you should be using that rather than tweaking
> hweight_long().

OK.

But.. if ever the GCC builtins can be used:

__builtin_popcount(unsigned int)
__builtin_popcountl(unsigned long)
__builtin_popcountll(unsigned long long)

it would be more natural to use hweight_long() or even introduce
hweight_longlong() for (transparent) compile time computation.

Thanks,
Fengguang
--
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/