[std-interval] passing by value vs reference

Guillaume Melquiond guillaume.melquiond at ens-lyon.fr
Wed Apr 5 18:26:33 PDT 2006


Le mercredi 05 avril 2006 à 11:39 -0300, Fernando Cacciola a écrit :

> Regarding the pass by-value vs by-const-reference discussion, it is 
> important to consider that the fields of this POD are floating point types.
> A compiler will use a register to pass the address of the interval in 
> the pass-by-const-reference case, but that's the wrong register...
> When the interval fields are actually used inside the called function, 
> they will have to be loaded in the FPU registers.
> 
> The most efficient scenario occurs when the compiler elides the 
> copy-constructor and the calling function had already the fields of the 
> interval arguments (typically 2) already loaded in FPU registers. In 
> that case, there is no argument passing at all.

This is not completely true: except for trivial cases, there is argument
passing even when registers are used.

Consider the following example with a, b, c, and d four intervals. I
will assume that the multiplications are not inlined (otherwise there
would be no difference between pass-by-reference and pass-by-value with
any sane compiler). Let us call r0:r1 the registers used to return an
interval, and a0:a1 and a2:a3 the registers used to pass first and
second interval arguments.

e = (a * b) * (c * d)

First (the expression is symmetric, so order of evaluation does not
matter), a * b is computed and the result is stored in r0:r1. It cannot
stay there, since r0:r1 will also be used to get the result of c * d. It
cannot be directly moved to a0:a1, since a0:a1 will be used to store c.
So it is moved to another register pair d0:d1. Then, the product c * d
can be computed and its value is moved from r0:r1 to a2:a3. Finally the
content of d0:d1 is moved to a0:a1 and the last multiplication can be
launched.

So there is argument passing and its cost is far from negligible: the
content of six registers has to be moved around just to get the
arguments of the last multiplication in the right places. Moreover,
registers like d0:d1 are probably either caller-save or callee-save. So
somebody somewhere may have to save and restore their values on the
stack. And so on.

But I agree that this whole register mess is still cheaper than any
stack usage caused by passing-by-reference on processors that are not
register-starved.

Best regards,

Guillaume



More information about the Std-interval mailing list