Advertisement
pushrbx

Optimized memzero

Jan 13th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. /**
  2.  * Optimized memset type operation for zeroing memory locations
  3.  */
  4. inline void appZeroMemory(sdVoid *destPtr, int count)
  5. {
  6.     __asm
  7.     {
  8.         mov ecx, [count] // store the repetition count for rep stosd
  9.         mov edi, [destPtr]
  10.         xor eax, eax // fill eax with zero, the contents of eax will go where the edi points to
  11.         // we need two counters:
  12.         mov edx, ecx
  13.         shr ecx, 2 // divide [count] by 4
  14.         and edx, 3 // store the rest of the counts to edx
  15.         // zero on every 4 bytes, that's why we need two counters
  16.         rep stosd // store dword from eax where the edi points to
  17.         // if the [count] variable can't be divided by 4, then zero the rest of the bytes:
  18.         mov ecx, edx
  19.         rep stosb // store the lower 8bit of eax to es:di, ecx times
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement