mem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #else
  52. # include <limits.h>
  53. #if CHAR_BIT != 8
  54. # error "this implementation requires char to be exactly 8-bit type"
  55. #endif
  56. typedef unsigned char BYTE;
  57. #if USHRT_MAX != 65535
  58. # error "this implementation requires short to be exactly 16-bit type"
  59. #endif
  60. typedef unsigned short U16;
  61. typedef signed short S16;
  62. #if UINT_MAX != 4294967295
  63. # error "this implementation requires int to be exactly 32-bit type"
  64. #endif
  65. typedef unsigned int U32;
  66. typedef signed int S32;
  67. /* note : there are no limits defined for long long type in C90.
  68. * limits exist in C99, however, in such case, <stdint.h> is preferred */
  69. typedef unsigned long long U64;
  70. typedef signed long long S64;
  71. #endif
  72. /*-**************************************************************
  73. * Memory I/O
  74. *****************************************************************/
  75. /* MEM_FORCE_MEMORY_ACCESS :
  76. * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable.
  77. * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal.
  78. * The below switch allow to select different access method for improved performance.
  79. * Method 0 (default) : use `memcpy()`. Safe and portable.
  80. * Method 1 : `__packed` statement. It depends on compiler extension (i.e., not portable).
  81. * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`.
  82. * Method 2 : direct access. This method is portable but violate C standard.
  83. * It can generate buggy code on targets depending on alignment.
  84. * In some circumstances, it's the only known way to get the most performance (i.e. GCC + ARMv6)
  85. * See http://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details.
  86. * Prefer these methods in priority order (0 > 1 > 2)
  87. */
  88. #ifndef MEM_FORCE_MEMORY_ACCESS /* can be defined externally, on command line for example */
  89. # 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__) )
  90. # define MEM_FORCE_MEMORY_ACCESS 2
  91. # elif defined(__INTEL_COMPILER) || defined(__GNUC__)
  92. # define MEM_FORCE_MEMORY_ACCESS 1
  93. # endif
  94. #endif
  95. MEM_STATIC unsigned MEM_32bits(void) { return sizeof(size_t)==4; }
  96. MEM_STATIC unsigned MEM_64bits(void) { return sizeof(size_t)==8; }
  97. MEM_STATIC unsigned MEM_isLittleEndian(void)
  98. {
  99. const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
  100. return one.c[0];
  101. }
  102. #if defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==2)
  103. /* violates C standard, by lying on structure alignment.
  104. Only use if no other choice to achieve best performance on target platform */
  105. MEM_STATIC U16 MEM_read16(const void* memPtr) { return *(const U16*) memPtr; }
  106. MEM_STATIC U32 MEM_read32(const void* memPtr) { return *(const U32*) memPtr; }
  107. MEM_STATIC U64 MEM_read64(const void* memPtr) { return *(const U64*) memPtr; }
  108. MEM_STATIC size_t MEM_readST(const void* memPtr) { return *(const size_t*) memPtr; }
  109. MEM_STATIC void MEM_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
  110. MEM_STATIC void MEM_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
  111. MEM_STATIC void MEM_write64(void* memPtr, U64 value) { *(U64*)memPtr = value; }
  112. #elif defined(MEM_FORCE_MEMORY_ACCESS) && (MEM_FORCE_MEMORY_ACCESS==1)
  113. /* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */
  114. /* currently only defined for gcc and icc */
  115. #if defined(_MSC_VER) || (defined(__INTEL_COMPILER) && defined(WIN32))
  116. __pragma( pack(push, 1) )
  117. typedef struct { U16 v; } unalign16;
  118. typedef struct { U32 v; } unalign32;
  119. typedef struct { U64 v; } unalign64;
  120. typedef struct { size_t v; } unalignArch;
  121. __pragma( pack(pop) )
  122. #else
  123. typedef struct { U16 v; } __attribute__((packed)) unalign16;
  124. typedef struct { U32 v; } __attribute__((packed)) unalign32;
  125. typedef struct { U64 v; } __attribute__((packed)) unalign64;
  126. typedef struct { size_t v; } __attribute__((packed)) unalignArch;
  127. #endif
  128. MEM_STATIC U16 MEM_read16(const void* ptr) { return ((const unalign16*)ptr)->v; }
  129. MEM_STATIC U32 MEM_read32(const void* ptr) { return ((const unalign32*)ptr)->v; }
  130. MEM_STATIC U64 MEM_read64(const void* ptr) { return ((const unalign64*)ptr)->v; }
  131. MEM_STATIC size_t MEM_readST(const void* ptr) { return ((const unalignArch*)ptr)->v; }
  132. MEM_STATIC void MEM_write16(void* memPtr, U16 value) { ((unalign16*)memPtr)->v = value; }
  133. MEM_STATIC void MEM_write32(void* memPtr, U32 value) { ((unalign32*)memPtr)->v = value; }
  134. MEM_STATIC void MEM_write64(void* memPtr, U64 value) { ((unalign64*)memPtr)->v = value; }
  135. #else
  136. /* default method, safe and standard.
  137. can sometimes prove slower */
  138. MEM_STATIC U16 MEM_read16(const void* memPtr)
  139. {
  140. U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
  141. }
  142. MEM_STATIC U32 MEM_read32(const void* memPtr)
  143. {
  144. U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
  145. }
  146. MEM_STATIC U64 MEM_read64(const void* memPtr)
  147. {
  148. U64 val; memcpy(&val, memPtr, sizeof(val)); return val;
  149. }
  150. MEM_STATIC size_t MEM_readST(const void* memPtr)
  151. {
  152. size_t val; memcpy(&val, memPtr, sizeof(val)); return val;
  153. }
  154. MEM_STATIC void MEM_write16(void* memPtr, U16 value)
  155. {
  156. memcpy(memPtr, &value, sizeof(value));
  157. }
  158. MEM_STATIC void MEM_write32(void* memPtr, U32 value)
  159. {
  160. memcpy(memPtr, &value, sizeof(value));
  161. }
  162. MEM_STATIC void MEM_write64(void* memPtr, U64 value)
  163. {
  164. memcpy(memPtr, &value, sizeof(value));
  165. }
  166. #endif /* MEM_FORCE_MEMORY_ACCESS */
  167. MEM_STATIC U32 MEM_swap32(U32 in)
  168. {
  169. #if defined(_MSC_VER) /* Visual Studio */
  170. return _byteswap_ulong(in);
  171. #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
  172. return __builtin_bswap32(in);
  173. #else
  174. return ((in << 24) & 0xff000000 ) |
  175. ((in << 8) & 0x00ff0000 ) |
  176. ((in >> 8) & 0x0000ff00 ) |
  177. ((in >> 24) & 0x000000ff );
  178. #endif
  179. }
  180. MEM_STATIC U64 MEM_swap64(U64 in)
  181. {
  182. #if defined(_MSC_VER) /* Visual Studio */
  183. return _byteswap_uint64(in);
  184. #elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)
  185. return __builtin_bswap64(in);
  186. #else
  187. return ((in << 56) & 0xff00000000000000ULL) |
  188. ((in << 40) & 0x00ff000000000000ULL) |
  189. ((in << 24) & 0x0000ff0000000000ULL) |
  190. ((in << 8) & 0x000000ff00000000ULL) |
  191. ((in >> 8) & 0x00000000ff000000ULL) |
  192. ((in >> 24) & 0x0000000000ff0000ULL) |
  193. ((in >> 40) & 0x000000000000ff00ULL) |
  194. ((in >> 56) & 0x00000000000000ffULL);
  195. #endif
  196. }
  197. MEM_STATIC size_t MEM_swapST(size_t in)
  198. {
  199. if (MEM_32bits())
  200. return (size_t)MEM_swap32((U32)in);
  201. else
  202. return (size_t)MEM_swap64((U64)in);
  203. }
  204. /*=== Little endian r/w ===*/
  205. MEM_STATIC U16 MEM_readLE16(const void* memPtr)
  206. {
  207. if (MEM_isLittleEndian())
  208. return MEM_read16(memPtr);
  209. else {
  210. const BYTE* p = (const BYTE*)memPtr;
  211. return (U16)(p[0] + (p[1]<<8));
  212. }
  213. }
  214. MEM_STATIC void MEM_writeLE16(void* memPtr, U16 val)
  215. {
  216. if (MEM_isLittleEndian()) {
  217. MEM_write16(memPtr, val);
  218. } else {
  219. BYTE* p = (BYTE*)memPtr;
  220. p[0] = (BYTE)val;
  221. p[1] = (BYTE)(val>>8);
  222. }
  223. }
  224. MEM_STATIC U32 MEM_readLE24(const void* memPtr)
  225. {
  226. return MEM_readLE16(memPtr) + (((const BYTE*)memPtr)[2] << 16);
  227. }
  228. MEM_STATIC void MEM_writeLE24(void* memPtr, U32 val)
  229. {
  230. MEM_writeLE16(memPtr, (U16)val);
  231. ((BYTE*)memPtr)[2] = (BYTE)(val>>16);
  232. }
  233. MEM_STATIC U32 MEM_readLE32(const void* memPtr)
  234. {
  235. if (MEM_isLittleEndian())
  236. return MEM_read32(memPtr);
  237. else
  238. return MEM_swap32(MEM_read32(memPtr));
  239. }
  240. MEM_STATIC void MEM_writeLE32(void* memPtr, U32 val32)
  241. {
  242. if (MEM_isLittleEndian())
  243. MEM_write32(memPtr, val32);
  244. else
  245. MEM_write32(memPtr, MEM_swap32(val32));
  246. }
  247. MEM_STATIC U64 MEM_readLE64(const void* memPtr)
  248. {
  249. if (MEM_isLittleEndian())
  250. return MEM_read64(memPtr);
  251. else
  252. return MEM_swap64(MEM_read64(memPtr));
  253. }
  254. MEM_STATIC void MEM_writeLE64(void* memPtr, U64 val64)
  255. {
  256. if (MEM_isLittleEndian())
  257. MEM_write64(memPtr, val64);
  258. else
  259. MEM_write64(memPtr, MEM_swap64(val64));
  260. }
  261. MEM_STATIC size_t MEM_readLEST(const void* memPtr)
  262. {
  263. if (MEM_32bits())
  264. return (size_t)MEM_readLE32(memPtr);
  265. else
  266. return (size_t)MEM_readLE64(memPtr);
  267. }
  268. MEM_STATIC void MEM_writeLEST(void* memPtr, size_t val)
  269. {
  270. if (MEM_32bits())
  271. MEM_writeLE32(memPtr, (U32)val);
  272. else
  273. MEM_writeLE64(memPtr, (U64)val);
  274. }
  275. /*=== Big endian r/w ===*/
  276. MEM_STATIC U32 MEM_readBE32(const void* memPtr)
  277. {
  278. if (MEM_isLittleEndian())
  279. return MEM_swap32(MEM_read32(memPtr));
  280. else
  281. return MEM_read32(memPtr);
  282. }
  283. MEM_STATIC void MEM_writeBE32(void* memPtr, U32 val32)
  284. {
  285. if (MEM_isLittleEndian())
  286. MEM_write32(memPtr, MEM_swap32(val32));
  287. else
  288. MEM_write32(memPtr, val32);
  289. }
  290. MEM_STATIC U64 MEM_readBE64(const void* memPtr)
  291. {
  292. if (MEM_isLittleEndian())
  293. return MEM_swap64(MEM_read64(memPtr));
  294. else
  295. return MEM_read64(memPtr);
  296. }
  297. MEM_STATIC void MEM_writeBE64(void* memPtr, U64 val64)
  298. {
  299. if (MEM_isLittleEndian())
  300. MEM_write64(memPtr, MEM_swap64(val64));
  301. else
  302. MEM_write64(memPtr, val64);
  303. }
  304. MEM_STATIC size_t MEM_readBEST(const void* memPtr)
  305. {
  306. if (MEM_32bits())
  307. return (size_t)MEM_readBE32(memPtr);
  308. else
  309. return (size_t)MEM_readBE64(memPtr);
  310. }
  311. MEM_STATIC void MEM_writeBEST(void* memPtr, size_t val)
  312. {
  313. if (MEM_32bits())
  314. MEM_writeBE32(memPtr, (U32)val);
  315. else
  316. MEM_writeBE64(memPtr, (U64)val);
  317. }
  318. #if defined (__cplusplus)
  319. }
  320. #endif
  321. #endif /* MEM_H_MODULE */