Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / C
Alternative
Tip/Trick

How to zero your memory?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 Aug 2011CPOL 11.1K   4
What about this, using SSE2 assembly (32 bits):void Zero(void* Buffer, int Count){ char* Cur= (char*)Buffer; char* End= (char*)Buffer + Count; // Clear the initial unaligned bytes while (Cur < End && (Cur - (char*)0) & 0xf) { *Cur++= 0; } // Clear...

What about this, using SSE2 assembly (32 bits):


C++
void Zero(void* Buffer, int Count)
{
    char* Cur= (char*)Buffer;
    char* End= (char*)Buffer + Count;
    // Clear the initial unaligned bytes
    while (Cur < End && (Cur - (char*)0) & 0xf)
    {
        *Cur++= 0;
    }
    // Clear the full, aligned blocks
    _asm
    {
        pxor xmm0, xmm0;        // 16 zeroes
        mov eax, Cur;
        mov ebx, End;
        and ebx, ~0xf;
While:
        cmp eax, ebx;        // while (Cur < End & ~0xf)
        jnb Wend;
        movapd [eax], xmm0;  // *Cur= 0;
        add eax, 16;         // Cur+= 16;
        jmp While
Wend:
        mov Cur, eax;
    }
    // Clear the final bytes
    while (Cur < End)
    {
        *Cur++= 0;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO VISION fOr VISION
Belgium Belgium
I fell into applied algorithmics at the age of 16 or so. This eventually brought me to develop machine vision software as a professional. This is Dreamland for algorithm lovers.

Comments and Discussions

 
GeneralRe: Apology, a small correction, it's a tip not article, if we t... Pin
ThatsAlok10-Aug-11 2:46
ThatsAlok10-Aug-11 2:46 
Generalrep will take 3+count of dwords CPU clocks to work, you have... Pin
Alexander Voronin9-Aug-11 4:46
Alexander Voronin9-Aug-11 4:46 
General"rep stosd" much faster. Pin
Alexander Voronin9-Aug-11 4:10
Alexander Voronin9-Aug-11 4:10 
GeneralRe: Have you got figures ? This is a strange article. Getting g... Pin
YvesDaoust9-Aug-11 4:18
YvesDaoust9-Aug-11 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.