register variable declaration question

Henry Spencer henry at utzoo.uucp
Thu Nov 10 03:58:33 AEST 1988


In article <624 at scotty.UUCP> jwr at scotty.UUCP (Dier Retlaw Semaj) writes:
>When should one declare a parameter to be a register variable? ...
>Is there a cutoff point where declaring the parameter
>to be a register variable is no longer efficient?

A precise answer is very implementation-dependent.  Modern compilers will
often ignore "register" entirely anyway, and figure it out for themselves.
(NB, the 4BSD VAX compilers are antiques.)  A reasonable rule of thumb is
that it's worth making a parameter "register" only if it is used more than
two or three times within the function.

Whether you *should* make the parameter "register", given that it appears
to be worthwhile, depends on how many other "register" variables you have
in the function.  Simpleminded compilers will often fill available registers
first-come-first-served, so if registers are scarce, a "register" parameter
used half a dozen times might get a register when a "register" loop
variable used thousands of times doesn't.

There is no entirely graceful solution to this.  The number 3 is magic
for historical reasons (the pdp11 had 3 user-available registers), and
one can also argue that if "register" is any good at all, there will
probably be 3 or 4 of them.  What I tend to do is to use "register" on
the three "hottest" variables, declare the rest "REGISTER", insert
the following near the beginning:

	#ifndef REGISTER
	#define	REGISTER	register
	#endif

and then use "-DREGISTER=" if I'm working on a machine with few registers.
More elaborate schemes are possible, but I suspect they're not worth the
added trouble unless you have zillions of local variables in a single
function.

>Should I immediately assign
>the value of the parameter to a local register variable?

This is unlikely to have any advantage over declaring the parameter itself
"register", and might even be worse.
-- 
The Earth is our mother.        |    Henry Spencer at U of Toronto Zoology
Our nine months are up.         |uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list