Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.5 KiB

  1. //===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares generic and optimized functions to swap the byte order of
  11. // an integral type.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_SUPPORT_SWAPBYTEORDER_H
  15. #define LLVM_SUPPORT_SWAPBYTEORDER_H
  16. #include "llvm/Support/DataTypes.h"
  17. #include <cstddef>
  18. #include <limits>
  19. namespace llvm {
  20. namespace sys {
  21. /// SwapByteOrder_16 - This function returns a byte-swapped representation of
  22. /// the 16-bit argument.
  23. inline uint16_t SwapByteOrder_16(uint16_t value) {
  24. #if defined(_MSC_VER) && !defined(_DEBUG)
  25. // The DLL version of the runtime lacks these functions (bug!?), but in a
  26. // release build they're replaced with BSWAP instructions anyway.
  27. return _byteswap_ushort(value);
  28. #else
  29. uint16_t Hi = value << 8;
  30. uint16_t Lo = value >> 8;
  31. return Hi | Lo;
  32. #endif
  33. }
  34. /// SwapByteOrder_32 - This function returns a byte-swapped representation of
  35. /// the 32-bit argument.
  36. inline uint32_t SwapByteOrder_32(uint32_t value) {
  37. #if defined(__llvm__) || \
  38. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC)
  39. return __builtin_bswap32(value);
  40. #elif defined(_MSC_VER) && !defined(_DEBUG)
  41. return _byteswap_ulong(value);
  42. #else
  43. uint32_t Byte0 = value & 0x000000FF;
  44. uint32_t Byte1 = value & 0x0000FF00;
  45. uint32_t Byte2 = value & 0x00FF0000;
  46. uint32_t Byte3 = value & 0xFF000000;
  47. return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
  48. #endif
  49. }
  50. /// SwapByteOrder_64 - This function returns a byte-swapped representation of
  51. /// the 64-bit argument.
  52. inline uint64_t SwapByteOrder_64(uint64_t value) {
  53. #if defined(__llvm__) || \
  54. (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC)
  55. return __builtin_bswap64(value);
  56. #elif defined(_MSC_VER) && !defined(_DEBUG)
  57. return _byteswap_uint64(value);
  58. #else
  59. uint64_t Hi = SwapByteOrder_32(uint32_t(value));
  60. uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
  61. return (Hi << 32) | Lo;
  62. #endif
  63. }
  64. inline unsigned char SwapByteOrder(unsigned char C) { return C; }
  65. inline signed char SwapByteOrder(signed char C) { return C; }
  66. inline char SwapByteOrder(char C) { return C; }
  67. inline unsigned short SwapByteOrder(unsigned short C) { return SwapByteOrder_16(C); }
  68. inline signed short SwapByteOrder( signed short C) { return SwapByteOrder_16(C); }
  69. inline unsigned int SwapByteOrder(unsigned int C) { return SwapByteOrder_32(C); }
  70. inline signed int SwapByteOrder( signed int C) { return SwapByteOrder_32(C); }
  71. #if __LONG_MAX__ == __INT_MAX__
  72. inline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_32(C); }
  73. inline signed long SwapByteOrder( signed long C) { return SwapByteOrder_32(C); }
  74. #elif __LONG_MAX__ == __LONG_LONG_MAX__
  75. inline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_64(C); }
  76. inline signed long SwapByteOrder( signed long C) { return SwapByteOrder_64(C); }
  77. #else
  78. #error "Unknown long size!"
  79. #endif
  80. inline unsigned long long SwapByteOrder(unsigned long long C) {
  81. return SwapByteOrder_64(C);
  82. }
  83. inline signed long long SwapByteOrder(signed long long C) {
  84. return SwapByteOrder_64(C);
  85. }
  86. } // end namespace sys
  87. } // end namespace llvm
  88. #endif