From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,7e86b7f8965dd11d X-Google-Attributes: gid1164f8,public From: bruce@ais.com Subject: Re: DEC Alpha- big endian or little endian? Date: 1996/02/16 Message-ID: <1996Feb16.010643.8731@ais>#1/1 X-Deja-AN: 139658228 references: <4ejjs1$2pr@sun20.ccd.bnl.gov> <4ffml4$fb2@sun20.ccd.bnl.gov> organization: Applied Information Systems, Chapel Hill, NC newsgroups: comp.sys.dec In article <4ffml4$fb2@sun20.ccd.bnl.gov>, olchansk@a0.phy.bnl.gov (Konstantin Olchanski) writes: > In article , 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