[net.micro.6809] Speed-optimizing 6809 'C' routines

knudsen@ihnss.UUCP (11/02/84)

<list of primes>

I just posted a benchmark timing of the Prime Sieve run on OS-9 C, Level 1,
Coco.  I think our favorite chip came off pretty well, tho can't judge till
I learn what the C64's clock rate is.
	Some hints I learned last nite about speeded up C functions:
(1) Although the 6809 cleary beats other 8-bitters in stack-frame addressing
for automatic variables, you can run even faster (and a tad shorter) by
re-declaring automatic locals as either DIRECT STATIC in the function body
or DIRECT external and DIRECT global outside of any bodies.

(2) Too many variables done as in (1) will overflow your direct page,
so another hint: Pick the variable that's most critical in a function
and declare that one LAST (or FIRST?).  The idea is to give it a zero offset
from the S-register, so it gets accessed as "0,S" == ",S" which is just
as fast (4 cycles for CHAR) as a DIRECT pager.  All other items in the
stack frame will be "n,S" and will take an extra clock cycle, unless n>7
in which case you pay two extra cycles and another byte.  So put other
critical automatics' declarations next to the most critical one.

(3) I don't know to what extent any C compilers take advantage of keeping
pointers in registers (I THINK that Microware C lacks REGISTER declaration),
but when writing assembler position-independent code, I would re-write
	for(i=0; i<SIZE; ) flags[i++]=1;
to the equivalent of:
	int *p,*top;
	top = flags+SIZE;
	p = flags;
	do {*p++ = 1;} while(p < top);

which gives a 3-instruction loop with no LEA's inside it.
Anyway, the point is to adapt your C programming style to the machine
(and compiler) at hand where speed is more important than clarity
(the for-loop seems a lot more clear in its intent, I admit!)  --mike k

jejones@ea.UUCP (11/05/84)

Re Microware C and register declarations: you can indeed use the
register declaration. One such declaration per function will be
honored (the first, I presume); the others will be treated as plain
vanilla automatics.

						James Jones