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>
47 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
48 #include <sys/sysctl.h>
51 #if defined( __i386__ ) || defined( __x86_64__ ) || defined( __powerpc__ ) \
52 || defined( __ppc__ ) || defined( __ppc64__ ) || defined( __powerpc64__ )
54 static bool check_OS_capability( const char *psz_capability, pid_t pid )
59 return false; /* fail safe :-/ */
61 while( waitpid( pid, &status, 0 ) == -1 );
63 if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
66 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
67 "operating system.\n", psz_capability );
68 fprintf( stderr, " some optimizations will be disabled unless "
69 "you upgrade your OS\n" );
73 # define check_capability(name, flag, code) \
78 signal(SIGILL, SIG_DFL); \
79 __asm__ __volatile__ ( code : : ); \
82 if( check_OS_capability((name), pid )) \
83 i_capabilities |= (flag); \
87 # define check_capability(name, flag, code) \
89 i_capabilities |= (flag); \
94 /*****************************************************************************
95 * CPUCapabilities: get the CPU capabilities
96 *****************************************************************************
97 * This function is called to list extensions the CPU may have.
98 *****************************************************************************/
99 uint32_t CPUCapabilities( void )
101 uint32_t i_capabilities = 0;
103 #if defined( __i386__ ) || defined( __x86_64__ )
104 unsigned int i_eax, i_ebx, i_ecx, i_edx;
107 /* Needed for x86 CPU capabilities detection */
108 # if defined( __x86_64__ )
109 # define cpuid( reg ) \
110 asm volatile ( "cpuid\n\t" \
111 "movl %%ebx,%1\n\t" \
119 # define cpuid( reg ) \
120 asm volatile ( "push %%ebx\n\t" \
122 "movl %%ebx,%1\n\t" \
131 /* Check if the OS really supports the requested instructions */
132 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
133 && !defined (__i686__) && !defined (__pentium4__) \
134 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
135 /* check if cpuid instruction is supported */
136 asm volatile ( "push %%ebx\n\t"
139 "movl %%eax, %%ebx\n\t"
140 "xorl $0x200000, %%eax\n\t"
156 /* the CPU supports the CPUID instruction - get its level */
159 # if defined (__i386__) && !defined (__i586__) \
160 && !defined (__i686__) && !defined (__pentium4__) \
161 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
166 /* borrowed from mpeg2dec */
167 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
168 && ( i_edx == 0x69746e65 );
170 /* test for the MMX flag */
172 # if !defined (__MMX__)
173 if( ! (i_edx & 0x00800000) )
176 i_capabilities |= CPU_CAPABILITY_MMX;
178 # if defined (__SSE__)
179 i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
181 if( i_edx & 0x02000000 )
183 i_capabilities |= CPU_CAPABILITY_MMXEXT;
185 # ifdef CAN_COMPILE_SSE
186 check_capability( "SSE", CPU_CAPABILITY_SSE,
187 "xorps %%xmm0,%%xmm0\n" );
192 # if defined (__SSE2__)
193 i_capabilities |= CPU_CAPABILITY_SSE2;
194 # elif defined (CAN_COMPILE_SSE2)
195 if( i_edx & 0x04000000 )
196 check_capability( "SSE2", CPU_CAPABILITY_SSE2,
197 "movupd %%xmm0, %%xmm0\n" );
200 # if defined (__SSE3__)
201 i_capabilities |= CPU_CAPABILITY_SSE3;
202 # elif defined (CAN_COMPILE_SSE3)
203 if( i_ecx & 0x00000001 )
204 check_capability( "SSE3", CPU_CAPABILITY_SSE3,
205 "movsldup %%xmm1, %%xmm0\n" );
208 # if defined (__SSSE3__)
209 i_capabilities |= CPU_CAPABILITY_SSSE3;
210 # elif defined (CAN_COMPILE_SSSE3)
211 if( i_ecx & 0x00000200 )
212 check_capability( "SSSE3", CPU_CAPABILITY_SSSE3,
213 "pabsw %%xmm1, %%xmm0\n" );
216 # if defined (__SSE4_1__)
217 i_capabilities |= CPU_CAPABILITY_SSE4_1;
218 # elif defined (CAN_COMPILE_SSE4_1)
219 if( i_ecx & 0x00080000 )
220 check_capability( "SSE4.1", CPU_CAPABILITY_SSE4_1,
221 "pmaxsb %%xmm1, %%xmm0\n" );
224 # if defined (__SSE4_2__)
225 i_capabilities |= CPU_CAPABILITY_SSE4_2;
226 # elif defined (CAN_COMPILE_SSE4_2)
227 if( i_ecx & 0x00100000 )
228 check_capability( "SSE4.2", CPU_CAPABILITY_SSE4_2,
229 "pcmpgtq %%xmm1, %%xmm0\n" );
232 /* test for additional capabilities */
235 if( i_eax < 0x80000001 )
238 /* list these additional capabilities */
241 # if defined (__3dNOW__)
242 i_capabilities |= CPU_CAPABILITY_3DNOW;
243 # elif defined (CAN_COMPILE_3DNOW)
244 if( i_edx & 0x80000000 )
245 check_capability( "3D Now!", CPU_CAPABILITY_3DNOW,
246 "pfadd %%mm0,%%mm0\n" "femms\n" );
249 if( b_amd && ( i_edx & 0x00400000 ) )
251 i_capabilities |= CPU_CAPABILITY_MMXEXT;
255 #elif defined( __arm__ )
256 # if defined( __ARM_NEON__ )
257 i_capabilities |= CPU_CAPABILITY_NEON;
260 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
261 || defined( __ppc64__ )
263 # if defined(__APPLE__)
264 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
265 int i_has_altivec = 0;
266 size_t i_length = sizeof( i_has_altivec );
267 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
269 if( i_error == 0 && i_has_altivec != 0 )
270 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
272 # elif defined( CAN_COMPILE_ALTIVEC )
276 signal(SIGILL, SIG_DFL);
277 asm volatile ("mtspr 256, %0\n\t"
278 "vand %%v0, %%v0, %%v0"
284 if( check_OS_capability( "Altivec", pid ) )
285 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
290 return i_capabilities;
293 uint32_t cpu_flags = 0;
296 /*****************************************************************************
297 * vlc_CPU: get pre-computed CPU capability flags
298 ****************************************************************************/
299 unsigned vlc_CPU (void)
309 #if defined ( __i386__ ) || defined ( __x86_64__ )
310 { CPU_CAPABILITY_MMX, "mmx" },
311 { CPU_CAPABILITY_MMXEXT, "mmxext" },
312 { CPU_CAPABILITY_3DNOW, "3dnow" },
313 { CPU_CAPABILITY_SSE, "sse" },
315 #if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
316 { CPU_CAPABILITY_ALTIVEC, "altivec" },
318 #if defined (__arm__)
319 { CPU_CAPABILITY_NEON, "arm_neon" },
324 * Check if a directory name contains usable plugins w.r.t. the hardware
325 * capabilities. Loading a plugin when the hardware has insufficient
326 * capabilities may lead to illegal instructions (SIGILL) and must be avoided.
328 * @param name the name of the directory (<b>not</b> the path)
330 * @return true if the hardware has sufficient capabilities or the directory
331 * does not require any special capability; false if the running hardware has
332 * insufficient capabilities.
334 bool vlc_CPU_CheckPluginDir (const char *name)
336 const unsigned flags = vlc_CPU ();
337 for (size_t i = 0; i < sizeof (cap_dirs) / sizeof (cap_dirs[0]); i++)
339 if (strcmp (name, cap_dirs[i].name))
341 return (flags & cap_dirs[i].value) != 0;
346 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
347 static vlc_memset_t pf_vlc_memset = memset;
349 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
358 * vlc_memcpy: fast CPU-dependent memcpy
360 void *vlc_memcpy (void *tgt, const void *src, size_t n)
362 return pf_vlc_memcpy (tgt, src, n);
366 * vlc_memset: fast CPU-dependent memset
368 void *vlc_memset (void *tgt, int c, size_t n)
370 return pf_vlc_memset (tgt, c, n);