blob: 87bf51ae8c896c1af9411d214a2e2c9a9e04f07a [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
2 * Copyright (c) 2016-present, 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet32fb4072017-08-18 16:52:05 -07009 */
10
Nick Terrell565e9252017-08-14 17:20:50 -070011#ifndef ZSTD_COMPILER_H
12#define ZSTD_COMPILER_H
13
14/*-*******************************************************
15* Compiler specifics
16*********************************************************/
17/* force inlining */
W. Felix Handte9d5f3962018-11-16 16:43:57 -080018
19#if !defined(ZSTD_NO_INLINE)
Nick Terrell565e9252017-08-14 17:20:50 -070020#if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
21# define INLINE_KEYWORD inline
22#else
23# define INLINE_KEYWORD
24#endif
25
26#if defined(__GNUC__)
27# define FORCE_INLINE_ATTR __attribute__((always_inline))
28#elif defined(_MSC_VER)
29# define FORCE_INLINE_ATTR __forceinline
30#else
31# define FORCE_INLINE_ATTR
32#endif
33
W. Felix Handte9d5f3962018-11-16 16:43:57 -080034#else
35
36#define INLINE_KEYWORD
37#define FORCE_INLINE_ATTR
38
39#endif
40
Nick Terrell565e9252017-08-14 17:20:50 -070041/**
42 * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
Josh Sorefa880ca22019-04-12 14:18:11 -040043 * parameters. They must be inlined for the compiler to eliminate the constant
Nick Terrell565e9252017-08-14 17:20:50 -070044 * branches.
45 */
46#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
47/**
48 * HINT_INLINE is used to help the compiler generate better code. It is *not*
49 * used for "templates", so it can be tweaked based on the compilers
50 * performance.
51 *
52 * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
53 * always_inline attribute.
54 *
55 * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
56 * attribute.
57 */
58#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
59# define HINT_INLINE static INLINE_KEYWORD
60#else
61# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
62#endif
63
64/* force no inlining */
65#ifdef _MSC_VER
66# define FORCE_NOINLINE static __declspec(noinline)
67#else
68# ifdef __GNUC__
69# define FORCE_NOINLINE static __attribute__((__noinline__))
70# else
71# define FORCE_NOINLINE static
72# endif
73#endif
74
Nick Terrell43191322018-02-02 18:03:09 -080075/* target attribute */
Yann Colletad15c1b2018-03-23 19:04:48 -070076#ifndef __has_attribute
77 #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
78#endif
Nick Terrell43191322018-02-02 18:03:09 -080079#if defined(__GNUC__)
80# define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
81#else
82# define TARGET_ATTRIBUTE(target)
83#endif
84
85/* Enable runtime BMI2 dispatch based on the CPU.
Yann Collet45b09e72018-03-01 15:02:18 -080086 * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
Nick Terrell43191322018-02-02 18:03:09 -080087 */
88#ifndef DYNAMIC_BMI2
taigacon2c3ad052018-04-24 06:41:50 +080089 #if ((defined(__clang__) && __has_attribute(__target__)) \
Yann Colletd02b44c2018-03-04 16:05:59 -080090 || (defined(__GNUC__) \
taigacon2c3ad052018-04-24 06:41:50 +080091 && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
Yann Colletd02b44c2018-03-04 16:05:59 -080092 && (defined(__x86_64__) || defined(_M_X86)) \
93 && !defined(__BMI2__)
Nick Terrell43191322018-02-02 18:03:09 -080094 # define DYNAMIC_BMI2 1
95 #else
96 # define DYNAMIC_BMI2 0
97 #endif
98#endif
99
Yann Colletbbd78df2018-07-06 17:06:04 -0700100/* prefetch
Yann Collet9126da52018-11-08 12:47:46 -0800101 * can be disabled, by declaring NO_PREFETCH build macro */
Yann Colletbbd78df2018-07-06 17:06:04 -0700102#if defined(NO_PREFETCH)
Yann Collet9126da52018-11-08 12:47:46 -0800103# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
Yann Collet626040a2018-11-12 17:05:32 -0800104# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
Yann Colletbbd78df2018-07-06 17:06:04 -0700105#else
106# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
107# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
Yann Collet9126da52018-11-08 12:47:46 -0800108# define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
Yann Collet626040a2018-11-12 17:05:32 -0800109# define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
Yann Colletbbd78df2018-07-06 17:06:04 -0700110# elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
Yann Collet9126da52018-11-08 12:47:46 -0800111# define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
Yann Collet626040a2018-11-12 17:05:32 -0800112# define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
Yann Colletbbd78df2018-07-06 17:06:04 -0700113# else
Yann Collet9126da52018-11-08 12:47:46 -0800114# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
Yann Collet626040a2018-11-12 17:05:32 -0800115# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
Yann Colletbbd78df2018-07-06 17:06:04 -0700116# endif
117#endif /* NO_PREFETCH */
Nick Terrell565e9252017-08-14 17:20:50 -0700118
Yann Collet4de344d2018-09-12 10:29:47 -0700119#define CACHELINE_SIZE 64
120
Yann Collet26182532018-09-12 16:15:37 -0700121#define PREFETCH_AREA(p, s) { \
122 const char* const _ptr = (const char*)(p); \
123 size_t const _size = (size_t)(s); \
124 size_t _pos; \
125 for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
Yann Collet626040a2018-11-12 17:05:32 -0800126 PREFETCH_L2(_ptr + _pos); \
Yann Collet26182532018-09-12 16:15:37 -0700127 } \
Yann Collet63a519d2018-09-11 17:23:44 -0700128}
129
mgrice812e8f22019-07-11 15:31:07 -0700130/* vectorization */
131#if !defined(__clang__) && defined(__GNUC__)
132# define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
133#else
134# define DONT_VECTORIZE
135#endif
136
Nick Terrell565e9252017-08-14 17:20:50 -0700137/* disable warnings */
138#ifdef _MSC_VER /* Visual Studio */
139# include <intrin.h> /* For Visual 2005 */
140# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
141# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
142# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
143# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
144# pragma warning(disable : 4324) /* disable: C4324: padded structure */
145#endif
146
147#endif /* ZSTD_COMPILER_H */