Search code examples
arduinoavravr-gccatmelatmega

ATMega peformance for different operations


Has anyone experiences replacing floating point operations on ATMega (2560) based systems? There are a couple of very common situations which happen every day.

For example:

  • Are comparisons faster than divisions/multiplications?

  • Are float to int type cast with followed multiplication/division faster than pure floating point operations without type cast?

I hope I don't have to make a benchmark just for me.

Example one:

int iPartialRes = (int)fArg1 * (int)fArg2;
iPartialRes *= iFoo;

faster as?:

float fPartialRes = fArg1 * fArg2;
fPartialRes *= iFoo;

And example two:

iSign = fVal < 0 ? -1 : 1;

faster as?:

iSign = fVal / fabs(fVal);

Solution

  • the questions could be solved just by thinking a moment about it.

    1. AVRs does not have a FPU so all floating point related stuff is done in software --> fp multiplication involves much more than a simple int multiplication

    2. since AVRs also does not have a integer division unit a simple branch is also much faster than a software division. if dividing floating points this is the worst worst case :)

    but please note, that your first 2 examples produce very different results.