1 /*****************************************************************************
2 * linux_cpu.c: CPU detection code for Linux
3 *****************************************************************************
4 * Copyright (C) 2012 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
27 #include <vlc_common.h>
32 # define CPU_FLAGS "Features\t:"
34 #elif defined (__i386__) || defined (__x86_64__)
35 # define CPU_FLAGS "flags\t\t:"
37 #elif defined (__powerpc__) || defined (__powerpc64__)
38 # define CPU_FLAGS "cpu\t\t:"
43 static uint32_t cpu_flags = 0;
45 static void vlc_CPU_init (void)
47 FILE *info = fopen ("/proc/cpuinfo", "rt");
53 uint_fast32_t all_caps = 0xFFFFFFFF;
55 while (getline (&line, &linelen, info) != -1)
57 if (strncmp (line, CPU_FLAGS, strlen (CPU_FLAGS)))
61 uint_fast32_t core_caps = 0;
63 while ((cap = strsep (&p, " ")) != NULL)
66 if (!strcmp (cap, "neon"))
67 core_caps |= VLC_CPU_ARM_NEON;
69 #elif defined (__i386__) || defined (__x86_64__)
71 if (!strcmp (cap, "mmx"))
72 core_caps |= CPU_CAPABILITY_MMX;
75 if (!strcmp (cap, "sse"))
76 core_caps |= CPU_CAPABILITY_SSE | CPU_CAPABILITY_MMXEXT;
77 if (!strcmp (cap, "mmxext"))
78 core_caps |= CPU_CAPABILITY_MMXEXT;
81 if (!strcmp (cap, "sse2"))
82 core_caps |= CPU_CAPABILITY_SSE2;
85 if (!strcmp (cap, "pni"))
86 core_caps |= CPU_CAPABILITY_SSE3;
89 if (!strcmp (cap, "ssse3"))
90 core_caps |= CPU_CAPABILITY_SSSE3;
93 if (!strcmp (cap, "sse4_1"))
94 core_caps |= CPU_CAPABILITY_SSE4_1;
97 if (!strcmp (cap, "sse4_2"))
98 core_caps |= CPU_CAPABILITY_SSE4_1;
100 if (!strcmp (cap, "sse4a"))
101 core_caps |= CPU_CAPABILITY_SSE4A;
103 if (!strcmp (cap, "3dnow"))
104 core_caps |= CPU_CAPABILITY_3DNOW;
107 #elif defined (__powerpc__) || defined (__powerpc64__)
108 if (!strcmp (cap, "altivec supported"))
109 core_caps |= CPU_CAPABILITY_ALTIVEC;
113 /* Take the intersection of capabilities of each processor */
114 all_caps &= core_caps;
119 if (all_caps == 0xFFFFFFFF) /* Error parsing of cpuinfo? */
120 all_caps = 0; /* Do not assume any capability! */
122 /* Always enable capabilities that were forced during compilation */
123 #if defined (__i386__) || defined (__x86_64__)
125 all_caps |= CPU_CAPABILITY_MMX;
128 all_caps |= CPU_CAPABILITY_SSE | CPU_CAPABILITY_MMXEXT;
131 all_caps |= CPU_CAPABILITY_SSE2;
134 all_caps |= CPU_CAPABILITY_SSE3;
137 all_caps |= CPU_CAPABILITY_SSSE3;
140 all_caps |= CPU_CAPABILITY_SSE4_1;
143 all_caps |= CPU_CAPABILITY_SSE4_2;
146 all_caps |= CPU_CAPABILITY_3DNOW;
150 cpu_flags = all_caps;
153 unsigned vlc_CPU (void)
155 static pthread_once_t once = PTHREAD_ONCE_INIT;
157 pthread_once (&once, vlc_CPU_init);
160 #else /* CPU_FLAGS */
161 unsigned vlc_CPU (void)