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