This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Code portability

Hello,
I was browsing through older posts that deal with the painful issue of portability (http://www.keil.com/forum/docs/thread8109.asp). I was (and still am) a big advocate of programming as much as possible conforming to the C standard, and having a layered structure that allowed "plugging-in" other hardware. But I have come to change my mind recently. I am reading the "ARM system developer's guide" (excellent book by the way. I'm reading it because I want to port some C167 code to an ARM9 environment) in which chapter 5 discusses writing efficient C code for an ARM. The point is, and it is fairly demonstrated, that even common, innocent looking C code can either be efficient of very inefficient on an ARM depending on specific choices made, let alone another processor used! So, if we are talking about squeezing every clock cycle out of a microcontroller - I do not believe that portability without ultimately littering the code is possible!

Parents
  • A reasonable approach, but if "processor pleasing C" will do, then why not use that?

    Because you can't guarantee that a 'C' construct will compile to the same object code each time.

    OH????
    I would like you to justify yor statement re the following pseudo code:

    ring_buffer[30]

    ring index++
    if ringindex%30
    ring index = 0

    as ooposed to
    ring buffer[32]
    ring index++
    ring index &= 0xc0

    I have no doubt what is the most "processor pleasing C" and no optimization will change which is the most efficient.

    Erik

Reply
  • A reasonable approach, but if "processor pleasing C" will do, then why not use that?

    Because you can't guarantee that a 'C' construct will compile to the same object code each time.

    OH????
    I would like you to justify yor statement re the following pseudo code:

    ring_buffer[30]

    ring index++
    if ringindex%30
    ring index = 0

    as ooposed to
    ring buffer[32]
    ring index++
    ring index &= 0xc0

    I have no doubt what is the most "processor pleasing C" and no optimization will change which is the most efficient.

    Erik

Children
  • A reasonable approach, but if "processor pleasing C" will do, then why not use that?

    Because you can't guarantee that a 'C' construct will compile to the same object code each time. Any change in compiler version or optimisation level may affect things, and as you will no doubt be using the highest optimisation level given that 'every clock cycle counts' any change to an unrelated area of code may alter timing in the critical parts.

    OH????
    I would like you to justify yor statement re the following pseudo code:

    It would be difficult to justify anything more than a career flipping burgers from the following code:

    ring_buffer[30]

    ring index++
    if ringindex%30
    ring index = 0

    as ooposed to
    ring buffer[32]
    ring index++
    ring index &= 0xc0

    I have no doubt what is the most "processor pleasing C" and no optimization will change which is the most efficient.

    The intent in your first example doesn't offer the same opportunity for optimisation as the second. Providing bits of 'code' that *try* to do different things isn't really on, is it?

    Here's a challenge for you: convert your 'pseudo-code' back into 'C' by removing the spaces from the variable names and replacing the semicolons, then see if you can spot the errors.