-1

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

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
Kevin Meier
  • 2,339
  • 3
  • 25
  • 52
  • [memcpy vs assignment](https://stackoverflow.com/questions/324011/memcpy-vs-assignment-in-c) – Mehdi Charife Sep 01 '23 at 19:00
  • Why don't you just measure it? What is the problem? – Support Ukraine Sep 01 '23 at 19:03
  • Interestingly, gcc with `-Os` generates the same code for both functions (the same as `f1` above). So it is capable of generating code for the `memcpy()` the same as an assignment. Does it really think `f2` (above) is faster? – pmacfarlane Sep 01 '23 at 19:24
  • alighnment. memcpy does not know anything about it. assignment - compiler knows that structs will be correctly aligned. check what happens if you pack the struct: https://godbolt.org/z/b84v5ffbK – 0___________ Sep 02 '23 at 00:07

0 Answers0