1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifdef HAVE_CONFIG_H
34 # include <config.h>
35 #endif
36
37 #include "private/cpu.h"
38 #include "share/compat.h"
39 #include <stdlib.h>
40 #include <memory.h>
41
42 #if defined(_MSC_VER)
43 # include <intrin.h> /* for __cpuid() and _xgetbv() */
44 #endif
45
46 #if defined __GNUC__ && defined HAVE_CPUID_H
47 # include <cpuid.h> /* for __get_cpuid() and __get_cpuid_max() */
48 #endif
49
50 #ifdef DEBUG
51 #include <stdio.h>
52
53 #define dfprintf fprintf
54 #else
55 /* This is bad practice, it should be a static void empty function */
56 #define dfprintf(file, format, ...)
57 #endif
58
59
60 #if defined FLAC__CPU_IA32
61 /* these are flags in EDX of CPUID AX=00000001 */
62 static const unsigned FLAC__CPUINFO_IA32_CPUID_CMOV = 0x00008000;
63 static const unsigned FLAC__CPUINFO_IA32_CPUID_MMX = 0x00800000;
64 static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE = 0x02000000;
65 static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE2 = 0x04000000;
66 #endif
67
68 #if FLAC__HAS_X86INTRIN || FLAC__AVX_SUPPORTED
69 /* these are flags in ECX of CPUID AX=00000001 */
70 static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE3 = 0x00000001;
71 static const unsigned FLAC__CPUINFO_IA32_CPUID_SSSE3 = 0x00000200;
72 static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE41 = 0x00080000;
73 static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE42 = 0x00100000;
74
75 /* these are flags in ECX of CPUID AX=00000001 */
76 static const unsigned FLAC__CPUINFO_IA32_CPUID_OSXSAVE = 0x08000000;
77 static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX = 0x10000000;
78 static const unsigned FLAC__CPUINFO_IA32_CPUID_FMA = 0x00001000;
79 /* these are flags in EBX of CPUID AX=00000007 */
80 static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX2 = 0x00000020;
81 #endif
82
83 #if defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64
84 static uint32_t
cpu_xgetbv_x86(void)85 cpu_xgetbv_x86(void)
86 {
87 #if (defined _MSC_VER || defined __INTEL_COMPILER) && FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED
88 return (uint32_t)_xgetbv(0);
89 #elif defined __GNUC__
90 uint32_t lo, hi;
91 asm volatile (".byte 0x0f, 0x01, 0xd0" : "=a"(lo), "=d"(hi) : "c" (0));
92 return lo;
93 #else
94 return 0;
95 #endif
96 }
97 #endif
98
99 static void
ia32_cpu_info(FLAC__CPUInfo * info)100 ia32_cpu_info (FLAC__CPUInfo *info)
101 {
102 #if !defined FLAC__CPU_IA32
103 (void) info;
104 #else
105 FLAC__bool ia32_osxsave = false;
106 FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;
107
108 #if !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN)
109 info->use_asm = true; /* we assume a minimum of 80386 with FLAC__CPU_IA32 */
110 #if defined FLAC__HAS_NASM
111 if(!FLAC__cpu_have_cpuid_asm_ia32())
112 return;
113 #endif
114 /* http://www.sandpile.org/x86/cpuid.htm */
115 if (FLAC__HAS_X86INTRIN) {
116 FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
117 info->ia32.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */
118 FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
119 }
120 else {
121 FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx);
122 }
123
124 info->ia32.cmov = (flags_edx & FLAC__CPUINFO_IA32_CPUID_CMOV ) ? true : false;
125 info->ia32.mmx = (flags_edx & FLAC__CPUINFO_IA32_CPUID_MMX ) ? true : false;
126 info->ia32.sse = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE ) ? true : false;
127 info->ia32.sse2 = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE2 ) ? true : false;
128 info->ia32.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false;
129 info->ia32.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false;
130 info->ia32.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false;
131 info->ia32.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false;
132
133 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
134 ia32_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false;
135 info->ia32.avx = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX ) ? true : false;
136 info->ia32.fma = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA ) ? true : false;
137 FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
138 info->ia32.avx2 = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2 ) ? true : false;
139 }
140
141 dfprintf(stderr, "CPU info (IA-32):\n");
142 dfprintf(stderr, " CMOV ....... %c\n", info->ia32.cmov ? 'Y' : 'n');
143 dfprintf(stderr, " MMX ........ %c\n", info->ia32.mmx ? 'Y' : 'n');
144 dfprintf(stderr, " SSE ........ %c\n", info->ia32.sse ? 'Y' : 'n');
145 dfprintf(stderr, " SSE2 ....... %c\n", info->ia32.sse2 ? 'Y' : 'n');
146 dfprintf(stderr, " SSE3 ....... %c\n", info->ia32.sse3 ? 'Y' : 'n');
147 dfprintf(stderr, " SSSE3 ...... %c\n", info->ia32.ssse3 ? 'Y' : 'n');
148 dfprintf(stderr, " SSE41 ...... %c\n", info->ia32.sse41 ? 'Y' : 'n');
149 dfprintf(stderr, " SSE42 ...... %c\n", info->ia32.sse42 ? 'Y' : 'n');
150
151 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
152 dfprintf(stderr, " AVX ........ %c\n", info->ia32.avx ? 'Y' : 'n');
153 dfprintf(stderr, " FMA ........ %c\n", info->ia32.fma ? 'Y' : 'n');
154 dfprintf(stderr, " AVX2 ....... %c\n", info->ia32.avx2 ? 'Y' : 'n');
155 }
156
157 /*
158 * now have to check for OS support of AVX instructions
159 */
160 if (!FLAC__HAS_X86INTRIN || !info->ia32.avx || !ia32_osxsave || (cpu_xgetbv_x86() & 0x6) != 0x6) {
161 /* no OS AVX support */
162 info->ia32.avx = false;
163 info->ia32.avx2 = false;
164 info->ia32.fma = false;
165 }
166
167 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
168 dfprintf(stderr, " AVX OS sup . %c\n", info->ia32.avx ? 'Y' : 'n');
169 }
170 #else
171 info->use_asm = false;
172 #endif
173 #endif
174 }
175
176 static void
x86_64_cpu_info(FLAC__CPUInfo * info)177 x86_64_cpu_info (FLAC__CPUInfo *info)
178 {
179 #if !defined FLAC__NO_ASM && FLAC__HAS_X86INTRIN
180 FLAC__bool x86_osxsave = false;
181 FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;
182
183 info->use_asm = true;
184
185 /* http://www.sandpile.org/x86/cpuid.htm */
186 FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
187 info->x86.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */
188 FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
189 info->x86.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false;
190 info->x86.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false;
191 info->x86.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false;
192 info->x86.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false;
193
194 if (FLAC__AVX_SUPPORTED) {
195 x86_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false;
196 info->x86.avx = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX ) ? true : false;
197 info->x86.fma = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA ) ? true : false;
198 FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
199 info->x86.avx2 = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2 ) ? true : false;
200 }
201
202 dfprintf(stderr, "CPU info (x86-64):\n");
203 dfprintf(stderr, " SSE3 ....... %c\n", info->x86.sse3 ? 'Y' : 'n');
204 dfprintf(stderr, " SSSE3 ...... %c\n", info->x86.ssse3 ? 'Y' : 'n');
205 dfprintf(stderr, " SSE41 ...... %c\n", info->x86.sse41 ? 'Y' : 'n');
206 dfprintf(stderr, " SSE42 ...... %c\n", info->x86.sse42 ? 'Y' : 'n');
207
208 if (FLAC__AVX_SUPPORTED) {
209 dfprintf(stderr, " AVX ........ %c\n", info->x86.avx ? 'Y' : 'n');
210 dfprintf(stderr, " FMA ........ %c\n", info->x86.fma ? 'Y' : 'n');
211 dfprintf(stderr, " AVX2 ....... %c\n", info->x86.avx2 ? 'Y' : 'n');
212 }
213
214 /*
215 * now have to check for OS support of AVX instructions
216 */
217 if (!info->x86.avx || !x86_osxsave || (cpu_xgetbv_x86() & 0x6) != 0x6) {
218 /* no OS AVX support */
219 info->x86.avx = false;
220 info->x86.avx2 = false;
221 info->x86.fma = false;
222 }
223
224 if (FLAC__AVX_SUPPORTED) {
225 dfprintf(stderr, " AVX OS sup . %c\n", info->x86.avx ? 'Y' : 'n');
226 }
227 #else
228 /* Silence compiler warnings. */
229 (void) info;
230 #if defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64
231 if (0) cpu_xgetbv_x86 ();
232 #endif
233 #endif
234 }
235
FLAC__cpu_info(FLAC__CPUInfo * info)236 void FLAC__cpu_info (FLAC__CPUInfo *info)
237 {
238 memset(info, 0, sizeof(*info));
239
240 #ifdef FLAC__CPU_IA32
241 info->type = FLAC__CPUINFO_TYPE_IA32;
242 #elif defined FLAC__CPU_X86_64
243 info->type = FLAC__CPUINFO_TYPE_X86_64;
244 #else
245 info->type = FLAC__CPUINFO_TYPE_UNKNOWN;
246 info->use_asm = false;
247 #endif
248
249 switch (info->type) {
250 case FLAC__CPUINFO_TYPE_IA32:
251 ia32_cpu_info (info);
252 break;
253 case FLAC__CPUINFO_TYPE_X86_64:
254 x86_64_cpu_info (info);
255 break;
256 default:
257 info->use_asm = false;
258 break;
259 }
260 }
261
262 #if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
263
FLAC__cpu_info_x86(FLAC__uint32 level,FLAC__uint32 * eax,FLAC__uint32 * ebx,FLAC__uint32 * ecx,FLAC__uint32 * edx)264 void FLAC__cpu_info_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx, FLAC__uint32 *ecx, FLAC__uint32 *edx)
265 {
266 #if defined _MSC_VER || defined __INTEL_COMPILER
267 int cpuinfo[4];
268 int ext = level & 0x80000000;
269 __cpuid(cpuinfo, ext);
270 if((unsigned)cpuinfo[0] >= level) {
271 #if FLAC__AVX_SUPPORTED
272 __cpuidex(cpuinfo, ext, 0); /* for AVX2 detection */
273 #else
274 __cpuid(cpuinfo, ext); /* some old compilers don't support __cpuidex */
275 #endif
276
277 *eax = cpuinfo[0]; *ebx = cpuinfo[1]; *ecx = cpuinfo[2]; *edx = cpuinfo[3];
278
279 return;
280 }
281 #elif defined __GNUC__ && defined HAVE_CPUID_H
282 FLAC__uint32 ext = level & 0x80000000;
283 __cpuid(ext, *eax, *ebx, *ecx, *edx);
284 if (*eax >= level) {
285 __cpuid_count(level, 0, *eax, *ebx, *ecx, *edx);
286
287 return;
288 }
289 #endif
290 *eax = *ebx = *ecx = *edx = 0;
291 }
292
293 #endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */
294