Register bit count

Z

zephyrtronium

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.
 
K

Keith Thompson

zephyrtronium said:
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.
 
M

Michael Tsang

zephyrtronium said:
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.
 
B

bart.c

zephyrtronium said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top