Path: blob/master/Utilities/cmzstd/lib/common/portability_macros.h
5020 views
/*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_PORTABILITY_MACROS_H11#define ZSTD_PORTABILITY_MACROS_H1213/**14* This header file contains macro definitions to support portability.15* This header is shared between C and ASM code, so it MUST only16* contain macro definitions. It MUST not contain any C code.17*18* This header ONLY defines macros to detect platforms/feature support.19*20*/212223/* compat. with non-clang compilers */24#ifndef __has_attribute25#define __has_attribute(x) 026#endif2728/* compat. with non-clang compilers */29#ifndef __has_builtin30# define __has_builtin(x) 031#endif3233/* compat. with non-clang compilers */34#ifndef __has_feature35# define __has_feature(x) 036#endif3738/* detects whether we are being compiled under msan */39#ifndef ZSTD_MEMORY_SANITIZER40# if __has_feature(memory_sanitizer)41# define ZSTD_MEMORY_SANITIZER 142# else43# define ZSTD_MEMORY_SANITIZER 044# endif45#endif4647/* detects whether we are being compiled under asan */48#ifndef ZSTD_ADDRESS_SANITIZER49# if __has_feature(address_sanitizer)50# define ZSTD_ADDRESS_SANITIZER 151# elif defined(__SANITIZE_ADDRESS__)52# define ZSTD_ADDRESS_SANITIZER 153# else54# define ZSTD_ADDRESS_SANITIZER 055# endif56#endif5758/* detects whether we are being compiled under dfsan */59#ifndef ZSTD_DATAFLOW_SANITIZER60# if __has_feature(dataflow_sanitizer)61# define ZSTD_DATAFLOW_SANITIZER 162# else63# define ZSTD_DATAFLOW_SANITIZER 064# endif65#endif6667/* Mark the internal assembly functions as hidden */68#ifdef __ELF__69# define ZSTD_HIDE_ASM_FUNCTION(func) .hidden func70#elif defined(__APPLE__)71# define ZSTD_HIDE_ASM_FUNCTION(func) .private_extern func72#else73# define ZSTD_HIDE_ASM_FUNCTION(func)74#endif7576/* Compile time determination of BMI2 support */77#ifndef STATIC_BMI278# if defined(__BMI2__)79# define STATIC_BMI2 180# elif defined(_MSC_VER) && defined(__AVX2__)81# define STATIC_BMI2 1 /* MSVC does not have a BMI2 specific flag, but every CPU that supports AVX2 also supports BMI2 */82# endif83#endif8485#ifndef STATIC_BMI286# define STATIC_BMI2 087#endif8889/* Enable runtime BMI2 dispatch based on the CPU.90* Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.91*/92#ifndef DYNAMIC_BMI293# if ((defined(__clang__) && __has_attribute(__target__)) \94|| (defined(__GNUC__) \95&& (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \96&& (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)) \97&& !defined(__BMI2__)98# define DYNAMIC_BMI2 199# else100# define DYNAMIC_BMI2 0101# endif102#endif103104/**105* Only enable assembly for GNU C compatible compilers,106* because other platforms may not support GAS assembly syntax.107*108* Only enable assembly for Linux / MacOS / Win32, other platforms may109* work, but they haven't been tested. This could likely be110* extended to BSD systems.111*112* Disable assembly when MSAN is enabled, because MSAN requires113* 100% of code to be instrumented to work.114*/115#if defined(__GNUC__)116# if defined(__linux__) || defined(__linux) || defined(__APPLE__) || defined(_WIN32)117# if ZSTD_MEMORY_SANITIZER118# define ZSTD_ASM_SUPPORTED 0119# elif ZSTD_DATAFLOW_SANITIZER120# define ZSTD_ASM_SUPPORTED 0121# else122# define ZSTD_ASM_SUPPORTED 1123# endif124# else125# define ZSTD_ASM_SUPPORTED 0126# endif127#else128# define ZSTD_ASM_SUPPORTED 0129#endif130131/**132* Determines whether we should enable assembly for x86-64133* with BMI2.134*135* Enable if all of the following conditions hold:136* - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM137* - Assembly is supported138* - We are compiling for x86-64 and either:139* - DYNAMIC_BMI2 is enabled140* - BMI2 is supported at compile time141*/142#if !defined(ZSTD_DISABLE_ASM) && \143ZSTD_ASM_SUPPORTED && \144defined(__x86_64__) && \145(DYNAMIC_BMI2 || defined(__BMI2__))146# define ZSTD_ENABLE_ASM_X86_64_BMI2 1147#else148# define ZSTD_ENABLE_ASM_X86_64_BMI2 0149#endif150151/*152* For x86 ELF targets, add .note.gnu.property section for Intel CET in153* assembly sources when CET is enabled.154*155* Additionally, any function that may be called indirectly must begin156* with ZSTD_CET_ENDBRANCH.157*/158#if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) \159&& defined(__has_include)160# if __has_include(<cet.h>)161# include <cet.h>162# define ZSTD_CET_ENDBRANCH _CET_ENDBR163# endif164#endif165166#ifndef ZSTD_CET_ENDBRANCH167# define ZSTD_CET_ENDBRANCH168#endif169170#endif /* ZSTD_PORTABILITY_MACROS_H */171172173