compiler.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */
  2. /*
  3. * Copyright (c) Meta Platforms, Inc. and affiliates.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under both the BSD-style license (found in the
  7. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  8. * in the COPYING file in the root directory of this source tree).
  9. * You may select, at your option, one of the above-listed licenses.
  10. */
  11. #ifndef ZSTD_COMPILER_H
  12. #define ZSTD_COMPILER_H
  13. #include <linux/types.h>
  14. #include "portability_macros.h"
  15. /*-*******************************************************
  16. * Compiler specifics
  17. *********************************************************/
  18. /* force inlining */
  19. #if !defined(ZSTD_NO_INLINE)
  20. #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
  21. # define INLINE_KEYWORD inline
  22. #else
  23. # define INLINE_KEYWORD
  24. #endif
  25. #define FORCE_INLINE_ATTR __attribute__((always_inline))
  26. #else
  27. #define INLINE_KEYWORD
  28. #define FORCE_INLINE_ATTR
  29. #endif
  30. /*
  31. On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
  32. This explicitly marks such functions as __cdecl so that the code will still compile
  33. if a CC other than __cdecl has been made the default.
  34. */
  35. #define WIN_CDECL
  36. /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
  37. #define UNUSED_ATTR __attribute__((unused))
  38. /*
  39. * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
  40. * parameters. They must be inlined for the compiler to eliminate the constant
  41. * branches.
  42. */
  43. #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR UNUSED_ATTR
  44. /*
  45. * HINT_INLINE is used to help the compiler generate better code. It is *not*
  46. * used for "templates", so it can be tweaked based on the compilers
  47. * performance.
  48. *
  49. * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
  50. * always_inline attribute.
  51. *
  52. * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
  53. * attribute.
  54. */
  55. #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
  56. # define HINT_INLINE static INLINE_KEYWORD
  57. #else
  58. # define HINT_INLINE FORCE_INLINE_TEMPLATE
  59. #endif
  60. /* "soft" inline :
  61. * The compiler is free to select if it's a good idea to inline or not.
  62. * The main objective is to silence compiler warnings
  63. * when a defined function in included but not used.
  64. *
  65. * Note : this macro is prefixed `MEM_` because it used to be provided by `mem.h` unit.
  66. * Updating the prefix is probably preferable, but requires a fairly large codemod,
  67. * since this name is used everywhere.
  68. */
  69. #ifndef MEM_STATIC /* already defined in Linux Kernel mem.h */
  70. #define MEM_STATIC static __inline UNUSED_ATTR
  71. #endif
  72. /* force no inlining */
  73. #define FORCE_NOINLINE static __attribute__((__noinline__))
  74. /* target attribute */
  75. #define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
  76. /* Target attribute for BMI2 dynamic dispatch.
  77. * Enable lzcnt, bmi, and bmi2.
  78. * We test for bmi1 & bmi2. lzcnt is included in bmi1.
  79. */
  80. #define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2")
  81. /* prefetch
  82. * can be disabled, by declaring NO_PREFETCH build macro */
  83. #if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
  84. # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
  85. # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
  86. #elif defined(__aarch64__)
  87. # define PREFETCH_L1(ptr) do { __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))); } while (0)
  88. # define PREFETCH_L2(ptr) do { __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))); } while (0)
  89. #else
  90. # define PREFETCH_L1(ptr) do { (void)(ptr); } while (0) /* disabled */
  91. # define PREFETCH_L2(ptr) do { (void)(ptr); } while (0) /* disabled */
  92. #endif /* NO_PREFETCH */
  93. #define CACHELINE_SIZE 64
  94. #define PREFETCH_AREA(p, s) \
  95. do { \
  96. const char* const _ptr = (const char*)(p); \
  97. size_t const _size = (size_t)(s); \
  98. size_t _pos; \
  99. for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
  100. PREFETCH_L2(_ptr + _pos); \
  101. } \
  102. } while (0)
  103. /* vectorization
  104. * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax,
  105. * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */
  106. #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__)
  107. # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
  108. # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
  109. # else
  110. # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
  111. # endif
  112. #else
  113. # define DONT_VECTORIZE
  114. #endif
  115. /* Tell the compiler that a branch is likely or unlikely.
  116. * Only use these macros if it causes the compiler to generate better code.
  117. * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
  118. * and clang, please do.
  119. */
  120. #define LIKELY(x) (__builtin_expect((x), 1))
  121. #define UNLIKELY(x) (__builtin_expect((x), 0))
  122. #if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
  123. # define ZSTD_UNREACHABLE do { assert(0), __builtin_unreachable(); } while (0)
  124. #else
  125. # define ZSTD_UNREACHABLE do { assert(0); } while (0)
  126. #endif
  127. /* disable warnings */
  128. /* compile time determination of SIMD support */
  129. /* C-language Attributes are added in C23. */
  130. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
  131. # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
  132. #else
  133. # define ZSTD_HAS_C_ATTRIBUTE(x) 0
  134. #endif
  135. /* Only use C++ attributes in C++. Some compilers report support for C++
  136. * attributes when compiling with C.
  137. */
  138. #define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
  139. /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
  140. * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
  141. * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
  142. * - Else: __attribute__((__fallthrough__))
  143. */
  144. #define ZSTD_FALLTHROUGH fallthrough
  145. /*-**************************************************************
  146. * Alignment
  147. *****************************************************************/
  148. /* @return 1 if @u is a 2^n value, 0 otherwise
  149. * useful to check a value is valid for alignment restrictions */
  150. MEM_STATIC int ZSTD_isPower2(size_t u) {
  151. return (u & (u-1)) == 0;
  152. }
  153. /* this test was initially positioned in mem.h,
  154. * but this file is removed (or replaced) for linux kernel
  155. * so it's now hosted in compiler.h,
  156. * which remains valid for both user & kernel spaces.
  157. */
  158. #ifndef ZSTD_ALIGNOF
  159. /* covers gcc, clang & MSVC */
  160. /* note : this section must come first, before C11,
  161. * due to a limitation in the kernel source generator */
  162. # define ZSTD_ALIGNOF(T) __alignof(T)
  163. #endif /* ZSTD_ALIGNOF */
  164. #ifndef ZSTD_ALIGNED
  165. /* C90-compatible alignment macro (GCC/Clang). Adjust for other compilers if needed. */
  166. #define ZSTD_ALIGNED(a) __attribute__((aligned(a)))
  167. #endif /* ZSTD_ALIGNED */
  168. /*-**************************************************************
  169. * Sanitizer
  170. *****************************************************************/
  171. /*
  172. * Zstd relies on pointer overflow in its decompressor.
  173. * We add this attribute to functions that rely on pointer overflow.
  174. */
  175. #ifndef ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  176. # if __has_attribute(no_sanitize)
  177. # if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 8
  178. /* gcc < 8 only has signed-integer-overlow which triggers on pointer overflow */
  179. # define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR __attribute__((no_sanitize("signed-integer-overflow")))
  180. # else
  181. /* older versions of clang [3.7, 5.0) will warn that pointer-overflow is ignored. */
  182. # define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR __attribute__((no_sanitize("pointer-overflow")))
  183. # endif
  184. # else
  185. # define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  186. # endif
  187. #endif
  188. /*
  189. * Helper function to perform a wrapped pointer difference without triggering
  190. * UBSAN.
  191. *
  192. * @returns lhs - rhs with wrapping
  193. */
  194. MEM_STATIC
  195. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  196. ptrdiff_t ZSTD_wrappedPtrDiff(unsigned char const* lhs, unsigned char const* rhs)
  197. {
  198. return lhs - rhs;
  199. }
  200. /*
  201. * Helper function to perform a wrapped pointer add without triggering UBSAN.
  202. *
  203. * @return ptr + add with wrapping
  204. */
  205. MEM_STATIC
  206. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  207. unsigned char const* ZSTD_wrappedPtrAdd(unsigned char const* ptr, ptrdiff_t add)
  208. {
  209. return ptr + add;
  210. }
  211. /*
  212. * Helper function to perform a wrapped pointer subtraction without triggering
  213. * UBSAN.
  214. *
  215. * @return ptr - sub with wrapping
  216. */
  217. MEM_STATIC
  218. ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
  219. unsigned char const* ZSTD_wrappedPtrSub(unsigned char const* ptr, ptrdiff_t sub)
  220. {
  221. return ptr - sub;
  222. }
  223. /*
  224. * Helper function to add to a pointer that works around C's undefined behavior
  225. * of adding 0 to NULL.
  226. *
  227. * @returns `ptr + add` except it defines `NULL + 0 == NULL`.
  228. */
  229. MEM_STATIC
  230. unsigned char* ZSTD_maybeNullPtrAdd(unsigned char* ptr, ptrdiff_t add)
  231. {
  232. return add > 0 ? ptr + add : ptr;
  233. }
  234. /* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an
  235. * abundance of caution, disable our custom poisoning on mingw. */
  236. #ifdef __MINGW32__
  237. #ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE
  238. #define ZSTD_ASAN_DONT_POISON_WORKSPACE 1
  239. #endif
  240. #ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE
  241. #define ZSTD_MSAN_DONT_POISON_WORKSPACE 1
  242. #endif
  243. #endif
  244. #endif /* ZSTD_COMPILER_H */