From: Zachary Turner Date: Mon, 6 Feb 2017 18:31:21 +0000 (+0000) Subject: [Support] Add support for runtime endian values. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=058101e7dcfec2e42a77e107a9301eccc939aed9;p=llvm [Support] Add support for runtime endian values. Endian functions only support reading and writing when the endianness is known at compile time. This patch adds overloads where the endianness is a runtime value, and then delegates the compile-time versions to the runtime versions. Differential Revision: https://reviews.llvm.org/D29467 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294209 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/Endian.h b/include/llvm/Support/Endian.h index cbe3d67b1f9..5619c75f09f 100644 --- a/include/llvm/Support/Endian.h +++ b/include/llvm/Support/Endian.h @@ -33,48 +33,71 @@ namespace detail { } // 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 @@ typedef detail::packed_endian_specific_integral 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 uint16_t read16be(const void *P) { return read16(P); } 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); } diff --git a/include/llvm/Support/Host.h b/include/llvm/Support/Host.h index 9df584c68c0..878debde605 100644 --- a/include/llvm/Support/Host.h +++ b/include/llvm/Support/Host.h @@ -32,9 +32,9 @@ namespace llvm { 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;