Boost logo

Boost :

Subject: Re: [boost] [boost::endian] Request for comments/interest
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-05-26 13:07:30


Tomas Puverle wrote:
>
> I wrote an endian library on the flight back from boostcon
> but it has taken a little time to clear the submission with
> the firm.

I recall our discussion of the inherent inefficiencies of Beman's approach -- that all operations on a boost::endian type potentially require swapping -- whereas the typical use case is to use native endianness until ready to read/write using wire format. It is only when ready to do I/O that there is a need to account for endianness.

> I realize that Beman's endian swapper library is already in
> the review queue but I hope my approach is different and
> interesting enough to warrant consideration.

Alternatives are good.

> The highlights of the library are the following:
> - Very simple interface: swap<machine_to_little/big>(),
> swap_in_place<machine_to_little/big>()

In my version of such functionality, I have byte_order::to_host() and byte_order::to_network() function templates. Those names are more in keeping with the C functions htons, ntohs, etc. I have versions that return a swapped copy of the input and those that take a non-const reference to a destination type, which means both types can be deduced and checked. Mine doesn't require the input and output type to be the same, but they must meet certain criteria checked via Boost.Concept Checking: they must be the same size, have the same signedness, and be of a supported size (currently up to 64 bits).

namespace byte_order
{
   template <class T, class U>
   void
   to_host(T &, U);

   template <class T, class U>
   T
   to_host(U);
}

> - Support for built-in and user-defined data types and
> ranges thereof. E.g. you can swap ranges of structs
> containing/deriving from other structs

I haven't looked at the implementation you've selected, but there are several ways to do the swapping: casting among pointer types, straight casting, union-based punning, and memcpy(). The optimal choice for a particular data size and platform varies. The pointer-casting approach can lead to type punning problems for GCC without -fno-strict-aliasing. Whether to choose one approach for simplicity or use performance testing to find all of the special cases and select the approach -- including platform intrinsics -- to maximize performance, is the choice you must make.

> - endian::iterator, which will iterate across a range
> swapping values as necessary. It works with any swappable
> type.

Nice idea!

> The library is described/documented/tested in the file
> main.cpp at the root of the zip archive.

I'll try to have a look.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk