www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/22/07:40:57

Message-ID: <8D53104ECD0CD211AF4000A0C9D60AE3579368@probe-2.acclaim-euro.net>
From: Shawn Hargreaves <ShawnH AT Probe DOT co DOT uk>
To: djgpp AT delorie DOT com
Cc: s-fery AT kkt DOT sote DOT hu
Subject: Re: timing with empty loops -- small green ufo-s?
Date: Mon, 22 Feb 1999 12:38:59 -0000
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.0.1460.8)
Reply-To: djgpp AT delorie DOT com

Engard Ferenc writes:
> My program does it's timing with empty loop:
> 
> void do_delay (unsigned long i)
> { while(i--) ; }

I don't think that is a very good way to do a timing delay. This is
incredibly prone to compiler optimisation, and will be affected by 
things like the code alignment, processor cache, and if you inline 
it within a larger function, the surrounding code will change how gcc 
deals with it.

I wouldn't use a delay loop for anything at all critical, which it
sounds like your situation is. If you really insist upon using this
method, though, at the very least you should:

- write it in asm.
- put it a separate .s file, not inlined within C a source.
- use .align directives to make sure the routine cannot move around.
- only have a single copy, used for both calibration and actual delays.

If you do that, it will probably be pretty reliable, as long as you
can guarantee no external interference (eg. interrupts are disabled).
If this needs to be really accurate, though, it would be much better 
to use a proper timing chip. The pentium has some hardware registers
specifically for this purpose, but I'm afraid I don't have any
references to hand on exactly how you would access them...


	Shawn Hargreaves.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019