title: C bitfield element with non-contiguous layout
body: <p>I'm looking for input on the most elegant interface to put around a memory-mapped register interface where the target object is split in the register:</p>
  
  <pre><code>union __attribute__ ((__packed__)) epsr_t {
      uint32_t storage;
      struct {
          unsigned reserved0    : 10;
          unsigned ICI_IT_2to7  :  6; // TOP HALF
          unsigned reserved1    :  8;
          unsigned T            :  1;
          unsigned ICI_IT_0to1  :  2; // BOTTOM HALF
          unsigned reserved2    :  5;
      } bits;
  };
  </code></pre>
  
  <p>In this case, accessing the single bit <code>T</code> or any of the <code>reserved</code> fields work fine, but to read or write the <code>ICI_IT</code> requires code more like:</p>
  
  <pre><code>union epsr_t epsr;
  // Reading:
  uint8_t ici_it = (epsr.bits.ICI_IT_2to7 &lt;&lt; 2) | epsr.bits.ICI_IT_0to1;
  // Writing:
  epsr.bits.ICI_IT_2to7 = ici_it &gt;&gt; 2;
  epsr.bits.ICI_IT_0to1 = ici_it &amp; 0x3;
  </code></pre>
  
  <p>At this point I've lost a chunk of the simplicity / convenience that the bitfield abstraction is trying to provide. I considered the macro solution:</p>
  
  <pre><code>#define GET_ICI_IT(_e)      ((_e.bits.ICI_IT_2to7 &lt;&lt; 2) | _e.bits.ICI_IT_0to1)
  #define SET_ICI_IT(_e, _i)  do {\
      _e.bits.ICI_IT_2to7 = _i &gt;&gt; 2;\
      _e.bits.ICI_IT_0to1 = _i &amp; 0x3;\
      while (0);
  </code></pre>
  
  <p>But I'm not a huge fan of macros like this as a general rule, I hate chasing them down when I'm reading someone else's code, and far be it from me to inflict such misery on others.  I was hoping there was a creative trick involving structs / unions / what-have-you to hide the split nature of this object more elegantly (ideally as a simple member of an object).</p>