Click to See Complete Forum and Search --> : whats faster memcpy or ...


answer
May 12th, 2004, 05:36 PM
Whats faster using memcpy or doing


for(X =0; X<w; X++)
{
*(WORD*)dbits = *(WORD*)sbits;
dbits += 2;
sbits += 2;
}

Thanx in advance :)

TheCPUWizard
May 12th, 2004, 05:41 PM
memcpy on 99% of the implementations. Ususally (Intel Platforms) it will load the two addresses and the count into registers and to the operation in a single hardware instruction.

Yves M
May 12th, 2004, 06:49 PM
Memcpy is most of the time compiled into code that's as fast as the computer can do it. On an IA32 machine, there is actually one machine instruction that can copy the whole block directly.

Here is an example:

// C++ code
#include <memory.h>

const int MAXSIZE = 1000;

int main()
{
int *a, *b, i;
a = new int[MAXSIZE];
b = new int[MAXSIZE];
memcpy(b, a, sizeof(int) * MAXSIZE);
return 0;
}

// Assembly code
// Supposing the address of a is in eax and the address of b in edx
mov ecx, 1000
mov esi, eax
mov edi, edx
rep movsd

answer
May 12th, 2004, 07:41 PM
Thanx TheCPUWizard for the info :)


Yves M, copying directly sounds very interesting :). I need some help with assembly.


I want to do this:
memcpy(dbits, sbits, w <<1);

_asm{
mov eax, sbits
mov edx, dbits
mov ecx, w
shl w, 1
mov esi, eax
mov edi, edx
rep movsd

}


The assembly code does not do what the memcpy does for me. Iam working with images and the image becomes out of order :confused:.

answer
May 12th, 2004, 08:33 PM
Thanx very much. I found out the problem :). It is at "rep movsd" Iam working with WORDS therefore it should be "rep movsw".

_asm{
mov ecx, w
mov esi, sbits
mov edi, dbits
rep movsw
}

Yves M
May 13th, 2004, 03:47 AM
Originally posted by answer
Yves M, copying directly sounds very interesting :). I need some help with assembly.

Why do you want to implement the copying directly in assembler? The assembler code I showed in my previous post is what the C++ compiler translates memcpy into. I'm sorry if this wasn't clear.

In any case, rep movsw is (theoretically) twice as slow as rep movsd.