Commit 702d2603b39b0e9b2ab4b6a390b2797d7126085e
1 parent
50c155a3
Portable use of bswap_32
Showing
2 changed files
with
27 additions
and
1 deletions
configure.ac
| @@ -72,6 +72,7 @@ AM_CONDITIONAL(OS_QNX, test "$os_qnx" = "true") | @@ -72,6 +72,7 @@ AM_CONDITIONAL(OS_QNX, test "$os_qnx" = "true") | ||
| 72 | LT_INIT([disable-static win32-dll pic-only]) | 72 | LT_INIT([disable-static win32-dll pic-only]) |
| 73 | AC_CHECK_HEADERS([ \ | 73 | AC_CHECK_HEADERS([ \ |
| 74 | arpa/inet.h \ | 74 | arpa/inet.h \ |
| 75 | + byteswap.h \ | ||
| 75 | errno.h \ | 76 | errno.h \ |
| 76 | fcntl.h \ | 77 | fcntl.h \ |
| 77 | limits.h \ | 78 | limits.h \ |
src/modbus-data.c
| @@ -24,10 +24,35 @@ | @@ -24,10 +24,35 @@ | ||
| 24 | #endif | 24 | #endif |
| 25 | #include <string.h> | 25 | #include <string.h> |
| 26 | #include <assert.h> | 26 | #include <assert.h> |
| 27 | -#include <byteswap.h> | ||
| 28 | 27 | ||
| 29 | #include "modbus.h" | 28 | #include "modbus.h" |
| 30 | 29 | ||
| 30 | +#if defined(HAVE_BYTESWAP_H) | ||
| 31 | +# include <byteswap.h> | ||
| 32 | +#endif | ||
| 33 | + | ||
| 34 | +#if defined(__GNUC__) | ||
| 35 | +# define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__ * 10) | ||
| 36 | +# if GCC_VERSION >= 430 | ||
| 37 | +// Since GCC >= 4.30, GCC provides __builtin_bswapXX() alternatives so we switch to them | ||
| 38 | +# undef bswap_32 | ||
| 39 | +# define bswap_32 __builtin_bswap32 | ||
| 40 | +# endif | ||
| 41 | +#endif | ||
| 42 | + | ||
| 43 | +#if !defined (bswap_16) || !defined (bswap_32) | ||
| 44 | +# warning "Fallback on C functions for bswap_32" | ||
| 45 | +static inline uint16_t bswap_16(uint16_t x) | ||
| 46 | +{ | ||
| 47 | + return (x >> 8) | (x << 8); | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +static inline uint32_t bswap_32(uint32_t x) | ||
| 51 | +{ | ||
| 52 | + return (bswap_16(x & 0xffff) << 16) | (bswap_16(x >> 16)); | ||
| 53 | +} | ||
| 54 | +#endif | ||
| 55 | + | ||
| 31 | /* Sets many bits from a single byte value (all 8 bits of the byte value are | 56 | /* Sets many bits from a single byte value (all 8 bits of the byte value are |
| 32 | set) */ | 57 | set) */ |
| 33 | void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value) | 58 | void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value) |