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>
48 #if defined(__APPLE__)
49 #include <sys/sysctl.h>
52 #if defined( __i386__ ) || defined( __x86_64__ ) || defined( __powerpc__ ) \
53 || defined( __ppc__ ) || defined( __ppc64__ ) || defined( __powerpc64__ )
55 static bool check_OS_capability( const char *psz_capability, pid_t pid )
60 return false; /* fail safe :-/ */
62 while( waitpid( pid, &status, 0 ) == -1 );
64 if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
67 fprintf( stderr, "warning: your CPU has %s instructions, but not your "
68 "operating system.\n", psz_capability );
69 fprintf( stderr, " some optimizations will be disabled unless "
70 "you upgrade your OS\n" );
74 # define check_capability(name, flag, code) \
79 signal(SIGILL, SIG_DFL); \
80 __asm__ __volatile__ ( code : : ); \
83 if( check_OS_capability((name), pid )) \
84 i_capabilities |= (flag); \
88 # define check_capability(name, flag, code) \
89 i_capabilities |= (flag);
93 /*****************************************************************************
94 * CPUCapabilities: get the CPU capabilities
95 *****************************************************************************
96 * This function is called to list extensions the CPU may have.
97 *****************************************************************************/
98 uint32_t CPUCapabilities( void )
100 uint32_t i_capabilities = 0;
102 #if defined( __i386__ ) || defined( __x86_64__ )
103 unsigned int i_eax, i_ebx, i_ecx, i_edx;
106 /* Needed for x86 CPU capabilities detection */
107 # if defined( __x86_64__ )
108 # define cpuid( reg ) \
109 asm volatile ( "cpuid\n\t" \
110 "movl %%ebx,%1\n\t" \
118 # define cpuid( reg ) \
119 asm volatile ( "push %%ebx\n\t" \
121 "movl %%ebx,%1\n\t" \
130 /* Check if the OS really supports the requested instructions */
131 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
132 && !defined (__i686__) && !defined (__pentium4__) \
133 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
134 /* check if cpuid instruction is supported */
135 asm volatile ( "push %%ebx\n\t"
138 "movl %%eax, %%ebx\n\t"
139 "xorl $0x200000, %%eax\n\t"
155 /* the CPU supports the CPUID instruction - get its level */
158 # if defined (__i386__) && !defined (__i586__) \
159 && !defined (__i686__) && !defined (__pentium4__) \
160 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
165 /* borrowed from mpeg2dec */
166 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
167 && ( i_edx == 0x69746e65 );
169 /* test for the MMX flag */
171 # if !defined (__MMX__)
172 if( ! (i_edx & 0x00800000) )
175 i_capabilities |= CPU_CAPABILITY_MMX;
177 # if defined (__SSE__)
178 i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
180 if( i_edx & 0x02000000 )
182 i_capabilities |= CPU_CAPABILITY_MMXEXT;
184 # ifdef CAN_COMPILE_SSE
185 check_capability( "SSE", CPU_CAPABILITY_SSE,
186 "xorps %%xmm0,%%xmm0\n" );
191 # if defined (__SSE2__)
192 i_capabilities |= CPU_CAPABILITY_SSE2;
193 # elif defined (CAN_COMPILE_SSE2)
194 if( i_edx & 0x04000000 )
195 check_capability( "SSE2", CPU_CAPABILITY_SSE2,
196 "movupd %%xmm0, %%xmm0\n" );
199 # if defined (__SSE3__)
200 i_capabilities |= CPU_CAPABILITY_SSE3;
201 # elif defined (CAN_COMPILE_SSE3)
202 if( i_ecx & 0x00000001 )
203 check_capability( "SSE3", CPU_CAPABILITY_SSE3,
204 "movsldup %%xmm1, %%xmm0\n" );
207 # if defined (__SSSE3__)
208 i_capabilities |= CPU_CAPABILITY_SSSE3;
209 # elif defined (CAN_COMPILE_SSSE3)
210 if( i_ecx & 0x00000200 )
211 check_capability( "SSSE3", CPU_CAPABILITY_SSSE3,
212 "pabsw %%xmm1, %%xmm0\n" );
215 # if defined (__SSE4_1__)
216 i_capabilities |= CPU_CAPABILITY_SSE4_1;
217 # elif defined (CAN_COMPILE_SSE4_1)
218 if( i_ecx & 0x00080000 )
219 check_capability( "SSE4.1", CPU_CAPABILITY_SSE4_1,
220 "pmaxsb %%xmm1, %%xmm0\n" );
223 # if defined (__SSE4_2__)
224 i_capabilities |= CPU_CAPABILITY_SSE4_2;
225 # elif defined (CAN_COMPILE_SSE4_2)
226 if( i_ecx & 0x00100000 )
227 check_capability( "SSE4.2", CPU_CAPABILITY_SSE4_2,
228 "pcmpgtq %%xmm1, %%xmm0\n" );
231 /* test for additional capabilities */
234 if( i_eax < 0x80000001 )
237 /* list these additional capabilities */
240 # if defined (__3dNOW__)
241 i_capabilities |= CPU_CAPABILITY_3DNOW;
242 # elif defined (CAN_COMPILE_3DNOW)
243 if( i_edx & 0x80000000 )
244 check_capability( "3D Now!", CPU_CAPABILITY_3DNOW,
245 "pfadd %%mm0,%%mm0\n" "femms\n" );
248 if( b_amd && ( i_edx & 0x00400000 ) )
250 i_capabilities |= CPU_CAPABILITY_MMXEXT;
254 #elif defined( __arm__ )
255 # if defined( __ARM_NEON__ )
256 i_capabilities |= CPU_CAPABILITY_NEON;
259 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
260 || defined( __ppc64__ )
262 # if defined(__APPLE__)
263 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
264 int i_has_altivec = 0;
265 size_t i_length = sizeof( i_has_altivec );
266 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
268 if( i_error == 0 && i_has_altivec != 0 )
269 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
271 # elif defined( CAN_COMPILE_ALTIVEC )
275 signal(SIGILL, SIG_DFL);
276 asm volatile ("mtspr 256, %0\n\t"
277 "vand %%v0, %%v0, %%v0"
283 if( check_OS_capability( "Altivec", pid ) )
284 i_capabilities |= CPU_CAPABILITY_ALTIVEC;
289 return i_capabilities;
292 uint32_t cpu_flags = 0;
295 /*****************************************************************************
296 * vlc_CPU: get pre-computed CPU capability flags
297 ****************************************************************************/
298 unsigned vlc_CPU (void)
308 #if defined ( __i386__ ) || defined ( __x86_64__ )
309 { CPU_CAPABILITY_MMX, "mmx" },
310 { CPU_CAPABILITY_MMXEXT, "mmxext" },
311 { CPU_CAPABILITY_3DNOW, "3dnow" },
312 { CPU_CAPABILITY_SSE, "sse" },
314 #if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
315 { CPU_CAPABILITY_ALTIVEC, "altivec" },
317 #if defined (__arm__)
318 { CPU_CAPABILITY_NEON, "arm_neon" },
323 * Return the number of available logical CPU.
325 unsigned vlc_GetCPUCount(void)
327 #if defined(WIN32) && !defined(UNDER_CE)
330 if (!GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask))
334 while (system_mask) {
339 #elif defined(HAVE_SCHED_GETAFFINITY)
342 if (sched_getaffinity(0, sizeof(cpu), &cpu) < 0)
345 for (unsigned i = 0; i < CPU_SETSIZE; i++)
346 count += CPU_ISSET(i, &cpu) != 0;
348 #elif defined(__APPLE__)
350 size_t size = sizeof(count) ;
351 if (sysctlbyname("hw.ncpu", &count, &size, NULL, 0))
352 return 1; /* Failure */
355 # warning "vlc_GetCPUCount is not implemented for your platform"
361 * Check if a directory name contains usable plugins w.r.t. the hardware
362 * capabilities. Loading a plugin when the hardware has insufficient
363 * capabilities may lead to illegal instructions (SIGILL) and must be avoided.
365 * @param name the name of the directory (<b>not</b> the path)
367 * @return true if the hardware has sufficient capabilities or the directory
368 * does not require any special capability; false if the running hardware has
369 * insufficient capabilities.
371 bool vlc_CPU_CheckPluginDir (const char *name)
373 const unsigned flags = vlc_CPU ();
374 for (size_t i = 0; i < sizeof (cap_dirs) / sizeof (cap_dirs[0]); i++)
376 if (strcmp (name, cap_dirs[i].name))
378 return (flags & cap_dirs[i].value) != 0;
383 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
384 static vlc_memset_t pf_vlc_memset = memset;
386 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
395 * vlc_memcpy: fast CPU-dependent memcpy
397 void *vlc_memcpy (void *tgt, const void *src, size_t n)
399 return pf_vlc_memcpy (tgt, src, n);
403 * vlc_memset: fast CPU-dependent memset
405 void *vlc_memset (void *tgt, int c, size_t n)
407 return pf_vlc_memset (tgt, c, n);
411 * Returned an aligned pointer on newly allocated memory.
412 * \param alignment must be a power of 2 and a multiple of sizeof(void*)
413 * \param size is the size of the usable memory returned.
415 * It must not be freed directly, *base must.
417 void *vlc_memalign(void **base, size_t alignment, size_t size)
419 assert(alignment >= sizeof(void*));
420 for (size_t t = alignment; t > 1; t >>= 1)
422 #if defined(HAVE_POSIX_MEMALIGN)
423 if (posix_memalign(base, alignment, size)) {
428 #elif defined(HAVE_MEMALIGN)
429 return *base = memalign(alignment, size);
431 unsigned char *p = *base = malloc(size + alignment - 1);
434 return (void*)((uintptr_t)(p + alignment - 1) & ~(alignment - 1));