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