Byte Order Swapping Function

Jonathan M Davis jmdavisProg at gmx.com
Thu Jul 14 03:59:21 PDT 2011


On Thursday 14 July 2011 11:51:14 Regan Heath wrote:
> On Thu, 14 Jul 2011 11:05:07 +0100, Jonathan M Davis <jmdavisProg at gmx.com>
> 
> wrote:
> > On Thursday 14 July 2011 10:55:49 Regan Heath wrote:
> >> On Thu, 14 Jul 2011 08:14:10 +0100, Jonathan M Davis
> >> <jmdavisProg at gmx.com>
> >> 
> >> wrote:
> >> > On Thursday 14 July 2011 00:03:23 Andrew Wiley wrote:
> >> >> On Wed, Jul 13, 2011 at 11:59 PM, Jonathan M Davis
> >> > 
> >> > <jmdavisProg at gmx.com>wrote:
> >> >> > On Wednesday 13 July 2011 23:37:02 Andrew Wiley wrote:
> >> >> > > Hey, does anyone else thing a function like this belongs
> >> >> > > in
> >> >> > > Phobos,
> >> >> > > and
> >> >> > 
> >> >> > if
> >> >> > 
> >> >> > > so, where do you think it should go?
> >> >> > > 
> >> >> > > T ntoh(T)(T val) if (__traits(isArithmetic, T)) {
> >> >> > > version(BigEndian) {
> >> >> > > 
> >> >> > >  return val;
> >> >> > > 
> >> >> > > }
> >> >> > > else version (LittleEndian) {
> >> >> > > 
> >> >> > >  ubyte[] arr = (cast(ubyte*)&val)[0 .. T.sizeof];
> >> >> > > 
> >> >> > > ubyte temp;
> >> >> > > for(int i = 0; i < T.sizeof/2; i++) {
> >> >> > > 
> >> >> > >  temp = arr[i];
> >> >> > > 
> >> >> > > arr[i] = arr[T.sizeof - i - 1];
> >> >> > > arr[T.sizeof - i - 1] = temp;
> >> >> > > 
> >> >> > >  }
> >> >> > > 
> >> >> > > return val;
> >> >> > > }
> >> >> > > 
> >> >> > >  else static assert(0, "Are you sure you're using a
> >> >> > >  computer?");
> >> >> > >  }
> >> >> > > 
> >> >> > > I was looking for something along these lines in the
> >> >> > > docs
> >> >> > > today and
> >> >> > 
> >> >> > couldn't
> >> >> > 
> >> >> > > find it. It looks like there's a stream in std.stream to
> >> >> > > do
> >> >> > > this,
> >> >> > > but,
> >> >> > > well, I think we've all been pretending std.stream
> >> >> > > doesn't
> >> >> > > exist for
> >> >> > > a
> >> >> > > while now.
> >> >> > 
> >> >> > core.sys.posix.arpa.inet.d
> >> >> > std.c.windows.winsock.d
> >> >> 
> >> >> Both of those are platform specific, and neither of them is
> >> >> general
> >> >> enough
> >> >> to handle longs.
> >> > 
> >> > They're only platform-specific in that you have to import one in
> >> > Posix
> >> > and
> >> > another in Windows. Other than that, they're completely
> >> > platform-independent.
> >> > They're the standard functions for it in C/C++. Now, it's true
> >> > that
> >> > there
> >> > isn't one for ulong (since there is no standard one for 64-bit in
> >> > C/C++), and
> >> > there would definitely be some value in having a function which
> >> > was
> >> > overloaded
> >> > on the type so that you could just use the one function for
> >> > ushort,
> >> > uint, and
> >> > ulong, but ntohs and ntohl _are_ provided by druntime and Phobos.
> >> > So,
> >> > it's not
> >> > like nothing is there.
> >> > 
> >> > I did have to create a 64-bit version for std.datetime, so it has
> >> > a
> >> > private
> >> > function called endianSwap64 to do the job. So, it's not like I'm
> >> 
> >> saying
> >> 
> >> > that
> >> > the situation couldn't be improved, but druntime and Phobos do
> >> 
> >> currently
> >> 
> >> > give
> >> > you the exact same thing that C and C++ do.
> >> 
> >> The crypto library I have been meaning to polish up and submit for
> >> inclusion into phobos needs these functions in several variants.  I
> >> was
> >> hoping they could/would be included somewhere more 'general' at some
> >> stage, and any duplicates like those already mentioned removed..
> > 
> > Oh, it could be done, and it probably should be done. But other than the
> > 64-
> > bit version (since it's not particularly standard in C or C++), the
> > functions
> > are there if you need them.
> 
> That's my point.  I need 8/16/32/64/128 bit versions and it really would
> be better if there were general variants.  My version are less than
> optimal, but do use intrinsics where possible.  Someone else can do a far
> better job than I, and it really should be done once, by that person.
> Surely we have the infrastructure for someone to add this to phobos?  If
> something this simple can't or won't be done, what hope do we have!?

I never said that it wouldn't be done. I just said that the standard C ones 
are available. It likely will get added.

However, I would point out that there really isn't any point in adding an 8-
bit version, and we don't have 128-bit integers in D yet, so you're not going 
to get a 128-bit version.

- Jonathan M Davis


More information about the Digitalmars-d mailing list