platform.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Macros for compiler / platform specific features and build options.
  6. Build options are:
  7. * BROTLI_BUILD_32_BIT disables 64-bit optimizations
  8. * BROTLI_BUILD_64_BIT forces to use 64-bit optimizations
  9. * BROTLI_BUILD_BIG_ENDIAN forces to use big-endian optimizations
  10. * BROTLI_BUILD_ENDIAN_NEUTRAL disables endian-aware optimizations
  11. * BROTLI_BUILD_LITTLE_ENDIAN forces to use little-endian optimizations
  12. * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned
  13. read and overlapping memcpy; this reduces decompression speed by 5%
  14. * BROTLI_BUILD_NO_RBIT disables "rbit" optimization for ARM CPUs
  15. * BROTLI_DEBUG dumps file name and line number when decoder detects stream
  16. or memory error
  17. * BROTLI_ENABLE_LOG enables asserts and dumps various state information
  18. */
  19. #ifndef BROTLI_COMMON_PLATFORM_H_
  20. #define BROTLI_COMMON_PLATFORM_H_
  21. #include <string.h> /* memcpy */
  22. #include <stdlib.h> /* malloc, free */
  23. #include <brotli/port.h>
  24. #include <brotli/types.h>
  25. #if defined(OS_LINUX) || defined(OS_CYGWIN)
  26. #include <endian.h>
  27. #elif defined(OS_FREEBSD)
  28. #include <machine/endian.h>
  29. #elif defined(OS_MACOSX)
  30. #include <machine/endian.h>
  31. /* Let's try and follow the Linux convention */
  32. #define BROTLI_X_BYTE_ORDER BYTE_ORDER
  33. #define BROTLI_X_LITTLE_ENDIAN LITTLE_ENDIAN
  34. #define BROTLI_X_BIG_ENDIAN BIG_ENDIAN
  35. #endif
  36. #if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
  37. #include <assert.h>
  38. #include <stdio.h>
  39. #endif
  40. /* The following macros were borrowed from https://github.com/nemequ/hedley
  41. * with permission of original author - Evan Nemerson <evan@nemerson.com> */
  42. /* >>> >>> >>> hedley macros */
  43. /* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable
  44. compilers.
  45. To apply compiler hint, enclose the branching condition into macros, like this:
  46. if (BROTLI_PREDICT_TRUE(zero == 0)) {
  47. // main execution path
  48. } else {
  49. // compiler should place this code outside of main execution path
  50. }
  51. OR:
  52. if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) {
  53. // compiler should place this code outside of main execution path
  54. }
  55. */
  56. #if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \
  57. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  58. BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) || \
  59. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  60. BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
  61. BROTLI_TI_VERSION_CHECK(7, 3, 0) || \
  62. BROTLI_TINYC_VERSION_CHECK(0, 9, 27)
  63. #define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
  64. #define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0))
  65. #else
  66. #define BROTLI_PREDICT_FALSE(x) (x)
  67. #define BROTLI_PREDICT_TRUE(x) (x)
  68. #endif
  69. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  70. !defined(__cplusplus)
  71. #define BROTLI_RESTRICT restrict
  72. #elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) || \
  73. BROTLI_MSVC_VERSION_CHECK(14, 0, 0) || \
  74. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  75. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  76. BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
  77. BROTLI_PGI_VERSION_CHECK(17, 10, 0) || \
  78. BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
  79. BROTLI_IAR_VERSION_CHECK(8, 0, 0) || \
  80. (BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus))
  81. #define BROTLI_RESTRICT __restrict
  82. #elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus)
  83. #define BROTLI_RESTRICT _Restrict
  84. #else
  85. #define BROTLI_RESTRICT
  86. #endif
  87. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  88. (defined(__cplusplus) && (__cplusplus >= 199711L))
  89. #define BROTLI_MAYBE_INLINE inline
  90. #elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \
  91. BROTLI_ARM_VERSION_CHECK(6, 2, 0)
  92. #define BROTLI_MAYBE_INLINE __inline__
  93. #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \
  94. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0)
  95. #define BROTLI_MAYBE_INLINE __inline
  96. #else
  97. #define BROTLI_MAYBE_INLINE
  98. #endif
  99. #if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) || \
  100. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  101. BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
  102. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  103. BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
  104. BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
  105. (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
  106. #define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__))
  107. #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
  108. #define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline
  109. #elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus)
  110. #define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  111. #elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
  112. #define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced")
  113. #else
  114. #define BROTLI_INLINE BROTLI_MAYBE_INLINE
  115. #endif
  116. #if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) || \
  117. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  118. BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
  119. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  120. BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
  121. BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
  122. (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
  123. #define BROTLI_NOINLINE __attribute__((__noinline__))
  124. #elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0)
  125. #define BROTLI_NOINLINE __declspec(noinline)
  126. #elif BROTLI_PGI_VERSION_CHECK(10, 2, 0)
  127. #define BROTLI_NOINLINE _Pragma("noinline")
  128. #elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus)
  129. #define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;")
  130. #elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
  131. #define BROTLI_NOINLINE _Pragma("inline=never")
  132. #else
  133. #define BROTLI_NOINLINE
  134. #endif
  135. /* BROTLI_INTERNAL could be defined to override visibility, e.g. for tests. */
  136. #if !defined(BROTLI_INTERNAL)
  137. #if defined(_WIN32) || defined(__CYGWIN__)
  138. #define BROTLI_INTERNAL
  139. #elif BROTLI_GNUC_VERSION_CHECK(3, 3, 0) || \
  140. BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
  141. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  142. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  143. BROTLI_IBM_VERSION_CHECK(13, 1, 0) || \
  144. BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
  145. (BROTLI_TI_VERSION_CHECK(7, 3, 0) && \
  146. defined(__TI_GNU_ATTRIBUTE_SUPPORT__) && defined(__TI_EABI__))
  147. #define BROTLI_INTERNAL __attribute__ ((visibility ("hidden")))
  148. #else
  149. #define BROTLI_INTERNAL
  150. #endif
  151. #endif
  152. /* <<< <<< <<< end of hedley macros. */
  153. #if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \
  154. BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
  155. #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused))
  156. #else
  157. #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE
  158. #endif
  159. #if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
  160. #define BROTLI_ALIGNED(N) __attribute__((aligned(N)))
  161. #else
  162. #define BROTLI_ALIGNED(N)
  163. #endif
  164. #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \
  165. (defined(M_ARM) && (M_ARM == 7))
  166. #define BROTLI_TARGET_ARMV7
  167. #endif /* ARMv7 */
  168. #if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \
  169. defined(__aarch64__) || defined(__ARM64_ARCH_8__)
  170. #define BROTLI_TARGET_ARMV8_ANY
  171. #if defined(__ARM_32BIT_STATE)
  172. #define BROTLI_TARGET_ARMV8_32
  173. #elif defined(__ARM_64BIT_STATE)
  174. #define BROTLI_TARGET_ARMV8_64
  175. #endif
  176. #endif /* ARMv8 */
  177. #if defined(__ARM_NEON__) || defined(__ARM_NEON)
  178. #define BROTLI_TARGET_NEON
  179. #endif
  180. #if defined(__i386) || defined(_M_IX86)
  181. #define BROTLI_TARGET_X86
  182. #endif
  183. #if defined(__x86_64__) || defined(_M_X64)
  184. #define BROTLI_TARGET_X64
  185. #endif
  186. #if defined(__PPC64__)
  187. #define BROTLI_TARGET_POWERPC64
  188. #endif
  189. #if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
  190. #define BROTLI_TARGET_RISCV64
  191. #endif
  192. #if defined(BROTLI_BUILD_64_BIT)
  193. #define BROTLI_64_BITS 1
  194. #elif defined(BROTLI_BUILD_32_BIT)
  195. #define BROTLI_64_BITS 0
  196. #elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \
  197. defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64)
  198. #define BROTLI_64_BITS 1
  199. #else
  200. #define BROTLI_64_BITS 0
  201. #endif
  202. #if (BROTLI_64_BITS)
  203. #define brotli_reg_t uint64_t
  204. #else
  205. #define brotli_reg_t uint32_t
  206. #endif
  207. #if defined(BROTLI_BUILD_BIG_ENDIAN)
  208. #define BROTLI_BIG_ENDIAN 1
  209. #elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
  210. #define BROTLI_LITTLE_ENDIAN 1
  211. #elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
  212. /* Just break elif chain. */
  213. #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  214. #define BROTLI_LITTLE_ENDIAN 1
  215. #elif defined(_WIN32) || defined(BROTLI_TARGET_X64)
  216. /* Win32 & x64 can currently always be assumed to be little endian */
  217. #define BROTLI_LITTLE_ENDIAN 1
  218. #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  219. #define BROTLI_BIG_ENDIAN 1
  220. #elif defined(BROTLI_X_BYTE_ORDER)
  221. #if BROTLI_X_BYTE_ORDER == BROTLI_X_LITTLE_ENDIAN
  222. #define BROTLI_LITTLE_ENDIAN 1
  223. #elif BROTLI_X_BYTE_ORDER == BROTLI_X_BIG_ENDIAN
  224. #define BROTLI_BIG_ENDIAN 1
  225. #endif
  226. #endif /* BROTLI_X_BYTE_ORDER */
  227. #if !defined(BROTLI_LITTLE_ENDIAN)
  228. #define BROTLI_LITTLE_ENDIAN 0
  229. #endif
  230. #if !defined(BROTLI_BIG_ENDIAN)
  231. #define BROTLI_BIG_ENDIAN 0
  232. #endif
  233. #if defined(BROTLI_X_BYTE_ORDER)
  234. #undef BROTLI_X_BYTE_ORDER
  235. #undef BROTLI_X_LITTLE_ENDIAN
  236. #undef BROTLI_X_BIG_ENDIAN
  237. #endif
  238. #if defined(BROTLI_BUILD_PORTABLE)
  239. #define BROTLI_ALIGNED_READ (!!1)
  240. #elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
  241. defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \
  242. defined(BROTLI_TARGET_RISCV64)
  243. /* Allow unaligned read only for white-listed CPUs. */
  244. #define BROTLI_ALIGNED_READ (!!0)
  245. #else
  246. #define BROTLI_ALIGNED_READ (!!1)
  247. #endif
  248. #if BROTLI_ALIGNED_READ
  249. /* Portable unaligned memory access: read / write values via memcpy. */
  250. static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {
  251. uint16_t t;
  252. memcpy(&t, p, sizeof t);
  253. return t;
  254. }
  255. static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {
  256. uint32_t t;
  257. memcpy(&t, p, sizeof t);
  258. return t;
  259. }
  260. static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
  261. uint64_t t;
  262. memcpy(&t, p, sizeof t);
  263. return t;
  264. }
  265. static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
  266. memcpy(p, &v, sizeof v);
  267. }
  268. #else /* BROTLI_ALIGNED_READ */
  269. /* Unaligned memory access is allowed: just cast pointer to requested type. */
  270. #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || \
  271. defined(MEMORY_SANITIZER)
  272. /* Consider we have an unaligned load/store of 4 bytes from address 0x...05.
  273. AddressSanitizer will treat it as a 3-byte access to the range 05:07 and
  274. will miss a bug if 08 is the first unaddressable byte.
  275. ThreadSanitizer will also treat this as a 3-byte access to 05:07 and will
  276. miss a race between this access and some other accesses to 08.
  277. MemorySanitizer will correctly propagate the shadow on unaligned stores
  278. and correctly report bugs on unaligned loads, but it may not properly
  279. update and report the origin of the uninitialized memory.
  280. For all three tools, replacing an unaligned access with a tool-specific
  281. callback solves the problem. */
  282. #if defined(__cplusplus)
  283. extern "C" {
  284. #endif /* __cplusplus */
  285. uint16_t __sanitizer_unaligned_load16(const void* p);
  286. uint32_t __sanitizer_unaligned_load32(const void* p);
  287. uint64_t __sanitizer_unaligned_load64(const void* p);
  288. void __sanitizer_unaligned_store64(void* p, uint64_t v);
  289. #if defined(__cplusplus)
  290. } /* extern "C" */
  291. #endif /* __cplusplus */
  292. #define BrotliUnalignedRead16 __sanitizer_unaligned_load16
  293. #define BrotliUnalignedRead32 __sanitizer_unaligned_load32
  294. #define BrotliUnalignedRead64 __sanitizer_unaligned_load64
  295. #define BrotliUnalignedWrite64 __sanitizer_unaligned_store64
  296. #else
  297. static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {
  298. return *(const uint16_t*)p;
  299. }
  300. static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {
  301. return *(const uint32_t*)p;
  302. }
  303. #if (BROTLI_64_BITS)
  304. static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
  305. return *(const uint64_t*)p;
  306. }
  307. static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
  308. *(uint64_t*)p = v;
  309. }
  310. #else /* BROTLI_64_BITS */
  311. /* Avoid emitting LDRD / STRD, which require properly aligned address. */
  312. /* If __attribute__(aligned) is available, use that. Otherwise, memcpy. */
  313. #if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
  314. typedef BROTLI_ALIGNED(1) uint64_t brotli_unaligned_uint64_t;
  315. static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
  316. return (uint64_t) ((brotli_unaligned_uint64_t*) p)[0];
  317. }
  318. static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
  319. brotli_unaligned_uint64_t* dwords = (brotli_unaligned_uint64_t*) p;
  320. dwords[0] = (brotli_unaligned_uint64_t) v;
  321. }
  322. #else /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */
  323. static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
  324. uint64_t v;
  325. memcpy(&v, p, sizeof(uint64_t));
  326. return v;
  327. }
  328. static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
  329. memcpy(p, &v, sizeof(uint64_t));
  330. }
  331. #endif /* BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0) */
  332. #endif /* BROTLI_64_BITS */
  333. #endif /* ASAN / TSAN / MSAN */
  334. #endif /* BROTLI_ALIGNED_READ */
  335. #if BROTLI_LITTLE_ENDIAN
  336. /* Straight endianness. Just read / write values. */
  337. #define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16
  338. #define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32
  339. #define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64
  340. #define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64
  341. #elif BROTLI_BIG_ENDIAN /* BROTLI_LITTLE_ENDIAN */
  342. /* Explain compiler to byte-swap values. */
  343. #define BROTLI_BSWAP16_(V) ((uint16_t)( \
  344. (((V) & 0xFFU) << 8) | \
  345. (((V) >> 8) & 0xFFU)))
  346. static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
  347. uint16_t value = BrotliUnalignedRead16(p);
  348. return BROTLI_BSWAP16_(value);
  349. }
  350. #define BROTLI_BSWAP32_(V) ( \
  351. (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \
  352. (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU))
  353. static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
  354. uint32_t value = BrotliUnalignedRead32(p);
  355. return BROTLI_BSWAP32_(value);
  356. }
  357. #define BROTLI_BSWAP64_(V) ( \
  358. (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \
  359. (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \
  360. (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \
  361. (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU))
  362. static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
  363. uint64_t value = BrotliUnalignedRead64(p);
  364. return BROTLI_BSWAP64_(value);
  365. }
  366. static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
  367. uint64_t value = BROTLI_BSWAP64_(v);
  368. BrotliUnalignedWrite64(p, value);
  369. }
  370. #else /* BROTLI_LITTLE_ENDIAN */
  371. /* Read / store values byte-wise; hopefully compiler will understand. */
  372. static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
  373. const uint8_t* in = (const uint8_t*)p;
  374. return (uint16_t)(in[0] | (in[1] << 8));
  375. }
  376. static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
  377. const uint8_t* in = (const uint8_t*)p;
  378. uint32_t value = (uint32_t)(in[0]);
  379. value |= (uint32_t)(in[1]) << 8;
  380. value |= (uint32_t)(in[2]) << 16;
  381. value |= (uint32_t)(in[3]) << 24;
  382. return value;
  383. }
  384. static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
  385. const uint8_t* in = (const uint8_t*)p;
  386. uint64_t value = (uint64_t)(in[0]);
  387. value |= (uint64_t)(in[1]) << 8;
  388. value |= (uint64_t)(in[2]) << 16;
  389. value |= (uint64_t)(in[3]) << 24;
  390. value |= (uint64_t)(in[4]) << 32;
  391. value |= (uint64_t)(in[5]) << 40;
  392. value |= (uint64_t)(in[6]) << 48;
  393. value |= (uint64_t)(in[7]) << 56;
  394. return value;
  395. }
  396. static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
  397. uint8_t* out = (uint8_t*)p;
  398. out[0] = (uint8_t)v;
  399. out[1] = (uint8_t)(v >> 8);
  400. out[2] = (uint8_t)(v >> 16);
  401. out[3] = (uint8_t)(v >> 24);
  402. out[4] = (uint8_t)(v >> 32);
  403. out[5] = (uint8_t)(v >> 40);
  404. out[6] = (uint8_t)(v >> 48);
  405. out[7] = (uint8_t)(v >> 56);
  406. }
  407. #endif /* BROTLI_LITTLE_ENDIAN */
  408. /* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */
  409. #if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \
  410. BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
  411. #define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x))
  412. #else
  413. #define BROTLI_IS_CONSTANT(x) (!!0)
  414. #endif
  415. #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
  416. #define BROTLI_HAS_UBFX (!!1)
  417. #else
  418. #define BROTLI_HAS_UBFX (!!0)
  419. #endif
  420. #if defined(BROTLI_ENABLE_LOG)
  421. #define BROTLI_DCHECK(x) assert(x)
  422. #define BROTLI_LOG(x) printf x
  423. #else
  424. #define BROTLI_DCHECK(x)
  425. #define BROTLI_LOG(x)
  426. #endif
  427. #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
  428. static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
  429. fprintf(stderr, "%s:%d (%s)\n", f, l, fn);
  430. fflush(stderr);
  431. }
  432. #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
  433. #else
  434. #define BROTLI_DUMP() (void)(0)
  435. #endif
  436. /* TODO: add appropriate icc/sunpro/arm/ibm/ti checks. */
  437. #if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \
  438. !defined(BROTLI_BUILD_NO_RBIT)
  439. #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
  440. /* TODO: detect ARMv6T2 and enable this code for it. */
  441. static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) {
  442. brotli_reg_t output;
  443. __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
  444. return output;
  445. }
  446. #define BROTLI_RBIT(x) BrotliRBit(x)
  447. #endif /* armv7 / armv8 */
  448. #endif /* gcc || clang */
  449. #if !defined(BROTLI_RBIT)
  450. static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ }
  451. #endif /* BROTLI_RBIT */
  452. #define BROTLI_REPEAT(N, X) { \
  453. if ((N & 1) != 0) {X;} \
  454. if ((N & 2) != 0) {X; X;} \
  455. if ((N & 4) != 0) {X; X; X; X;} \
  456. }
  457. #define BROTLI_UNUSED(X) (void)(X)
  458. #define BROTLI_MIN_MAX(T) \
  459. static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \
  460. static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; }
  461. BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int)
  462. BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t)
  463. #undef BROTLI_MIN_MAX
  464. #define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))
  465. #define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))
  466. #define BROTLI_SWAP(T, A, I, J) { \
  467. T __brotli_swap_tmp = (A)[(I)]; \
  468. (A)[(I)] = (A)[(J)]; \
  469. (A)[(J)] = __brotli_swap_tmp; \
  470. }
  471. /* Default brotli_alloc_func */
  472. static void* BrotliDefaultAllocFunc(void* opaque, size_t size) {
  473. BROTLI_UNUSED(opaque);
  474. return malloc(size);
  475. }
  476. /* Default brotli_free_func */
  477. static void BrotliDefaultFreeFunc(void* opaque, void* address) {
  478. BROTLI_UNUSED(opaque);
  479. free(address);
  480. }
  481. BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) {
  482. BROTLI_UNUSED(&BrotliSuppressUnusedFunctions);
  483. BROTLI_UNUSED(&BrotliUnalignedRead16);
  484. BROTLI_UNUSED(&BrotliUnalignedRead32);
  485. BROTLI_UNUSED(&BrotliUnalignedRead64);
  486. BROTLI_UNUSED(&BrotliUnalignedWrite64);
  487. BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE);
  488. BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE);
  489. BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE);
  490. BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE);
  491. BROTLI_UNUSED(&BrotliRBit);
  492. BROTLI_UNUSED(&brotli_min_double);
  493. BROTLI_UNUSED(&brotli_max_double);
  494. BROTLI_UNUSED(&brotli_min_float);
  495. BROTLI_UNUSED(&brotli_max_float);
  496. BROTLI_UNUSED(&brotli_min_int);
  497. BROTLI_UNUSED(&brotli_max_int);
  498. BROTLI_UNUSED(&brotli_min_size_t);
  499. BROTLI_UNUSED(&brotli_max_size_t);
  500. BROTLI_UNUSED(&brotli_min_uint32_t);
  501. BROTLI_UNUSED(&brotli_max_uint32_t);
  502. BROTLI_UNUSED(&brotli_min_uint8_t);
  503. BROTLI_UNUSED(&brotli_max_uint8_t);
  504. BROTLI_UNUSED(&BrotliDefaultAllocFunc);
  505. BROTLI_UNUSED(&BrotliDefaultFreeFunc);
  506. #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
  507. BROTLI_UNUSED(&BrotliDump);
  508. #endif
  509. }
  510. #endif /* BROTLI_COMMON_PLATFORM_H_ */