ARM: Efficient C for ARM: Bitfields

by David Thomas on

Bitfields

  • Mini structure members.
  • Subject to pointer aliasing.
  • Layout not guaranteed.

On the whole, you’re better off using a flags word.

Example

typedef struct {
  unsigned int hasLasers   : 1;
  unsigned int hasMissiles : 1;
  unsigned int hasBomb     : 1;
  unsigned int hasECM      : 1;
} ShipData;

ShipFlags getShipFlags(const ShipData *d)
{
  return (d->hasLasers   << 0) |
         (d->hasMissiles << 1) |
         (d->hasBomb     << 2) |
         (d->hasECM      << 3);
}
getShipFlags LDR r0,[r0,#0]
             AND r0,r0,#0xf
             MOV pc,lr

Becomes:

typedef struct {
  ShipFlags flags;
} ShipData2;

ShipFlags getShipFlags2(const ShipData2 *d)
{
  return d->flags;
}
getShipFlags2 LDR r0,[r0,#0]
              MOV pc,lr

Navigate