[comp.lang.c] register variable declaration question

jwr@scotty.UUCP (Dier Retlaw Semaj) (11/08/88)

A question:

When should one declare a parameter to be a register variable?

For example:

int
wally(the_beav)
char 
	*the_beav;
{
	{ Lots of stuff using the_beav }
}

Am I missing the boat entirely on this matter?

Is there a cutoff point
where declaring the parameter to be a register variable is no longer efficient?

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

I have wondered this off & on for quite some time.
Thanks for your time.

-- 

Dier R. Semaj	{ames,cmcl2,rutgers}!rochester!kodak!fedsys!wally!jwr

--

djones@megatest.UUCP (Dave Jones) (11/09/88)

From article <624@scotty.UUCP>, by jwr@scotty.UUCP (Dier Retlaw Semaj):
> 
> A question:
> 
> When should one declare a parameter to be a register variable?
> 

I can't tell you when you "should" declare a register parameter,
but I can give you an example of how I use them.

I write lot's of C routines which are in the "object-oriented" mold.
That is to say, they update only one kind of data-structure.
I name the structures ("classes", in the current jargon) with typenames
with capital letters.  If a routine-name begins with a capitol letter,
the reader knows theat it is tied to a class -- it is a "method"
of that class.  It's first parameter, named "obj",  will invariably be 
a pointer to a structure of the given type.  I usually declare that pointer
to be "register", because it will be dereferenced many times within
the procedure.

typedef struct
{
  type1 field1;
  type2 field2;
  /* etc. */
}Widget;


int
Widget_fiddle(obj, arg1, arg2)
   register Widget* obj;
   int arg1;
   int arg2;
{
  /* Fiddle with the widget. */

}

henry@utzoo.uucp (Henry Spencer) (11/10/88)

In article <624@scotty.UUCP> jwr@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@zoo.toronto.edu