Thread: memcpy madness

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    memcpy madness

    Hello,

    I'm copying some memory from one region to another. As I'm dealing with 32-bit colour data, the area of each memory region is a multiple of 4, so I was wondering whether memcpy(...), which copies a single byte at a time, is best optimised for the task at hand.

    May I be better off using a bit of inline asm involving MOVSD (Move a doubleword string)? I gather there's been a bit of argument about this on the net, as some people say that the some CPUs doesn't always do it faster.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try it with memcpy first, then once you have that working and want it to be faster you can try to improve the speed. Simplicity and clarity come first, then performance if it's truly needed.
    My best code is written with the delete key.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

    Cool

    which copies a single byte at a time
    Why? I think memcpy, memset, etc would use native word size as much as possible and unroll the loop.

    Rough example:
    Code:
    nCount = nBytes >>  2;  // = /4 maybe.
    while (nCount--) {
         *destint = *fromint;
    }
    
    nCount = nBytes & 0x3; // = % 4 maybe.
    while (nCount--) {
         *destchar = *fromchar;
    }
    This could be optimised in C by unrolling the loop with a Duff construct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  3. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  4. Trying to copy buffers using memcpy in C under UNIX
    By Meeper in forum C Programming
    Replies: 3
    Last Post: 07-26-2003, 08:51 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM