VLC 4.0.0-dev
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 (__i386__) || defined (__x86_64__)
45# define HAVE_FPU 1
46# define VLC_CPU_SSE2 0x00000080
47# define VLC_CPU_SSE3 0x00000100
48# define VLC_CPU_SSSE3 0x00000200
49# define VLC_CPU_SSE4_1 0x00000400
50# define VLC_CPU_AVX 0x00002000
51# define VLC_CPU_AVX2 0x00004000
52
53# if defined (__SSE__)
54# define VLC_SSE
55# else
56# define VLC_SSE __attribute__ ((__target__ ("sse")))
57# endif
58
59# ifdef __SSE2__
60# define vlc_CPU_SSE2() (1)
61# else
62# define vlc_CPU_SSE2() ((vlc_CPU() & VLC_CPU_SSE2) != 0)
63# endif
64
65# ifdef __SSE3__
66# define vlc_CPU_SSE3() (1)
67# else
68# define vlc_CPU_SSE3() ((vlc_CPU() & VLC_CPU_SSE3) != 0)
69# endif
70
71# ifdef __SSSE3__
72# define vlc_CPU_SSSE3() (1)
73# else
74# define vlc_CPU_SSSE3() ((vlc_CPU() & VLC_CPU_SSSE3) != 0)
75# endif
76
77# ifdef __SSE4_1__
78# define vlc_CPU_SSE4_1() (1)
79# else
80# define vlc_CPU_SSE4_1() ((vlc_CPU() & VLC_CPU_SSE4_1) != 0)
81# endif
82
83# ifdef __AVX__
84# define vlc_CPU_AVX() (1)
85# define VLC_AVX
86# else
87# define vlc_CPU_AVX() ((vlc_CPU() & VLC_CPU_AVX) != 0)
88# define VLC_AVX __attribute__ ((__target__ ("avx")))
89# endif
90
91# ifdef __AVX2__
92# define vlc_CPU_AVX2() (1)
93# else
94# define vlc_CPU_AVX2() ((vlc_CPU() & VLC_CPU_AVX2) != 0)
95# endif
96
97# elif defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
98# define HAVE_FPU 1
99# define VLC_CPU_ALTIVEC 2
100
101# ifdef ALTIVEC
102# define vlc_CPU_ALTIVEC() (1)
103# define VLC_ALTIVEC
104# else
105# define vlc_CPU_ALTIVEC() ((vlc_CPU() & VLC_CPU_ALTIVEC) != 0)
106# define VLC_ALTIVEC __attribute__ ((__target__ ("altivec")))
107# endif
108
109# elif defined (__arm__)
110# if defined (__VFP_FP__) && !defined (__SOFTFP__)
111# define HAVE_FPU 1
112# else
113# define HAVE_FPU 0
114# endif
115# define VLC_CPU_ARMv6 4
116# define VLC_CPU_ARM_NEON 2
117
118# if defined (__ARM_ARCH_7A__)
119# define VLC_CPU_ARM_ARCH 7
120# elif defined (__ARM_ARCH_6__) || defined (__ARM_ARCH_6T2__)
121# define VLC_CPU_ARM_ARCH 6
122# else
123# define VLC_CPU_ARM_ARCH 4
124# endif
125
126# if (VLC_CPU_ARM_ARCH >= 6)
127# define vlc_CPU_ARMv6() (1)
128# else
129# define vlc_CPU_ARMv6() ((vlc_CPU() & VLC_CPU_ARMv6) != 0)
130# endif
131
132# ifdef __ARM_NEON__
133# define vlc_CPU_ARM_NEON() (1)
134# else
135# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
136# endif
137
138# elif defined (__aarch64__)
139# define HAVE_FPU 1
140# define VLC_CPU_ARM_NEON 0x1
141# define VLC_CPU_ARM_SVE 0x2
142
143# ifdef __ARM_NEON
144# define vlc_CPU_ARM_NEON() (1)
145# else
146# define vlc_CPU_ARM_NEON() ((vlc_CPU() & VLC_CPU_ARM_NEON) != 0)
147# endif
148
149# ifdef __ARM_FEATURE_SVE
150# define vlc_CPU_ARM_SVE() (1)
151# else
152# define vlc_CPU_ARM_SVE() ((vlc_CPU() & VLC_CPU_ARM_SVE) != 0)
153# endif
154
155# elif defined (__sparc__)
156# define HAVE_FPU 1
157
158# elif defined (__mips_hard_float)
159# define HAVE_FPU 1
160
161# elif defined (__riscv)
162# ifdef __riscv_flen
163# define HAVE_FPU 1
164# endif
165# define VLC_CPU_RV_V 0x1
166
167# ifdef __riscv_v
168# define vlc_CPU_RV_V() (1)
169# else
170# define vlc_CPU_RV_V() ((vlc_CPU() & VLC_CPU_RV_V) != 0)
171# endif
172
173# else
174/**
175 * Are single precision floating point operations "fast"?
176 * If this preprocessor constant is zero, floating point should be avoided
177 * (especially relevant for audio codecs).
178 */
179# define HAVE_FPU 0
181# endif
182
183/**
184 * Initialises DSP functions.
185 *
186 * This helper looks for accelerated Digital Signal Processing functions
187 * identified by the supplied type name. Those functions ares typically
188 * implemented using architecture-specific assembler code with
189 * Single Instruction Multiple Data (SIMD) opcodes for faster processing.
190 *
191 * The exact purposes and semantics of the DSP functions is uniquely identified
192 * by a nul-terminated string.
193 *
194 * \note This function should not be used directly. It is recommended to use
195 * the convenience wrapper vlc_CPU_functions_init_once() instead.
196 *
197 * \param name nul-terminated type identifier (cannot be NULL)
198 * \param [inout] funcs type-specific data structure to be initialised
199 */
200VLC_API void vlc_CPU_functions_init(const char *name, void *restrict funcs);
201
202# ifndef __cplusplus
203/**
204 * Initialises DSP functions once.
205 *
206 * This is a convenience wrapper for vlc_CPU_functions_init().
207 * It only initialises the functions the first time it is evaluated.
208 */
209static inline void vlc_CPU_functions_init_once(const char *name,
210 void *restrict funcs)
211{
212 static vlc_once_t once = VLC_STATIC_ONCE;
213
214 if (!vlc_once_begin(&once)) {
216 vlc_once_complete(&once);
217 }
218}
219# endif
220
221#define set_cpu_funcs(name, activate, priority) \
222 set_callback(VLC_CHECKED_TYPE(void (*)(void *), activate)) \
223 set_capability(name, priority)
224
225#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:532
#define vlc_once_begin(once)
Definition: vlc_threads.h:659
#define VLC_STATIC_ONCE
Static initializer for one-time initialization.
Definition: vlc_threads.h:625
const char name[16]
Definition: httpd.c:1281
One-time initialization.
Definition: vlc_threads.h:618
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:205
static void vlc_CPU_functions_init_once(const char *name, void *restrict funcs)
Initialises DSP functions once.
Definition: vlc_cpu.h:210
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.