|
1 | 1 | /* |
2 | | - * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc. |
| 2 | + * Copyright (c) 2016-2020, Yann Collect, Facebook, Inc. |
3 | 3 | * All rights reserved. |
4 | 4 | * |
5 | 5 | * This source code is licensed under both the BSD-style license (found in the |
|
12 | 12 | #define ZSTD_COMPILER_H |
13 | 13 |
|
14 | 14 | /*-******************************************************* |
15 | | -* Compiler specifics |
16 | | -*********************************************************/ |
| 15 | + * Compiler specifics |
| 16 | + *********************************************************/ |
17 | 17 | /* force inlining */ |
18 | 18 |
|
19 | 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 |
| 20 | +#if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || \ |
| 21 | + defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
| 22 | +#define INLINE_KEYWORD inline |
22 | 23 | #else |
23 | | -# define INLINE_KEYWORD |
| 24 | +#define INLINE_KEYWORD |
24 | 25 | #endif |
25 | 26 |
|
26 | 27 | #if defined(__GNUC__) || defined(__ICCARM__) |
27 | | -# define FORCE_INLINE_ATTR __attribute__((always_inline)) |
| 28 | +#define FORCE_INLINE_ATTR __attribute__((always_inline)) |
28 | 29 | #elif defined(_MSC_VER) |
29 | | -# define FORCE_INLINE_ATTR __forceinline |
| 30 | +#define FORCE_INLINE_ATTR __forceinline |
30 | 31 | #else |
31 | | -# define FORCE_INLINE_ATTR |
| 32 | +#define FORCE_INLINE_ATTR |
32 | 33 | #endif |
33 | 34 |
|
34 | 35 | #else |
|
39 | 40 | #endif |
40 | 41 |
|
41 | 42 | /** |
42 | | - On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC). |
43 | | - This explicitly marks such functions as __cdecl so that the code will still compile |
| 43 | + On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC). |
| 44 | + This explicitly marks such functions as __cdecl so that the code will still compile |
44 | 45 | if a CC other than __cdecl has been made the default. |
45 | 46 | */ |
46 | | -#if defined(_MSC_VER) |
47 | | -# define WIN_CDECL __cdecl |
| 47 | +#if defined(_MSC_VER) |
| 48 | +#define WIN_CDECL __cdecl |
48 | 49 | #else |
49 | | -# define WIN_CDECL |
| 50 | +#define WIN_CDECL |
50 | 51 | #endif |
51 | 52 |
|
52 | 53 | /** |
|
67 | 68 | * attribute. |
68 | 69 | */ |
69 | 70 | #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5 |
70 | | -# define HINT_INLINE static INLINE_KEYWORD |
| 71 | +#define HINT_INLINE static INLINE_KEYWORD |
71 | 72 | #else |
72 | | -# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR |
| 73 | +#define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR |
73 | 74 | #endif |
74 | 75 |
|
75 | 76 | /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */ |
76 | 77 | #if defined(__GNUC__) |
77 | | -# define UNUSED_ATTR __attribute__((unused)) |
| 78 | +#define UNUSED_ATTR __attribute__((unused)) |
78 | 79 | #else |
79 | | -# define UNUSED_ATTR |
| 80 | +#define UNUSED_ATTR |
80 | 81 | #endif |
81 | 82 |
|
82 | 83 | /* force no inlining */ |
83 | 84 | #ifdef _MSC_VER |
84 | | -# define FORCE_NOINLINE static __declspec(noinline) |
| 85 | +#define FORCE_NOINLINE static __declspec(noinline) |
| 86 | +#else |
| 87 | +#if defined(__GNUC__) || defined(__ICCARM__) |
| 88 | +#define FORCE_NOINLINE static __attribute__((__noinline__)) |
85 | 89 | #else |
86 | | -# if defined(__GNUC__) || defined(__ICCARM__) |
87 | | -# define FORCE_NOINLINE static __attribute__((__noinline__)) |
88 | | -# else |
89 | | -# define FORCE_NOINLINE static |
90 | | -# endif |
| 90 | +#define FORCE_NOINLINE static |
| 91 | +#endif |
91 | 92 | #endif |
92 | 93 |
|
93 | 94 | /* target attribute */ |
94 | 95 | #ifndef __has_attribute |
95 | | - #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ |
| 96 | +#define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ |
96 | 97 | #endif |
97 | 98 | #if defined(__GNUC__) || defined(__ICCARM__) |
98 | | -# define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) |
| 99 | +#define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) |
99 | 100 | #else |
100 | | -# define TARGET_ATTRIBUTE(target) |
| 101 | +#define TARGET_ATTRIBUTE(target) |
101 | 102 | #endif |
102 | 103 |
|
103 | 104 | /* Enable runtime BMI2 dispatch based on the CPU. |
104 | 105 | * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default. |
105 | 106 | */ |
106 | 107 | #ifndef DYNAMIC_BMI2 |
107 | | - #if ((defined(__clang__) && __has_attribute(__target__)) \ |
108 | | - || (defined(__GNUC__) \ |
109 | | - && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \ |
110 | | - && (defined(__x86_64__) || defined(_M_X86)) \ |
111 | | - && !defined(__BMI2__) |
112 | | - # define DYNAMIC_BMI2 1 |
113 | | - #else |
114 | | - # define DYNAMIC_BMI2 0 |
115 | | - #endif |
| 108 | +#if ((defined(__clang__) && __has_attribute(__target__)) || \ |
| 109 | + (defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) && \ |
| 110 | + (defined(__x86_64__) || defined(_M_X86)) && !defined(__BMI2__) |
| 111 | +#define DYNAMIC_BMI2 1 |
| 112 | +#else |
| 113 | +#define DYNAMIC_BMI2 0 |
| 114 | +#endif |
116 | 115 | #endif |
117 | 116 |
|
118 | 117 | /* prefetch |
119 | 118 | * can be disabled, by declaring NO_PREFETCH build macro */ |
120 | 119 | #if defined(NO_PREFETCH) |
121 | | -# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ |
122 | | -# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ |
| 120 | +#define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ |
| 121 | +#define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ |
| 122 | +#else |
| 123 | +#if defined(_MSC_VER) && \ |
| 124 | + (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */ |
| 125 | +#include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */ |
| 126 | +#define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0) |
| 127 | +#define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1) |
| 128 | +#elif defined(__aarch64__) |
| 129 | +#define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))) |
| 130 | +#define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))) |
| 131 | +#elif defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1))) |
| 132 | +#define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */) |
| 133 | +#define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */) |
123 | 134 | #else |
124 | | -# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */ |
125 | | -# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */ |
126 | | -# define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0) |
127 | | -# define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1) |
128 | | -# elif defined(__aarch64__) |
129 | | -# define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))) |
130 | | -# define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))) |
131 | | -# elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) ) |
132 | | -# define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */) |
133 | | -# define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */) |
134 | | -# else |
135 | | -# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ |
136 | | -# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ |
137 | | -# endif |
138 | | -#endif /* NO_PREFETCH */ |
| 135 | +#define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ |
| 136 | +#define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ |
| 137 | +#endif |
| 138 | +#endif /* NO_PREFETCH */ |
139 | 139 |
|
140 | 140 | #define CACHELINE_SIZE 64 |
141 | 141 |
|
142 | | -#define PREFETCH_AREA(p, s) { \ |
143 | | - const char* const _ptr = (const char*)(p); \ |
144 | | - size_t const _size = (size_t)(s); \ |
145 | | - size_t _pos; \ |
146 | | - for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \ |
147 | | - PREFETCH_L2(_ptr + _pos); \ |
148 | | - } \ |
149 | | -} |
| 142 | +#define PREFETCH_AREA(p, s) \ |
| 143 | + { \ |
| 144 | + const char* const _ptr = (const char*)(p); \ |
| 145 | + size_t const _size = (size_t)(s); \ |
| 146 | + size_t _pos; \ |
| 147 | + for (_pos = 0; _pos < _size; _pos += CACHELINE_SIZE) { PREFETCH_L2(_ptr + _pos); } \ |
| 148 | + } |
150 | 149 |
|
151 | 150 | /* vectorization |
152 | 151 | * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */ |
153 | 152 | #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) |
154 | | -# if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5) |
155 | | -# define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize"))) |
156 | | -# else |
157 | | -# define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")") |
158 | | -# endif |
| 153 | +#if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5) |
| 154 | +#define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize"))) |
| 155 | +#else |
| 156 | +#define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")") |
| 157 | +#endif |
159 | 158 | #else |
160 | | -# define DONT_VECTORIZE |
| 159 | +#define DONT_VECTORIZE |
161 | 160 | #endif |
162 | 161 |
|
163 | 162 | /* Tell the compiler that a branch is likely or unlikely. |
|
174 | 173 | #endif |
175 | 174 |
|
176 | 175 | /* disable warnings */ |
177 | | -#ifdef _MSC_VER /* Visual Studio */ |
178 | | -# include <intrin.h> /* For Visual 2005 */ |
179 | | -# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */ |
180 | | -# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
181 | | -# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
182 | | -# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ |
183 | | -# pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
| 176 | +#ifdef _MSC_VER /* Visual Studio */ |
| 177 | +#include <intrin.h> /* For Visual 2005 */ |
| 178 | +#pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */ |
| 179 | +#pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 180 | +#pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
| 181 | +#pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ |
| 182 | +#pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
184 | 183 | #endif |
185 | 184 |
|
186 | 185 | #endif /* ZSTD_COMPILER_H */ |
0 commit comments