3 msgInline assembly and local variable storage
2 msgHelp with -march and -mcpu issues
3 msgGCC - Preserving statement expression results o...
10 msgSolution sought to GCC 4.1.1 backtrace problem

Can something similar to &x=&y be accom...
\ Jim Stapleton (29 Aug 2007)
. \ Diego Novillo (29 Aug 2007)
. \ Tom St Denis (29 Aug 2007)
. . \ Jim Stapleton (29 Aug 2007)
. . . \ Tom St Denis (30 Aug 2007)

1 msgGNAT/GCC 4.2 build problem on FreeBSD-CURRENT
3 msgLocal disable of specific warnings for a sectio...
3 msgcan gcc compile from memory buffer?
2 msgMac OS X cross compile for i386-elf
2 msgUnsubscription
13 msgSUSv3's 'memory location' and threads
2 msghow to code global indirect branches ?
2 msgHelp about calculation of 'S' notation for a re...
1 msgmovaps missaligment in threads
3 msgcall stack's order of parameters
2 msg-fno-stack-protector not ensuring binary compat...
8 msgGCC asm block optimizations on x86_64
2 msg.p2align
3 msgcompile errors from gcc
5 msgBuilding gmp and mpfr within gcc
Subject:Re: Can something similar to &x=&y be accomplished in C
Group:Gcc-help
From:Tom St Denis
Date:30 Aug 2007


Jim Stapleton wrote:
> Thanks, I was more concerned with performance (an extra op or two each
> access), than with ease of use.
>

As the others pointed out &x references are actually implemented via
pointer deferences. C++ isn't some magical language, it's bound by the
same rules of the architecture as C. So to read/write that reference
you have to work through a pointer.

Note that GCC may alias *X to a register during it's computation. For
example,

int func(int *x, int *y)
{
*x = (*x * *y) + *y - (*y >> 2) + (*y << 4);
return *x;
}

produces this X86 code with GCC 4.1.2 (-O3 -fomit-frame-pointer)

func:
.LFB2:
movl (%rsi), %edx
movl %edx, %eax
movl %edx, %ecx
imull (%rdi), %eax
sarl $2, %ecx
leal (%rdx,%rax), %eax
sall $4, %edx
subl %ecx, %eax
addl %edx, %eax
movl %eax, (%rdi)
ret


As you can see x and y are pointed to by rsi and rdi. rsi get loaded
into edx, and reused. And now with using C++ ref's you get

_Z4funcRiS_:
.LFB2:
movl (%rsi), %edx
movl %edx, %eax
movl %edx, %ecx
imull (%rdi), %eax
sarl $2, %ecx
leal (%rdx,%rax), %eax
sall $4, %edx
subl %ecx, %eax
addl %edx, %eax
movl %eax, (%rdi)
ret

Notice some similarities?

Tom


© 2004-2008 readlist.com