Subject: Re: byte swapping required
From: [email-masked] (Wolfgang Glas)

26 Nov 1998 17:25:56 -0800
Message-ID: <[email-masked]>
References: <[email-masked]>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
To: Michel Mehl <[email-masked]>
Cc: [email-masked]

Hi Michel !

  Maybe your program is dependent on the orderíng of bytes in a word or
long-word ? I also wrote software, which runs under IRIX and NT
(CygWin32) and fiddles 'round with binary data.
 
  I explain in short the difference between the storage layout of an PC
(little endian architecture) and the SGI workstation (big endian
architecture)

PC: The least-significant byte is stored first in the memory
    So, if you define 

    unsigned long i=0x04030201;

    'i' is stored as follows:

    byte |  0  |  1  |  2  | 3
    ------------------------------
         | 0x01| 0x02| 0x03| 0x04

SGI: The most-significant byte is stored first:

    So, if you define 

    unsigned long i=0x04030201;

    'i' is stored as follows:

    byte |  0  |  1  |  2  | 3
    ------------------------------
         | 0x04| 0x03| 0x02| 0x01

I give you a sample code, that generates a header file, which includes
preprocessor directives, that let you deside on which kind of
architecture you compile your code.

If you run the prgoram, it writes a file 'conf.h', which is used in your
C program like this:

#include "conf.h"

#if BYTE_ORDER == BIG_ENDIAN
/* some code depending on SGI byte ordering */
.......
#else
/* some code depending on PC byte ordering */
.......
#endif

This is conformant with the definitions in IRIX'
/usr/include/sys/endian.h file, which isn' existant on all
architectures.

  Wolfgang

P.S.: The following program is also suitable for DEC alphas, which have
an 8 byte 'long' type. It should determine the byte ordering for almost
all gcc platforms. :-)

----------------------file conf.c-------------------------
#include<stdio.h>

#define SIZEOF_FMT "#define conf_sizeof_%s %d\n"

/* check the byte ordering */
char *which_endian ()
{
  int   int_lword = 0x04030201;
  long long_lword = 0x04030201L;
  char *p;

/* sizeof(long) may be 8, so use int, if it is sufficient. */
  if (sizeof(int) == 4)
    p = (char *)(&int_lword);
  else
    p = (char *)(&long_lword);

  if (p[0]==1 && p[1]==2 && p[2]==3 && p[3]==4) return "LITTLE_ENDIAN";
  if (p[0]==4 && p[1]==3 && p[2]==2 && p[3]==1) return "BIG_ENDIAN";
  if (p[0]==3 && p[1]==4 && p[2]==1 && p[3]==2) return "PDP_ENDIAN";

  fprintf(stderr,"Unknown byte ordering %d%d%d%d in 'conf.c'.\n",
	  (int)(p[0]),(int)(p[1]),(int)(p[2]),(int)(p[3]));

  return "UNDEFINED_BYTE_ORDER";
}

int main(int argc, char *argv[])
{
  char *endian;
  FILE *conf = fopen ("conf.h","w");

  if (!conf) { perror(argv[0]); return 1; }
  
  fputs ("/* This file is created automatically by 'conf.c'.
*/\n",conf);
  fputs ("/* Do not edit manually.                          
*/\n",conf);

  fputs ("\n",conf);

  fprintf(conf,SIZEOF_FMT,"short  ",sizeof(short));
  fprintf(conf,SIZEOF_FMT,"int    ",sizeof(int));
  fprintf(conf,SIZEOF_FMT,"long   ",sizeof(long));
  fprintf(conf,SIZEOF_FMT,"float  ",sizeof(float));
  fprintf(conf,SIZEOF_FMT,"double ",sizeof(double));
  fprintf(conf,SIZEOF_FMT,"pointer",sizeof(void *));

  fputs ("\n",conf);

  fputs ("/* Imitate POSIX's endian.h */\n",conf);

  fputs ("#define	LITTLE_ENDIAN	1234	/* least-significant byte first
(vax) */\n",conf);
  fputs ("#define	BIG_ENDIAN	4321	/* most-significant byte first (IBM,
net) */\n",conf);
  fputs ("#define	PDP_ENDIAN	3412	/* LSB first in word, MSW first in
long (pdp) */\n",conf);
  
  fprintf(conf,"#define BYTE_ORDER %s\n",which_endian());

  fputs ("\n",conf);
  fputs ("/* end of file bsa_conf.h */\n",conf);

  fclose(conf);

  return 0;
}

----------------------end of file conf.c------------------ 
> Hi,
> 
> B20: Ported software execution problem (NT 4.0)
> 
> I have successfully compiled a program that run before on a SGI O2
> workstation.
> 
> However, at execution, the message "Architecture word mismatch : byte
> swapping required"  appears, even if I put an "exit" just after "main".
> I do absolutely not know from where it comes and how to bypass this
> trouble.
> 
> Than you.
> 
> --
> Regards
> Michel Mehl
> L.I.P, E.N.S. LYON, France
> * Office No. : +33 04 72 72 82 59
> * Email      : [email-masked]
> * Web        : http://wwwcaids.u-strasbg.fr/~mehl/
> -
> For help on using this list (especially unsubscribing), send a message to
> "[email-masked]" with one line of text: "help".

--
Mag. Wolfgang Glas
Institut fuer hydraulische Stroemungsmaschinen
Kopernikusgasse 24                              Phone:++43/316/873/7578
A-8010 Graz                                     Fax:  ++43/316/873/7577

mailto:[email-masked]   
http://fhysmsg01.tu-graz.ac.at/Wolfgang.Glas/
-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".