From: Michael Gernoth Date: Sun, 13 Jan 2008 12:04:29 +0000 (+0100) Subject: automatic byteorder detection X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/micropolis/commitdiff_plain/e9c442bbb6d129259d7489eb227dd289c8b3f67c?ds=inline automatic byteorder detection --- diff --git a/src/sim/g_cam.c b/src/sim/g_cam.c index c2f1b12..139da46 100644 --- a/src/sim/g_cam.c +++ b/src/sim/g_cam.c @@ -1083,39 +1083,45 @@ cam_load_rule(Cam *cam, char *filename) FILE *fp; QUAD magic, neighborhood, rule_size; Byte *rule; + int test = 1; if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "cam: Can't open rule file \"%s\"\n", filename); return; } -/* XXX: Make this byte order independent!!! */ - -#if defined(MSDOS) || defined(OSF1) || defined(IS_INTEL) #define SWAPQUAD(x) ((x = ((x <<24) & 0xff000000) | \ ((x <<8) & 0x00ff0000) | \ ((x >>8) & 0x0000ff00) | \ ((x >>24) & 0x000000ff)), 0) -#else - -#define SWAPQUAD(x) 0 - -#endif - - if ((fread(&magic, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || - SWAPQUAD(magic) || - (magic != 0xcac0cac0) || - (fread(&neighborhood, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || - SWAPQUAD(neighborhood) || - (fread(&rule_size, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || - SWAPQUAD(rule_size) || - ((rule = (Byte *)malloc(rule_size)) == NULL) || - (fread(rule, 1, rule_size, fp) != rule_size)) { - fprintf(stderr, "cam: Bad rule file \"%s\"\n", filename); - fclose(fp); - return; +/* XXX: Make this byte order independent!!! */ + if ((*(unsigned char*) (&test))) { + if ((fread(&magic, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || + SWAPQUAD(magic) || + (magic != 0xcac0cac0) || + (fread(&neighborhood, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || + SWAPQUAD(neighborhood) || + (fread(&rule_size, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || + SWAPQUAD(rule_size) || + ((rule = (Byte *)malloc(rule_size)) == NULL) || + (fread(rule, 1, rule_size, fp) != rule_size)) { + fprintf(stderr, "cam: Bad rule file \"%s\"\n", filename); + fclose(fp); + return; + } + } else { + if ((fread(&magic, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || + (magic != 0xcac0cac0) || + (fread(&neighborhood, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || + (fread(&rule_size, 1, sizeof(QUAD), fp) != sizeof(QUAD)) || + ((rule = (Byte *)malloc(rule_size)) == NULL) || + (fread(rule, 1, rule_size, fp) != rule_size)) { + fprintf(stderr, "cam: Bad rule file \"%s\"\n", filename); + fclose(fp); + return; + } } fclose(fp); diff --git a/src/sim/makefile b/src/sim/makefile index e74f94e..bce5113 100644 --- a/src/sim/makefile +++ b/src/sim/makefile @@ -10,8 +10,8 @@ CC = gcc OPTFLAGS = -O3 #OPTFLAGS = -g -#DEFINES = -DIS_LINUX -DIS_INTEL -DCAM -DNET -DEFINES = -DIS_LINUX -DIS_INTEL +#DEFINES = -DIS_LINUX -DCAM -DNET +DEFINES = -DIS_LINUX CFLAGS = $(OPTFLAGS) $(DEFINES) diff --git a/src/sim/s_fileio.c b/src/sim/s_fileio.c index 3768f01..37637f7 100644 --- a/src/sim/s_fileio.c +++ b/src/sim/s_fileio.c @@ -62,17 +62,19 @@ #include "sim.h" -#if defined(MSDOS) || defined(OSF1) || defined(IS_INTEL) - #define SWAP_SHORTS(a,b) _swap_shorts(a,b) #define SWAP_LONGS(a,b) _swap_longs(a,b) #define HALF_SWAP_LONGS(a,b) _half_swap_longs(a,b) +#define NOOP_ON_BE { int test = 1; if (!(*(unsigned char*) (&test))) return; } + static void _swap_shorts(short *buf, int len) { int i; + NOOP_ON_BE; + /* Flip bytes in each short! */ for (i = 0; i < len; i++) { *buf = ((*buf & 0xFF) <<8) | ((*buf &0xFF00) >>8); @@ -85,6 +87,8 @@ _swap_longs(long *buf, int len) { int i; + NOOP_ON_BE; + /* Flip bytes in each long! */ for (i = 0; i < len; i++) { long l = *buf; @@ -102,6 +106,8 @@ _half_swap_longs(long *buf, int len) { int i; + NOOP_ON_BE + /* Flip bytes in each long! */ for (i = 0; i < len; i++) { long l = *buf; @@ -112,15 +118,6 @@ _half_swap_longs(long *buf, int len) } } -#else - -#define SWAP_SHORTS(a, b) -#define SWAP_LONGS(a, b) -#define HALF_SWAP_LONGS(a, b) - -#endif - - static int _load_short(short *buf, int len, FILE *f) {