Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Register bit count

Reply
Thread Tools

Register bit count

 
 
zephyrtronium
Guest
Posts: n/a
 
      05-31-2010
I'm looking for a portable way to get the maximum number of bits in a
register, preferably at run time. Is there an easier method than
getting a popcount of a negated 0? If not, do I need 0, 0L, or 0LL? My
current solution (using GCC 4.5.0) is __builtin_popcount(~0L) which
gives me 32 on my 32-bit machine, but will that work in every case?

It seems __builtin_popcount(~0LL) also returns 32. Would that then
work in 64-bit processors? What about others? What if the processor
instruction set doesn't contain a popcount instruction?

Sorry if none of this makes any sense; I probably should have gone to
sleep many hours ago.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-31-2010
zephyrtronium <(E-Mail Removed)> writes:
> I'm looking for a portable way to get the maximum number of bits in a
> register, preferably at run time. Is there an easier method than
> getting a popcount of a negated 0? If not, do I need 0, 0L, or 0LL? My
> current solution (using GCC 4.5.0) is __builtin_popcount(~0L) which
> gives me 32 on my 32-bit machine, but will that work in every case?
>
> It seems __builtin_popcount(~0LL) also returns 32. Would that then
> work in 64-bit processors? What about others? What if the processor
> instruction set doesn't contain a popcount instruction?
>
> Sorry if none of this makes any sense; I probably should have gone to
> sleep many hours ago.


Each of 0, 0L, and 0LL is of a language-specified type: int, long,
and long long respectively. If you know the type anyway, it's easy
to determine the number of bits in the type: CHAR_BIT * sizeof(int)
and so forth. This ignores the possibility of padding bits, but
most implementations don't have padding bits for integer types.

<OT>If you want to use gcc's __builtin_popcount(), you should be
aware of __builtin_popcountl() and __builtin_popcountll(); see the
gcc documentation for more information.</OT>

But this still doesn't tell you anything about the size of a
register, since C doesn't specify anything about registers.
Note also that some systems have registers of different sizes.

What use do you intend to make of the information? As far as I can
tell, there's not really anything you can portably do with
the size of a register.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Michael Tsang
Guest
Posts: n/a
 
      05-31-2010
zephyrtronium wrote:

> I'm looking for a portable way to get the maximum number of bits in a
> register, preferably at run time. Is there an easier method than
> getting a popcount of a negated 0? If not, do I need 0, 0L, or 0LL? My
> current solution (using GCC 4.5.0) is __builtin_popcount(~0L) which
> gives me 32 on my 32-bit machine, but will that work in every case?
>
> It seems __builtin_popcount(~0LL) also returns 32. Would that then
> work in 64-bit processors? What about others? What if the processor
> instruction set doesn't contain a popcount instruction?
>
> Sorry if none of this makes any sense; I probably should have gone to
> sleep many hours ago.

"Registers" are hardware-specific so there is no portable way to do this.
 
Reply With Quote
 
bart.c
Guest
Posts: n/a
 
      05-31-2010

"zephyrtronium" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm looking for a portable way to get the maximum number of bits in a
> register, preferably at run time. Is there an easier method than
> getting a popcount of a negated 0? If not, do I need 0, 0L, or 0LL? My
> current solution (using GCC 4.5.0) is __builtin_popcount(~0L) which
> gives me 32 on my 32-bit machine, but will that work in every case?
>
> It seems __builtin_popcount(~0LL) also returns 32. Would that then
> work in 64-bit processors? What about others? What if the processor
> instruction set doesn't contain a popcount instruction?


Probably then it would be done in software. And it might return 32 or 64
whatever the actual register size might be.

What's the signature for the popcount() function?

--
Bartc

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bit count or bit set && Python3 Charles Hixson Python 5 10-26-2012 04:30 PM
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
8 bit into 256 bit shift register Christian Vallant VHDL 8 05-23-2006 11:02 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments