2017-08-18 23:52:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2017-08-18 23:52:05 +00:00
|
|
|
*/
|
|
|
|
|
2017-08-15 00:20:50 +00:00
|
|
|
#ifndef ZSTD_COMPILER_H
|
|
|
|
#define ZSTD_COMPILER_H
|
|
|
|
|
|
|
|
/*-*******************************************************
|
|
|
|
* Compiler specifics
|
|
|
|
*********************************************************/
|
|
|
|
/* force inlining */
|
2018-11-17 00:43:57 +00:00
|
|
|
|
|
|
|
#if !defined(ZSTD_NO_INLINE)
|
2017-08-15 00:20:50 +00:00
|
|
|
#if defined (__GNUC__) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */
|
|
|
|
# define INLINE_KEYWORD inline
|
|
|
|
#else
|
|
|
|
# define INLINE_KEYWORD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
# define FORCE_INLINE_ATTR __attribute__((always_inline))
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
# define FORCE_INLINE_ATTR __forceinline
|
|
|
|
#else
|
|
|
|
# define FORCE_INLINE_ATTR
|
|
|
|
#endif
|
|
|
|
|
2018-11-17 00:43:57 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define INLINE_KEYWORD
|
|
|
|
#define FORCE_INLINE_ATTR
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-08-15 00:20:50 +00:00
|
|
|
/**
|
|
|
|
* FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
|
2019-04-12 18:18:11 +00:00
|
|
|
* parameters. They must be inlined for the compiler to eliminate the constant
|
2017-08-15 00:20:50 +00:00
|
|
|
* branches.
|
|
|
|
*/
|
|
|
|
#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
|
|
|
|
/**
|
|
|
|
* HINT_INLINE is used to help the compiler generate better code. It is *not*
|
|
|
|
* used for "templates", so it can be tweaked based on the compilers
|
|
|
|
* performance.
|
|
|
|
*
|
|
|
|
* gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
|
|
|
|
* always_inline attribute.
|
|
|
|
*
|
|
|
|
* clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
|
|
|
|
* attribute.
|
|
|
|
*/
|
|
|
|
#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
|
|
|
|
# define HINT_INLINE static INLINE_KEYWORD
|
|
|
|
#else
|
|
|
|
# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* force no inlining */
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define FORCE_NOINLINE static __declspec(noinline)
|
|
|
|
#else
|
|
|
|
# ifdef __GNUC__
|
|
|
|
# define FORCE_NOINLINE static __attribute__((__noinline__))
|
|
|
|
# else
|
|
|
|
# define FORCE_NOINLINE static
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2018-02-03 02:03:09 +00:00
|
|
|
/* target attribute */
|
2018-03-24 02:04:48 +00:00
|
|
|
#ifndef __has_attribute
|
|
|
|
#define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */
|
|
|
|
#endif
|
2018-02-03 02:03:09 +00:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
# define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
|
|
|
|
#else
|
|
|
|
# define TARGET_ATTRIBUTE(target)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Enable runtime BMI2 dispatch based on the CPU.
|
2018-03-01 23:02:18 +00:00
|
|
|
* Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
|
2018-02-03 02:03:09 +00:00
|
|
|
*/
|
|
|
|
#ifndef DYNAMIC_BMI2
|
2018-04-23 22:41:50 +00:00
|
|
|
#if ((defined(__clang__) && __has_attribute(__target__)) \
|
2018-03-05 00:05:59 +00:00
|
|
|
|| (defined(__GNUC__) \
|
2018-04-23 22:41:50 +00:00
|
|
|
&& (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
|
2018-03-05 00:05:59 +00:00
|
|
|
&& (defined(__x86_64__) || defined(_M_X86)) \
|
|
|
|
&& !defined(__BMI2__)
|
2018-02-03 02:03:09 +00:00
|
|
|
# define DYNAMIC_BMI2 1
|
|
|
|
#else
|
|
|
|
# define DYNAMIC_BMI2 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-07-07 00:06:04 +00:00
|
|
|
/* prefetch
|
2018-11-08 20:47:46 +00:00
|
|
|
* can be disabled, by declaring NO_PREFETCH build macro */
|
2018-07-07 00:06:04 +00:00
|
|
|
#if defined(NO_PREFETCH)
|
2018-11-08 20:47:46 +00:00
|
|
|
# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
|
2018-11-13 01:05:32 +00:00
|
|
|
# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
|
2018-07-07 00:06:04 +00:00
|
|
|
#else
|
|
|
|
# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
|
|
|
|
# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
|
2018-11-08 20:47:46 +00:00
|
|
|
# define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
|
2018-11-13 01:05:32 +00:00
|
|
|
# define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
|
2018-07-07 00:06:04 +00:00
|
|
|
# elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
|
2018-11-08 20:47:46 +00:00
|
|
|
# define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
|
2018-11-13 01:05:32 +00:00
|
|
|
# define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
|
2018-07-07 00:06:04 +00:00
|
|
|
# else
|
2018-11-08 20:47:46 +00:00
|
|
|
# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
|
2018-11-13 01:05:32 +00:00
|
|
|
# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
|
2018-07-07 00:06:04 +00:00
|
|
|
# endif
|
|
|
|
#endif /* NO_PREFETCH */
|
2017-08-15 00:20:50 +00:00
|
|
|
|
2018-09-12 17:29:47 +00:00
|
|
|
#define CACHELINE_SIZE 64
|
|
|
|
|
2018-09-12 23:15:37 +00:00
|
|
|
#define PREFETCH_AREA(p, s) { \
|
|
|
|
const char* const _ptr = (const char*)(p); \
|
|
|
|
size_t const _size = (size_t)(s); \
|
|
|
|
size_t _pos; \
|
|
|
|
for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
|
2018-11-13 01:05:32 +00:00
|
|
|
PREFETCH_L2(_ptr + _pos); \
|
2018-09-12 23:15:37 +00:00
|
|
|
} \
|
2018-09-12 00:23:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 00:20:50 +00:00
|
|
|
/* disable warnings */
|
|
|
|
#ifdef _MSC_VER /* Visual Studio */
|
|
|
|
# include <intrin.h> /* For Visual 2005 */
|
|
|
|
# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */
|
|
|
|
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
|
|
|
|
# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */
|
|
|
|
# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */
|
|
|
|
# pragma warning(disable : 4324) /* disable: C4324: padded structure */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* ZSTD_COMPILER_H */
|