1 /*****************************************************************************
2 * cpu.c: CPU detection code
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
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
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 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 General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 # include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
33 # include <setjmp.h> /* longjmp, setjmp */
36 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
37 #include <sys/sysctl.h>
40 /*****************************************************************************
42 *****************************************************************************/
44 static void SigHandler ( int );
47 /*****************************************************************************
48 * Global variables - they're needed for signal handling
49 *****************************************************************************/
53 #if defined( __i386__ ) || defined( __x86_64__ )
54 static const char *psz_capability;
58 /*****************************************************************************
59 * CPUCapabilities: get the CPU capabilities
60 *****************************************************************************
61 * This function is called to list extensions the CPU may have.
62 *****************************************************************************/
63 uint32_t CPUCapabilities( void )
65 volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
67 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
68 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
69 int i_has_altivec = 0;
70 size_t i_length = sizeof( i_has_altivec );
71 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
73 i_capabilities |= CPU_CAPABILITY_FPU;
75 if( i_error == 0 && i_has_altivec != 0 )
76 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
78 return i_capabilities;
80 #elif defined( __i386__ ) || defined( __x86_64__ )
81 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
82 volatile vlc_bool_t b_amd;
84 /* Needed for x86 CPU capabilities detection */
85 # if defined( __x86_64__ )
86 # define cpuid( reg ) \
87 asm volatile ( "cpuid\n\t" \
96 # define cpuid( reg ) \
97 asm volatile ( "push %%ebx\n\t" \
109 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
110 && defined( HAVE_SIGNAL_H )
111 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
114 i_capabilities |= CPU_CAPABILITY_FPU;
116 # if defined( __i386__ )
117 /* check if cpuid instruction is supported */
118 asm volatile ( "push %%ebx\n\t"
121 "movl %%eax, %%ebx\n\t"
122 "xorl $0x200000, %%eax\n\t"
136 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
137 && defined( HAVE_SIGNAL_H )
138 signal( SIGILL, pf_sigill );
140 return i_capabilities;
143 /* x86_64 supports cpuid instruction, so we dont need to check it */
146 i_capabilities |= CPU_CAPABILITY_486;
148 /* the CPU supports the CPUID instruction - get its level */
153 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
154 && defined( HAVE_SIGNAL_H )
155 signal( SIGILL, pf_sigill );
157 return i_capabilities;
160 /* FIXME: this isn't correct, since some 486s have cpuid */
161 i_capabilities |= CPU_CAPABILITY_586;
163 /* borrowed from mpeg2dec */
164 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
165 && ( i_edx == 0x69746e65 );
167 /* test for the MMX flag */
170 if( ! (i_edx & 0x00800000) )
172 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
173 && defined( HAVE_SIGNAL_H )
174 signal( SIGILL, pf_sigill );
176 return i_capabilities;
179 i_capabilities |= CPU_CAPABILITY_MMX;
181 if( i_edx & 0x02000000 )
183 i_capabilities |= CPU_CAPABILITY_MMXEXT;
185 # ifdef CAN_COMPILE_SSE
186 /* We test if OS supports the SSE instructions */
187 psz_capability = "SSE";
190 if( setjmp( env ) == 0 )
192 /* Test a SSE instruction */
193 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
198 i_capabilities |= CPU_CAPABILITY_SSE;
203 if( i_edx & 0x04000000 )
205 # if defined(CAN_COMPILE_SSE)
206 /* We test if OS supports the SSE instructions */
207 psz_capability = "SSE2";
210 if( setjmp( env ) == 0 )
212 /* Test a SSE2 instruction */
213 __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
218 i_capabilities |= CPU_CAPABILITY_SSE2;
223 /* test for additional capabilities */
226 if( i_eax < 0x80000001 )
228 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
229 && defined( HAVE_SIGNAL_H )
230 signal( SIGILL, pf_sigill );
232 return i_capabilities;
235 /* list these additional capabilities */
238 # ifdef CAN_COMPILE_3DNOW
239 if( i_edx & 0x80000000 )
241 psz_capability = "3D Now!";
244 if( setjmp( env ) == 0 )
246 /* Test a 3D Now! instruction */
247 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
252 i_capabilities |= CPU_CAPABILITY_3DNOW;
257 if( b_amd && ( i_edx & 0x00400000 ) )
259 i_capabilities |= CPU_CAPABILITY_MMXEXT;
262 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
263 && defined( HAVE_SIGNAL_H )
264 signal( SIGILL, pf_sigill );
266 return i_capabilities;
268 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
270 # ifdef CAN_COMPILE_ALTIVEC && defined( HAVE_SIGNAL_H )
271 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
273 i_capabilities |= CPU_CAPABILITY_FPU;
277 if( setjmp( env ) == 0 )
279 asm volatile ("mtspr 256, %0\n\t"
280 "vand %%v0, %%v0, %%v0"
287 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
290 signal( SIGILL, pf_sigill );
293 return i_capabilities;
295 #elif defined( __sparc__ )
297 i_capabilities |= CPU_CAPABILITY_FPU;
298 return i_capabilities;
300 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
301 i_capabilities |= CPU_CAPABILITY_FPU;
302 return i_capabilities;
305 /* default behaviour */
306 return i_capabilities;
311 /*****************************************************************************
312 * SigHandler: system signal handler
313 *****************************************************************************
314 * This function is called when an illegal instruction signal is received by
315 * the program. We use this function to test OS and CPU capabilities
316 *****************************************************************************/
317 #if defined( HAVE_SIGNAL_H )
318 static void SigHandler( int i_signal )
320 /* Acknowledge the signal received */
324 sigrelse( i_signal );
327 #if defined( __i386__ )
328 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
329 "operating system.\n", psz_capability );
330 fprintf( stderr, " some optimizations will be disabled unless "
331 "you upgrade your OS\n" );
332 # if defined( SYS_LINUX )
333 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );
342 uint32_t cpu_flags = 0;
345 /*****************************************************************************
346 * vlc_CPU: get pre-computed CPU capability flags
347 ****************************************************************************/
348 unsigned vlc_CPU (void)