Re: [patch] Add helper macros for little-endian bitfields

From: Roland Dreier
Date: Sun Aug 24 2008 - 21:37:55 EST


> + * NOTE: When using multibyte bitfields, you need to convert the data
> + * from Little Endian to CPU before you can access the bitfield
> + * (to make it simpler):
> + *
> + * union something {
> + * le16 value;
> + * DECL_BF_LE3(
> + * u16 bf0:3,
> + * u16 bf1:10,
> + * u16 bf2:3
> + * ) __attribute__((packed));
> + * };
> + *
> + * ...
> + *
> + * union something s;
> + *
> + * s.value = le16_to_cpu(something LE read from hw);
> + * frame_count = s.bf1;

I can't help thinking there's got to be a better way to do this... in
particular having 16 DECL_BF_LEnn() macros (and needing to count the
number of members every time you use one) is not particularly pretty.

In general the consensus with kernel code seems to be that you're better
off avoiding bitfields in structures, and just using shift/mask
operations to get at the data (with helper functions as needed).

However all of that is said without actually looking at the driver code
that uses this.

- R.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/