Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: bruce@ais.com
Subject: Re: DEC Alpha- big endian or little endian?
Date: 1996/02/16	[thread overview]
Message-ID: <1996Feb16.010643.8731@ais> (raw)
In-Reply-To: 4ffml4$fb2@sun20.ccd.bnl.gov

In article <4ffml4$fb2@sun20.ccd.bnl.gov>, olchansk@a0.phy.bnl.gov (Konstantin Olchanski) writes:
> In article <jfm6-0302962341500001@dlup-j.jagunet.com>, jfm6@jagunet.com (John McNamara) writes:
>> In article <4ejjs1$2pr@sun20.ccd.bnl.gov>,
>> olchansk@a0.phy.bnl.gov (Konstantin Olchanski) wrote:
>> >
>> >The problem is that all our data sets are written in the
>> >        big-endian format and the little-endian version
>> >        of our code cannot read them.
>> 
>> Why not just write the few lines of code to swap bytes on
>> read and write?  If you're really dealing with large files,
>> the programs are likely memory bound in which case the few
>> extra logical ops to swap bytes should be essentially free.
> 
> "just swapping bytes" gets somewhat complicated if the data
> 	is a horrible mix of ints, shorts and chars...
> 
> but, I guess there is no other way... 
> 
> I am considering writing
> 	a program in perl to read the C header files
> 	and automatically produce endian-changing routines.
> 	Maybe somebody already did this? Any suggestions?

I don't have anything to read the .h files and produce code to extract
data from the structs, though I doubt that such a thing would be very
difficult to produce.  I do have some macros that make converting
between the two representations a bit less painful.  The following is
cut with a few modifications from a header file used by a fairly large
project that runs on a variety of architectures of varying word sizes
and on big and little endien machines.  Note that this does NOT deal
with IEEE or VAX-format floating point issues;  that program does not
use floats in its binary files.  You will also notice that the macros
are rather "little-endien-centric" in that they assume that the data
require no conversion on little endien machines;  your definitions of
the EXTRACT_* macros would have to have the opposite #ifdef.  Beware also
that the macros are "fragile" in the sense that you cannot use arguments
with side effects, e.g., EXTRACT_WORD(*p++) is a no-no;  in this way
they are similar to the usual macro definitions of max and min which are
also fragile.

#ifdef _ALPHA
#define	WORD16	short
#define	LONG32	int
#else
#define	WORD16	short
#define LONG32	long
#endif

typedef unsigned char	BYTE;
typedef unsigned WORD16	WORD;
typedef unsigned LONG32	DWORD;

#define LOBYTE(w)	((BYTE)(w))
#define HIBYTE(w)	((BYTE)(((UINT)(w) >> 8) & 0xff))
#define LOWORD(l)	((WORD)(DWORD)(l))
#define HIWORD(l)	((WORD)((((DWORD)(l)) >> 16) & 0xffff))

#ifdef LITTLE_ENDIEN
#define EXTRACT_WORD(w)	(w)
#define EXTRACT_LONG(l)	(l)
#else
#define EXTRACT_WORD(w)	((LOBYTE(w)<<8) | HIBYTE(w))
#define EXTRACT_LONG(l)	((EXTRACT_WORD(LOWORD(l))<<16)|EXTRACT_WORD(HIWORD(l)))
#endif

You can also swap bytes by extracting the high and low bytes and moving
them into an array of bytes, then reading it out as a word or long word
as appropriate, but on most architectures that's probably not as efficient
unless the compiler is smart enough to spot what you're doing.

Good luck,

Bruce C. Wright



  reply	other threads:[~1996-02-16  5:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-01-29  5:00 DEC Alpha- big endian or little endian? Konstantin Olchanski
1996-02-03  5:00 ` John McNamara
1996-02-04  5:00   ` bruce
1996-02-09  5:00   ` Konstantin Olchanski
1996-02-16  5:00     ` bruce [this message]
1996-02-04  5:00 ` fairfield
1996-02-05  5:00 ` Steve Lionel
read-only archive

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