compiler.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 ZSTD_COMPILER_H
  11. #define ZSTD_COMPILER_H
  12. /*-*******************************************************
  13. * Compiler specifics
  14. *********************************************************/
  15. /* force inlining */
  16. #if !defined(ZSTD_NO_INLINE)
  17. #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  18. # define INLINE_KEYWORD inline
  19. #else
  20. # define INLINE_KEYWORD
  21. #endif
  22. #if defined(__GNUC__) || defined(__ICCARM__)
  23. # define FORCE_INLINE_ATTR __attribute__((always_inline))
  24. #elif defined(_MSC_VER)
  25. # define FORCE_INLINE_ATTR __forceinline
  26. #else
  27. # define FORCE_INLINE_ATTR
  28. #endif
  29. #else
  30. #define INLINE_KEYWORD
  31. #define FORCE_INLINE_ATTR
  32. #endif
  33. /**
  34. * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
  35. * parameters. They must be inlined for the compiler to eliminate the constant
  36. * branches.
  37. */
  38. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
  39. /**
  40. * HINT_INLINE is used to help the compiler generate better code. It is *not*
  41. * used for "templates", so it can be tweaked based on the compilers
  42. * performance.
  43. *
  44. * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
  45. * always_inline attribute.
  46. *
  47. * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
  48. * attribute.
  49. */
  50. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
  51. # define HINT_INLINE static INLINE_KEYWORD
  52. #else
  53. # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
  54. #endif
  55. /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
  56. #if defined(__GNUC__)
  57. # define UNUSED_ATTR __attribute__((unused))
  58. #else
  59. # define UNUSED_ATTR
  60. #endif
  61. /* force no inlining */
  62. #ifdef _MSC_VER
  63. # define FORCE_NOINLINE static __declspec(noinline)
  64. #else
  65. # if defined(__GNUC__) || defined(__ICCARM__)
  66. # define FORCE_NOINLINE static __attribute__((__noinline__))
  67. # else
  68. # define FORCE_NOINLINE static
  69. # endif
  70. #endif
  71. /* target attribute */
  72. #ifndef __has_attribute
  73. #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
  74. #endif
  75. #if defined(__GNUC__) || defined(__ICCARM__)
  76. # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
  77. #else
  78. # define TARGET_ATTRIBUTE(target)
  79. #endif
  80. /* Enable runtime BMI2 dispatch based on the CPU.
  81. * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
  82. */
  83. #ifndef DYNAMIC_BMI2
  84. #if ((defined(__clang__) && __has_attribute(__target__)) \
  85. || (defined(__GNUC__) \
  86. && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
  87. && (defined(__x86_64__) || defined(_M_X86)) \
  88. && !defined(__BMI2__)
  89. # define DYNAMIC_BMI2 1
  90. #else
  91. # define DYNAMIC_BMI2 0
  92. #endif
  93. #endif
  94. /* prefetch
  95. * can be disabled, by declaring NO_PREFETCH build macro */
  96. #if defined(NO_PREFETCH)
  97. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  98. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  99. #else
  100. # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
  101. # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
  102. # define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
  103. # define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
  104. # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  105. # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  106. # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
  107. # else
  108. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  109. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  110. # endif
  111. #endif /* NO_PREFETCH */
  112. #define CACHELINE_SIZE 64
  113. #define PREFETCH_AREA(p, s) { \
  114. const char* const _ptr = (const char*)(p); \
  115. size_t const _size = (size_t)(s); \
  116. size_t _pos; \
  117. for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
  118. PREFETCH_L2(_ptr + _pos); \
  119. } \
  120. }
  121. /* vectorization
  122. * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */
  123. #if !defined(__clang__) && defined(__GNUC__)
  124. # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
  125. # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
  126. # else
  127. # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
  128. # endif
  129. #else
  130. # define DONT_VECTORIZE
  131. #endif
  132. /* disable warnings */
  133. #ifdef _MSC_VER /* Visual Studio */
  134. # include <intrin.h> /* For Visual 2005 */
  135. # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
  136. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  137. # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
  138. # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
  139. # pragma warning(disable : 4324) /* disable: C4324: padded structure */
  140. #endif
  141. #endif /* ZSTD_COMPILER_H */