Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* "byte sex" or "endian" conversion program
@ 1993-10-02 21:25 Greg Sandell
  1993-10-03 14:56 ` Charles M Richmond
  1993-10-04 16:24 ` Steve Lionel
  0 siblings, 2 replies; 6+ messages in thread
From: Greg Sandell @ 1993-10-02 21:25 UTC (permalink / raw)


I work across several platforms including one DEC and several non-DEC 
machines.  The DEC represents binary differently than all the others.
(I suppose this isn't news to many people who read this bboard!)  I've
been told that the difference between standards is merely a swapping of
the upper and lower signficant portions of the value.  However, when
I've tried my own conversion programs (reading in a binary value then
writing it out swapped) they don't work.  There must be something I
don't know.

For example, if I had a loop to read in binary floats:

        char data[4], out[4];
        while (read(0,data,4) != 0) {

...I had thought that doing the following was the right thing:

	out[0] = data[2] ; out[1] = data[3];
	out[2] = data[0]; out[3] = data[1]
        write(1,out,4);
	
If this is wrong, can someone show me the right way?

Thanks in advance.  Email replies appreciated, as I don't typically read
this bboard.

Greg
--
Gregory J. Sandell  (sandell@epunix.sussex.ac.uk)
Experimental Psychology, University of Sussex
Brighton BN1 9QG England  +44-273-678058


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "byte sex" or "endian" conversion program
  1993-10-02 21:25 "byte sex" or "endian" conversion program Greg Sandell
@ 1993-10-03 14:56 ` Charles M Richmond
  1993-10-04  4:33   ` Thad P Floryan
  1993-10-04 16:24 ` Steve Lionel
  1 sibling, 1 reply; 6+ messages in thread
From: Charles M Richmond @ 1993-10-03 14:56 UTC (permalink / raw)




Newsgroups: comp.sys.dec
Subject: Re: "byte sex" or "endian" conversion program
References: <1993Oct2.212547.17237@syma.sussex.ac.uk>

sandell@epunix.susx.ac.uk (Greg Sandell) writes:

>I work across several platforms including one DEC and several non-DEC 
>machines.  The DEC represents binary differently than all the others.
>(I suppose this isn't news to many people who read this bboard!)  I've
>been told that the difference between standards is merely a swapping of
>the upper and lower signficant portions of the value.  However, when
>I've tried my own conversion programs (reading in a binary value then
>writing it out swapped) they don't work.  There must be something I
>don't know.


This kind of misinformed and disgruntled muttering has finally irritated
me to the point of sending out a small flame.

First off, the writer is blaming his ignorance of big/little endian on 
DEC. The vast majority of systems in the world are byte organized in the
same way as the DEC systems. This is demonstrably true when one considers
that all of the IBM PC systems are in this group. Secondly, just doing a
"man -k swap" yields the following:

DS3100 ULTRIX 4.3

asoko.iisc.com:267:man -k swap
fpc, get_fpc_csr, set_fpc_csr, swapRM, swapINX (3)  - floating-point control reg
isters
swab (3)                - swap bytes
swapon (2)              - add a swap device for interleaved paging/swapping
swapon (8)              - specify additional device for paging and swapping
swap_lw_bytes, swap_word_bytes, swap_words (3)  - perform byte swapping operatio
ns
swap_word, swap_half, swap_filehdr, swap_aouthdr, swap_scnhdr, swap_hdr, swap_fd
, swap_fi, swap_sym, swap_ext, swap_pd, swap_dn, swap_opt, swap_aux, swap_reloc,
 swap_ranlib (3) - swap the sex of the specified structure


SUNOS 

!$ man -k swap
swab (3)                - swap bytes
swapon (2)              - add a swap device for interleaved paging/swapping
swapon (8)              - specify additional device for paging and swapping
vswap (1)               - convert a foreign font file
!$ 


DEC OSF2.0   (1.2 and 1.3 are similar in content)

aijin> man -k swap
fstab (4)               - Static information about file systems and swap partiti
ons
swab (3)                - Swaps bytes
swapctl (2)             - manages the system swap space
swapon (2)              - Adds a swap device for interleaved paging and swapping
swapon (8)              - Specifies additional file for paging and swapping
swapsex (3)             - swap the sex of the specified structure

Charlie
-- 
*****************************************************************************
*  Charles Richmond 	Integrated International Systems Corporation        *
*  cmr@world.std.com							    *
*  Specializing in UNIX, X, Image Processing, and Communications.           *


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "byte sex" or "endian" conversion program
  1993-10-03 14:56 ` Charles M Richmond
@ 1993-10-04  4:33   ` Thad P Floryan
  0 siblings, 0 replies; 6+ messages in thread
From: Thad P Floryan @ 1993-10-04  4:33 UTC (permalink / raw)


In article <CEBtIw.A9B@world.std.com>
cmr@world.std.com (Charles M Richmond) writes:

|  sandell@epunix.susx.ac.uk (Greg Sandell) writes:
|  >
|  >I work across several platforms including one DEC and several non-DEC 
|  >machines.  The DEC represents binary differently than all the others.
|  >[...]
|  
|  This kind of misinformed and disgruntled muttering has finally irritated
|  me to the point of sending out a small flame.
|  
|  First off, the writer is blaming his ignorance of big/little endian on 
|  DEC. The vast majority of systems in the world are byte organized in the
|  same way as the DEC systems. This is demonstrably true when one considers
|  that all of the IBM PC systems are in this group. Secondly, just doing a
|  "man -k swap" yields the following:
|  [...]

Well, your article and flame are about as useless as tits on a bird.

The original poster was having problems with floating-point byte ordering,
so let's show him the nature of the problem.

Enclosed is an extract from an article I posted a year or so ago which
illustrates the "problem" on VAX (DEC) architectures contrasted to HP, AT&T,
Amiga, Apple, SGI, and Sun systems.  Though my example uses "double", the
problem with "float" is similar.  Besides which, DEC doesn't use the STANDARD
IEEE floating-point representation across-the-board by default on all its
platforms (if any).

In Silicon Valley, Intel is known as "First With the Worst", so the number of
machines using Intel's (and DEC's) byte-ordering as found on IBM-PCs (and
clones thereof) is irrelevant to the current discussion (though I do
understand the original reason for "little endian" memory data/instruction
organization (cost of memory back in the 1960s and 1970s)).

Thad Floryan [ thad@btr.com, thad@cup.portal.com, thad@netcom.com ]

---------- begin enclosure

/*	showres - display floating point numbers on architecture
 */

#include <stdio.h>
#include <sys/utsname.h>

typedef union {
	double	dbl_num;
	struct {
	   long	lw0;
	   long	lw1;
	} Ints;
} DBL;

DBL		foo;
struct utsname	arch;

main()
{
	uname(&arch);
	printf("Sys=%s, node=%s, release=%s, version=%s, machine=%s\n",
	arch.sysname, arch.nodename, arch.release, arch.version, arch.machine);

	foo.dbl_num = 1.0;		showme();
	foo.dbl_num = 10.0;		showme();
	foo.dbl_num = 100.0;		showme();
	foo.dbl_num = 1.23456789;	showme();
	foo.dbl_num = 12.3456789;	showme();
	foo.dbl_num = 123.456789;	showme();
}

showme()
{
printf("\t%20.10f = %08x, %08x\n", foo.dbl_num, foo.Ints.lw0, foo.Ints.lw1);
}


Sys=HP-UX, node=fig, release=7.0, version=B, machine=9000/350
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de1b
	       12.3456789000 = 4028b0fc, d324d5a2
	      123.4567890000 = 405edd3c, 07ee0b0b

Sys=UNIX, node=thadlabs, release=SYSTEM5, version=3.51m, machine=mc68k
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de1b
	       12.3456789000 = 4028b0fc, d324d5a2
	      123.4567890000 = 405edd3c, 07ee0b0b

Sys=HP-UX, node=hpdemo1, release=A.08.07, version=A, machine=9000/705
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de1b
	       12.3456789000 = 4028b0fc, d324d5a2
	      123.4567890000 = 405edd3c, 07ee0b0b

Sys=HP-UX, node=flame, release=A.B7.00, version=U, machine=9000/845
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de1b
	       12.3456789000 = 4028b0fc, d324d5a2
	      123.4567890000 = 405edd3c, 07ee0b0b

[System is a 68020/68881 Amiga]
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de18
	       12.3456789000 = 4028b0fc, d324d59e
	      123.4567890000 = 405edd3c, 07ee0b06

Sys=A/UX, node=aux, release=2.0, version=SVR2, machine=mc68020
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de1b
	       12.3456789000 = 4028b0fc, d324d5a2
	      123.4567890000 = 405edd3c, 07ee0b0b

Sys=IRIX, node=iris, release=4.0.5, version=06151813, machine=IP6
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de1b
	       12.3456789000 = 4028b0fc, d324d5a2
	      123.4567890000 = 405edd3c, 07ee0b0b

Sys=SunOS, node=cosmic, release=4.1.1, version=1, machine=sun3
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de1b
	       12.3456789000 = 4028b0fc, d324d5a2
	      123.4567890000 = 405edd3c, 07ee0b0b

Sys=SunOS, node=netcom, release=4.1.2_ER, version=1, machine=sun4m
	        1.0000000000 = 3ff00000, 00000000
	       10.0000000000 = 40240000, 00000000
	      100.0000000000 = 40590000, 00000000
	        1.2345678900 = 3ff3c0ca, 4283de1b
	       12.3456789000 = 4028b0fc, d324d5a2
	      123.4567890000 = 405edd3c, 07ee0b0b

[System is VAX-780 running VMS]
                1.0000000000 = 00004080, 00000000
               10.0000000000 = 00004220, 00000000
              100.0000000000 = 000043c8, 00000000
                1.2345678900 = 0652409e, f0dc141e
               12.3456789000 = 87e64245, ad139926
              123.4567890000 = e9e043f6, 58583f70

---------- end enclosure


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "byte sex" or "endian" conversion program
  1993-10-02 21:25 "byte sex" or "endian" conversion program Greg Sandell
  1993-10-03 14:56 ` Charles M Richmond
@ 1993-10-04 16:24 ` Steve Lionel
  1993-10-08 20:33   ` Kevin Ferguson
  1 sibling, 1 reply; 6+ messages in thread
From: Steve Lionel @ 1993-10-04 16:24 UTC (permalink / raw)



In article <1993Oct2.212547.17237@syma.sussex.ac.uk>, 
sandell@epunix.susx.ac.uk (Greg Sandell) writes:
|>I work across several platforms including one DEC and several non-DEC 
|>machines.  The DEC represents binary differently than all the others.
|>(I suppose this isn't news to many people who read this bboard!)  I've
|>been told that the difference between standards is merely a swapping of
|>the upper and lower signficant portions of the value.  However, when
|>I've tried my own conversion programs (reading in a binary value then
|>writing it out swapped) they don't work.  There must be something I
|>don't know.
|>

There are two independent issues you need to be concerned with.  The first
is byte ordering.  Digital systems use "little-endian" byte ordering, where
(for integers), the lowest addressed byte contains the least significant
bit. Some other systems use "big-endian" ordering, where the lowest addressed
byte contains the most significant bit. 

The second is floating point representation.  Digital VAX systems use VAX
floating point, which was derived from a format used on the PDP-11 and was
in use long before the other major format, IEEE floating, was developed.

If you're dealing with converting floating-point data between a
big-endian IEEE system and VAX, you'll need to swap the byte order of
integers and do a big-endian-IEEE-to-VAX floating conversion.  If you're
using a Digital system other than VAX (DECstations or AXP systems), those
systems support IEEE floating and you need only be concerned with the 
byte ordering.  (Note that on OpenVMS AXP, IEEE floating is not the default,
but you can select it with a compiler qualifier.)

If you're using Fortran, all DEC Fortran implementations (VAX, AXP and
RISC ULTRIX) support a feature that will do the conversions for you
automatically during unformatted input or output.  See the DEC Fortran
User Manual for more information (or send me mail and I'll be glad to help.)

OpenVMS AXP also has a routine called CVT$CONVERT_FLOAT which will do the
conversions on a single data item.

-- 

Steve Lionel                           lionel@quark.enet.dec.com
SDT Languages Group
Digital Equipment Corporation
110 Spit Brook Road, ZKO2-3/N30
Nashua, NH 03062                       "Free advice is worth every cent"


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "byte sex" or "endian" conversion program
  1993-10-04 16:24 ` Steve Lionel
@ 1993-10-08 20:33   ` Kevin Ferguson
  1993-10-09  1:49     ` fairfield
  0 siblings, 1 reply; 6+ messages in thread
From: Kevin Ferguson @ 1993-10-08 20:33 UTC (permalink / raw)


In article <28pio0$sgh@jac.nuo.dec.com>,
Steve Lionel <lionel@quark.enet.dec.com> wrote:

   [...snip...]
>
>If you're dealing with converting floating-point data between a
>big-endian IEEE system and VAX, you'll need to swap the byte order of
>integers and do a big-endian-IEEE-to-VAX floating conversion.  

   [...snip...]

I have a question regarding the IEEE-to-VAX F_floating point conversion.

I was recently presented with a pair of routines that
supposedly performed this conversion in each direction.  The
amazing thing was (to me anyway) that they obtained the VAX
float format by swapping bytes as you indicated and then
just multiplying by 4.  <jaw hits floor>

I was under the inpression that, due to the different 
normalized forms of both the exponent and mantissa, the 
conversion was more involved than that.

If memory serves me right (I don't have a copy of IEEE-754),
IEEE format uses an excess-127 form for its exponent and represents
the most-significant fraction bit.  The VAX F_floating point uses
an excess-128 form and the most-significant fraction bit is
implicitly assumed.

Am I missing something here?

TiA,
kevin ferguson (sorely confused)



-- 
                           "Face piles of trials with smiles.
Kevin Ferguson              It riles them to believe that
kferg@vpnet.chi.il.us         you perceive the web they weave."
                                                - Moody Blues


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: "byte sex" or "endian" conversion program
  1993-10-08 20:33   ` Kevin Ferguson
@ 1993-10-09  1:49     ` fairfield
  0 siblings, 0 replies; 6+ messages in thread
From: fairfield @ 1993-10-09  1:49 UTC (permalink / raw)


In article <1993Oct8.203318.18135@vpnet.chi.il.us>, kferg@vpnet.chi.il.us (Kevin Ferguson) writes:
> In article <28pio0$sgh@jac.nuo.dec.com>,
> Steve Lionel <lionel@quark.enet.dec.com> wrote:
> 
>    [...snip...]
>>
>>If you're dealing with converting floating-point data between a
>>big-endian IEEE system and VAX, you'll need to swap the byte order of
>>integers and do a big-endian-IEEE-to-VAX floating conversion.  
> 
>    [...snip...]
> 
> I have a question regarding the IEEE-to-VAX F_floating point conversion.
> 
> I was recently presented with a pair of routines that
> supposedly performed this conversion in each direction.  The
> amazing thing was (to me anyway) that they obtained the VAX
> float format by swapping bytes as you indicated and then
> just multiplying by 4.  <jaw hits floor>
> 
> I was under the inpression that, due to the different 
> normalized forms of both the exponent and mantissa, the 
> conversion was more involved than that.
> 
> If memory serves me right (I don't have a copy of IEEE-754),
> IEEE format uses an excess-127 form for its exponent and represents
> the most-significant fraction bit.  The VAX F_floating point uses
> an excess-128 form and the most-significant fraction bit is
> implicitly assumed.
> 
> Am I missing something here?

        Nope, you've got  it  right.   Actually  it's  quite  easy to see.
    After  swapping the bytes (well words) of a VAX F_float, it looks very
    much like an IEEE single: sign-bit in the left-most position, followed
    by 8 bits of exponent, followed by 23 bits of mantissa.

        IEEE chose to use the 8-bits of mantissa differently from the VAX:
    they chose to  use  an  approximately  symmetric  range  of minimum to
    maximum  values (1.7*10**-38 to 1.7*10**38 as compared to 0.29*10**-38
    to 1.7*10*38), and they can do de-normalized numbers, NaN's and Inf's,
    etc., with the bit patterns that are "left over" compared to  the  VAX
    format.

        The two important  points  are:  1)  if  you  take  the 8-bit IEEE
    exponent and interpret it as excess-127 instead of excess-128, you get
    one factor of two; and 2) if you take the normalization of IEEE:

		1.bbbbbbbbbbbbbbbbbbbbbbb

    and compare to the VAX normalization:

		0.1bbbbbbbbbbbbbbbbbbbbbbb

    you get a second factor of two.   Result: a simple factor of 4 between
    the  two  representations...at least for normalized values within  the
    common range of both representations.

        BTW, I'll bet is  faster  to  simply  add  or  subtract 2 from the
    exponent field than it is to do a floating multiply-by-4, although you
    do have to do bit-field manipulations...

	-Ken
-- 
 Dr. Kenneth H. Fairfield    |   Internet: Fairfield@Slac.Stanford.Edu
 SLAC, P.O.Box 4349, MS 98   |   DECnet:   45537::FAIRFIELD (45537=SLACVX)
 Stanford, CA   94309        |   FAX:      (415) 926-4335
 ----------------------------------------------------------------------------
 These opinions are mine, not SLAC's, Stanford's, nor the DOE's...


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1993-10-09  1:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1993-10-02 21:25 "byte sex" or "endian" conversion program Greg Sandell
1993-10-03 14:56 ` Charles M Richmond
1993-10-04  4:33   ` Thad P Floryan
1993-10-04 16:24 ` Steve Lionel
1993-10-08 20:33   ` Kevin Ferguson
1993-10-09  1:49     ` fairfield

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox