Shop OBEX P1 Docs P2 Docs Learn Events
C code optimization — Parallax Forums

C code optimization

ersmithersmith Posts: 6,099
edited 2011-12-10 06:40 in Propeller 1
I just noticed a funny thing about the pong game I posted in the other forum. I was trying to squeeze its code into COG memory, so I wanted to avoid pulling in the divide function from the library. So I wrote code that looks like:
    tens = 0;
    while (number >= 10) {
        tens++;
        number -= 10;
    }
to calculate the digits of the score.

Imagine my surprise when I found __UDIVSI (the low level divide function) in the symbol table for the output. It turns out that GCC recognized that we were setting "tens = number/10" and re-wrote the loop accordingly!

In this particular case it wasn't really what I wanted, but in the general case it would very much be an improvement. I'm constantly amazed by how good the machine independent optimization in GCC is. I guess that's what you get when you have an open source compiler that's worked on by top people all over the world for 20+ years.

Eric

Comments

  • Heater.Heater. Posts: 21,230
    edited 2011-12-10 06:16
    Amazing.
    Now I get it, what GCC did with my FFT is to throw it away and put in a better one:)
  • mindrobotsmindrobots Posts: 6,506
    edited 2011-12-10 06:40
    Compilers are becoming frighteningly good. The last time one optimized my code, it printed out an invitation to a career change seminar!! :smile:
Sign In or Register to comment.