public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Wei Qin <wqin@EE.Princeton.EDU>
To: kelley.r.cook@gm.com
Cc: Preeti Aira <ms_preeti@hotmail.com>, <gcc@gcc.gnu.org>,
	<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:13:00 -0000	[thread overview]
Message-ID: <Pine.SOL.4.43.0204251302420.27320-100000@ivy.ee.princeton.edu> (raw)
In-Reply-To: <OFA35D7DD4.BD1129AF-ON85256BA6.00569284@mail.gm.com>


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.

/*need to generate the table yourself*/
unsigned char mytable[256] = {0, 1, 1, 2, 1, ...};
int count (long long x) {

	int ret;
	union {
		long long val;
		unsigned char s[8];
	} a;

	ret = mytable[a.s[0]];
	ret += mytable[a.s[1]];
	....
	ret += mytable[a.s[7]];

	return ret;
}


On Thu, 25 Apr 2002 kelley.r.cook@gm.com wrote:

> int count (long long x) {
>
> int result;
>
> result =
> (x & 0x00000001 ? 1 : 0) +
> (x & 0x00000002 ? 1 : 0) +
> (x & 0x00000004 ? 1 : 0) +
> (x & 0x00000008 ? 1 : 0) +
> (x & 0x00000010 ? 1 : 0) +
> (x & 0x00000020 ? 1 : 0) +
> (x & 0x00000040 ? 1 : 0) +
> (x & 0x00000080 ? 1 : 0) +
> (x & 0x00000100 ? 1 : 0) +
> (x & 0x00000200 ? 1 : 0) +
> (x & 0x00000400 ? 1 : 0) +
> (x & 0x00000800 ? 1 : 0) +
> (x & 0x00001000 ? 1 : 0) +
> (x & 0x00002000 ? 1 : 0) +
> (x & 0x00004000 ? 1 : 0) +
> (x & 0x00008000 ? 1 : 0) +
> (x & 0x00010000 ? 1 : 0) +
> (x & 0x00020000 ? 1 : 0) +
> (x & 0x00040000 ? 1 : 0) +
> (x & 0x00080000 ? 1 : 0) +
> (x & 0x00100000 ? 1 : 0) +
> (x & 0x00200000 ? 1 : 0) +
> (x & 0x00400000 ? 1 : 0) +
> (x & 0x00800000 ? 1 : 0) +
> (x & 0x01000000 ? 1 : 0) +
> (x & 0x02000000 ? 1 : 0) +
> (x & 0x04000000 ? 1 : 0) +
> (x & 0x08000000 ? 1 : 0) +
> (x & 0x10000000 ? 1 : 0) +
> (x & 0x20000000 ? 1 : 0) +
> (x & 0x40000000 ? 1 : 0) +
> (x & 0x80000000 ? 1 : 0);
> return result;
> }
>
>

  reply	other threads:[~2002-04-25 17:08 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 [this message]
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     ` 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=Pine.SOL.4.43.0204251302420.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 \
    /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).