blob: 69eb7b91030cf3cbd7a69bcff5ed140d638518c3 [file] [log] [blame]
Nick Terrell565e9252017-08-14 17:20:50 -07001#ifndef ZSTD_COMPILER_H
2#define ZSTD_COMPILER_H
3
4/*-*******************************************************
5* Compiler specifics
6*********************************************************/
7/* force inlining */
8#if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
9# define INLINE_KEYWORD inline
10#else
11# define INLINE_KEYWORD
12#endif
13
14#if defined(__GNUC__)
15# define FORCE_INLINE_ATTR __attribute__((always_inline))
16#elif defined(_MSC_VER)
17# define FORCE_INLINE_ATTR __forceinline
18#else
19# define FORCE_INLINE_ATTR
20#endif
21
22/**
23 * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
24 * parameters. They must be inlined for the compiler to elimininate the constant
25 * branches.
26 */
27#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
28/**
29 * HINT_INLINE is used to help the compiler generate better code. It is *not*
30 * used for "templates", so it can be tweaked based on the compilers
31 * performance.
32 *
33 * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
34 * always_inline attribute.
35 *
36 * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
37 * attribute.
38 */
39#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
40# define HINT_INLINE static INLINE_KEYWORD
41#else
42# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
43#endif
44
45/* force no inlining */
46#ifdef _MSC_VER
47# define FORCE_NOINLINE static __declspec(noinline)
48#else
49# ifdef __GNUC__
50# define FORCE_NOINLINE static __attribute__((__noinline__))
51# else
52# define FORCE_NOINLINE static
53# endif
54#endif
55
56/* prefetch */
57#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
58# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
59# define PREFETCH(ptr) _mm_prefetch((const char*)ptr, _MM_HINT_T0)
60#elif defined(__GNUC__)
61# define PREFETCH(ptr) __builtin_prefetch(ptr, 0, 0)
62#else
63# define PREFETCH(ptr) /* disabled */
64#endif
65
66/* disable warnings */
67#ifdef _MSC_VER /* Visual Studio */
68# include <intrin.h> /* For Visual 2005 */
69# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
70# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
71# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
72# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
73# pragma warning(disable : 4324) /* disable: C4324: padded structure */
74#endif
75
76#endif /* ZSTD_COMPILER_H */