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>
36 #include <sys/types.h>
44 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
45 #include <sys/sysctl.h>
48 #if defined( __i386__ ) || defined( __x86_64__ ) || defined( __powerpc__ ) \
49 || defined( __ppc__ ) || defined( __ppc64__ ) || defined( __powerpc64__ )
50 static bool check_OS_capability( const char *psz_capability, pid_t pid )
56 return false; /* fail safe :-/ */
58 while( waitpid( pid, &status, 0 ) == -1 );
60 if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
63 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
64 "operating system.\n", psz_capability );
65 fprintf( stderr, " some optimizations will be disabled unless "
66 "you upgrade your OS\n" );
70 # define fork() (errno = ENOSYS, -1)
78 /*****************************************************************************
79 * CPUCapabilities: get the CPU capabilities
80 *****************************************************************************
81 * This function is called to list extensions the CPU may have.
82 *****************************************************************************/
83 uint32_t CPUCapabilities( void )
85 uint32_t i_capabilities = 0;
87 #if defined( __i386__ ) || defined( __x86_64__ )
88 unsigned int i_eax, i_ebx, i_ecx, i_edx;
91 /* Needed for x86 CPU capabilities detection */
92 # if defined( __x86_64__ )
93 # define cpuid( reg ) \
94 asm volatile ( "cpuid\n\t" \
103 # define cpuid( reg ) \
104 asm volatile ( "push %%ebx\n\t" \
106 "movl %%ebx,%1\n\t" \
116 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
117 && !defined (__i686__) && !defined (__pentium4__) \
118 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
119 /* check if cpuid instruction is supported */
120 asm volatile ( "push %%ebx\n\t"
123 "movl %%eax, %%ebx\n\t"
124 "xorl $0x200000, %%eax\n\t"
140 /* the CPU supports the CPUID instruction - get its level */
143 # if defined (__i386__) && !defined (__i586__) \
144 && !defined (__i686__) && !defined (__pentium4__) \
145 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
150 /* borrowed from mpeg2dec */
151 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
152 && ( i_edx == 0x69746e65 );
154 /* test for the MMX flag */
156 # if !defined (__MMX__)
157 if( ! (i_edx & 0x00800000) )
160 i_capabilities |= CPU_CAPABILITY_MMX;
162 # if defined (__SSE__)
163 i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
165 if( i_edx & 0x02000000 )
167 i_capabilities |= CPU_CAPABILITY_MMXEXT;
169 # ifdef CAN_COMPILE_SSE
170 /* We test if OS supports the SSE instructions */
174 /* Test a SSE instruction */
175 __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
178 if( check_OS_capability( "SSE", pid ) )
179 i_capabilities |= CPU_CAPABILITY_SSE;
184 # if defined (__SSE2__)
185 i_capabilities |= CPU_CAPABILITY_SSE2;
186 # elif defined (CAN_COMPILE_SSE)
187 if( i_edx & 0x04000000 )
189 /* We test if OS supports the SSE2 instructions */
193 /* Test a SSE2 instruction */
194 __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
197 if( check_OS_capability( "SSE2", pid ) )
198 i_capabilities |= CPU_CAPABILITY_SSE2;
202 # if defined (__SSE3__)
203 i_capabilities |= CPU_CAPABILITY_SSE3;
204 # elif defined (CAN_COMPILE_SSE3)
205 if( i_ecx & 0x00000001 )
207 /* We test if OS supports the SSE3 instructions */
211 /* Test a SSE3 instruction */
212 __asm__ __volatile__ ( "movsldup %%xmm1, %%xmm0\n" : : );
215 if( check_OS_capability( "SSE3", pid ) )
216 i_capabilities |= CPU_CAPABILITY_SSE3;
220 /* test for additional capabilities */
223 if( i_eax < 0x80000001 )
226 /* list these additional capabilities */
229 # if defined (__3dNOW__)
230 i_capabilities |= CPU_CAPABILITY_3DNOW;
231 # elif defined (CAN_COMPILE_3DNOW)
232 if( i_edx & 0x80000000 )
237 /* Test a 3D Now! instruction */
238 __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
241 if( check_OS_capability( "3D Now!", pid ) )
242 i_capabilities |= CPU_CAPABILITY_3DNOW;
246 if( b_amd && ( i_edx & 0x00400000 ) )
248 i_capabilities |= CPU_CAPABILITY_MMXEXT;
252 #elif defined( __arm__ )
253 # if defined( __ARM_NEON__ )
254 i_capabilities |= CPU_CAPABILITY_NEON;
257 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
258 || defined( __ppc64__ )
260 # if defined(__APPLE__)
261 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
262 int i_has_altivec = 0;
263 size_t i_length = sizeof( i_has_altivec );
264 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
266 if( i_error == 0 && i_has_altivec != 0 )
267 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
269 # elif defined( CAN_COMPILE_ALTIVEC )
273 asm volatile ("mtspr 256, %0\n\t"
274 "vand %%v0, %%v0, %%v0"
280 if( check_OS_capability( "Altivec", pid ) )
281 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
286 return i_capabilities;
289 uint32_t cpu_flags = 0;
292 /*****************************************************************************
293 * vlc_CPU: get pre-computed CPU capability flags
294 ****************************************************************************/
295 unsigned vlc_CPU (void)
300 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
301 static vlc_memset_t pf_vlc_memset = memset;
303 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
312 * vlc_memcpy: fast CPU-dependent memcpy
314 void *vlc_memcpy (void *tgt, const void *src, size_t n)
316 return pf_vlc_memcpy (tgt, src, n);
320 * vlc_memset: fast CPU-dependent memset
322 void *vlc_memset (void *tgt, int c, size_t n)
324 return pf_vlc_memset (tgt, c, n);