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( __arm__ )
230 # if defined( __ARM_EABI__ ) && !defined( __SOFTFP__ )
231 i_capabilities |= CPU_CAPABILITY_FPU;
233 return i_capabilities;
235 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
237 i_capabilities |= CPU_CAPABILITY_FPU;
239 # if defined(__APPLE__)
240 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
241 int i_has_altivec = 0;
242 size_t i_length = sizeof( i_has_altivec );
243 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
245 if( i_error == 0 && i_has_altivec != 0 )
246 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
248 # elif defined( CAN_COMPILE_ALTIVEC )
249 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
253 if( setjmp( env ) == 0 )
255 asm volatile ("mtspr 256, %0\n\t"
256 "vand %%v0, %%v0, %%v0"
262 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
264 signal( SIGILL, pf_sigill );
266 (void)SigHandler; /* Don't complain about dead code here */
269 #elif defined( __sparc__ )
270 i_capabilities |= CPU_CAPABILITY_FPU;
272 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
273 i_capabilities |= CPU_CAPABILITY_FPU;
276 return i_capabilities;
279 /*****************************************************************************
280 * SigHandler: system signal handler
281 *****************************************************************************
282 * This function is called when an illegal instruction signal is received by
283 * the program. We use this function to test OS and CPU capabilities
284 *****************************************************************************/
285 static void SigHandler( int i_signal )
287 /* Acknowledge the signal received */
291 sigrelse( i_signal );
293 VLC_UNUSED( i_signal );
296 #if defined( __i386__ )
297 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
298 "operating system.\n", psz_capability );
299 fprintf( stderr, " some optimizations will be disabled unless "
300 "you upgrade your OS\n" );
301 # if defined( __linux__ )
302 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );
310 uint32_t cpu_flags = 0;
313 /*****************************************************************************
314 * vlc_CPU: get pre-computed CPU capability flags
315 ****************************************************************************/
316 unsigned vlc_CPU (void)
321 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
322 static vlc_memset_t pf_vlc_memset = memset;
324 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
333 * vlc_memcpy: fast CPU-dependent memcpy
335 void *vlc_memcpy (void *tgt, const void *src, size_t n)
337 return pf_vlc_memcpy (tgt, src, n);
341 * vlc_memset: fast CPU-dependent memset
343 void *vlc_memset (void *tgt, int c, size_t n)
345 return pf_vlc_memset (tgt, c, n);