portability_macros.h 4.4 KB

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