Path: blob/main/sys/contrib/openzfs/module/zstd/lib/common/compiler.h
105295 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only1/*2* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.3* All rights reserved.4*5* This source code is licensed under both the BSD-style license (found in the6* LICENSE file in the root directory of this source tree) and the GPLv2 (found7* 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*/1011#ifndef ZSTD_COMPILER_H12#define ZSTD_COMPILER_H1314/*-*******************************************************15* Compiler specifics16*********************************************************/17/* force inlining */1819#if !defined(ZSTD_NO_INLINE)20#if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */21# define INLINE_KEYWORD inline22#else23# define INLINE_KEYWORD24#endif2526#if defined(__GNUC__) || defined(__ICCARM__)27# define FORCE_INLINE_ATTR __attribute__((always_inline))28#elif defined(_MSC_VER)29# define FORCE_INLINE_ATTR __forceinline30#else31# define FORCE_INLINE_ATTR32#endif3334#else3536#define INLINE_KEYWORD37#define FORCE_INLINE_ATTR3839#endif4041/**42* FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant43* parameters. They must be inlined for the compiler to eliminate the constant44* branches.45*/46#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR47/**48* HINT_INLINE is used to help the compiler generate better code. It is *not*49* used for "templates", so it can be tweaked based on the compilers50* performance.51*52* gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the53* always_inline attribute.54*55* clang up to 5.0.0 (trunk) benefit tremendously from the always_inline56* attribute.57*/58#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 559# define HINT_INLINE static INLINE_KEYWORD60#else61# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR62#endif6364/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */65#if defined(__GNUC__)66# define UNUSED_ATTR __attribute__((unused))67#else68# define UNUSED_ATTR69#endif7071/* force no inlining */72#ifdef _MSC_VER73# define FORCE_NOINLINE static __declspec(noinline)74#else75# if defined(__GNUC__) || defined(__ICCARM__)76# define FORCE_NOINLINE static __attribute__((__noinline__))77# else78# define FORCE_NOINLINE static79# endif80#endif8182/* target attribute */83#ifndef __has_attribute84#define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */85#endif86#if defined(__GNUC__) || defined(__ICCARM__)87# define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))88#else89# define TARGET_ATTRIBUTE(target)90#endif9192/* Enable runtime BMI2 dispatch based on the CPU.93* Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.94*/95#ifndef DYNAMIC_BMI296#if ((defined(__clang__) && __has_attribute(__target__)) \97|| (defined(__GNUC__) \98&& (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \99&& (defined(__x86_64__) || defined(_M_X86)) \100&& !defined(__BMI2__)101# define DYNAMIC_BMI2 1102#else103# define DYNAMIC_BMI2 0104#endif105#endif106107/* prefetch108* can be disabled, by declaring NO_PREFETCH build macro */109#if defined(NO_PREFETCH)110# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */111# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */112#else113# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */114# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */115# define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)116# define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)117# elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )118# define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)119# define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)120# else121# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */122# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */123# endif124#endif /* NO_PREFETCH */125126#define CACHELINE_SIZE 64127128#define PREFETCH_AREA(p, s) { \129const char* const _ptr = (const char*)(p); \130size_t const _size = (size_t)(s); \131size_t _pos; \132for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \133PREFETCH_L2(_ptr + _pos); \134} \135}136137/* vectorization138* older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */139#if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)140# if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)141# define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))142# else143# define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")144# endif145#else146# define DONT_VECTORIZE147#endif148149/* Tell the compiler that a branch is likely or unlikely.150* Only use these macros if it causes the compiler to generate better code.151* If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc152* and clang, please do.153*/154#if defined(__GNUC__)155#define LIKELY(x) (__builtin_expect((x), 1))156#define UNLIKELY(x) (__builtin_expect((x), 0))157#else158#define LIKELY(x) (x)159#define UNLIKELY(x) (x)160#endif161162/* disable warnings */163#ifdef _MSC_VER /* Visual Studio */164# include <intrin.h> /* For Visual 2005 */165# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */166# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */167# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */168# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */169# pragma warning(disable : 4324) /* disable: C4324: padded structure */170#endif171172#endif /* ZSTD_COMPILER_H */173174175