Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) Yann Collet, Facebook, Inc.
0003  * All rights reserved.
0004  *
0005  * This source code is licensed under both the BSD-style license (found in the
0006  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
0007  * in the COPYING file in the root directory of this source tree).
0008  * You may select, at your option, one of the above-listed licenses.
0009  */
0010 
0011 #ifndef ZSTD_COMPILER_H
0012 #define ZSTD_COMPILER_H
0013 
0014 /*-*******************************************************
0015 *  Compiler specifics
0016 *********************************************************/
0017 /* force inlining */
0018 
0019 #if !defined(ZSTD_NO_INLINE)
0020 #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
0021 #  define INLINE_KEYWORD inline
0022 #else
0023 #  define INLINE_KEYWORD
0024 #endif
0025 
0026 #define FORCE_INLINE_ATTR __attribute__((always_inline))
0027 
0028 #else
0029 
0030 #define INLINE_KEYWORD
0031 #define FORCE_INLINE_ATTR
0032 
0033 #endif
0034 
0035 /*
0036   On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
0037   This explictly marks such functions as __cdecl so that the code will still compile
0038   if a CC other than __cdecl has been made the default.
0039 */
0040 #define WIN_CDECL
0041 
0042 /*
0043  * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
0044  * parameters. They must be inlined for the compiler to eliminate the constant
0045  * branches.
0046  */
0047 #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
0048 /*
0049  * HINT_INLINE is used to help the compiler generate better code. It is *not*
0050  * used for "templates", so it can be tweaked based on the compilers
0051  * performance.
0052  *
0053  * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
0054  * always_inline attribute.
0055  *
0056  * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
0057  * attribute.
0058  */
0059 #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
0060 #  define HINT_INLINE static INLINE_KEYWORD
0061 #else
0062 #  define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
0063 #endif
0064 
0065 /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
0066 #define UNUSED_ATTR __attribute__((unused))
0067 
0068 /* force no inlining */
0069 #define FORCE_NOINLINE static __attribute__((__noinline__))
0070 
0071 
0072 /* target attribute */
0073 #ifndef __has_attribute
0074   #define __has_attribute(x) 0  /* Compatibility with non-clang compilers. */
0075 #endif
0076 #define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
0077 
0078 /* Enable runtime BMI2 dispatch based on the CPU.
0079  * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
0080  */
0081 #ifndef DYNAMIC_BMI2
0082   #if ((defined(__clang__) && __has_attribute(__target__)) \
0083       || (defined(__GNUC__) \
0084           && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
0085       && (defined(__x86_64__) || defined(_M_X86)) \
0086       && !defined(__BMI2__)
0087   #  define DYNAMIC_BMI2 1
0088   #else
0089   #  define DYNAMIC_BMI2 0
0090   #endif
0091 #endif
0092 
0093 /* prefetch
0094  * can be disabled, by declaring NO_PREFETCH build macro */
0095 #if ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
0096 #  define PREFETCH_L1(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
0097 #  define PREFETCH_L2(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
0098 #elif defined(__aarch64__)
0099 #  define PREFETCH_L1(ptr)  __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
0100 #  define PREFETCH_L2(ptr)  __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
0101 #else
0102 #  define PREFETCH_L1(ptr) (void)(ptr)  /* disabled */
0103 #  define PREFETCH_L2(ptr) (void)(ptr)  /* disabled */
0104 #endif  /* NO_PREFETCH */
0105 
0106 #define CACHELINE_SIZE 64
0107 
0108 #define PREFETCH_AREA(p, s)  {            \
0109     const char* const _ptr = (const char*)(p);  \
0110     size_t const _size = (size_t)(s);     \
0111     size_t _pos;                          \
0112     for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) {  \
0113         PREFETCH_L2(_ptr + _pos);         \
0114     }                                     \
0115 }
0116 
0117 /* vectorization
0118  * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */
0119 #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__)
0120 #  if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
0121 #    define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
0122 #  else
0123 #    define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
0124 #  endif
0125 #else
0126 #  define DONT_VECTORIZE
0127 #endif
0128 
0129 /* Tell the compiler that a branch is likely or unlikely.
0130  * Only use these macros if it causes the compiler to generate better code.
0131  * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
0132  * and clang, please do.
0133  */
0134 #define LIKELY(x) (__builtin_expect((x), 1))
0135 #define UNLIKELY(x) (__builtin_expect((x), 0))
0136 
0137 /* disable warnings */
0138 
0139 /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/
0140 
0141 
0142 /* compat. with non-clang compilers */
0143 #ifndef __has_builtin
0144 #  define __has_builtin(x) 0
0145 #endif
0146 
0147 /* compat. with non-clang compilers */
0148 #ifndef __has_feature
0149 #  define __has_feature(x) 0
0150 #endif
0151 
0152 /* C-language Attributes are added in C23. */
0153 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
0154 # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
0155 #else
0156 # define ZSTD_HAS_C_ATTRIBUTE(x) 0
0157 #endif
0158 
0159 /* Only use C++ attributes in C++. Some compilers report support for C++
0160  * attributes when compiling with C.
0161  */
0162 #define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
0163 
0164 /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
0165  * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
0166  * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
0167  * - Else: __attribute__((__fallthrough__))
0168  */
0169 #define ZSTD_FALLTHROUGH fallthrough
0170 
0171 /* detects whether we are being compiled under msan */
0172 
0173 
0174 /* detects whether we are being compiled under asan */
0175 
0176 
0177 #endif /* ZSTD_COMPILER_H */