/*1* Copyright (c) Meta Platforms, Inc. and affiliates.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#ifndef ZSTD_COMPILER_H11#define ZSTD_COMPILER_H1213#include "portability_macros.h"1415/*-*******************************************************16* Compiler specifics17*********************************************************/18/* force inlining */1920#if !defined(ZSTD_NO_INLINE)21#if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */22# define INLINE_KEYWORD inline23#else24# define INLINE_KEYWORD25#endif2627#if defined(__GNUC__) || defined(__ICCARM__)28# define FORCE_INLINE_ATTR __attribute__((always_inline))29#elif defined(_MSC_VER)30# define FORCE_INLINE_ATTR __forceinline31#else32# define FORCE_INLINE_ATTR33#endif3435#else3637#define INLINE_KEYWORD38#define FORCE_INLINE_ATTR3940#endif4142/**43On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).44This explicitly marks such functions as __cdecl so that the code will still compile45if a CC other than __cdecl has been made the default.46*/47#if defined(_MSC_VER)48# define WIN_CDECL __cdecl49#else50# define WIN_CDECL51#endif5253/**54* FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant55* parameters. They must be inlined for the compiler to eliminate the constant56* branches.57*/58#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR59/**60* HINT_INLINE is used to help the compiler generate better code. It is *not*61* used for "templates", so it can be tweaked based on the compilers62* performance.63*64* gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the65* always_inline attribute.66*67* clang up to 5.0.0 (trunk) benefit tremendously from the always_inline68* attribute.69*/70#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 571# define HINT_INLINE static INLINE_KEYWORD72#else73# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR74#endif7576/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */77#if defined(__GNUC__)78# define UNUSED_ATTR __attribute__((unused))79#else80# define UNUSED_ATTR81#endif8283/* force no inlining */84#ifdef _MSC_VER85# define FORCE_NOINLINE static __declspec(noinline)86#else87# if defined(__GNUC__) || defined(__ICCARM__)88# define FORCE_NOINLINE static __attribute__((__noinline__))89# else90# define FORCE_NOINLINE static91# endif92#endif939495/* target attribute */96#if defined(__GNUC__) || defined(__ICCARM__)97# define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))98#else99# define TARGET_ATTRIBUTE(target)100#endif101102/* Target attribute for BMI2 dynamic dispatch.103* Enable lzcnt, bmi, and bmi2.104* We test for bmi1 & bmi2. lzcnt is included in bmi1.105*/106#define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2")107108/* prefetch109* can be disabled, by declaring NO_PREFETCH build macro */110#if defined(NO_PREFETCH)111# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */112# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */113#else114# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */115# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */116# define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)117# define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)118# elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )119# define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)120# define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)121# elif defined(__aarch64__)122# define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))123# define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))124# else125# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */126# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */127# endif128#endif /* NO_PREFETCH */129130#define CACHELINE_SIZE 64131132#define PREFETCH_AREA(p, s) { \133const char* const _ptr = (const char*)(p); \134size_t const _size = (size_t)(s); \135size_t _pos; \136for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \137PREFETCH_L2(_ptr + _pos); \138} \139}140141/* vectorization142* older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax,143* and some compilers, like Intel ICC and MCST LCC, do not support it at all. */144#if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__)145# if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)146# define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))147# else148# define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")149# endif150#else151# define DONT_VECTORIZE152#endif153154/* Tell the compiler that a branch is likely or unlikely.155* Only use these macros if it causes the compiler to generate better code.156* If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc157* and clang, please do.158*/159#if defined(__GNUC__)160#define LIKELY(x) (__builtin_expect((x), 1))161#define UNLIKELY(x) (__builtin_expect((x), 0))162#else163#define LIKELY(x) (x)164#define UNLIKELY(x) (x)165#endif166167#if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))168# define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); }169#else170# define ZSTD_UNREACHABLE { assert(0); }171#endif172173/* disable warnings */174#ifdef _MSC_VER /* Visual Studio */175# include <intrin.h> /* For Visual 2005 */176# pragma warning(disable : 4100) /* disable: C4100: unreferenced formal parameter */177# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */178# pragma warning(disable : 4204) /* disable: C4204: non-constant aggregate initializer */179# pragma warning(disable : 4214) /* disable: C4214: non-int bitfields */180# pragma warning(disable : 4324) /* disable: C4324: padded structure */181#endif182183/*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/184#ifndef STATIC_BMI2185# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86))186# ifdef __AVX2__ //MSVC does not have a BMI2 specific flag, but every CPU that supports AVX2 also supports BMI2187# define STATIC_BMI2 1188# endif189# elif defined(__BMI2__) && defined(__x86_64__) && defined(__GNUC__)190# define STATIC_BMI2 1191# endif192#endif193194#ifndef STATIC_BMI2195#define STATIC_BMI2 0196#endif197198/* compile time determination of SIMD support */199#if !defined(ZSTD_NO_INTRINSICS)200# if defined(__SSE2__) || defined(_M_AMD64) || (defined (_M_IX86) && defined(_M_IX86_FP) && (_M_IX86_FP >= 2))201# define ZSTD_ARCH_X86_SSE2202# endif203# if defined(__ARM_NEON) || defined(_M_ARM64)204# define ZSTD_ARCH_ARM_NEON205# endif206#207# if defined(ZSTD_ARCH_X86_SSE2)208# include <emmintrin.h>209# elif defined(ZSTD_ARCH_ARM_NEON)210# include <arm_neon.h>211# endif212#endif213214/* C-language Attributes are added in C23. */215#if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)216# define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)217#else218# define ZSTD_HAS_C_ATTRIBUTE(x) 0219#endif220221/* Only use C++ attributes in C++. Some compilers report support for C++222* attributes when compiling with C.223*/224#if defined(__cplusplus) && defined(__has_cpp_attribute)225# define ZSTD_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)226#else227# define ZSTD_HAS_CPP_ATTRIBUTE(x) 0228#endif229230/* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.231* - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough232* - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough233* - Else: __attribute__((__fallthrough__))234*/235#ifndef ZSTD_FALLTHROUGH236# if ZSTD_HAS_C_ATTRIBUTE(fallthrough)237# define ZSTD_FALLTHROUGH [[fallthrough]]238# elif ZSTD_HAS_CPP_ATTRIBUTE(fallthrough)239# define ZSTD_FALLTHROUGH [[fallthrough]]240# elif __has_attribute(__fallthrough__)241/* Leading semicolon is to satisfy gcc-11 with -pedantic. Without the semicolon242* gcc complains about: a label can only be part of a statement and a declaration is not a statement.243*/244# define ZSTD_FALLTHROUGH ; __attribute__((__fallthrough__))245# else246# define ZSTD_FALLTHROUGH247# endif248#endif249250/*-**************************************************************251* Alignment check252*****************************************************************/253254/* this test was initially positioned in mem.h,255* but this file is removed (or replaced) for linux kernel256* so it's now hosted in compiler.h,257* which remains valid for both user & kernel spaces.258*/259260#ifndef ZSTD_ALIGNOF261# if defined(__GNUC__) || defined(_MSC_VER)262/* covers gcc, clang & MSVC */263/* note : this section must come first, before C11,264* due to a limitation in the kernel source generator */265# define ZSTD_ALIGNOF(T) __alignof(T)266267# elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)268/* Oracle Studio */269# define ZSTD_ALIGNOF(T) _Alignof(T)270271# elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)272/* C11 support */273# include <stdalign.h>274# define ZSTD_ALIGNOF(T) alignof(T)275276# else277/* No known support for alignof() - imperfect backup */278# define ZSTD_ALIGNOF(T) (sizeof(void*) < sizeof(T) ? sizeof(void*) : sizeof(T))279280# endif281#endif /* ZSTD_ALIGNOF */282283/*-**************************************************************284* Sanitizer285*****************************************************************/286287/* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an288* abundance of caution, disable our custom poisoning on mingw. */289#ifdef __MINGW32__290#ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE291#define ZSTD_ASAN_DONT_POISON_WORKSPACE 1292#endif293#ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE294#define ZSTD_MSAN_DONT_POISON_WORKSPACE 1295#endif296#endif297298#if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)299/* Not all platforms that support msan provide sanitizers/msan_interface.h.300* We therefore declare the functions we need ourselves, rather than trying to301* include the header file... */302#include <stddef.h> /* size_t */303#define ZSTD_DEPS_NEED_STDINT304#include "zstd_deps.h" /* intptr_t */305306/* Make memory region fully initialized (without changing its contents). */307void __msan_unpoison(const volatile void *a, size_t size);308309/* Make memory region fully uninitialized (without changing its contents).310This is a legacy interface that does not update origin information. Use311__msan_allocated_memory() instead. */312void __msan_poison(const volatile void *a, size_t size);313314/* Returns the offset of the first (at least partially) poisoned byte in the315memory range, or -1 if the whole range is good. */316intptr_t __msan_test_shadow(const volatile void *x, size_t size);317318/* Print shadow and origin for the memory range to stderr in a human-readable319format. */320void __msan_print_shadow(const volatile void *x, size_t size);321#endif322323#if ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE)324/* Not all platforms that support asan provide sanitizers/asan_interface.h.325* We therefore declare the functions we need ourselves, rather than trying to326* include the header file... */327#include <stddef.h> /* size_t */328329/**330* Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.331*332* This memory must be previously allocated by your program. Instrumented333* code is forbidden from accessing addresses in this region until it is334* unpoisoned. This function is not guaranteed to poison the entire region -335* it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan336* alignment restrictions.337*338* \note This function is not thread-safe because no two threads can poison or339* unpoison memory in the same memory region simultaneously.340*341* \param addr Start of memory region.342* \param size Size of memory region. */343void __asan_poison_memory_region(void const volatile *addr, size_t size);344345/**346* Marks a memory region (<c>[addr, addr+size)</c>) as addressable.347*348* This memory must be previously allocated by your program. Accessing349* addresses in this region is allowed until this region is poisoned again.350* This function could unpoison a super-region of <c>[addr, addr+size)</c> due351* to ASan alignment restrictions.352*353* \note This function is not thread-safe because no two threads can354* poison or unpoison memory in the same memory region simultaneously.355*356* \param addr Start of memory region.357* \param size Size of memory region. */358void __asan_unpoison_memory_region(void const volatile *addr, size_t size);359#endif360361#endif /* ZSTD_COMPILER_H */362363364