#ifndef ENDIANESS_H
#define ENDIANESS_H
#include <endian.h>
#include <SupportDefs.h>
\file endianess.h
\break Endianess conversion support functions.
The functions le2h() and h2le() convert various integer types from
little endian to host format and vice versa respectively.
*/
static inline
uint8
swap_value(uint8 v)
{
return v;
}
static inline
int8
swap_value(int8 v)
{
return v;
}
static inline
uint16
swap_value(uint16 v)
{
return ((v & 0xff) << 8) | (v >> 8);
}
static inline
int16
swap_value(int16 v)
{
return (int16)swap_value((uint16)v);
}
static inline
uint32
swap_value(uint32 v)
{
return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8)
| (v >> 24);
}
static inline
int32
swap_value(int32 v)
{
return (int32)swap_value((uint32)v);
}
static inline
uint64
swap_value(uint64 v)
{
return (uint64(swap_value(uint32(v & 0xffffffffULL))) << 32)
| uint64(swap_value(uint32((v & 0xffffffff00000000ULL) >> 32)));
}
static inline
int64
swap_value(int64 v)
{
return (int64)swap_value((uint64)v);
}
template<typename T>
static inline
T
le2h(const T &v)
{
#if LITTLE_ENDIAN
return v;
#else
return swap_value(v);
#endif
}
template<typename T>
static inline
T
h2le(const T &v)
{
#if LITTLE_ENDIAN
return v;
#else
return swap_value(v);
#endif
}
#endif