1 /*
2 * Copyright © 2018-2022, VideoLAN and dav1d authors
3 * Copyright © 2018-2022, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef DAV1D_SRC_CPU_H
29 #define DAV1D_SRC_CPU_H
30
31 #include "config.h"
32
33 #include "common/attributes.h"
34
35 #include "dav1d/common.h"
36 #include "dav1d/dav1d.h"
37
38 #if ARCH_AARCH64 || ARCH_ARM
39 #include "src/arm/cpu.h"
40 #elif ARCH_LOONGARCH
41 #include "src/loongarch/cpu.h"
42 #elif ARCH_PPC64LE
43 #include "src/ppc/cpu.h"
44 #elif ARCH_RISCV
45 #include "src/riscv/cpu.h"
46 #elif ARCH_X86
47 #include "src/x86/cpu.h"
48 #endif
49
50 EXTERN unsigned dav1d_cpu_flags;
51 EXTERN unsigned dav1d_cpu_flags_mask;
52
53 void dav1d_init_cpu(void);
54 DAV1D_API void dav1d_set_cpu_flags_mask(unsigned mask);
55 int dav1d_num_logical_processors(Dav1dContext *c);
56
dav1d_get_cpu_flags(void)57 static ALWAYS_INLINE unsigned dav1d_get_cpu_flags(void) {
58 unsigned flags = dav1d_cpu_flags & dav1d_cpu_flags_mask;
59
60 #if TRIM_DSP_FUNCTIONS
61 /* Since this function is inlined, unconditionally setting a flag here will
62 * enable dead code elimination in the calling function. */
63 #if ARCH_AARCH64 || ARCH_ARM
64 #if defined(__ARM_NEON) || defined(__APPLE__) || defined(_WIN32) || ARCH_AARCH64
65 flags |= DAV1D_ARM_CPU_FLAG_NEON;
66 #endif
67 #ifdef __ARM_FEATURE_DOTPROD
68 flags |= DAV1D_ARM_CPU_FLAG_DOTPROD;
69 #endif
70 #ifdef __ARM_FEATURE_MATMUL_INT8
71 flags |= DAV1D_ARM_CPU_FLAG_I8MM;
72 #endif
73 #if ARCH_AARCH64
74 #ifdef __ARM_FEATURE_SVE
75 flags |= DAV1D_ARM_CPU_FLAG_SVE;
76 #endif
77 #ifdef __ARM_FEATURE_SVE2
78 flags |= DAV1D_ARM_CPU_FLAG_SVE2;
79 #endif
80 #endif /* ARCH_AARCH64 */
81 #elif ARCH_PPC64LE
82 #if defined(__VSX__)
83 flags |= DAV1D_PPC_CPU_FLAG_VSX;
84 #endif
85 #if defined(__POWER9_VECTOR__)
86 flags |= DAV1D_PPC_CPU_FLAG_PWR9;
87 #endif
88 #elif ARCH_RISCV
89 #if defined(__riscv_v)
90 flags |= DAV1D_RISCV_CPU_FLAG_V;
91 #endif
92 #elif ARCH_X86
93 #if defined(__AVX512F__) && defined(__AVX512CD__) && \
94 defined(__AVX512BW__) && defined(__AVX512DQ__) && \
95 defined(__AVX512VL__) && defined(__AVX512VNNI__) && \
96 defined(__AVX512IFMA__) && defined(__AVX512VBMI__) && \
97 defined(__AVX512VBMI2__) && defined(__AVX512VPOPCNTDQ__) && \
98 defined(__AVX512BITALG__) && defined(__GFNI__) && \
99 defined(__VAES__) && defined(__VPCLMULQDQ__)
100 flags |= DAV1D_X86_CPU_FLAG_AVX512ICL |
101 DAV1D_X86_CPU_FLAG_AVX2 |
102 DAV1D_X86_CPU_FLAG_SSE41 |
103 DAV1D_X86_CPU_FLAG_SSSE3 |
104 DAV1D_X86_CPU_FLAG_SSE2;
105 #elif defined(__AVX2__)
106 flags |= DAV1D_X86_CPU_FLAG_AVX2 |
107 DAV1D_X86_CPU_FLAG_SSE41 |
108 DAV1D_X86_CPU_FLAG_SSSE3 |
109 DAV1D_X86_CPU_FLAG_SSE2;
110 #elif defined(__SSE4_1__) || defined(__AVX__)
111 flags |= DAV1D_X86_CPU_FLAG_SSE41 |
112 DAV1D_X86_CPU_FLAG_SSSE3 |
113 DAV1D_X86_CPU_FLAG_SSE2;
114 #elif defined(__SSSE3__)
115 flags |= DAV1D_X86_CPU_FLAG_SSSE3 |
116 DAV1D_X86_CPU_FLAG_SSE2;
117 #elif ARCH_X86_64 || defined(__SSE2__) || \
118 (defined(_M_IX86_FP) && _M_IX86_FP >= 2)
119 flags |= DAV1D_X86_CPU_FLAG_SSE2;
120 #endif
121 #endif
122 #endif
123
124 return flags;
125 }
126
127 #endif /* DAV1D_SRC_CPU_H */
128