1*09537850SAkhilesh Sanikop // Copyright 2019 The libgav1 Authors
2*09537850SAkhilesh Sanikop //
3*09537850SAkhilesh Sanikop // Licensed under the Apache License, Version 2.0 (the "License");
4*09537850SAkhilesh Sanikop // you may not use this file except in compliance with the License.
5*09537850SAkhilesh Sanikop // You may obtain a copy of the License at
6*09537850SAkhilesh Sanikop //
7*09537850SAkhilesh Sanikop // http://www.apache.org/licenses/LICENSE-2.0
8*09537850SAkhilesh Sanikop //
9*09537850SAkhilesh Sanikop // Unless required by applicable law or agreed to in writing, software
10*09537850SAkhilesh Sanikop // distributed under the License is distributed on an "AS IS" BASIS,
11*09537850SAkhilesh Sanikop // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*09537850SAkhilesh Sanikop // See the License for the specific language governing permissions and
13*09537850SAkhilesh Sanikop // limitations under the License.
14*09537850SAkhilesh Sanikop
15*09537850SAkhilesh Sanikop #include "src/utils/cpu.h"
16*09537850SAkhilesh Sanikop
17*09537850SAkhilesh Sanikop #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
18*09537850SAkhilesh Sanikop #include <cpuid.h>
19*09537850SAkhilesh Sanikop #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
20*09537850SAkhilesh Sanikop #include <immintrin.h> // _xgetbv
21*09537850SAkhilesh Sanikop #include <intrin.h>
22*09537850SAkhilesh Sanikop #endif
23*09537850SAkhilesh Sanikop
24*09537850SAkhilesh Sanikop namespace libgav1 {
25*09537850SAkhilesh Sanikop
26*09537850SAkhilesh Sanikop #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
27*09537850SAkhilesh Sanikop defined(_M_X64)
28*09537850SAkhilesh Sanikop namespace {
29*09537850SAkhilesh Sanikop
30*09537850SAkhilesh Sanikop #if defined(__GNUC__)
CpuId(int leaf,uint32_t info[4])31*09537850SAkhilesh Sanikop void CpuId(int leaf, uint32_t info[4]) {
32*09537850SAkhilesh Sanikop __cpuid_count(leaf, 0 /*ecx=subleaf*/, info[0], info[1], info[2], info[3]);
33*09537850SAkhilesh Sanikop }
34*09537850SAkhilesh Sanikop
Xgetbv()35*09537850SAkhilesh Sanikop uint64_t Xgetbv() {
36*09537850SAkhilesh Sanikop const uint32_t ecx = 0; // ecx specifies the extended control register
37*09537850SAkhilesh Sanikop uint32_t eax;
38*09537850SAkhilesh Sanikop uint32_t edx;
39*09537850SAkhilesh Sanikop __asm__ volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(ecx));
40*09537850SAkhilesh Sanikop return (static_cast<uint64_t>(edx) << 32) | eax;
41*09537850SAkhilesh Sanikop }
42*09537850SAkhilesh Sanikop #else // _MSC_VER
43*09537850SAkhilesh Sanikop void CpuId(int leaf, uint32_t info[4]) {
44*09537850SAkhilesh Sanikop __cpuidex(reinterpret_cast<int*>(info), leaf, 0 /*ecx=subleaf*/);
45*09537850SAkhilesh Sanikop }
46*09537850SAkhilesh Sanikop
47*09537850SAkhilesh Sanikop uint64_t Xgetbv() { return _xgetbv(0); }
48*09537850SAkhilesh Sanikop #endif // __GNUC__
49*09537850SAkhilesh Sanikop
50*09537850SAkhilesh Sanikop } // namespace
51*09537850SAkhilesh Sanikop
GetCpuInfo()52*09537850SAkhilesh Sanikop uint32_t GetCpuInfo() {
53*09537850SAkhilesh Sanikop uint32_t info[4];
54*09537850SAkhilesh Sanikop
55*09537850SAkhilesh Sanikop // Get the highest feature value cpuid supports
56*09537850SAkhilesh Sanikop CpuId(0, info);
57*09537850SAkhilesh Sanikop const int max_cpuid_value = info[0];
58*09537850SAkhilesh Sanikop if (max_cpuid_value < 1) return 0;
59*09537850SAkhilesh Sanikop
60*09537850SAkhilesh Sanikop CpuId(1, info);
61*09537850SAkhilesh Sanikop uint32_t features = 0;
62*09537850SAkhilesh Sanikop if ((info[3] & (1 << 26)) != 0) features |= kSSE2;
63*09537850SAkhilesh Sanikop if ((info[2] & (1 << 9)) != 0) features |= kSSSE3;
64*09537850SAkhilesh Sanikop if ((info[2] & (1 << 19)) != 0) features |= kSSE4_1;
65*09537850SAkhilesh Sanikop
66*09537850SAkhilesh Sanikop // Bits 27 (OSXSAVE) & 28 (256-bit AVX)
67*09537850SAkhilesh Sanikop if ((info[2] & (3 << 27)) == (3 << 27)) {
68*09537850SAkhilesh Sanikop // XMM state and YMM state enabled by the OS
69*09537850SAkhilesh Sanikop if ((Xgetbv() & 0x6) == 0x6) {
70*09537850SAkhilesh Sanikop features |= kAVX;
71*09537850SAkhilesh Sanikop if (max_cpuid_value >= 7) {
72*09537850SAkhilesh Sanikop CpuId(7, info);
73*09537850SAkhilesh Sanikop if ((info[1] & (1 << 5)) != 0) features |= kAVX2;
74*09537850SAkhilesh Sanikop }
75*09537850SAkhilesh Sanikop }
76*09537850SAkhilesh Sanikop }
77*09537850SAkhilesh Sanikop
78*09537850SAkhilesh Sanikop return features;
79*09537850SAkhilesh Sanikop }
80*09537850SAkhilesh Sanikop #else
81*09537850SAkhilesh Sanikop uint32_t GetCpuInfo() { return 0; }
82*09537850SAkhilesh Sanikop #endif // x86 || x86_64
83*09537850SAkhilesh Sanikop
84*09537850SAkhilesh Sanikop } // namespace libgav1
85