2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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.
11 #ifndef ZSTD_COMPILER_H
12 #define ZSTD_COMPILER_H
14 #include "portability_macros.h"
16 /*-*******************************************************
18 *********************************************************/
21 #if !defined(ZSTD_NO_INLINE)
22 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
23 # define INLINE_KEYWORD inline
25 # define INLINE_KEYWORD
28 #if defined(__GNUC__) || defined(__ICCARM__)
29 # define FORCE_INLINE_ATTR __attribute__((always_inline))
30 #elif defined(_MSC_VER)
31 # define FORCE_INLINE_ATTR __forceinline
33 # define FORCE_INLINE_ATTR
38 #define INLINE_KEYWORD
39 #define FORCE_INLINE_ATTR
44 On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
45 This explicitly marks such functions as __cdecl so that the code will still compile
46 if a CC other than __cdecl has been made the default.
49 # define WIN_CDECL __cdecl
55 * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
56 * parameters. They must be inlined for the compiler to eliminate the constant
59 #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
61 * HINT_INLINE is used to help the compiler generate better code. It is *not*
62 * used for "templates", so it can be tweaked based on the compilers
65 * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
66 * always_inline attribute.
68 * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
71 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
72 # define HINT_INLINE static INLINE_KEYWORD
74 # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
77 /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
79 # define UNUSED_ATTR __attribute__((unused))
84 /* force no inlining */
86 # define FORCE_NOINLINE static __declspec(noinline)
88 # if defined(__GNUC__) || defined(__ICCARM__)
89 # define FORCE_NOINLINE static __attribute__((__noinline__))
91 # define FORCE_NOINLINE static
96 /* target attribute */
97 #if defined(__GNUC__) || defined(__ICCARM__)
98 # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
100 # define TARGET_ATTRIBUTE(target)
103 /* Target attribute for BMI2 dynamic dispatch.
104 * Enable lzcnt, bmi, and bmi2.
105 * We test for bmi1 & bmi2. lzcnt is included in bmi1.
107 #define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2")
110 * can be disabled, by declaring NO_PREFETCH build macro */
111 #if defined(NO_PREFETCH)
112 # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
113 # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
115 # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
116 # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
117 # define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
118 # define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
119 # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
120 # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
121 # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
122 # elif defined(__aarch64__)
123 # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
124 # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
126 # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
127 # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
129 #endif /* NO_PREFETCH */
131 #define CACHELINE_SIZE 64
133 #define PREFETCH_AREA(p, s) { \
134 const char* const _ptr = (const char*)(p); \
135 size_t const _size = (size_t)(s); \
137 for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
138 PREFETCH_L2(_ptr + _pos); \
143 * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax,
144 * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */
145 #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__)
146 # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
147 # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
149 # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
152 # define DONT_VECTORIZE
155 /* Tell the compiler that a branch is likely or unlikely.
156 * Only use these macros if it causes the compiler to generate better code.
157 * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
158 * and clang, please do.
160 #if defined(__GNUC__)
161 #define LIKELY(x) (__builtin_expect((x), 1))
162 #define UNLIKELY(x) (__builtin_expect((x), 0))
164 #define LIKELY(x) (x)
165 #define UNLIKELY(x) (x)
168 #if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
169 # define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); }
171 # define ZSTD_UNREACHABLE { assert(0); }
174 /* disable warnings */
175 #ifdef _MSC_VER /* Visual Studio */
176 # include <intrin.h> /* For Visual 2005 */
177 # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
178 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
179 # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
180 # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
181 # pragma warning(disable : 4324) /* disable: C4324: padded structure */
184 /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/
186 # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86))
187 # ifdef __AVX2__ //MSVC does not have a BMI2 specific flag, but every CPU that supports AVX2 also supports BMI2
188 # define STATIC_BMI2 1
190 # elif defined(__BMI2__) && defined(__x86_64__) && defined(__GNUC__)
191 # define STATIC_BMI2 1
196 #define STATIC_BMI2 0
199 /* compile time determination of SIMD support */
200 #if !defined(ZSTD_NO_INTRINSICS)
201 # if defined(__SSE2__) || defined(_M_AMD64) || (defined (_M_IX86) && defined(_M_IX86_FP) && (_M_IX86_FP >= 2))
202 # define ZSTD_ARCH_X86_SSE2
204 # if defined(__ARM_NEON) || defined(_M_ARM64)
205 # define ZSTD_ARCH_ARM_NEON
208 # if defined(ZSTD_ARCH_X86_SSE2)
209 # include <emmintrin.h>
210 # elif defined(ZSTD_ARCH_ARM_NEON)
211 # include <arm_neon.h>
215 /* C-language Attributes are added in C23. */
216 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
217 # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
219 # define ZSTD_HAS_C_ATTRIBUTE(x) 0
222 /* Only use C++ attributes in C++. Some compilers report support for C++
223 * attributes when compiling with C.
225 #if defined(__cplusplus) && defined(__has_cpp_attribute)
226 # define ZSTD_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
228 # define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
231 /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
232 * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
233 * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
234 * - Else: __attribute__((__fallthrough__))
236 #ifndef ZSTD_FALLTHROUGH
237 # if ZSTD_HAS_C_ATTRIBUTE(fallthrough)
238 # define ZSTD_FALLTHROUGH [[fallthrough]]
239 # elif ZSTD_HAS_CPP_ATTRIBUTE(fallthrough)
240 # define ZSTD_FALLTHROUGH [[fallthrough]]
241 # elif __has_attribute(__fallthrough__)
242 /* Leading semicolon is to satisfy gcc-11 with -pedantic. Without the semicolon
243 * gcc complains about: a label can only be part of a statement and a declaration is not a statement.
245 # define ZSTD_FALLTHROUGH ; __attribute__((__fallthrough__))
247 # define ZSTD_FALLTHROUGH
251 /*-**************************************************************
253 *****************************************************************/
255 /* this test was initially positioned in mem.h,
256 * but this file is removed (or replaced) for linux kernel
257 * so it's now hosted in compiler.h,
258 * which remains valid for both user & kernel spaces.
262 # if defined(__GNUC__) || defined(_MSC_VER)
263 /* covers gcc, clang & MSVC */
264 /* note : this section must come first, before C11,
265 * due to a limitation in the kernel source generator */
266 # define ZSTD_ALIGNOF(T) __alignof(T)
268 # elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
270 # include <stdalign.h>
271 # define ZSTD_ALIGNOF(T) alignof(T)
274 /* No known support for alignof() - imperfect backup */
275 # define ZSTD_ALIGNOF(T) (sizeof(void*) < sizeof(T) ? sizeof(void*) : sizeof(T))
278 #endif /* ZSTD_ALIGNOF */
280 /*-**************************************************************
282 *****************************************************************/
284 /* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an
285 * abundance of caution, disable our custom poisoning on mingw. */
287 #ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE
288 #define ZSTD_ASAN_DONT_POISON_WORKSPACE 1
290 #ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE
291 #define ZSTD_MSAN_DONT_POISON_WORKSPACE 1
295 #if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)
296 /* Not all platforms that support msan provide sanitizers/msan_interface.h.
297 * We therefore declare the functions we need ourselves, rather than trying to
298 * include the header file... */
299 #include <stddef.h> /* size_t */
300 #define ZSTD_DEPS_NEED_STDINT
301 #include "zstd_deps.h" /* intptr_t */
303 /* Make memory region fully initialized (without changing its contents). */
304 void __msan_unpoison(const volatile void *a, size_t size);
306 /* Make memory region fully uninitialized (without changing its contents).
307 This is a legacy interface that does not update origin information. Use
308 __msan_allocated_memory() instead. */
309 void __msan_poison(const volatile void *a, size_t size);
311 /* Returns the offset of the first (at least partially) poisoned byte in the
312 memory range, or -1 if the whole range is good. */
313 intptr_t __msan_test_shadow(const volatile void *x, size_t size);
315 /* Print shadow and origin for the memory range to stderr in a human-readable
317 void __msan_print_shadow(const volatile void *x, size_t size);
320 #if ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE)
321 /* Not all platforms that support asan provide sanitizers/asan_interface.h.
322 * We therefore declare the functions we need ourselves, rather than trying to
323 * include the header file... */
324 #include <stddef.h> /* size_t */
327 * Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.
329 * This memory must be previously allocated by your program. Instrumented
330 * code is forbidden from accessing addresses in this region until it is
331 * unpoisoned. This function is not guaranteed to poison the entire region -
332 * it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan
333 * alignment restrictions.
335 * \note This function is not thread-safe because no two threads can poison or
336 * unpoison memory in the same memory region simultaneously.
338 * \param addr Start of memory region.
339 * \param size Size of memory region. */
340 void __asan_poison_memory_region(void const volatile *addr, size_t size);
343 * Marks a memory region (<c>[addr, addr+size)</c>) as addressable.
345 * This memory must be previously allocated by your program. Accessing
346 * addresses in this region is allowed until this region is poisoned again.
347 * This function could unpoison a super-region of <c>[addr, addr+size)</c> due
348 * to ASan alignment restrictions.
350 * \note This function is not thread-safe because no two threads can
351 * poison or unpoison memory in the same memory region simultaneously.
353 * \param addr Start of memory region.
354 * \param size Size of memory region. */
355 void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
358 #endif /* ZSTD_COMPILER_H */