sysdep.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * MessagePack system dependencies modified for erlpack.
  3. *
  4. * Copyright (C) 2008-2010 FURUHASHI Sadayuki
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef ERLPACK_SYSDEP_H__
  19. #define ERLPACK_SYSDEP_H__
  20. #include <stdlib.h>
  21. #include <stddef.h>
  22. #if defined(_MSC_VER) && _MSC_VER < 1600
  23. typedef __int8 int8_t;
  24. typedef unsigned __int8 uint8_t;
  25. typedef __int16 int16_t;
  26. typedef unsigned __int16 uint16_t;
  27. typedef __int32 int32_t;
  28. typedef unsigned __int32 uint32_t;
  29. typedef __int64 int64_t;
  30. typedef unsigned __int64 uint64_t;
  31. #elif defined(_MSC_VER) // && _MSC_VER >= 1600
  32. #include <stdint.h>
  33. #else
  34. #include <stdint.h>
  35. #include <stdbool.h>
  36. #endif
  37. #if defined(__linux__)
  38. #include <endian.h>
  39. #endif
  40. #ifdef _WIN32
  41. #ifdef __cplusplus
  42. /* numeric_limits<T>::min,max */
  43. #ifdef max
  44. #undef max
  45. #endif
  46. #ifdef min
  47. #undef min
  48. #endif
  49. #endif
  50. #else
  51. #include <arpa/inet.h> /* __BYTE_ORDER */
  52. #endif
  53. #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
  54. #if __BYTE_ORDER == __LITTLE_ENDIAN
  55. #define __LITTLE_ENDIAN__
  56. #elif __BYTE_ORDER == __BIG_ENDIAN
  57. #define __BIG_ENDIAN__
  58. #elif _WIN32
  59. #define __LITTLE_ENDIAN__
  60. #endif
  61. #endif
  62. #ifdef __LITTLE_ENDIAN__
  63. #ifdef _WIN32
  64. # if defined(ntohs)
  65. # define _erlpack_be16(x) ntohs(x)
  66. # elif defined(_byteswap_ushort) || (defined(_MSC_VER) && _MSC_VER >= 1400)
  67. # define _erlpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x))
  68. # else
  69. # define _erlpack_be16(x) ( \
  70. ((((uint16_t)x) << 8) ) | \
  71. ((((uint16_t)x) >> 8) ) )
  72. # endif
  73. #else
  74. # define _erlpack_be16(x) ntohs(x)
  75. #endif
  76. #ifdef _WIN32
  77. # if defined(ntohl)
  78. # define _erlpack_be32(x) ntohl(x)
  79. # elif defined(_byteswap_ulong) || (defined(_MSC_VER) && _MSC_VER >= 1400)
  80. # define _erlpack_be32(x) ((uint32_t)_byteswap_ulong((unsigned long)x))
  81. # else
  82. # define _erlpack_be32(x) \
  83. ( ((((uint32_t)x) << 24) ) | \
  84. ((((uint32_t)x) << 8) & 0x00ff0000U ) | \
  85. ((((uint32_t)x) >> 8) & 0x0000ff00U ) | \
  86. ((((uint32_t)x) >> 24) ) )
  87. # endif
  88. #else
  89. # define _erlpack_be32(x) ntohl(x)
  90. #endif
  91. #if defined(_byteswap_uint64) || (defined(_MSC_VER) && _MSC_VER >= 1400)
  92. # define _erlpack_be64(x) (_byteswap_uint64(x))
  93. #elif defined(bswap_64)
  94. # define _erlpack_be64(x) bswap_64(x)
  95. #elif defined(__DARWIN_OSSwapInt64)
  96. # define _erlpack_be64(x) __DARWIN_OSSwapInt64(x)
  97. #elif defined(__linux__)
  98. # define _erlpack_be64(x) be64toh(x)
  99. #else
  100. # define _erlpack_be64(x) \
  101. ( ((((uint64_t)x) << 56) ) | \
  102. ((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \
  103. ((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \
  104. ((((uint64_t)x) << 8) & 0x000000ff00000000ULL ) | \
  105. ((((uint64_t)x) >> 8) & 0x00000000ff000000ULL ) | \
  106. ((((uint64_t)x) >> 24) & 0x0000000000ff0000ULL ) | \
  107. ((((uint64_t)x) >> 40) & 0x000000000000ff00ULL ) | \
  108. ((((uint64_t)x) >> 56) ) )
  109. #endif
  110. #else
  111. #define _erlpack_be16(x) (x)
  112. #define _erlpack_be32(x) (x)
  113. #define _erlpack_be64(x) (x)
  114. #endif
  115. #define _erlpack_store16(to, num) \
  116. do { uint16_t val = _erlpack_be16(num); memcpy(to, &val, 2); } while(0)
  117. #define _erlpack_store32(to, num) \
  118. do { uint32_t val = _erlpack_be32(num); memcpy(to, &val, 4); } while(0)
  119. #define _erlpack_store64(to, num) \
  120. do { uint64_t val = _erlpack_be64(num); memcpy(to, &val, 8); } while(0)
  121. #endif /* sysdep.h */