1 /*****************************************************************************
2 * cpu.c: CPU detection code
3 *****************************************************************************
4 * Copyright (C) 1998-2002 VideoLAN
5 * $Id: cpu.c,v 1.6 2002/08/19 11:37:57 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 );
47 static u32 Capabilities ( vlc_object_t * );
49 /*****************************************************************************
50 * Global variables - they're needed for signal handling
51 *****************************************************************************/
54 #if defined( __i386__ )
55 static char *psz_capability;
58 /*****************************************************************************
59 * CPUCapabilities: get the CPU capabilities
60 *****************************************************************************
61 * This function is a wrapper around Capabilities().
62 *****************************************************************************/
63 u32 __CPUCapabilities( vlc_object_t *p_this )
67 vlc_mutex_lock( p_this->p_vlc->p_global_lock );
68 i_capabilities = Capabilities( p_this );
69 vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
71 return i_capabilities;
74 /*****************************************************************************
75 * Capabilities: list the processors MMX support and other capabilities
76 *****************************************************************************
77 * This function is called to list extensions the CPU may have.
78 *****************************************************************************/
79 static u32 Capabilities( vlc_object_t *p_this )
81 volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
83 #if defined( SYS_DARWIN )
84 struct host_basic_info hi;
86 host_name_port_t host;
89 char *psz_name, *psz_subname;
91 i_capabilities |= CPU_CAPABILITY_FPU;
93 /* Should 'never' fail? */
94 host = mach_host_self();
96 i_size = sizeof( hi ) / sizeof( int );
97 ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
99 if( ret != KERN_SUCCESS )
101 fprintf( stderr, "error: couldn't get CPU information\n" );
102 return i_capabilities;
105 slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
106 /* FIXME: need better way to detect newer proccessors.
107 * could do strncmp(a,b,5), but that's real ugly */
108 if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
110 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
113 return i_capabilities;
115 #elif defined( __i386__ )
116 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
117 volatile vlc_bool_t b_amd;
119 /* Needed for x86 CPU capabilities detection */
120 # define cpuid( a ) \
121 asm volatile ( "pushl %%ebx\n\t" \
123 "movl %%ebx,%1\n\t" \
132 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
133 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
136 i_capabilities |= CPU_CAPABILITY_FPU;
138 /* test for a 486 CPU */
139 asm volatile ( "pushl %%ebx\n\t"
142 "movl %%eax, %%ebx\n\t"
143 "xorl $0x200000, %%eax\n\t"
157 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
158 signal( SIGILL, pf_sigill );
160 return i_capabilities;
163 i_capabilities |= CPU_CAPABILITY_486;
165 /* the CPU supports the CPUID instruction - get its level */
170 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
171 signal( SIGILL, pf_sigill );
173 return i_capabilities;
176 /* FIXME: this isn't correct, since some 486s have cpuid */
177 i_capabilities |= CPU_CAPABILITY_586;
179 /* borrowed from mpeg2dec */
180 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
181 && ( i_edx == 0x69746e65 );
183 /* test for the MMX flag */
186 if( ! (i_edx & 0x00800000) )
188 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
189 signal( SIGILL, pf_sigill );
191 return i_capabilities;
194 i_capabilities |= CPU_CAPABILITY_MMX;
196 if( i_edx & 0x02000000 )
198 i_capabilities |= CPU_CAPABILITY_MMXEXT;
200 # ifdef CAN_COMPILE_SSE
201 /* We test if OS supports the SSE instructions */
202 psz_capability = "SSE";
205 if( setjmp( env ) == 0 )
207 /* Test a SSE instruction */
208 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
213 i_capabilities |= CPU_CAPABILITY_SSE;
218 /* test for additional capabilities */
221 if( i_eax < 0x80000001 )
223 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
224 signal( SIGILL, pf_sigill );
226 return i_capabilities;
229 /* list these additional capabilities */
232 # ifdef CAN_COMPILE_3DNOW
233 if( i_edx & 0x80000000 )
235 psz_capability = "3D Now!";
238 if( setjmp( env ) == 0 )
240 /* Test a 3D Now! instruction */
241 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
246 i_capabilities |= CPU_CAPABILITY_3DNOW;
251 if( b_amd && ( i_edx & 0x00400000 ) )
253 i_capabilities |= CPU_CAPABILITY_MMXEXT;
256 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
257 signal( SIGILL, pf_sigill );
259 return i_capabilities;
261 #elif defined( __powerpc__ )
263 # ifdef CAN_COMPILE_ALTIVEC
264 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
266 i_capabilities |= CPU_CAPABILITY_FPU;
270 if( setjmp( env ) == 0 )
272 asm volatile ("mtspr 256, %0\n\t"
273 "vand %%v0, %%v0, %%v0"
280 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
283 signal( SIGILL, pf_sigill );
286 return i_capabilities;
288 #elif defined( __sparc__ )
290 i_capabilities |= CPU_CAPABILITY_FPU;
291 return i_capabilities;
294 /* default behaviour */
295 return i_capabilities;
300 /*****************************************************************************
301 * SigHandler: system signal handler
302 *****************************************************************************
303 * This function is called when an illegal instruction signal is received by
304 * the program. We use this function to test OS and CPU capabilities
305 *****************************************************************************/
306 static void SigHandler( int i_signal )
308 /* Acknowledge the signal received */
312 sigrelse( i_signal );
315 #if defined( __i386__ )
316 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
317 "operating system.\n", psz_capability );
318 fprintf( stderr, " some optimizations will be disabled unless "
319 "you upgrade your OS\n" );
320 # if defined( SYS_LINUX )
321 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );