22

There is a version of C99/posix memcpy function in GCC: __builtin_memcpy.

Sometimes it can be replaced by GCC to inline version of memcpy and in other cases it is replaced by call to libc's memcpy. E.g. it was noted here:

Finally, on a compiler note, __builtin_memcpy can fall back to emitting a memcpy function call.

What is the logic in this selection? Is it logic the same in other gcc-compatible compilers, like clang/llvm, intel c++ compiler, PCC, suncc (oracle studio)?

When I should prefer of using __builtin_memcpy over plain memcpy?

osgx
  • 90,338
  • 53
  • 357
  • 513
  • 2
    It appears that it uses an inline version when the size is a compile time constant and it's less than or equal to 8192. – Z boson Aug 14 '14 at 17:06

1 Answers1

20

I had been experimenting with the builtin replacement some time ago and I found out that the <string.h> functions are only replaced when the size of the source argument can be known at compile time. In which case the call to libc is replaced directly by unrolled code.

Unless you compile with -fno-builtin, -ansi, -std=c89 or something similar, it actually doesn't matter wether you use the __builtin_ prefix or not.

Although it's hard to follow, the code that deciedes whether to emit a library call or a chunk of code seems to be here.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
C2H5OH
  • 5,452
  • 2
  • 27
  • 39