1*5ff13fbcSAllan Jude /* 2*5ff13fbcSAllan Jude * Copyright (c) Facebook, Inc. 3*5ff13fbcSAllan Jude * All rights reserved. 4*5ff13fbcSAllan Jude * 5*5ff13fbcSAllan Jude * This source code is licensed under both the BSD-style license (found in the 6*5ff13fbcSAllan Jude * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7*5ff13fbcSAllan Jude * in the COPYING file in the root directory of this source tree). 8*5ff13fbcSAllan Jude * You may select, at your option, one of the above-listed licenses. 9*5ff13fbcSAllan Jude */ 10*5ff13fbcSAllan Jude 11*5ff13fbcSAllan Jude #ifndef ZSTD_PORTABILITY_MACROS_H 12*5ff13fbcSAllan Jude #define ZSTD_PORTABILITY_MACROS_H 13*5ff13fbcSAllan Jude 14*5ff13fbcSAllan Jude /** 15*5ff13fbcSAllan Jude * This header file contains macro defintions to support portability. 16*5ff13fbcSAllan Jude * This header is shared between C and ASM code, so it MUST only 17*5ff13fbcSAllan Jude * contain macro definitions. It MUST not contain any C code. 18*5ff13fbcSAllan Jude * 19*5ff13fbcSAllan Jude * This header ONLY defines macros to detect platforms/feature support. 20*5ff13fbcSAllan Jude * 21*5ff13fbcSAllan Jude */ 22*5ff13fbcSAllan Jude 23*5ff13fbcSAllan Jude 24*5ff13fbcSAllan Jude /* compat. with non-clang compilers */ 25*5ff13fbcSAllan Jude #ifndef __has_attribute 26*5ff13fbcSAllan Jude #define __has_attribute(x) 0 27*5ff13fbcSAllan Jude #endif 28*5ff13fbcSAllan Jude 29*5ff13fbcSAllan Jude /* compat. with non-clang compilers */ 30*5ff13fbcSAllan Jude #ifndef __has_builtin 31*5ff13fbcSAllan Jude # define __has_builtin(x) 0 32*5ff13fbcSAllan Jude #endif 33*5ff13fbcSAllan Jude 34*5ff13fbcSAllan Jude /* compat. with non-clang compilers */ 35*5ff13fbcSAllan Jude #ifndef __has_feature 36*5ff13fbcSAllan Jude # define __has_feature(x) 0 37*5ff13fbcSAllan Jude #endif 38*5ff13fbcSAllan Jude 39*5ff13fbcSAllan Jude /* detects whether we are being compiled under msan */ 40*5ff13fbcSAllan Jude #ifndef ZSTD_MEMORY_SANITIZER 41*5ff13fbcSAllan Jude # if __has_feature(memory_sanitizer) 42*5ff13fbcSAllan Jude # define ZSTD_MEMORY_SANITIZER 1 43*5ff13fbcSAllan Jude # else 44*5ff13fbcSAllan Jude # define ZSTD_MEMORY_SANITIZER 0 45*5ff13fbcSAllan Jude # endif 46*5ff13fbcSAllan Jude #endif 47*5ff13fbcSAllan Jude 48*5ff13fbcSAllan Jude /* detects whether we are being compiled under asan */ 49*5ff13fbcSAllan Jude #ifndef ZSTD_ADDRESS_SANITIZER 50*5ff13fbcSAllan Jude # if __has_feature(address_sanitizer) 51*5ff13fbcSAllan Jude # define ZSTD_ADDRESS_SANITIZER 1 52*5ff13fbcSAllan Jude # elif defined(__SANITIZE_ADDRESS__) 53*5ff13fbcSAllan Jude # define ZSTD_ADDRESS_SANITIZER 1 54*5ff13fbcSAllan Jude # else 55*5ff13fbcSAllan Jude # define ZSTD_ADDRESS_SANITIZER 0 56*5ff13fbcSAllan Jude # endif 57*5ff13fbcSAllan Jude #endif 58*5ff13fbcSAllan Jude 59*5ff13fbcSAllan Jude /* detects whether we are being compiled under dfsan */ 60*5ff13fbcSAllan Jude #ifndef ZSTD_DATAFLOW_SANITIZER 61*5ff13fbcSAllan Jude # if __has_feature(dataflow_sanitizer) 62*5ff13fbcSAllan Jude # define ZSTD_DATAFLOW_SANITIZER 1 63*5ff13fbcSAllan Jude # else 64*5ff13fbcSAllan Jude # define ZSTD_DATAFLOW_SANITIZER 0 65*5ff13fbcSAllan Jude # endif 66*5ff13fbcSAllan Jude #endif 67*5ff13fbcSAllan Jude 68*5ff13fbcSAllan Jude /* Mark the internal assembly functions as hidden */ 69*5ff13fbcSAllan Jude #ifdef __ELF__ 70*5ff13fbcSAllan Jude # define ZSTD_HIDE_ASM_FUNCTION(func) .hidden func 71*5ff13fbcSAllan Jude #else 72*5ff13fbcSAllan Jude # define ZSTD_HIDE_ASM_FUNCTION(func) 73*5ff13fbcSAllan Jude #endif 74*5ff13fbcSAllan Jude 75*5ff13fbcSAllan Jude /* Enable runtime BMI2 dispatch based on the CPU. 76*5ff13fbcSAllan Jude * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default. 77*5ff13fbcSAllan Jude */ 78*5ff13fbcSAllan Jude #ifndef DYNAMIC_BMI2 79*5ff13fbcSAllan Jude #if ((defined(__clang__) && __has_attribute(__target__)) \ 80*5ff13fbcSAllan Jude || (defined(__GNUC__) \ 81*5ff13fbcSAllan Jude && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \ 82*5ff13fbcSAllan Jude && (defined(__x86_64__) || defined(_M_X64)) \ 83*5ff13fbcSAllan Jude && !defined(__BMI2__) 84*5ff13fbcSAllan Jude # define DYNAMIC_BMI2 1 85*5ff13fbcSAllan Jude #else 86*5ff13fbcSAllan Jude # define DYNAMIC_BMI2 0 87*5ff13fbcSAllan Jude #endif 88*5ff13fbcSAllan Jude #endif 89*5ff13fbcSAllan Jude 90*5ff13fbcSAllan Jude /** 91*5ff13fbcSAllan Jude * Only enable assembly for GNUC comptabile compilers, 92*5ff13fbcSAllan Jude * because other platforms may not support GAS assembly syntax. 93*5ff13fbcSAllan Jude * 94*5ff13fbcSAllan Jude * Only enable assembly for Linux / MacOS, other platforms may 95*5ff13fbcSAllan Jude * work, but they haven't been tested. This could likely be 96*5ff13fbcSAllan Jude * extended to BSD systems. 97*5ff13fbcSAllan Jude * 98*5ff13fbcSAllan Jude * Disable assembly when MSAN is enabled, because MSAN requires 99*5ff13fbcSAllan Jude * 100% of code to be instrumented to work. 100*5ff13fbcSAllan Jude */ 101*5ff13fbcSAllan Jude #if defined(__GNUC__) 102*5ff13fbcSAllan Jude # if defined(__linux__) || defined(__linux) || defined(__APPLE__) 103*5ff13fbcSAllan Jude # if ZSTD_MEMORY_SANITIZER 104*5ff13fbcSAllan Jude # define ZSTD_ASM_SUPPORTED 0 105*5ff13fbcSAllan Jude # elif ZSTD_DATAFLOW_SANITIZER 106*5ff13fbcSAllan Jude # define ZSTD_ASM_SUPPORTED 0 107*5ff13fbcSAllan Jude # else 108*5ff13fbcSAllan Jude # define ZSTD_ASM_SUPPORTED 1 109*5ff13fbcSAllan Jude # endif 110*5ff13fbcSAllan Jude # else 111*5ff13fbcSAllan Jude # define ZSTD_ASM_SUPPORTED 0 112*5ff13fbcSAllan Jude # endif 113*5ff13fbcSAllan Jude #else 114*5ff13fbcSAllan Jude # define ZSTD_ASM_SUPPORTED 0 115*5ff13fbcSAllan Jude #endif 116*5ff13fbcSAllan Jude 117*5ff13fbcSAllan Jude /** 118*5ff13fbcSAllan Jude * Determines whether we should enable assembly for x86-64 119*5ff13fbcSAllan Jude * with BMI2. 120*5ff13fbcSAllan Jude * 121*5ff13fbcSAllan Jude * Enable if all of the following conditions hold: 122*5ff13fbcSAllan Jude * - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM 123*5ff13fbcSAllan Jude * - Assembly is supported 124*5ff13fbcSAllan Jude * - We are compiling for x86-64 and either: 125*5ff13fbcSAllan Jude * - DYNAMIC_BMI2 is enabled 126*5ff13fbcSAllan Jude * - BMI2 is supported at compile time 127*5ff13fbcSAllan Jude */ 128*5ff13fbcSAllan Jude #if !defined(ZSTD_DISABLE_ASM) && \ 129*5ff13fbcSAllan Jude ZSTD_ASM_SUPPORTED && \ 130*5ff13fbcSAllan Jude defined(__x86_64__) && \ 131*5ff13fbcSAllan Jude (DYNAMIC_BMI2 || defined(__BMI2__)) 132*5ff13fbcSAllan Jude # define ZSTD_ENABLE_ASM_X86_64_BMI2 1 133*5ff13fbcSAllan Jude #else 134*5ff13fbcSAllan Jude # define ZSTD_ENABLE_ASM_X86_64_BMI2 0 135*5ff13fbcSAllan Jude #endif 136*5ff13fbcSAllan Jude 137*5ff13fbcSAllan Jude #endif /* ZSTD_PORTABILITY_MACROS_H */ 138