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 *****************************************************************************/
55 #if defined( __i386__ ) || defined( __x86_64__ )
56 static void warn_cap( const char *psz_capability )
58 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
59 "operating system.\n", psz_capability );
60 fprintf( stderr, " some optimizations will be disabled unless "
61 "you upgrade your OS\n" );
65 /*****************************************************************************
66 * CPUCapabilities: get the CPU capabilities
67 *****************************************************************************
68 * This function is called to list extensions the CPU may have.
69 *****************************************************************************/
70 uint32_t CPUCapabilities( void )
72 volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
74 #if defined( __i386__ ) || defined( __x86_64__ )
75 volatile unsigned int i_eax, i_ebx, i_ecx, i_edx;
78 /* Needed for x86 CPU capabilities detection */
79 # if defined( __x86_64__ )
80 # define cpuid( reg ) \
81 asm volatile ( "cpuid\n\t" \
90 # define cpuid( reg ) \
91 asm volatile ( "push %%ebx\n\t" \
103 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
104 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
107 i_capabilities |= CPU_CAPABILITY_FPU;
109 # if defined( __i386__ )
110 /* check if cpuid instruction is supported */
111 asm volatile ( "push %%ebx\n\t"
114 "movl %%eax, %%ebx\n\t"
115 "xorl $0x200000, %%eax\n\t"
130 /* x86_64 supports cpuid instruction, so we dont need to check it */
133 i_capabilities |= CPU_CAPABILITY_486;
135 /* the CPU supports the CPUID instruction - get its level */
141 /* FIXME: this isn't correct, since some 486s have cpuid */
142 i_capabilities |= CPU_CAPABILITY_586;
144 /* borrowed from mpeg2dec */
145 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
146 && ( i_edx == 0x69746e65 );
148 /* test for the MMX flag */
151 if( ! (i_edx & 0x00800000) )
154 i_capabilities |= CPU_CAPABILITY_MMX;
156 if( i_edx & 0x02000000 )
158 i_capabilities |= CPU_CAPABILITY_MMXEXT;
160 # ifdef CAN_COMPILE_SSE
161 /* We test if OS supports the SSE instructions */
164 if( setjmp( env ) == 0 )
166 /* Test a SSE instruction */
167 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
171 i_capabilities |= CPU_CAPABILITY_SSE;
177 if( i_edx & 0x04000000 )
179 # if defined(CAN_COMPILE_SSE)
180 /* We test if OS supports the SSE instructions */
183 if( setjmp( env ) == 0 )
185 /* Test a SSE2 instruction */
186 __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
190 i_capabilities |= CPU_CAPABILITY_SSE2;
196 /* test for additional capabilities */
199 if( i_eax < 0x80000001 )
202 /* list these additional capabilities */
205 # ifdef CAN_COMPILE_3DNOW
206 if( i_edx & 0x80000000 )
210 if( setjmp( env ) == 0 )
212 /* Test a 3D Now! instruction */
213 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
217 i_capabilities |= CPU_CAPABILITY_3DNOW;
219 warn_cap( "3D Now!" );
223 if( b_amd && ( i_edx & 0x00400000 ) )
225 i_capabilities |= CPU_CAPABILITY_MMXEXT;
229 # if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
230 signal( SIGILL, pf_sigill );
233 #elif defined( __arm__ )
234 # if defined( __ARM_EABI__ ) && !defined( __SOFTFP__ )
235 i_capabilities |= CPU_CAPABILITY_FPU;
238 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
240 i_capabilities |= CPU_CAPABILITY_FPU;
242 # if defined(__APPLE__)
243 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
244 int i_has_altivec = 0;
245 size_t i_length = sizeof( i_has_altivec );
246 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
248 if( i_error == 0 && i_has_altivec != 0 )
249 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
251 # elif defined( CAN_COMPILE_ALTIVEC )
252 void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
256 if( setjmp( env ) == 0 )
258 asm volatile ("mtspr 256, %0\n\t"
259 "vand %%v0, %%v0, %%v0"
265 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
267 signal( SIGILL, pf_sigill );
269 (void)SigHandler; /* Don't complain about dead code here */
272 #elif defined( __sparc__ )
273 i_capabilities |= CPU_CAPABILITY_FPU;
275 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
276 i_capabilities |= CPU_CAPABILITY_FPU;
279 return i_capabilities;
282 /*****************************************************************************
283 * SigHandler: system signal handler
284 *****************************************************************************
285 * This function is called when an illegal instruction signal is received by
286 * the program. We use this function to test OS and CPU capabilities
287 *****************************************************************************/
288 static void SigHandler( int i_signal )
290 /* Acknowledge the signal received */
294 sigrelse( i_signal );
296 VLC_UNUSED( i_signal );
302 uint32_t cpu_flags = 0;
305 /*****************************************************************************
306 * vlc_CPU: get pre-computed CPU capability flags
307 ****************************************************************************/
308 unsigned vlc_CPU (void)
313 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
314 static vlc_memset_t pf_vlc_memset = memset;
316 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
325 * vlc_memcpy: fast CPU-dependent memcpy
327 void *vlc_memcpy (void *tgt, const void *src, size_t n)
329 return pf_vlc_memcpy (tgt, src, n);
333 * vlc_memset: fast CPU-dependent memset
335 void *vlc_memset (void *tgt, int c, size_t n)
337 return pf_vlc_memset (tgt, c, n);