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