title: Fast ARM NEON memcpy
body: <p>I want to copy an image on an ARMv7 core. The naive implementation is to call memcpy per line.</p>
  
  <pre><code>for(i = 0; i &lt; h; i++) {
    memcpy(d, s, w);
    s += sp;
    d += dp;
  }
  </code></pre>
  
  <p>I know that the following</p>
  
  <pre><code>d, dp, s, sp, w
  </code></pre>
  
  <p>are all 32-byte aligned, so my next (still quite naive) implementation was along the lines of</p>
  
  <pre><code>for (int i = 0; i &lt; h; i++) {
    uint8_t* dst = d;
    const uint8_t* src = s;
    int remaining = w;
    asm volatile (
      "1:                                               \n"
      "subs     %[rem], %[rem], #32                     \n"
      "vld1.u8  {d0, d1, d2, d3}, [%[src],:256]!        \n"
      "vst1.u8  {d0, d1, d2, d3}, [%[dst],:256]!        \n"
      "bgt      1b                                      \n"
      : [dst]"+r"(dst), [src]"+r"(src), [rem]"+r"(remaining)
      :
      : "d0", "d1", "d2", "d3", "cc", "memory"
    );
    d += dp;
    s += sp;
  }
  </code></pre>
  
  <p>Which was ~150% faster than memcpy over a large number of iterations (on different images, so not taking advantage of caching). I feel like this should be nowhere near the optimum because I am yet to use preloading, but when I do I only seem to be able to make performance substantially worse. Does anyone have any insight here?</p>