port.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #ifndef BROTLI_COMMON_PORT_H_
  7. #define BROTLI_COMMON_PORT_H_
  8. /* Compatibility with non-clang compilers. */
  9. #ifndef __has_builtin
  10. #define __has_builtin(x) 0
  11. #endif
  12. #ifndef __has_attribute
  13. #define __has_attribute(x) 0
  14. #endif
  15. #ifndef __has_feature
  16. #define __has_feature(x) 0
  17. #endif
  18. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  19. #define BROTLI_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  20. #else
  21. #define BROTLI_GCC_VERSION 0
  22. #endif
  23. #if defined(__ICC)
  24. #define BROTLI_ICC_VERSION __ICC
  25. #else
  26. #define BROTLI_ICC_VERSION 0
  27. #endif
  28. #if defined(BROTLI_BUILD_MODERN_COMPILER)
  29. #define BROTLI_MODERN_COMPILER 1
  30. #elif BROTLI_GCC_VERSION >= 304 || BROTLI_ICC_VERSION >= 1600
  31. #define BROTLI_MODERN_COMPILER 1
  32. #else
  33. #define BROTLI_MODERN_COMPILER 0
  34. #endif
  35. /* Define "BROTLI_PREDICT_TRUE" and "BROTLI_PREDICT_FALSE" macros for capable
  36. compilers.
  37. To apply compiler hint, enclose the branching condition into macros, like this:
  38. if (BROTLI_PREDICT_TRUE(zero == 0)) {
  39. // main execution path
  40. } else {
  41. // compiler should place this code outside of main execution path
  42. }
  43. OR:
  44. if (BROTLI_PREDICT_FALSE(something_rare_or_unexpected_happens)) {
  45. // compiler should place this code outside of main execution path
  46. }
  47. */
  48. #if BROTLI_MODERN_COMPILER || __has_builtin(__builtin_expect)
  49. #define BROTLI_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
  50. #define BROTLI_PREDICT_FALSE(x) (__builtin_expect(x, 0))
  51. #else
  52. #define BROTLI_PREDICT_FALSE(x) (x)
  53. #define BROTLI_PREDICT_TRUE(x) (x)
  54. #endif
  55. #if BROTLI_MODERN_COMPILER || __has_attribute(always_inline)
  56. #define BROTLI_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
  57. #else
  58. #define BROTLI_ATTRIBUTE_ALWAYS_INLINE
  59. #endif
  60. #if defined(_WIN32) || defined(__CYGWIN__)
  61. #define BROTLI_ATTRIBUTE_VISIBILITY_HIDDEN
  62. #elif BROTLI_MODERN_COMPILER || __has_attribute(visibility)
  63. #define BROTLI_ATTRIBUTE_VISIBILITY_HIDDEN \
  64. __attribute__ ((visibility ("hidden")))
  65. #else
  66. #define BROTLI_ATTRIBUTE_VISIBILITY_HIDDEN
  67. #endif
  68. #ifndef BROTLI_INTERNAL
  69. #define BROTLI_INTERNAL BROTLI_ATTRIBUTE_VISIBILITY_HIDDEN
  70. #endif
  71. #if defined(BROTLI_SHARED_COMPILATION) && defined(_WIN32)
  72. #if defined(BROTLICOMMON_SHARED_COMPILATION)
  73. #define BROTLI_COMMON_API __declspec(dllexport)
  74. #else
  75. #define BROTLI_COMMON_API __declspec(dllimport)
  76. #endif /* BROTLICOMMON_SHARED_COMPILATION */
  77. #if defined(BROTLIDEC_SHARED_COMPILATION)
  78. #define BROTLI_DEC_API __declspec(dllexport)
  79. #else
  80. #define BROTLI_DEC_API __declspec(dllimport)
  81. #endif /* BROTLIDEC_SHARED_COMPILATION */
  82. #if defined(BROTLIENC_SHARED_COMPILATION)
  83. #define BROTLI_ENC_API __declspec(dllexport)
  84. #else
  85. #define BROTLI_ENC_API __declspec(dllimport)
  86. #endif /* BROTLIENC_SHARED_COMPILATION */
  87. #else /* BROTLI_SHARED_COMPILATION && _WIN32 */
  88. #define BROTLI_COMMON_API
  89. #define BROTLI_DEC_API
  90. #define BROTLI_ENC_API
  91. #endif
  92. #ifndef _MSC_VER
  93. #if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \
  94. (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
  95. #define BROTLI_INLINE inline BROTLI_ATTRIBUTE_ALWAYS_INLINE
  96. #else
  97. #define BROTLI_INLINE
  98. #endif
  99. #else /* _MSC_VER */
  100. #define BROTLI_INLINE __forceinline
  101. #endif /* _MSC_VER */
  102. #if !defined(__cplusplus) && !defined(c_plusplus) && \
  103. (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
  104. #define BROTLI_RESTRICT restrict
  105. #elif BROTLI_GCC_VERSION > 295 || defined(__llvm__)
  106. #define BROTLI_RESTRICT __restrict
  107. #else
  108. #define BROTLI_RESTRICT
  109. #endif
  110. #if BROTLI_MODERN_COMPILER || __has_attribute(noinline)
  111. #define BROTLI_NOINLINE __attribute__((noinline))
  112. #else
  113. #define BROTLI_NOINLINE
  114. #endif
  115. #if BROTLI_MODERN_COMPILER || __has_attribute(deprecated)
  116. #define BROTLI_DEPRECATED __attribute__((deprecated))
  117. #else
  118. #define BROTLI_DEPRECATED
  119. #endif
  120. #define BROTLI_UNUSED(X) (void)(X)
  121. #endif /* BROTLI_COMMON_PORT_H_ */