compiler.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  17. # define INLINE_KEYWORD inline
  18. #else
  19. # define INLINE_KEYWORD
  20. #endif
  21. #if defined(__GNUC__)
  22. # define FORCE_INLINE_ATTR __attribute__((always_inline))
  23. #elif defined(_MSC_VER)
  24. # define FORCE_INLINE_ATTR __forceinline
  25. #else
  26. # define FORCE_INLINE_ATTR
  27. #endif
  28. /**
  29. * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
  30. * parameters. They must be inlined for the compiler to elimininate the constant
  31. * branches.
  32. */
  33. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
  34. /**
  35. * HINT_INLINE is used to help the compiler generate better code. It is *not*
  36. * used for "templates", so it can be tweaked based on the compilers
  37. * performance.
  38. *
  39. * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
  40. * always_inline attribute.
  41. *
  42. * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
  43. * attribute.
  44. */
  45. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
  46. # define HINT_INLINE static INLINE_KEYWORD
  47. #else
  48. # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
  49. #endif
  50. /* force no inlining */
  51. #ifdef _MSC_VER
  52. # define FORCE_NOINLINE static __declspec(noinline)
  53. #else
  54. # ifdef __GNUC__
  55. # define FORCE_NOINLINE static __attribute__((__noinline__))
  56. # else
  57. # define FORCE_NOINLINE static
  58. # endif
  59. #endif
  60. /* target attribute */
  61. #ifndef __has_attribute
  62. #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
  63. #endif
  64. #if defined(__GNUC__)
  65. # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
  66. #else
  67. # define TARGET_ATTRIBUTE(target)
  68. #endif
  69. /* Enable runtime BMI2 dispatch based on the CPU.
  70. * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
  71. */
  72. #ifndef DYNAMIC_BMI2
  73. #if (defined(__clang__) && __has_attribute(__target__)) \
  74. || (defined(__GNUC__) \
  75. && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) \
  76. && (defined(__x86_64__) || defined(_M_X86)) \
  77. && !defined(__BMI2__)
  78. # define DYNAMIC_BMI2 1
  79. #else
  80. # define DYNAMIC_BMI2 0
  81. #endif
  82. #endif
  83. /* prefetch */
  84. #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
  85. # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
  86. # define PREFETCH(ptr) _mm_prefetch((const char*)ptr, _MM_HINT_T0)
  87. #elif defined(__GNUC__)
  88. # define PREFETCH(ptr) __builtin_prefetch(ptr, 0, 0)
  89. #else
  90. # define PREFETCH(ptr) /* disabled */
  91. #endif
  92. /* disable warnings */
  93. #ifdef _MSC_VER /* Visual Studio */
  94. # include <intrin.h> /* For Visual 2005 */
  95. # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
  96. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  97. # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
  98. # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
  99. # pragma warning(disable : 4324) /* disable: C4324: padded structure */
  100. #endif
  101. #endif /* ZSTD_COMPILER_H */