title: ARM Cortex M7 unaligned access and memcpy
body: <p>I am compiling this code for a Cortex M7 using GCC:</p>
  
  <pre><code>// copy manually
  void write_test_plain(uint8_t * ptr, uint32_t value)
  {
      *ptr++ = (u8)(value);
      *ptr++ = (u8)(value &gt;&gt; 8);
      *ptr++ = (u8)(value &gt;&gt; 16);
      *ptr++ = (u8)(value &gt;&gt; 24); 
  }
  
  // copy using memcpy
  void write_test_memcpy(uint8_t * ptr, uint32_t value)
  {
      void *px = (void*)&amp;value;
      memcpy(ptr, px, 4);
  }
  
  int main(void) 
  {
      extern uint8_t data[];
      extern uint32_t value;
  
      // i added some offsets to data to
      // make sure the compiler cannot
      // assume it's aligned in memory
  
      write_test_plain(data + 2, value);
      __asm volatile("": : :"memory"); // just to split inlined calls
      write_test_memcpy(data + 5, value);
  
      ... do something with data ...
  }
  </code></pre>
  
  <p>And I get the following Thumb2 assembly with -O2:</p>
  
  <pre><code>// write_test_plain(data + 2, value);
  800031c:    2478        movs    r4, #120 ; 0x78
  800031e:    2056        movs    r0, #86  ; 0x56
  8000320:    2134        movs    r1, #52  ; 0x34
  8000322:    2212        movs    r2, #18  ; 0x12
  8000324:    759c        strb    r4, [r3, #22]
  8000326:    75d8        strb    r0, [r3, #23]
  8000328:    7619        strb    r1, [r3, #24]
  800032a:    765a        strb    r2, [r3, #25]
  
  // write_test_memcpy(data + 5, value);
  800032c:    4ac4        ldr r2, [pc, #784]  ; (8000640 &lt;main+0x3a0&gt;)
  800032e:    923b        str r2, [sp, #236]  ; 0xec
  8000330:    983b        ldr r0, [sp, #236]  ; 0xec
  8000332:    f8c3 0019   str.w   r0, [r3, #25]
  </code></pre>
  
  <p>Can someone explain how the <code>memcpy</code> version works? This looks like inlined 32-bit store to the destination address, but isn't this a problem since <code>data + 5</code> is most certainly not aligned to a 4-byte boundary?</p>
  
  <p>Is this perhaps some optimization which happens due to some undefined behavior in my source?</p>