lib: try GCC builtins for endian byteswap
This commit is contained in:
parent
b3b83e0343
commit
0abd16d62c
1 changed files with 35 additions and 26 deletions
|
|
@ -310,29 +310,40 @@ CAPN_INLINE uint8_t capn_flip8(uint8_t v) {
|
|||
return v;
|
||||
}
|
||||
CAPN_INLINE uint16_t capn_flip16(uint16_t v) {
|
||||
#if !defined(__BYTE_ORDER) || (__BYTE_ORDER != __LITTLE_ENDIAN)
|
||||
#if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN)
|
||||
return v;
|
||||
#elif defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN) && \
|
||||
defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8
|
||||
return __builtin_bswap16(v);
|
||||
#else
|
||||
union { uint16_t u; uint8_t v[2]; } s;
|
||||
s.v[0] = (uint8_t)v;
|
||||
s.v[1] = (uint8_t)(v>>8);
|
||||
return s.u;
|
||||
#else
|
||||
return v;
|
||||
#endif
|
||||
}
|
||||
CAPN_INLINE uint32_t capn_flip32(uint32_t v) {
|
||||
#if !defined(__BYTE_ORDER) || (__BYTE_ORDER != __LITTLE_ENDIAN)
|
||||
#if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN)
|
||||
return v;
|
||||
#elif defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN) && \
|
||||
defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8
|
||||
return __builtin_bswap32(v);
|
||||
#else
|
||||
union { uint32_t u; uint8_t v[4]; } s;
|
||||
s.v[0] = (uint8_t)v;
|
||||
s.v[1] = (uint8_t)(v>>8);
|
||||
s.v[2] = (uint8_t)(v>>16);
|
||||
s.v[3] = (uint8_t)(v>>24);
|
||||
return s.u;
|
||||
#else
|
||||
return v;
|
||||
#endif
|
||||
}
|
||||
CAPN_INLINE uint64_t capn_flip64(uint64_t v) {
|
||||
#if !defined(__BYTE_ORDER) || (__BYTE_ORDER != __LITTLE_ENDIAN)
|
||||
#if defined(__BYTE_ORDER) && (__BYTE_ORDER == __LITTLE_ENDIAN)
|
||||
return v;
|
||||
#elif defined(__BYTE_ORDER) && (__BYTE_ORDER == __BIG_ENDIAN) && \
|
||||
defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8
|
||||
return __builtin_bswap64(v);
|
||||
#else
|
||||
union { uint64_t u; uint8_t v[8]; } s;
|
||||
s.v[0] = (uint8_t)v;
|
||||
s.v[1] = (uint8_t)(v>>8);
|
||||
|
|
@ -343,8 +354,6 @@ CAPN_INLINE uint64_t capn_flip64(uint64_t v) {
|
|||
s.v[6] = (uint8_t)(v>>48);
|
||||
s.v[7] = (uint8_t)(v>>56);
|
||||
return s.u;
|
||||
#else
|
||||
return v;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue