1 /*****************************************************************************
2 * cpu.c: CPU detection code
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VLC authors and VideoLAN
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 it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
40 #include <sys/types.h>
50 #include <sys/sysctl.h>
53 #include <cpu-features.h>
56 #if defined(__OpenBSD__) && defined(__powerpc__)
57 #include <sys/param.h>
58 #include <sys/sysctl.h>
59 #include <machine/cpu.h>
62 static uint32_t cpu_flags;
64 #if defined (__i386__) || defined (__x86_64__) || defined (__powerpc__) \
65 || defined (__ppc__) || defined (__ppc64__) || defined (__powerpc64__)
66 # if !defined (WIN32) && !defined (__OS2__)
67 static bool vlc_CPU_check (const char *name, void (*func) (void))
74 signal (SIGILL, SIG_DFL);
76 //__asm__ __volatile__ ( code : : input );
81 //i_capabilities |= (flag);
84 while( waitpid( pid, &status, 0 ) == -1 );
86 if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
89 fprintf (stderr, "Warning: your CPU has %s instructions, but not your "
90 "operating system.\n", name);
91 fprintf( stderr, " some optimizations will be disabled unless "
92 "you upgrade your OS\n" );
96 #if defined (CAN_COMPILE_SSE) && !defined (__SSE__)
97 VLC_SSE static void SSE_test (void)
99 asm volatile ("xorps %%xmm0,%%xmm0\n" : : : "xmm0", "xmm1");
102 #if defined (CAN_COMPILE_3DNOW)
103 VLC_MMX static void ThreeD_Now_test (void)
105 asm volatile ("pfadd %%mm0,%%mm0\n" "femms\n" : : : "mm0");
109 #if defined (CAN_COMPILE_ALTIVEC)
110 static void Altivec_test (void)
112 asm volatile ("mtspr 256, %0\n" "vand %%v0, %%v0, %%v0\n" : : "r" (-1));
116 #else /* WIN32 || __OS2__ */
117 # define vlc_CPU_check(name, func) (1)
122 * Determines the CPU capabilities and stores them in cpu_flags.
123 * The result can be retrieved with vlc_CPU().
125 void vlc_CPU_init (void)
127 uint32_t i_capabilities = 0;
129 #if defined( __i386__ ) || defined( __x86_64__ )
130 unsigned int i_eax, i_ebx, i_ecx, i_edx;
133 /* Needed for x86 CPU capabilities detection */
134 # if defined (__i386__) && defined (__PIC__)
135 # define cpuid(reg) \
136 asm volatile ("xchgl %%ebx,%1\n\t" \
138 "xchgl %%ebx,%1\n\t" \
139 : "=a" (i_eax), "=r" (i_ebx), "=c" (i_ecx), "=d" (i_edx) \
143 # define cpuid(reg) \
144 asm volatile ("cpuid\n\t" \
145 : "=a" (i_eax), "=b" (i_ebx), "=c" (i_ecx), "=d" (i_edx) \
149 /* Check if the OS really supports the requested instructions */
150 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
151 && !defined (__i686__) && !defined (__pentium4__) \
152 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
153 /* check if cpuid instruction is supported */
154 asm volatile ( "push %%ebx\n\t"
157 "movl %%eax, %%ebx\n\t"
158 "xorl $0x200000, %%eax\n\t"
174 /* the CPU supports the CPUID instruction - get its level */
177 # if defined (__i386__) && !defined (__i586__) \
178 && !defined (__i686__) && !defined (__pentium4__) \
179 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
184 /* borrowed from mpeg2dec */
185 b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
186 && ( i_edx == 0x69746e65 );
188 /* test for the MMX flag */
190 # if !defined (__MMX__)
191 if( ! (i_edx & 0x00800000) )
194 i_capabilities |= VLC_CPU_MMX;
196 if( i_edx & 0x02000000 )
197 i_capabilities |= VLC_CPU_MMXEXT;
198 # if defined (CAN_COMPILE_SSE) && !defined (__SSE__)
199 if (( i_edx & 0x02000000 ) && vlc_CPU_check ("SSE", SSE_test))
202 /*if( i_edx & 0x02000000 )*/
203 i_capabilities |= VLC_CPU_SSE;
204 if (i_edx & 0x04000000)
205 i_capabilities |= VLC_CPU_SSE2;
206 if (i_ecx & 0x00000001)
207 i_capabilities |= VLC_CPU_SSE3;
208 if (i_ecx & 0x00000200)
209 i_capabilities |= VLC_CPU_SSSE3;
210 if (i_ecx & 0x00080000)
211 i_capabilities |= VLC_CPU_SSE4_1;
212 if (i_ecx & 0x00100000)
213 i_capabilities |= VLC_CPU_SSE4_2;
216 /* test for additional capabilities */
219 if( i_eax < 0x80000001 )
222 /* list these additional capabilities */
225 # if defined (CAN_COMPILE_3DNOW) && !defined (__3dNOW__)
226 if ((i_edx & 0x80000000) && vlc_CPU_check ("3D Now!", ThreeD_Now_test))
228 i_capabilities |= VLC_CPU_3dNOW;
230 if( b_amd && ( i_edx & 0x00400000 ) )
231 i_capabilities |= VLC_CPU_MMXEXT;
234 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
235 || defined( __ppc64__ )
237 # if defined(__APPLE__) || defined(__OpenBSD__)
238 # if defined(__OpenBSD__)
239 int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
241 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
243 int i_has_altivec = 0;
244 size_t i_length = sizeof( i_has_altivec );
245 int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
247 if( i_error == 0 && i_has_altivec != 0 )
248 i_capabilities |= VLC_CPU_ALTIVEC;
250 # elif defined( CAN_COMPILE_ALTIVEC )
251 if (vlc_CPU_check ("Altivec", Altivec_test))
252 i_capabilities |= VLC_CPU_ALTIVEC;
256 #elif defined ( __arm__)
258 if (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON)
259 i_capabilities |= VLC_CPU_ARM_NEON;
264 cpu_flags = i_capabilities;
268 * Retrieves pre-computed CPU capability flags
270 unsigned vlc_CPU (void)
272 /* On Windows and OS/2,
273 * initialized from DllMain() and _DLL_InitTerm() respectively, instead */
274 #if !defined(WIN32) && !defined(__OS2__)
275 static pthread_once_t once = PTHREAD_ONCE_INIT;
276 pthread_once (&once, vlc_CPU_init);
282 void vlc_CPU_dump (vlc_object_t *obj)
284 char buf[200], *p = buf;
286 #if defined (__i386__) || defined (__x86_64__)
287 if (vlc_CPU_MMX()) p += sprintf (p, "MMX ");
288 if (vlc_CPU_MMXEXT()) p += sprintf (p, "MMXEXT ");
289 if (vlc_CPU_SSE()) p += sprintf (p, "SSE ");
290 if (vlc_CPU_SSE2()) p += sprintf (p, "SSE2 ");
291 if (vlc_CPU_SSE3()) p += sprintf (p, "SSE3 ");
292 if (vlc_CPU_SSSE3()) p += sprintf (p, "SSSE3 ");
293 if (vlc_CPU_SSE4_1()) p += sprintf (p, "SSE4.1 ");
294 if (vlc_CPU_SSE4_2()) p += sprintf (p, "SSE4.2 ");
295 if (vlc_CPU_SSE4A()) p += sprintf (p, "SSE4A ");
296 if (vlc_CPU_AVX()) p += sprintf (p, "AVX ");
297 if (vlc_CPU_AVX2()) p += sprintf (p, "AVX ");
298 if (vlc_CPU_3dNOW()) p += sprintf (p, "3DNow! ");
299 if (vlc_CPU_XOP()) p += sprintf (p, "XOP ");
300 if (vlc_CPU_FMA4()) p += sprintf (p, "FMA4 ");
302 #elif defined (__powerpc__) || defined (__ppc__) || defined (__ppc64__)
303 if (vlc_CPU_ALTIVEC()) p += sprintf (p, "AltiVec");
305 #elif defined (__arm__)
306 if (vlc_CPU_ARM_NEON()) p += sprintf (p, "ARM_NEON ");
311 p += sprintf (p, "FPU ");
315 msg_Dbg (obj, "CPU has capabilities %s", buf);