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