micro-optimizing loops (was Help with casts)

Chris Torek torek at elf.ee.lbl.gov
Tue Feb 26 06:34:23 AEST 1991


In article <15396:Feb2419:32:5891 at kramden.acf.nyu.edu>
brnstnd at kramden.acf.nyu.edu (Dan Bernstein) writes:
>What Chris doesn't point out is that
>
>  i = blah;
>  do
>   {
>    ...
>   }
>  while (--i);
>
>will compile to the optimal code on most machines under most compilers ...

This is, of course, because you have manually moved the loop test to
the bottom.  This almost invariably saves one `jump' instruction, if
nothing else.

>... whether they have exposed jump delays or not ...

Actually,

	do { ... } while (i--);

is a bit easier on some exposed pipeline machines, simply because it
compiles directly to

	jne	r1, r0, loop	# branch if i != 0
	dec	r1		# but do i-- first

without any thought whatsoever.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list