Part 19: __start and __stop Symbols, Byte Swapping
__start and __stop Symbols
Byte Swapping
// Endian simply indicates whether the host is big endian or not.
struct Endian { public:
// Used for template specializations.
static const bool host_big_endian = __BYTE_ORDER == __BIG_ENDIAN;
};
// Valtype_base is a template based on size (8, 16, 32, 64) which
// defines the type Valtype as the unsigned integer of the specified size.
template struct Valtype_base;
template<> struct Valtype_base<16> { typedef uint16_t Valtype; };
// Convert_endian is a template based on size and on whether the host // and target have the same endianness. It defines the type Valtype // as Valtype_base does, and also defines a function convert_host // which takes an argument of type Valtype and returns the same value,
// but swapped if the host and target have different endianness.
template struct Convert_endian;
template struct Convert_endian { typedef typename Valtype_base::Valtype Valtype;
static inline Valtype convert_host(Valtype v) { return v; } };
template<> struct Convert_endian<16, false> { typedef Valtype_base<16>::Valtype Valtype;
static inline Valtype convert_host(Valtype v) { return bswap_16(v); } };
// Convert is a template based on size and on whether the target is
// big endian. It defines Valtype and convert_host like
// Convert_endian. That is, it is just like Convert_endian except in
// the meaning of the second template parameter.
template struct Convert { typedef typename Valtype_base::Valtype Valtype;
static inline Valtype convert_host(Valtype v) { return Convert_endian ::convert_host(v); } };
// Swap is a template based on size and on whether the target is big
// endian. It defines the type Valtype and the functions readval and
// writeval. The functions read and write values of the appropriate
// size out of buffers, swapping them if necessary.
template struct Swap { typedef typename Valtype_base::Valtype Valtype;
static inline Valtype readval(const Valtype* wv) { return Convert::convert_host(*wv); }
static inline void writeval(Valtype* wv, Valtype v) { *wv = Convert::convert_host(v); }Last updated
Was this helpful?