|
11 | 11 | #ifndef ZSTD_COMPILER_H |
12 | 12 | #define ZSTD_COMPILER_H |
13 | 13 |
|
| 14 | +#include "portability_macros.h" |
| 15 | + |
14 | 16 | /*-******************************************************* |
15 | 17 | * Compiler specifics |
16 | 18 | *********************************************************/ |
|
34 | 36 |
|
35 | 37 | /* |
36 | 38 | On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC). |
37 | | - This explictly marks such functions as __cdecl so that the code will still compile |
| 39 | + This explicitly marks such functions as __cdecl so that the code will still compile |
38 | 40 | if a CC other than __cdecl has been made the default. |
39 | 41 | */ |
40 | 42 | #define WIN_CDECL |
|
70 | 72 |
|
71 | 73 |
|
72 | 74 | /* target attribute */ |
73 | | -#ifndef __has_attribute |
74 | | - #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ |
75 | | -#endif |
76 | 75 | #define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) |
77 | 76 |
|
78 | | -/* Enable runtime BMI2 dispatch based on the CPU. |
79 | | - * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default. |
| 77 | +/* Target attribute for BMI2 dynamic dispatch. |
| 78 | + * Enable lzcnt, bmi, and bmi2. |
| 79 | + * We test for bmi1 & bmi2. lzcnt is included in bmi1. |
80 | 80 | */ |
81 | | -#ifndef DYNAMIC_BMI2 |
82 | | - #if ((defined(__clang__) && __has_attribute(__target__)) \ |
83 | | - || (defined(__GNUC__) \ |
84 | | - && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \ |
85 | | - && (defined(__x86_64__) || defined(_M_X86)) \ |
86 | | - && !defined(__BMI2__) |
87 | | - # define DYNAMIC_BMI2 1 |
88 | | - #else |
89 | | - # define DYNAMIC_BMI2 0 |
90 | | - #endif |
91 | | -#endif |
| 81 | +#define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2") |
92 | 82 |
|
93 | 83 | /* prefetch |
94 | 84 | * can be disabled, by declaring NO_PREFETCH build macro */ |
|
115 | 105 | } |
116 | 106 |
|
117 | 107 | /* vectorization |
118 | | - * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */ |
119 | | -#if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) |
| 108 | + * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax, |
| 109 | + * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */ |
| 110 | +#if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__) |
120 | 111 | # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5) |
121 | 112 | # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize"))) |
122 | 113 | # else |
|
134 | 125 | #define LIKELY(x) (__builtin_expect((x), 1)) |
135 | 126 | #define UNLIKELY(x) (__builtin_expect((x), 0)) |
136 | 127 |
|
| 128 | +#if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))) |
| 129 | +# define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); } |
| 130 | +#else |
| 131 | +# define ZSTD_UNREACHABLE { assert(0); } |
| 132 | +#endif |
| 133 | + |
137 | 134 | /* disable warnings */ |
138 | 135 |
|
139 | 136 | /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/ |
140 | 137 |
|
141 | 138 |
|
142 | | -/* compat. with non-clang compilers */ |
143 | | -#ifndef __has_builtin |
144 | | -# define __has_builtin(x) 0 |
145 | | -#endif |
146 | | - |
147 | | -/* compat. with non-clang compilers */ |
148 | | -#ifndef __has_feature |
149 | | -# define __has_feature(x) 0 |
150 | | -#endif |
| 139 | +/* compile time determination of SIMD support */ |
151 | 140 |
|
152 | 141 | /* C-language Attributes are added in C23. */ |
153 | 142 | #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute) |
|
168 | 157 | */ |
169 | 158 | #define ZSTD_FALLTHROUGH fallthrough |
170 | 159 |
|
171 | | -/* detects whether we are being compiled under msan */ |
| 160 | +/*-************************************************************** |
| 161 | +* Alignment check |
| 162 | +*****************************************************************/ |
| 163 | + |
| 164 | +/* this test was initially positioned in mem.h, |
| 165 | + * but this file is removed (or replaced) for linux kernel |
| 166 | + * so it's now hosted in compiler.h, |
| 167 | + * which remains valid for both user & kernel spaces. |
| 168 | + */ |
| 169 | + |
| 170 | +#ifndef ZSTD_ALIGNOF |
| 171 | +/* covers gcc, clang & MSVC */ |
| 172 | +/* note : this section must come first, before C11, |
| 173 | + * due to a limitation in the kernel source generator */ |
| 174 | +# define ZSTD_ALIGNOF(T) __alignof(T) |
| 175 | + |
| 176 | +#endif /* ZSTD_ALIGNOF */ |
172 | 177 |
|
| 178 | +/*-************************************************************** |
| 179 | +* Sanitizer |
| 180 | +*****************************************************************/ |
173 | 181 |
|
174 | | -/* detects whether we are being compiled under asan */ |
175 | 182 |
|
176 | 183 |
|
177 | 184 | #endif /* ZSTD_COMPILER_H */ |
0 commit comments