Stop using endian macros in NumberUtils.cfh Use an inline function instead. Modern compilers can compute the result at compile time and eliminate the if/else statement.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/a923dd38 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/a923dd38 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/a923dd38 Branch: refs/heads/master Commit: a923dd3858b899e477e9135f1a56c95a23b8797b Parents: 4b54156 Author: Nick Wellnhofer <[email protected]> Authored: Fri Feb 3 14:05:44 2017 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Fri Feb 3 14:52:54 2017 +0100 ---------------------------------------------------------------------- core/Lucy/Util/NumberUtils.cfh | 115 +++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 49 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/a923dd38/core/Lucy/Util/NumberUtils.cfh ---------------------------------------------------------------------- diff --git a/core/Lucy/Util/NumberUtils.cfh b/core/Lucy/Util/NumberUtils.cfh index 6a36928..b2bd04a 100644 --- a/core/Lucy/Util/NumberUtils.cfh +++ b/core/Lucy/Util/NumberUtils.cfh @@ -202,48 +202,63 @@ __C__ #include <string.h> +// Modern compilers should compute the result at compile time. +// Taken from http://stackoverflow.com/a/1001373 +static CFISH_INLINE bool +lucy_NumUtil_is_bigend() { + union { + uint32_t i; + char c[4]; + } u = { 0x01020304 }; + + return u.c[0] == 1; +} + static CFISH_INLINE void lucy_NumUtil_encode_bigend_u16(uint16_t value, void *dest_ptr) { uint8_t *dest = *(uint8_t**)dest_ptr; -#ifdef CFISH_BIG_END - memcpy(dest, &value, sizeof(uint16_t)); -#else /* little endian */ - uint8_t *source = (uint8_t*)&value; - dest[0] = source[1]; - dest[1] = source[0]; -#endif /* CFISH_BIG_END (and little endian) */ + if (lucy_NumUtil_is_bigend()) { + memcpy(dest, &value, sizeof(uint16_t)); + } + else { + uint8_t *source = (uint8_t*)&value; + dest[0] = source[1]; + dest[1] = source[0]; + } } static CFISH_INLINE void lucy_NumUtil_encode_bigend_u32(uint32_t value, void *dest_ptr) { uint8_t *dest = *(uint8_t**)dest_ptr; -#ifdef CFISH_BIG_END - memcpy(dest, &value, sizeof(uint32_t)); -#else /* little endian */ - uint8_t *source = (uint8_t*)&value; - dest[0] = source[3]; - dest[1] = source[2]; - dest[2] = source[1]; - dest[3] = source[0]; -#endif /* CFISH_BIG_END (and little endian) */ + if (lucy_NumUtil_is_bigend()) { + memcpy(dest, &value, sizeof(uint32_t)); + } + else { + uint8_t *source = (uint8_t*)&value; + dest[0] = source[3]; + dest[1] = source[2]; + dest[2] = source[1]; + dest[3] = source[0]; + } } static CFISH_INLINE void lucy_NumUtil_encode_bigend_u64(uint64_t value, void *dest_ptr) { uint8_t *dest = *(uint8_t**)dest_ptr; -#ifdef CFISH_BIG_END - memcpy(dest, &value, sizeof(uint64_t)); -#else /* little endian */ - uint8_t *source = (uint8_t*)&value; - dest[0] = source[7]; - dest[1] = source[6]; - dest[2] = source[5]; - dest[3] = source[4]; - dest[4] = source[3]; - dest[5] = source[2]; - dest[6] = source[1]; - dest[7] = source[0]; -#endif /* CFISH_BIG_END (and little endian) */ + if (lucy_NumUtil_is_bigend()) { + memcpy(dest, &value, sizeof(uint64_t)); + } + else { + uint8_t *source = (uint8_t*)&value; + dest[0] = source[7]; + dest[1] = source[6]; + dest[2] = source[5]; + dest[3] = source[4]; + dest[4] = source[3]; + dest[5] = source[2]; + dest[6] = source[1]; + dest[7] = source[0]; + } } static CFISH_INLINE uint16_t @@ -280,34 +295,36 @@ lucy_NumUtil_decode_bigend_u64(const void *source) { static CFISH_INLINE void lucy_NumUtil_encode_bigend_f32(float value, void *dest_ptr) { uint8_t *dest = *(uint8_t**)dest_ptr; -#ifdef CFISH_BIG_END - memcpy(dest, &value, sizeof(float)); -#else - union { float f; uint32_t u32; } duo; - duo.f = value; - lucy_NumUtil_encode_bigend_u32(duo.u32, &dest); -#endif + if (lucy_NumUtil_is_bigend()) { + memcpy(dest, &value, sizeof(float)); + } + else { + union { float f; uint32_t u32; } duo; + duo.f = value; + lucy_NumUtil_encode_bigend_u32(duo.u32, &dest); + } } static CFISH_INLINE void lucy_NumUtil_encode_bigend_f64(double value, void *dest_ptr) { uint8_t *dest = *(uint8_t**)dest_ptr; -#ifdef CFISH_BIG_END - memcpy(dest, &value, sizeof(double)); -#else - union { double d; uint64_t u64; } duo; - duo.d = value; - lucy_NumUtil_encode_bigend_u64(duo.u64, &dest); -#endif + if (lucy_NumUtil_is_bigend()) { + memcpy(dest, &value, sizeof(double)); + } + else { + union { double d; uint64_t u64; } duo; + duo.d = value; + lucy_NumUtil_encode_bigend_u64(duo.u64, &dest); + } } static CFISH_INLINE float lucy_NumUtil_decode_bigend_f32(const void *source) { union { float f; uint32_t u32; } duo; memcpy(&duo, source, sizeof(float)); -#ifdef CFISH_LITTLE_END - duo.u32 = lucy_NumUtil_decode_bigend_u32(&duo.u32); -#endif + if (!lucy_NumUtil_is_bigend()) { + duo.u32 = lucy_NumUtil_decode_bigend_u32(&duo.u32); + } return duo.f; } @@ -315,9 +332,9 @@ static CFISH_INLINE double lucy_NumUtil_decode_bigend_f64(const void *source) { union { double d; uint64_t u64; } duo; memcpy(&duo, source, sizeof(double)); -#ifdef CFISH_LITTLE_END - duo.u64 = lucy_NumUtil_decode_bigend_u64(&duo.u64); -#endif + if (!lucy_NumUtil_is_bigend()) { + duo.u64 = lucy_NumUtil_decode_bigend_u64(&duo.u64); + } return duo.d; }
