mem.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #ifndef MEM_H_MODULE
  11. #define MEM_H_MODULE
  12. #if defined (__cplusplus)
  13. extern "C" {
  14. #endif
  15. /*-****************************************
  16. * Dependencies
  17. ******************************************/
  18. #include <stddef.h> /* size_t, ptrdiff_t */
  19. #include <string.h> /* memcpy */
  20. /*-****************************************
  21. * Compiler specifics
  22. ******************************************/
  23. #if defined(_MSC_VER) /* Visual Studio */
  24. # include <stdlib.h> /* _byteswap_ulong */
  25. # include <intrin.h> /* _byteswap_* */
  26. #endif
  27. #if defined(__GNUC__)
  28. # define MEM_STATIC static __inline __attribute__((unused))
  29. #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  30. # define MEM_STATIC static inline
  31. #elif defined(_MSC_VER)
  32. # define MEM_STATIC static __inline
  33. #else
  34. # define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
  35. #endif
  36. /* code only tested on 32 and 64 bits systems */
  37. #define MEM_STATIC_ASSERT(c) { enum { MEM_static_assert = 1/(int)(!!(c)) }; }
  38. MEM_STATIC void MEM_check(void) { MEM_STATIC_ASSERT((sizeof(size_t)==4) || (sizeof(size_t)==8)); }
  39. /*-**************************************************************
  40. * Basic Types
  41. *****************************************************************/
  42. #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
  43. # include <stdint.h>
  44. typedef uint8_t BYTE;
  45. typedef uint16_t U16;
  46. typedef int16_t S16;
  47. typedef uint32_t U32;
  48. typedef int32_t S32;
  49. typedef uint64_t U64;
  50. typedef int64_t S64;
  51. typedef intptr_t iPtrDiff;
  52. typedef uintptr_t uPtrDiff;
  53. #else
  54. typedef unsigned char BYTE;
  55. typedef unsigned short U16;
  56. typedef signed short S16;
  57. typedef unsigned int U32;
  58. typedef signed int S32;
  59. typedef unsigned long long U64;
  60. typedef signed long long S64;
  61. typedef ptrdiff_t iPtrDiff;
  62. typedef size_t uPtrDiff;
  63. #endif
  64. /*-**************************************************************
  65. * Memory I/O
  66. *****************************************************************/
  67. /* MEM_FORCE_MEMORY_ACCESS :
  68. * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
  69. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
  70. * The below switch allow to select different access method for improved performance.
  71. * Method 0 (default) : use `memcpy()`. Safe and portable.
  72. * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable).
  73. * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
  74. * Method 2 : direct access. This method is portable but violate C standard.
  75. * It can generate buggy code on targets depending on alignment.
  76. * In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6)
  77. * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
  78. * Prefer these methods in priority order (0 > 1 > 2)
  79. */
  80. #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  81. # if defined(__GNUC__) && ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) )
  82. # define MEM_FORCE_MEMORY_ACCESS 2
  83. # elif defined(__INTEL_COMPILER) || defined(__GNUC__)
  84. # define MEM_FORCE_MEMORY_ACCESS 1
  85. # endif
  86. #endif
  87. MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; }
  88. MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
  89. MEM_STATIC unsigned MEM_isLittleEndian(void)
  90. {
  91. const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
  92. return one.c[0];
  93. }
  94. #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
  95. /* violates C standard, by lying on structure alignment.
  96. Only use if no other choice to achieve best performance on target platform */
  97. MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
  98. MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
  99. MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
  100. MEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; }
  101. MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
  102. MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
  103. MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
  104. #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
  105. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  106. /* currently only defined for gcc and icc */
  107. #if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))
  108. __pragma( pack(push, 1) )
  109. typedef union { U16 u16; U32 u32; U64 u64; size_t st; } unalign;
  110. __pragma( pack(pop) )
  111. #else
  112. typedef union { U16 u16; U32 u32; U64 u64; size_t st; } __attribute__((packed)) unalign;
  113. #endif
  114. MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
  115. MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  116. MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
  117. MEM_STATIC size_t MEM_readST(const void* ptr) { return ((const unalign*)ptr)->st; }
  118. MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
  119. MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; }
  120. MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign*)memPtr)->u64 = value; }
  121. #else
  122. /* default method, safe and standard.
  123. can sometimes prove slower */
  124. MEM_STATIC U16 MEM_read16(const void* memPtr)
  125. {
  126. U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
  127. }
  128. MEM_STATIC U32 MEM_read32(const void* memPtr)
  129. {
  130. U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
  131. }
  132. MEM_STATIC U64 MEM_read64(const void* memPtr)
  133. {
  134. U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
  135. }
  136. MEM_STATIC size_t MEM_readST(const void* memPtr)
  137. {
  138. size_t val; memcpy(&val, memPtr, sizeof(val)); return val;
  139. }
  140. MEM_STATIC void MEM_write16(void* memPtr, U16 value)
  141. {
  142. memcpy(memPtr, &value, sizeof(value));
  143. }
  144. MEM_STATIC void MEM_write32(void* memPtr, U32 value)
  145. {
  146. memcpy(memPtr, &value, sizeof(value));
  147. }
  148. MEM_STATIC void MEM_write64(void* memPtr, U64 value)
  149. {
  150. memcpy(memPtr, &value, sizeof(value));
  151. }
  152. #endif /* MEM_FORCE_MEMORY_ACCESS */
  153. MEM_STATIC U32 MEM_swap32(U32 in)
  154. {
  155. #if defined(_MSC_VER) /* Visual Studio */
  156. return _byteswap_ulong(in);
  157. #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
  158. return __builtin_bswap32(in);
  159. #else
  160. return ((in << 24) & 0xff000000 ) |
  161. ((in << 8) & 0x00ff0000 ) |
  162. ((in >> 8) & 0x0000ff00 ) |
  163. ((in >> 24) & 0x000000ff );
  164. #endif
  165. }
  166. MEM_STATIC U64 MEM_swap64(U64 in)
  167. {
  168. #if defined(_MSC_VER) /* Visual Studio */
  169. return _byteswap_uint64(in);
  170. #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
  171. return __builtin_bswap64(in);
  172. #else
  173. return ((in << 56) & 0xff00000000000000ULL) |
  174. ((in << 40) & 0x00ff000000000000ULL) |
  175. ((in << 24) & 0x0000ff0000000000ULL) |
  176. ((in << 8) & 0x000000ff00000000ULL) |
  177. ((in >> 8) & 0x00000000ff000000ULL) |
  178. ((in >> 24) & 0x0000000000ff0000ULL) |
  179. ((in >> 40) & 0x000000000000ff00ULL) |
  180. ((in >> 56) & 0x00000000000000ffULL);
  181. #endif
  182. }
  183. MEM_STATIC size_t MEM_swapST(size_t in)
  184. {
  185. if (MEM_32bits())
  186. return (size_t)MEM_swap32((U32)in);
  187. else
  188. return (size_t)MEM_swap64((U64)in);
  189. }
  190. /*=== Little endian r/w ===*/
  191. MEM_STATIC U16 MEM_readLE16(const void* memPtr)
  192. {
  193. if (MEM_isLittleEndian())
  194. return MEM_read16(memPtr);
  195. else {
  196. const BYTE* p = (const BYTE*)memPtr;
  197. return (U16)(p[0] + (p[1]<<8));
  198. }
  199. }
  200. MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
  201. {
  202. if (MEM_isLittleEndian()) {
  203. MEM_write16(memPtr, val);
  204. } else {
  205. BYTE* p = (BYTE*)memPtr;
  206. p[0] = (BYTE)val;
  207. p[1] = (BYTE)(val>>8);
  208. }
  209. }
  210. MEM_STATIC U32 MEM_readLE24(const void* memPtr)
  211. {
  212. return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
  213. }
  214. MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)
  215. {
  216. MEM_writeLE16(memPtr, (U16)val);
  217. ((BYTE*)memPtr)[2] = (BYTE)(val>>16);
  218. }
  219. MEM_STATIC U32 MEM_readLE32(const void* memPtr)
  220. {
  221. if (MEM_isLittleEndian())
  222. return MEM_read32(memPtr);
  223. else
  224. return MEM_swap32(MEM_read32(memPtr));
  225. }
  226. MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
  227. {
  228. if (MEM_isLittleEndian())
  229. MEM_write32(memPtr, val32);
  230. else
  231. MEM_write32(memPtr, MEM_swap32(val32));
  232. }
  233. MEM_STATIC U64 MEM_readLE64(const void* memPtr)
  234. {
  235. if (MEM_isLittleEndian())
  236. return MEM_read64(memPtr);
  237. else
  238. return MEM_swap64(MEM_read64(memPtr));
  239. }
  240. MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
  241. {
  242. if (MEM_isLittleEndian())
  243. MEM_write64(memPtr, val64);
  244. else
  245. MEM_write64(memPtr, MEM_swap64(val64));
  246. }
  247. MEM_STATIC size_t MEM_readLEST(const void* memPtr)
  248. {
  249. if (MEM_32bits())
  250. return (size_t)MEM_readLE32(memPtr);
  251. else
  252. return (size_t)MEM_readLE64(memPtr);
  253. }
  254. MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
  255. {
  256. if (MEM_32bits())
  257. MEM_writeLE32(memPtr, (U32)val);
  258. else
  259. MEM_writeLE64(memPtr, (U64)val);
  260. }
  261. /*=== Big endian r/w ===*/
  262. MEM_STATIC U32 MEM_readBE32(const void* memPtr)
  263. {
  264. if (MEM_isLittleEndian())
  265. return MEM_swap32(MEM_read32(memPtr));
  266. else
  267. return MEM_read32(memPtr);
  268. }
  269. MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32)
  270. {
  271. if (MEM_isLittleEndian())
  272. MEM_write32(memPtr, MEM_swap32(val32));
  273. else
  274. MEM_write32(memPtr, val32);
  275. }
  276. MEM_STATIC U64 MEM_readBE64(const void* memPtr)
  277. {
  278. if (MEM_isLittleEndian())
  279. return MEM_swap64(MEM_read64(memPtr));
  280. else
  281. return MEM_read64(memPtr);
  282. }
  283. MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64)
  284. {
  285. if (MEM_isLittleEndian())
  286. MEM_write64(memPtr, MEM_swap64(val64));
  287. else
  288. MEM_write64(memPtr, val64);
  289. }
  290. MEM_STATIC size_t MEM_readBEST(const void* memPtr)
  291. {
  292. if (MEM_32bits())
  293. return (size_t)MEM_readBE32(memPtr);
  294. else
  295. return (size_t)MEM_readBE64(memPtr);
  296. }
  297. MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val)
  298. {
  299. if (MEM_32bits())
  300. MEM_writeBE32(memPtr, (U32)val);
  301. else
  302. MEM_writeBE64(memPtr, (U64)val);
  303. }
  304. #if defined (__cplusplus)
  305. }
  306. #endif
  307. #endif /* MEM_H_MODULE */