2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 #ifndef ZSTD_PORTABILITY_MACROS_H
12 #define ZSTD_PORTABILITY_MACROS_H
15 * This header file contains macro definitions to support portability.
16 * This header is shared between C and ASM code, so it MUST only
17 * contain macro definitions. It MUST not contain any C code.
19 * This header ONLY defines macros to detect platforms/feature support.
24 /* compat. with non-clang compilers */
25 #ifndef __has_attribute
26 #define __has_attribute(x) 0
29 /* compat. with non-clang compilers */
31 # define __has_builtin(x) 0
34 /* compat. with non-clang compilers */
36 # define __has_feature(x) 0
39 /* detects whether we are being compiled under msan */
40 #ifndef ZSTD_MEMORY_SANITIZER
41 # if __has_feature(memory_sanitizer)
42 # define ZSTD_MEMORY_SANITIZER 1
44 # define ZSTD_MEMORY_SANITIZER 0
48 /* detects whether we are being compiled under asan */
49 #ifndef ZSTD_ADDRESS_SANITIZER
50 # if __has_feature(address_sanitizer)
51 # define ZSTD_ADDRESS_SANITIZER 1
52 # elif defined(__SANITIZE_ADDRESS__)
53 # define ZSTD_ADDRESS_SANITIZER 1
55 # define ZSTD_ADDRESS_SANITIZER 0
59 /* detects whether we are being compiled under dfsan */
60 #ifndef ZSTD_DATAFLOW_SANITIZER
61 # if __has_feature(dataflow_sanitizer)
62 # define ZSTD_DATAFLOW_SANITIZER 1
64 # define ZSTD_DATAFLOW_SANITIZER 0
68 /* Mark the internal assembly functions as hidden */
70 # define ZSTD_HIDE_ASM_FUNCTION(func) .hidden func
72 # define ZSTD_HIDE_ASM_FUNCTION(func)
75 /* Enable runtime BMI2 dispatch based on the CPU.
76 * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
79 #if ((defined(__clang__) && __has_attribute(__target__)) \
80 || (defined(__GNUC__) \
81 && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
82 && (defined(__x86_64__) || defined(_M_X64)) \
84 # define DYNAMIC_BMI2 1
86 # define DYNAMIC_BMI2 0
91 * Only enable assembly for GNUC compatible compilers,
92 * because other platforms may not support GAS assembly syntax.
94 * Only enable assembly for Linux / MacOS, other platforms may
95 * work, but they haven't been tested. This could likely be
96 * extended to BSD systems.
98 * Disable assembly when MSAN is enabled, because MSAN requires
99 * 100% of code to be instrumented to work.
101 #if defined(__GNUC__)
102 # if defined(__linux__) || defined(__linux) || defined(__APPLE__)
103 # if ZSTD_MEMORY_SANITIZER
104 # define ZSTD_ASM_SUPPORTED 0
105 # elif ZSTD_DATAFLOW_SANITIZER
106 # define ZSTD_ASM_SUPPORTED 0
108 # define ZSTD_ASM_SUPPORTED 1
111 # define ZSTD_ASM_SUPPORTED 0
114 # define ZSTD_ASM_SUPPORTED 0
118 * Determines whether we should enable assembly for x86-64
121 * Enable if all of the following conditions hold:
122 * - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM
123 * - Assembly is supported
124 * - We are compiling for x86-64 and either:
125 * - DYNAMIC_BMI2 is enabled
126 * - BMI2 is supported at compile time
128 #if !defined(ZSTD_DISABLE_ASM) && \
129 ZSTD_ASM_SUPPORTED && \
130 defined(__x86_64__) && \
131 (DYNAMIC_BMI2 || defined(__BMI2__))
132 # define ZSTD_ENABLE_ASM_X86_64_BMI2 1
134 # define ZSTD_ENABLE_ASM_X86_64_BMI2 0
138 * For x86 ELF targets, add .note.gnu.property section for Intel CET in
139 * assembly sources when CET is enabled.
141 * Additionally, any function that may be called indirectly must begin
142 * with ZSTD_CET_ENDBRANCH.
144 #if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) \
145 && defined(__has_include)
146 # if __has_include(<cet.h>)
148 # define ZSTD_CET_ENDBRANCH _CET_ENDBR
152 #ifndef ZSTD_CET_ENDBRANCH
153 # define ZSTD_CET_ENDBRANCH
156 #endif /* ZSTD_PORTABILITY_MACROS_H */