xref: /openbmc/linux/lib/zstd/common/compiler.h (revision f21e49be)
1 /*
2  * Copyright (c) Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
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.
9  */
10 
11 #ifndef ZSTD_COMPILER_H
12 #define ZSTD_COMPILER_H
13 
14 /*-*******************************************************
15 *  Compiler specifics
16 *********************************************************/
17 /* force inlining */
18 
19 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
20 #  define INLINE_KEYWORD inline
21 #else
22 #  define INLINE_KEYWORD
23 #endif
24 
25 #define FORCE_INLINE_ATTR __attribute__((always_inline))
26 
27 
28 /*
29   On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
30   This explictly marks such functions as __cdecl so that the code will still compile
31   if a CC other than __cdecl has been made the default.
32 */
33 #define WIN_CDECL
34 
35 /*
36  * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
37  * parameters. They must be inlined for the compiler to eliminate the constant
38  * branches.
39  */
40 #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
41 /*
42  * HINT_INLINE is used to help the compiler generate better code. It is *not*
43  * used for "templates", so it can be tweaked based on the compilers
44  * performance.
45  *
46  * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
47  * always_inline attribute.
48  *
49  * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
50  * attribute.
51  */
52 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
53 #  define HINT_INLINE static INLINE_KEYWORD
54 #else
55 #  define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
56 #endif
57 
58 /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
59 #define UNUSED_ATTR __attribute__((unused))
60 
61 /* force no inlining */
62 #define FORCE_NOINLINE static __attribute__((__noinline__))
63 
64 
65 /* target attribute */
66 #ifndef __has_attribute
67   #define __has_attribute(x) 0  /* Compatibility with non-clang compilers. */
68 #endif
69 #define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
70 
71 /* Enable runtime BMI2 dispatch based on the CPU.
72  * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
73  */
74 #ifndef DYNAMIC_BMI2
75   #if ((defined(__clang__) && __has_attribute(__target__)) \
76       || (defined(__GNUC__) \
77           && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
78       && (defined(__x86_64__) || defined(_M_X86)) \
79       && !defined(__BMI2__)
80   #  define DYNAMIC_BMI2 1
81   #else
82   #  define DYNAMIC_BMI2 0
83   #endif
84 #endif
85 
86 /* prefetch
87  * can be disabled, by declaring NO_PREFETCH build macro */
88 #if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
89 #  define PREFETCH_L1(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
90 #  define PREFETCH_L2(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
91 #elif defined(__aarch64__)
92 #  define PREFETCH_L1(ptr)  __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
93 #  define PREFETCH_L2(ptr)  __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
94 #else
95 #  define PREFETCH_L1(ptr) (void)(ptr)  /* disabled */
96 #  define PREFETCH_L2(ptr) (void)(ptr)  /* disabled */
97 #endif  /* NO_PREFETCH */
98 
99 #define CACHELINE_SIZE 64
100 
101 #define PREFETCH_AREA(p, s)  {            \
102     const char* const _ptr = (const char*)(p);  \
103     size_t const _size = (size_t)(s);     \
104     size_t _pos;                          \
105     for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) {  \
106         PREFETCH_L2(_ptr + _pos);         \
107     }                                     \
108 }
109 
110 /* vectorization
111  * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */
112 #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)
113 #  if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
114 #    define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
115 #  else
116 #    define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
117 #  endif
118 #else
119 #  define DONT_VECTORIZE
120 #endif
121 
122 /* Tell the compiler that a branch is likely or unlikely.
123  * Only use these macros if it causes the compiler to generate better code.
124  * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
125  * and clang, please do.
126  */
127 #define LIKELY(x) (__builtin_expect((x), 1))
128 #define UNLIKELY(x) (__builtin_expect((x), 0))
129 
130 /* disable warnings */
131 
132 /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/
133 
134 
135 /* compat. with non-clang compilers */
136 #ifndef __has_builtin
137 #  define __has_builtin(x) 0
138 #endif
139 
140 /* compat. with non-clang compilers */
141 #ifndef __has_feature
142 #  define __has_feature(x) 0
143 #endif
144 
145 /* C-language Attributes are added in C23. */
146 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
147 # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
148 #else
149 # define ZSTD_HAS_C_ATTRIBUTE(x) 0
150 #endif
151 
152 /* Only use C++ attributes in C++. Some compilers report support for C++
153  * attributes when compiling with C.
154  */
155 #define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
156 
157 /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
158  * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
159  * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
160  * - Else: __attribute__((__fallthrough__))
161  */
162 #define ZSTD_FALLTHROUGH fallthrough
163 
164 /* detects whether we are being compiled under msan */
165 
166 
167 /* detects whether we are being compiled under asan */
168 
169 
170 #endif /* ZSTD_COMPILER_H */
171