xref: /freebsd/sys/contrib/zstd/lib/common/compiler.h (revision 19fcbaf1424b464269f1a7621fab747bb75afc36)
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 */
180c16b537SWarner Losh #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
190c16b537SWarner Losh #  define INLINE_KEYWORD inline
200c16b537SWarner Losh #else
210c16b537SWarner Losh #  define INLINE_KEYWORD
220c16b537SWarner Losh #endif
230c16b537SWarner Losh 
240c16b537SWarner Losh #if defined(__GNUC__)
250c16b537SWarner Losh #  define FORCE_INLINE_ATTR __attribute__((always_inline))
260c16b537SWarner Losh #elif defined(_MSC_VER)
270c16b537SWarner Losh #  define FORCE_INLINE_ATTR __forceinline
280c16b537SWarner Losh #else
290c16b537SWarner Losh #  define FORCE_INLINE_ATTR
300c16b537SWarner Losh #endif
310c16b537SWarner Losh 
320c16b537SWarner Losh /**
330c16b537SWarner Losh  * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
340c16b537SWarner Losh  * parameters. They must be inlined for the compiler to elimininate the constant
350c16b537SWarner Losh  * branches.
360c16b537SWarner Losh  */
370c16b537SWarner Losh #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
380c16b537SWarner Losh /**
390c16b537SWarner Losh  * HINT_INLINE is used to help the compiler generate better code. It is *not*
400c16b537SWarner Losh  * used for "templates", so it can be tweaked based on the compilers
410c16b537SWarner Losh  * performance.
420c16b537SWarner Losh  *
430c16b537SWarner Losh  * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
440c16b537SWarner Losh  * always_inline attribute.
450c16b537SWarner Losh  *
460c16b537SWarner Losh  * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
470c16b537SWarner Losh  * attribute.
480c16b537SWarner Losh  */
490c16b537SWarner Losh #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
500c16b537SWarner Losh #  define HINT_INLINE static INLINE_KEYWORD
510c16b537SWarner Losh #else
520c16b537SWarner Losh #  define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
530c16b537SWarner Losh #endif
540c16b537SWarner Losh 
550c16b537SWarner Losh /* force no inlining */
560c16b537SWarner Losh #ifdef _MSC_VER
570c16b537SWarner Losh #  define FORCE_NOINLINE static __declspec(noinline)
580c16b537SWarner Losh #else
590c16b537SWarner Losh #  ifdef __GNUC__
600c16b537SWarner Losh #    define FORCE_NOINLINE static __attribute__((__noinline__))
610c16b537SWarner Losh #  else
620c16b537SWarner Losh #    define FORCE_NOINLINE static
630c16b537SWarner Losh #  endif
640c16b537SWarner Losh #endif
650c16b537SWarner Losh 
66*19fcbaf1SConrad Meyer /* target attribute */
67*19fcbaf1SConrad Meyer #ifndef __has_attribute
68*19fcbaf1SConrad Meyer   #define __has_attribute(x) 0  /* Compatibility with non-clang compilers. */
69*19fcbaf1SConrad Meyer #endif
70*19fcbaf1SConrad Meyer #if defined(__GNUC__)
71*19fcbaf1SConrad Meyer #  define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
72*19fcbaf1SConrad Meyer #else
73*19fcbaf1SConrad Meyer #  define TARGET_ATTRIBUTE(target)
74*19fcbaf1SConrad Meyer #endif
75*19fcbaf1SConrad Meyer 
76*19fcbaf1SConrad Meyer /* Enable runtime BMI2 dispatch based on the CPU.
77*19fcbaf1SConrad Meyer  * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
78*19fcbaf1SConrad Meyer  */
79*19fcbaf1SConrad Meyer #ifndef DYNAMIC_BMI2
80*19fcbaf1SConrad Meyer   #if (defined(__clang__) && __has_attribute(__target__)) \
81*19fcbaf1SConrad Meyer       || (defined(__GNUC__) \
82*19fcbaf1SConrad Meyer           && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) \
83*19fcbaf1SConrad Meyer       && (defined(__x86_64__) || defined(_M_X86)) \
84*19fcbaf1SConrad Meyer       && !defined(__BMI2__)
85*19fcbaf1SConrad Meyer   #  define DYNAMIC_BMI2 1
86*19fcbaf1SConrad Meyer   #else
87*19fcbaf1SConrad Meyer   #  define DYNAMIC_BMI2 0
88*19fcbaf1SConrad Meyer   #endif
89*19fcbaf1SConrad Meyer #endif
90*19fcbaf1SConrad Meyer 
910c16b537SWarner Losh /* prefetch */
920c16b537SWarner Losh #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86))  /* _mm_prefetch() is not defined outside of x86/x64 */
930c16b537SWarner Losh #  include <mmintrin.h>   /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
940c16b537SWarner Losh #  define PREFETCH(ptr)   _mm_prefetch((const char*)ptr, _MM_HINT_T0)
950c16b537SWarner Losh #elif defined(__GNUC__)
960c16b537SWarner Losh #  define PREFETCH(ptr)   __builtin_prefetch(ptr, 0, 0)
970c16b537SWarner Losh #else
980c16b537SWarner Losh #  define PREFETCH(ptr)   /* disabled */
990c16b537SWarner Losh #endif
1000c16b537SWarner Losh 
1010c16b537SWarner Losh /* disable warnings */
1020c16b537SWarner Losh #ifdef _MSC_VER    /* Visual Studio */
1030c16b537SWarner Losh #  include <intrin.h>                    /* For Visual 2005 */
1040c16b537SWarner Losh #  pragma warning(disable : 4100)        /* disable: C4100: unreferenced formal parameter */
1050c16b537SWarner Losh #  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
1060c16b537SWarner Losh #  pragma warning(disable : 4204)        /* disable: C4204: non-constant aggregate initializer */
1070c16b537SWarner Losh #  pragma warning(disable : 4214)        /* disable: C4214: non-int bitfields */
1080c16b537SWarner Losh #  pragma warning(disable : 4324)        /* disable: C4324: padded structure */
1090c16b537SWarner Losh #endif
1100c16b537SWarner Losh 
1110c16b537SWarner Losh #endif /* ZSTD_COMPILER_H */
112