portability_macros.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) Facebook, Inc.
  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 defintions 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. /* detects whether we are being compiled under asan */
  34. /* detects whether we are being compiled under dfsan */
  35. /* Mark the internal assembly functions as hidden */
  36. #ifdef __ELF__
  37. # define ZSTD_HIDE_ASM_FUNCTION(func) .hidden func
  38. #else
  39. # define ZSTD_HIDE_ASM_FUNCTION(func)
  40. #endif
  41. /* Enable runtime BMI2 dispatch based on the CPU.
  42. * Enabled for clang & gcc >=4.8 on x86 when BMI2 isn't enabled by default.
  43. */
  44. #ifndef DYNAMIC_BMI2
  45. #if ((defined(__clang__) && __has_attribute(__target__)) \
  46. || (defined(__GNUC__) \
  47. && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))) \
  48. && (defined(__x86_64__) || defined(_M_X64)) \
  49. && !defined(__BMI2__)
  50. # define DYNAMIC_BMI2 1
  51. #else
  52. # define DYNAMIC_BMI2 0
  53. #endif
  54. #endif
  55. /*
  56. * Only enable assembly for GNUC comptabile compilers,
  57. * because other platforms may not support GAS assembly syntax.
  58. *
  59. * Only enable assembly for Linux / MacOS, other platforms may
  60. * work, but they haven't been tested. This could likely be
  61. * extended to BSD systems.
  62. *
  63. * Disable assembly when MSAN is enabled, because MSAN requires
  64. * 100% of code to be instrumented to work.
  65. */
  66. #define ZSTD_ASM_SUPPORTED 1
  67. /*
  68. * Determines whether we should enable assembly for x86-64
  69. * with BMI2.
  70. *
  71. * Enable if all of the following conditions hold:
  72. * - ASM hasn't been explicitly disabled by defining ZSTD_DISABLE_ASM
  73. * - Assembly is supported
  74. * - We are compiling for x86-64 and either:
  75. * - DYNAMIC_BMI2 is enabled
  76. * - BMI2 is supported at compile time
  77. */
  78. #define ZSTD_ENABLE_ASM_X86_64_BMI2 0
  79. #endif /* ZSTD_PORTABILITY_MACROS_H */