1 /*****************************************************************************
2 * cpu.c: CPU detection code
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: cpu.c,v 1.7 2002/10/03 13:21:55 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 *****************************************************************************/
29 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
30 #include <setjmp.h> /* longjmp, setjmp */
35 # include <mach/mach.h> /* AltiVec detection */
36 # include <mach/mach_error.h> /* some day the header files||compiler *
37 will define it for us */
38 # include <mach/bootstrap.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static void SigHandler ( int );
48 /*****************************************************************************
49 * Global variables - they're needed for signal handling
50 *****************************************************************************/
53 #if defined( __i386__ )
54 static char *psz_capability;
57 /*****************************************************************************
58 * CPUCapabilities: get the CPU capabilities
59 *****************************************************************************
60 * This function is called to list extensions the CPU may have.
61 *****************************************************************************/
62 u32 CPUCapabilities( void )
64 volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
66 #if defined( SYS_DARWIN )
67 struct host_basic_info hi;
69 host_name_port_t host;
72 char *psz_name, *psz_subname;
74 i_capabilities |= CPU_CAPABILITY_FPU;
76 /* Should 'never' fail? */
77 host = mach_host_self();
79 i_size = sizeof( hi ) / sizeof( int );
80 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
82 if( ret != KERN_SUCCESS )
84 fprintf( stderr, "error: couldn't get CPU information\n" );
85 return i_capabilities;
88 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
89 /* FIXME: need better way to detect newer proccessors.
90 * could do strncmp(a,b,5), but that's real ugly */
91 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
93 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
96 return i_capabilities;
98 #elif defined( __i386__ )
99 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
100 volatile vlc_bool_t b_amd;
102 /* Needed for x86 CPU capabilities detection */
103 # define cpuid( a ) \
104 asm volatile ( "pushl %%ebx\n\t" \
106 "movl %%ebx,%1\n\t" \
115 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
116 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
119 i_capabilities |= CPU_CAPABILITY_FPU;
121 /* test for a 486 CPU */
122 asm volatile ( "pushl %%ebx\n\t"
125 "movl %%eax, %%ebx\n\t"
126 "xorl $0x200000, %%eax\n\t"
140 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
141 signal( SIGILL, pf_sigill );
143 return i_capabilities;
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 signal( SIGILL, pf_sigill );
156 return i_capabilities;
159 /* FIXME: this isn't correct, since some 486s have cpuid */
160 i_capabilities |= CPU_CAPABILITY_586;
162 /* borrowed from mpeg2dec */
163 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
164 && ( i_edx == 0x69746e65 );
166 /* test for the MMX flag */
169 if( ! (i_edx & 0x00800000) )
171 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
172 signal( SIGILL, pf_sigill );
174 return i_capabilities;
177 i_capabilities |= CPU_CAPABILITY_MMX;
179 if( i_edx & 0x02000000 )
181 i_capabilities |= CPU_CAPABILITY_MMXEXT;
183 # ifdef CAN_COMPILE_SSE
184 /* We test if OS supports the SSE instructions */
185 psz_capability = "SSE";
188 if( setjmp( env ) == 0 )
190 /* Test a SSE instruction */
191 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
196 i_capabilities |= CPU_CAPABILITY_SSE;
201 /* test for additional capabilities */
204 if( i_eax < 0x80000001 )
206 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
207 signal( SIGILL, pf_sigill );
209 return i_capabilities;
212 /* list these additional capabilities */
215 # ifdef CAN_COMPILE_3DNOW
216 if( i_edx & 0x80000000 )
218 psz_capability = "3D Now!";
221 if( setjmp( env ) == 0 )
223 /* Test a 3D Now! instruction */
224 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
229 i_capabilities |= CPU_CAPABILITY_3DNOW;
234 if( b_amd && ( i_edx & 0x00400000 ) )
236 i_capabilities |= CPU_CAPABILITY_MMXEXT;
239 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
240 signal( SIGILL, pf_sigill );
242 return i_capabilities;
244 #elif defined( __powerpc__ )
246 # ifdef CAN_COMPILE_ALTIVEC
247 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
249 i_capabilities |= CPU_CAPABILITY_FPU;
253 if( setjmp( env ) == 0 )
255 asm volatile ("mtspr 256, %0\n\t"
256 "vand %%v0, %%v0, %%v0"
263 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
266 signal( SIGILL, pf_sigill );
269 return i_capabilities;
271 #elif defined( __sparc__ )
273 i_capabilities |= CPU_CAPABILITY_FPU;
274 return i_capabilities;
277 /* default behaviour */
278 return i_capabilities;
283 /*****************************************************************************
284 * SigHandler: system signal handler
285 *****************************************************************************
286 * This function is called when an illegal instruction signal is received by
287 * the program. We use this function to test OS and CPU capabilities
288 *****************************************************************************/
289 static void SigHandler( int i_signal )
291 /* Acknowledge the signal received */
295 sigrelse( i_signal );
298 #if defined( __i386__ )
299 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
300 "operating system.\n", psz_capability );
301 fprintf( stderr, " some optimizations will be disabled unless "
302 "you upgrade your OS\n" );
303 # if defined( SYS_LINUX )
304 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );