27 #ifndef INCLUDED_IntegerByteSwap_h_GUID_11841216_B020_47A7_D0A9_9B1CFFEB1C5C
28 #define INCLUDED_IntegerByteSwap_h_GUID_11841216_B020_47A7_D0A9_9B1CFFEB1C5C
35 #include <boost/integer.hpp>
38 #include <type_traits>
40 #if defined(OSVR_HAVE_INTRIN_H) && defined(OSVR_HAVE_WORKING_MS_BYTESWAPS)
44 #ifdef OSVR_HAVE_BYTESWAP_H
62 using IsByteSwapperUnspecialized = std::integral_constant<
63 bool, std::is_base_of<UnspecializedByteSwapBase, T>::value>;
68 using IsByteSwapperSpecialized =
69 std::integral_constant<bool, !IsByteSwapperUnspecialized<T>::value>;
72 template <
typename ArgType>
static ArgType apply(ArgType
const v) {
82 #if defined(OSVR_HAVE_WORKING_MS_BYTESWAPS)
83 template <>
struct ByteSwap<uint16_t> {
84 static uint16_t
apply(uint16_t
const v) {
85 return _byteswap_ushort(v);
89 template <>
struct ByteSwap<uint32_t> {
90 static uint32_t
apply(uint32_t
const v) {
91 return _byteswap_ulong(v);
94 template <>
struct ByteSwap<uint64_t> {
95 static uint64_t
apply(uint64_t
const v) {
96 return _byteswap_uint64(v);
99 #elif defined(OSVR_HAVE_BYTESWAP_H) && defined(OSVR_HAVE_WORKING_BSWAP)
100 template <>
struct ByteSwap<uint16_t> {
101 static uint16_t
apply(uint16_t
const v) {
return bswap16(v); }
103 template <>
struct ByteSwap<uint32_t> {
104 static uint32_t
apply(uint32_t
const v) {
return bswap32(v); }
106 template <>
struct ByteSwap<uint64_t> {
107 static uint64_t
apply(uint64_t
const v) {
return bswap64(v); }
109 #elif defined(OSVR_HAVE_BYTESWAP_H) && \
110 defined(OSVR_HAVE_WORKING_BSWAP_UNDERSCORE)
111 template <>
struct ByteSwap<uint16_t> {
112 static uint16_t
apply(uint16_t
const v) {
return bswap_16(v); }
115 template <>
struct ByteSwap<uint32_t> {
116 static uint32_t
apply(uint32_t
const v) {
return bswap_32(v); }
118 template <>
struct ByteSwap<uint64_t> {
119 static uint64_t
apply(uint64_t
const v) {
return bswap_64(v); }
121 #elif defined(OSVR_HAVE_BYTESWAP_H) && \
122 defined(OSVR_HAVE_WORKING_UNDERSCORES_BSWAP)
123 template <>
struct ByteSwap<uint16_t> {
124 static uint16_t
apply(uint16_t
const v) {
return __bswap_16(v); }
127 template <>
struct ByteSwap<uint32_t> {
128 static uint32_t
apply(uint32_t
const v) {
return __bswap_32(v); }
130 template <>
struct ByteSwap<uint64_t> {
131 static uint64_t
apply(uint64_t
const v) {
return __bswap_64(v); }
134 template <
typename T>
inline T genericByteSwap(T
const v) {
138 for (
size_t i = 0; i <
sizeof(T); ++i) {
139 reinterpret_cast<char *
>(&ret)[i] =
140 reinterpret_cast<char const *>(&v)[
sizeof(T) - 1 - i];
151 typedef typename boost::int_t<sizeof(T) * 8>::exact int_t;
152 typedef typename boost::uint_t<sizeof(T) * 8>::exact uint_t;
154 typename std::conditional<std::is_signed<T>::value, uint_t,
155 int_t>::type opposite_signedness_type;
156 typedef typename std::add_const<opposite_signedness_type>::type
157 const_opposite_signedness_type;
162 static const bool HAVE_BYTESWAPPER =
163 IsByteSwapperSpecialized<ByteSwapper>::value;
164 static const bool HAVE_OPPPOSITEBYTESWAPPER =
165 IsByteSwapperSpecialized<OppositeByteSwapper>::value;
170 template <
typename T>
171 inline T integerByteSwapImpl(
173 typename std::enable_if<
182 template <
typename T>
183 inline T integerByteSwapImpl(
185 typename std::enable_if<
186 (!IntegerByteSwapTraits<T>::HAVE_BYTESWAPPER) &&
187 (IntegerByteSwapTraits<T>::HAVE_OPPPOSITEBYTESWAPPER)>::type * =
189 typedef IntegerByteSwapTraits<T> Traits;
191 static_cast<typename Traits::const_opposite_signedness_type>(
198 template <
typename T>
199 inline T integerByteSwapImpl(
201 typename std::enable_if<
202 (!IntegerByteSwapTraits<T>::HAVE_BYTESWAPPER) &&
203 (!IntegerByteSwapTraits<T>::HAVE_OPPPOSITEBYTESWAPPER)>::
205 return genericByteSwap(v);
216 static_assert(std::is_integral<Type>::value,
217 "Can only apply integerByteSwap to integers");
218 typedef typename std::remove_cv<
219 typename std::remove_reference<Type>::type>::type T;
221 return detail::integerByteSwapImpl<T>(v);
226 #endif // INCLUDED_IntegerByteSwap_h_GUID_11841216_B020_47A7_D0A9_9B1CFFEB1C5C
Automatically-generated configuration header - do not edit!
Template for providing/describing optimized/intrinsic byte swap functions.
Header wrapping the C99 standard stdint header.
typename F::template apply< Args...> apply
Apply an alias class.
Tag type used only for recognizing ByteSwap specializations that aren't explicitly specialized...
Traits assisting selection of optimized integer byte swapping.
Type integerByteSwap(Type const v)
Swap the order of bytes of an arbitrary integer type.