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>
35 #include <signal.h> /* SIGHUP, SIGINT, SIGKILL */
36 #include <setjmp.h> /* longjmp, setjmp */
40 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
41 #include <sys/sysctl.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static void SigHandler ( int );
49 /*****************************************************************************
50 * Global variables - they're needed for signal handling
51 *****************************************************************************/
54 #if defined( __i386__ ) || defined( __x86_64__ )
55 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( __i386__ ) || defined( __x86_64__ )
68 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
71 /* Needed for x86 CPU capabilities detection */
72 # if defined( __x86_64__ )
73 # define cpuid( reg ) \
74 asm volatile ( "cpuid\n\t" \
83 # define cpuid( reg ) \
84 asm volatile ( "push %%ebx\n\t" \
96 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
97 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
100 i_capabilities |= CPU_CAPABILITY_FPU;
102 # if defined( __i386__ )
103 /* check if cpuid instruction is supported */
104 asm volatile ( "push %%ebx\n\t"
107 "movl %%eax, %%ebx\n\t"
108 "xorl $0x200000, %%eax\n\t"
123 /* x86_64 supports cpuid instruction, so we dont need to check it */
126 i_capabilities |= CPU_CAPABILITY_486;
128 /* the CPU supports the CPUID instruction - get its level */
134 /* FIXME: this isn't correct, since some 486s have cpuid */
135 i_capabilities |= CPU_CAPABILITY_586;
137 /* borrowed from mpeg2dec */
138 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
139 && ( i_edx == 0x69746e65 );
141 /* test for the MMX flag */
144 if( ! (i_edx & 0x00800000) )
147 i_capabilities |= CPU_CAPABILITY_MMX;
149 if( i_edx & 0x02000000 )
151 i_capabilities |= CPU_CAPABILITY_MMXEXT;
153 # ifdef CAN_COMPILE_SSE
154 /* We test if OS supports the SSE instructions */
155 psz_capability = "SSE";
158 if( setjmp( env ) == 0 )
160 /* Test a SSE instruction */
161 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
166 i_capabilities |= CPU_CAPABILITY_SSE;
171 if( i_edx & 0x04000000 )
173 # if defined(CAN_COMPILE_SSE)
174 /* We test if OS supports the SSE instructions */
175 psz_capability = "SSE2";
178 if( setjmp( env ) == 0 )
180 /* Test a SSE2 instruction */
181 __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
186 i_capabilities |= CPU_CAPABILITY_SSE2;
191 /* test for additional capabilities */
194 if( i_eax < 0x80000001 )
197 /* list these additional capabilities */
200 # ifdef CAN_COMPILE_3DNOW
201 if( i_edx & 0x80000000 )
203 psz_capability = "3D Now!";
206 if( setjmp( env ) == 0 )
208 /* Test a 3D Now! instruction */
209 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
214 i_capabilities |= CPU_CAPABILITY_3DNOW;
219 if( b_amd && ( i_edx & 0x00400000 ) )
221 i_capabilities |= CPU_CAPABILITY_MMXEXT;
225 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
226 signal( SIGILL, pf_sigill );
229 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
231 i_capabilities |= CPU_CAPABILITY_FPU;
233 # if defined(__APPLE__)
234 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
235 int i_has_altivec = 0;
236 size_t i_length = sizeof( i_has_altivec );
237 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
239 if( i_error == 0 && i_has_altivec != 0 )
240 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
242 # elif defined( CAN_COMPILE_ALTIVEC )
243 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
247 if( setjmp( env ) == 0 )
249 asm volatile ("mtspr 256, %0\n\t"
250 "vand %%v0, %%v0, %%v0"
256 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
258 signal( SIGILL, pf_sigill );
260 (void)SigHandler; /* Don't complain about dead code here */
263 #elif defined( __sparc__ )
264 i_capabilities |= CPU_CAPABILITY_FPU;
266 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
267 i_capabilities |= CPU_CAPABILITY_FPU;
270 return i_capabilities;
273 /*****************************************************************************
274 * SigHandler: system signal handler
275 *****************************************************************************
276 * This function is called when an illegal instruction signal is received by
277 * the program. We use this function to test OS and CPU capabilities
278 *****************************************************************************/
279 static void SigHandler( int i_signal )
281 /* Acknowledge the signal received */
285 sigrelse( i_signal );
287 VLC_UNUSED( i_signal );
290 #if defined( __i386__ )
291 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
292 "operating system.\n", psz_capability );
293 fprintf( stderr, " some optimizations will be disabled unless "
294 "you upgrade your OS\n" );
295 # if defined( __linux__ )
296 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );
304 uint32_t cpu_flags = 0;
307 /*****************************************************************************
308 * vlc_CPU: get pre-computed CPU capability flags
309 ****************************************************************************/
310 unsigned vlc_CPU (void)
315 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
316 static vlc_memset_t pf_vlc_memset = memset;
318 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
327 * vlc_memcpy: fast CPU-dependent memcpy
329 void *vlc_memcpy (void *tgt, const void *src, size_t n)
331 return pf_vlc_memcpy (tgt, src, n);
335 * vlc_memset: fast CPU-dependent memset
337 void *vlc_memset (void *tgt, int c, size_t n)
339 return pf_vlc_memset (tgt, c, n);