platform.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_NO_RBIT disables "rbit" optimization for ARM CPUs
  13. * BROTLI_BUILD_NO_UNALIGNED_READ_FAST forces off the fast-unaligned-read
  14. optimizations (mainly for testing purposes)
  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. * BROTLI_ENABLE_DUMP overrides default "dump" behaviour
  19. */
  20. #ifndef BROTLI_COMMON_PLATFORM_H_
  21. #define BROTLI_COMMON_PLATFORM_H_
  22. #include <string.h> /* memcpy */
  23. #include <brotli/port.h>
  24. #include <brotli/types.h>
  25. #if defined(OS_LINUX) || defined(OS_CYGWIN) || defined(__EMSCRIPTEN__)
  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 BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
  37. #include <intrin.h>
  38. #endif
  39. #if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
  40. #include <assert.h>
  41. #include <stdio.h>
  42. #endif
  43. /* The following macros were borrowed from https://github.com/nemequ/hedley
  44. * with permission of original author - Evan Nemerson <evan@nemerson.com> */
  45. /* >>> >>> >>> hedley macros */
  46. /* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable
  47. compilers.
  48. To apply compiler hint, enclose the branching condition into macros, like this:
  49. if (BROTLI_PREDICT_TRUE(zero == 0)) {
  50. // main execution path
  51. } else {
  52. // compiler should place this code outside of main execution path
  53. }
  54. OR:
  55. if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) {
  56. // compiler should place this code outside of main execution path
  57. }
  58. */
  59. #if BROTLI_GNUC_HAS_BUILTIN(__builtin_expect, 3, 0, 0) || \
  60. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  61. BROTLI_SUNPRO_VERSION_CHECK(5, 15, 0) || \
  62. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  63. BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
  64. BROTLI_TI_VERSION_CHECK(7, 3, 0) || \
  65. BROTLI_TINYC_VERSION_CHECK(0, 9, 27)
  66. #define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
  67. #define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0))
  68. #else
  69. #define BROTLI_PREDICT_FALSE(x) (x)
  70. #define BROTLI_PREDICT_TRUE(x) (x)
  71. #endif
  72. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
  73. !defined(__cplusplus)
  74. #define BROTLI_RESTRICT restrict
  75. #elif BROTLI_GNUC_VERSION_CHECK(3, 1, 0) || \
  76. BROTLI_MSVC_VERSION_CHECK(14, 0, 0) || \
  77. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  78. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  79. BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
  80. BROTLI_PGI_VERSION_CHECK(17, 10, 0) || \
  81. BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
  82. BROTLI_IAR_VERSION_CHECK(8, 0, 0) || \
  83. (BROTLI_SUNPRO_VERSION_CHECK(5, 14, 0) && defined(__cplusplus))
  84. #define BROTLI_RESTRICT __restrict
  85. #elif BROTLI_SUNPRO_VERSION_CHECK(5, 3, 0) && !defined(__cplusplus)
  86. #define BROTLI_RESTRICT _Restrict
  87. #else
  88. #define BROTLI_RESTRICT
  89. #endif
  90. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
  91. (defined(__cplusplus) && (__cplusplus >= 199711L))
  92. #define BROTLI_MAYBE_INLINE inline
  93. #elif defined(__GNUC_STDC_INLINE__) || defined(__GNUC_GNU_INLINE__) || \
  94. BROTLI_ARM_VERSION_CHECK(6, 2, 0)
  95. #define BROTLI_MAYBE_INLINE __inline__
  96. #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0) || \
  97. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || BROTLI_TI_VERSION_CHECK(8, 0, 0)
  98. #define BROTLI_MAYBE_INLINE __inline
  99. #else
  100. #define BROTLI_MAYBE_INLINE
  101. #endif
  102. #if BROTLI_GNUC_HAS_ATTRIBUTE(always_inline, 4, 0, 0) || \
  103. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  104. BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
  105. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  106. BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
  107. BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
  108. (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
  109. #define BROTLI_INLINE BROTLI_MAYBE_INLINE __attribute__((__always_inline__))
  110. #elif BROTLI_MSVC_VERSION_CHECK(12, 0, 0)
  111. #define BROTLI_INLINE BROTLI_MAYBE_INLINE __forceinline
  112. #elif BROTLI_TI_VERSION_CHECK(7, 0, 0) && defined(__cplusplus)
  113. #define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
  114. #elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
  115. #define BROTLI_INLINE BROTLI_MAYBE_INLINE _Pragma("inline=forced")
  116. #else
  117. #define BROTLI_INLINE BROTLI_MAYBE_INLINE
  118. #endif
  119. #if BROTLI_GNUC_HAS_ATTRIBUTE(noinline, 4, 0, 0) || \
  120. BROTLI_INTEL_VERSION_CHECK(16, 0, 0) || \
  121. BROTLI_SUNPRO_VERSION_CHECK(5, 11, 0) || \
  122. BROTLI_ARM_VERSION_CHECK(4, 1, 0) || \
  123. BROTLI_IBM_VERSION_CHECK(10, 1, 0) || \
  124. BROTLI_TI_VERSION_CHECK(8, 0, 0) || \
  125. (BROTLI_TI_VERSION_CHECK(7, 3, 0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
  126. #define BROTLI_NOINLINE __attribute__((__noinline__))
  127. #elif BROTLI_MSVC_VERSION_CHECK(13, 10, 0)
  128. #define BROTLI_NOINLINE __declspec(noinline)
  129. #elif BROTLI_PGI_VERSION_CHECK(10, 2, 0)
  130. #define BROTLI_NOINLINE _Pragma("noinline")
  131. #elif BROTLI_TI_VERSION_CHECK(6, 0, 0) && defined(__cplusplus)
  132. #define BROTLI_NOINLINE _Pragma("FUNC_CANNOT_INLINE;")
  133. #elif BROTLI_IAR_VERSION_CHECK(8, 0, 0)
  134. #define BROTLI_NOINLINE _Pragma("inline=never")
  135. #else
  136. #define BROTLI_NOINLINE
  137. #endif
  138. /* <<< <<< <<< end of hedley macros. */
  139. #if BROTLI_GNUC_HAS_ATTRIBUTE(unused, 2, 7, 0) || \
  140. BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
  141. #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE __attribute__ ((unused))
  142. #else
  143. #define BROTLI_UNUSED_FUNCTION static BROTLI_INLINE
  144. #endif
  145. #if BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0)
  146. #define BROTLI_ALIGNED(N) __attribute__((aligned(N)))
  147. #else
  148. #define BROTLI_ALIGNED(N)
  149. #endif
  150. #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \
  151. (defined(M_ARM) && (M_ARM == 7))
  152. #define BROTLI_TARGET_ARMV7
  153. #endif /* ARMv7 */
  154. #if (defined(__ARM_ARCH) && (__ARM_ARCH == 8)) || \
  155. defined(__aarch64__) || defined(__ARM64_ARCH_8__)
  156. #define BROTLI_TARGET_ARMV8_ANY
  157. #if defined(__ARM_32BIT_STATE)
  158. #define BROTLI_TARGET_ARMV8_32
  159. #elif defined(__ARM_64BIT_STATE)
  160. #define BROTLI_TARGET_ARMV8_64
  161. #endif
  162. #endif /* ARMv8 */
  163. #if defined(__ARM_NEON__) || defined(__ARM_NEON)
  164. #define BROTLI_TARGET_NEON
  165. #endif
  166. #if defined(__i386) || defined(_M_IX86)
  167. #define BROTLI_TARGET_X86
  168. #endif
  169. #if defined(__x86_64__) || defined(_M_X64)
  170. #define BROTLI_TARGET_X64
  171. #endif
  172. #if defined(__PPC64__)
  173. #define BROTLI_TARGET_POWERPC64
  174. #endif
  175. #if defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
  176. #define BROTLI_TARGET_RISCV64
  177. #endif
  178. #if defined(__loongarch_lp64)
  179. #define BROTLI_TARGET_LOONGARCH64
  180. #endif
  181. #if defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8_64) || \
  182. defined(BROTLI_TARGET_POWERPC64) || defined(BROTLI_TARGET_RISCV64) || \
  183. defined(BROTLI_TARGET_LOONGARCH64)
  184. #define BROTLI_TARGET_64_BITS 1
  185. #else
  186. #define BROTLI_TARGET_64_BITS 0
  187. #endif
  188. #if defined(BROTLI_BUILD_64_BIT)
  189. #define BROTLI_64_BITS 1
  190. #elif defined(BROTLI_BUILD_32_BIT)
  191. #define BROTLI_64_BITS 0
  192. #else
  193. #define BROTLI_64_BITS BROTLI_TARGET_64_BITS
  194. #endif
  195. #if (BROTLI_64_BITS)
  196. #define brotli_reg_t uint64_t
  197. #else
  198. #define brotli_reg_t uint32_t
  199. #endif
  200. #if defined(BROTLI_BUILD_BIG_ENDIAN)
  201. #define BROTLI_BIG_ENDIAN 1
  202. #elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
  203. #define BROTLI_LITTLE_ENDIAN 1
  204. #elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
  205. /* Just break elif chain. */
  206. #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  207. #define BROTLI_LITTLE_ENDIAN 1
  208. #elif defined(_WIN32) || defined(BROTLI_TARGET_X64)
  209. /* Win32 & x64 can currently always be assumed to be little endian */
  210. #define BROTLI_LITTLE_ENDIAN 1
  211. #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  212. #define BROTLI_BIG_ENDIAN 1
  213. #elif defined(BROTLI_X_BYTE_ORDER)
  214. #if BROTLI_X_BYTE_ORDER == BROTLI_X_LITTLE_ENDIAN
  215. #define BROTLI_LITTLE_ENDIAN 1
  216. #elif BROTLI_X_BYTE_ORDER == BROTLI_X_BIG_ENDIAN
  217. #define BROTLI_BIG_ENDIAN 1
  218. #endif
  219. #endif /* BROTLI_X_BYTE_ORDER */
  220. #if !defined(BROTLI_LITTLE_ENDIAN)
  221. #define BROTLI_LITTLE_ENDIAN 0
  222. #endif
  223. #if !defined(BROTLI_BIG_ENDIAN)
  224. #define BROTLI_BIG_ENDIAN 0
  225. #endif
  226. #if defined(BROTLI_X_BYTE_ORDER)
  227. #undef BROTLI_X_BYTE_ORDER
  228. #undef BROTLI_X_LITTLE_ENDIAN
  229. #undef BROTLI_X_BIG_ENDIAN
  230. #endif
  231. #if defined(BROTLI_BUILD_NO_UNALIGNED_READ_FAST)
  232. #define BROTLI_UNALIGNED_READ_FAST (!!0)
  233. #elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
  234. defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY) || \
  235. defined(BROTLI_TARGET_RISCV64) || defined(BROTLI_TARGET_LOONGARCH64)
  236. /* These targets are known to generate efficient code for unaligned reads
  237. * (e.g. a single instruction, not multiple 1-byte loads, shifted and or'd
  238. * together). */
  239. #define BROTLI_UNALIGNED_READ_FAST (!!1)
  240. #else
  241. #define BROTLI_UNALIGNED_READ_FAST (!!0)
  242. #endif
  243. /* Portable unaligned memory access: read / write values via memcpy. */
  244. static BROTLI_INLINE uint16_t BrotliUnalignedRead16(const void* p) {
  245. uint16_t t;
  246. memcpy(&t, p, sizeof t);
  247. return t;
  248. }
  249. static BROTLI_INLINE uint32_t BrotliUnalignedRead32(const void* p) {
  250. uint32_t t;
  251. memcpy(&t, p, sizeof t);
  252. return t;
  253. }
  254. static BROTLI_INLINE uint64_t BrotliUnalignedRead64(const void* p) {
  255. uint64_t t;
  256. memcpy(&t, p, sizeof t);
  257. return t;
  258. }
  259. static BROTLI_INLINE void BrotliUnalignedWrite64(void* p, uint64_t v) {
  260. memcpy(p, &v, sizeof v);
  261. }
  262. #if BROTLI_LITTLE_ENDIAN
  263. /* Straight endianness. Just read / write values. */
  264. #define BROTLI_UNALIGNED_LOAD16LE BrotliUnalignedRead16
  265. #define BROTLI_UNALIGNED_LOAD32LE BrotliUnalignedRead32
  266. #define BROTLI_UNALIGNED_LOAD64LE BrotliUnalignedRead64
  267. #define BROTLI_UNALIGNED_STORE64LE BrotliUnalignedWrite64
  268. #elif BROTLI_BIG_ENDIAN /* BROTLI_LITTLE_ENDIAN */
  269. /* Explain compiler to byte-swap values. */
  270. #define BROTLI_BSWAP16_(V) ((uint16_t)( \
  271. (((V) & 0xFFU) << 8) | \
  272. (((V) >> 8) & 0xFFU)))
  273. static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
  274. uint16_t value = BrotliUnalignedRead16(p);
  275. return BROTLI_BSWAP16_(value);
  276. }
  277. #define BROTLI_BSWAP32_(V) ( \
  278. (((V) & 0xFFU) << 24) | (((V) & 0xFF00U) << 8) | \
  279. (((V) >> 8) & 0xFF00U) | (((V) >> 24) & 0xFFU))
  280. static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
  281. uint32_t value = BrotliUnalignedRead32(p);
  282. return BROTLI_BSWAP32_(value);
  283. }
  284. #define BROTLI_BSWAP64_(V) ( \
  285. (((V) & 0xFFU) << 56) | (((V) & 0xFF00U) << 40) | \
  286. (((V) & 0xFF0000U) << 24) | (((V) & 0xFF000000U) << 8) | \
  287. (((V) >> 8) & 0xFF000000U) | (((V) >> 24) & 0xFF0000U) | \
  288. (((V) >> 40) & 0xFF00U) | (((V) >> 56) & 0xFFU))
  289. static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
  290. uint64_t value = BrotliUnalignedRead64(p);
  291. return BROTLI_BSWAP64_(value);
  292. }
  293. static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
  294. uint64_t value = BROTLI_BSWAP64_(v);
  295. BrotliUnalignedWrite64(p, value);
  296. }
  297. #else /* BROTLI_LITTLE_ENDIAN */
  298. /* Read / store values byte-wise; hopefully compiler will understand. */
  299. static BROTLI_INLINE uint16_t BROTLI_UNALIGNED_LOAD16LE(const void* p) {
  300. const uint8_t* in = (const uint8_t*)p;
  301. return (uint16_t)(in[0] | (in[1] << 8));
  302. }
  303. static BROTLI_INLINE uint32_t BROTLI_UNALIGNED_LOAD32LE(const void* p) {
  304. const uint8_t* in = (const uint8_t*)p;
  305. uint32_t value = (uint32_t)(in[0]);
  306. value |= (uint32_t)(in[1]) << 8;
  307. value |= (uint32_t)(in[2]) << 16;
  308. value |= (uint32_t)(in[3]) << 24;
  309. return value;
  310. }
  311. static BROTLI_INLINE uint64_t BROTLI_UNALIGNED_LOAD64LE(const void* p) {
  312. const uint8_t* in = (const uint8_t*)p;
  313. uint64_t value = (uint64_t)(in[0]);
  314. value |= (uint64_t)(in[1]) << 8;
  315. value |= (uint64_t)(in[2]) << 16;
  316. value |= (uint64_t)(in[3]) << 24;
  317. value |= (uint64_t)(in[4]) << 32;
  318. value |= (uint64_t)(in[5]) << 40;
  319. value |= (uint64_t)(in[6]) << 48;
  320. value |= (uint64_t)(in[7]) << 56;
  321. return value;
  322. }
  323. static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
  324. uint8_t* out = (uint8_t*)p;
  325. out[0] = (uint8_t)v;
  326. out[1] = (uint8_t)(v >> 8);
  327. out[2] = (uint8_t)(v >> 16);
  328. out[3] = (uint8_t)(v >> 24);
  329. out[4] = (uint8_t)(v >> 32);
  330. out[5] = (uint8_t)(v >> 40);
  331. out[6] = (uint8_t)(v >> 48);
  332. out[7] = (uint8_t)(v >> 56);
  333. }
  334. #endif /* BROTLI_LITTLE_ENDIAN */
  335. static BROTLI_INLINE void* BROTLI_UNALIGNED_LOAD_PTR(const void* p) {
  336. void* v;
  337. memcpy(&v, p, sizeof(void*));
  338. return v;
  339. }
  340. static BROTLI_INLINE void BROTLI_UNALIGNED_STORE_PTR(void* p, const void* v) {
  341. memcpy(p, &v, sizeof(void*));
  342. }
  343. /* BROTLI_IS_CONSTANT macros returns true for compile-time constants. */
  344. #if BROTLI_GNUC_HAS_BUILTIN(__builtin_constant_p, 3, 0, 1) || \
  345. BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
  346. #define BROTLI_IS_CONSTANT(x) (!!__builtin_constant_p(x))
  347. #else
  348. #define BROTLI_IS_CONSTANT(x) (!!0)
  349. #endif
  350. #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
  351. #define BROTLI_HAS_UBFX (!!1)
  352. #else
  353. #define BROTLI_HAS_UBFX (!!0)
  354. #endif
  355. #if defined(BROTLI_ENABLE_LOG)
  356. #define BROTLI_LOG(x) printf x
  357. #else
  358. #define BROTLI_LOG(x)
  359. #endif
  360. #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
  361. #define BROTLI_ENABLE_DUMP_DEFAULT 1
  362. #define BROTLI_DCHECK(x) assert(x)
  363. #else
  364. #define BROTLI_ENABLE_DUMP_DEFAULT 0
  365. #define BROTLI_DCHECK(x)
  366. #endif
  367. #if !defined(BROTLI_ENABLE_DUMP)
  368. #define BROTLI_ENABLE_DUMP BROTLI_ENABLE_DUMP_DEFAULT
  369. #endif
  370. #if BROTLI_ENABLE_DUMP
  371. static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
  372. fprintf(stderr, "%s:%d (%s)\n", f, l, fn);
  373. fflush(stderr);
  374. }
  375. #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
  376. #else
  377. #define BROTLI_DUMP() (void)(0)
  378. #endif
  379. /* BrotliRBit assumes brotli_reg_t fits native CPU register type. */
  380. #if (BROTLI_64_BITS == BROTLI_TARGET_64_BITS)
  381. /* TODO(eustas): add appropriate icc/sunpro/arm/ibm/ti checks. */
  382. #if (BROTLI_GNUC_VERSION_CHECK(3, 0, 0) || defined(__llvm__)) && \
  383. !defined(BROTLI_BUILD_NO_RBIT)
  384. #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_ANY)
  385. /* TODO(eustas): detect ARMv6T2 and enable this code for it. */
  386. static BROTLI_INLINE brotli_reg_t BrotliRBit(brotli_reg_t input) {
  387. brotli_reg_t output;
  388. __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
  389. return output;
  390. }
  391. #define BROTLI_RBIT(x) BrotliRBit(x)
  392. #endif /* armv7 / armv8 */
  393. #endif /* gcc || clang */
  394. #endif /* brotli_reg_t is native */
  395. #if !defined(BROTLI_RBIT)
  396. static BROTLI_INLINE void BrotliRBit(void) { /* Should break build if used. */ }
  397. #endif /* BROTLI_RBIT */
  398. #define BROTLI_REPEAT_4(X) {X; X; X; X;}
  399. #define BROTLI_REPEAT_5(X) {X; X; X; X; X;}
  400. #define BROTLI_REPEAT_6(X) {X; X; X; X; X; X;}
  401. #define BROTLI_UNUSED(X) (void)(X)
  402. #define BROTLI_MIN_MAX(T) \
  403. static BROTLI_INLINE T brotli_min_ ## T (T a, T b) { return a < b ? a : b; } \
  404. static BROTLI_INLINE T brotli_max_ ## T (T a, T b) { return a > b ? a : b; }
  405. BROTLI_MIN_MAX(double) BROTLI_MIN_MAX(float) BROTLI_MIN_MAX(int)
  406. BROTLI_MIN_MAX(size_t) BROTLI_MIN_MAX(uint32_t) BROTLI_MIN_MAX(uint8_t)
  407. #undef BROTLI_MIN_MAX
  408. #define BROTLI_MIN(T, A, B) (brotli_min_ ## T((A), (B)))
  409. #define BROTLI_MAX(T, A, B) (brotli_max_ ## T((A), (B)))
  410. #define BROTLI_SWAP(T, A, I, J) { \
  411. T __brotli_swap_tmp = (A)[(I)]; \
  412. (A)[(I)] = (A)[(J)]; \
  413. (A)[(J)] = __brotli_swap_tmp; \
  414. }
  415. #if BROTLI_64_BITS
  416. #if BROTLI_GNUC_HAS_BUILTIN(__builtin_ctzll, 3, 4, 0) || \
  417. BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
  418. #define BROTLI_TZCNT64 __builtin_ctzll
  419. #elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
  420. #if defined(BROTLI_TARGET_X64)
  421. #define BROTLI_TZCNT64 _tzcnt_u64
  422. #else /* BROTLI_TARGET_X64 */
  423. static BROTLI_INLINE uint32_t BrotliBsf64Msvc(uint64_t x) {
  424. uint32_t lsb;
  425. _BitScanForward64(&lsb, x);
  426. return lsb;
  427. }
  428. #define BROTLI_TZCNT64 BrotliBsf64Msvc
  429. #endif /* BROTLI_TARGET_X64 */
  430. #endif /* __builtin_ctzll */
  431. #endif /* BROTLI_64_BITS */
  432. #if BROTLI_GNUC_HAS_BUILTIN(__builtin_clz, 3, 4, 0) || \
  433. BROTLI_INTEL_VERSION_CHECK(16, 0, 0)
  434. #define BROTLI_BSR32(x) (31u ^ (uint32_t)__builtin_clz(x))
  435. #elif BROTLI_MSVC_VERSION_CHECK(18, 0, 0)
  436. static BROTLI_INLINE uint32_t BrotliBsr32Msvc(uint32_t x) {
  437. unsigned long msb;
  438. _BitScanReverse(&msb, x);
  439. return (uint32_t)msb;
  440. }
  441. #define BROTLI_BSR32 BrotliBsr32Msvc
  442. #endif /* __builtin_clz */
  443. /* Default brotli_alloc_func */
  444. BROTLI_COMMON_API void* BrotliDefaultAllocFunc(void* opaque, size_t size);
  445. /* Default brotli_free_func */
  446. BROTLI_COMMON_API void BrotliDefaultFreeFunc(void* opaque, void* address);
  447. BROTLI_UNUSED_FUNCTION void BrotliSuppressUnusedFunctions(void) {
  448. BROTLI_UNUSED(&BrotliSuppressUnusedFunctions);
  449. BROTLI_UNUSED(&BrotliUnalignedRead16);
  450. BROTLI_UNUSED(&BrotliUnalignedRead32);
  451. BROTLI_UNUSED(&BrotliUnalignedRead64);
  452. BROTLI_UNUSED(&BrotliUnalignedWrite64);
  453. BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD16LE);
  454. BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD32LE);
  455. BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD64LE);
  456. BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE64LE);
  457. BROTLI_UNUSED(&BROTLI_UNALIGNED_LOAD_PTR);
  458. BROTLI_UNUSED(&BROTLI_UNALIGNED_STORE_PTR);
  459. BROTLI_UNUSED(&BrotliRBit);
  460. BROTLI_UNUSED(&brotli_min_double);
  461. BROTLI_UNUSED(&brotli_max_double);
  462. BROTLI_UNUSED(&brotli_min_float);
  463. BROTLI_UNUSED(&brotli_max_float);
  464. BROTLI_UNUSED(&brotli_min_int);
  465. BROTLI_UNUSED(&brotli_max_int);
  466. BROTLI_UNUSED(&brotli_min_size_t);
  467. BROTLI_UNUSED(&brotli_max_size_t);
  468. BROTLI_UNUSED(&brotli_min_uint32_t);
  469. BROTLI_UNUSED(&brotli_max_uint32_t);
  470. BROTLI_UNUSED(&brotli_min_uint8_t);
  471. BROTLI_UNUSED(&brotli_max_uint8_t);
  472. BROTLI_UNUSED(&BrotliDefaultAllocFunc);
  473. BROTLI_UNUSED(&BrotliDefaultFreeFunc);
  474. #if BROTLI_ENABLE_DUMP
  475. BROTLI_UNUSED(&BrotliDump);
  476. #endif
  477. }
  478. #endif /* BROTLI_COMMON_PLATFORM_H_ */