| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
| Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 11 | #ifndef ZSTD_COMPILER_H |
| 12 | #define ZSTD_COMPILER_H |
| 13 | |
| 14 | /*-******************************************************* |
| 15 | * Compiler specifics |
| 16 | *********************************************************/ |
| 17 | /* force inlining */ |
| W. Felix Handte | 9d5f396 | 2018-11-16 16:43:57 -0800 | [diff] [blame] | 18 | |
| 19 | #if !defined(ZSTD_NO_INLINE) |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 20 | #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 | |
| Joseph Chen | 3855bc4 | 2019-07-29 15:20:37 +0800 | [diff] [blame] | 26 | #if defined(__GNUC__) || defined(__ICCARM__) |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 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 Handte | 9d5f396 | 2018-11-16 16:43:57 -0800 | [diff] [blame] | 34 | #else |
| 35 | |
| 36 | #define INLINE_KEYWORD |
| 37 | #define FORCE_INLINE_ATTR |
| 38 | |
| 39 | #endif |
| 40 | |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 41 | /** |
| 42 | * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant |
| Josh Soref | a880ca2 | 2019-04-12 14:18:11 -0400 | [diff] [blame] | 43 | * parameters. They must be inlined for the compiler to eliminate the constant |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 44 | * 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 | |
| Nick Terrell | 5cb7615 | 2019-09-20 21:37:13 -0700 | [diff] [blame] | 64 | /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */ |
| 65 | #if defined(__GNUC__) |
| 66 | # define UNUSED_ATTR __attribute__((unused)) |
| 67 | #else |
| 68 | # define UNUSED_ATTR |
| 69 | #endif |
| 70 | |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 71 | /* force no inlining */ |
| 72 | #ifdef _MSC_VER |
| 73 | # define FORCE_NOINLINE static __declspec(noinline) |
| 74 | #else |
| Joseph Chen | 3855bc4 | 2019-07-29 15:20:37 +0800 | [diff] [blame] | 75 | # if defined(__GNUC__) || defined(__ICCARM__) |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 76 | # define FORCE_NOINLINE static __attribute__((__noinline__)) |
| 77 | # else |
| 78 | # define FORCE_NOINLINE static |
| 79 | # endif |
| 80 | #endif |
| 81 | |
| Nick Terrell | 4319132 | 2018-02-02 18:03:09 -0800 | [diff] [blame] | 82 | /* target attribute */ |
| Yann Collet | ad15c1b | 2018-03-23 19:04:48 -0700 | [diff] [blame] | 83 | #ifndef __has_attribute |
| 84 | #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ |
| 85 | #endif |
| Joseph Chen | 3855bc4 | 2019-07-29 15:20:37 +0800 | [diff] [blame] | 86 | #if defined(__GNUC__) || defined(__ICCARM__) |
| Nick Terrell | 4319132 | 2018-02-02 18:03:09 -0800 | [diff] [blame] | 87 | # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) |
| 88 | #else |
| 89 | # define TARGET_ATTRIBUTE(target) |
| 90 | #endif |
| 91 | |
| 92 | /* Enable runtime BMI2 dispatch based on the CPU. |
| Yann Collet | 45b09e7 | 2018-03-01 15:02:18 -0800 | [diff] [blame] | 93 | * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default. |
| Nick Terrell | 4319132 | 2018-02-02 18:03:09 -0800 | [diff] [blame] | 94 | */ |
| 95 | #ifndef DYNAMIC_BMI2 |
| taigacon | 2c3ad05 | 2018-04-24 06:41:50 +0800 | [diff] [blame] | 96 | #if ((defined(__clang__) && __has_attribute(__target__)) \ |
| Yann Collet | d02b44c | 2018-03-04 16:05:59 -0800 | [diff] [blame] | 97 | || (defined(__GNUC__) \ |
| taigacon | 2c3ad05 | 2018-04-24 06:41:50 +0800 | [diff] [blame] | 98 | && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \ |
| Yann Collet | d02b44c | 2018-03-04 16:05:59 -0800 | [diff] [blame] | 99 | && (defined(__x86_64__) || defined(_M_X86)) \ |
| 100 | && !defined(__BMI2__) |
| Nick Terrell | 4319132 | 2018-02-02 18:03:09 -0800 | [diff] [blame] | 101 | # define DYNAMIC_BMI2 1 |
| 102 | #else |
| 103 | # define DYNAMIC_BMI2 0 |
| 104 | #endif |
| 105 | #endif |
| 106 | |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 107 | /* prefetch |
| Yann Collet | 9126da5 | 2018-11-08 12:47:46 -0800 | [diff] [blame] | 108 | * can be disabled, by declaring NO_PREFETCH build macro */ |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 109 | #if defined(NO_PREFETCH) |
| Yann Collet | 9126da5 | 2018-11-08 12:47:46 -0800 | [diff] [blame] | 110 | # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ |
| Yann Collet | 626040a | 2018-11-12 17:05:32 -0800 | [diff] [blame] | 111 | # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 112 | #else |
| 113 | # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */ |
| 114 | # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */ |
| Yann Collet | 9126da5 | 2018-11-08 12:47:46 -0800 | [diff] [blame] | 115 | # define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0) |
| Yann Collet | 626040a | 2018-11-12 17:05:32 -0800 | [diff] [blame] | 116 | # define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1) |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 117 | # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) ) |
| Yann Collet | 9126da5 | 2018-11-08 12:47:46 -0800 | [diff] [blame] | 118 | # define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */) |
| Yann Collet | 626040a | 2018-11-12 17:05:32 -0800 | [diff] [blame] | 119 | # define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */) |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 120 | # else |
| Yann Collet | 9126da5 | 2018-11-08 12:47:46 -0800 | [diff] [blame] | 121 | # define PREFETCH_L1(ptr) (void)(ptr) /* disabled */ |
| Yann Collet | 626040a | 2018-11-12 17:05:32 -0800 | [diff] [blame] | 122 | # define PREFETCH_L2(ptr) (void)(ptr) /* disabled */ |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 123 | # endif |
| 124 | #endif /* NO_PREFETCH */ |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 125 | |
| Yann Collet | 4de344d | 2018-09-12 10:29:47 -0700 | [diff] [blame] | 126 | #define CACHELINE_SIZE 64 |
| 127 | |
| Yann Collet | 2618253 | 2018-09-12 16:15:37 -0700 | [diff] [blame] | 128 | #define PREFETCH_AREA(p, s) { \ |
| 129 | const char* const _ptr = (const char*)(p); \ |
| 130 | size_t const _size = (size_t)(s); \ |
| 131 | size_t _pos; \ |
| 132 | for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \ |
| Yann Collet | 626040a | 2018-11-12 17:05:32 -0800 | [diff] [blame] | 133 | PREFETCH_L2(_ptr + _pos); \ |
| Yann Collet | 2618253 | 2018-09-12 16:15:37 -0700 | [diff] [blame] | 134 | } \ |
| Yann Collet | 63a519d | 2018-09-11 17:23:44 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| Carl Woffenden | 901ea61 | 2019-08-21 17:49:17 +0200 | [diff] [blame] | 137 | /* vectorization |
| 138 | * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax */ |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 139 | #if !defined(__clang__) && defined(__GNUC__) |
| Carl Woffenden | 901ea61 | 2019-08-21 17:49:17 +0200 | [diff] [blame] | 140 | # if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5) |
| 141 | # define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize"))) |
| 142 | # else |
| 143 | # define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")") |
| 144 | # endif |
| mgrice | 812e8f2 | 2019-07-11 15:31:07 -0700 | [diff] [blame] | 145 | #else |
| 146 | # define DONT_VECTORIZE |
| 147 | #endif |
| 148 | |
| Nick Terrell | 718f00f | 2019-11-25 18:26:19 -0800 | [diff] [blame^] | 149 | /* Tell the compiler that a branch is likely or unlikely. |
| 150 | * Only use these macros if it causes the compiler to generate better code. |
| 151 | * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc |
| 152 | * and clang, please do. |
| 153 | */ |
| 154 | #if defined(__GNUC__) |
| 155 | #define LIKELY(x) (__builtin_expect((x), 1)) |
| 156 | #define UNLIKELY(x) (__builtin_expect((x), 0)) |
| 157 | #else |
| 158 | #define LIKELY(x) (x) |
| 159 | #define UNLIKELY(x) (x) |
| 160 | #endif |
| 161 | |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 162 | /* disable warnings */ |
| 163 | #ifdef _MSC_VER /* Visual Studio */ |
| 164 | # include <intrin.h> /* For Visual 2005 */ |
| 165 | # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */ |
| 166 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 167 | # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
| 168 | # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ |
| 169 | # pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
| 170 | #endif |
| 171 | |
| 172 | #endif /* ZSTD_COMPILER_H */ |