On Thu, Jun 09, 2005 at 11:41:00PM +0900, David Cournapeau wrote:
The other problem "is [] as efficient for vector
and plain
c array ?"
possibly maybe:
#include <vector>
int access(int* v, int i)
{
return v[i];
}
int access(std::vector<int> v, int i)
{
return v[i];
}
produces (g++ -fverbose-asm -O3 -S):
_Z6accessPii:
pushl %ebp
movl %esp, %ebp
movl 12(%ebp), %ecx # i, i
movl 8(%ebp), %edx # v, v
popl %ebp
movl (%edx,%ecx,4), %eax # * v
ret
_Z6accessSt6vectorIiSaIiEEi:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edx # v, this
movl 12(%ebp), %eax # i, i
popl %ebp
movl (%edx), %ecx # * this
sall $2, %eax
addl %ecx, %eax
movl (%eax), %eax
ret
on intel and
_Z6accessPii:
slwi 0,4,2 # i
lwzx 3,3,0 # * v, v
blr
_Z6accessSt6vectorIiSaIiEEi:
lwz 0,0(3) # * this
slwi 5,4,2 # i
lwzx 3,5,0 # this
blr
on ppc. note that on ppc the generated code is equivalent,
while on intel
movl (%edx,%ecx,4), %eax
might translate to a shift, add, fetch anyway. but probably
it's one of the 172 addressing modes.
basically, accessing std::vector needs one indirection more;
gcc should be smart enough (only verified on ppc) to hold
the actual array pointer in one of the numerous registers
when chaining accesses.
<sk>