| 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 */ |
| 18 | #if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ |
| 19 | # define INLINE_KEYWORD inline |
| 20 | #else |
| 21 | # define INLINE_KEYWORD |
| 22 | #endif |
| 23 | |
| 24 | #if defined(__GNUC__) |
| 25 | # define FORCE_INLINE_ATTR __attribute__((always_inline)) |
| 26 | #elif defined(_MSC_VER) |
| 27 | # define FORCE_INLINE_ATTR __forceinline |
| 28 | #else |
| 29 | # define FORCE_INLINE_ATTR |
| 30 | #endif |
| 31 | |
| 32 | /** |
| 33 | * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant |
| 34 | * parameters. They must be inlined for the compiler to elimininate the constant |
| 35 | * branches. |
| 36 | */ |
| 37 | #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR |
| 38 | /** |
| 39 | * HINT_INLINE is used to help the compiler generate better code. It is *not* |
| 40 | * used for "templates", so it can be tweaked based on the compilers |
| 41 | * performance. |
| 42 | * |
| 43 | * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the |
| 44 | * always_inline attribute. |
| 45 | * |
| 46 | * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline |
| 47 | * attribute. |
| 48 | */ |
| 49 | #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5 |
| 50 | # define HINT_INLINE static INLINE_KEYWORD |
| 51 | #else |
| 52 | # define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR |
| 53 | #endif |
| 54 | |
| 55 | /* force no inlining */ |
| 56 | #ifdef _MSC_VER |
| 57 | # define FORCE_NOINLINE static __declspec(noinline) |
| 58 | #else |
| 59 | # ifdef __GNUC__ |
| 60 | # define FORCE_NOINLINE static __attribute__((__noinline__)) |
| 61 | # else |
| 62 | # define FORCE_NOINLINE static |
| 63 | # endif |
| 64 | #endif |
| 65 | |
| Nick Terrell | 4319132 | 2018-02-02 18:03:09 -0800 | [diff] [blame] | 66 | /* target attribute */ |
| Yann Collet | ad15c1b | 2018-03-23 19:04:48 -0700 | [diff] [blame] | 67 | #ifndef __has_attribute |
| 68 | #define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ |
| 69 | #endif |
| Nick Terrell | 4319132 | 2018-02-02 18:03:09 -0800 | [diff] [blame] | 70 | #if defined(__GNUC__) |
| 71 | # define TARGET_ATTRIBUTE(target) __attribute__((__target__(target))) |
| 72 | #else |
| 73 | # define TARGET_ATTRIBUTE(target) |
| 74 | #endif |
| 75 | |
| 76 | /* Enable runtime BMI2 dispatch based on the CPU. |
| Yann Collet | 45b09e7 | 2018-03-01 15:02:18 -0800 | [diff] [blame] | 77 | * 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] | 78 | */ |
| 79 | #ifndef DYNAMIC_BMI2 |
| taigacon | 2c3ad05 | 2018-04-24 06:41:50 +0800 | [diff] [blame] | 80 | #if ((defined(__clang__) && __has_attribute(__target__)) \ |
| Yann Collet | d02b44c | 2018-03-04 16:05:59 -0800 | [diff] [blame] | 81 | || (defined(__GNUC__) \ |
| taigacon | 2c3ad05 | 2018-04-24 06:41:50 +0800 | [diff] [blame] | 82 | && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \ |
| Yann Collet | d02b44c | 2018-03-04 16:05:59 -0800 | [diff] [blame] | 83 | && (defined(__x86_64__) || defined(_M_X86)) \ |
| 84 | && !defined(__BMI2__) |
| Nick Terrell | 4319132 | 2018-02-02 18:03:09 -0800 | [diff] [blame] | 85 | # define DYNAMIC_BMI2 1 |
| 86 | #else |
| 87 | # define DYNAMIC_BMI2 0 |
| 88 | #endif |
| 89 | #endif |
| 90 | |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 91 | /* prefetch |
| 92 | * can be disabled, by declaring NO_PREFETCH macro */ |
| 93 | #if defined(NO_PREFETCH) |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 94 | # define PREFETCH(ptr) /* disabled */ |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 95 | #else |
| 96 | # if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */ |
| 97 | # include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */ |
| Yann Collet | 4de344d | 2018-09-12 10:29:47 -0700 | [diff] [blame^] | 98 | # define PREFETCH(ptr) _mm_prefetch((const char*)ptr, _MM_HINT_T1) |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 99 | # elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) ) |
| Yann Collet | 4de344d | 2018-09-12 10:29:47 -0700 | [diff] [blame^] | 100 | # define PREFETCH(ptr) __builtin_prefetch(ptr, 0 /* rw==read */, 2 /* locality */) |
| Yann Collet | bbd78df | 2018-07-06 17:06:04 -0700 | [diff] [blame] | 101 | # else |
| 102 | # define PREFETCH(ptr) /* disabled */ |
| 103 | # endif |
| 104 | #endif /* NO_PREFETCH */ |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 105 | |
| Yann Collet | 4de344d | 2018-09-12 10:29:47 -0700 | [diff] [blame^] | 106 | #define CACHELINE_SIZE 64 |
| 107 | |
| 108 | #define PREFETCH_AREA(ptr, size) { \ |
| 109 | size_t pos; \ |
| 110 | for (pos=0; pos<size; pos+=CACHELINE_SIZE) { \ |
| 111 | PREFETCH( (const char*)ptr + pos); \ |
| 112 | } \ |
| Yann Collet | 63a519d | 2018-09-11 17:23:44 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| Nick Terrell | 565e925 | 2017-08-14 17:20:50 -0700 | [diff] [blame] | 115 | /* disable warnings */ |
| 116 | #ifdef _MSC_VER /* Visual Studio */ |
| 117 | # include <intrin.h> /* For Visual 2005 */ |
| 118 | # pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */ |
| 119 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 120 | # pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */ |
| 121 | # pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */ |
| 122 | # pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
| 123 | #endif |
| 124 | |
| 125 | #endif /* ZSTD_COMPILER_H */ |