Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!agate!dog.ee.lbl.gov!elf.ee.lbl.gov!torek From: torek@elf.ee.lbl.gov (Chris Torek) Newsgroups: comp.lang.c Subject: Re: micro-optimizing loops (was Help with casts) Message-ID: <10248@dog.ee.lbl.gov> Date: 25 Feb 91 19:34:23 GMT References: <339@smds.UUCP> <414@ceco.ceco.com> <10191@dog.ee.lbl.gov> <15396:Feb2419:32:5891@kramden.acf.nyu.edu> Reply-To: torek@elf.ee.lbl.gov (Chris Torek) Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 33 X-Local-Date: Mon, 25 Feb 91 11:34:23 PST In article <15396:Feb2419:32:5891@kramden.acf.nyu.edu> brnstnd@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@ee.lbl.gov