1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2001-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
37 #include "private/cpu.h"
38 #include "share/compat.h"
43 # include <intrin.h> /* for __cpuid() and _xgetbv() */
46 #if defined __GNUC__ && defined HAVE_CPUID_H
47 # include <cpuid.h> /* for __get_cpuid() and __get_cpuid_max() */
53 #define dfprintf fprintf
55 /* This is bad practice, it should be a static void empty function */
56 #define dfprintf(file, format, ...)
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;
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;
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;
83 #if defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64
87 #if (defined _MSC_VER || defined __INTEL_COMPILER) && FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED
88 return (uint32_t)_xgetbv(0);
89 #elif defined __GNUC__
91 asm volatile (".byte 0x0f, 0x01, 0xd0" : "=a"(lo), "=d"(hi) : "c" (0));
100 ia32_cpu_info (FLAC__CPUInfo *info)
102 #if !defined FLAC__CPU_IA32
105 FLAC__bool ia32_osxsave = false;
106 FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;
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())
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);
121 FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx);
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;
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;
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');
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');
158 * now have to check for OS support of AVX instructions
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;
167 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
168 dfprintf(stderr, " AVX OS sup . %c\n", info->ia32.avx ? 'Y' : 'n');
171 info->use_asm = false;
177 x86_64_cpu_info (FLAC__CPUInfo *info)
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;
183 info->use_asm = true;
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;
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;
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');
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');
215 * now have to check for OS support of AVX instructions
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;
224 if (FLAC__AVX_SUPPORTED) {
225 dfprintf(stderr, " AVX OS sup . %c\n", info->x86.avx ? 'Y' : 'n');
228 /* Silence compiler warnings. */
230 #if defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64
231 if (0) cpu_xgetbv_x86 ();
236 void FLAC__cpu_info (FLAC__CPUInfo *info)
238 memset(info, 0, sizeof(*info));
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;
245 info->type = FLAC__CPUINFO_TYPE_UNKNOWN;
246 info->use_asm = false;
249 switch (info->type) {
250 case FLAC__CPUINFO_TYPE_IA32:
251 ia32_cpu_info (info);
253 case FLAC__CPUINFO_TYPE_X86_64:
254 x86_64_cpu_info (info);
257 info->use_asm = false;
262 #if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
264 void FLAC__cpu_info_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx, FLAC__uint32 *ecx, FLAC__uint32 *edx)
266 #if defined _MSC_VER || defined __INTEL_COMPILER
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 */
274 __cpuid(cpuinfo, ext); /* some old compilers don't support __cpuidex */
277 *eax = cpuinfo[0]; *ebx = cpuinfo[1]; *ecx = cpuinfo[2]; *edx = cpuinfo[3];
281 #elif defined __GNUC__ && defined HAVE_CPUID_H
282 FLAC__uint32 ext = level & 0x80000000;
283 __cpuid(ext, *eax, *ebx, *ecx, *edx);
285 __cpuid_count(level, 0, *eax, *ebx, *ecx, *edx);
290 *eax = *ebx = *ecx = *edx = 0;
293 #endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */