You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

portability_macros.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #else
  62. # define ZSTD_HIDE_ASM_FUNCTION(func)
  63. #endif
  64. /* Enable runtime BMI2 dispatch based on the CPU.
  65. * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
  66. */
  67. #ifndef DYNAMIC_BMI2
  68. #if ((defined(__clang__) && __has_attribute(__target__)) \
  69. || (defined(__GNUC__) \
  70. && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
  71. && (defined(__x86_64__) || defined(_M_X64)) \
  72. && !defined(__BMI2__)
  73. # define DYNAMIC_BMI2 1
  74. #else
  75. # define DYNAMIC_BMI2 0
  76. #endif
  77. #endif
  78. /**
  79. * Only enable assembly for GNUC compatible compilers,
  80. * because other platforms may not support GAS assembly syntax.
  81. *
  82. * Only enable assembly for Linux / MacOS, other platforms may
  83. * work, but they haven't been tested. This could likely be
  84. * extended to BSD systems.
  85. *
  86. * Disable assembly when MSAN is enabled, because MSAN requires
  87. * 100% of code to be instrumented to work.
  88. */
  89. #if defined(__GNUC__)
  90. # if defined(__linux__) || defined(__linux) || defined(__APPLE__)
  91. # if ZSTD_MEMORY_SANITIZER
  92. # define ZSTD_ASM_SUPPORTED 0
  93. # elif ZSTD_DATAFLOW_SANITIZER
  94. # define ZSTD_ASM_SUPPORTED 0
  95. # else
  96. # define ZSTD_ASM_SUPPORTED 1
  97. # endif
  98. # else
  99. # define ZSTD_ASM_SUPPORTED 0
  100. # endif
  101. #else
  102. # define ZSTD_ASM_SUPPORTED 0
  103. #endif
  104. /**
  105. * Determines whether we should enable assembly for x86-64
  106. * with BMI2.
  107. *
  108. * Enable if all of the following conditions hold:
  109. * - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM
  110. * - Assembly is supported
  111. * - We are compiling for x86-64 and either:
  112. * - DYNAMIC_BMI2 is enabled
  113. * - BMI2 is supported at compile time
  114. */
  115. #if !defined(ZSTD_DISABLE_ASM) && \
  116. ZSTD_ASM_SUPPORTED && \
  117. defined(__x86_64__) && \
  118. (DYNAMIC_BMI2 || defined(__BMI2__))
  119. # define ZSTD_ENABLE_ASM_X86_64_BMI2 1
  120. #else
  121. # define ZSTD_ENABLE_ASM_X86_64_BMI2 0
  122. #endif
  123. /*
  124. * For x86 ELF targets, add .note.gnu.property section for Intel CET in
  125. * assembly sources when CET is enabled.
  126. *
  127. * Additionally, any function that may be called indirectly must begin
  128. * with ZSTD_CET_ENDBRANCH.
  129. */
  130. #if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) \
  131. && defined(__has_include)
  132. # if __has_include(<cet.h>)
  133. # include <cet.h>
  134. # define ZSTD_CET_ENDBRANCH _CET_ENDBR
  135. # endif
  136. #endif
  137. #ifndef ZSTD_CET_ENDBRANCH
  138. # define ZSTD_CET_ENDBRANCH
  139. #endif
  140. #endif /* ZSTD_PORTABILITY_MACROS_H */