platform.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // SKIP_ABSL_INLINE_NAMESPACE_CHECK
  15. #ifndef ABSL_RANDOM_INTERNAL_PLATFORM_H_
  16. #define ABSL_RANDOM_INTERNAL_PLATFORM_H_
  17. // HERMETIC NOTE: The randen_hwaes target must not introduce duplicate
  18. // symbols from arbitrary system and other headers, since it may be built
  19. // with different flags from other targets, using different levels of
  20. // optimization, potentially introducing ODR violations.
  21. // -----------------------------------------------------------------------------
  22. // Platform Feature Checks
  23. // -----------------------------------------------------------------------------
  24. // Currently supported operating systems and associated preprocessor
  25. // symbols:
  26. //
  27. // Linux and Linux-derived __linux__
  28. // Android __ANDROID__ (implies __linux__)
  29. // Linux (non-Android) __linux__ && !__ANDROID__
  30. // Darwin (macOS and iOS) __APPLE__
  31. // Akaros (http://akaros.org) __ros__
  32. // Windows _WIN32
  33. // NaCL __native_client__
  34. // AsmJS __asmjs__
  35. // WebAssembly __wasm__
  36. // Fuchsia __Fuchsia__
  37. //
  38. // Note that since Android defines both __ANDROID__ and __linux__, one
  39. // may probe for either Linux or Android by simply testing for __linux__.
  40. //
  41. // NOTE: For __APPLE__ platforms, we use #include <TargetConditionals.h>
  42. // to distinguish os variants.
  43. //
  44. // http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
  45. #if defined(__APPLE__)
  46. #include <TargetConditionals.h>
  47. #endif
  48. // -----------------------------------------------------------------------------
  49. // Architecture Checks
  50. // -----------------------------------------------------------------------------
  51. // These preprocessor directives are trying to determine CPU architecture,
  52. // including necessary headers to support hardware AES.
  53. //
  54. // ABSL_ARCH_{X86/PPC/ARM} macros determine the platform.
  55. #if defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) || \
  56. defined(_M_X64)
  57. #define ABSL_ARCH_X86_64
  58. #elif defined(__i386) || defined(_M_IX86)
  59. #define ABSL_ARCH_X86_32
  60. #elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
  61. #define ABSL_ARCH_AARCH64
  62. #elif defined(__arm__) || defined(__ARMEL__) || defined(_M_ARM)
  63. #define ABSL_ARCH_ARM
  64. #elif defined(__powerpc64__) || defined(__PPC64__) || defined(__powerpc__) || \
  65. defined(__ppc__) || defined(__PPC__)
  66. #define ABSL_ARCH_PPC
  67. #else
  68. // Unsupported architecture.
  69. // * https://sourceforge.net/p/predef/wiki/Architectures/
  70. // * https://msdn.microsoft.com/en-us/library/b0084kay.aspx
  71. // * for gcc, clang: "echo | gcc -E -dM -"
  72. #endif
  73. // -----------------------------------------------------------------------------
  74. // Attribute Checks
  75. // -----------------------------------------------------------------------------
  76. // ABSL_RANDOM_INTERNAL_RESTRICT annotates whether pointers may be considered
  77. // to be unaliased.
  78. #if defined(__clang__) || defined(__GNUC__)
  79. #define ABSL_RANDOM_INTERNAL_RESTRICT __restrict__
  80. #elif defined(_MSC_VER)
  81. #define ABSL_RANDOM_INTERNAL_RESTRICT __restrict
  82. #else
  83. #define ABSL_RANDOM_INTERNAL_RESTRICT
  84. #endif
  85. // ABSL_HAVE_ACCELERATED_AES indicates whether the currently active compiler
  86. // flags (e.g. -maes) allow using hardware accelerated AES instructions, which
  87. // implies us assuming that the target platform supports them.
  88. #define ABSL_HAVE_ACCELERATED_AES 0
  89. #if defined(ABSL_ARCH_X86_64)
  90. #if defined(__AES__) || defined(__AVX__)
  91. #undef ABSL_HAVE_ACCELERATED_AES
  92. #define ABSL_HAVE_ACCELERATED_AES 1
  93. #endif
  94. #elif defined(ABSL_ARCH_PPC)
  95. // Rely on VSX and CRYPTO extensions for vcipher on PowerPC.
  96. #if (defined(__VEC__) || defined(__ALTIVEC__)) && defined(__VSX__) && \
  97. defined(__CRYPTO__)
  98. #undef ABSL_HAVE_ACCELERATED_AES
  99. #define ABSL_HAVE_ACCELERATED_AES 1
  100. #endif
  101. #elif defined(ABSL_ARCH_ARM) || defined(ABSL_ARCH_AARCH64)
  102. // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0053c/IHI0053C_acle_2_0.pdf
  103. // Rely on NEON+CRYPTO extensions for ARM.
  104. #if defined(__ARM_NEON) && defined(__ARM_FEATURE_CRYPTO)
  105. #undef ABSL_HAVE_ACCELERATED_AES
  106. #define ABSL_HAVE_ACCELERATED_AES 1
  107. #endif
  108. #endif
  109. // NaCl does not allow AES.
  110. #if defined(__native_client__)
  111. #undef ABSL_HAVE_ACCELERATED_AES
  112. #define ABSL_HAVE_ACCELERATED_AES 0
  113. #endif
  114. // ABSL_RANDOM_INTERNAL_AES_DISPATCH indicates whether the currently active
  115. // platform has, or should use run-time dispatch for selecting the
  116. // accelerated Randen implementation.
  117. #define ABSL_RANDOM_INTERNAL_AES_DISPATCH 0
  118. // iOS does not support dispatch, even on x86, since applications
  119. // should be bundled as fat binaries, with a different build tailored for
  120. // each specific supported platform/architecture.
  121. #if (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || \
  122. (defined(TARGET_OS_IPHONE_SIMULATOR) && TARGET_OS_IPHONE_SIMULATOR)
  123. #undef ABSL_RANDOM_INTERNAL_AES_DISPATCH
  124. #define ABSL_RANDOM_INTERNAL_AES_DISPATCH 0
  125. #elif defined(ABSL_ARCH_X86_64)
  126. // Dispatch is available on x86_64
  127. #undef ABSL_RANDOM_INTERNAL_AES_DISPATCH
  128. #define ABSL_RANDOM_INTERNAL_AES_DISPATCH 1
  129. #elif defined(__linux__) && defined(ABSL_ARCH_PPC)
  130. // Or when running linux PPC
  131. #undef ABSL_RANDOM_INTERNAL_AES_DISPATCH
  132. #define ABSL_RANDOM_INTERNAL_AES_DISPATCH 1
  133. #elif (defined(__linux__) || defined(__APPLE__)) && defined(ABSL_ARCH_AARCH64)
  134. // Or when running linux or macOS AArch64
  135. #undef ABSL_RANDOM_INTERNAL_AES_DISPATCH
  136. #define ABSL_RANDOM_INTERNAL_AES_DISPATCH 1
  137. #elif defined(__linux__) && defined(ABSL_ARCH_ARM) && (__ARM_ARCH >= 8)
  138. // Or when running linux ARM v8 or higher.
  139. // (This captures a lot of Android configurations.)
  140. #undef ABSL_RANDOM_INTERNAL_AES_DISPATCH
  141. #define ABSL_RANDOM_INTERNAL_AES_DISPATCH 1
  142. #endif
  143. // NaCl does not allow dispatch.
  144. #if defined(__native_client__)
  145. #undef ABSL_RANDOM_INTERNAL_AES_DISPATCH
  146. #define ABSL_RANDOM_INTERNAL_AES_DISPATCH 0
  147. #endif
  148. #endif // ABSL_RANDOM_INTERNAL_PLATFORM_H_