title: ARM unaligned memory access workaround
body: <p>I have to port source code from to an ARM platform that runs Linux. Unfortunately I have run into unaligned memory access problems. The source uses pointer casts and access heavily.</p>
  
  <p>Code like the one below has spread over the codebase like a virus. I can pinpoint the problematic locations thanks to the gcc <code>-Wcast-align</code> command line option but there are over a thousand instances to go through.</p>
  
  <pre><code>u = (IEC_BOOL);
  (((*(IEC_LINT*)pSP).H &lt; b.H) 
     || (((*(IEC_LINT*)pSP).H == b.H) &amp;&amp; ((*(IEC_LINT*)pSP).L &lt; b.L) )) ? 1 : 0);
  *(IEC_DWORD OS_SPTR *)pSP = 
      (IEC_DWORD)(*(IEC_DWORD OS_SPTR *)pSP &gt;&gt; u);  
  *(IEC_DWORD OS_SPTR *)pSP = 
      (IEC_DWORD)(*(IEC_DWORD OS_SPTR *)pSP &lt;&lt; -u);  
  u = (IEC_BYTE)((*(IEC_DINT*)pSP != b) ? 1 : 0);  
  *(IEC_DWORD*)pSP = (IEC_DWORD)(*(IEC_DWORD*)pSP &amp; w);  
  (*(IEC_ULINT*)pSP).H += u.H;   
  (((*(IEC_ULINT OS_SPTR *)pSP).H == b.H) 
      &amp;&amp; ((*(IEC_ULINT OS_SPTR *)pSP).L &gt; b.L))) ? 1 : 0);
  u = (IEC_BYTE)((*(IEC_REAL*)pSP &gt;= b) ? 1 : 0);
  </code></pre>
  
  <p>Using <code>echo 2 &gt; /proc/cpu/alignment</code> on makes the Linux Kernel fix the problems but the performance of the application is degraded to a point that is not acceptable any more.</p>
  
  <p>I searched the net for something like a <code>__unaligned</code> or <code>__packed</code> keyword for the GCC (v4.4.1) compiler but as of yet came up empty.</p>
  
  <p>I thought a lot of the poblematic code lines could be fixed with a more or less complex regexp/replace but now, after doing that for a while I see, that also this approach will take enourmous amounts of tedious work.</p>
  
  <p>Do you guys have any suggestion how to get this job done? I think a gcc 4.5 compiler plugin would be overkill but is there something better than regular expressions? what other suggestions can you come up with? Not necessarily all problem instances have to be fixed since I can still rely on the Kernel for a few rarer cases.</p>