Commit 702d2603b39b0e9b2ab4b6a390b2797d7126085e
1 parent
50c155a3
Portable use of bswap_32
Showing
2 changed files
with
27 additions
and
1 deletions
configure.ac
src/modbus-data.c
| ... | ... | @@ -24,10 +24,35 @@ |
| 24 | 24 | #endif |
| 25 | 25 | #include <string.h> |
| 26 | 26 | #include <assert.h> |
| 27 | -#include <byteswap.h> | |
| 28 | 27 | |
| 29 | 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 | 56 | /* Sets many bits from a single byte value (all 8 bits of the byte value are |
| 32 | 57 | set) */ |
| 33 | 58 | void modbus_set_bits_from_byte(uint8_t *dest, int index, const uint8_t value) | ... | ... |