1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BERBERIS_RUNTIME_PRIMITIVES_PLATFORM_H_
18 #define BERBERIS_RUNTIME_PRIMITIVES_PLATFORM_H_
19 
20 namespace berberis::host_platform {
21 
22 #if defined(__i386__) && !defined(__x32__)
23 inline constexpr bool kIsX86_32 = true;
24 inline constexpr bool kIsX86_64 = false;
25 #endif
26 #if defined(__x86_64__) || defined(__x32__)
27 inline constexpr bool kIsX86_32 = false;
28 inline constexpr bool kIsX86_64 = true;
29 #endif
30 #if !defined(__i386__) && !defined(__x86_64__)
31 inline constexpr bool kIsX86_32 = false;
32 inline constexpr bool kIsX86_64 = false;
33 #endif
34 inline constexpr bool kIsX86 = (kIsX86_32 || kIsX86_64);
35 
36 #if defined(__i386__) || defined(__x86_64__)
37 extern const struct PlatformCapabilities {
38   bool kIsAuthenticAMD;
39   bool kHasAES;
40   bool kHasAVX;
41   bool kHasBMI;
42   bool kHasBMI2;
43   bool kHasCLMUL;
44   bool kHasF16C;
45   bool kHasFMA;
46   bool kHasFMA4;
47   bool kHasLZCNT;
48   bool kHasPDEP;
49   bool kHasPOPCNT;
50   bool kHasSHA;
51   bool kHasSSE3;
52   bool kHasSSSE3;
53   bool kHasSSE4a;
54   bool kHasSSE4_1;
55   bool kHasSSE4_2;
56   bool kHasCustomCapability;
57 } kPlatformCapabilities;
58 // These are "runtime constants": they can not be determined at compile
59 // time but each particular CPU has them set to true or false and that
60 // value can not ever change in the lifetime of a program.
61 inline const bool& kIsAuthenticAMD = kPlatformCapabilities.kIsAuthenticAMD;
62 inline const bool& kHasAES = kPlatformCapabilities.kHasAES;
63 inline const bool& kHasAVX = kPlatformCapabilities.kHasAVX;
64 inline const bool& kHasBMI = kPlatformCapabilities.kHasBMI;
65 inline const bool& kHasBMI2 = kPlatformCapabilities.kHasBMI2;
66 inline const bool& kHasCLMUL = kPlatformCapabilities.kHasCLMUL;
67 inline const bool& kHasF16C = kPlatformCapabilities.kHasF16C;
68 inline const bool& kHasFMA = kPlatformCapabilities.kHasFMA;
69 inline const bool& kHasFMA4 = kPlatformCapabilities.kHasFMA4;
70 inline const bool& kHasLZCNT = kPlatformCapabilities.kHasLZCNT;
71 inline const bool& kHasPDEP = kPlatformCapabilities.kHasPDEP;
72 inline const bool& kHasPOPCNT = kPlatformCapabilities.kHasPOPCNT;
73 inline const bool& kHasSHA = kPlatformCapabilities.kHasSHA;
74 inline const bool& kHasSSE3 = kPlatformCapabilities.kHasSSE3;
75 inline const bool& kHasSSSE3 = kPlatformCapabilities.kHasSSSE3;
76 inline const bool& kHasSSE4a = kPlatformCapabilities.kHasSSE4a;
77 inline const bool& kHasSSE4_1 = kPlatformCapabilities.kHasSSE4_1;
78 inline const bool& kHasSSE4_2 = kPlatformCapabilities.kHasSSE4_2;
79 inline const bool& kHasCustomCapability = kPlatformCapabilities.kHasCustomCapability;
80 #endif
81 
82 }  // namespace berberis::host_platform
83 
84 #endif  // BERBERIS_RUNTIME_PRIMITIVES_PLATFORM_H_
85