compiler.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 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. #define FORCE_INLINE_ATTR __attribute__((always_inline))
  23. #else
  24. #define INLINE_KEYWORD
  25. #define FORCE_INLINE_ATTR
  26. #endif
  27. /*
  28. On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
  29. This explictly marks such functions as __cdecl so that the code will still compile
  30. if a CC other than __cdecl has been made the default.
  31. */
  32. #define WIN_CDECL
  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. #define UNUSED_ATTR __attribute__((unused))
  57. /* force no inlining */
  58. #define FORCE_NOINLINE static __attribute__((__noinline__))
  59. /* target attribute */
  60. #ifndef __has_attribute
  61. #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
  62. #endif
  63. #define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
  64. /* Enable runtime BMI2 dispatch based on the CPU.
  65. * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
  66. */
  67. #ifndef DYNAMIC_BMI2
  68. #if ((defined(__clang__) && __has_attribute(__target__)) \
  69. || (defined(__GNUC__) \
  70. && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
  71. && (defined(__x86_64__) || defined(_M_X86)) \
  72. && !defined(__BMI2__)
  73. # define DYNAMIC_BMI2 1
  74. #else
  75. # define DYNAMIC_BMI2 0
  76. #endif
  77. #endif
  78. /* prefetch
  79. * can be disabled, by declaring NO_PREFETCH build macro */
  80. #if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  81. # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  82. # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
  83. #elif defined(__aarch64__)
  84. # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
  85. # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
  86. #else
  87. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  88. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  89. #endif /* NO_PREFETCH */
  90. #define CACHELINE_SIZE 64
  91. #define PREFETCH_AREA(p, s) { \
  92. const char* const _ptr = (const char*)(p); \
  93. size_t const _size = (size_t)(s); \
  94. size_t _pos; \
  95. for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
  96. PREFETCH_L2(_ptr + _pos); \
  97. } \
  98. }
  99. /* vectorization
  100. * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */
  101. #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)
  102. # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
  103. # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
  104. # else
  105. # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
  106. # endif
  107. #else
  108. # define DONT_VECTORIZE
  109. #endif
  110. /* Tell the compiler that a branch is likely or unlikely.
  111. * Only use these macros if it causes the compiler to generate better code.
  112. * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
  113. * and clang, please do.
  114. */
  115. #define LIKELY(x) (__builtin_expect((x), 1))
  116. #define UNLIKELY(x) (__builtin_expect((x), 0))
  117. /* disable warnings */
  118. /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/
  119. /* compat. with non-clang compilers */
  120. #ifndef __has_builtin
  121. # define __has_builtin(x) 0
  122. #endif
  123. /* compat. with non-clang compilers */
  124. #ifndef __has_feature
  125. # define __has_feature(x) 0
  126. #endif
  127. /* C-language Attributes are added in C23. */
  128. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
  129. # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
  130. #else
  131. # define ZSTD_HAS_C_ATTRIBUTE(x) 0
  132. #endif
  133. /* Only use C++ attributes in C++. Some compilers report support for C++
  134. * attributes when compiling with C.
  135. */
  136. #define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
  137. /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
  138. * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
  139. * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
  140. * - Else: __attribute__((__fallthrough__))
  141. */
  142. #define ZSTD_FALLTHROUGH fallthrough
  143. /* detects whether we are being compiled under msan */
  144. /* detects whether we are being compiled under asan */
  145. #endif /* ZSTD_COMPILER_H */