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(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
68 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
69 int i_has_altivec = 0;
70 size_t i_length = sizeof( i_has_altivec );
71 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
73 i_capabilities |= CPU_CAPABILITY_FPU;
75 if( i_error == 0 && i_has_altivec != 0 )
76 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
78 return i_capabilities;
80 #elif defined( __i386__ ) || defined( __x86_64__ )
81 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
84 /* Needed for x86 CPU capabilities detection */
85 # if defined( __x86_64__ )
86 # define cpuid( reg ) \
87 asm volatile ( "cpuid\n\t" \
96 # define cpuid( reg ) \
97 asm volatile ( "push %%ebx\n\t" \
109 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
110 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
113 i_capabilities |= CPU_CAPABILITY_FPU;
115 # if defined( __i386__ )
116 /* check if cpuid instruction is supported */
117 asm volatile ( "push %%ebx\n\t"
120 "movl %%eax, %%ebx\n\t"
121 "xorl $0x200000, %%eax\n\t"
135 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
136 signal( SIGILL, pf_sigill );
138 return i_capabilities;
141 /* x86_64 supports cpuid instruction, so we dont need to check it */
144 i_capabilities |= CPU_CAPABILITY_486;
146 /* the CPU supports the CPUID instruction - get its level */
151 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
152 signal( SIGILL, pf_sigill );
154 return i_capabilities;
157 /* FIXME: this isn't correct, since some 486s have cpuid */
158 i_capabilities |= CPU_CAPABILITY_586;
160 /* borrowed from mpeg2dec */
161 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
162 && ( i_edx == 0x69746e65 );
164 /* test for the MMX flag */
167 if( ! (i_edx & 0x00800000) )
169 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
170 signal( SIGILL, pf_sigill );
172 return i_capabilities;
175 i_capabilities |= CPU_CAPABILITY_MMX;
177 if( i_edx & 0x02000000 )
179 i_capabilities |= CPU_CAPABILITY_MMXEXT;
181 # ifdef CAN_COMPILE_SSE
182 /* We test if OS supports the SSE instructions */
183 psz_capability = "SSE";
186 if( setjmp( env ) == 0 )
188 /* Test a SSE instruction */
189 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
194 i_capabilities |= CPU_CAPABILITY_SSE;
199 if( i_edx & 0x04000000 )
201 # if defined(CAN_COMPILE_SSE)
202 /* We test if OS supports the SSE instructions */
203 psz_capability = "SSE2";
206 if( setjmp( env ) == 0 )
208 /* Test a SSE2 instruction */
209 __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
214 i_capabilities |= CPU_CAPABILITY_SSE2;
219 /* test for additional capabilities */
222 if( i_eax < 0x80000001 )
224 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
225 signal( SIGILL, pf_sigill );
227 return i_capabilities;
230 /* list these additional capabilities */
233 # ifdef CAN_COMPILE_3DNOW
234 if( i_edx & 0x80000000 )
236 psz_capability = "3D Now!";
239 if( setjmp( env ) == 0 )
241 /* Test a 3D Now! instruction */
242 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
247 i_capabilities |= CPU_CAPABILITY_3DNOW;
252 if( b_amd && ( i_edx & 0x00400000 ) )
254 i_capabilities |= CPU_CAPABILITY_MMXEXT;
257 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
258 signal( SIGILL, pf_sigill );
260 return i_capabilities;
262 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
264 # ifdef CAN_COMPILE_ALTIVEC
265 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
267 i_capabilities |= CPU_CAPABILITY_FPU;
271 if( setjmp( env ) == 0 )
273 asm volatile ("mtspr 256, %0\n\t"
274 "vand %%v0, %%v0, %%v0"
281 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
284 signal( SIGILL, pf_sigill );
286 (void)SigHandler; /* Don't complain about dead code here */
289 return i_capabilities;
291 #elif defined( __sparc__ )
293 i_capabilities |= CPU_CAPABILITY_FPU;
294 return i_capabilities;
296 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
297 i_capabilities |= CPU_CAPABILITY_FPU;
298 return i_capabilities;
301 /* default behaviour */
302 return i_capabilities;
307 /*****************************************************************************
308 * SigHandler: system signal handler
309 *****************************************************************************
310 * This function is called when an illegal instruction signal is received by
311 * the program. We use this function to test OS and CPU capabilities
312 *****************************************************************************/
313 static void SigHandler( int i_signal )
315 /* Acknowledge the signal received */
319 sigrelse( i_signal );
321 VLC_UNUSED( i_signal );
324 #if defined( __i386__ )
325 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
326 "operating system.\n", psz_capability );
327 fprintf( stderr, " some optimizations will be disabled unless "
328 "you upgrade your OS\n" );
329 # if defined( __linux__ )
330 fprintf( stderr, " (for instance Linux kernel 2.4.x or later)\n" );
338 uint32_t cpu_flags = 0;
341 /*****************************************************************************
342 * vlc_CPU: get pre-computed CPU capability flags
343 ****************************************************************************/
344 unsigned vlc_CPU (void)
349 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
350 static vlc_memset_t pf_vlc_memset = memset;
352 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
361 * vlc_memcpy: fast CPU-dependent memcpy
363 void *vlc_memcpy (void *tgt, const void *src, size_t n)
365 return pf_vlc_memcpy (tgt, src, n);
369 * vlc_memset: fast CPU-dependent memset
371 void *vlc_memset (void *tgt, int c, size_t n)
373 return pf_vlc_memset (tgt, c, n);