2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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 /*-*******************************************************
16 *********************************************************/
19 #if !defined(ZSTD_NO_INLINE)
20 #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
21 # define INLINE_KEYWORD inline
23 # define INLINE_KEYWORD
27 # define FORCE_INLINE_ATTR __attribute__((always_inline))
28 #elif defined(_MSC_VER)
29 # define FORCE_INLINE_ATTR __forceinline
31 # define FORCE_INLINE_ATTR
36 #define INLINE_KEYWORD
37 #define FORCE_INLINE_ATTR
42 * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
43 * parameters. They must be inlined for the compiler to eliminate the constant
46 #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
48 * HINT_INLINE is used to help the compiler generate better code. It is *not*
49 * used for "templates", so it can be tweaked based on the compilers
52 * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
53 * always_inline attribute.
55 * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
58 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
59 # define HINT_INLINE static INLINE_KEYWORD
61 # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
64 /* force no inlining */
66 # define FORCE_NOINLINE static __declspec(noinline)
69 # define FORCE_NOINLINE static __attribute__((__noinline__))
71 # define FORCE_NOINLINE static
75 /* target attribute */
76 #ifndef __has_attribute
77 #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
80 # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
82 # define TARGET_ATTRIBUTE(target)
85 /* Enable runtime BMI2 dispatch based on the CPU.
86 * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
89 #if ((defined(__clang__) && __has_attribute(__target__)) \
90 || (defined(__GNUC__) \
91 && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
92 && (defined(__x86_64__) || defined(_M_X86)) \
94 # define DYNAMIC_BMI2 1
96 # define DYNAMIC_BMI2 0
101 * can be disabled, by declaring NO_PREFETCH build macro */
102 #if defined(NO_PREFETCH)
103 # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
104 # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
106 # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
107 # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
108 # define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
109 # define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
110 # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
111 # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
112 # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
114 # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
115 # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
117 #endif /* NO_PREFETCH */
119 #define CACHELINE_SIZE 64
121 #define PREFETCH_AREA(p, s) { \
122 const char* const _ptr = (const char*)(p); \
123 size_t const _size = (size_t)(s); \
125 for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
126 PREFETCH_L2(_ptr + _pos); \
131 #if !defined(__clang__) && defined(__GNUC__)
132 # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
134 # define DONT_VECTORIZE
137 /* disable warnings */
138 #ifdef _MSC_VER /* Visual Studio */
139 # include <intrin.h> /* For Visual 2005 */
140 # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
141 # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
142 # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
143 # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
144 # pragma warning(disable : 4324) /* disable: C4324: padded structure */
147 #endif /* ZSTD_COMPILER_H */