1 /*****************************************************************************
2 * cpu.c: CPU detection code
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: cpu.c,v 1.10 2002/12/06 16:34:08 sam Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 # include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
33 # include <setjmp.h> /* longjmp, setjmp */
37 # include <mach/mach.h> /* AltiVec detection */
38 # include <mach/mach_error.h> /* some day the header files||compiler *
39 will define it for us */
40 # include <mach/bootstrap.h>
45 /*****************************************************************************
47 *****************************************************************************/
49 static void SigHandler ( int );
52 /*****************************************************************************
53 * Global variables - they're needed for signal handling
54 *****************************************************************************/
58 #if defined( __i386__ )
59 static char *psz_capability;
63 /*****************************************************************************
64 * CPUCapabilities: get the CPU capabilities
65 *****************************************************************************
66 * This function is called to list extensions the CPU may have.
67 *****************************************************************************/
68 uint32_t CPUCapabilities( void )
70 volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
72 #if defined( SYS_DARWIN )
73 struct host_basic_info hi;
75 host_name_port_t host;
78 char *psz_name, *psz_subname;
80 i_capabilities |= CPU_CAPABILITY_FPU;
82 /* Should 'never' fail? */
83 host = mach_host_self();
85 i_size = sizeof( hi ) / sizeof( int );
86 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
88 if( ret != KERN_SUCCESS )
90 fprintf( stderr, "error: couldn't get CPU information\n" );
91 return i_capabilities;
94 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
95 /* FIXME: need better way to detect newer proccessors.
96 * could do strncmp(a,b,5), but that's real ugly */
97 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
99 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
102 return i_capabilities;
104 #elif defined( __i386__ )
105 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
106 volatile vlc_bool_t b_amd;
108 /* Needed for x86 CPU capabilities detection */
109 # define cpuid( reg ) \
110 asm volatile ( "pushl %%ebx\n\t" \
112 "movl %%ebx,%1\n\t" \
121 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
122 && defined( HAVE_SIGNAL_H )
123 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
126 i_capabilities |= CPU_CAPABILITY_FPU;
128 /* test for a 486 CPU */
129 asm volatile ( "pushl %%ebx\n\t"
132 "movl %%eax, %%ebx\n\t"
133 "xorl $0x200000, %%eax\n\t"
147 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
148 && defined( HAVE_SIGNAL_H )
149 signal( SIGILL, pf_sigill );
151 return i_capabilities;
154 i_capabilities |= CPU_CAPABILITY_486;
156 /* the CPU supports the CPUID instruction - get its level */
161 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
162 && defined( HAVE_SIGNAL_H )
163 signal( SIGILL, pf_sigill );
165 return i_capabilities;
168 /* FIXME: this isn't correct, since some 486s have cpuid */
169 i_capabilities |= CPU_CAPABILITY_586;
171 /* borrowed from mpeg2dec */
172 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
173 && ( i_edx == 0x69746e65 );
175 /* test for the MMX flag */
178 if( ! (i_edx & 0x00800000) )
180 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
181 && defined( HAVE_SIGNAL_H )
182 signal( SIGILL, pf_sigill );
184 return i_capabilities;
187 i_capabilities |= CPU_CAPABILITY_MMX;
189 if( i_edx & 0x02000000 )
191 i_capabilities |= CPU_CAPABILITY_MMXEXT;
193 # ifdef CAN_COMPILE_SSE
194 /* We test if OS supports the SSE instructions */
195 psz_capability = "SSE";
198 if( setjmp( env ) == 0 )
200 /* Test a SSE instruction */
201 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
206 i_capabilities |= CPU_CAPABILITY_SSE;
211 /* test for additional capabilities */
214 if( i_eax < 0x80000001 )
216 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
217 && defined( HAVE_SIGNAL_H )
218 signal( SIGILL, pf_sigill );
220 return i_capabilities;
223 /* list these additional capabilities */
226 # ifdef CAN_COMPILE_3DNOW
227 if( i_edx & 0x80000000 )
229 psz_capability = "3D Now!";
232 if( setjmp( env ) == 0 )
234 /* Test a 3D Now! instruction */
235 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
240 i_capabilities |= CPU_CAPABILITY_3DNOW;
245 if( b_amd && ( i_edx & 0x00400000 ) )
247 i_capabilities |= CPU_CAPABILITY_MMXEXT;
250 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
251 && defined( HAVE_SIGNAL_H )
252 signal( SIGILL, pf_sigill );
254 return i_capabilities;
256 #elif defined( __powerpc__ )
258 # ifdef CAN_COMPILE_ALTIVEC && defined( HAVE_SIGNAL_H )
259 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
261 i_capabilities |= CPU_CAPABILITY_FPU;
265 if( setjmp( env ) == 0 )
267 asm volatile ("mtspr 256, %0\n\t"
268 "vand %%v0, %%v0, %%v0"
275 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
278 signal( SIGILL, pf_sigill );
281 return i_capabilities;
283 #elif defined( __sparc__ )
285 i_capabilities |= CPU_CAPABILITY_FPU;
286 return i_capabilities;
289 /* default behaviour */
290 return i_capabilities;
295 /*****************************************************************************
296 * SigHandler: system signal handler
297 *****************************************************************************
298 * This function is called when an illegal instruction signal is received by
299 * the program. We use this function to test OS and CPU capabilities
300 *****************************************************************************/
301 #if defined( HAVE_SIGNAL_H )
302 static void SigHandler( int i_signal )
304 /* Acknowledge the signal received */
308 sigrelse( i_signal );
311 #if defined( __i386__ )
312 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
313 "operating system.\n", psz_capability );
314 fprintf( stderr, " some optimizations will be disabled unless "
315 "you upgrade your OS\n" );
316 # if defined( SYS_LINUX )
317 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );