0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef ZSTD_COMPILER_H
0012 #define ZSTD_COMPILER_H
0013
0014
0015
0016
0017
0018
0019 #if !defined(ZSTD_NO_INLINE)
0020 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
0021 # define INLINE_KEYWORD inline
0022 #else
0023 # define INLINE_KEYWORD
0024 #endif
0025
0026 #define FORCE_INLINE_ATTR __attribute__((always_inline))
0027
0028 #else
0029
0030 #define INLINE_KEYWORD
0031 #define FORCE_INLINE_ATTR
0032
0033 #endif
0034
0035
0036
0037
0038
0039
0040 #define WIN_CDECL
0041
0042
0043
0044
0045
0046
0047 #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
0060 # define HINT_INLINE static INLINE_KEYWORD
0061 #else
0062 # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
0063 #endif
0064
0065
0066 #define UNUSED_ATTR __attribute__((unused))
0067
0068
0069 #define FORCE_NOINLINE static __attribute__((__noinline__))
0070
0071
0072
0073 #ifndef __has_attribute
0074 #define __has_attribute(x) 0
0075 #endif
0076 #define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
0077
0078
0079
0080
0081 #ifndef DYNAMIC_BMI2
0082 #if ((defined(__clang__) && __has_attribute(__target__)) \
0083 || (defined(__GNUC__) \
0084 && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
0085 && (defined(__x86_64__) || defined(_M_X86)) \
0086 && !defined(__BMI2__)
0087 # define DYNAMIC_BMI2 1
0088 #else
0089 # define DYNAMIC_BMI2 0
0090 #endif
0091 #endif
0092
0093
0094
0095 #if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
0096 # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 , 3 )
0097 # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 , 2 )
0098 #elif defined(__aarch64__)
0099 # define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
0100 # define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
0101 #else
0102 # define PREFETCH_L1(ptr) (void)(ptr)
0103 # define PREFETCH_L2(ptr) (void)(ptr)
0104 #endif
0105
0106 #define CACHELINE_SIZE 64
0107
0108 #define PREFETCH_AREA(p, s) { \
0109 const char* const _ptr = (const char*)(p); \
0110 size_t const _size = (size_t)(s); \
0111 size_t _pos; \
0112 for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
0113 PREFETCH_L2(_ptr + _pos); \
0114 } \
0115 }
0116
0117
0118
0119 #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)
0120 # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
0121 # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
0122 # else
0123 # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
0124 # endif
0125 #else
0126 # define DONT_VECTORIZE
0127 #endif
0128
0129
0130
0131
0132
0133
0134 #define LIKELY(x) (__builtin_expect((x), 1))
0135 #define UNLIKELY(x) (__builtin_expect((x), 0))
0136
0137
0138
0139
0140
0141
0142
0143 #ifndef __has_builtin
0144 # define __has_builtin(x) 0
0145 #endif
0146
0147
0148 #ifndef __has_feature
0149 # define __has_feature(x) 0
0150 #endif
0151
0152
0153 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
0154 # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
0155 #else
0156 # define ZSTD_HAS_C_ATTRIBUTE(x) 0
0157 #endif
0158
0159
0160
0161
0162 #define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
0163
0164
0165
0166
0167
0168
0169 #define ZSTD_FALLTHROUGH fallthrough
0170
0171
0172
0173
0174
0175
0176
0177 #endif