Oliver Rutsch

HCS08: Why is incrementing a 32 bit integer so slow?

Discussion created by Oliver Rutsch on Jan 12, 2010
Latest reply on Jan 14, 2010 by CompilerGuru



My target is an MC9S08QG8 an I'm wondering why incrementing a 32 bit integer is so expensive.

All measurements were done in the simulator.


For example:



UINT32 Counter=0;

UINT16 lc;


for (lc=;lc<30000;lc++)

  Counter++;  // this line will take about 156 CPU cycles!


So incrementing Counter will take about 156 CPU cycles which is MUCH more than incrementing a 16 bit integer. It's clear that incrementing a 32 bit integer is more effort than a 16 bit integer but the following is still much faster:


// this function takes 37 CPU cycles

void IncUINT32(UINT32 * li)
  ((UINT16 *)li)[1]++;
  if (!((UINT16 *)li)[1])
    ((UINT16 *)li)[0]++;

// This macro takes only 18 CPU cycles but it does not fit in every situation, e.g. for loops
#define INC2_LI(li) ((UINT16 *)&li)[1]++; if (!((UINT16 *)&li)[1]) ((UINT16 *)&li)[0]++;

So, any idea why the compiler produces so slow code with the ++ operator? In the debugger I see that the function "_LINC" is called in RTSHC08.c.