Make compiler copy characters using movsd

Posted by Suma on Stack Overflow See other posts from Stack Overflow or by Suma
Published on 2009-07-16T12:48:58Z Indexed on 2010/04/12 11:03 UTC
Read the original article Hit count: 367

I would like to copy a relatively short sequence of memory (less than 1 KB, typically 2-200 bytes) in a time critical function. The best code for this on CPU side seems to be rep movsd. However I somehow cannot make my compiler to generate this code. I hoped (and I vaguely remember seeing so) using memcpy would do this using compiler built-in instrinsic, but based on disassembly and debugging it seems compiler is using call to memcpy/memmove library implementation instead. I also hoped the compiler might be smart enough to recognize following loop and use rep movsd on its own, but it seems it does not.

char *dst;
const char *src;
// ...
for (int r=size; --r>=0; ) *dst++ = *src++;

Is there some way to make the Visual Studio compiler to generate rep movsd sequence other than using inline assembly?

© Stack Overflow or respective owner

Related posts about c++

Related posts about memcpy