From: David Griffith Date: Tue, 17 Dec 2013 06:58:25 +0000 (-0800) Subject: Runtime endian determination. X-Git-Url: https://scope-eye.net/git/?a=commitdiff_plain;h=2fc870119910cd77a6fd2ca3b342801065b31777;p=liskon_frotz.git Runtime endian determination. --- diff --git a/src/blorb/blorblib.c b/src/blorb/blorblib.c index 0ea93a0..988fa20 100644 --- a/src/blorb/blorblib.c +++ b/src/blorb/blorblib.c @@ -11,51 +11,43 @@ #include "blorb.h" #include "blorblow.h" -/* This endian stuff needs to be fixed with something from - * http://www.ibm.com/developerworks/aix/library/au-endianc/index.html - */ - -#ifdef BLORB_BIG_ENDIAN -static char contentsticker[] = "\nBlorb Library 1.0 (big-endian)\n"; -#define bb_native2(v) (v) -#define bb_native4(v) (v) -#endif - -#ifdef BLORB_LITTLE_ENDIAN -static char contentsticker[] = "\nBlorb Library 1.0 (little-endian)\n"; -#define bb_native2(v) \ - ( (((uint16)(v) >> 8) & 0x00ff) \ - | (((uint16)(v) << 8) & 0xff00)) -#define bb_native4(v) \ - ( (((uint32)(v) >> 24) & 0x000000ff) \ - | (((uint32)(v) >> 8) & 0x0000ff00) \ - | (((uint32)(v) << 8) & 0x00ff0000) \ - | (((uint32)(v) << 24) & 0xff000000)) -#endif - -#ifdef CRAPOLA -#ifndef bb_native4 -#error "You must define either BLORB_BIG_ENDIAN" -#error "or BLORB_LITTLE_ENDIAN in blorb.h in order" -#error "to compile this library." -#endif -#endif /* CRAPOLA */ - static int lib_inited = FALSE; static bb_err_t bb_initialize_map(bb_map_t *map); static bb_err_t bb_initialize(void); static int sortsplot(const void *p1, const void *p2); +static int bigendian; + +static uint16 bb_native2(uint16 v) +{ + if (bigendian) return v; + return ( (((uint16)(v) >> 8) & 0x00ff) + | (((uint16)(v) << 8) & 0xff00)); +} + +static uint32 bb_native4(uint32 v) +{ + if (bigendian) return v; + return ( (((uint32)(v) >> 24) & 0x000000ff) + | (((uint32)(v) >> 8) & 0x0000ff00) + | (((uint32)(v) << 8) & 0x00ff0000) + | (((uint32)(v) << 24) & 0xff000000)); +} /* Do some one-time startup tests. */ static bb_err_t bb_initialize() { + int i = 1; + char *p = (char *)&i; union { uint32 val; unsigned char ch[4]; } test; uint32 val; + if (p[0] == 1) bigendian = 0; + else bigendian = 1; + if (sizeof(uint32) != 4 || sizeof(uint16) != 2) return bb_err_CompileTime; /* Basic types are the wrong size. */