register variables

alex at ucla-cs.UUCP alex at ucla-cs.UUCP
Tue Jun 25 03:05:33 AEST 1985


>Just be somewhat careful to declare things in the order you care
>about.  This cuts both ways -- if you move to a machine with fewer
>registers than yours, you can loose the most critical register uses in
>favor of less critical.  For example, let's posit a two register
>machine and the following code
>
>	register int	i, j;
>
>	for (i = 0; ...)
>		for (j = 0; ...)
>
>Now, move it to a one register machine, and "j", which is the most
>tested variable, is out of a register and "i", which is used less
>often, is still in one.  Not what you want.

You are assuming the compiler assigns register variables in the order
they are declared, which is not always true.  An alternative solution
is to do something like this:

#define REG1 register
#define REG2 register
 ...
#define REG8 register                /* assume 8 registers on our machine */
#define REG9
 ...
#define REG20                        /* but up to 20 variables that might */
				     /* be put into registers */

Then declare the local variables as

  REG1 int j;     /* j is most important */
  REG2 int i;     /* i is next most important */
  ...

The register #defines can go in a header file that uses #ifdefs to
do the #defines appropriately for the machine you are using.  This
method makes it clear which variables are important to be placed in
registers and which aren't.

Alex



More information about the Comp.lang.c mailing list