framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
word_swap.hpp
Go to the documentation of this file.
1 // Copyright (C) 2012 iwg molw5
2 // For conditions of distribution and use, see copyright notice in COPYING
3 
13 #pragma once
14 
15 namespace framework
16 {
20  template <typename T>
22  T wswap32 (T&& in)
23  {
24  static_assert(sizeof(T) == 4, "32-bit type expected");
25  uint32_t const x = static_cast <uint32_t> (in);
26  return static_cast <T> ((x >> 16) | (x << 16));
27  }
28 
32  template <typename T>
34  T wswap64 (T&& in)
35  {
36  static_assert(sizeof(T) == 8, "64-bit type expected");
37  uint64_t x = static_cast <uint64_t> (in);
38  x = ((x & 0xFFFF0000FFFF0000) >> 16) | ((x & 0x0000FFFF0000FFFF) << 16);
39  return static_cast <T> ((x >> 32) | (x << 32));
40  }
41 }
42 
43 #define FRAMEWORK_WORDSWAP8(x) x
44 #define FRAMEWORK_WORDSWAP16(x) x
45 #define FRAMEWORK_WORDSWAP32(x) ::framework::wswap32(x)
46 #define FRAMEWORK_WORDSWAP64(x) ::framework::wswap64(x)