PageRenderTime 76ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/mordor/endian.h

http://github.com/mozy/mordor
C Header | 173 lines | 141 code | 24 blank | 8 comment | 16 complexity | 8f0e16832205d276ba005570f9a48cc6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_ENDIAN_H__
  2. #define __MORDOR_ENDIAN_H__
  3. // Copyright (c) 2009 - Mozy, Inc.
  4. #include <boost/utility/enable_if.hpp>
  5. #include "version.h"
  6. #define MORDOR_LITTLE_ENDIAN 1
  7. #define MORDOR_BIG_ENDIAN 2
  8. #ifdef WINDOWS
  9. #include <stdlib.h>
  10. #elif defined(OSX)
  11. #include <libkern/OSByteOrder.h>
  12. #include <stdint.h>
  13. #include <sys/_endian.h>
  14. #elif defined(FREEBSD)
  15. #include <stdint.h>
  16. #include <sys/endian.h>
  17. #else
  18. #include <byteswap.h>
  19. #include <stdint.h>
  20. #endif
  21. namespace Mordor {
  22. #ifdef WINDOWS
  23. template <class T>
  24. typename boost::enable_if_c<sizeof(T) == sizeof(unsigned __int64), T>::type
  25. byteswap(T value)
  26. {
  27. return (T)_byteswap_uint64((unsigned __int64)value);
  28. }
  29. template <class T>
  30. typename boost::enable_if_c<sizeof(T) == sizeof(unsigned long), T>::type
  31. byteswap(T value)
  32. {
  33. return (T)_byteswap_ulong((unsigned long)value);
  34. }
  35. template <class T>
  36. typename boost::enable_if_c<sizeof(T) == sizeof(unsigned short), T>::type
  37. byteswap(T value)
  38. {
  39. return (T)_byteswap_ushort((unsigned short)value);
  40. }
  41. #define MORDOR_BYTE_ORDER MORDOR_LITTLE_ENDIAN
  42. #elif defined(OSX)
  43. template <class T>
  44. typename boost::enable_if_c<sizeof(T) == sizeof(uint64_t), T>::type
  45. byteswap(T value)
  46. {
  47. return (T)_OSSwapInt64((uint64_t)value);
  48. }
  49. template <class T>
  50. typename boost::enable_if_c<sizeof(T) == sizeof(uint32_t), T>::type
  51. byteswap(T value)
  52. {
  53. return (T)_OSSwapInt32((uint32_t)value);
  54. }
  55. template <class T>
  56. typename boost::enable_if_c<sizeof(T) == sizeof(uint16_t), T>::type
  57. byteswap(T value)
  58. {
  59. return (T)_OSSwapInt16((uint16_t)value);
  60. }
  61. #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
  62. #error Do not know the endianess of this architecture
  63. #endif
  64. #ifdef __BIG_ENDIAN__
  65. #define MORDOR_BYTE_ORDER MORDOR_BIG_ENDIAN
  66. #else
  67. #define MORDOR_BYTE_ORDER MORDOR_LITTLE_ENDIAN
  68. #endif
  69. #elif defined(FREEBSD)
  70. template <class T>
  71. typename boost::enable_if_c<sizeof(T) == sizeof(uint64_t), T>::type
  72. byteswap(T value)
  73. {
  74. return (T)bswap64((uint64_t)value);
  75. }
  76. template <class T>
  77. typename boost::enable_if_c<sizeof(T) == sizeof(uint32_t), T>::type
  78. byteswap(T value)
  79. {
  80. return (T)bswap32((uint32_t)value);
  81. }
  82. template <class T>
  83. typename boost::enable_if_c<sizeof(T) == sizeof(uint16_t), T>::type
  84. byteswap(T value)
  85. {
  86. return (T)bswap16((uint16_t)value);
  87. }
  88. #if _BYTE_ORDER == _BIG_ENDIAN
  89. #define MORDOR_BYTE_ORDER MORDOR_BIG_ENDIAN
  90. #else
  91. #define MORDOR_BYTE_ORDER MORDOR_LITTLE_ENDIAN
  92. #endif
  93. #else
  94. template <class T>
  95. typename boost::enable_if_c<sizeof(T) == sizeof(uint64_t), T>::type
  96. byteswap(T value)
  97. {
  98. return (T)bswap_64((uint64_t)value);
  99. }
  100. template <class T>
  101. typename boost::enable_if_c<sizeof(T) == sizeof(uint32_t), T>::type
  102. byteswap(T value)
  103. {
  104. return (T)bswap_32((uint32_t)value);
  105. }
  106. template <class T>
  107. typename boost::enable_if_c<sizeof(T) == sizeof(uint16_t), T>::type
  108. byteswap(T value)
  109. {
  110. return (T)bswap_16((uint16_t)value);
  111. }
  112. #if BYTE_ORDER == BIG_ENDIAN
  113. #define MORDOR_BYTE_ORDER MORDOR_BIG_ENDIAN
  114. #else
  115. #define MORDOR_BYTE_ORDER MORDOR_LITTLE_ENDIAN
  116. #endif
  117. #endif
  118. #if MORDOR_BYTE_ORDER == MORDOR_BIG_ENDIAN
  119. template <class T>
  120. T byteswapOnLittleEndian(T t)
  121. {
  122. return t;
  123. }
  124. template <class T>
  125. T byteswapOnBigEndian(T t)
  126. {
  127. return byteswap(t);
  128. }
  129. #else
  130. /// byteswap only when running on a little endian platform
  131. ///
  132. /// On big endian platforms, it's a no op. This is the equivalent of
  133. /// htonX/ntohX
  134. template <class T>
  135. T byteswapOnLittleEndian(T t)
  136. {
  137. return byteswap(t);
  138. }
  139. /// byteswap only when running on a big endian platform
  140. ///
  141. /// On little endian platforms, it's a no op.
  142. template <class T>
  143. T byteswapOnBigEndian(T t)
  144. {
  145. return t;
  146. }
  147. #endif
  148. }
  149. #endif