Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Register parameters

Reply
Thread Tools

Register parameters

 
 
sensei
Guest
Posts: n/a
 
      07-21-2012
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!
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-21-2012
On 7/21/2012 4:42 PM, sensei wrote:
> 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: ___."

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      07-21-2012
בתאריך יום שבת,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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-21-2012
sensei <(E-Mail Removed)> writes:

> 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>
--
Ben.
 
Reply With Quote
 
Ralf Damaschke
Guest
Posts: n/a
 
      07-21-2012
Malcolm McLean <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      07-23-2012
בתאריך יום ש*י,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.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      07-23-2012
"Malcolm McLean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> בתאריך יום ש*י, 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 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.)

--
Bartc

 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      07-23-2012
On 2012-07-23, BartC <(E-Mail Removed)> wrote:
> "Malcolm McLean" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> ?????? ??? ???, 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 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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Re: Register parameters santosh C Programming 4 07-17-2008 10:25 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments