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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  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. #if defined(__GNUC__) || defined(__ICCARM__)
  24. # define FORCE_INLINE_ATTR __attribute__((always_inline))
  25. #elif defined(_MSC_VER)
  26. # define FORCE_INLINE_ATTR __forceinline
  27. #else
  28. # define FORCE_INLINE_ATTR
  29. #endif
  30. #else
  31. #define INLINE_KEYWORD
  32. #define FORCE_INLINE_ATTR
  33. #endif
  34. /**
  35. On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
  36. This explicitly marks such functions as __cdecl so that the code will still compile
  37. if a CC other than __cdecl has been made the default.
  38. */
  39. #if defined(_MSC_VER)
  40. # define WIN_CDECL __cdecl
  41. #else
  42. # define WIN_CDECL
  43. #endif
  44. /**
  45. * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
  46. * parameters. They must be inlined for the compiler to eliminate the constant
  47. * branches.
  48. */
  49. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
  50. /**
  51. * HINT_INLINE is used to help the compiler generate better code. It is *not*
  52. * used for "templates", so it can be tweaked based on the compilers
  53. * performance.
  54. *
  55. * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
  56. * always_inline attribute.
  57. *
  58. * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
  59. * attribute.
  60. */
  61. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
  62. # define HINT_INLINE static INLINE_KEYWORD
  63. #else
  64. # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
  65. #endif
  66. /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
  67. #if defined(__GNUC__)
  68. # define UNUSED_ATTR __attribute__((unused))
  69. #else
  70. # define UNUSED_ATTR
  71. #endif
  72. /* force no inlining */
  73. #ifdef _MSC_VER
  74. # define FORCE_NOINLINE static __declspec(noinline)
  75. #else
  76. # if defined(__GNUC__) || defined(__ICCARM__)
  77. # define FORCE_NOINLINE static __attribute__((__noinline__))
  78. # else
  79. # define FORCE_NOINLINE static
  80. # endif
  81. #endif
  82. /* target attribute */
  83. #if defined(__GNUC__) || defined(__ICCARM__)
  84. # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
  85. #else
  86. # define TARGET_ATTRIBUTE(target)
  87. #endif
  88. /* Target attribute for BMI2 dynamic dispatch.
  89. * Enable lzcnt, bmi, and bmi2.
  90. * We test for bmi1 & bmi2. lzcnt is included in bmi1.
  91. */
  92. #define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2")
  93. /* prefetch
  94. * can be disabled, by declaring NO_PREFETCH build macro */
  95. #if defined(NO_PREFETCH)
  96. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  97. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  98. #else
  99. # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
  100. # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
  101. # define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
  102. # define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
  103. # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  104. # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  105. # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
  106. # elif defined(__aarch64__)
  107. # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
  108. # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
  109. # else
  110. # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
  111. # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
  112. # endif
  113. #endif /* NO_PREFETCH */
  114. #define CACHELINE_SIZE 64
  115. #define PREFETCH_AREA(p, s) { \
  116. const char* const _ptr = (const char*)(p); \
  117. size_t const _size = (size_t)(s); \
  118. size_t _pos; \
  119. for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
  120. PREFETCH_L2(_ptr + _pos); \
  121. } \
  122. }
  123. /* vectorization
  124. * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax,
  125. * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */
  126. #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__)
  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. #if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
  148. # define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); }
  149. #else
  150. # define ZSTD_UNREACHABLE { assert(0); }
  151. #endif
  152. /* disable warnings */
  153. #ifdef _MSC_VER /* Visual Studio */
  154. # include <intrin.h> /* For Visual 2005 */
  155. # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
  156. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  157. # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
  158. # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
  159. # pragma warning(disable : 4324) /* disable: C4324: padded structure */
  160. #endif
  161. /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/
  162. #ifndef STATIC_BMI2
  163. # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86))
  164. # ifdef __AVX2__ //MSVC does not have a BMI2 specific flag, but every CPU that supports AVX2 also supports BMI2
  165. # define STATIC_BMI2 1
  166. # endif
  167. # elif defined(__BMI2__) && defined(__x86_64__) && defined(__GNUC__)
  168. # define STATIC_BMI2 1
  169. # endif
  170. #endif
  171. #ifndef STATIC_BMI2
  172. #define STATIC_BMI2 0
  173. #endif
  174. /* compile time determination of SIMD support */
  175. #if !defined(ZSTD_NO_INTRINSICS)
  176. # if defined(__SSE2__) || defined(_M_AMD64) || (defined (_M_IX86) && defined(_M_IX86_FP) && (_M_IX86_FP >= 2))
  177. # define ZSTD_ARCH_X86_SSE2
  178. # endif
  179. # if defined(__ARM_NEON) || defined(_M_ARM64)
  180. # define ZSTD_ARCH_ARM_NEON
  181. # endif
  182. #
  183. # if defined(ZSTD_ARCH_X86_SSE2)
  184. # include <emmintrin.h>
  185. # elif defined(ZSTD_ARCH_ARM_NEON)
  186. # include <arm_neon.h>
  187. # endif
  188. #endif
  189. /* C-language Attributes are added in C23. */
  190. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
  191. # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
  192. #else
  193. # define ZSTD_HAS_C_ATTRIBUTE(x) 0
  194. #endif
  195. /* Only use C++ attributes in C++. Some compilers report support for C++
  196. * attributes when compiling with C.
  197. */
  198. #if defined(__cplusplus) && defined(__has_cpp_attribute)
  199. # define ZSTD_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
  200. #else
  201. # define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
  202. #endif
  203. /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
  204. * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
  205. * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
  206. * - Else: __attribute__((__fallthrough__))
  207. */
  208. #ifndef ZSTD_FALLTHROUGH
  209. # if ZSTD_HAS_C_ATTRIBUTE(fallthrough)
  210. # define ZSTD_FALLTHROUGH [[fallthrough]]
  211. # elif ZSTD_HAS_CPP_ATTRIBUTE(fallthrough)
  212. # define ZSTD_FALLTHROUGH [[fallthrough]]
  213. # elif __has_attribute(__fallthrough__)
  214. /* Leading semicolon is to satisfy gcc-11 with -pedantic. Without the semicolon
  215. * gcc complains about: a label can only be part of a statement and a declaration is not a statement.
  216. */
  217. # define ZSTD_FALLTHROUGH ; __attribute__((__fallthrough__))
  218. # else
  219. # define ZSTD_FALLTHROUGH
  220. # endif
  221. #endif
  222. /*-**************************************************************
  223. * Alignment check
  224. *****************************************************************/
  225. /* this test was initially positioned in mem.h,
  226. * but this file is removed (or replaced) for linux kernel
  227. * so it's now hosted in compiler.h,
  228. * which remains valid for both user & kernel spaces.
  229. */
  230. #ifndef ZSTD_ALIGNOF
  231. # if defined(__GNUC__) || defined(_MSC_VER)
  232. /* covers gcc, clang & MSVC */
  233. /* note : this section must come first, before C11,
  234. * due to a limitation in the kernel source generator */
  235. # define ZSTD_ALIGNOF(T) __alignof(T)
  236. # elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
  237. /* C11 support */
  238. # include <stdalign.h>
  239. # define ZSTD_ALIGNOF(T) alignof(T)
  240. # else
  241. /* No known support for alignof() - imperfect backup */
  242. # define ZSTD_ALIGNOF(T) (sizeof(void*) < sizeof(T) ? sizeof(void*) : sizeof(T))
  243. # endif
  244. #endif /* ZSTD_ALIGNOF */
  245. /*-**************************************************************
  246. * Sanitizer
  247. *****************************************************************/
  248. /* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an
  249. * abundance of caution, disable our custom poisoning on mingw. */
  250. #ifdef __MINGW32__
  251. #ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE
  252. #define ZSTD_ASAN_DONT_POISON_WORKSPACE 1
  253. #endif
  254. #ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE
  255. #define ZSTD_MSAN_DONT_POISON_WORKSPACE 1
  256. #endif
  257. #endif
  258. #if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)
  259. /* Not all platforms that support msan provide sanitizers/msan_interface.h.
  260. * We therefore declare the functions we need ourselves, rather than trying to
  261. * include the header file... */
  262. #include <stddef.h> /* size_t */
  263. #define ZSTD_DEPS_NEED_STDINT
  264. #include "zstd_deps.h" /* intptr_t */
  265. /* Make memory region fully initialized (without changing its contents). */
  266. void __msan_unpoison(const volatile void *a, size_t size);
  267. /* Make memory region fully uninitialized (without changing its contents).
  268. This is a legacy interface that does not update origin information. Use
  269. __msan_allocated_memory() instead. */
  270. void __msan_poison(const volatile void *a, size_t size);
  271. /* Returns the offset of the first (at least partially) poisoned byte in the
  272. memory range, or -1 if the whole range is good. */
  273. intptr_t __msan_test_shadow(const volatile void *x, size_t size);
  274. #endif
  275. #if ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE)
  276. /* Not all platforms that support asan provide sanitizers/asan_interface.h.
  277. * We therefore declare the functions we need ourselves, rather than trying to
  278. * include the header file... */
  279. #include <stddef.h> /* size_t */
  280. /**
  281. * Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.
  282. *
  283. * This memory must be previously allocated by your program. Instrumented
  284. * code is forbidden from accessing addresses in this region until it is
  285. * unpoisoned. This function is not guaranteed to poison the entire region -
  286. * it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan
  287. * alignment restrictions.
  288. *
  289. * \note This function is not thread-safe because no two threads can poison or
  290. * unpoison memory in the same memory region simultaneously.
  291. *
  292. * \param addr Start of memory region.
  293. * \param size Size of memory region. */
  294. void __asan_poison_memory_region(void const volatile *addr, size_t size);
  295. /**
  296. * Marks a memory region (<c>[addr, addr+size)</c>) as addressable.
  297. *
  298. * This memory must be previously allocated by your program. Accessing
  299. * addresses in this region is allowed until this region is poisoned again.
  300. * This function could unpoison a super-region of <c>[addr, addr+size)</c> due
  301. * to ASan alignment restrictions.
  302. *
  303. * \note This function is not thread-safe because no two threads can
  304. * poison or unpoison memory in the same memory region simultaneously.
  305. *
  306. * \param addr Start of memory region.
  307. * \param size Size of memory region. */
  308. void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
  309. #endif
  310. #endif /* ZSTD_COMPILER_H */