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