xref: /freebsd/sys/contrib/zstd/lib/common/compiler.h (revision 2b9c00cb6bd9392645dc8afca59cf57c42df4e2d)
10c16b537SWarner Losh /*
20c16b537SWarner Losh  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
30c16b537SWarner Losh  * All rights reserved.
40c16b537SWarner Losh  *
50c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh  */
100c16b537SWarner Losh 
110c16b537SWarner Losh #ifndef ZSTD_COMPILER_H
120c16b537SWarner Losh #define ZSTD_COMPILER_H
130c16b537SWarner Losh 
140c16b537SWarner Losh /*-*******************************************************
150c16b537SWarner Losh *  Compiler specifics
160c16b537SWarner Losh *********************************************************/
170c16b537SWarner Losh /* force inlining */
18a0483764SConrad Meyer 
19a0483764SConrad Meyer #if !defined(ZSTD_NO_INLINE)
200c16b537SWarner Losh #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
210c16b537SWarner Losh #  define INLINE_KEYWORD inline
220c16b537SWarner Losh #else
230c16b537SWarner Losh #  define INLINE_KEYWORD
240c16b537SWarner Losh #endif
250c16b537SWarner Losh 
260c16b537SWarner Losh #if defined(__GNUC__)
270c16b537SWarner Losh #  define FORCE_INLINE_ATTR __attribute__((always_inline))
280c16b537SWarner Losh #elif defined(_MSC_VER)
290c16b537SWarner Losh #  define FORCE_INLINE_ATTR __forceinline
300c16b537SWarner Losh #else
310c16b537SWarner Losh #  define FORCE_INLINE_ATTR
320c16b537SWarner Losh #endif
330c16b537SWarner Losh 
34a0483764SConrad Meyer #else
35a0483764SConrad Meyer 
36a0483764SConrad Meyer #define INLINE_KEYWORD
37a0483764SConrad Meyer #define FORCE_INLINE_ATTR
38a0483764SConrad Meyer 
39a0483764SConrad Meyer #endif
40a0483764SConrad Meyer 
410c16b537SWarner Losh /**
420c16b537SWarner Losh  * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
43*2b9c00cbSConrad Meyer  * parameters. They must be inlined for the compiler to eliminate the constant
440c16b537SWarner Losh  * branches.
450c16b537SWarner Losh  */
460c16b537SWarner Losh #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
470c16b537SWarner Losh /**
480c16b537SWarner Losh  * HINT_INLINE is used to help the compiler generate better code. It is *not*
490c16b537SWarner Losh  * used for "templates", so it can be tweaked based on the compilers
500c16b537SWarner Losh  * performance.
510c16b537SWarner Losh  *
520c16b537SWarner Losh  * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
530c16b537SWarner Losh  * always_inline attribute.
540c16b537SWarner Losh  *
550c16b537SWarner Losh  * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
560c16b537SWarner Losh  * attribute.
570c16b537SWarner Losh  */
580c16b537SWarner Losh #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
590c16b537SWarner Losh #  define HINT_INLINE static INLINE_KEYWORD
600c16b537SWarner Losh #else
610c16b537SWarner Losh #  define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
620c16b537SWarner Losh #endif
630c16b537SWarner Losh 
640c16b537SWarner Losh /* force no inlining */
650c16b537SWarner Losh #ifdef _MSC_VER
660c16b537SWarner Losh #  define FORCE_NOINLINE static __declspec(noinline)
670c16b537SWarner Losh #else
680c16b537SWarner Losh #  ifdef __GNUC__
690c16b537SWarner Losh #    define FORCE_NOINLINE static __attribute__((__noinline__))
700c16b537SWarner Losh #  else
710c16b537SWarner Losh #    define FORCE_NOINLINE static
720c16b537SWarner Losh #  endif
730c16b537SWarner Losh #endif
740c16b537SWarner Losh 
7519fcbaf1SConrad Meyer /* target attribute */
7619fcbaf1SConrad Meyer #ifndef __has_attribute
7719fcbaf1SConrad Meyer   #define __has_attribute(x) 0  /* Compatibility with non-clang compilers. */
7819fcbaf1SConrad Meyer #endif
7919fcbaf1SConrad Meyer #if defined(__GNUC__)
8019fcbaf1SConrad Meyer #  define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
8119fcbaf1SConrad Meyer #else
8219fcbaf1SConrad Meyer #  define TARGET_ATTRIBUTE(target)
8319fcbaf1SConrad Meyer #endif
8419fcbaf1SConrad Meyer 
8519fcbaf1SConrad Meyer /* Enable runtime BMI2 dispatch based on the CPU.
8619fcbaf1SConrad Meyer  * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
8719fcbaf1SConrad Meyer  */
8819fcbaf1SConrad Meyer #ifndef DYNAMIC_BMI2
890f743729SConrad Meyer   #if ((defined(__clang__) && __has_attribute(__target__)) \
9019fcbaf1SConrad Meyer       || (defined(__GNUC__) \
910f743729SConrad Meyer           && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
9219fcbaf1SConrad Meyer       && (defined(__x86_64__) || defined(_M_X86)) \
9319fcbaf1SConrad Meyer       && !defined(__BMI2__)
9419fcbaf1SConrad Meyer   #  define DYNAMIC_BMI2 1
9519fcbaf1SConrad Meyer   #else
9619fcbaf1SConrad Meyer   #  define DYNAMIC_BMI2 0
9719fcbaf1SConrad Meyer   #endif
9819fcbaf1SConrad Meyer #endif
9919fcbaf1SConrad Meyer 
1000f743729SConrad Meyer /* prefetch
101a0483764SConrad Meyer  * can be disabled, by declaring NO_PREFETCH build macro */
1020f743729SConrad Meyer #if defined(NO_PREFETCH)
103a0483764SConrad Meyer #  define PREFETCH_L1(ptr)  (void)(ptr)  /* disabled */
104a0483764SConrad Meyer #  define PREFETCH_L2(ptr)  (void)(ptr)  /* disabled */
1050f743729SConrad Meyer #else
1060c16b537SWarner Losh #  if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86))  /* _mm_prefetch() is not defined outside of x86/x64 */
1070c16b537SWarner Losh #    include <mmintrin.h>   /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
108a0483764SConrad Meyer #    define PREFETCH_L1(ptr)  _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
109a0483764SConrad Meyer #    define PREFETCH_L2(ptr)  _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
1100f743729SConrad Meyer #  elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
111a0483764SConrad Meyer #    define PREFETCH_L1(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
112a0483764SConrad Meyer #    define PREFETCH_L2(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
1130c16b537SWarner Losh #  else
114a0483764SConrad Meyer #    define PREFETCH_L1(ptr) (void)(ptr)  /* disabled */
115a0483764SConrad Meyer #    define PREFETCH_L2(ptr) (void)(ptr)  /* disabled */
1160c16b537SWarner Losh #  endif
1170f743729SConrad Meyer #endif  /* NO_PREFETCH */
1180f743729SConrad Meyer 
1190f743729SConrad Meyer #define CACHELINE_SIZE 64
1200f743729SConrad Meyer 
1210f743729SConrad Meyer #define PREFETCH_AREA(p, s)  {            \
1220f743729SConrad Meyer     const char* const _ptr = (const char*)(p);  \
1230f743729SConrad Meyer     size_t const _size = (size_t)(s);     \
1240f743729SConrad Meyer     size_t _pos;                          \
1250f743729SConrad Meyer     for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) {  \
126a0483764SConrad Meyer         PREFETCH_L2(_ptr + _pos);         \
1270f743729SConrad Meyer     }                                     \
1280f743729SConrad Meyer }
1290c16b537SWarner Losh 
1300c16b537SWarner Losh /* disable warnings */
1310c16b537SWarner Losh #ifdef _MSC_VER    /* Visual Studio */
1320c16b537SWarner Losh #  include <intrin.h>                    /* For Visual 2005 */
1330c16b537SWarner Losh #  pragma warning(disable : 4100)        /* disable: C4100: unreferenced formal parameter */
1340c16b537SWarner Losh #  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
1350c16b537SWarner Losh #  pragma warning(disable : 4204)        /* disable: C4204: non-constant aggregate initializer */
1360c16b537SWarner Losh #  pragma warning(disable : 4214)        /* disable: C4214: non-int bitfields */
1370c16b537SWarner Losh #  pragma warning(disable : 4324)        /* disable: C4324: padded structure */
1380c16b537SWarner Losh #endif
1390c16b537SWarner Losh 
1400c16b537SWarner Losh #endif /* ZSTD_COMPILER_H */
141