Pointer Aliasing

In [20]:
!rm -Rf tmp
!mkdir -p tmp
In [21]:
%%writefile tmp/alias.c
void copy_twice(float * a, float * b, int n)
{
  for (int i = 0; i < n; ++i)
  {
    b[2*i] = 2*a[i];
    b[2*i+1] = 2*a[i];
  }

}
Writing tmp/alias.c
In [22]:
!cd tmp; gcc -c -O alias.c
!objdump --disassemble tmp/alias.o
tmp/alias.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <copy_twice>:
   0:	85 d2                	test   %edx,%edx
   2:	7e 33                	jle    37 <copy_twice+0x37>
   4:	8d 4a ff             	lea    -0x1(%rdx),%ecx
   7:	b8 00 00 00 00       	mov    $0x0,%eax
   c:	eb 03                	jmp    11 <copy_twice+0x11>
   e:	48 89 d0             	mov    %rdx,%rax
  11:	f3 0f 10 04 87       	movss  (%rdi,%rax,4),%xmm0
  16:	f3 0f 58 c0          	addss  %xmm0,%xmm0
  1a:	f3 0f 11 04 c6       	movss  %xmm0,(%rsi,%rax,8)
  1f:	f3 0f 10 04 87       	movss  (%rdi,%rax,4),%xmm0
  24:	f3 0f 58 c0          	addss  %xmm0,%xmm0
  28:	f3 0f 11 44 c6 04    	movss  %xmm0,0x4(%rsi,%rax,8)
  2e:	48 8d 50 01          	lea    0x1(%rax),%rdx
  32:	48 39 c8             	cmp    %rcx,%rax
  35:	75 d7                	jne    e <copy_twice+0xe>
  37:	c3                   	retq   
  • How can we prevent the value from a from being reloaded?
  • What happens without the 2*?