title: Simple Memory Copy (rep movs) possible error?
body: <p>I'm trying to implement simple C/C++ language compiler dependent memcpy function.</p>
  <p><strong>What are the possible erroneous results of this simple memory copy mechanism?</strong></p>
  <p>(Excluding null check and the case of copying from/to same address)</p>
  <p>The mechanism is as follows.</p>
  <p>(1) Define structs vary by size.</p>
  <pre><code>struct S100000000Byte
  {
      unsigned char pData[100000000];
  };
  </code></pre>
  <p>(2) Cast void* argument to struct pointer, dereference the casted argument pointer, and copy.</p>
  <pre><code>__forceinline void Copy(void* _pBuffer1, void* _pBuffer0, const unsigned long long _ullSize)
  {
      ...
      (*static_cast&lt;S100000000Byte*&gt;(_pBuffer1)) = (*static_cast&lt;S100000000Byte*&gt;(_pBuffer0));
  }
  </code></pre>
  <p>Test was taken by copying buffer size of 100000000 bytes 1000 times each.</p>
  <p>This mechanism resulted to be faster than memcpy in the following condition.</p>
  <p>CPU : i7-10875H</p>
  <p>IDE : Visual Studio 2019</p>
  <p>Platform Toolset : Visual Studio 2019 (v142)</p>
  <p>Optimization : /O2</p>
  <p>Time performance difference was</p>
  <p>memcpy : 6609 ms
  Implemented Copy : 6547 ms</p>
  <p>memcpy : 7062 ms
  Implemented Copy : 6766 ms</p>
  <p>so far.</p>
  <p>Disassembly of mechanism (2) was</p>
  <pre><code>rep movs byte ptr [rdi],byte ptr [rsi]
  </code></pre>
  <p>References were</p>
  <p><a href="https://www.geeksforgeeks.org/write-memcpy/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/write-memcpy/</a></p>
  <p><a href="https://stackoverflow.com/questions/11876361/implementing-own-memcpy-size-in-bytes">Implementing own memcpy (size in bytes?)</a></p>
  <p>and articles referring about rep movsb.</p>