1 /*
2  * Copyright 2019 The libgav1 Authors
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 LIBGAV1_SRC_UTILS_CPU_H_
18 #define LIBGAV1_SRC_UTILS_CPU_H_
19 
20 #include <cstdint>
21 
22 namespace libgav1 {
23 
24 #if defined(__i386__) || defined(__x86_64__)
25 #define LIBGAV1_X86
26 #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
27 #define LIBGAV1_X86
28 #define LIBGAV1_X86_MSVC
29 #endif
30 
31 #if defined(LIBGAV1_X86)
32 
33 #if !defined(LIBGAV1_ENABLE_SSE4_1)
34 #define LIBGAV1_ENABLE_SSE4_1 1
35 #endif
36 
37 #if LIBGAV1_ENABLE_SSE4_1
38 #if !defined(LIBGAV1_ENABLE_AVX2)
39 #define LIBGAV1_ENABLE_AVX2 1
40 #endif  // !defined(LIBGAV1_ENABLE_AVX2)
41 #else   // !LIBGAV1_ENABLE_SSE4_1
42 // Disable AVX2 when SSE4.1 is disabled as it may rely on shared components.
43 #undef LIBGAV1_ENABLE_AVX2
44 #define LIBGAV1_ENABLE_AVX2 0
45 #endif  // LIBGAV1_ENABLE_SSE4_1
46 
47 #else  // !LIBGAV1_X86
48 
49 #undef LIBGAV1_ENABLE_AVX2
50 #define LIBGAV1_ENABLE_AVX2 0
51 #undef LIBGAV1_ENABLE_SSE4_1
52 #define LIBGAV1_ENABLE_SSE4_1 0
53 
54 #endif  // LIBGAV1_X86
55 
56 // For x86 LIBGAV1_TARGETING_* indicate the source being built is targeting
57 // (at least) that instruction set. This prevents disabling other instruction
58 // sets if the current instruction set isn't a global target, e.g., building
59 // *_avx2.cc w/-mavx2, but the remaining files without the flag.
60 #if LIBGAV1_ENABLE_AVX2 && defined(__AVX2__)
61 #define LIBGAV1_TARGETING_AVX2 1
62 #else
63 #define LIBGAV1_TARGETING_AVX2 0
64 #endif
65 
66 // Note: LIBGAV1_X86_MSVC isn't completely correct for Visual Studio, but there
67 // is no equivalent to __SSE4_1__. LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS will be
68 // enabled in dsp.h to compensate for this.
69 #if LIBGAV1_ENABLE_SSE4_1 && (defined(__SSE4_1__) || defined(LIBGAV1_X86_MSVC))
70 #define LIBGAV1_TARGETING_SSE4_1 1
71 #else
72 #define LIBGAV1_TARGETING_SSE4_1 0
73 #endif
74 
75 #undef LIBGAV1_X86
76 
77 #if !defined(LIBGAV1_ENABLE_NEON)
78 // TODO(jzern): add support for _M_ARM64.
79 #if defined(__ARM_NEON__) || defined(__aarch64__) || \
80     (defined(_MSC_VER) && defined(_M_ARM))
81 #define LIBGAV1_ENABLE_NEON 1
82 #else
83 #define LIBGAV1_ENABLE_NEON 0
84 #endif
85 #endif  // !defined(LIBGAV1_ENABLE_NEON)
86 
87 enum CpuFeatures : uint8_t {
88   kSSE2 = 1 << 0,
89 #define LIBGAV1_CPU_SSE2 (1 << 0)
90   kSSSE3 = 1 << 1,
91 #define LIBGAV1_CPU_SSSE3 (1 << 1)
92   kSSE4_1 = 1 << 2,
93 #define LIBGAV1_CPU_SSE4_1 (1 << 2)
94   kAVX = 1 << 3,
95 #define LIBGAV1_CPU_AVX (1 << 3)
96   kAVX2 = 1 << 4,
97 #define LIBGAV1_CPU_AVX2 (1 << 4)
98   kNEON = 1 << 5,
99 #define LIBGAV1_CPU_NEON (1 << 5)
100 };
101 
102 // Returns a bit-wise OR of CpuFeatures supported by this platform.
103 uint32_t GetCpuInfo();
104 
105 }  // namespace libgav1
106 
107 #endif  // LIBGAV1_SRC_UTILS_CPU_H_
108