title: Controlling read and write access width to memory mapped registers in C
body: <p>I'm using and x86 based core to manipulate a 32-bit memory mapped register.  My hardware behaves correctly only if the CPU generates 32-bit wide reads and writes to this register.  The register is aligned on a 32-bit address and is not addressable at byte granularity.</p>
  
  <p>What can I do to guarantee that my C (or C99) compiler will only generate full 32-bit wide reads and writes in all cases?</p>
  
  <p>For example, if I do a read-modify-write operation like this:</p>
  
  <pre><code>volatile uint32_t* p_reg = 0xCAFE0000;
  *p_reg |= 0x01;
  </code></pre>
  
  <p>I don't want the compiler to get smart about the fact that only the bottom byte changes and generate 8-bit wide read/writes.  Since the machine code is often more dense for 8-bit operations on x86, I'm afraid of unwanted optimizations.  Disabling optimizations in general is not an option.</p>
  
  <p>----- EDIT -------<br>
  An interesting and very relevant paper: <a href="http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf" rel="nofollow noreferrer">http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf</a></p>