public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Nathan Sidwell <nathan@acm.org>
To: Wei Qin <wqin@EE.Princeton.EDU>
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 10:36:00 -0000	[thread overview]
Message-ID: <3CC83D87.506F1499@acm.org> (raw)
In-Reply-To: <Pine.SOL.4.43.0204251302420.27320-100000@ivy.ee.princeton.edu>

Wei Qin wrote:
> 
> First, I see only 32 bits being counted. The top 32 bits seems missing.
> Second, this is a very slow implementation. A faster one can be
> implemented through table lookup. It takes no masking, no branching.
> 
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

  reply	other threads:[~2002-04-25 17:33 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 [this message]
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     ` Number of 1's in 64 bit number...[don't read if not interested " Wei Qin
2002-04-25 15:15       ` 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=3CC83D87.506F1499@acm.org \
    --to=nathan@acm.org \
    --cc=gcc-help@gcc.gnu.org \
    --cc=gcc@gcc.gnu.org \
    --cc=kelley.r.cook@gm.com \
    --cc=ms_preeti@hotmail.com \
    --cc=nathan@compsci.bristol.ac.uk \
    --cc=wqin@EE.Princeton.EDU \
    /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).