#define LLVM_SUPPORT_ENDIAN_H
#include "llvm/Config/config.h"
+#include "llvm/System/Host.h"
#include "llvm/System/SwapByteOrder.h"
#include "llvm/Support/type_traits.h"
enum endianness {big, little};
enum alignment {unaligned, aligned};
-template<typename value_type, int host, int target>
-static typename enable_if_c<host == target, value_type>::type
-SwapByteOrderIfDifferent(value_type value) {
- // Target endianess is the same as the host. Just pass the value through.
- return value;
-}
-
-template<typename value_type, int host, int target>
-static typename enable_if_c<host != target, value_type>::type
-SwapByteOrderIfDifferent(value_type value) {
- return sys::SwapByteOrder<value_type>(value);
-}
-
namespace detail {
template<typename value_type, alignment align>
} // end namespace detail
-#if defined(LLVM_IS_HOST_BIG_ENDIAN) \
- || defined(_BIG_ENDIAN) || defined(__BIG_ENDIAN__)
-static const endianness host_endianness = big;
-#else
-static const endianness host_endianness = little;
-#endif
-
-struct endian {
- template<typename value_type, alignment align>
- static value_type read_le(const void *memory) {
- return SwapByteOrderIfDifferent<value_type, host_endianness, little>(
- reinterpret_cast<const detail::alignment_access_helper
- <value_type, align> *>(memory)->val);
- }
-
- template<typename value_type, alignment align>
- static void write_le(void *memory, value_type value) {
- reinterpret_cast<detail::alignment_access_helper<value_type, align> *>
- (memory)->val =
- SwapByteOrderIfDifferent< value_type
- , host_endianness
- , little>(value);
- }
-
- template<typename value_type, alignment align>
- static value_type read_be(const void *memory) {
- return SwapByteOrderIfDifferent<value_type, host_endianness, big>(
- reinterpret_cast<const detail::alignment_access_helper
- <value_type, align> *>(memory)->val);
- }
-
- template<typename value_type, alignment align>
- static void write_be(void *memory, value_type value) {
- reinterpret_cast<detail::alignment_access_helper
- <value_type, align> *>(memory)->val =
- SwapByteOrderIfDifferent< value_type
- , host_endianness
- , big>(value);
+namespace endian {\r
+ template<typename value_type, alignment align>\r
+ static value_type read_le(const void *memory) {\r
+ value_type t =\r
+ reinterpret_cast<const detail::alignment_access_helper\r
+ <value_type, align> *>(memory)->val;\r
+ if (sys::isBigEndianHost())\r
+ return sys::SwapByteOrder(t);\r
+ return t;\r
+ }\r
+\r
+ template<typename value_type, alignment align>\r
+ static void write_le(void *memory, value_type value) {\r
+ if (sys::isBigEndianHost())\r
+ value = sys::SwapByteOrder(value);\r
+ reinterpret_cast<detail::alignment_access_helper<value_type, align> *>\r
+ (memory)->val = value;\r
+ }\r
+\r
+ template<typename value_type, alignment align>\r
+ static value_type read_be(const void *memory) {\r
+ value_type t =\r
+ reinterpret_cast<const detail::alignment_access_helper\r
+ <value_type, align> *>(memory)->val;\r
+ if (sys::isLittleEndianHost())\r
+ return sys::SwapByteOrder(t);\r
+ return t;\r
+ }\r
+\r
+ template<typename value_type, alignment align>\r
+ static void write_be(void *memory, value_type value) {\r
+ if (sys::isLittleEndianHost())\r
+ value = sys::SwapByteOrder(value);\r
+ reinterpret_cast<detail::alignment_access_helper<value_type, align> *>\r
+ (memory)->val = value;\r
}
};
namespace detail {
template<typename value_type,
- endianness target_endianness,
- alignment target_alignment>
+ endianness endian,
+ alignment align>
class packed_endian_specific_integral;
template<typename value_type>