xref: /aosp_15_r20/external/libdav1d/src/cpu.h (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
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_default_cpu_flags(void)57 static ALWAYS_INLINE unsigned dav1d_get_default_cpu_flags(void) {
58     unsigned flags = 0;
59 
60 #if ARCH_AARCH64 || ARCH_ARM
61 #if defined(__ARM_NEON) || defined(__APPLE__) || defined(_WIN32) || ARCH_AARCH64
62     flags |= DAV1D_ARM_CPU_FLAG_NEON;
63 #endif
64 #ifdef __ARM_FEATURE_DOTPROD
65     flags |= DAV1D_ARM_CPU_FLAG_DOTPROD;
66 #endif
67 #ifdef __ARM_FEATURE_MATMUL_INT8
68     flags |= DAV1D_ARM_CPU_FLAG_I8MM;
69 #endif
70 #if ARCH_AARCH64
71 #ifdef __ARM_FEATURE_SVE
72     flags |= DAV1D_ARM_CPU_FLAG_SVE;
73 #endif
74 #ifdef __ARM_FEATURE_SVE2
75     flags |= DAV1D_ARM_CPU_FLAG_SVE2;
76 #endif
77 #endif /* ARCH_AARCH64 */
78 #elif ARCH_PPC64LE
79 #if defined(__VSX__)
80     flags |= DAV1D_PPC_CPU_FLAG_VSX;
81 #endif
82 #if defined(__POWER9_VECTOR__)
83     flags |= DAV1D_PPC_CPU_FLAG_PWR9;
84 #endif
85 #elif ARCH_RISCV
86 #if defined(__riscv_v)
87     flags |= DAV1D_RISCV_CPU_FLAG_V;
88 #endif
89 #elif ARCH_X86
90 #if defined(__AVX512F__) && defined(__AVX512CD__) && \
91     defined(__AVX512BW__) && defined(__AVX512DQ__) && \
92     defined(__AVX512VL__) && defined(__AVX512VNNI__) && \
93     defined(__AVX512IFMA__) && defined(__AVX512VBMI__) && \
94     defined(__AVX512VBMI2__) && defined(__AVX512VPOPCNTDQ__) && \
95     defined(__AVX512BITALG__) && defined(__GFNI__) && \
96     defined(__VAES__) && defined(__VPCLMULQDQ__)
97     flags |= DAV1D_X86_CPU_FLAG_AVX512ICL |
98              DAV1D_X86_CPU_FLAG_AVX2 |
99              DAV1D_X86_CPU_FLAG_SSE41 |
100              DAV1D_X86_CPU_FLAG_SSSE3 |
101              DAV1D_X86_CPU_FLAG_SSE2;
102 #elif defined(__AVX2__)
103     flags |= DAV1D_X86_CPU_FLAG_AVX2 |
104              DAV1D_X86_CPU_FLAG_SSE41 |
105              DAV1D_X86_CPU_FLAG_SSSE3 |
106              DAV1D_X86_CPU_FLAG_SSE2;
107 #elif defined(__SSE4_1__) || defined(__AVX__)
108     flags |= DAV1D_X86_CPU_FLAG_SSE41 |
109              DAV1D_X86_CPU_FLAG_SSSE3 |
110              DAV1D_X86_CPU_FLAG_SSE2;
111 #elif defined(__SSSE3__)
112     flags |= DAV1D_X86_CPU_FLAG_SSSE3 |
113              DAV1D_X86_CPU_FLAG_SSE2;
114 #elif ARCH_X86_64 || defined(__SSE2__) || \
115       (defined(_M_IX86_FP) && _M_IX86_FP >= 2)
116     flags |= DAV1D_X86_CPU_FLAG_SSE2;
117 #endif
118 #endif
119 
120     return flags;
121 }
122 
dav1d_get_cpu_flags(void)123 static ALWAYS_INLINE unsigned dav1d_get_cpu_flags(void) {
124     unsigned flags = dav1d_cpu_flags & dav1d_cpu_flags_mask;
125 
126 #if TRIM_DSP_FUNCTIONS
127 /* Since this function is inlined, unconditionally setting a flag here will
128  * enable dead code elimination in the calling function. */
129     flags |= dav1d_get_default_cpu_flags();
130 #endif
131 
132     return flags;
133 }
134 
135 #endif /* DAV1D_SRC_CPU_H */
136