zephyrtronium <> 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)
kst- <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"