port.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright 2015 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_MODERN_COMPILER forces to use modern compilers built-ins,
  13. features and attributes
  14. * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned
  15. read and overlapping memcpy; this reduces decompression speed by 5%
  16. * BROTLI_DEBUG dumps file name and line number when decoder detects stream
  17. or memory error
  18. * BROTLI_ENABLE_LOG enables asserts and dumps various state information
  19. */
  20. #ifndef BROTLI_DEC_PORT_H_
  21. #define BROTLI_DEC_PORT_H_
  22. #if defined(BROTLI_ENABLE_LOG) || defined(BROTLI_DEBUG)
  23. #include <assert.h>
  24. #include <stdio.h>
  25. #endif
  26. #include "../common/port.h"
  27. #if defined(__arm__) || defined(__thumb__) || \
  28. defined(_M_ARM) || defined(_M_ARMT)
  29. #define BROTLI_TARGET_ARM
  30. #if (defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) || \
  31. (defined(M_ARM) && (M_ARM >= 7))
  32. #define BROTLI_TARGET_ARMV7
  33. #endif /* ARMv7 */
  34. #if defined(__aarch64__)
  35. #define BROTLI_TARGET_ARMV8
  36. #endif /* ARMv8 */
  37. #endif /* ARM */
  38. #if defined(__i386) || defined(_M_IX86)
  39. #define BROTLI_TARGET_X86
  40. #endif
  41. #if defined(__x86_64__) || defined(_M_X64)
  42. #define BROTLI_TARGET_X64
  43. #endif
  44. #if defined(__PPC64__)
  45. #define BROTLI_TARGET_POWERPC64
  46. #endif
  47. #ifdef BROTLI_BUILD_PORTABLE
  48. #define BROTLI_ALIGNED_READ (!!1)
  49. #elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
  50. defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8)
  51. /* Allow unaligned read only for whitelisted CPUs. */
  52. #define BROTLI_ALIGNED_READ (!!0)
  53. #else
  54. #define BROTLI_ALIGNED_READ (!!1)
  55. #endif
  56. /* IS_CONSTANT macros returns true for compile-time constant expressions. */
  57. #if BROTLI_MODERN_COMPILER || __has_builtin(__builtin_constant_p)
  58. #define IS_CONSTANT(x) (!!__builtin_constant_p(x))
  59. #else
  60. #define IS_CONSTANT(x) (!!0)
  61. #endif
  62. #ifdef BROTLI_ENABLE_LOG
  63. #define BROTLI_DCHECK(x) assert(x)
  64. #define BROTLI_LOG(x) printf x
  65. #else
  66. #define BROTLI_DCHECK(x)
  67. #define BROTLI_LOG(x)
  68. #endif
  69. #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
  70. static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
  71. fprintf(stderr, "%s:%d (%s)\n", f, l, fn);
  72. fflush(stderr);
  73. }
  74. #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
  75. #else
  76. #define BROTLI_DUMP() (void)(0)
  77. #endif
  78. #if defined(BROTLI_BUILD_64_BIT)
  79. #define BROTLI_64_BITS 1
  80. #elif defined(BROTLI_BUILD_32_BIT)
  81. #define BROTLI_64_BITS 0
  82. #elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8) || \
  83. defined(BROTLI_TARGET_POWERPC64)
  84. #define BROTLI_64_BITS 1
  85. #else
  86. #define BROTLI_64_BITS 0
  87. #endif
  88. #if defined(BROTLI_BUILD_BIG_ENDIAN)
  89. #define BROTLI_LITTLE_ENDIAN 0
  90. #define BROTLI_BIG_ENDIAN 1
  91. #elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
  92. #define BROTLI_LITTLE_ENDIAN 1
  93. #define BROTLI_BIG_ENDIAN 0
  94. #elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
  95. #define BROTLI_LITTLE_ENDIAN 0
  96. #define BROTLI_BIG_ENDIAN 0
  97. #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  98. #define BROTLI_LITTLE_ENDIAN 1
  99. #define BROTLI_BIG_ENDIAN 0
  100. #elif defined(_WIN32)
  101. /* Win32 can currently always be assumed to be little endian */
  102. #define BROTLI_LITTLE_ENDIAN 1
  103. #define BROTLI_BIG_ENDIAN 0
  104. #else
  105. #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  106. #define BROTLI_BIG_ENDIAN 1
  107. #else
  108. #define BROTLI_BIG_ENDIAN 0
  109. #endif
  110. #define BROTLI_LITTLE_ENDIAN 0
  111. #endif
  112. #define BROTLI_REPEAT(N, X) { \
  113. if ((N & 1) != 0) {X;} \
  114. if ((N & 2) != 0) {X; X;} \
  115. if ((N & 4) != 0) {X; X; X; X;} \
  116. }
  117. #if BROTLI_MODERN_COMPILER || defined(__llvm__)
  118. #if defined(BROTLI_TARGET_ARMV7)
  119. static BROTLI_INLINE unsigned BrotliRBit(unsigned input) {
  120. unsigned output;
  121. __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
  122. return output;
  123. }
  124. #define BROTLI_RBIT(x) BrotliRBit(x)
  125. #endif /* armv7 */
  126. #endif /* gcc || clang */
  127. #if defined(BROTLI_TARGET_ARM)
  128. #define BROTLI_HAS_UBFX (!!1)
  129. #else
  130. #define BROTLI_HAS_UBFX (!!0)
  131. #endif
  132. #define BROTLI_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L)
  133. #define BROTLI_FREE(S, X) { \
  134. S->free_func(S->memory_manager_opaque, X); \
  135. X = NULL; \
  136. }
  137. #endif /* BROTLI_DEC_PORT_H_ */