On Sat, Jul 09, 2011 at 05:36:28PM +0200, Ed Schouten wrote:
> Hi Kostik,
> 
> * Konstantin Belousov <[email protected]>, 20110709 16:29:
> > +static __inline uint16_t
> > +bitcount16(uint32_t x)
> 
> Shouldn't we use uint16_t for the argument here?
Not sure. uint32_t type of argument avoids repromotion, allowing to
do the full-register calculation on both 32 and 64 bit architectures.

The function correctly handles non-zero upper half-word on its own.

> 
> When I saw the code, I thought by myself, this could be done more
> efficiently:
> 
> | static __inline uint16_t
> | bitcount16(uint16_t x)
> | {
> |
> |     x = (x & 0x5555) + ((x >> 1) & 0x5555);
> |     x = (x & 0x3333) + ((x >> 2) & 0x3333);
> |     x *= 0x1111;
> |     return (x >> 12);
> | }
> 
> But some testing revealed it works for all inputs, except 65536. d'oh!
> 
> -- 
>  Ed Schouten <[email protected]>
>  WWW: http://80386.nl/


Attachment: pgpog8Dqae1W7.pgp
Description: PGP signature

Reply via email to