[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
This commit is contained in:
Michael J. Spencer 2013-01-02 20:14:11 +00:00
parent 8da72f82be
commit c8b18df9a7
4 changed files with 87 additions and 119 deletions

View File

@ -151,7 +151,6 @@
#define LLVM_UNLIKELY(EXPR) (EXPR)
#endif
// C++ doesn't support 'extern template' of template specializations. GCC does,
// but requires __extension__ before it. In the header, use this:
// EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
@ -187,7 +186,6 @@
#define LLVM_ATTRIBUTE_ALWAYS_INLINE
#endif
#ifdef __GNUC__
#define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
@ -225,6 +223,10 @@
#if defined(__clang__) || (__GNUC__ > 4) \
|| (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
#elif defined(_MSC_VER)
# define LLVM_BUILTIN_UNREACHABLE __assume(false)
#else
# define LLVM_BUILTIN_UNREACHABLE 0
#endif
/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
@ -236,4 +238,14 @@
# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
#endif
/// \macro LLVM_ASSUME_ALIGNED
/// \brief Returns a pointer with an assumed alignment.
#if defined(__GNUC__) && !defined(__clang__)
// FIXME: Enable on clang when it supports it.
# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
#else
# define LLVM_ASSUME_ALIGNED(p, a) \
(((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
#endif
#endif

View File

@ -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<typename value_type, alignment align>
struct alignment_access_helper;
template<typename value_type>
struct alignment_access_helper<value_type, aligned>
{
value_type val;
};
// Provides unaligned loads and stores.
#pragma pack(push)
#pragma pack(1)
template<typename value_type>
struct alignment_access_helper<value_type, unaligned>
{
value_type val;
};
#pragma pack(pop)
/// \brief ::value is either alignment, or alignof(T) if alignment is 0.
template<class T, int alignment>
struct PickAlignment {
enum {value = alignment == 0 ? AlignOf<T>::Alignment : alignment};
};
} // end namespace detail
namespace endian {
template<typename value_type, alignment align>
inline value_type read_le(const void *memory) {
value_type t =
reinterpret_cast<const detail::alignment_access_helper
<value_type, align> *>(memory)->val;
if (sys::isBigEndianHost())
return sys::SwapByteOrder(t);
return t;
}
template<typename value_type, alignment align>
inline void write_le(void *memory, value_type value) {
if (sys::isBigEndianHost())
value = sys::SwapByteOrder(value);
reinterpret_cast<detail::alignment_access_helper<value_type, align> *>
(memory)->val = value;
}
template<typename value_type, alignment align>
inline value_type read_be(const void *memory) {
value_type t =
reinterpret_cast<const detail::alignment_access_helper
<value_type, align> *>(memory)->val;
if (sys::isLittleEndianHost())
return sys::SwapByteOrder(t);
return t;
}
template<typename value_type, alignment align>
inline void write_be(void *memory, value_type value) {
if (sys::isLittleEndianHost())
value = sys::SwapByteOrder(value);
reinterpret_cast<detail::alignment_access_helper<value_type, align> *>
(memory)->val = value;
}
template<typename value_type, endianness endian>
inline value_type byte_swap(value_type value) {
if (endian != native && sys::isBigEndianHost() != (endian == big))
return sys::SwapByteOrder(value);
return value;
}
namespace detail {
template<typename value_type,
endianness endian,
alignment align>
class packed_endian_specific_integral;
std::size_t alignment>
inline value_type read(const void *memory) {
value_type ret;
template<typename value_type>
class packed_endian_specific_integral<value_type, little, unaligned> {
public:
memcpy(&ret,
LLVM_ASSUME_ALIGNED(memory,
(detail::PickAlignment<value_type, alignment>::value)),
sizeof(value_type));
return byte_swap<value_type, endian>(ret);
}
template<typename value_type,
endianness endian,
std::size_t alignment>
inline void write(void *memory, value_type value) {
value = byte_swap<value_type, endian>(value);
memcpy(LLVM_ASSUME_ALIGNED(memory,
(detail::PickAlignment<value_type, alignment>::value)),
&value,
sizeof(value_type));
}
} // end namespace endian
namespace detail {
template<typename value_type,
endianness endian,
std::size_t alignment>
struct packed_endian_specific_integral {
operator value_type() const {
return endian::read_le<value_type, unaligned>(Value);
return endian::read<value_type, endian, alignment>(
(const void*)Value.buffer);
}
void operator=(value_type newValue) {
endian::write_le<value_type, unaligned>((void *)&Value, newValue);
}
private:
uint8_t Value[sizeof(value_type)];
};
template<typename value_type>
class packed_endian_specific_integral<value_type, big, unaligned> {
public:
operator value_type() const {
return endian::read_be<value_type, unaligned>(Value);
}
void operator=(value_type newValue) {
endian::write_be<value_type, unaligned>((void *)&Value, newValue);
endian::write<value_type, endian, alignment>(
(void*)Value.buffer, newValue);
}
private:
uint8_t Value[sizeof(value_type)];
};
template<typename value_type>
class packed_endian_specific_integral<value_type, little, aligned> {
public:
operator value_type() const {
return endian::read_le<value_type, aligned>(&Value);
}
void operator=(value_type newValue) {
endian::write_le<value_type, aligned>((void *)&Value, newValue);
}
private:
value_type Value;
AlignedCharArray<PickAlignment<value_type, alignment>::value,
sizeof(value_type)> Value;
};
template<typename value_type>
class packed_endian_specific_integral<value_type, big, aligned> {
public:
operator value_type() const {
return endian::read_be<value_type, aligned>(&Value);
}
void operator=(value_type newValue) {
endian::write_be<value_type, aligned>((void *)&Value, newValue);
}
private:
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
<int64_t, big, aligned> aligned_big64_t;
typedef detail::packed_endian_specific_integral
<uint16_t, native, unaligned> unaligned_uint16_t;
typedef detail::packed_endian_specific_integral
<uint32_t, native, unaligned> unaligned_uint32_t;
typedef detail::packed_endian_specific_integral
<uint64_t, native, unaligned> unaligned_uint64_t;
typedef detail::packed_endian_specific_integral
<int16_t, native, unaligned> unaligned_int16_t;
typedef detail::packed_endian_specific_integral
<int32_t, native, unaligned> unaligned_int32_t;
typedef detail::packed_endian_specific_integral
<int64_t, native, unaligned> unaligned_int64_t;
} // end namespace llvm
} // end namespace support

View File

@ -21,36 +21,36 @@ namespace {
TEST(Endian, Read) {
// These are 5 bytes so we can be sure at least one of the reads is unaligned.
unsigned char big[] = {0x00, 0x01, 0x02, 0x03, 0x04};
unsigned char little[] = {0x00, 0x04, 0x03, 0x02, 0x01};
unsigned char bigval[] = {0x00, 0x01, 0x02, 0x03, 0x04};
unsigned char littleval[] = {0x00, 0x04, 0x03, 0x02, 0x01};
int32_t BigAsHost = 0x00010203;
EXPECT_EQ(BigAsHost, (endian::read_be<int32_t, unaligned>(big)));
EXPECT_EQ(BigAsHost, (endian::read<int32_t, big, unaligned>(bigval)));
int32_t LittleAsHost = 0x02030400;
EXPECT_EQ(LittleAsHost, (endian::read_le<int32_t, unaligned>(little)));
EXPECT_EQ(LittleAsHost,(endian::read<int32_t, little, unaligned>(littleval)));
EXPECT_EQ((endian::read_be<int32_t, unaligned>(big + 1)),
(endian::read_le<int32_t, unaligned>(little + 1)));
EXPECT_EQ((endian::read<int32_t, big, unaligned>(bigval + 1)),
(endian::read<int32_t, little, unaligned>(littleval + 1)));
}
TEST(Endian, Write) {
unsigned char data[5];
endian::write_be<int32_t, unaligned>(data, -1362446643);
endian::write<int32_t, big, unaligned>(data, -1362446643);
EXPECT_EQ(data[0], 0xAE);
EXPECT_EQ(data[1], 0xCA);
EXPECT_EQ(data[2], 0xB6);
EXPECT_EQ(data[3], 0xCD);
endian::write_be<int32_t, unaligned>(data + 1, -1362446643);
endian::write<int32_t, big, unaligned>(data + 1, -1362446643);
EXPECT_EQ(data[1], 0xAE);
EXPECT_EQ(data[2], 0xCA);
EXPECT_EQ(data[3], 0xB6);
EXPECT_EQ(data[4], 0xCD);
endian::write_le<int32_t, unaligned>(data, -1362446643);
endian::write<int32_t, little, unaligned>(data, -1362446643);
EXPECT_EQ(data[0], 0xCD);
EXPECT_EQ(data[1], 0xB6);
EXPECT_EQ(data[2], 0xCA);
EXPECT_EQ(data[3], 0xAE);
endian::write_le<int32_t, unaligned>(data + 1, -1362446643);
endian::write<int32_t, little, unaligned>(data + 1, -1362446643);
EXPECT_EQ(data[1], 0xCD);
EXPECT_EQ(data[2], 0xB6);
EXPECT_EQ(data[3], 0xCA);
@ -69,4 +69,4 @@ TEST(Endian, PackedEndianSpecificIntegral) {
EXPECT_EQ(*big_val, *little_val);
}
}
} // end anon namespace

View File

@ -790,7 +790,8 @@ template <typename value_type>
raw_ostream &operator <<( raw_ostream &OS
, const binary_le_impl<value_type> &BLE) {
char Buffer[sizeof(BLE.Value)];
support::endian::write_le<value_type, support::unaligned>(Buffer, BLE.Value);
support::endian::write<value_type, support::little, support::unaligned>(
Buffer, BLE.Value);
OS.write(Buffer, sizeof(BLE.Value));
return OS;
}