Continued Look at Fixed Point Math on the Arduino Platform

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.

Advertisements

About Jim Eli

µC experimenter
This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s