The Sparta Modeling Framework
Loading...
Searching...
No Matches
ByteOrder.hpp
Go to the documentation of this file.
1// <ByteOrder.hpp> -*- C++ -*-
2
10#pragma once
11
12#include <iostream>
13#include <typeinfo>
14
15#include <boost/mpl/assert.hpp>
16
19
24#if __BYTE_ORDER != __LITTLE_ENDIAN
25#error Byte order of host must be little endian for ArchData to run properly
26#endif
27
28#define HOST_INT_SIZE sizeof(uint32_t)
29
30namespace sparta
31{
74 {
75 LE = 0,
76 BE = 1
77 };
78
79
81 template <typename T>
82 inline typename std::enable_if<std::is_integral<T>::value, T>::type
83 byte_swap(T val)
84 {
85 throw SpartaException("Do not know how to byteswap type '") << typeid(T).name()
86 << "' for value " << val;
87 }
88
90 template <>
91 inline uint8_t byte_swap(uint8_t val) {
92 return val;
93 }
94
96 template <>
97 inline int8_t byte_swap(int8_t val) {
98 return val;
99 }
100
102 template <>
103 inline uint16_t byte_swap(uint16_t val) {
104 return (val << 8 | val >> 8);
105 }
106
108 template <>
109 inline int16_t byte_swap(int16_t val) {
110 return (val << 8 | val >> 8);
111 }
112
114 template <>
115 inline uint32_t byte_swap(uint32_t val) {
116 return __builtin_bswap32(val);
117 }
118
120 template <>
121 inline int32_t byte_swap(int32_t val) {
122 return __builtin_bswap32(val);
123 }
124
126 template <>
127 inline uint64_t byte_swap(uint64_t val) {
128 return __builtin_bswap64(val);
129 }
130
132 template <>
133 inline int64_t byte_swap(int64_t val) {
134 return __builtin_bswap64(val);
135 }
136
137
140 template <typename T, enum ByteOrder BO>
141 inline
142 typename std::enable_if<std::is_same<boost::mpl::int_<BO>, boost::mpl::int_<BE> >::value , T>::type
143 reorder(const T& t) {
144 return byte_swap<T>(t); // byte-reordering required
145 }
146
149 template <typename T, enum ByteOrder BO>
150 inline
151 typename std::enable_if<std::is_same<boost::mpl::int_<BO>, boost::mpl::int_<LE> >::value , T>::type
152 reorder(const T& t) {
153 return t; // No reorder needed on LE host.
154 }
155
156} // namespace sparta
Set of macros for Sparta assertions. Caught by the framework.
Exception class for all of Sparta.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Macros for handling exponential backoff.
std::enable_if< std::is_same< boost::mpl::int_< BO >, boost::mpl::int_< BE > >::value, T >::type reorder(const T &t)
std::enable_if< std::is_integral< T >::value, T >::type byte_swap(T val)
Swaps the order of bytes for various types.
Definition ByteOrder.hpp:83
ByteOrder
Byte order enum for read/write methods.
Definition ByteOrder.hpp:74
@ BE
Little endian.
Definition ByteOrder.hpp:76