Subject: bug#22689: [PATCH] Add logcount

From: Mark Oteiza <[email-masked]>
Date: Sat, 30 Sep 2017 09:16:36 -0400

On 30/09/17 at 02:42pm, Eli Zaretskii wrote:
> > +#if defined (__POPCNT__) && defined (__GNUC__) && (__GNUC__> 4 || (__GNUC__== 4 && __GNUC_MINOR__> 1))
> > +#define HAVE_BUILTIN_POPCOUNTLL
> > +#endif
> 
> Where does __POPCNT__ definition come from?  I don't see it in my
> GCC's "gcc -dM" output.
> 
> As for the rest of the condition, I think you should use GNUC_PREREQ,
> like this:
> 
>  #if GNUC_PREREQ (4, 1, 0)

I guess it comes from nowhere, that condition and the two helper
functions come from here
https://rosettacode.org/wiki/Population_count#C

> > +static uint64_t
> > +bitcount64 (uint64_t b)
> > +{
> > +  b -= (b >> 1) & 0x5555555555555555;
> > +  b = (b & 0x3333333333333333) + ((b >> 2) & 0x3333333333333333);
> > +  b = (b + (b >> 4)) & 0x0f0f0f0f0f0f0f0f;
> > +  return (b * 0x0101010101010101) >> 56;
> 
> Shouldn't these constants have a ULL suffix, to make sure they are not
> truncated to a 32-bit int?

Probably, I don't know--I'm out of my comfort zone here.

> > +#else  /* HAVE_BUILTIN_POPCOUNTLL */
> > +  if (XINT (value) <= UINT_MAX)
> > +    XSETINT (res, bitcount32 (XUINT (value)));
> > +  else if (XINT (value) <= ULONG_MAX)
> > +    XSETINT (res, bitcount64 (XUINT (value)));
> 
> The comparisons against Uxxx_MAX seem to assume that VALUE is
> unsigned, but that's not guaranteed, right?

True.  Should I instead be doing

  XINT (value) <= xxx_MAX &&
  XINT (value) >= xxx_MIN

or might there be a better check?  For negative values popcount
typically counts ones of the two's complement