1 /* 1 /* 2 * Copyright (c) Yann Collet, Facebook, Inc. 2 * Copyright (c) Yann Collet, Facebook, Inc. 3 * All rights reserved. 3 * All rights reserved. 4 * 4 * 5 * This source code is licensed under both the 5 * This source code is licensed under both the BSD-style license (found in the 6 * LICENSE file in the root directory of this 6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7 * in the COPYING file in the root directory o 7 * in the COPYING file in the root directory of this source tree). 8 * You may select, at your option, one of the 8 * You may select, at your option, one of the above-listed licenses. 9 */ 9 */ 10 10 11 #ifndef ZSTD_COMPILER_H 11 #ifndef ZSTD_COMPILER_H 12 #define ZSTD_COMPILER_H 12 #define ZSTD_COMPILER_H 13 13 14 #include "portability_macros.h" << 15 << 16 /*-******************************************* 14 /*-******************************************************* 17 * Compiler specifics 15 * Compiler specifics 18 ********************************************** 16 *********************************************************/ 19 /* force inlining */ 17 /* force inlining */ 20 18 21 #if !defined(ZSTD_NO_INLINE) 19 #if !defined(ZSTD_NO_INLINE) 22 #if (defined(__GNUC__) && !defined(__STRICT_AN 20 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ 23 # define INLINE_KEYWORD inline 21 # define INLINE_KEYWORD inline 24 #else 22 #else 25 # define INLINE_KEYWORD 23 # define INLINE_KEYWORD 26 #endif 24 #endif 27 25 28 #define FORCE_INLINE_ATTR __attribute__((alway 26 #define FORCE_INLINE_ATTR __attribute__((always_inline)) 29 27 30 #else 28 #else 31 29 32 #define INLINE_KEYWORD 30 #define INLINE_KEYWORD 33 #define FORCE_INLINE_ATTR 31 #define FORCE_INLINE_ATTR 34 32 35 #endif 33 #endif 36 34 37 /* 35 /* 38 On MSVC qsort requires that functions passed 36 On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC). 39 This explicitly marks such functions as __cd !! 37 This explictly marks such functions as __cdecl so that the code will still compile 40 if a CC other than __cdecl has been made the 38 if a CC other than __cdecl has been made the default. 41 */ 39 */ 42 #define WIN_CDECL 40 #define WIN_CDECL 43 41 44 /* 42 /* 45 * FORCE_INLINE_TEMPLATE is used to define C " 43 * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant 46 * parameters. They must be inlined for the co 44 * parameters. They must be inlined for the compiler to eliminate the constant 47 * branches. 45 * branches. 48 */ 46 */ 49 #define FORCE_INLINE_TEMPLATE static INLINE_KE 47 #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR 50 /* 48 /* 51 * HINT_INLINE is used to help the compiler ge 49 * HINT_INLINE is used to help the compiler generate better code. It is *not* 52 * used for "templates", so it can be tweaked 50 * used for "templates", so it can be tweaked based on the compilers 53 * performance. 51 * performance. 54 * 52 * 55 * gcc-4.8 and gcc-4.9 have been shown to bene 53 * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the 56 * always_inline attribute. 54 * always_inline attribute. 57 * 55 * 58 * clang up to 5.0.0 (trunk) benefit tremendou 56 * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline 59 * attribute. 57 * attribute. 60 */ 58 */ 61 #if !defined(__clang__) && defined(__GNUC__) & 59 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5 62 # define HINT_INLINE static INLINE_KEYWORD 60 # define HINT_INLINE static INLINE_KEYWORD 63 #else 61 #else 64 # define HINT_INLINE static INLINE_KEYWORD FO 62 # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR 65 #endif 63 #endif 66 64 67 /* UNUSED_ATTR tells the compiler it is okay i 65 /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */ 68 #define UNUSED_ATTR __attribute__((unused)) 66 #define UNUSED_ATTR __attribute__((unused)) 69 67 70 /* force no inlining */ 68 /* force no inlining */ 71 #define FORCE_NOINLINE static __attribute__((_ 69 #define FORCE_NOINLINE static __attribute__((__noinline__)) 72 70 73 71 74 /* target attribute */ 72 /* target attribute */ >> 73 #ifndef __has_attribute >> 74 #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ >> 75 #endif 75 #define TARGET_ATTRIBUTE(target) __attribute__ 76 #define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) 76 77 77 /* Target attribute for BMI2 dynamic dispatch. !! 78 /* Enable runtime BMI2 dispatch based on the CPU. 78 * Enable lzcnt, bmi, and bmi2. !! 79 * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default. 79 * We test for bmi1 & bmi2. lzcnt is included << 80 */ 80 */ 81 #define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE !! 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 82 92 83 /* prefetch 93 /* prefetch 84 * can be disabled, by declaring NO_PREFETCH b 94 * can be disabled, by declaring NO_PREFETCH build macro */ 85 #if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && 95 #if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) ) 86 # define PREFETCH_L1(ptr) __builtin_prefetch 96 # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */) 87 # define PREFETCH_L2(ptr) __builtin_prefetch 97 # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */) 88 #elif defined(__aarch64__) 98 #elif defined(__aarch64__) 89 # define PREFETCH_L1(ptr) __asm__ __volatile 99 # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))) 90 # define PREFETCH_L2(ptr) __asm__ __volatile 100 # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))) 91 #else 101 #else 92 # define PREFETCH_L1(ptr) (void)(ptr) /* dis 102 # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ 93 # define PREFETCH_L2(ptr) (void)(ptr) /* dis 103 # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ 94 #endif /* NO_PREFETCH */ 104 #endif /* NO_PREFETCH */ 95 105 96 #define CACHELINE_SIZE 64 106 #define CACHELINE_SIZE 64 97 107 98 #define PREFETCH_AREA(p, s) { \ 108 #define PREFETCH_AREA(p, s) { \ 99 const char* const _ptr = (const char*)(p); 109 const char* const _ptr = (const char*)(p); \ 100 size_t const _size = (size_t)(s); \ 110 size_t const _size = (size_t)(s); \ 101 size_t _pos; \ 111 size_t _pos; \ 102 for (_pos=0; _pos<_size; _pos+=CACHELINE_S 112 for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \ 103 PREFETCH_L2(_ptr + _pos); \ 113 PREFETCH_L2(_ptr + _pos); \ 104 } \ 114 } \ 105 } 115 } 106 116 107 /* vectorization 117 /* vectorization 108 * older GCC (pre gcc-4.3 picked as the cutoff !! 118 * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */ 109 * and some compilers, like Intel ICC and MCST !! 119 #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) 110 #if !defined(__INTEL_COMPILER) && !defined(__c << 111 # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || 120 # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5) 112 # define DONT_VECTORIZE __attribute__((opti 121 # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize"))) 113 # else 122 # else 114 # define DONT_VECTORIZE _Pragma("GCC optimi 123 # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")") 115 # endif 124 # endif 116 #else 125 #else 117 # define DONT_VECTORIZE 126 # define DONT_VECTORIZE 118 #endif 127 #endif 119 128 120 /* Tell the compiler that a branch is likely o 129 /* Tell the compiler that a branch is likely or unlikely. 121 * Only use these macros if it causes the comp 130 * Only use these macros if it causes the compiler to generate better code. 122 * If you can remove a LIKELY/UNLIKELY annotat 131 * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc 123 * and clang, please do. 132 * and clang, please do. 124 */ 133 */ 125 #define LIKELY(x) (__builtin_expect((x), 1)) 134 #define LIKELY(x) (__builtin_expect((x), 1)) 126 #define UNLIKELY(x) (__builtin_expect((x), 0)) 135 #define UNLIKELY(x) (__builtin_expect((x), 0)) 127 136 128 #if __has_builtin(__builtin_unreachable) || (d << 129 # define ZSTD_UNREACHABLE { assert(0), __buil << 130 #else << 131 # define ZSTD_UNREACHABLE { assert(0); } << 132 #endif << 133 << 134 /* disable warnings */ 137 /* disable warnings */ 135 138 136 /*Like DYNAMIC_BMI2 but for compile time deter 139 /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/ 137 140 138 141 139 /* compile time determination of SIMD support !! 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 140 151 141 /* C-language Attributes are added in C23. */ 152 /* C-language Attributes are added in C23. */ 142 #if defined(__STDC_VERSION__) && (__STDC_VERSI 153 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute) 143 # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attri 154 # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x) 144 #else 155 #else 145 # define ZSTD_HAS_C_ATTRIBUTE(x) 0 156 # define ZSTD_HAS_C_ATTRIBUTE(x) 0 146 #endif 157 #endif 147 158 148 /* Only use C++ attributes in C++. Some compil 159 /* Only use C++ attributes in C++. Some compilers report support for C++ 149 * attributes when compiling with C. 160 * attributes when compiling with C. 150 */ 161 */ 151 #define ZSTD_HAS_CPP_ATTRIBUTE(x) 0 162 #define ZSTD_HAS_CPP_ATTRIBUTE(x) 0 152 163 153 /* Define ZSTD_FALLTHROUGH macro for annotatin 164 /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute. 154 * - C23: https://en.cppreference.com/w/c/lang 165 * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough 155 * - CPP17: https://en.cppreference.com/w/cpp/ 166 * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough 156 * - Else: __attribute__((__fallthrough__)) 167 * - Else: __attribute__((__fallthrough__)) 157 */ 168 */ 158 #define ZSTD_FALLTHROUGH fallthrough 169 #define ZSTD_FALLTHROUGH fallthrough 159 170 160 /*-******************************************* !! 171 /* detects whether we are being compiled under msan */ 161 * Alignment check !! 172 162 ********************************************** << 163 << 164 /* this test was initially positioned in mem.h << 165 * but this file is removed (or replaced) for << 166 * so it's now hosted in compiler.h, << 167 * which remains valid for both user & kernel << 168 */ << 169 << 170 #ifndef ZSTD_ALIGNOF << 171 /* covers gcc, clang & MSVC */ << 172 /* note : this section must come first, before << 173 * due to a limitation in the kernel source ge << 174 # define ZSTD_ALIGNOF(T) __alignof(T) << 175 << 176 #endif /* ZSTD_ALIGNOF */ << 177 << 178 /*-******************************************* << 179 * Sanitizer << 180 ********************************************** << 181 173 >> 174 /* detects whether we are being compiled under asan */ 182 175 183 176 184 #endif /* ZSTD_COMPILER_H */ 177 #endif /* ZSTD_COMPILER_H */ 185 178
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.