Assume I want to copy a large struct. There are two trivial way to do it:
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
struct x {
int a[123];
char b[321];
short c[1342];
long d[123];
};
void f1(struct x *__restrict a, struct x *__restrict b)
{
*a = *b;
}
void f2(struct x *__restrict a, struct x *__restrict b)
{
memcpy(a, b, sizeof(*a));
}
int main()
{
}
gcc generates this assembly (with full optimizations):
f1:
mov ecx, 561
rep movsq
ret
f2:
mov rax, QWORD PTR [rsi]
mov rcx, rdi
lea rdi, [rdi+8]
mov QWORD PTR [rdi-8], rax
mov rax, QWORD PTR [rsi+4480]
mov QWORD PTR [rdi+4472], rax
and rdi, -8
sub rcx, rdi
sub rsi, rcx
add ecx, 4488
shr ecx, 3
rep movsq
ret
main:
xor eax, eax
ret
https://godbolt.org/z/vvKjThnKs
Why are the two functions so different and which assembly code is in general "better"?
Thanks a lot