Message-ID: From: Shawn Hargreaves To: djgpp AT delorie DOT com Subject: Re: I need to advice on timing... Date: Wed, 2 Feb 2000 12:55:04 -0000 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2650.21) Content-Type: text/plain; charset="iso-8859-1" Reply-To: djgpp AT delorie DOT com Dieter Buerssner writes: >>I don't know the details of exactly why this is, but presumably it is >>something to do with branch prediction, or forcing which of the two >>Pentium execution units will be used for the main loop. > > I cannot follow your argument, anyway. I would have thougt, that > such a delay loop should be, as much as possible, independant from > the processor type. Your mention of "Pentium execution units" seems > to contradict this. Anyway, I can not think of any easier case for > the branch prediction logic, than this code. It should almost always > do right. Sure, but 'almost always' isn't so good for a timing loop. Even if only one time in a hundred it doesn't get the branch prediction right, that would alter the time spent in the loop, which is a bad thing (it doesn't matter how slow this code is, as long as it stays consistent each time you call it). So I think the jumps at the beginning are not to force good branch prediction, but actually to confuse the processor and force _bad_ predictions, to make sure that it will always be equally bad, and therefore consistent. Likewise the execution units thing: modern processors can execute several instructions in parallel, at least in certain situations, which could again affect the accuracy of the measurements. The chain of jumps prevents any parallel execution, though, which makes sure that by the time it reaches the actual timing loop, the code will always be executed the same way, with instructions going through the exact same parallel execution methods. Shawn Hargreaves.