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 *****************************************************************************/
33 #include <vlc_common.h>
36 # include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
37 # include <setjmp.h> /* longjmp, setjmp */
42 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
43 #include <sys/sysctl.h>
46 /*****************************************************************************
48 *****************************************************************************/
50 static void SigHandler ( int );
53 /*****************************************************************************
54 * Global variables - they're needed for signal handling
55 *****************************************************************************/
59 #if defined( __i386__ ) || defined( __x86_64__ )
60 static const char *psz_capability;
64 /*****************************************************************************
65 * CPUCapabilities: get the CPU capabilities
66 *****************************************************************************
67 * This function is called to list extensions the CPU may have.
68 *****************************************************************************/
69 uint32_t CPUCapabilities( void )
71 volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
73 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
74 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
75 int i_has_altivec = 0;
76 size_t i_length = sizeof( i_has_altivec );
77 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
79 i_capabilities |= CPU_CAPABILITY_FPU;
81 if( i_error == 0 && i_has_altivec != 0 )
82 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
84 return i_capabilities;
86 #elif defined( __i386__ ) || defined( __x86_64__ )
87 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
90 /* Needed for x86 CPU capabilities detection */
91 # if defined( __x86_64__ )
92 # define cpuid( reg ) \
93 asm volatile ( "cpuid\n\t" \
102 # define cpuid( reg ) \
103 asm volatile ( "push %%ebx\n\t" \
105 "movl %%ebx,%1\n\t" \
115 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
116 && defined( HAVE_SIGNAL_H )
117 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
120 i_capabilities |= CPU_CAPABILITY_FPU;
122 # if defined( __i386__ )
123 /* check if cpuid instruction is supported */
124 asm volatile ( "push %%ebx\n\t"
127 "movl %%eax, %%ebx\n\t"
128 "xorl $0x200000, %%eax\n\t"
142 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
143 && defined( HAVE_SIGNAL_H )
144 signal( SIGILL, pf_sigill );
146 return i_capabilities;
149 /* x86_64 supports cpuid instruction, so we dont need to check it */
152 i_capabilities |= CPU_CAPABILITY_486;
154 /* the CPU supports the CPUID instruction - get its level */
159 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
160 && defined( HAVE_SIGNAL_H )
161 signal( SIGILL, pf_sigill );
163 return i_capabilities;
166 /* FIXME: this isn't correct, since some 486s have cpuid */
167 i_capabilities |= CPU_CAPABILITY_586;
169 /* borrowed from mpeg2dec */
170 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
171 && ( i_edx == 0x69746e65 );
173 /* test for the MMX flag */
176 if( ! (i_edx & 0x00800000) )
178 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
179 && defined( HAVE_SIGNAL_H )
180 signal( SIGILL, pf_sigill );
182 return i_capabilities;
185 i_capabilities |= CPU_CAPABILITY_MMX;
187 if( i_edx & 0x02000000 )
189 i_capabilities |= CPU_CAPABILITY_MMXEXT;
191 # ifdef CAN_COMPILE_SSE
192 /* We test if OS supports the SSE instructions */
193 psz_capability = "SSE";
196 if( setjmp( env ) == 0 )
198 /* Test a SSE instruction */
199 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
204 i_capabilities |= CPU_CAPABILITY_SSE;
209 if( i_edx & 0x04000000 )
211 # if defined(CAN_COMPILE_SSE)
212 /* We test if OS supports the SSE instructions */
213 psz_capability = "SSE2";
216 if( setjmp( env ) == 0 )
218 /* Test a SSE2 instruction */
219 __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
224 i_capabilities |= CPU_CAPABILITY_SSE2;
229 /* test for additional capabilities */
232 if( i_eax < 0x80000001 )
234 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
235 && defined( HAVE_SIGNAL_H )
236 signal( SIGILL, pf_sigill );
238 return i_capabilities;
241 /* list these additional capabilities */
244 # ifdef CAN_COMPILE_3DNOW
245 if( i_edx & 0x80000000 )
247 psz_capability = "3D Now!";
250 if( setjmp( env ) == 0 )
252 /* Test a 3D Now! instruction */
253 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
258 i_capabilities |= CPU_CAPABILITY_3DNOW;
263 if( b_amd && ( i_edx & 0x00400000 ) )
265 i_capabilities |= CPU_CAPABILITY_MMXEXT;
268 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
269 && defined( HAVE_SIGNAL_H )
270 signal( SIGILL, pf_sigill );
272 return i_capabilities;
274 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
276 # ifdef CAN_COMPILE_ALTIVEC && defined( HAVE_SIGNAL_H )
277 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
279 i_capabilities |= CPU_CAPABILITY_FPU;
283 if( setjmp( env ) == 0 )
285 asm volatile ("mtspr 256, %0\n\t"
286 "vand %%v0, %%v0, %%v0"
293 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
296 signal( SIGILL, pf_sigill );
298 (void)SigHandler; /* Don't complain about dead code here */
301 return i_capabilities;
303 #elif defined( __sparc__ )
305 i_capabilities |= CPU_CAPABILITY_FPU;
306 return i_capabilities;
308 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
309 i_capabilities |= CPU_CAPABILITY_FPU;
310 return i_capabilities;
313 /* default behaviour */
314 return i_capabilities;
319 /*****************************************************************************
320 * SigHandler: system signal handler
321 *****************************************************************************
322 * This function is called when an illegal instruction signal is received by
323 * the program. We use this function to test OS and CPU capabilities
324 *****************************************************************************/
325 #if defined( HAVE_SIGNAL_H )
326 static void SigHandler( int i_signal )
328 /* Acknowledge the signal received */
332 sigrelse( i_signal );
334 VLC_UNUSED( i_signal );
337 #if defined( __i386__ )
338 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
339 "operating system.\n", psz_capability );
340 fprintf( stderr, " some optimizations will be disabled unless "
341 "you upgrade your OS\n" );
342 # if defined( __linux__ )
343 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );
352 uint32_t cpu_flags = 0;
355 /*****************************************************************************
356 * vlc_CPU: get pre-computed CPU capability flags
357 ****************************************************************************/
358 unsigned vlc_CPU (void)
363 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
364 static vlc_memset_t pf_vlc_memset = memset;
366 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
375 * vlc_memcpy: fast CPU-dependent memcpy
377 void *vlc_memcpy (void *tgt, const void *src, size_t n)
379 return pf_vlc_memcpy (tgt, src, n);
383 * vlc_memset: fast CPU-dependent memset
385 void *vlc_memset (void *tgt, int c, size_t n)
387 return pf_vlc_memset (tgt, c, n);