1 /*
2  * Copyright (C) 2014 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 #include "berberis/runtime_primitives/platform.h"
18 #include "berberis/base/config_globals.h"
19 
20 #if defined(__i386__) || defined(__x86_64__)
21 #include <cpuid.h>
22 #endif
23 
24 #include <cinttypes>
25 
26 namespace berberis::host_platform {
27 
28 namespace {
29 
30 #if defined(__i386__) || defined(__x86_64__)
Init()31 auto Init() {
32   PlatformCapabilities platform_capabilities = {};
33   unsigned int eax, ebx, ecx, edx;
34   // Technically Zen,Zen+/Zen2 AMD CPUs support BMI2 and thus PDEP/PEXT instruction, but they are
35   // not usable there: https://twitter.com/instlatx64/status/1322503571288559617
36   // That's why we need special emulated CPUID flag for these instructions.
37   bool use_pdep_if_present = true;
38   __cpuid(0, eax, ebx, ecx, edx);
39   if (((ebx == signature_AMD_ebx && ecx == signature_AMD_ecx && edx == signature_AMD_edx) ||
40        (ebx == signature_HYGON_ebx && ecx == signature_HYGON_ecx && edx == signature_HYGON_edx))) {
41     platform_capabilities.kIsAuthenticAMD = true;
42     __cpuid(1, eax, ebx, ecx, edx);
43     uint8_t family = (eax >> 8) & 0b1111;
44     if (family == 0b1111) {
45       family += (eax >> 20) & 0b11111111;
46       if (family < 0x19) {
47         use_pdep_if_present = false;
48       }
49     }
50   } else {
51     __cpuid(1, eax, ebx, ecx, edx);
52   }
53   platform_capabilities.kHasAES = ecx & bit_AES;
54   platform_capabilities.kHasAVX = ecx & bit_AVX;
55   platform_capabilities.kHasCLMUL = ecx & bit_PCLMUL;
56   platform_capabilities.kHasF16C = ecx & bit_F16C;
57   platform_capabilities.kHasFMA = ecx & bit_FMA;
58   platform_capabilities.kHasPOPCNT = ecx & bit_POPCNT;
59   platform_capabilities.kHasSSE3 = ecx & bit_SSE3;
60   platform_capabilities.kHasSSSE3 = ecx & bit_SSSE3;
61   platform_capabilities.kHasSSE4_1 = ecx & bit_SSE4_1;
62   platform_capabilities.kHasSSE4_2 = ecx & bit_SSE4_2;
63   __cpuid(0x80000001, eax, ebx, ecx, edx);
64   platform_capabilities.kHasFMA4 = ecx & bit_FMA4;
65   platform_capabilities.kHasLZCNT = ecx & bit_LZCNT;
66   platform_capabilities.kHasSSE4a = ecx & bit_SSE4a;
67   __cpuid_count(7, 0, eax, ebx, ecx, edx);
68   platform_capabilities.kHasBMI = ebx & bit_BMI;
69   platform_capabilities.kHasBMI2 = ebx & bit_BMI2;
70   platform_capabilities.kHasPDEP = ebx & bit_BMI2 && use_pdep_if_present;
71   platform_capabilities.kHasSHA = ebx & bit_SHA;
72   platform_capabilities.kHasCustomCapability = IsConfigFlagSet(kPlatformCustomCPUCapability);
73   return platform_capabilities;
74 }
75 #endif
76 
77 }  // namespace
78 
79 #if defined(__i386__) || defined(__x86_64__)
80 const PlatformCapabilities kPlatformCapabilities = Init();
81 #endif
82 
83 }  // namespace berberis::host_platform
84