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>
35 #include <vlc_memstream.h>
40 #include <sys/types.h>
50 #include <sys/sysctl.h>
53 #if defined(__OpenBSD__) && defined(__powerpc__)
54 #include <sys/param.h>
55 #include <sys/sysctl.h>
56 #include <machine/cpu.h>
59 static uint32_t cpu_flags
;
61 #if defined (__i386__) || defined (__x86_64__) || defined (__powerpc__) \
62 || defined (__ppc__) || defined (__ppc64__) || defined (__powerpc64__)
63 # if defined (HAVE_FORK)
64 static bool vlc_CPU_check (const char *name
, void (*func
) (void))
71 signal (SIGILL
, SIG_DFL
);
79 while( waitpid( pid
, &status
, 0 ) == -1 );
81 if( WIFEXITED( status
) && WEXITSTATUS( status
) == 0 )
84 fprintf (stderr
, "Warning: your CPU has %s instructions, but not your "
85 "operating system.\n", name
);
86 fprintf( stderr
, " some optimizations will be disabled unless "
87 "you upgrade your OS\n" );
91 #if defined (CAN_COMPILE_SSE) && !defined (__SSE__)
92 VLC_SSE
static void SSE_test (void)
94 asm volatile ("xorps %%xmm0,%%xmm0\n" : : : "xmm0", "xmm1");
97 #if defined (CAN_COMPILE_3DNOW)
98 VLC_MMX
static void ThreeD_Now_test (void)
100 asm volatile ("pfadd %%mm0,%%mm0\n" "femms\n" : : : "mm0");
104 #if defined (CAN_COMPILE_ALTIVEC)
105 static void Altivec_test (void)
107 asm volatile ("mtspr 256, %0\n" "vand %%v0, %%v0, %%v0\n" : : "r" (-1));
111 #else /* _WIN32 || __OS2__ */
112 # define vlc_CPU_check(name, func) (1)
117 * Determines the CPU capabilities and stores them in cpu_flags.
118 * The result can be retrieved with vlc_CPU().
120 static void vlc_CPU_init(void)
122 uint32_t i_capabilities
= 0;
124 #if defined( __i386__ ) || defined( __x86_64__ )
125 unsigned int i_eax
, i_ebx
, i_ecx
, i_edx
;
128 /* Needed for x86 CPU capabilities detection */
129 # if defined (__i386__) && defined (__PIC__)
130 # define cpuid(reg) \
131 asm volatile ("xchgl %%ebx,%1\n\t" \
133 "xchgl %%ebx,%1\n\t" \
134 : "=a" (i_eax), "=r" (i_ebx), "=c" (i_ecx), "=d" (i_edx) \
138 # define cpuid(reg) \
139 asm volatile ("cpuid\n\t" \
140 : "=a" (i_eax), "=b" (i_ebx), "=c" (i_ecx), "=d" (i_edx) \
144 /* Check if the OS really supports the requested instructions */
145 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
146 && !defined (__i686__) && !defined (__pentium4__) \
147 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
148 /* check if cpuid instruction is supported */
149 asm volatile ( "push %%ebx\n\t"
152 "movl %%eax, %%ebx\n\t"
153 "xorl $0x200000, %%eax\n\t"
169 /* the CPU supports the CPUID instruction - get its level */
172 # if defined (__i386__) && !defined (__i586__) \
173 && !defined (__i686__) && !defined (__pentium4__) \
174 && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
179 /* borrowed from mpeg2dec */
180 b_amd
= ( i_ebx
== 0x68747541 ) && ( i_ecx
== 0x444d4163 )
181 && ( i_edx
== 0x69746e65 );
183 /* test for the MMX flag */
185 # if !defined (__MMX__)
186 if( ! (i_edx
& 0x00800000) )
189 i_capabilities
|= VLC_CPU_MMX
;
191 if( i_edx
& 0x02000000 )
192 i_capabilities
|= VLC_CPU_MMXEXT
;
193 # if defined (CAN_COMPILE_SSE) && !defined (__SSE__)
194 if (( i_edx
& 0x02000000 ) && vlc_CPU_check ("SSE", SSE_test
))
197 /*if( i_edx & 0x02000000 )*/
198 i_capabilities
|= VLC_CPU_SSE
;
199 if (i_edx
& 0x04000000)
200 i_capabilities
|= VLC_CPU_SSE2
;
201 if (i_ecx
& 0x00000001)
202 i_capabilities
|= VLC_CPU_SSE3
;
203 if (i_ecx
& 0x00000200)
204 i_capabilities
|= VLC_CPU_SSSE3
;
205 if (i_ecx
& 0x00080000)
206 i_capabilities
|= VLC_CPU_SSE4_1
;
207 if (i_ecx
& 0x00100000)
208 i_capabilities
|= VLC_CPU_SSE4_2
;
211 /* test for additional capabilities */
214 if( i_eax
< 0x80000001 )
217 /* list these additional capabilities */
220 # if defined (CAN_COMPILE_3DNOW) && !defined (__3dNOW__)
221 if ((i_edx
& 0x80000000) && vlc_CPU_check ("3D Now!", ThreeD_Now_test
))
223 i_capabilities
|= VLC_CPU_3dNOW
;
225 if( b_amd
&& ( i_edx
& 0x00400000 ) )
226 i_capabilities
|= VLC_CPU_MMXEXT
;
229 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
230 || defined( __ppc64__ )
232 # if defined(__APPLE__) || defined(__OpenBSD__)
233 # if defined(__OpenBSD__)
234 int selectors
[2] = { CTL_MACHDEP
, CPU_ALTIVEC
};
236 int selectors
[2] = { CTL_HW
, HW_VECTORUNIT
};
238 int i_has_altivec
= 0;
239 size_t i_length
= sizeof( i_has_altivec
);
240 int i_error
= sysctl( selectors
, 2, &i_has_altivec
, &i_length
, NULL
, 0);
242 if( i_error
== 0 && i_has_altivec
!= 0 )
243 i_capabilities
|= VLC_CPU_ALTIVEC
;
245 # elif defined( CAN_COMPILE_ALTIVEC )
246 if (vlc_CPU_check ("Altivec", Altivec_test
))
247 i_capabilities
|= VLC_CPU_ALTIVEC
;
253 cpu_flags
= i_capabilities
;
257 * Retrieves pre-computed CPU capability flags
259 VLC_WEAK
unsigned vlc_CPU(void)
261 static vlc_once_t once
= VLC_STATIC_ONCE
;
262 vlc_once(&once
, vlc_CPU_init
);
266 void vlc_CPU_dump (vlc_object_t
*obj
)
268 struct vlc_memstream stream
;
270 vlc_memstream_open(&stream
);
272 #if defined (__i386__) || defined (__x86_64__)
274 vlc_memstream_puts(&stream
, "MMX ");
275 if (vlc_CPU_MMXEXT())
276 vlc_memstream_puts(&stream
, "MMXEXT ");
278 vlc_memstream_puts(&stream
, "SSE ");
280 vlc_memstream_puts(&stream
, "SSE2 ");
282 vlc_memstream_puts(&stream
, "SSE3 ");
284 vlc_memstream_puts(&stream
, "SSSE3 ");
285 if (vlc_CPU_SSE4_1())
286 vlc_memstream_puts(&stream
, "SSE4.1 ");
287 if (vlc_CPU_SSE4_2())
288 vlc_memstream_puts(&stream
, "SSE4.2 ");
290 vlc_memstream_puts(&stream
, "SSE4A ");
292 vlc_memstream_puts(&stream
, "AVX ");
294 vlc_memstream_puts(&stream
, "AVX2 ");
296 vlc_memstream_puts(&stream
, "3DNow! ");
298 vlc_memstream_puts(&stream
, "XOP ");
300 vlc_memstream_puts(&stream
, "FMA4 ");
302 #elif defined (__powerpc__) || defined (__ppc__) || defined (__ppc64__)
303 if (vlc_CPU_ALTIVEC())
304 vlc_memstream_puts(&stream
, "AltiVec");
306 #elif defined (__arm__)
307 if (vlc_CPU_ARM_NEON())
308 vlc_memstream_puts(&stream
, "ARM_NEON ");
313 vlc_memstream_puts(&stream
, "FPU ");
316 if (vlc_memstream_close(&stream
) == 0)
318 msg_Dbg (obj
, "CPU has capabilities %s", stream
.ptr
);