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!
|
|
0
|
|
|
|
Reply
|
nospam21 (11322)
|
7/21/2012 8:42:14 PM |
|
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
esosman@ieee-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman2 (2945)
|
7/21/2012 9:35:22 PM
|
|
=D7=91=D7=AA=D7=90=D7=A8=D7=99=D7=9A =D7=99=D7=95=D7=9D =D7=A9=D7=91=D7=AA,=
21 =D7=91=D7=99=D7=95=D7=9C=D7=99 2012 21:42:14 UTC+1, =D7=9E=D7=90=D7=AA =
sensei:
> I have recently encountered an old code in K & R style which declared
> function parameters as register. That puzzles me,=20
>
Very simplistic compilers used to always pass parameters on the stack, unle=
ss explicitly declared register. So you could get quite a decent speed up b=
y declaring them register and then being careful not to try to preserve the=
m across subroutine calls.
It's an obsolete technique now, as far as I know.
|
|
0
|
|
|
|
Reply
|
malcolm.mclean5 (728)
|
7/21/2012 9:36:12 PM
|
|
sensei <nospam@nospam.com> 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.
|
|
0
|
|
|
|
Reply
|
ben.usenet (6515)
|
7/21/2012 10:23:57 PM
|
|
Malcolm McLean <malcolm.mclean5@btinternet.com> 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
|
|
0
|
|
|
|
Reply
|
rwspam (81)
|
7/21/2012 11:54:52 PM
|
|
On 7/21/2012 5:36 PM, Malcolm McLean wrote:
> בתאריך יום שבת, 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.
Nit: That depends entirely on the platform.
> 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.
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.
--
Kenneth Brody
|
|
0
|
|
|
|
Reply
|
kenbrody (1860)
|
7/23/2012 5:54:46 PM
|
|
=D7=91=D7=AA=D7=90=D7=A8=D7=99=D7=9A =D7=99=D7=95=D7=9D =D7=A9=D7=A0=D7=99,=
23 =D7=91=D7=99=D7=95=D7=9C=D7=99 2012 18:54:46 UTC+1, =D7=9E=D7=90=D7=AA =
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=20
> the function would immediately store it in a register for use throughout =
the=20
> function.
>=20
Quite early on the standard calling conventions were developed. Typically t=
he 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 regis=
ter cahnignthe calling signature of the function.
|
|
0
|
|
|
|
Reply
|
malcolm.mclean5 (728)
|
7/23/2012 7:32:50 PM
|
|
"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message
news:f9209791-4076-444b-aff8-1365b731bc50@googlegroups.com...
> בתאריך יום שני, 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
|
|
0
|
|
|
|
Reply
|
bc (2211)
|
7/23/2012 9:03:16 PM
|
|
On 2012-07-23, BartC <bc@freeuk.com> wrote:
> "Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message
> news:f9209791-4076-444b-aff8-1365b731bc50@googlegroups.com...
>> ?????? ??? ???, 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).
|
|
0
|
|
|
|
Reply
|
ike7 (162)
|
7/23/2012 9:39:19 PM
|
|
On Mon, 23 Jul 2012 22:03:16 +0100, "BartC" <bc@freeuk.com> wrote:
>"Malcolm McLean" <malcolm.mclean5@btinternet.com> wrote in message
>news:f9209791-4076-444b-aff8-1365b731bc50@googlegroups.com...
>> ?????? ??? ???, 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.)
MS or GCC __fastcall passes the first two DWORD or smaller operands in
registers, Borland fastcall did three, Watcom "register" did four
operands in registers. IIRC, Watcom only used registers for the
contiguous set of parameters that fit starting with the first. There
are other examples.
|
|
0
|
|
|
|
Reply
|
robertwessel2 (1339)
|
7/24/2012 3:49:41 AM
|
|
|
9 Replies
48 Views
(page loaded in 0.168 seconds)
|