compiler.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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__)
  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 elimininate 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. /* force no inlining */
  56. #ifdef _MSC_VER
  57. # define FORCE_NOINLINE static __declspec(noinline)
  58. #else
  59. # ifdef __GNUC__
  60. # define FORCE_NOINLINE static __attribute__((__noinline__))
  61. # else
  62. # define FORCE_NOINLINE static
  63. # endif
  64. #endif
  65. /* target attribute */
  66. #ifndef __has_attribute
  67. #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
  68. #endif
  69. #if defined(__GNUC__)
  70. # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
  71. #else
  72. # define TARGET_ATTRIBUTE(target)
  73. #endif
  74. /* Enable runtime BMI2 dispatch based on the CPU.
  75. * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
  76. */
  77. #ifndef DYNAMIC_BMI2
  78. #if ((defined(__clang__) && __has_attribute(__target__)) \
  79. || (defined(__GNUC__) \
  80. && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
  81. && (defined(__x86_64__) || defined(_M_X86)) \
  82. && !defined(__BMI2__)
  83. # define DYNAMIC_BMI2 1
  84. #else
  85. # define DYNAMIC_BMI2 0
  86. #endif
  87. #endif
  88. /* prefetch
  89. * can be disabled, by declaring NO_PREFETCH build macro */
  90. #if defined(NO_PREFETCH)
  91. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  92. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  93. #else
  94. # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
  95. # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
  96. # define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
  97. # define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
  98. # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  99. # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  100. # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
  101. # else
  102. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  103. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  104. # endif
  105. #endif /* NO_PREFETCH */
  106. #define CACHELINE_SIZE 64
  107. #define PREFETCH_AREA(p, s) { \
  108. const char* const _ptr = (const char*)(p); \
  109. size_t const _size = (size_t)(s); \
  110. size_t _pos; \
  111. for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
  112. PREFETCH_L2(_ptr + _pos); \
  113. } \
  114. }
  115. /* disable warnings */
  116. #ifdef _MSC_VER /* Visual Studio */
  117. # include <intrin.h> /* For Visual 2005 */
  118. # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
  119. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  120. # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
  121. # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
  122. # pragma warning(disable : 4324) /* disable: C4324: padded structure */
  123. #endif
  124. #endif /* ZSTD_COMPILER_H */