162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) Yann Collet, Facebook, Inc. 362306a36Sopenharmony_ci * All rights reserved. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * This source code is licensed under both the BSD-style license (found in the 662306a36Sopenharmony_ci * LICENSE file in the root directory of this source tree) and the GPLv2 (found 762306a36Sopenharmony_ci * in the COPYING file in the root directory of this source tree). 862306a36Sopenharmony_ci * You may select, at your option, one of the above-listed licenses. 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#ifndef ZSTD_COMPILER_H 1262306a36Sopenharmony_ci#define ZSTD_COMPILER_H 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include "portability_macros.h" 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci/*-******************************************************* 1762306a36Sopenharmony_ci* Compiler specifics 1862306a36Sopenharmony_ci*********************************************************/ 1962306a36Sopenharmony_ci/* force inlining */ 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#if !defined(ZSTD_NO_INLINE) 2262306a36Sopenharmony_ci#if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ 2362306a36Sopenharmony_ci# define INLINE_KEYWORD inline 2462306a36Sopenharmony_ci#else 2562306a36Sopenharmony_ci# define INLINE_KEYWORD 2662306a36Sopenharmony_ci#endif 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci#define FORCE_INLINE_ATTR __attribute__((always_inline)) 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci#else 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci#define INLINE_KEYWORD 3362306a36Sopenharmony_ci#define FORCE_INLINE_ATTR 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci#endif 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci/* 3862306a36Sopenharmony_ci On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC). 3962306a36Sopenharmony_ci This explicitly marks such functions as __cdecl so that the code will still compile 4062306a36Sopenharmony_ci if a CC other than __cdecl has been made the default. 4162306a36Sopenharmony_ci*/ 4262306a36Sopenharmony_ci#define WIN_CDECL 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci/* 4562306a36Sopenharmony_ci * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant 4662306a36Sopenharmony_ci * parameters. They must be inlined for the compiler to eliminate the constant 4762306a36Sopenharmony_ci * branches. 4862306a36Sopenharmony_ci */ 4962306a36Sopenharmony_ci#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR 5062306a36Sopenharmony_ci/* 5162306a36Sopenharmony_ci * HINT_INLINE is used to help the compiler generate better code. It is *not* 5262306a36Sopenharmony_ci * used for "templates", so it can be tweaked based on the compilers 5362306a36Sopenharmony_ci * performance. 5462306a36Sopenharmony_ci * 5562306a36Sopenharmony_ci * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the 5662306a36Sopenharmony_ci * always_inline attribute. 5762306a36Sopenharmony_ci * 5862306a36Sopenharmony_ci * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline 5962306a36Sopenharmony_ci * attribute. 6062306a36Sopenharmony_ci */ 6162306a36Sopenharmony_ci#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5 6262306a36Sopenharmony_ci# define HINT_INLINE static INLINE_KEYWORD 6362306a36Sopenharmony_ci#else 6462306a36Sopenharmony_ci# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR 6562306a36Sopenharmony_ci#endif 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */ 6862306a36Sopenharmony_ci#define UNUSED_ATTR __attribute__((unused)) 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/* force no inlining */ 7162306a36Sopenharmony_ci#define FORCE_NOINLINE static __attribute__((__noinline__)) 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci/* target attribute */ 7562306a36Sopenharmony_ci#define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci/* Target attribute for BMI2 dynamic dispatch. 7862306a36Sopenharmony_ci * Enable lzcnt, bmi, and bmi2. 7962306a36Sopenharmony_ci * We test for bmi1 & bmi2. lzcnt is included in bmi1. 8062306a36Sopenharmony_ci */ 8162306a36Sopenharmony_ci#define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2") 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci/* prefetch 8462306a36Sopenharmony_ci * can be disabled, by declaring NO_PREFETCH build macro */ 8562306a36Sopenharmony_ci#if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) ) 8662306a36Sopenharmony_ci# define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */) 8762306a36Sopenharmony_ci# define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */) 8862306a36Sopenharmony_ci#elif defined(__aarch64__) 8962306a36Sopenharmony_ci# define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))) 9062306a36Sopenharmony_ci# define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))) 9162306a36Sopenharmony_ci#else 9262306a36Sopenharmony_ci# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ 9362306a36Sopenharmony_ci# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ 9462306a36Sopenharmony_ci#endif /* NO_PREFETCH */ 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci#define CACHELINE_SIZE 64 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci#define PREFETCH_AREA(p, s) { \ 9962306a36Sopenharmony_ci const char* const _ptr = (const char*)(p); \ 10062306a36Sopenharmony_ci size_t const _size = (size_t)(s); \ 10162306a36Sopenharmony_ci size_t _pos; \ 10262306a36Sopenharmony_ci for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \ 10362306a36Sopenharmony_ci PREFETCH_L2(_ptr + _pos); \ 10462306a36Sopenharmony_ci } \ 10562306a36Sopenharmony_ci} 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci/* vectorization 10862306a36Sopenharmony_ci * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax, 10962306a36Sopenharmony_ci * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */ 11062306a36Sopenharmony_ci#if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__) 11162306a36Sopenharmony_ci# if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5) 11262306a36Sopenharmony_ci# define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize"))) 11362306a36Sopenharmony_ci# else 11462306a36Sopenharmony_ci# define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")") 11562306a36Sopenharmony_ci# endif 11662306a36Sopenharmony_ci#else 11762306a36Sopenharmony_ci# define DONT_VECTORIZE 11862306a36Sopenharmony_ci#endif 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci/* Tell the compiler that a branch is likely or unlikely. 12162306a36Sopenharmony_ci * Only use these macros if it causes the compiler to generate better code. 12262306a36Sopenharmony_ci * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc 12362306a36Sopenharmony_ci * and clang, please do. 12462306a36Sopenharmony_ci */ 12562306a36Sopenharmony_ci#define LIKELY(x) (__builtin_expect((x), 1)) 12662306a36Sopenharmony_ci#define UNLIKELY(x) (__builtin_expect((x), 0)) 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci#if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))) 12962306a36Sopenharmony_ci# define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); } 13062306a36Sopenharmony_ci#else 13162306a36Sopenharmony_ci# define ZSTD_UNREACHABLE { assert(0); } 13262306a36Sopenharmony_ci#endif 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci/* disable warnings */ 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci/*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/ 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci/* compile time determination of SIMD support */ 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci/* C-language Attributes are added in C23. */ 14262306a36Sopenharmony_ci#if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute) 14362306a36Sopenharmony_ci# define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x) 14462306a36Sopenharmony_ci#else 14562306a36Sopenharmony_ci# define ZSTD_HAS_C_ATTRIBUTE(x) 0 14662306a36Sopenharmony_ci#endif 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci/* Only use C++ attributes in C++. Some compilers report support for C++ 14962306a36Sopenharmony_ci * attributes when compiling with C. 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ci#define ZSTD_HAS_CPP_ATTRIBUTE(x) 0 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci/* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute. 15462306a36Sopenharmony_ci * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough 15562306a36Sopenharmony_ci * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough 15662306a36Sopenharmony_ci * - Else: __attribute__((__fallthrough__)) 15762306a36Sopenharmony_ci */ 15862306a36Sopenharmony_ci#define ZSTD_FALLTHROUGH fallthrough 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci/*-************************************************************** 16162306a36Sopenharmony_ci* Alignment check 16262306a36Sopenharmony_ci*****************************************************************/ 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci/* this test was initially positioned in mem.h, 16562306a36Sopenharmony_ci * but this file is removed (or replaced) for linux kernel 16662306a36Sopenharmony_ci * so it's now hosted in compiler.h, 16762306a36Sopenharmony_ci * which remains valid for both user & kernel spaces. 16862306a36Sopenharmony_ci */ 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci#ifndef ZSTD_ALIGNOF 17162306a36Sopenharmony_ci/* covers gcc, clang & MSVC */ 17262306a36Sopenharmony_ci/* note : this section must come first, before C11, 17362306a36Sopenharmony_ci * due to a limitation in the kernel source generator */ 17462306a36Sopenharmony_ci# define ZSTD_ALIGNOF(T) __alignof(T) 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci#endif /* ZSTD_ALIGNOF */ 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci/*-************************************************************** 17962306a36Sopenharmony_ci* Sanitizer 18062306a36Sopenharmony_ci*****************************************************************/ 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci#endif /* ZSTD_COMPILER_H */ 185