From 4334114a74fe7155f735407ce62867e8039a4a9e Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 15 Sep 2017 22:44:51 +0200 Subject: [PATCH] mapshape, maptree: determine endianess at compile time Removing the unnecessary runtime checks and the byte swapping code reduces the library size by 5 kB. --- mapshape.c | 32 +++++++++++--------------------- maptree.c | 23 ++++++++++++----------- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/mapshape.c b/mapshape.c index a24f7b0885..19f7b0508d 100644 --- a/mapshape.c +++ b/mapshape.c @@ -52,7 +52,17 @@ #define ByteCopy( a, b, c ) memcpy( b, a, c ) -static int bBigEndian; +#ifdef __BYTE_ORDER__ +/* GCC/clang predefined macro */ +#define bBigEndian (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#elif defined(_MSC_VER) +/* MSVC doesn't support the C99 trick below, but all Microsoft + platforms are little-endian */ +#define bBigEndian false +#else +/* generic check */ +#define bBigEndian (((union{int in;char out;}){1}).out) +#endif /* SHX reading */ static int msSHXLoadAll( SHPHandle psSHP ); @@ -215,16 +225,6 @@ SHPHandle msSHPOpen( const char * pszLayer, const char * pszAccess ) else pszAccess = "rb"; - /* -------------------------------------------------------------------- */ - /* Establish the byte order on this machine. */ - /* -------------------------------------------------------------------- */ - i = 1; - /* cppcheck-suppress knownConditionTrueFalse */ - if( *((uchar *) &i) == 1 ) - bBigEndian = MS_FALSE; - else - bBigEndian = MS_TRUE; - /* -------------------------------------------------------------------- */ /* Initialize the info structure. */ /* -------------------------------------------------------------------- */ @@ -464,16 +464,6 @@ SHPHandle msSHPCreate( const char * pszLayer, int nShapeType ) ms_int32 i32; double dValue; - /* -------------------------------------------------------------------- */ - /* Establish the byte order on this system. */ - /* -------------------------------------------------------------------- */ - i = 1; - /* cppcheck-suppress knownConditionTrueFalse */ - if( *((uchar *) &i) == 1 ) - bBigEndian = MS_FALSE; - else - bBigEndian = MS_TRUE; - /* -------------------------------------------------------------------- */ /* Compute the base (layer) name. If there is any extension */ /* on the passed in filename we will strip it off. */ diff --git a/maptree.c b/maptree.c index 7149fe8bbd..4dd4be2001 100644 --- a/maptree.c +++ b/maptree.c @@ -33,6 +33,18 @@ #include +#ifdef __BYTE_ORDER__ +/* GCC/clang predefined macro */ +#define bBigEndian (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +#elif defined(_MSC_VER) +/* MSVC doesn't support the C99 trick below, but all Microsoft + platforms are little-endian */ +#define bBigEndian false +#else +/* generic check */ +#define bBigEndian (((union{int in;char out;}){1}).out) +#endif + /* -------------------------------------------------------------------- */ /* If the following is 0.5, nodes will be split in half. If it */ /* is 0.6 then each subnode will contain 60% of the parent */ @@ -89,17 +101,6 @@ SHPTreeHandle msSHPDiskTreeOpen(const char * pszTree, int debug) char pabyBuf[16]; int i; - char bBigEndian; - - /* -------------------------------------------------------------------- */ - /* Establish the byte order on this machine. */ - /* -------------------------------------------------------------------- */ - i = 1; - /* cppcheck-suppress knownConditionTrueFalse */ - if( *((uchar *) &i) == 1 ) - bBigEndian = MS_FALSE; - else - bBigEndian = MS_TRUE; /* -------------------------------------------------------------------- */ /* Initialize the info structure. */