Re: Fastest way to byte address ints?

Discussion in 'C++' started by Marcel Müller, Mar 4, 2010.

  1. Peter Olcott wrote:
    > I had always been using:
    >
    > union {
    > uint8 Bytes[4];
    > uint32 Integer;
    > };


    You must not read another field of a union than you wrote the last time.
    Everything else is undefined behavior.

    > because it worked on my platform up to now.


    This is another question.

    > Is this the right syntax for addressing the lowest order
    > three bytes of an int?


    > unsigned int Integer;
    > unsigned char 1stByte = *( ( (unsigned char*) &Integer) +
    > 0 )
    > unsigned char 2ndByte = *( ( (unsigned char*) &Integer) +
    > 1 )
    > unsigned char 3rdByte = *( ( (unsigned char*) &Integer) +
    > 2 )


    No. This will badly fail on big endian machines where the lowest order
    byte is at index 3. Furthermore it will not work on rvalues.

    The only portable way is by using shift operators.
    unsigned char 2stByte = (unsigned char)((Integer >> 8) & 0xff);

    > Is there any other way that would execute faster?


    The reinterpret_cast usually breaks some compiler optimizations. E.g. it
    will most likely no longer be able to store the content of integer in a
    register. So your coding could backfire (besides it is non-portable).

    However, the question about speed mainly depends on your platform and
    you compiler. Some platforms have barrel shifters where >>8 takes no
    longer than other basic arithmetic operations. Some compilers can detect
    byte access by the shift operators and optimize their generated code if
    the data is taken from a memory location anyway or byte swap operations
    are available. Some platforms may cycle until the data is shifted to the
    right location.

    If speed really counts, implement an inline assembly function protected
    by #ifdef to match the platforms where it applies to and fall back to
    the portable solution if you cannot identify the platform. This is often
    a reasonable compromise.
    But you should check the available optimizations of your compiler first.
    Compilers have grown well up to these days. And sometimes their code is
    better than any assembler you write, e.g. because the hand made assembly
    breaks instruction scheduling and instruction alignment.


    Marcel
    Marcel Müller, Mar 4, 2010
    #1
    1. Advertising

  2. Marcel Müller

    REH Guest

    On Mar 4, 8:18 pm, Juha Nieminen <> wrote:
    > Daniel T. wrote:
    > > I agree with Marcel. Strictly speaking though, his code is assuming 8
    > > bit bytes. For maximum flexibility, you would need to use CHAR_BIT and
    > > CHAR_MAX. I used to routinely have to write code that worked on both big
    > > and small endien systems, but I've never worked with a system where a
    > > byte was more (or less) than 8 bits.

    >
    >   I don't think using CHAR_BIT is very relevant. Can you name even one
    > single computer (obsolete or otherwise) with a char type which is not 8
    > bits long and which has a standard C++ compiler? (Some mainframes from
    > the 60's and 70's had non-8-bit chars, but C++ didn't even exist back
    > then, and wasn't even standardized until 1998.)
    >
    >   Even if such an obscure architecture and C++ compiler pair exists (I
    > honestly don't know), I don't think anybody is going to be compiling the
    > original poster's programs for it.
    >


    Yes, pretty much every modern DSP I've ever written code for (using C,
    C++, and Ada) has 32-bit char.

    REH
    REH, Mar 5, 2010
    #2
    1. Advertising

  3. Marcel Müller

    James Kanze Guest

    On 5 Mar, 01:18, Juha Nieminen <> wrote:
    > Daniel T. wrote:
    > > I agree with Marcel. Strictly speaking though, his code is
    > > assuming 8 bit bytes. For maximum flexibility, you would
    > > need to use CHAR_BIT and CHAR_MAX. I used to routinely have
    > > to write code that worked on both big and small endien
    > > systems, but I've never worked with a system where a byte
    > > was more (or less) than 8 bits.


    > I don't think using CHAR_BIT is very relevant. Can you name
    > even one single computer (obsolete or otherwise) with a char
    > type which is not 8 bits long and which has a standard C++
    > compiler? (Some mainframes from the 60's and 70's had
    > non-8-bit chars, but C++ didn't even exist back then, and
    > wasn't even standardized until 1998.)


    Unisys 2200. It's still being sold, and has a C++ compiler.
    (At one time, at least, it's manual was available on line, but I
    can't find it at present.) It's a 36 bit one's complement
    machine, with 9 bit bytes.

    From what I've heard, some embedded processors or signal
    processors have 32 bit char's. Some of the most modern, in
    fact.

    > Even if such an obscure architecture and C++ compiler pair
    > exists (I honestly don't know), I don't think anybody is going
    > to be compiling the original poster's programs for it.


    Yes. It's worth realizing that the possibility exists, but most
    people don't need that degree of portability.

    Also, when converting int's to bytes (or vice versa), the usual
    goal is data transmission. And even if internally, your char is
    9 bits, the transmission protocols want 8. In this case, using
    CHAR_BIT instead of 8 is an error.

    --
    James Kanze
    James Kanze, Mar 5, 2010
    #3
    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. Chris
    Replies:
    5
    Views:
    396
    Randolf Richardson
    Jan 28, 2007
  2. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    820
    Jack Klein
    Jul 10, 2004
  3. Laszlo Nagy
    Replies:
    2
    Views:
    262
    Matthew Woodcraft
    Aug 9, 2008
  4. M.-A. Lemburg
    Replies:
    3
    Views:
    270
    castironpi
    Aug 10, 2008
  5. Joshua Maurice

    Re: Fastest way to byte address ints?

    Joshua Maurice, Mar 5, 2010, in forum: C++
    Replies:
    0
    Views:
    422
    Joshua Maurice
    Mar 5, 2010
Loading...

Share This Page