Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!adm!rbj@icst-cmr.arpa
From: rbj@icst-cmr.arpa (Root Boy Jim)
Newsgroups: comp.unix.questions
Subject: Wierd Compilers
Message-ID: <10861@brl-adm.ARPA>
Date: 15 Dec 87 15:51:35 GMT
Sender: news@brl-adm.ARPA
Lines: 44


   From: Dave Caswell 

BTW, that's `weird'.

   Does anyone have a guess why program 1 runs in half the time of program 2?

By now you have seen assembly code listings, with and without optimization,
which should make things clear. I'll attempt to address a few points
which nobody has made as of yet.

There seem to be four basic styles of while loops; one dimension is
pre-/post-decrement, the other is `while () ...;' vs `do ... while ();'

You can get n-1, n, n, and n+1 repetitions out of the constructs
`while (--n) ...;', `while (n--) ...;', `do ... while (--n)', and
`do ... while (n--);' respectively. 

I always thought that the `do ... while();' syntax was a gratuitous
addition to the language until someone pointed out that it was tailored
to the PDP-11 `sob' (subtract one and branch if *not* zero) instruction.

The VAX has (is?) an `sob' as well. So does the 680[012]0 as well, altho
it's called `dbra', a special case of the `DBcc', wher `cc' is one of
any sixteen branch conditions. Oddly, the SUN compilers don't seem to
make use of it, so what I am about to say next is irrelevant.

Another feature of the 680[12]0, but not the 68000, is what is called
`loop' mode. If a one word instruxion (oops! back to my old habits :-)
is followed by a DBcc that references it, the whole loop is executed
without refetching the two instructions (got it right this time :-).
Thus, if we could coax the compiler into using dbra, the best way to
copy register int n bytes from register char *p to register char *q
would be `do *q++ = *p++; while (--n);'.

   BTW this is a SUN 3.

So that would be a 68020. The 68020 may handle bigger loops in loop
mode as it has a bigger instruction prefetch queue.

	(Root Boy) Jim Cottrell	
	National Bureau of Standards
	Flamer's Hotline: (301) 975-5688
	Isn't this my STOP?!