Optimize retro_endianness when MSB_FIRST is known

This commit is contained in:
twinaphex 2017-05-06 16:52:30 +02:00
parent 593334747b
commit 0ba93887bf

View File

@ -61,13 +61,13 @@
* Returns: greater than 0 if little-endian,
* otherwise big-endian.
**/
#if defined(MSB_FIRST)
#define is_little_endian() (0)
#elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
#define is_little_endian() (1)
#else
static INLINE uint8_t is_little_endian(void)
{
#if defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
return 1;
#elif defined(MSB_FIRST)
return 0;
#else
union
{
uint16_t x;
@ -76,8 +76,8 @@ static INLINE uint8_t is_little_endian(void)
u.x = 1;
return u.y[0];
#endif
}
#endif
/**
* swap_if_big64:
@ -88,12 +88,19 @@ static INLINE uint8_t is_little_endian(void)
* Returns: Byteswapped value in case system is big-endian,
* otherwise returns same value.
**/
#if defined(MSB_FIRST)
#define swap_if_big64(val) (SWAP64(val))
#elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
#define swap_if_big64(val) (val)
#else
static INLINE uint64_t swap_if_big64(uint64_t val)
{
if (is_little_endian())
return val;
return SWAP64(val);
}
#endif
/**
* swap_if_big32:
@ -104,12 +111,19 @@ static INLINE uint64_t swap_if_big64(uint64_t val)
* Returns: Byteswapped value in case system is big-endian,
* otherwise returns same value.
**/
#if defined(MSB_FIRST)
#define swap_if_big32(val) (SWAP32(val))
#elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
#define swap_if_big32(val) (val)
#else
static INLINE uint32_t swap_if_big32(uint32_t val)
{
if (is_little_endian())
return val;
return SWAP32(val);
}
#endif
/**
* swap_if_little64:
@ -120,12 +134,19 @@ static INLINE uint32_t swap_if_big32(uint32_t val)
* Returns: Byteswapped value in case system is little-endian,
* otherwise returns same value.
**/
#if defined(MSB_FIRST)
#define swap_if_little64(val) (val)
#elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
#define swap_if_little64(val) (SWAP64(val))
#else
static INLINE uint64_t swap_if_little64(uint64_t val)
{
if (is_little_endian())
return SWAP64(val);
return val;
}
#endif
/**
* swap_if_little32:
@ -136,12 +157,19 @@ static INLINE uint64_t swap_if_little64(uint64_t val)
* Returns: Byteswapped value in case system is little-endian,
* otherwise returns same value.
**/
#if defined(MSB_FIRST)
#define swap_if_little32(val) (val)
#elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
#define swap_if_little32(val) (SWAP32(val))
#else
static INLINE uint32_t swap_if_little32(uint32_t val)
{
if (is_little_endian())
return SWAP32(val);
return val;
}
#endif
/**
* swap_if_big16:
@ -152,13 +180,19 @@ static INLINE uint32_t swap_if_little32(uint32_t val)
* Returns: Byteswapped value in case system is big-endian,
* otherwise returns same value.
**/
#if defined(MSB_FIRST)
#define swap_if_big16(val) (SWAP16(val))
#elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
#define swap_if_big16(val) (val)
#else
static INLINE uint16_t swap_if_big16(uint16_t val)
{
if (is_little_endian())
return val;
return SWAP16(val);
}
#endif
/**
* swap_if_little16:
@ -169,12 +203,19 @@ static INLINE uint16_t swap_if_big16(uint16_t val)
* Returns: Byteswapped value in case system is little-endian,
* otherwise returns same value.
**/
#if defined(MSB_FIRST)
#define swap_if_little16(val) (val)
#elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
#define swap_if_little16(val) (SWAP16(val))
#else
static INLINE uint16_t swap_if_little16(uint16_t val)
{
if (is_little_endian())
return SWAP16(val);
return val;
}
#endif
/**
* store32be: