VLC 4.0.0-dev
Loading...
Searching...
No Matches
vlc_cpu.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_cpu.h: CPU capabilities
3 *****************************************************************************
4 * Copyright (C) 1998-2009 VLC authors and VideoLAN
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20
21/**
22 * \file
23 * This file provides CPU features detection.
24 */
25
26#ifndef VLC_CPU_H
27# define VLC_CPU_H 1
28
29#include <vlc_threads.h>
30
31/**
32 * Retrieves CPU capability flags.
33 */
34VLC_API unsigned vlc_CPU(void);
35
36/**
37 * Computes CPU capability flags.
38 *
39 * Do not call this function directly.
40 * Call vlc_CPU() instead, which caches the correct value.
41 */
42unsigned vlc_CPU_raw(void);
43
44# if defined (__aarch64__)
45# define HAVE_FPU 1
46# define VLC_CPU_ARM_NEON 0x1
47# define VLC_CPU_ARM_SVE 0x2
48
49# ifdef __ARM_NEON
50# define vlc_CPU_ARM_NEON() (1)
51# else
52# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
53# endif
54
55# ifdef __ARM_FEATURE_SVE
56# define vlc_CPU_ARM_SVE() (1)
57# else
58# define vlc_CPU_ARM_SVE() ((vlc_CPU() & VLC_CPU_ARM_SVE) != 0)
59# endif
60
61# elif defined (__arm__)
62# if defined (__VFP_FP__) && !defined (__SOFTFP__)
63# define HAVE_FPU 1
64# else
65# define HAVE_FPU 0
66# endif
67# define VLC_CPU_ARMv6 4
68# define VLC_CPU_ARM_NEON 2
69
70# if defined (__ARM_ARCH_7A__)
71# define VLC_CPU_ARM_ARCH 7
72# elif defined (__ARM_ARCH_6__) || defined (__ARM_ARCH_6T2__)
73# define VLC_CPU_ARM_ARCH 6
74# else
75# define VLC_CPU_ARM_ARCH 4
76# endif
77
78# if (VLC_CPU_ARM_ARCH >= 6)
79# define vlc_CPU_ARMv6() (1)
80# else
81# define vlc_CPU_ARMv6() ((vlc_CPU() & VLC_CPU_ARMv6) != 0)
82# endif
83
84# ifdef __ARM_NEON__
85# define vlc_CPU_ARM_NEON() (1)
86# else
87# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
88# endif
89
90# elif defined (__loongarch__)
91# define HAVE_FPU 1
92
93# elif defined (__mips)
94# if defined (__mips_hard_float)
95# define HAVE_FPU 1
96# else
97# define HAVE_FPU 0
98# endif
99
100# elif defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
101# define HAVE_FPU 1
102# define VLC_CPU_ALTIVEC 2
103
104# ifdef ALTIVEC
105# define vlc_CPU_ALTIVEC() (1)
106# define VLC_ALTIVEC
107# else
108# define vlc_CPU_ALTIVEC() ((vlc_CPU() & VLC_CPU_ALTIVEC) != 0)
109# define VLC_ALTIVEC __attribute__ ((__target__ ("altivec")))
110# endif
111
112# elif defined (__riscv)
113# ifdef __riscv_f
114# define HAVE_FPU 1
115# else
116# define HAVE_FPU 0
117# endif
118# define VLC_CPU_RV_V 0x1
119# define VLC_CPU_RV_B 0x2
120
121# ifdef __riscv_v
122# define vlc_CPU_RV_V() (1)
123# else
124# define vlc_CPU_RV_V() ((vlc_CPU() & VLC_CPU_RV_V) != 0)
125# endif
126
127# if (defined (__riscv_b) || (defined (__riscv_zba) && defined (__riscv_zbb) \
128 && defined (__riscv_zbs)))
129# define vlc_CPU_RV_B() (1)
130# else
131# define vlc_CPU_RV_B() ((vlc_CPU() & VLC_CPU_RV_B) != 0)
132# endif
133
134# elif defined (__sparc__)
135# define HAVE_FPU 1
136
137# elif defined (__x86_64__) || defined (__i386__)
138# define HAVE_FPU 1
139# define VLC_CPU_SSE2 0x00000080
140# define VLC_CPU_SSE3 0x00000100
141# define VLC_CPU_SSSE3 0x00000200
142# define VLC_CPU_SSE4_1 0x00000400
143# define VLC_CPU_AVX 0x00002000
144# define VLC_CPU_AVX2 0x00004000
145
146# if defined (__SSE__)
147# define VLC_SSE
148# else
149# define VLC_SSE __attribute__ ((__target__ ("sse")))
150# endif
151
152# ifdef __SSE2__
153# define vlc_CPU_SSE2() (1)
154# else
155# define vlc_CPU_SSE2() ((vlc_CPU() & VLC_CPU_SSE2) != 0)
156# endif
157
158# ifdef __SSE3__
159# define vlc_CPU_SSE3() (1)
160# else
161# define vlc_CPU_SSE3() ((vlc_CPU() & VLC_CPU_SSE3) != 0)
162# endif
163
164# ifdef __SSSE3__
165# define vlc_CPU_SSSE3() (1)
166# else
167# define vlc_CPU_SSSE3() ((vlc_CPU() & VLC_CPU_SSSE3) != 0)
168# endif
169
170# ifdef __SSE4_1__
171# define vlc_CPU_SSE4_1() (1)
172# else
173# define vlc_CPU_SSE4_1() ((vlc_CPU() & VLC_CPU_SSE4_1) != 0)
174# endif
175
176# ifdef __AVX__
177# define vlc_CPU_AVX() (1)
178# define VLC_AVX
179# else
180# define vlc_CPU_AVX() ((vlc_CPU() & VLC_CPU_AVX) != 0)
181# define VLC_AVX __attribute__ ((__target__ ("avx")))
182# endif
183
184# ifdef __AVX2__
185# define vlc_CPU_AVX2() (1)
186# else
187# define vlc_CPU_AVX2() ((vlc_CPU() & VLC_CPU_AVX2) != 0)
188# endif
189
190# else
191/**
192 * Are single precision floating point operations "fast"?
193 * If this preprocessor constant is zero, floating point should be avoided
194 * (especially relevant for audio codecs).
195 */
196# define HAVE_FPU 1
198# endif
199
200/**
201 * Initialises DSP functions.
202 *
203 * This helper looks for accelerated Digital Signal Processing functions
204 * identified by the supplied type name. Those functions ares typically
205 * implemented using architecture-specific assembler code with
206 * Single Instruction Multiple Data (SIMD) opcodes for faster processing.
207 *
208 * The exact purposes and semantics of the DSP functions is uniquely identified
209 * by a nul-terminated string.
210 *
211 * \note This function should not be used directly. It is recommended to use
212 * the convenience wrapper vlc_CPU_functions_init_once() instead.
213 *
214 * \param name nul-terminated type identifier (cannot be NULL)
215 * \param [inout] funcs type-specific data structure to be initialised
216 */
217VLC_API void vlc_CPU_functions_init(const char *name, void *restrict funcs);
218
219# ifndef __cplusplus
220/**
221 * Initialises DSP functions once.
222 *
223 * This is a convenience wrapper for vlc_CPU_functions_init().
224 * It only initialises the functions the first time it is evaluated.
225 */
226static inline void vlc_CPU_functions_init_once(const char *name,
227 void *restrict funcs)
228{
229 static vlc_once_t once = VLC_STATIC_ONCE;
230
231 if (!vlc_once_begin(&once)) {
233 vlc_once_complete(&once);
234 }
235}
236# endif
237
238#define set_cpu_funcs(name, activate, priority) \
239 set_callback(VLC_CHECKED_TYPE(void (*)(void *), activate)) \
240 set_capability(name, priority)
241
242#endif /* !VLC_CPU_H */
#define VLC_API
Definition fourcc_gen.c:31
void vlc_once_complete(vlc_once_t *restrict once)
Completes a one-time initialization.
Definition threads.c:508
#define vlc_once_begin(once)
Definition vlc_threads.h:565
#define VLC_STATIC_ONCE
Static initializer for one-time initialization.
Definition vlc_threads.h:531
const char name[16]
Definition httpd.c:1298
One-time initialization.
Definition vlc_threads.h:524
This file is a collection of common definitions and types.
void vlc_CPU_functions_init(const char *name, void *restrict funcs)
Initialises DSP functions.
Definition cpu.c:207
static void vlc_CPU_functions_init_once(const char *name, void *restrict funcs)
Initialises DSP functions once.
Definition vlc_cpu.h:227
unsigned vlc_CPU(void)
Retrieves CPU capability flags.
Definition cpu.c:147
unsigned vlc_CPU_raw(void)
Computes CPU capability flags.
Definition cpu.c:55
Thread primitive declarations.