kreuzerkrieg
kreuzerkrieg

Reputation: 3229

Odd memcpy_s behaviour in VS2015

recently I was profiling one application, and I have noticed that memcpy_s assembly implementation behaves strangely. I'm talking about implementation residing in Microsoft Visual Studio 14.0\VC\crt\src\i386\memcpy.asm I'm reaching the CopyUpLargeMov: then I expect it to choose the SSE2 path, or use any other available optimized implementation. the code as following:

    CopyUpLargeMov:
        bt      __favor, __FAVOR_ENFSTRG        ; check if Enhanced Fast Strings is supported
        jnc     CopyUpSSE2Check                 ; if not, check for SSE2 support
        rep     movsb
        mov     eax,[esp + 0Ch]                 ; return original destination pointer
        pop     esi
        pop     edi
        M_EXIT

Whatever I do with optimization tweaking it never reaches CopyUpSSE2Check.
Tested with Release|Win32, VS2015 Upd3, Windows10 x64.

The actual C++ code

std::vector<uint8_t> src(1024*1024*20,0);
std::vector<uint8_t> dst(1024*1024*20,0);
for (auto i = 0ul; i < 1000; ++i)
{
    memcpy_s(dst.data(), dst.size(), src.data(), src.size());
}

Any ideas?

EDIT001:
It seems that x64 does not exhibits the strange behavior, it falls into Enhanced Fast Strings optimization part of the code. Maybe the above a x86 limitation?

Upvotes: 4

Views: 958

Answers (1)

kreuzerkrieg
kreuzerkrieg

Reputation: 3229

As @EOF pointed out in his comment, the rep movsb is the optimization. It moves the data from string to string, so called "enhanced fast strings" optimization. So I just overlooked it, the memcpy is working as it expected to work.

Upvotes: 1

Related Questions