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