compile time endianess check, faster on little endian cpus
This commit is contained in:
parent
a15accf4eb
commit
ec9bb1417e
1 changed files with 40 additions and 20 deletions
|
|
@ -24,6 +24,8 @@ extern "C" {
|
|||
#define CAPN_INLINE static
|
||||
#endif
|
||||
|
||||
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
|
||||
|
||||
#define CAPN_VERSION 1
|
||||
|
||||
/* struct capn is a common structure shared between segments in the same
|
||||
|
|
@ -299,30 +301,48 @@ CAPN_INLINE uint8_t capn_flip8(uint8_t v) {
|
|||
return v;
|
||||
}
|
||||
CAPN_INLINE uint16_t capn_flip16(uint16_t v) {
|
||||
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;
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
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;
|
||||
#elif __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return v;
|
||||
#else
|
||||
#error "Unable to determine endianess ";
|
||||
#endif
|
||||
}
|
||||
CAPN_INLINE uint32_t capn_flip32(uint32_t v) {
|
||||
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;
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
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;
|
||||
#elif __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return v;
|
||||
#else
|
||||
#error "Unable to determine endianess ";
|
||||
#endif
|
||||
}
|
||||
CAPN_INLINE uint64_t capn_flip64(uint64_t v) {
|
||||
union { uint64_t u; uint8_t v[8]; } 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);
|
||||
s.v[4] = (uint8_t)(v>>32);
|
||||
s.v[5] = (uint8_t)(v>>40);
|
||||
s.v[6] = (uint8_t)(v>>48);
|
||||
s.v[7] = (uint8_t)(v>>56);
|
||||
return s.u;
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
union { uint64_t u; uint8_t v[8]; } 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);
|
||||
s.v[4] = (uint8_t)(v>>32);
|
||||
s.v[5] = (uint8_t)(v>>40);
|
||||
s.v[6] = (uint8_t)(v>>48);
|
||||
s.v[7] = (uint8_t)(v>>56);
|
||||
return s.u;
|
||||
#elif __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return v;
|
||||
#else
|
||||
#error "Unable to determine endianess ";
|
||||
#endif
|
||||
}
|
||||
|
||||
CAPN_INLINE int capn_write1(capn_ptr p, int off, int val) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue