Register parameters

Discussion in 'C Programming' started by sensei, Jul 21, 2012.

  1. sensei

    sensei Guest

    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!
    sensei, Jul 21, 2012
    #1
    1. Advertising

  2. sensei

    Eric Sosman Guest

    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
    d
    Eric Sosman, Jul 21, 2012
    #2
    1. Advertising

  3. בת×ריך ×™×•× ×©×‘×ª,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.
    Malcolm McLean, Jul 21, 2012
    #3
  4. sensei <> 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.
    Ben Bacarisse, Jul 21, 2012
    #4
  5. Malcolm McLean <> 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
    Ralf Damaschke, Jul 22, 2012
    #5
  6. בת×ריך ×™×•× ×©× ×™,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.
    Malcolm McLean, Jul 23, 2012
    #6
  7. sensei

    BartC Guest

    "Malcolm McLean" <> wrote in message
    news:...
    > בת×ריך ×™×•× ×©× ×™, 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
    BartC, Jul 23, 2012
    #7
  8. sensei

    Ike Naar Guest

    On 2012-07-23, BartC <> wrote:
    > "Malcolm McLean" <> wrote in message
    > news:...
    >> ?????? ??? ???, 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).
    Ike Naar, Jul 23, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    976
    Jonck van der Kogel
    May 27, 2004
  2. Jason
    Replies:
    2
    Views:
    506
    Jonathan Mcdougall
    May 13, 2006
  3. santosh

    Re: Register parameters

    santosh, Jul 17, 2008, in forum: C Programming
    Replies:
    4
    Views:
    282
    Keith Thompson
    Jul 17, 2008
  4. kito
    Replies:
    2
    Views:
    413
  5. Florian Loitsch
    Replies:
    11
    Views:
    245
    Michael Winter
    Mar 15, 2005
Loading...

Share This Page