On 10/11/10 18:05, Paul Koning wrote:
C++ lets you define explicit-order integer types and hide all the
conversions.  I used that a couple of jobs ago, back around 1996 or
so -- worked nicely, and should work even better now that C++ is more
mature.


Yes, a lot of such things can be done with C++ classes. But I see several advantages in implementing it in the compiler rather than classes.

First, it will be available in C as well as C++. For lots of reasons, many programs are written in C and not C++.

Secondly, the best way to implement endian re-ordering varies from target to target. Some targets have assembly instructions that can be used, requiring inline assembly. Some work best using shifts and masks, others using union types. You can't make a C++ class that will give the best code on many targets without making it very messy, so typically it will be re-implemented for each target.

It's easy to make mistakes with C++ class using casting operators to do the endian swapping. Miss out some details, and you might find the compiler omitting the endian swap somewhere.

Finally, if you want a struct with endian-swapped members, then each member must be explicitly declared as the appropriate class. You can't swap the entire struct at once, as you could do with a memory space (or attribute) solution.

mvh.,

David


paul

On Nov 10, 2010, at 7:00 AM, David Brown wrote:

Would it be possible to use the named address space syntax to
implement reverse-endian data?  Conversion between little-endian
and big-endian data structures is something that turns up regularly
in embedded systems, where you might well be using two different
architectures with different endianness.  Some compilers offer
direct support for endian swapping, but gcc has no neat solution.
You can use the __builtin_bswap32 (but no __builtin_bswap16?)
function in recent versions of gcc, but you still need to handle
the swapping explicitly.

Named address spaces would give a very neat syntax for using such
byte-swapped areas.  Ideally you'd be able to write something
like:

__swapendian stuct { int a; short b; } data;

and every access to data.a and data.b would be endian-swapped.  You
could also have __bigendian and __litteendian defined to
__swapendian or blank depending on the native ordering of the
target.


I've started reading a little about how named address spaces work,
but I don't know enough to see whether this is feasible or not.


Another addition in a similar vein would be __nonaligned, for
targets which cannot directly access non-aligned data.  The loads
and stores would be done byte-wise for slower but correct
functionality.





Reply via email to