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 <sys/types.h>
43 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
44 #include <sys/sysctl.h>
47 #if defined( __i386__ ) || defined( __x86_64__ ) \
48 || defined( __ppc__ ) || defined( __ppc64__ )
49 static bool check_OS_capability( const char *psz_capability, pid_t pid )
55 return false; /* fail safe :-/ */
57 while( waitpid( pid, &status, 0 ) == -1 );
59 if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
62 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
63 "operating system.\n", psz_capability );
64 fprintf( stderr, " some optimizations will be disabled unless "
65 "you upgrade your OS\n" );
69 # define fork() (errno = ENOSYS, -1)
77 /*****************************************************************************
78 * CPUCapabilities: get the CPU capabilities
79 *****************************************************************************
80 * This function is called to list extensions the CPU may have.
81 *****************************************************************************/
82 uint32_t CPUCapabilities( void )
84 uint32_t i_capabilities = CPU_CAPABILITY_NONE;
86 #if defined( __i386__ ) || defined( __x86_64__ )
87 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 i_capabilities |= CPU_CAPABILITY_FPU;
117 # if defined( __i386__ )
118 /* check if cpuid instruction is supported */
119 asm volatile ( "push %%ebx\n\t"
122 "movl %%eax, %%ebx\n\t"
123 "xorl $0x200000, %%eax\n\t"
138 /* x86_64 supports cpuid instruction, so we dont need to check it */
141 i_capabilities |= CPU_CAPABILITY_486;
143 /* the CPU supports the CPUID instruction - get its level */
149 /* FIXME: this isn't correct, since some 486s have cpuid */
150 i_capabilities |= CPU_CAPABILITY_586;
152 /* borrowed from mpeg2dec */
153 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
154 && ( i_edx == 0x69746e65 );
156 /* test for the MMX flag */
159 if( ! (i_edx & 0x00800000) )
162 i_capabilities |= CPU_CAPABILITY_MMX;
164 if( i_edx & 0x02000000 )
166 i_capabilities |= CPU_CAPABILITY_MMXEXT;
168 # ifdef CAN_COMPILE_SSE
169 /* We test if OS supports the SSE instructions */
173 /* Test a SSE instruction */
174 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
177 if( check_OS_capability( "SSE", pid ) )
178 i_capabilities |= CPU_CAPABILITY_SSE;
182 if( i_edx & 0x04000000 )
184 # if defined(CAN_COMPILE_SSE)
185 /* We test if OS supports the SSE2 instructions */
189 /* Test a SSE2 instruction */
190 __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
193 if( check_OS_capability( "SSE2", pid ) )
194 i_capabilities |= CPU_CAPABILITY_SSE2;
198 /* test for additional capabilities */
201 if( i_eax < 0x80000001 )
204 /* list these additional capabilities */
207 # ifdef CAN_COMPILE_3DNOW
208 if( i_edx & 0x80000000 )
213 /* Test a 3D Now! instruction */
214 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
217 if( check_OS_capability( "3D Now!", pid ) )
218 i_capabilities |= CPU_CAPABILITY_3DNOW;
222 if( b_amd && ( i_edx & 0x00400000 ) )
224 i_capabilities |= CPU_CAPABILITY_MMXEXT;
228 #elif defined( __arm__ )
229 # if defined( __ARM_EABI__ ) && !defined( __SOFTFP__ )
230 i_capabilities |= CPU_CAPABILITY_FPU;
233 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
235 i_capabilities |= CPU_CAPABILITY_FPU;
237 # if defined(__APPLE__)
238 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
239 int i_has_altivec = 0;
240 size_t i_length = sizeof( i_has_altivec );
241 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
243 if( i_error == 0 && i_has_altivec != 0 )
244 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
246 # elif defined( CAN_COMPILE_ALTIVEC )
250 asm volatile ("mtspr 256, %0\n\t"
251 "vand %%v0, %%v0, %%v0"
257 if( check_OS_capability( "Altivec", pid ) )
258 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
262 #elif defined( __sparc__ )
263 i_capabilities |= CPU_CAPABILITY_FPU;
265 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
266 i_capabilities |= CPU_CAPABILITY_FPU;
269 return i_capabilities;
272 uint32_t cpu_flags = 0;
275 /*****************************************************************************
276 * vlc_CPU: get pre-computed CPU capability flags
277 ****************************************************************************/
278 unsigned vlc_CPU (void)
283 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
284 static vlc_memset_t pf_vlc_memset = memset;
286 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
295 * vlc_memcpy: fast CPU-dependent memcpy
297 void *vlc_memcpy (void *tgt, const void *src, size_t n)
299 return pf_vlc_memcpy (tgt, src, n);
303 * vlc_memset: fast CPU-dependent memset
305 void *vlc_memset (void *tgt, int c, size_t n)
307 return pf_vlc_memset (tgt, c, n);