A nasty got ya with timing loops and GCC

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This got me the other day and its something that other compilers dont always do and that is the complete removal of timing delay loops such as
{int sum=0;
int ix;

for (ix=0;ix!=10000;ix++)
sum+= something;
}

if sum can provably be shown not to be visible outside the loop ,and the loop can be shown to be finite gcc will remove the loop.
this was something that didnt happen in earlier versions of GCC because it was assumed,quite rightly that the only reason you would have an 'empty'loop was for timing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Moving invariant code out of a loop, or precomputing an invariant result computed in a loop are things good c-compilers can do to optimize code.

Try the following inside your delay loop

asm volatile ("nop");

or

asm volatile ("");

- John

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes I encounted same problem.after compiled program using winavr,some program code been removed without warning.maybe I missed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use _delay_loop_1() or _delay_loop_2() from , or
their convenience counterparts _delay_us() and _delay_ms().

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or make either ix or sum 'volatile' (which basically has the effect of saying "don't optimise these").

Or (very silly idea!) build the file/project with -O0

But, like Jörg says, home made delay routines are pointless when all the (well tried and tested) routines you need already exist in but if you use those make sure you DON'T build -O0 !

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> but if you use those make sure you DON'T build -O0

That's only half of the truth. _delay_loop_1 and _delay_loop_2
are fine with any optimization level. It's only the convenience
functions _delay_us and _delay_ms that require optimization.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jörg,

I have a sneaking suspicion that beginners are more likely drawn to use _delay_us and _delay_ms as they have interesting sounding names ;-)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use "home made" delay loops using templates to have "proper" timing loops using ms/us/ns even with -O0. Nice to have the convenience when debugging since it doesn't depend on the optimization level.

Robert

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> I use "home made" delay loops using templates to have "proper"
> timing loops using ms/us/ns even with -O0.

Given that with -O0, all bets on timing are off anyway, what does that
help you?

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dl8dtl wrote:

Given that with -O0, all bets on timing are off anyway, what does that
help you?

Not that much except that the actual "wait" is almost correct so if I'm toggling a pin using a delay and looking at the output of the said pin, it will be almost correct even with -O0.

Also, using delay_ms drags in the floating point support and blows the memory budget quite easily and this approach doesn't.

/Robert

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No _delay_ms() does NOT drag in the floating support if you use it as described in the manual. It's true that if you build -O0 it will but when you follow the usage advice and build with optimisation on the floating point calculations are done at compile time so no run time float library is required.

(but who in their right mind would build -O0?)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
No _delay_ms() does NOT drag in the floating support if you use it as described in the manual. It's true that if you build -O0 it will but when you follow the usage advice and build with optimisation on the floating point calculations are done at compile time so no run time float library is required.

(but who in their right mind would build -O0?)

Cliff

Hey, relax!

I did NOT say that _delay_ms pulls in the floating point support when optimization is turned on and I'm well aware that it works as supposed when proper optimization options are used.

I merley said that if one want to run _without_ optimization _delay_ms might blow your budget and generate a delay that is out of this world and the solution I use does not have this problem.

Anyway, I just wanted to give input on my approach, which I happen to like. People are free to use any approach they like and the builtin functions in avrlibc are very good.

/Robert