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