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