[std-interval] passing by value vs reference

Fernando Cacciola fernando_cacciola at datafull.com
Wed Apr 5 14:55:21 PDT 2006


Guillaume Melquiond wrote:

>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)
>
>  
>
If  the FPU registers are stacked, like in the x86 family, it wouldn't 
be too uncommon, IMHO, for a compiler to do:

( a * b )

values stacked in st0,st1,st2,st3 in the FPU
result stored in st0,st1, and the FPU stack pointer moved to st2

( c * d )

values stacked in st2,st3,st4,st5 in the FPU
result stored in st2,st3, and the FPU stack pointer moved to st4

[( a * b )] * [( c * d )]

values for the subexpressions ALREADY stored in st0,st1,st2 and st3
final result stored in st0,st1


That is, stacked FPU registers allows the compiler to allocate the 
function frames (which is the problem in your example) without using the 
external memory-based stack or rearranging the registers manually.

Anyway, I was just pointing out the fact that pass-by-value allows reuse 
of FP registers (in non-inlined cases, of course, which is the point).
That's a fundamental source of FP optimizations even if it is only 
applicable to a fraction of the entire argument set.
Just consider that, for example, in the x86 architecture, storing a 
value back into memory may involve rounding besides the actual copy. 
Keeping values in the registers is more significant with floating-points 
than it is with integrals.

Best

Fernando Cacciola
SciSoft
http://fcacciola.50webs.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://compgeom.poly.edu/pipermail/std-interval/attachments/20060405/a1a398cf/attachment.htm


More information about the Std-interval mailing list