lkml.org 
[lkml]   [2010]   [Jan]   [29]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [tip:perf/core] bitops: Provide compile time HWEIGHT{8,16,32,64}
On Fri, 29 Jan 2010 11:04:31 +0100 Ingo Molnar <mingo@elte.hu> wrote:

>
> * Andrew Morton <akpm@linux-foundation.org> wrote:
>
> > On Fri, 29 Jan 2010 09:28:04 GMT tip-bot for Peter Zijlstra <a.p.zijlstra@chello.nl> wrote:
> >
> > > +#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))
> >
> > Would be nice if it had a comment explaining why it exists. If people
> > accidentally use this with non-constant arguments, the generated code
> > will be pretty ghastly.
> >
> > Or add some barf-if-not-__constant_p() thing, perhaps.
>
> Yeah, agreed.
>

Also...

Should we just do

#define HWEIGHT(x) HWEIGHT64(x)

and make HWEIGHT() the sole officially-exported interface? I mean, all
it does is emit an obfuscated constant - perhaps we can save users from
having to pick which one of the above to use by giving them a "this one
always works" interface.

That might require some casting to suppress "shift out of range"
warnings though.

<wonders if we'd otherwise end up needing an HWEIGHT_LONG()>


\
 
 \ /
  Last update: 2010-01-29 11:19    [W:0.123 / U:0.232 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site