access: rist: fix misspellings on help text
[vlc.git] / src / misc / cpu.c
blobe8d3013adea98159e3f6166ceb6d6f4164d10724
1 /*****************************************************************************
2 * cpu.c: CPU detection code
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Christophe Massiot <massiot@via.ecp.fr>
9 * Eugenio Jarosiewicz <ej0@cise.ufl.eduEujenio>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_cpu.h>
35 #include <vlc_memstream.h>
36 #include "libvlc.h"
38 #include <assert.h>
40 #include <sys/types.h>
41 #ifndef _WIN32
42 #include <unistd.h>
43 #include <sys/wait.h>
44 #include <signal.h>
45 #else
46 #include <errno.h>
47 #endif
49 #ifdef __APPLE__
50 #include <sys/sysctl.h>
51 #endif
53 #if defined(__OpenBSD__) && defined(__powerpc__)
54 #include <sys/param.h>
55 #include <sys/sysctl.h>
56 #include <machine/cpu.h>
57 #endif
59 static uint32_t cpu_flags;
61 #if defined (__i386__) || defined (__x86_64__) || defined (__powerpc__) \
62 || defined (__ppc__) || defined (__ppc64__) || defined (__powerpc64__)
63 # if defined (HAVE_FORK)
64 static bool vlc_CPU_check (const char *name, void (*func) (void))
66 pid_t pid = fork();
68 switch (pid)
70 case 0:
71 signal (SIGILL, SIG_DFL);
72 func ();
73 _exit (0);
74 case -1:
75 return false;
78 int status;
79 while( waitpid( pid, &status, 0 ) == -1 );
81 if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
82 return true;
84 fprintf (stderr, "Warning: your CPU has %s instructions, but not your "
85 "operating system.\n", name);
86 fprintf( stderr, " some optimizations will be disabled unless "
87 "you upgrade your OS\n" );
88 return false;
91 #if defined (CAN_COMPILE_SSE) && !defined (__SSE__)
92 VLC_SSE static void SSE_test (void)
94 asm volatile ("xorps %%xmm0,%%xmm0\n" : : : "xmm0", "xmm1");
96 #endif
97 #if defined (CAN_COMPILE_3DNOW)
98 VLC_MMX static void ThreeD_Now_test (void)
100 asm volatile ("pfadd %%mm0,%%mm0\n" "femms\n" : : : "mm0");
102 #endif
104 #if defined (CAN_COMPILE_ALTIVEC)
105 static void Altivec_test (void)
107 asm volatile ("mtspr 256, %0\n" "vand %%v0, %%v0, %%v0\n" : : "r" (-1));
109 #endif
111 #else /* _WIN32 || __OS2__ */
112 # define vlc_CPU_check(name, func) (1)
113 #endif
114 #endif
117 * Determines the CPU capabilities and stores them in cpu_flags.
118 * The result can be retrieved with vlc_CPU().
120 static void vlc_CPU_init(void)
122 uint32_t i_capabilities = 0;
124 #if defined( __i386__ ) || defined( __x86_64__ )
125 unsigned int i_eax, i_ebx, i_ecx, i_edx;
126 bool b_amd;
128 /* Needed for x86 CPU capabilities detection */
129 # if defined (__i386__) && defined (__PIC__)
130 # define cpuid(reg) \
131 asm volatile ("xchgl %%ebx,%1\n\t" \
132 "cpuid\n\t" \
133 "xchgl %%ebx,%1\n\t" \
134 : "=a" (i_eax), "=r" (i_ebx), "=c" (i_ecx), "=d" (i_edx) \
135 : "a" (reg) \
136 : "cc");
137 # else
138 # define cpuid(reg) \
139 asm volatile ("cpuid\n\t" \
140 : "=a" (i_eax), "=b" (i_ebx), "=c" (i_ecx), "=d" (i_edx) \
141 : "a" (reg) \
142 : "cc");
143 # endif
144 /* Check if the OS really supports the requested instructions */
145 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
146 && !defined (__i686__) && !defined (__pentium4__) \
147 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
148 /* check if cpuid instruction is supported */
149 asm volatile ( "push %%ebx\n\t"
150 "pushf\n\t"
151 "pop %%eax\n\t"
152 "movl %%eax, %%ebx\n\t"
153 "xorl $0x200000, %%eax\n\t"
154 "push %%eax\n\t"
155 "popf\n\t"
156 "pushf\n\t"
157 "pop %%eax\n\t"
158 "movl %%ebx,%1\n\t"
159 "pop %%ebx\n\t"
160 : "=a" ( i_eax ),
161 "=r" ( i_ebx )
163 : "cc" );
165 if( i_eax == i_ebx )
166 goto out;
167 # endif
169 /* the CPU supports the CPUID instruction - get its level */
170 cpuid( 0x00000000 );
172 # if defined (__i386__) && !defined (__i586__) \
173 && !defined (__i686__) && !defined (__pentium4__) \
174 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
175 if( !i_eax )
176 goto out;
177 #endif
179 /* borrowed from mpeg2dec */
180 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
181 && ( i_edx == 0x69746e65 );
183 /* test for the MMX flag */
184 cpuid( 0x00000001 );
185 # if !defined (__MMX__)
186 if( ! (i_edx & 0x00800000) )
187 goto out;
188 # endif
189 i_capabilities |= VLC_CPU_MMX;
191 if( i_edx & 0x02000000 )
192 i_capabilities |= VLC_CPU_MMXEXT;
193 # if defined (CAN_COMPILE_SSE) && !defined (__SSE__)
194 if (( i_edx & 0x02000000 ) && vlc_CPU_check ("SSE", SSE_test))
195 # endif
197 /*if( i_edx & 0x02000000 )*/
198 i_capabilities |= VLC_CPU_SSE;
199 if (i_edx & 0x04000000)
200 i_capabilities |= VLC_CPU_SSE2;
201 if (i_ecx & 0x00000001)
202 i_capabilities |= VLC_CPU_SSE3;
203 if (i_ecx & 0x00000200)
204 i_capabilities |= VLC_CPU_SSSE3;
205 if (i_ecx & 0x00080000)
206 i_capabilities |= VLC_CPU_SSE4_1;
207 if (i_ecx & 0x00100000)
208 i_capabilities |= VLC_CPU_SSE4_2;
211 /* test for additional capabilities */
212 cpuid( 0x80000000 );
214 if( i_eax < 0x80000001 )
215 goto out;
217 /* list these additional capabilities */
218 cpuid( 0x80000001 );
220 # if defined (CAN_COMPILE_3DNOW) && !defined (__3dNOW__)
221 if ((i_edx & 0x80000000) && vlc_CPU_check ("3D Now!", ThreeD_Now_test))
222 # endif
223 i_capabilities |= VLC_CPU_3dNOW;
225 if( b_amd && ( i_edx & 0x00400000 ) )
226 i_capabilities |= VLC_CPU_MMXEXT;
227 out:
229 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
230 || defined( __ppc64__ )
232 # if defined(__APPLE__) || defined(__OpenBSD__)
233 # if defined(__OpenBSD__)
234 int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
235 # else
236 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
237 # endif
238 int i_has_altivec = 0;
239 size_t i_length = sizeof( i_has_altivec );
240 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
242 if( i_error == 0 && i_has_altivec != 0 )
243 i_capabilities |= VLC_CPU_ALTIVEC;
245 # elif defined( CAN_COMPILE_ALTIVEC )
246 if (vlc_CPU_check ("Altivec", Altivec_test))
247 i_capabilities |= VLC_CPU_ALTIVEC;
249 # endif
251 #endif
253 cpu_flags = i_capabilities;
257 * Retrieves pre-computed CPU capability flags
259 VLC_WEAK unsigned vlc_CPU(void)
261 static vlc_once_t once = VLC_STATIC_ONCE;
262 vlc_once(&once, vlc_CPU_init);
263 return cpu_flags;
266 void vlc_CPU_dump (vlc_object_t *obj)
268 struct vlc_memstream stream;
270 vlc_memstream_open(&stream);
272 #if defined (__i386__) || defined (__x86_64__)
273 if (vlc_CPU_MMX())
274 vlc_memstream_puts(&stream, "MMX ");
275 if (vlc_CPU_MMXEXT())
276 vlc_memstream_puts(&stream, "MMXEXT ");
277 if (vlc_CPU_SSE())
278 vlc_memstream_puts(&stream, "SSE ");
279 if (vlc_CPU_SSE2())
280 vlc_memstream_puts(&stream, "SSE2 ");
281 if (vlc_CPU_SSE3())
282 vlc_memstream_puts(&stream, "SSE3 ");
283 if (vlc_CPU_SSSE3())
284 vlc_memstream_puts(&stream, "SSSE3 ");
285 if (vlc_CPU_SSE4_1())
286 vlc_memstream_puts(&stream, "SSE4.1 ");
287 if (vlc_CPU_SSE4_2())
288 vlc_memstream_puts(&stream, "SSE4.2 ");
289 if (vlc_CPU_SSE4A())
290 vlc_memstream_puts(&stream, "SSE4A ");
291 if (vlc_CPU_AVX())
292 vlc_memstream_puts(&stream, "AVX ");
293 if (vlc_CPU_AVX2())
294 vlc_memstream_puts(&stream, "AVX2 ");
295 if (vlc_CPU_3dNOW())
296 vlc_memstream_puts(&stream, "3DNow! ");
297 if (vlc_CPU_XOP())
298 vlc_memstream_puts(&stream, "XOP ");
299 if (vlc_CPU_FMA4())
300 vlc_memstream_puts(&stream, "FMA4 ");
302 #elif defined (__powerpc__) || defined (__ppc__) || defined (__ppc64__)
303 if (vlc_CPU_ALTIVEC())
304 vlc_memstream_puts(&stream, "AltiVec");
306 #elif defined (__arm__)
307 if (vlc_CPU_ARM_NEON())
308 vlc_memstream_puts(&stream, "ARM_NEON ");
310 #endif
312 #if HAVE_FPU
313 vlc_memstream_puts(&stream, "FPU ");
314 #endif
316 if (vlc_memstream_close(&stream) == 0)
318 msg_Dbg (obj, "CPU has capabilities %s", stream.ptr);
319 free(stream.ptr);