From c8b18df9a79ddb759d6a563dd7ebd90b85ae4918 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Wed, 2 Jan 2013 20:14:11 +0000 Subject: [Support][Endian] Add support for specifying the alignment and native unaligned types. * Add support for specifying the alignment to use. * Add the concept of native endianness. Used for unaligned native types. The native alignment and read/write simplification is based on a patch by Richard Smith. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171406 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Endian.h | 161 +++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 103 deletions(-) (limited to 'include/llvm/Support/Endian.h') diff --git a/include/llvm/Support/Endian.h b/include/llvm/Support/Endian.h index 8d5649d..d438fac 100644 --- a/include/llvm/Support/Endian.h +++ b/include/llvm/Support/Endian.h @@ -14,136 +14,78 @@ #ifndef LLVM_SUPPORT_ENDIAN_H #define LLVM_SUPPORT_ENDIAN_H +#include "llvm/Support/AlignOf.h" #include "llvm/Support/Host.h" #include "llvm/Support/SwapByteOrder.h" #include "llvm/Support/type_traits.h" namespace llvm { namespace support { +enum endianness {big, little, native}; -enum endianness {big, little}; -enum alignment {unaligned, aligned}; +// These are named values for common alignments. +enum {aligned = 0, unaligned = 1}; namespace detail { - -template -struct alignment_access_helper; - -template -struct alignment_access_helper -{ - value_type val; -}; - -// Provides unaligned loads and stores. -#pragma pack(push) -#pragma pack(1) -template -struct alignment_access_helper -{ - value_type val; -}; -#pragma pack(pop) - + /// \brief ::value is either alignment, or alignof(T) if alignment is 0. + template + struct PickAlignment { + enum {value = alignment == 0 ? AlignOf::Alignment : alignment}; + }; } // end namespace detail namespace endian { - template - inline value_type read_le(const void *memory) { - value_type t = - reinterpret_cast *>(memory)->val; - if (sys::isBigEndianHost()) - return sys::SwapByteOrder(t); - return t; - } - - template - inline void write_le(void *memory, value_type value) { - if (sys::isBigEndianHost()) - value = sys::SwapByteOrder(value); - reinterpret_cast *> - (memory)->val = value; - } +template +inline value_type byte_swap(value_type value) { + if (endian != native && sys::isBigEndianHost() != (endian == big)) + return sys::SwapByteOrder(value); + return value; +} - template - inline value_type read_be(const void *memory) { - value_type t = - reinterpret_cast *>(memory)->val; - if (sys::isLittleEndianHost()) - return sys::SwapByteOrder(t); - return t; - } +template +inline value_type read(const void *memory) { + value_type ret; + + memcpy(&ret, + LLVM_ASSUME_ALIGNED(memory, + (detail::PickAlignment::value)), + sizeof(value_type)); + return byte_swap(ret); +} - template - inline void write_be(void *memory, value_type value) { - if (sys::isLittleEndianHost()) - value = sys::SwapByteOrder(value); - reinterpret_cast *> - (memory)->val = value; - } +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)); } +} // end namespace endian namespace detail { - template -class packed_endian_specific_integral; - -template -class packed_endian_specific_integral { -public: - operator value_type() const { - return endian::read_le(Value); - } - void operator=(value_type newValue) { - endian::write_le((void *)&Value, newValue); - } -private: - uint8_t Value[sizeof(value_type)]; -}; - -template -class packed_endian_specific_integral { -public: + std::size_t alignment> +struct packed_endian_specific_integral { operator value_type() const { - return endian::read_be(Value); + return endian::read( + (const void*)Value.buffer); } - void operator=(value_type newValue) { - endian::write_be((void *)&Value, newValue); - } -private: - uint8_t Value[sizeof(value_type)]; -}; -template -class packed_endian_specific_integral { -public: - operator value_type() const { - return endian::read_le(&Value); - } void operator=(value_type newValue) { - endian::write_le((void *)&Value, newValue); + endian::write( + (void*)Value.buffer, newValue); } -private: - value_type Value; -}; -template -class packed_endian_specific_integral { -public: - operator value_type() const { - return endian::read_be(&Value); - } - void operator=(value_type newValue) { - endian::write_be((void *)&Value, newValue); - } private: - value_type Value; + AlignedCharArray::value, + sizeof(value_type)> Value; }; - } // end namespace detail typedef detail::packed_endian_specific_integral @@ -218,6 +160,19 @@ typedef detail::packed_endian_specific_integral typedef detail::packed_endian_specific_integral aligned_big64_t; +typedef detail::packed_endian_specific_integral + unaligned_uint16_t; +typedef detail::packed_endian_specific_integral + unaligned_uint32_t; +typedef detail::packed_endian_specific_integral + unaligned_uint64_t; + +typedef detail::packed_endian_specific_integral + unaligned_int16_t; +typedef detail::packed_endian_specific_integral + unaligned_int32_t; +typedef detail::packed_endian_specific_integral + unaligned_int64_t; } // end namespace llvm } // end namespace support -- cgit v1.1