Re: Being more careful about iospace accesses..

From: David Woodhouse
Date: Thu Sep 16 2004 - 07:32:20 EST


On Wed, 2004-09-15 at 16:26 -0700, Linus Torvalds wrote:
> - if you want to go outside that bitwise type, you have to convert it
> properly first. For example, if you want to add a constant to a __le16
> type, you can do so, but you have to use the proper sequence:
>
> __le16 sum, a, b;
>
> sum = a + b; /* INVALID! "warning: incompatible types for operation (+)" */
> sum = cpu_to_le16(le16_to_cpu(a) + le16_to_cpu(b)); /* Ok */
>
> See?

Yeah right, that latter case is _so_ much more readable, and makes it
_so_ easy for the compiler to optimise precisely when it wants to do the
byte-swapping, especially if the back end has load-and-swap or
store-and-swap instructions. :)

It's even nicer when it ends up as:

sum = cpu_to_le16(le16_to_cpu(a) + le16_to_cpu(b)); /* Ok */
sum |= c;
sum = cpu_to_le16(le16_to_cpu(sum) + le16_to_cpu(d));

I'd really quite like to see the real compiler know about endianness,
too. I dare say I _could_ optimise the above (admittedly contrived but
not _so_ unlikely) case, but I don't _want_ to hand-optimise my code --
that's what I keep a compiler _for_.

--
dwmw2


-
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/