diff --git a/CMakeLists.txt b/CMakeLists.txt index 9441ca7..8ace05a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,11 @@ set(ALL_SRC_FILES ${ALL_SRC_FILES}) # Find bit size of the target architecture math(EXPR GMIO_TARGET_ARCH_BIT_SIZE "8 * ${CMAKE_SIZEOF_VOID_P}") +# Test if host's architecture is big-endian +include(TestBigEndian) +test_big_endian(GMIO_HOST_IS_BIG_ENDIAN) + +# Find available C99 features if(NOT BUILD_STRICT_C90) # Have ? check_include_files(stdint.h GMIO_HAVE_STDINT_H) diff --git a/src/gmio_core/config.h.cmake b/src/gmio_core/config.h.cmake index d269156..6e65221 100644 --- a/src/gmio_core/config.h.cmake +++ b/src/gmio_core/config.h.cmake @@ -27,6 +27,8 @@ #cmakedefine GMIO_HAVE_MSVC_BUILTIN_BSWAP_FUNC +#cmakedefine GMIO_HOST_IS_BIG_ENDIAN + #if defined(__APPLE__) # if defined(__i386__) || defined(__ppc__) # define GMIO_TARGET_ARCH_BIT_SIZE 32 diff --git a/src/gmio_core/endian.h b/src/gmio_core/endian.h index 08a79ce..95a5d7c 100644 --- a/src/gmio_core/endian.h +++ b/src/gmio_core/endian.h @@ -33,6 +33,14 @@ enum gmio_endianness typedef enum gmio_endianness gmio_endianness_t; +enum { +#ifdef GMIO_HOST_IS_BIG_ENDIAN + GMIO_HOST_ENDIANNESS = GMIO_BIG_ENDIAN +#else + GMIO_HOST_ENDIANNESS = GMIO_LITTLE_ENDIAN +#endif +}; + GMIO_C_LINKAGE_BEGIN /*! Returns endianness (byte order) of the host's CPU architecture */ diff --git a/src/gmio_stl/stlb_read.c b/src/gmio_stl/stlb_read.c index 9c1cc29..6d43366 100644 --- a/src/gmio_stl/stlb_read.c +++ b/src/gmio_stl/stlb_read.c @@ -72,10 +72,8 @@ int gmio_stlb_read( const gmio_stlb_read_options_t* options) { /* Constants */ - const gmio_endianness_t host_byte_order = - gmio_host_endianness(); const gmio_endianness_t byte_order = - options != NULL ? options->byte_order : host_byte_order; + options != NULL ? options->byte_order : GMIO_HOST_ENDIANNESS; const uint32_t max_facet_count_per_read = trsf != NULL ? gmio_size_to_uint32( @@ -92,7 +90,7 @@ int gmio_stlb_read( return error; /* Initialize rparams */ - if (host_byte_order != byte_order) + if (byte_order != GMIO_HOST_ENDIANNESS) rparams.fix_endian_func = gmio_stl_triangle_bswap; /* Read header */ @@ -107,7 +105,7 @@ int gmio_stlb_read( return GMIO_STLB_READ_FACET_COUNT_ERROR; memcpy(&total_facet_count, trsf->buffer, sizeof(uint32_t)); - if (host_byte_order != byte_order) + if (byte_order != GMIO_HOST_ENDIANNESS) total_facet_count = gmio_uint32_bswap(total_facet_count); /* Callback to notify triangle count and header data */ diff --git a/src/gmio_stl/stlb_write.c b/src/gmio_stl/stlb_write.c index ec154d0..5ffbb68 100644 --- a/src/gmio_stl/stlb_write.c +++ b/src/gmio_stl/stlb_write.c @@ -73,10 +73,8 @@ int gmio_stlb_write( const gmio_stlb_write_options_t* options) { /* Constants */ - const gmio_endianness_t host_byte_order = - gmio_host_endianness(); const gmio_endianness_t byte_order = - options != NULL ? options->byte_order : host_byte_order; + options != NULL ? options->byte_order : GMIO_HOST_ENDIANNESS; const uint8_t* header_data = options != NULL ? options->header_data : NULL; const uint32_t facet_count = @@ -93,7 +91,7 @@ int gmio_stlb_write( return error; /* Initialize wparams */ - if (host_byte_order != byte_order) + if (byte_order != GMIO_HOST_ENDIANNESS) wparams.fix_endian_func = gmio_stl_triangle_bswap; wparams.facet_count = gmio_size_to_uint32( trsf->buffer_size / GMIO_STLB_TRIANGLE_RAWSIZE);