Path: blob/master/Utilities/cmzstd/lib/common/portability_macros.h
3158 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#else71# define ZSTD_HIDE_ASM_FUNCTION(func)72#endif7374/* Enable runtime BMI2 dispatch based on the CPU.75* Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.76*/77#ifndef DYNAMIC_BMI278#if ((defined(__clang__) && __has_attribute(__target__)) \79|| (defined(__GNUC__) \80&& (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \81&& (defined(__x86_64__) || defined(_M_X64)) \82&& !defined(__BMI2__)83# define DYNAMIC_BMI2 184#else85# define DYNAMIC_BMI2 086#endif87#endif8889/**90* Only enable assembly for GNUC compatible compilers,91* because other platforms may not support GAS assembly syntax.92*93* Only enable assembly for Linux / MacOS, other platforms may94* work, but they haven't been tested. This could likely be95* extended to BSD systems.96*97* Disable assembly when MSAN is enabled, because MSAN requires98* 100% of code to be instrumented to work.99*/100#if defined(__GNUC__)101# if defined(__linux__) || defined(__linux) || defined(__APPLE__)102# if ZSTD_MEMORY_SANITIZER103# define ZSTD_ASM_SUPPORTED 0104# elif ZSTD_DATAFLOW_SANITIZER105# define ZSTD_ASM_SUPPORTED 0106# else107# define ZSTD_ASM_SUPPORTED 1108# endif109# else110# define ZSTD_ASM_SUPPORTED 0111# endif112#else113# define ZSTD_ASM_SUPPORTED 0114#endif115116/**117* Determines whether we should enable assembly for x86-64118* with BMI2.119*120* Enable if all of the following conditions hold:121* - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM122* - Assembly is supported123* - We are compiling for x86-64 and either:124* - DYNAMIC_BMI2 is enabled125* - BMI2 is supported at compile time126*/127#if !defined(ZSTD_DISABLE_ASM) && \128ZSTD_ASM_SUPPORTED && \129defined(__x86_64__) && \130(DYNAMIC_BMI2 || defined(__BMI2__))131# define ZSTD_ENABLE_ASM_X86_64_BMI2 1132#else133# define ZSTD_ENABLE_ASM_X86_64_BMI2 0134#endif135136/*137* For x86 ELF targets, add .note.gnu.property section for Intel CET in138* assembly sources when CET is enabled.139*140* Additionally, any function that may be called indirectly must begin141* with ZSTD_CET_ENDBRANCH.142*/143#if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) \144&& defined(__has_include)145# if __has_include(<cet.h>)146# include <cet.h>147# define ZSTD_CET_ENDBRANCH _CET_ENDBR148# endif149#endif150151#ifndef ZSTD_CET_ENDBRANCH152# define ZSTD_CET_ENDBRANCH153#endif154155#endif /* ZSTD_PORTABILITY_MACROS_H */156157158