I compiled the following C code into assembly with -03 and I am confused why we shift right to %xmm1 and add it back to %xmm0. Can someone walk me through what the assembly code does and why it makes everything a factor of 16 than 4? The code in C is
int testFunction(int* input, int length) {
int sum = 0;
for (int i = 0; i < length; ++i) {
sum += input[i];
}
return sum;
}
and the assembly code compiled with -03 is
testFunction:
movq %rdi, %rdx
testl %esi, %esi
jle .L7
leal -1(%rsi), %eax
cmpl $2, %eax
jbe .L8
movl %esi, %ecx
movq %rdi, %rax
pxor %xmm0, %xmm0
shrl $2, %ecx
subl $1, %ecx
salq $4, %rcx
leaq 16(%rdi,%rcx), %rcx
.L4:
movdqu (%rax), %xmm2
addq $16, %rax
paddd %xmm2, %xmm0
cmpq %rcx, %rax
jne .L4
movdqa %xmm0, %xmm1
movl %esi, %ecx
psrldq $8, %xmm1
andl $-4, %ecx
paddd %xmm1, %xmm0
movdqa %xmm0, %xmm1
psrldq $4, %xmm1
paddd %xmm1, %xmm0
movd %xmm0, %eax
testb $3, %sil
je .L11
.L3:
movslq %ecx, %rdi
leaq 0(,%rdi,4), %r8
addl (%rdx,%rdi,4), %eax
leal 1(%rcx), %edi
cmpl %edi, %esi
jle .L1
addl $2, %ecx
addl 4(%rdx,%r8), %eax
cmpl %ecx, %esi
jle .L1
addl 8(%rdx,%r8), %eax
ret
.L7:
xorl %eax, %eax
.L1:
ret
.L11:
ret
.L8:
xorl %ecx, %ecx
xorl %eax, %eax
jmp .L3