Index: llvm/include/llvm/Support/Endian.h =================================================================== --- llvm/include/llvm/Support/Endian.h +++ llvm/include/llvm/Support/Endian.h @@ -33,48 +33,71 @@ } // end namespace detail namespace endian { +constexpr endianness system_endianness() { + return sys::IsBigEndianHost ? big : little; +} + +template +inline value_type byte_swap(value_type value, endianness endian) { + if ((endian != native) && (endian != system_endianness())) + sys::swapByteOrder(value); + return value; +} + /// Swap the bytes of value to match the given endianness. template inline value_type byte_swap(value_type value) { - if (endian != native && sys::IsBigEndianHost != (endian == big)) - sys::swapByteOrder(value); - return value; + return byte_swap(value, endian); } /// Read a value of a particular endianness from memory. -template -inline value_type read(const void *memory) { +template +inline value_type read(const void *memory, endianness endian) { value_type ret; memcpy(&ret, - LLVM_ASSUME_ALIGNED(memory, - (detail::PickAlignment::value)), + LLVM_ASSUME_ALIGNED( + memory, (detail::PickAlignment::value)), sizeof(value_type)); - return byte_swap(ret); + return byte_swap(ret, endian); +} + +template +inline value_type read(const void *memory) { + return read(memory, endian); } /// Read a value of a particular endianness from a buffer, and increment the /// buffer past that value. +template +inline value_type readNext(const CharT *&memory, endianness endian) { + value_type ret = read(memory, endian); + memory += sizeof(value_type); + return ret; +} + template inline value_type readNext(const CharT *&memory) { - value_type ret = read(memory); - memory += sizeof(value_type); - return ret; + return readNext(memory, endian); } /// Write a value to memory with a particular endianness. +template +inline void write(void *memory, value_type value, endianness endian) { + value = byte_swap(value, endian); + memcpy(LLVM_ASSUME_ALIGNED( + memory, (detail::PickAlignment::value)), + &value, sizeof(value_type)); +} + template inline void write(void *memory, value_type value) { - value = byte_swap(value); - memcpy(LLVM_ASSUME_ALIGNED(memory, - (detail::PickAlignment::value)), - &value, - sizeof(value_type)); + write(memory, value, endian); } template @@ -300,10 +323,24 @@ unaligned_int64_t; namespace endian { +template inline T read(const void *P, endianness E) { + return read(P, E); +} + template inline T read(const void *P) { return *(const detail::packed_endian_specific_integral *)P; } +inline uint16_t read16(const void *P, endianness E) { + return read(P, E); +} +inline uint32_t read32(const void *P, endianness E) { + return read(P, E); +} +inline uint64_t read64(const void *P, endianness E) { + return read(P, E); +} + template inline uint16_t read16(const void *P) { return read(P); } @@ -321,10 +358,24 @@ inline uint32_t read32be(const void *P) { return read32(P); } inline uint64_t read64be(const void *P) { return read64(P); } +template inline void write(void *P, T V, endianness E) { + write(P, V, E); +} + template inline void write(void *P, T V) { *(detail::packed_endian_specific_integral *)P = V; } +inline void write16(void *P, uint16_t V, endianness E) { + write(P, V, E); +} +inline void write32(void *P, uint32_t V, endianness E) { + write(P, V, E); +} +inline void write64(void *P, uint64_t V, endianness E) { + write(P, V, E); +} + template inline void write16(void *P, uint16_t V) { write(P, V); } Index: llvm/include/llvm/Support/Host.h =================================================================== --- llvm/include/llvm/Support/Host.h +++ llvm/include/llvm/Support/Host.h @@ -32,9 +32,9 @@ namespace sys { #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN - static const bool IsBigEndianHost = true; +constexpr bool IsBigEndianHost = true; #else - static const bool IsBigEndianHost = false; +constexpr bool IsBigEndianHost = false; #endif static const bool IsLittleEndianHost = !IsBigEndianHost;