public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Wei Qin <wqin@EE.Princeton.EDU>
To: nathan@cs.bris.ac.uk
Cc: kelley.r.cook@gm.com, Preeti Aira <ms_preeti@hotmail.com>,
	gcc <gcc@gcc.gnu.org>, gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Number of 1's in 64 bit number...[don't read if not interested in          algos/math...]
Date: Thu, 25 Apr 2002 12:15:00 -0000	[thread overview]
Message-ID: <Pine.SOL.4.43.0204251500020.27320-100000@ivy.ee.princeton.edu> (raw)
In-Reply-To: <3CC83D87.506F1499@acm.org>


This is very interesting code. A friend of mine tried it for 64bit
popcount and compared it with the tablelook up one. He inlines the
function and run the 32 bit bitson twice.
On a x86 linux platform, it takes 80% more time than the table lookup one.
I have seen some more interesting code which does

int popcount(unsigned long long x) {

#define TWO(k) (BIGONE << (k))
#define CYCL(k) (BIGNONE/(1 + (TWO(TWO(k)))))
#define BSUM(x,k) ((x)+=(x) >> TWO(k), (x) &= CYCL(k))

  x = (x & CYCL(0)) + ((x>>TWO(0)) & CYCL(0));
  x = (x & CYCL(1)) + ((x>>TWO(1)) & CYCL(1));
  BSUM(x,2);
  BSUM(x,3);
  BSUM(x,4);
  BSUM(x,5);
}

But it is even slower.


Wei


On Thu, 25 Apr 2002, Nathan Sidwell wrote:

> >
> I suspect something like...
> int bitson (unsigned long x)
> {
>   x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
>   x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
>   x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
>   x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
>   x = (x & 0x0000FFFF) + ((x >> 16) & 0x0000FFFF);
>   return x;
> }
> might be better (you can extend that to long long quite simply), and
> some of those later masks will be unnecessary as you can prove carry
> won't happen
>
> nathan
>
> --
> Dr Nathan Sidwell :: Computer Science Department :: Bristol University
>            The voices in my head told me to say this
> nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk
>


  parent reply	other threads:[~2002-04-25 19:13 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-25  9:46 kelley.r.cook
2002-04-25 10:13 ` Wei Qin
2002-04-25 10:36   ` Nathan Sidwell
2002-04-25 11:24     ` Gabriel Paubert
2002-04-26  2:41       ` [OT] Re: Number of 1's in 64 bit number...[don't read if not intereste d " Bernd Jendrissek
2002-04-26  4:23         ` Gabriel Paubert
2002-04-25 12:15     ` Wei Qin [this message]
2002-04-25 15:15       ` Number of 1's in 64 bit number...[don't read if not interested " Richard Henderson
2002-04-25 15:47         ` Wei Qin
2002-04-25 17:55   ` Daniel Berlin
  -- strict thread matches above, loose matches on Subject: below --
2002-04-27 12:44 David Rasmussen
2002-04-24 21:55 Preeti Aira
2002-04-24 22:35 ` Alan Modra
2002-04-24 23:08 ` Vivek Srivastava
2002-04-24 23:51   ` Jaswant
2002-04-25  0:39   ` Gerald Pfeifer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.SOL.4.43.0204251500020.27320-100000@ivy.ee.princeton.edu \
    --to=wqin@ee.princeton.edu \
    --cc=gcc-help@gcc.gnu.org \
    --cc=gcc@gcc.gnu.org \
    --cc=kelley.r.cook@gm.com \
    --cc=ms_preeti@hotmail.com \
    --cc=nathan@cs.bris.ac.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).