Continuing my overly simplified comparison of 24.8 fixed point vs. floating math on the AVR168/328 chip, I compared the elapsed time of the following two division operations running at 16Mhz:

#define FIXED_BITS 32
#define FIXED_WBITS 24
#define FIXED_FBITS 8
#define FIXED_TO_INT(a) ((a) >> FIXED_FBITS)
#define FIXED_FROM_INT(a) (int32_t)((a) << FIXED_FBITS)
#define FIXED_MAKE(a) (int32_t)((a*(1 << FIXED_FBITS)))
int32_t a, b, c;
float x, y, z;
static int32_t FIXED_Div(int32_t a, int32_t b) {
return(((int32_t)a << FIXED_FBITS)/(int32_t)b);
}
int main(void) {
//floating point
x = 8.0; //11 clock cycles
y = 2.5; //12 clock cycles
z = x/y; //1366 clock cycles (83.88us)
//fixed point
a = FIXED_MAKE(8.0); //11 clock cycles
b = FIXED_MAKE(2.5); //12 clock cycles
c = FIXED_Div(a, b); //725 clock cycles (45.31us)
return 0;
}

That’s nearly 1.8 times faster for the fixed-point math.

If I substitute the 64-bit division routine below, the run time increases for the fixed-point math to 4367 cycles or 272.94us!

static int32_t FIXED_Div(int32_t a, int32_t b) {
return(((int64_t)a << FIXED_FBITS)/(int64_t)b);
}

This mirrors the findings reported here and here.

And see my posting here for basic arduino compatible routines.

### Like this:

Like Loading...

*Related*

Very interesting. Thanks. How do you count the clock cycles?

Either by hand, or the easy method is to run the code inside Atmel Studio, which has a feature to count clock cycles.

To do it by hand, you examine the underlying assembly code and count the clock cycles per the datasheet.