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) \
88 i_capabilities |= (flag);
92 /*****************************************************************************
93 * CPUCapabilities: get the CPU capabilities
94 *****************************************************************************
95 * This function is called to list extensions the CPU may have.
96 *****************************************************************************/
97 uint32_t CPUCapabilities( void )
99 uint32_t i_capabilities = 0;
101 #if defined( __i386__ ) || defined( __x86_64__ )
102 unsigned int i_eax, i_ebx, i_ecx, i_edx;
105 /* Needed for x86 CPU capabilities detection */
106 # if defined( __x86_64__ )
107 # define cpuid( reg ) \
108 asm volatile ( "cpuid\n\t" \
109 "movl %%ebx,%1\n\t" \
117 # define cpuid( reg ) \
118 asm volatile ( "push %%ebx\n\t" \
120 "movl %%ebx,%1\n\t" \
129 /* Check if the OS really supports the requested instructions */
130 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
131 && !defined (__i686__) && !defined (__pentium4__) \
132 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
133 /* check if cpuid instruction is supported */
134 asm volatile ( "push %%ebx\n\t"
137 "movl %%eax, %%ebx\n\t"
138 "xorl $0x200000, %%eax\n\t"
154 /* the CPU supports the CPUID instruction - get its level */
157 # if defined (__i386__) && !defined (__i586__) \
158 && !defined (__i686__) && !defined (__pentium4__) \
159 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
164 /* borrowed from mpeg2dec */
165 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
166 && ( i_edx == 0x69746e65 );
168 /* test for the MMX flag */
170 # if !defined (__MMX__)
171 if( ! (i_edx & 0x00800000) )
174 i_capabilities |= CPU_CAPABILITY_MMX;
176 # if defined (__SSE__)
177 i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
179 if( i_edx & 0x02000000 )
181 i_capabilities |= CPU_CAPABILITY_MMXEXT;
183 # ifdef CAN_COMPILE_SSE
184 check_capability( "SSE", CPU_CAPABILITY_SSE,
185 "xorps %%xmm0,%%xmm0\n" );
190 # if defined (__SSE2__)
191 i_capabilities |= CPU_CAPABILITY_SSE2;
192 # elif defined (CAN_COMPILE_SSE2)
193 if( i_edx & 0x04000000 )
194 check_capability( "SSE2", CPU_CAPABILITY_SSE2,
195 "movupd %%xmm0, %%xmm0\n" );
198 # if defined (__SSE3__)
199 i_capabilities |= CPU_CAPABILITY_SSE3;
200 # elif defined (CAN_COMPILE_SSE3)
201 if( i_ecx & 0x00000001 )
202 check_capability( "SSE3", CPU_CAPABILITY_SSE3,
203 "movsldup %%xmm1, %%xmm0\n" );
206 # if defined (__SSSE3__)
207 i_capabilities |= CPU_CAPABILITY_SSSE3;
208 # elif defined (CAN_COMPILE_SSSE3)
209 if( i_ecx & 0x00000200 )
210 check_capability( "SSSE3", CPU_CAPABILITY_SSSE3,
211 "pabsw %%xmm1, %%xmm0\n" );
214 # if defined (__SSE4_1__)
215 i_capabilities |= CPU_CAPABILITY_SSE4_1;
216 # elif defined (CAN_COMPILE_SSE4_1)
217 if( i_ecx & 0x00080000 )
218 check_capability( "SSE4.1", CPU_CAPABILITY_SSE4_1,
219 "pmaxsb %%xmm1, %%xmm0\n" );
222 # if defined (__SSE4_2__)
223 i_capabilities |= CPU_CAPABILITY_SSE4_2;
224 # elif defined (CAN_COMPILE_SSE4_2)
225 if( i_ecx & 0x00100000 )
226 check_capability( "SSE4.2", CPU_CAPABILITY_SSE4_2,
227 "pcmpgtq %%xmm1, %%xmm0\n" );
230 /* test for additional capabilities */
233 if( i_eax < 0x80000001 )
236 /* list these additional capabilities */
239 # if defined (__3dNOW__)
240 i_capabilities |= CPU_CAPABILITY_3DNOW;
241 # elif defined (CAN_COMPILE_3DNOW)
242 if( i_edx & 0x80000000 )
243 check_capability( "3D Now!", CPU_CAPABILITY_3DNOW,
244 "pfadd %%mm0,%%mm0\n" "femms\n" );
247 if( b_amd && ( i_edx & 0x00400000 ) )
249 i_capabilities |= CPU_CAPABILITY_MMXEXT;
253 #elif defined( __arm__ )
254 # if defined( __ARM_NEON__ )
255 i_capabilities |= CPU_CAPABILITY_NEON;
258 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
259 || defined( __ppc64__ )
261 # if defined(__APPLE__)
262 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
263 int i_has_altivec = 0;
264 size_t i_length = sizeof( i_has_altivec );
265 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
267 if( i_error == 0 && i_has_altivec != 0 )
268 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
270 # elif defined( CAN_COMPILE_ALTIVEC )
274 signal(SIGILL, SIG_DFL);
275 asm volatile ("mtspr 256, %0\n\t"
276 "vand %%v0, %%v0, %%v0"
282 if( check_OS_capability( "Altivec", pid ) )
283 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
288 return i_capabilities;
291 uint32_t cpu_flags = 0;
294 /*****************************************************************************
295 * vlc_CPU: get pre-computed CPU capability flags
296 ****************************************************************************/
297 unsigned vlc_CPU (void)
307 #if defined ( __i386__ ) || defined ( __x86_64__ )
308 { CPU_CAPABILITY_MMX, "mmx" },
309 { CPU_CAPABILITY_MMXEXT, "mmxext" },
310 { CPU_CAPABILITY_3DNOW, "3dnow" },
311 { CPU_CAPABILITY_SSE, "sse" },
313 #if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
314 { CPU_CAPABILITY_ALTIVEC, "altivec" },
316 #if defined (__arm__)
317 { CPU_CAPABILITY_NEON, "arm_neon" },
322 * Return the number of available logical CPU.
324 unsigned vlc_GetCPUCount(void)
326 #if defined(WIN32) && !defined(UNDER_CE)
329 if (!GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask))
333 while (system_mask) {
338 #elif HAVE_SCHED_GETAFFINITY
341 if (sched_getaffinity(0, sizeof(cpu), &cpu) < 0)
344 for (unsigned i = 0; i < CPU_SETSIZE; i++)
345 count += CPU_ISSET(i, &cpu) != 0;
348 # warning "vlc_GetCPUCount is not implemented for your platform"
354 * Check if a directory name contains usable plugins w.r.t. the hardware
355 * capabilities. Loading a plugin when the hardware has insufficient
356 * capabilities may lead to illegal instructions (SIGILL) and must be avoided.
358 * @param name the name of the directory (<b>not</b> the path)
360 * @return true if the hardware has sufficient capabilities or the directory
361 * does not require any special capability; false if the running hardware has
362 * insufficient capabilities.
364 bool vlc_CPU_CheckPluginDir (const char *name)
366 const unsigned flags = vlc_CPU ();
367 for (size_t i = 0; i < sizeof (cap_dirs) / sizeof (cap_dirs[0]); i++)
369 if (strcmp (name, cap_dirs[i].name))
371 return (flags & cap_dirs[i].value) != 0;
376 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
377 static vlc_memset_t pf_vlc_memset = memset;
379 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
388 * vlc_memcpy: fast CPU-dependent memcpy
390 void *vlc_memcpy (void *tgt, const void *src, size_t n)
392 return pf_vlc_memcpy (tgt, src, n);
396 * vlc_memset: fast CPU-dependent memset
398 void *vlc_memset (void *tgt, int c, size_t n)
400 return pf_vlc_memset (tgt, c, n);