Register bit count

Discussion in 'C Programming' started by zephyrtronium, May 31, 2010.

  1. 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.
    zephyrtronium, May 31, 2010
    #1
    1. Advertising

  2. 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) <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"
    Keith Thompson, May 31, 2010
    #2
    1. Advertising

  3. 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.
    Michael Tsang, May 31, 2010
    #3
  4. zephyrtronium

    bart.c Guest

    "zephyrtronium" <> wrote in message
    news:...
    > 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
    bart.c, May 31, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Vallant

    8 bit into 256 bit shift register

    Christian Vallant, May 23, 2006, in forum: VHDL
    Replies:
    8
    Views:
    3,917
    Mike Treseler
    May 24, 2006
  2. Charles Hixson

    bit count or bit set && Python3

    Charles Hixson, Oct 25, 2012, in forum: Python
    Replies:
    5
    Views:
    200
    Charles Hixson
    Oct 26, 2012
  3. MRAB
    Replies:
    0
    Views:
    143
  4. Steven D'Aprano

    Re: bit count or bit set && Python3

    Steven D'Aprano, Oct 25, 2012, in forum: Python
    Replies:
    12
    Views:
    254
    Neil Cerutti
    Oct 26, 2012
  5. lokesh kumar
    Replies:
    1
    Views:
    400
Loading...

Share This Page