Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/module/zstd/lib/common/compiler.h
48774 views
1
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
2
/*
3
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
4
* All rights reserved.
5
*
6
* This source code is licensed under both the BSD-style license (found in the
7
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
8
* in the COPYING file in the root directory of this source tree).
9
* You may select, at your option, one of the above-listed licenses.
10
*/
11
12
#ifndef ZSTD_COMPILER_H
13
#define ZSTD_COMPILER_H
14
15
/*-*******************************************************
16
* Compiler specifics
17
*********************************************************/
18
/* force inlining */
19
20
#if !defined(ZSTD_NO_INLINE)
21
#if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
22
# define INLINE_KEYWORD inline
23
#else
24
# define INLINE_KEYWORD
25
#endif
26
27
#if defined(__GNUC__) || defined(__ICCARM__)
28
# define FORCE_INLINE_ATTR __attribute__((always_inline))
29
#elif defined(_MSC_VER)
30
# define FORCE_INLINE_ATTR __forceinline
31
#else
32
# define FORCE_INLINE_ATTR
33
#endif
34
35
#else
36
37
#define INLINE_KEYWORD
38
#define FORCE_INLINE_ATTR
39
40
#endif
41
42
/**
43
* FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
44
* parameters. They must be inlined for the compiler to eliminate the constant
45
* branches.
46
*/
47
#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
48
/**
49
* HINT_INLINE is used to help the compiler generate better code. It is *not*
50
* used for "templates", so it can be tweaked based on the compilers
51
* performance.
52
*
53
* gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
54
* always_inline attribute.
55
*
56
* clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
57
* attribute.
58
*/
59
#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
60
# define HINT_INLINE static INLINE_KEYWORD
61
#else
62
# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
63
#endif
64
65
/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
66
#if defined(__GNUC__)
67
# define UNUSED_ATTR __attribute__((unused))
68
#else
69
# define UNUSED_ATTR
70
#endif
71
72
/* force no inlining */
73
#ifdef _MSC_VER
74
# define FORCE_NOINLINE static __declspec(noinline)
75
#else
76
# if defined(__GNUC__) || defined(__ICCARM__)
77
# define FORCE_NOINLINE static __attribute__((__noinline__))
78
# else
79
# define FORCE_NOINLINE static
80
# endif
81
#endif
82
83
/* target attribute */
84
#ifndef __has_attribute
85
#define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
86
#endif
87
#if defined(__GNUC__) || defined(__ICCARM__)
88
# define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
89
#else
90
# define TARGET_ATTRIBUTE(target)
91
#endif
92
93
/* Enable runtime BMI2 dispatch based on the CPU.
94
* Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
95
*/
96
#ifndef DYNAMIC_BMI2
97
#if ((defined(__clang__) && __has_attribute(__target__)) \
98
|| (defined(__GNUC__) \
99
&& (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
100
&& (defined(__x86_64__) || defined(_M_X86)) \
101
&& !defined(__BMI2__)
102
# define DYNAMIC_BMI2 1
103
#else
104
# define DYNAMIC_BMI2 0
105
#endif
106
#endif
107
108
/* prefetch
109
* can be disabled, by declaring NO_PREFETCH build macro */
110
#if defined(NO_PREFETCH)
111
# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
112
# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
113
#else
114
# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
115
# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
116
# define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
117
# define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
118
# elif defined(__aarch64__)
119
# define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
120
# define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
121
# elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
122
# define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
123
# define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
124
# else
125
# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
126
# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
127
# endif
128
#endif /* NO_PREFETCH */
129
130
#define CACHELINE_SIZE 64
131
132
#define PREFETCH_AREA(p, s) { \
133
const char* const _ptr = (const char*)(p); \
134
size_t const _size = (size_t)(s); \
135
size_t _pos; \
136
for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
137
PREFETCH_L2(_ptr + _pos); \
138
} \
139
}
140
141
/* vectorization
142
* older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */
143
#if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)
144
# if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
145
# define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
146
# else
147
# define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
148
# endif
149
#else
150
# define DONT_VECTORIZE
151
#endif
152
153
/* Tell the compiler that a branch is likely or unlikely.
154
* Only use these macros if it causes the compiler to generate better code.
155
* If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
156
* and clang, please do.
157
*/
158
#if defined(__GNUC__)
159
#define LIKELY(x) (__builtin_expect((x), 1))
160
#define UNLIKELY(x) (__builtin_expect((x), 0))
161
#else
162
#define LIKELY(x) (x)
163
#define UNLIKELY(x) (x)
164
#endif
165
166
/* disable warnings */
167
#ifdef _MSC_VER /* Visual Studio */
168
# include <intrin.h> /* For Visual 2005 */
169
# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
170
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
171
# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
172
# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
173
# pragma warning(disable : 4324) /* disable: C4324: padded structure */
174
#endif
175
176
#endif /* ZSTD_COMPILER_H */
177
178