Register parameters


S

sensei

I have recently encountered an old code in K&R style which declared
function parameters as register. That puzzles me, and I know this is
implementation defined, and depends on architecture, compiler and so
on. But my question is, how can be a parameter made "faster" (6.7.1,
with note 103).

On Intel and similar implementations all parameters are, as far as I
know, pushed into the stack. On PPC they can be passed through
registers.

Can someone clarify the use of this keyword and how it may work for
function parameters in a real-world example with "faster access" than a
normal parameter?

I know this is, again, implementation defined, but I am not interested
in a particular compiler or architecture, I am curious about *some*
examples of them, if any.

Thanks!
 
Ad

Advertisements

E

Eric Sosman

I have recently encountered an old code in K&R style which declared
function parameters as register. That puzzles me, and I know this is
implementation defined, and depends on architecture, compiler and so
on. But my question is, how can be a parameter made "faster" (6.7.1,
with note 103).

Aside: I suspect you mean "note 103" in some edition of the
C99 Standard. In the current C11 Standard the corresponding note
is 121. Successive Standard versions apparently try to keep the
section and paragraph numbering similar, where that makes sense,
but pay no heed to notes. So when referring to a note, it's a
good idea to mention which document version you're using. Even
better, say "the note to 9.8.7p6" -- even better still, quote
the note!
On Intel and similar implementations all parameters are, as far as I
know, pushed into the stack. On PPC they can be passed through
registers.

Can someone clarify the use of this keyword and how it may work for
function parameters in a real-world example with "faster access" than a
normal parameter?

I know this is, again, implementation defined, but I am not interested
in a particular compiler or architecture, I am curious about *some*
examples of them, if any.

Bear in mind that a function parameter is very much like a
local variable: It behaves in most respects like an `auto' variable
of the function's block, magically initialized with the argument
value. Attaching `register' to the parameter suggests a desire for
fast access *to that local variable*, regardless of the argument-
passing mechanism that initializes it.

So: On systems where argument values are passed on a stack,
`register' suggests that it might be a good idea for the called
function to copy the argument into a CPU register and use that
register as the parameter variable. On systems where argument
values are passed in registers, `register' suggests that it might
be a good idea if the called function left the argument where it
was instead of storing it in memory and re-using the register for
some other purpose. There are as many other possibilities as
there are different argument-passing mechanisms.

... and, as you may be aware, `register' is nowadays pretty
much a dead letter, whose only meaning (to many compilers) is
"Complain if someone tries to compute the address of: ___."
 
M

Malcolm McLean

בת×ריך ×™×•× ×©×‘×ª,21 ביולי 2012 21:42:14 UTC+1, מ×ת sensei:
I have recently encountered an old code in K & R style which declared
function parameters as register. That puzzles me,
Very simplistic compilers used to always pass parameters on the stack, unless explicitly declared register. So you could get quite a decent speed up by declaring them register and then being careful not to try to preserve them across subroutine calls.
It's an obsolete technique now, as far as I know.
 
B

Ben Bacarisse

sensei said:
I have recently encountered an old code in K&R style which declared
function parameters as register. That puzzles me, and I know this is
implementation defined, and depends on architecture, compiler and so
on. But my question is, how can be a parameter made "faster" (6.7.1,
with note 103).

On Intel and similar implementations all parameters are, as far as I
know, pushed into the stack. On PPC they can be passed through
registers.

Can someone clarify the use of this keyword and how it may work for
function parameters in a real-world example with "faster access" than a
normal parameter?

First off, I should say that the received wisdom is that using
"register" for speed is at best pointless these days; the reason being
that compilers are better at optimising code if you leave them to it
unfettered, so many (most?) just ignore this keyword. I have to say
the "received wisdom" because I've not seen the inside of a C compiler
for (yikes!) 30 years.

However, in those days it was not uncommon to see parameters declared
with the storage class "register". In the compiler I knew reasonably
well back then, this did not affect the way the argument was passed, but
it would try to get the value into a register as soon as possible and to
keep it there as long as possible. This only helped, of course, if the
parameter was used a lot -- particularly if it was modified a lot.

<snip>
 
R

Ralf Damaschke

Malcolm McLean said:
Very simplistic compilers used to always pass parameters on the
stack, unless explicitly declared register.

That use of "register" would be a bit tricky since the keyword is
ignored in all function declarations that are not definitions.
Eric and Ben already described the actual usage of register.
So you could get
quite a decent speed up by declaring them register and then
being careful not to try to preserve them across subroutine
calls.

C relieves the programmer from saving and restoring registers
across function calls.

-- Ralf
 
M

Malcolm McLean

בת×ריך ×™×•× ×©× ×™,23 ביולי 2012 18:54:46 UTC+1, מ×ת Kenneth Brody:
On 7/21/2012 5:36 PM, Malcolm McLean wrote:
On the compilers I used (long ago) where using 'register' on a
parameter had any meaning, the effect was to still pass it on the stack, but
the function would immediately store it in a register for use throughout the
function.
Quite early on the standard calling conventions were developed. Typically the first four paramters would be passed in registers, the rest on the stack.. So you tried to write your functions to take only four arguments.
I never tried to "vectorise" C code. So I might have been wrong about register cahnignthe calling signature of the function.
 
Ad

Advertisements

B

BartC

Malcolm McLean said:
בת×ריך ×™×•× ×©× ×™, 23 ביולי 2012 18:54:46 UTC+1, מ×ת Kenneth Brody:
Quite early on the standard calling conventions were developed. Typically
the first four paramters would be passed in registers, the rest on the
stack. So you tried to write your functions to take only four arguments.

I never heard of anything like that. The 32-bit C compilers I've played with
all seem to just use the stack. It's the Intel 64-bit architecture that
seems to want to impose that 4-register/rest-on-stack convention.

(And of course any language can use whatever convention it likes when
calling a function within the same language and using the same compiler.)
 
Ad

Advertisements

I

Ike Naar

I never heard of anything like that. The 32-bit C compilers I've played with
all seem to just use the stack. It's the Intel 64-bit architecture that
seems to want to impose that 4-register/rest-on-stack convention.

The SPARC has a similar calling convention (passing via registers,
that is).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top