tags:

views:

40

answers:

1

I read the article Optimizing Memcpy improves speed, I have a question about the modified-GNU algorithm, I get an error running the code the src & 0xFFFFFFFC , src is a void pointer, can it be a left operand for ‘&’ ? Did I miss anything here?

Thanks

Error 1 error C2296: '&' : illegal, left operand has type 'const void *’

void * memcpy(void * dst, void const * src, size_t len)
{
    long * plDst = (long *) dst;
    long const * plSrc = (long const *) src;
    if (!(src & 0xFFFFFFFC) && !(dst & 0xFFFFFFFC))
    {
        while (len >= 4)
    {
            *plDst++ = *plSrc++;
            len -= 4;
        }
    }
    char * pcDst = (char *) plDst;
    char const * pcSrc = (char const *) plSrc;
    len += 4;
    while (len--)
    {
        *pcDst++ = *pcSrc++;
    }
    return (dst);
}
+1  A: 

You can bitmask pointers - it is a legitimate operation if you know what you are doing.

First off, make sure your compiler is not in C++ mode (since it appears you are using MSVC++) - this should be a warning in C, not an error.

Second, 0xFFFFFFFC is an integer - you should make the appropriate cast for the operation in question (make it a pointer type).

Third, I would expect your compiler/libc already has a fantastic implementation of memcpy - use the built in one, not your own version. Compilers do perform some trickery with C and standard library functions, and may even inline and unroll memcpy loops. When you make your own memcpy, this trickery is usually not done.

Yann Ramin
Blurmylife
The idea is to align writes on 32-bit boundaries.
Adam Shiemke
To further these comments: writes aligned on 32-bit boundaries are often much faster than writes that are not. Writing an aligned 32-bit chunk is often much faster than writing the 4 bytes individually. This all depends on processor, here, we're likely meaning some x86 based thing, though this holds true for many (just not necessarily all) architectures.
Thanatos