Skip to content

Commit

Permalink
mapshape, maptree: determine endianess at compile time
Browse files Browse the repository at this point in the history
Removing the unnecessary runtime checks and the byte swapping code
reduces the library size by 5 kB.
  • Loading branch information
MaxKellermann committed Oct 7, 2021
1 parent dfc4559 commit 4334114
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
32 changes: 11 additions & 21 deletions mapshape.c
Expand Up @@ -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 );
Expand Down Expand Up @@ -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. */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -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. */
Expand Down
23 changes: 12 additions & 11 deletions maptree.c
Expand Up @@ -33,6 +33,18 @@

#include <limits.h>

#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 */
Expand Down Expand Up @@ -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. */
Expand Down

0 comments on commit 4334114

Please sign in to comment.