[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Discuss-gnuradio] sparc <--> x86 data exchange

From: Dave Dodge
Subject: Re: [Discuss-gnuradio] sparc <--> x86 data exchange
Date: Thu, 15 Dec 2005 20:00:56 -0500
User-agent: Mutt/1.4.2i

On Thu, Dec 15, 2005 at 03:29:38PM -0500, cswiger wrote:
> Gang - any hints on what happens to float data when transfered
> from a sparc machine to an x86?  I understand byte-swap but this
> looks like word-swap.

The problem is that the default settings for "od" are almost never
what you actually want, and are making things confusing.

What "od" is doing is treating the file as a sequence of 16-bit
integers in native byte order.  On a little-endian machine this means
that od is _not_ showing you the order that the bytes actually appear
in the file.  Since the values you're really interested in are 32
bits, it's also not grabbing the right number of bytes for display.

Assume the 32-bit value in the application's memory is 0x11223344.
When you write this to a file in native byte order, you get the byte

  sparc: 11 22 33 44
    x86: 44 33 22 11

If you "od" each file on its own system, you get:

  sparc on sparc: 0000000 1122 3344
      x86 on x86: 0000000 3344 1122

This has the appearance of the "word swapping" you were seeing.  Now,
if you move the sparc file over to x86 and od it, you get:

  sparc on x86: 0000000 2211 4433

Which looks like byte swapping.  If you then use dd to byte-swap the
contents of the file, you get the file:

  sparc swapped: 22 11 44 33

When run through od on x86:

  sparc swapped on x86: 0000000 1122 3344

That matches the od output from sparc on sparc, but od isn't looking
at the right thing so it's still not right.  If x86 reads a 32-bit
value from these files, neither of the sparc files will work:

            x86 reading from x86: 0x11223344   <-- correct
          x86 reading from sparc: 0x44332211
  x86 reading from sparc swapped: 0x33441122

So assuming the application is actually reading and writing this data
as 32-bit native words, what you need to do is reverse the order of
each _four_ bytes of the file when moving between sparc and x86.
Here's a bit of perl code to try.  I'm sure there's much better tools
for doing this same thing, or a perl one-liner that works faster, but
this should at least get you started:

      my $a = getc || exit;
      my $b = getc || exit;
      my $c = getc || exit;
      my $d = getc || exit;
      print $d,$c,$b,$a;

When using "od" with these files, what you really want to do is

  od --format=x1
    which will show you the actual byte ordering in the file.

  od --format=x4
    which will read 32-bit words from the file in the machine's byte
    order and show you what values the application is seeing.  When
    you've got the files convered properly, you should see the same
    result from this on both sparc and x86.

For data files intended to be portable, or when transmitting over the
network, the best solution is to pick a fixed byte ordering for the
file and do your byte reordering during I/O.  In the TCP/IP networking
world, big-endian (such as used by Sparc) is commonly used in network
protocols and is sometimes called "network order"; there's little
functions like "htonl" and "ntohs" that can convert values between
"host" and "network" byte order in memory.

                                                  -Dave Dodge

reply via email to

[Prev in Thread] Current Thread [Next in Thread]