Arduino Timing Failure

Fixed-Point Math Timing
Why does this attempt to time a fixed-point math multiplication yield 0 microseconds?

//32-bit fixed point
#define FIXED_BITS           32
#define FIXED_LBITS          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))) 

static int32_t FIXED_Mul(int32_t a, int32_t b) {
  return(((int32_t)a*(int32_t)b) >> FIXED_FBITS);
}

void setup() {
  int32_t x1, x2, x3;
  uint32_t t1, t2;

  Serial.begin(9600);

  t1 = micros();  //start time
  x1 = FIXED_FROM_INT(8);
  x2 = FIXED_MAKE(2.5);
  x3 = FIXED_Mul(x1, x2);
  t2 = micros();  //end time

  Serial.print(x3);
  Serial.println(t2 - t1);  //duration
}

void loop() {
}

After examining the resultant assembly code produced, it appears the optimizing compiler performed the math, eliminated the fixed-point code and treated the answer as a constant. Look here at the disassembly of the object file produced by the Arduino IDE:

  t1 = micros();
 12a:	7f d0       	rcall	.+254    	; 0x22a
 12c:	7b 01       	movw	r14, r22
 12e:	8c 01       	movw	r16, r24
  x1 = FIXED_FROM_INT(8);
  x2 = FIXED_MAKE(2.5);
  x3 = FIXED_Mul(x1, x2);
  t2 = micros();
 130:	7c d0       	rcall	.+248    	; 0x22a
 132:	5b 01       	movw	r10, r22
 134:	6c 01       	movw	r12, r24

Nothing happens between the two calls to micros(). Not exactly what I expected.

About these ads

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