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 * Masks CPU capability flags.
38 *
39 * This overrides the cached CPU capability flags with the intersection of
40 * the actually-detected CPU capabilities and the supplied mask. Bits cannot be
41 * added, only cleared: masking never reports a capability that hardware
42 * doesn't actually have.
43 *
44 * This is intended for testing purposes only (e.g. checkasm), to force
45 * code paths for lesser CPU capabilities to be exercised.
46 *
47 * \warning This function is not thread-safe with respect to vlc_CPU(): it
48 * must be called before any thread makes its first call to vlc_CPU(),
49 * otherwise the lazy initialisation in vlc_CPU() may race with and
50 * overwrite the masked value.
51 */
52VLC_API void vlc_CPU_set(unsigned mask);
53
54/**
55 * Computes CPU capability flags.
56 *
57 * Do not call this function directly.
58 * Call vlc_CPU() instead, which caches the correct value.
59 */
60unsigned vlc_CPU_raw(void);
61
62# if defined (__aarch64__)
63# define HAVE_FPU 1
64# define VLC_CPU_ARM_NEON 0x1
65# define VLC_CPU_ARM_SVE 0x2
66
67# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
68# define vlc_CPU_ARM_SVE() ((vlc_CPU() & VLC_CPU_ARM_SVE) != 0)
69
70# elif defined (__arm__)
71# if defined (__VFP_FP__) && !defined (__SOFTFP__)
72# define HAVE_FPU 1
73# else
74# define HAVE_FPU 0
75# endif
76# define VLC_CPU_ARMv6 4
77# define VLC_CPU_ARM_NEON 2
78
79# if defined (__ARM_ARCH_7A__)
80# define VLC_CPU_ARM_ARCH 7
81# elif defined (__ARM_ARCH_6__) || defined (__ARM_ARCH_6T2__)
82# define VLC_CPU_ARM_ARCH 6
83# else
84# define VLC_CPU_ARM_ARCH 4
85# endif
86
87# define vlc_CPU_ARMv6() ((vlc_CPU() & VLC_CPU_ARMv6) != 0)
88# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
89
90
91# elif defined (__loongarch__)
92# define HAVE_FPU 1
93
94# elif defined (__mips)
95# if defined (__mips_hard_float)
96# define HAVE_FPU 1
97# else
98# define HAVE_FPU 0
99# endif
100
101# elif defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
102# define HAVE_FPU 1
103# define VLC_CPU_ALTIVEC 2
104
105# define vlc_CPU_ALTIVEC() ((vlc_CPU() & VLC_CPU_ALTIVEC) != 0)
106# ifdef ALTIVEC
107# define VLC_ALTIVEC
108# else
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# define vlc_CPU_RV_V() ((vlc_CPU() & VLC_CPU_RV_V) != 0)
122# define vlc_CPU_RV_B() ((vlc_CPU() & VLC_CPU_RV_B) != 0)
123
124# elif defined (__sparc__)
125# define HAVE_FPU 1
126
127# elif defined (__x86_64__) || defined (__i386__)
128# define HAVE_FPU 1
129# define VLC_CPU_SSE2 0x00000080
130# define VLC_CPU_SSE3 0x00000100
131# define VLC_CPU_SSSE3 0x00000200
132# define VLC_CPU_SSE4_1 0x00000400
133# define VLC_CPU_AVX 0x00002000
134# define VLC_CPU_AVX2 0x00004000
135
136# if defined (__SSE__)
137# define VLC_SSE
138# else
139# define VLC_SSE __attribute__ ((__target__ ("sse")))
140# endif
141
142# define vlc_CPU_SSE2() ((vlc_CPU() & VLC_CPU_SSE2) != 0)
143# define vlc_CPU_SSE3() ((vlc_CPU() & VLC_CPU_SSE3) != 0)
144# define vlc_CPU_SSSE3() ((vlc_CPU() & VLC_CPU_SSSE3) != 0)
145# define vlc_CPU_SSE4_1() ((vlc_CPU() & VLC_CPU_SSE4_1) != 0)
146
147# define vlc_CPU_AVX() ((vlc_CPU() & VLC_CPU_AVX) != 0)
148# ifdef __AVX__
149# define VLC_AVX
150# else
151# define VLC_AVX __attribute__ ((__target__ ("avx")))
152# endif
153
154# define vlc_CPU_AVX2() ((vlc_CPU() & VLC_CPU_AVX2) != 0)
155
156# else
157/**
158 * Are single precision floating point operations "fast"?
159 * If this preprocessor constant is zero, floating point should be avoided
160 * (especially relevant for audio codecs).
161 */
162# define HAVE_FPU 1
164# endif
165
166/**
167 * Initialises DSP functions.
168 *
169 * This helper looks for accelerated Digital Signal Processing functions
170 * identified by the supplied type name. Those functions ares typically
171 * implemented using architecture-specific assembler code with
172 * Single Instruction Multiple Data (SIMD) opcodes for faster processing.
173 *
174 * The exact purposes and semantics of the DSP functions is uniquely identified
175 * by a nul-terminated string.
176 *
177 * \note This function should not be used directly. It is recommended to use
178 * the convenience wrapper vlc_CPU_functions_init_once() instead.
179 *
180 * \param name nul-terminated type identifier (cannot be NULL)
181 * \param [inout] funcs type-specific data structure to be initialised
182 */
183VLC_API void vlc_CPU_functions_init(const char *name, void *restrict funcs);
184
185# ifndef __cplusplus
186/**
187 * Initialises DSP functions once.
188 *
189 * This is a convenience wrapper for vlc_CPU_functions_init().
190 * It only initialises the functions the first time it is evaluated.
191 */
192static inline void vlc_CPU_functions_init_once(const char *name,
193 void *restrict funcs)
194{
195 static vlc_once_t once = VLC_STATIC_ONCE;
196
197 if (!vlc_once_begin(&once)) {
199 vlc_once_complete(&once);
200 }
201}
202# endif
203
204#define set_cpu_funcs(name, activate, priority) \
205 set_callback(VLC_CHECKED_TYPE(void (*)(void *), activate)) \
206 set_capability(name, priority)
207
208#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:213
static void vlc_CPU_functions_init_once(const char *name, void *restrict funcs)
Initialises DSP functions once.
Definition vlc_cpu.h:193
unsigned vlc_CPU(void)
Retrieves CPU capability flags.
Definition cpu.c:149
unsigned vlc_CPU_raw(void)
Computes CPU capability flags.
Definition cpu.c:55
void vlc_CPU_set(unsigned mask)
Masks CPU capability flags.
Definition cpu.c:161
Thread primitive declarations.