compiler.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "portability_macros.h"
  13. /*-*******************************************************
  14. * Compiler specifics
  15. *********************************************************/
  16. /* force inlining */
  17. #if !defined(ZSTD_NO_INLINE)
  18. #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  19. # define INLINE_KEYWORD inline
  20. #else
  21. # define INLINE_KEYWORD
  22. #endif
  23. #define FORCE_INLINE_ATTR __attribute__((always_inline))
  24. #else
  25. #define INLINE_KEYWORD
  26. #define FORCE_INLINE_ATTR
  27. #endif
  28. /*
  29. On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
  30. This explicitly marks such functions as __cdecl so that the code will still compile
  31. if a CC other than __cdecl has been made the default.
  32. */
  33. #define WIN_CDECL
  34. /*
  35. * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
  36. * parameters. They must be inlined for the compiler to eliminate the constant
  37. * branches.
  38. */
  39. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
  40. /*
  41. * HINT_INLINE is used to help the compiler generate better code. It is *not*
  42. * used for "templates", so it can be tweaked based on the compilers
  43. * performance.
  44. *
  45. * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
  46. * always_inline attribute.
  47. *
  48. * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
  49. * attribute.
  50. */
  51. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
  52. # define HINT_INLINE static INLINE_KEYWORD
  53. #else
  54. # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
  55. #endif
  56. /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
  57. #define UNUSED_ATTR __attribute__((unused))
  58. /* force no inlining */
  59. #define FORCE_NOINLINE static __attribute__((__noinline__))
  60. /* target attribute */
  61. #define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
  62. /* Target attribute for BMI2 dynamic dispatch.
  63. * Enable lzcnt, bmi, and bmi2.
  64. * We test for bmi1 & bmi2. lzcnt is included in bmi1.
  65. */
  66. #define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2")
  67. /* prefetch
  68. * can be disabled, by declaring NO_PREFETCH build macro */
  69. #if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  70. # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  71. # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
  72. #elif defined(__aarch64__)
  73. # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
  74. # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
  75. #else
  76. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  77. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  78. #endif /* NO_PREFETCH */
  79. #define CACHELINE_SIZE 64
  80. #define PREFETCH_AREA(p, s) { \
  81. const char* const _ptr = (const char*)(p); \
  82. size_t const _size = (size_t)(s); \
  83. size_t _pos; \
  84. for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
  85. PREFETCH_L2(_ptr + _pos); \
  86. } \
  87. }
  88. /* vectorization
  89. * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax,
  90. * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */
  91. #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__)
  92. # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
  93. # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
  94. # else
  95. # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
  96. # endif
  97. #else
  98. # define DONT_VECTORIZE
  99. #endif
  100. /* Tell the compiler that a branch is likely or unlikely.
  101. * Only use these macros if it causes the compiler to generate better code.
  102. * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
  103. * and clang, please do.
  104. */
  105. #define LIKELY(x) (__builtin_expect((x), 1))
  106. #define UNLIKELY(x) (__builtin_expect((x), 0))
  107. #if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
  108. # define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); }
  109. #else
  110. # define ZSTD_UNREACHABLE { assert(0); }
  111. #endif
  112. /* disable warnings */
  113. /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/
  114. /* compile time determination of SIMD support */
  115. /* C-language Attributes are added in C23. */
  116. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
  117. # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
  118. #else
  119. # define ZSTD_HAS_C_ATTRIBUTE(x) 0
  120. #endif
  121. /* Only use C++ attributes in C++. Some compilers report support for C++
  122. * attributes when compiling with C.
  123. */
  124. #define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
  125. /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
  126. * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
  127. * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
  128. * - Else: __attribute__((__fallthrough__))
  129. */
  130. #define ZSTD_FALLTHROUGH fallthrough
  131. /*-**************************************************************
  132. * Alignment check
  133. *****************************************************************/
  134. /* this test was initially positioned in mem.h,
  135. * but this file is removed (or replaced) for linux kernel
  136. * so it's now hosted in compiler.h,
  137. * which remains valid for both user & kernel spaces.
  138. */
  139. #ifndef ZSTD_ALIGNOF
  140. /* covers gcc, clang & MSVC */
  141. /* note : this section must come first, before C11,
  142. * due to a limitation in the kernel source generator */
  143. # define ZSTD_ALIGNOF(T) __alignof(T)
  144. #endif /* ZSTD_ALIGNOF */
  145. /*-**************************************************************
  146. * Sanitizer
  147. *****************************************************************/
  148. #endif /* ZSTD_COMPILER_H */