You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiler.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2016-2020, 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(__STRICT_ANSI__)) || 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(__aarch64__)
  105. # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
  106. # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
  107. # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  108. # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  109. # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
  110. # else
  111. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  112. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  113. # endif
  114. #endif /* NO_PREFETCH */
  115. #define CACHELINE_SIZE 64
  116. #define PREFETCH_AREA(p, s) { \
  117. const char* const _ptr = (const char*)(p); \
  118. size_t const _size = (size_t)(s); \
  119. size_t _pos; \
  120. for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
  121. PREFETCH_L2(_ptr + _pos); \
  122. } \
  123. }
  124. /* vectorization
  125. * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */
  126. #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)
  127. # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
  128. # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
  129. # else
  130. # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
  131. # endif
  132. #else
  133. # define DONT_VECTORIZE
  134. #endif
  135. /* Tell the compiler that a branch is likely or unlikely.
  136. * Only use these macros if it causes the compiler to generate better code.
  137. * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
  138. * and clang, please do.
  139. */
  140. #if defined(__GNUC__)
  141. #define LIKELY(x) (__builtin_expect((x), 1))
  142. #define UNLIKELY(x) (__builtin_expect((x), 0))
  143. #else
  144. #define LIKELY(x) (x)
  145. #define UNLIKELY(x) (x)
  146. #endif
  147. /* disable warnings */
  148. #ifdef _MSC_VER /* Visual Studio */
  149. # include <intrin.h> /* For Visual 2005 */
  150. # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
  151. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  152. # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
  153. # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
  154. # pragma warning(disable : 4324) /* disable: C4324: padded structure */
  155. #endif
  156. #endif /* ZSTD_COMPILER_H */