blob: a89d162762acc0d19f30c977d37c74aea3ff0151 [file] [log] [blame]
Josh Coalsonedade2c2001-05-23 20:53:31 +00001/* libFLAC - Free Lossless Audio Codec library
Erik de Castro Lopob1982fb2013-05-25 17:11:19 +10002 * Copyright (C) 2001-2009 Josh Coalson
Erik de Castro Lopo14373912014-11-24 22:07:15 +11003 * Copyright (C) 2011-2014 Xiph.Org Foundation
Josh Coalsonedade2c2001-05-23 20:53:31 +00004 *
Josh Coalsonafd81072003-01-31 23:34:56 +00005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
Josh Coalsonedade2c2001-05-23 20:53:31 +00008 *
Josh Coalsonafd81072003-01-31 23:34:56 +00009 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
Josh Coalsonedade2c2001-05-23 20:53:31 +000011 *
Josh Coalsonafd81072003-01-31 23:34:56 +000012 * - 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.
Josh Coalsonedade2c2001-05-23 20:53:31 +000031 */
32
Erik de Castro Lopo006b8352014-03-23 21:59:46 +110033#ifdef HAVE_CONFIG_H
Josh Coalsonb1ec7962006-05-24 04:41:36 +000034# include <config.h>
35#endif
36
Josh Coalsonedade2c2001-05-23 20:53:31 +000037#include "private/cpu.h"
Erik de Castro Lopoa08e90c2016-06-26 21:09:08 +100038#include "share/compat.h"
Josh Coalson663c5992005-01-21 01:53:02 +000039#include <stdlib.h>
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +100040#include <memory.h>
Erik de Castro Lopo23778a32016-06-25 17:02:06 +100041
Erik de Castro Lopo23778a32016-06-25 17:02:06 +100042#if defined (__NetBSD__) || defined(__OpenBSD__)
43# include <sys/param.h>
44# include <sys/sysctl.h>
45# include <machine/cpu.h>
46#endif
47
48#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
49# include <sys/types.h>
50# include <sys/sysctl.h>
51#endif
52
53#if defined(__linux__) && defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) && !FLAC__SSE_OS
54# include <sys/ucontext.h>
55#endif
56
57#if defined(_MSC_VER)
58# include <windows.h>
59# include <intrin.h> /* for __cpuid() and _xgetbv() */
60#endif
61
62#if defined __GNUC__ && defined HAVE_CPUID_H
63# include <cpuid.h> /* for __get_cpuid() and __get_cpuid_max() */
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +100064#endif
Josh Coalsonedade2c2001-05-23 20:53:31 +000065
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +100066#ifdef DEBUG
Julian Calaby828e33b2016-06-27 22:10:47 +100067#include <stdio.h>
68
Julian Calaby601c0512016-06-27 22:08:51 +100069#define dfprintf fprintf
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +100070#else
Julian Calaby601c0512016-06-27 22:08:51 +100071/* This is bad practice, it should be a static void empty function */
72#define dfprintf(file, format, args...)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +100073#endif
74
75
Josh Coalsonf2f328b2007-03-13 06:35:55 +000076/* these are flags in EDX of CPUID AX=00000001 */
77static const unsigned FLAC__CPUINFO_IA32_CPUID_CMOV = 0x00008000;
78static const unsigned FLAC__CPUINFO_IA32_CPUID_MMX = 0x00800000;
79static const unsigned FLAC__CPUINFO_IA32_CPUID_FXSR = 0x01000000;
80static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE = 0x02000000;
81static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE2 = 0x04000000;
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +100082
Josh Coalsonf2f328b2007-03-13 06:35:55 +000083/* these are flags in ECX of CPUID AX=00000001 */
84static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE3 = 0x00000001;
85static const unsigned FLAC__CPUINFO_IA32_CPUID_SSSE3 = 0x00000200;
Erik de Castro Lopo8fe2c232013-09-25 23:05:13 +100086static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE41 = 0x00080000;
87static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE42 = 0x00100000;
Josh Coalsonedade2c2001-05-23 20:53:31 +000088
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +100089/* these are flags in ECX of CPUID AX=00000001 */
90static const unsigned FLAC__CPUINFO_IA32_CPUID_OSXSAVE = 0x08000000;
91static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX = 0x10000000;
92static const unsigned FLAC__CPUINFO_IA32_CPUID_FMA = 0x00001000;
93/* these are flags in EBX of CPUID AX=00000007 */
94static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX2 = 0x00000020;
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +100095
Josh Coalson83f42e32007-03-03 01:52:19 +000096/*
97 * Extra stuff needed for detection of OS support for SSE on IA-32
98 */
Erik de Castro Lopo23778a32016-06-25 17:02:06 +100099#if defined(__linux__) && defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) && !FLAC__SSE_OS
Josh Coalson83f42e32007-03-03 01:52:19 +0000100/*
101 * If the OS doesn't support SSE, we will get here with a SIGILL. We
102 * modify the return address to jump over the offending SSE instruction
103 * and also the operation following it that indicates the instruction
104 * executed successfully. In this way we use no global variables and
105 * stay thread-safe.
106 *
107 * 3 + 3 + 6:
108 * 3 bytes for "xorps xmm0,xmm0"
109 * 3 bytes for estimate of how long the follwing "inc var" instruction is
110 * 6 bytes extra in case our estimate is wrong
111 * 12 bytes puts us in the NOP "landing zone"
112 */
Erik de Castro Lopo23778a32016-06-25 17:02:06 +1000113static void sigill_handler_sse_os(int signal, siginfo_t *si, void *uc)
114{
115 (void)signal, (void)si;
116 ((ucontext_t*)uc)->uc_mcontext.gregs[14/*REG_EIP*/] += 3 + 3 + 6;
117}
Josh Coalson83f42e32007-03-03 01:52:19 +0000118#endif
119
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000120#if defined FLAC__CPU_IA32
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000121static void
122ia32_disable_sse(FLAC__CPUInfo *info)
123{
124 info->ia32.sse = false;
125 info->ia32.sse2 = false;
126 info->ia32.sse3 = false;
127 info->ia32.ssse3 = false;
128 info->ia32.sse41 = false;
129 info->ia32.sse42 = false;
130}
131
132static void
133ia32_disable_avx(FLAC__CPUInfo *info)
134{
135 info->ia32.avx = false;
136 info->ia32.avx2 = false;
137 info->ia32.fma = false;
138}
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000139#endif
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000140
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000141#if defined FLAC__CPU_X86_64
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000142static void
143x86_64_disable_avx(FLAC__CPUInfo *info)
144{
145 info->x86.avx = false;
146 info->x86.avx2 = false;
147 info->x86.fma = false;
148}
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000149#endif
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000150
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000151#if defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000152static uint32_t
153cpu_xgetbv_x86(void)
Erik de Castro Lopofa246132016-06-26 10:39:18 +1000154{
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000155#if (defined _MSC_VER || defined __INTEL_COMPILER) && FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED
Erik de Castro Lopofa246132016-06-26 10:39:18 +1000156 return (uint32_t)_xgetbv(0);
157#elif defined __GNUC__
158 uint32_t lo, hi;
159 asm volatile (".byte 0x0f, 0x01, 0xd0" : "=a"(lo), "=d"(hi) : "c" (0));
160 return lo;
161#else
162 return 0;
163#endif
164}
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000165#endif
Josh Coalson83f42e32007-03-03 01:52:19 +0000166
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000167static void
168ia32_cpu_info (FLAC__CPUInfo *info)
Josh Coalsonedade2c2001-05-23 20:53:31 +0000169{
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000170#if !defined FLAC__CPU_IA32
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000171 (void) info;
Julian Calaby078c6312016-06-27 22:13:30 +1000172#elif defined(__ANDROID__) || defined(ANDROID)
173 /* no need to check OS SSE support */
174 info->use_asm = true;
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000175#else
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000176 FLAC__bool ia32_fxsr = false;
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000177 FLAC__bool ia32_osxsave = false;
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000178 FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;
179
Erik de Castro Lopo36a0ab12016-06-20 20:29:59 +1000180#if !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN)
Josh Coalsonf2f328b2007-03-13 06:35:55 +0000181 info->use_asm = true; /* we assume a minimum of 80386 with FLAC__CPU_IA32 */
Erik de Castro Lopofa246132016-06-26 10:39:18 +1000182#if !FLAC__HAS_X86INTRIN
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000183 if(!FLAC__cpu_have_cpuid_asm_ia32())
184 return;
185#endif
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000186 /* http://www.sandpile.org/x86/cpuid.htm */
187 if (FLAC__HAS_X86INTRIN) {
188 FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
189 info->ia32.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */
190 FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
191 }
192 else {
193 FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000194 }
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000195
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000196 info->ia32.cmov = (flags_edx & FLAC__CPUINFO_IA32_CPUID_CMOV ) ? true : false;
197 info->ia32.mmx = (flags_edx & FLAC__CPUINFO_IA32_CPUID_MMX ) ? true : false;
198 ia32_fxsr = (flags_edx & FLAC__CPUINFO_IA32_CPUID_FXSR ) ? true : false;
199 info->ia32.sse = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE ) ? true : false;
200 info->ia32.sse2 = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE2 ) ? true : false;
201 info->ia32.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false;
202 info->ia32.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false;
203 info->ia32.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false;
204 info->ia32.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false;
205
Erik de Castro Lopo936e9682016-06-26 11:32:06 +1000206 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000207 ia32_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false;
208 info->ia32.avx = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX ) ? true : false;
209 info->ia32.fma = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA ) ? true : false;
210 FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
211 info->ia32.avx2 = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2 ) ? true : false;
Erik de Castro Lopo936e9682016-06-26 11:32:06 +1000212 }
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000213
Julian Calaby601c0512016-06-27 22:08:51 +1000214 dfprintf(stderr, "CPU info (IA-32):\n");
215 dfprintf(stderr, " CMOV ....... %c\n", info->ia32.cmov ? 'Y' : 'n');
216 dfprintf(stderr, " MMX ........ %c\n", info->ia32.mmx ? 'Y' : 'n');
217 dfprintf(stderr, " SSE ........ %c\n", info->ia32.sse ? 'Y' : 'n');
218 dfprintf(stderr, " SSE2 ....... %c\n", info->ia32.sse2 ? 'Y' : 'n');
219 dfprintf(stderr, " SSE3 ....... %c\n", info->ia32.sse3 ? 'Y' : 'n');
220 dfprintf(stderr, " SSSE3 ...... %c\n", info->ia32.ssse3 ? 'Y' : 'n');
221 dfprintf(stderr, " SSE41 ...... %c\n", info->ia32.sse41 ? 'Y' : 'n');
222 dfprintf(stderr, " SSE42 ...... %c\n", info->ia32.sse42 ? 'Y' : 'n');
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000223
Julian Calaby601c0512016-06-27 22:08:51 +1000224 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
225 dfprintf(stderr, " AVX ........ %c\n", info->ia32.avx ? 'Y' : 'n');
226 dfprintf(stderr, " FMA ........ %c\n", info->ia32.fma ? 'Y' : 'n');
227 dfprintf(stderr, " AVX2 ....... %c\n", info->ia32.avx2 ? 'Y' : 'n');
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000228 }
Josh Coalsonf2f328b2007-03-13 06:35:55 +0000229
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000230 /*
231 * now have to check for OS support of SSE instructions
232 */
233 if(info->ia32.sse) {
Erik de Castro Lopo23778a32016-06-25 17:02:06 +1000234#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000235 int sse = 0;
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000236 size_t len = sizeof(sse);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000237 /* at least one of these must work: */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000238 sse = sse || (sysctlbyname("hw.instruction_sse", &sse, &len, NULL, 0) == 0 && sse);
239 sse = sse || (sysctlbyname("hw.optional.sse" , &sse, &len, NULL, 0) == 0 && sse); /* __APPLE__ ? */
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000240 if(!sse)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000241 ia32_disable_sse(info);
Josh Coalson83f42e32007-03-03 01:52:19 +0000242#elif defined(__NetBSD__) || defined (__OpenBSD__)
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000243 int val = 0, mib[2] = { CTL_MACHDEP, CPU_SSE };
244 size_t len = sizeof(val);
245 if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000246 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000247 else { /* double-check SSE2 */
248 mib[1] = CPU_SSE2;
249 len = sizeof(val);
250 if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val) {
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000251 ia32_disable_sse(info);
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000252 info->ia32.sse = true;
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000253 }
254 }
Erik de Castro Lopoe1200372016-06-19 21:51:31 +1000255#elif defined(__linux__) && !FLAC__SSE_OS
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000256 int sse = 0;
257 struct sigaction sigill_save;
258 struct sigaction sigill_sse;
259 sigill_sse.sa_sigaction = sigill_handler_sse_os;
Erik de Castro Lopoa9f84422016-03-14 18:14:31 +1100260 sigemptyset(&sigill_sse.sa_mask);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000261 sigill_sse.sa_flags = SA_SIGINFO | SA_RESETHAND; /* SA_RESETHAND just in case our SIGILL return jump breaks, so we don't get stuck in a loop */
262 if(0 == sigaction(SIGILL, &sigill_sse, &sigill_save))
263 {
264 /* http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html */
265 /* see sigill_handler_sse_os() for an explanation of the following: */
266 asm volatile (
267 "xorps %%xmm0,%%xmm0\n\t" /* will cause SIGILL if unsupported by OS */
268 "incl %0\n\t" /* SIGILL handler will jump over this */
269 /* landing zone */
270 "nop\n\t" /* SIGILL jump lands here if "inc" is 9 bytes */
271 "nop\n\t"
272 "nop\n\t"
273 "nop\n\t"
274 "nop\n\t"
275 "nop\n\t"
276 "nop\n\t" /* SIGILL jump lands here if "inc" is 3 bytes (expected) */
277 "nop\n\t"
278 "nop" /* SIGILL jump lands here if "inc" is 1 byte */
279 : "=r"(sse)
280 : "0"(sse)
281 );
Josh Coalson83f42e32007-03-03 01:52:19 +0000282
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000283 sigaction(SIGILL, &sigill_save, NULL);
284 }
Josh Coalson83f42e32007-03-03 01:52:19 +0000285
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000286 if(!sse)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000287 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000288#elif defined(_MSC_VER)
289 __try {
290 __asm {
291 xorps xmm0,xmm0
292 }
293 }
294 __except(EXCEPTION_EXECUTE_HANDLER) {
295 if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000296 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000297 }
298#elif defined(__GNUC__) /* MinGW goes here */
299 int sse = 0;
300 /* Based on the idea described in Agner Fog's manual "Optimizing subroutines in assembly language" */
301 /* In theory, not guaranteed to detect lack of OS SSE support on some future Intel CPUs, but in practice works (see the aforementioned manual) */
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000302 if (ia32_fxsr) {
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000303 struct {
304 FLAC__uint32 buff[128];
305 } __attribute__((aligned(16))) fxsr;
306 FLAC__uint32 old_val, new_val;
307
Erik de Castro Lopo8d432822015-10-13 20:20:29 +1100308 memset(fxsr.buff, 0, sizeof (fxsr.buff));
309
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000310 asm volatile ("fxsave %0" : "=m" (fxsr) : "m" (fxsr));
311 old_val = fxsr.buff[50];
312 fxsr.buff[50] ^= 0x0013c0de; /* change value in the buffer */
313 asm volatile ("fxrstor %0" : "=m" (fxsr) : "m" (fxsr)); /* try to change SSE register */
314 fxsr.buff[50] = old_val; /* restore old value in the buffer */
Erik de Castro Lopo6ec64d32015-11-18 18:59:26 +1100315 asm volatile ("fxsave %0" : "=m" (fxsr) : "m" (fxsr)); /* old value will be overwritten if SSE register was changed */
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000316 new_val = fxsr.buff[50]; /* == old_val if FXRSTOR didn't change SSE register and (old_val ^ 0x0013c0de) otherwise */
317 fxsr.buff[50] = old_val; /* again restore old value in the buffer */
318 asm volatile ("fxrstor %0" : "=m" (fxsr) : "m" (fxsr)); /* restore old values of registers */
319
320 if ((old_val^new_val) == 0x0013c0de)
321 sse = 1;
322 }
323 if(!sse)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000324 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000325#else
326 /* no way to test, disable to be safe */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000327 ia32_disable_sse(info);
Josh Coalson83f42e32007-03-03 01:52:19 +0000328#endif
Julian Calaby601c0512016-06-27 22:08:51 +1000329 dfprintf(stderr, " SSE OS sup . %c\n", info->ia32.sse ? 'Y' : 'n');
Josh Coalsonedade2c2001-05-23 20:53:31 +0000330 }
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000331 else /* info->ia32.sse == false */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000332 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000333
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000334 /*
335 * now have to check for OS support of AVX instructions
336 */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000337 if (FLAC__HAS_X86INTRIN && info->ia32.avx && ia32_osxsave) {
Erik de Castro Lopofa246132016-06-26 10:39:18 +1000338 FLAC__uint32 ecr = cpu_xgetbv_x86();
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000339 if ((ecr & 0x6) != 0x6)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000340 ia32_disable_avx(info);
Julian Calaby601c0512016-06-27 22:08:51 +1000341
342 dfprintf(stderr, " AVX OS sup . %c\n", info->ia32.avx ? 'Y' : 'n');
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000343 }
Erik de Castro Lopo6ec64d32015-11-18 18:59:26 +1100344 else /* no OS AVX support */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000345 ia32_disable_avx(info);
346
Erik de Castro Lopo826b64f2015-11-07 07:06:23 +1100347#else
Josh Coalsonedade2c2001-05-23 20:53:31 +0000348 info->use_asm = false;
349#endif
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000350#endif
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000351}
Josh Coalson83f42e32007-03-03 01:52:19 +0000352
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000353static void
354x86_64_cpu_info (FLAC__CPUInfo *info)
355{
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000356#if !defined FLAC__CPU_X86_64
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000357 (void) info;
Julian Calaby078c6312016-06-27 22:13:30 +1000358#elif defined(__ANDROID__) || defined(ANDROID)
359 /* no need to check OS SSE support */
360 info->use_asm = true;
Julian Calabye58a6e72016-06-27 22:17:03 +1000361#elif !defined FLAC__NO_ASM && FLAC__HAS_X86INTRIN
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000362 FLAC__bool x86_osxsave = false;
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000363 FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;
364
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000365 info->use_asm = true;
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000366
367 /* http://www.sandpile.org/x86/cpuid.htm */
368 FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
369 info->x86.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */
370 FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
371 info->x86.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false;
372 info->x86.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false;
373 info->x86.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false;
374 info->x86.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false;
375
376 if (FLAC__AVX_SUPPORTED) {
377 x86_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false;
378 info->x86.avx = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX ) ? true : false;
379 info->x86.fma = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA ) ? true : false;
380 FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
381 info->x86.avx2 = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2 ) ? true : false;
382 }
383
Julian Calaby601c0512016-06-27 22:08:51 +1000384 dfprintf(stderr, "CPU info (x86-64):\n");
385 dfprintf(stderr, " SSE3 ....... %c\n", info->x86.sse3 ? 'Y' : 'n');
386 dfprintf(stderr, " SSSE3 ...... %c\n", info->x86.ssse3 ? 'Y' : 'n');
387 dfprintf(stderr, " SSE41 ...... %c\n", info->x86.sse41 ? 'Y' : 'n');
388 dfprintf(stderr, " SSE42 ...... %c\n", info->x86.sse42 ? 'Y' : 'n');
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000389
Julian Calaby601c0512016-06-27 22:08:51 +1000390 if (FLAC__AVX_SUPPORTED) {
391 dfprintf(stderr, " AVX ........ %c\n", info->x86.avx ? 'Y' : 'n');
392 dfprintf(stderr, " FMA ........ %c\n", info->x86.fma ? 'Y' : 'n');
393 dfprintf(stderr, " AVX2 ....... %c\n", info->x86.avx2 ? 'Y' : 'n');
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000394 }
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000395
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000396 /*
397 * now have to check for OS support of AVX instructions
398 */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000399 if (info->x86.avx && x86_osxsave) {
Erik de Castro Lopofa246132016-06-26 10:39:18 +1000400 FLAC__uint32 ecr = cpu_xgetbv_x86();
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000401 if ((ecr & 0x6) != 0x6)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000402 x86_64_disable_avx(info);
Julian Calaby601c0512016-06-27 22:08:51 +1000403
404 dfprintf(stderr, " AVX OS sup . %c\n", info->x86.avx ? 'Y' : 'n');
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000405 }
Erik de Castro Lopo6ec64d32015-11-18 18:59:26 +1100406 else /* no OS AVX support */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000407 x86_64_disable_avx(info);
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000408#endif
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000409}
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000410
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000411void FLAC__cpu_info (FLAC__CPUInfo *info)
412{
413 memset(info, 0, sizeof(*info));
414
415#ifdef FLAC__CPU_IA32
416 info->type = FLAC__CPUINFO_TYPE_IA32;
417#elif defined FLAC__CPU_X86_64
418 info->type = FLAC__CPUINFO_TYPE_X86_64;
Josh Coalsonedade2c2001-05-23 20:53:31 +0000419#else
Josh Coalsonedade2c2001-05-23 20:53:31 +0000420 info->type = FLAC__CPUINFO_TYPE_UNKNOWN;
421 info->use_asm = false;
422#endif
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000423
424 switch (info->type) {
425 case FLAC__CPUINFO_TYPE_IA32:
426 ia32_cpu_info (info);
427 break;
428 case FLAC__CPUINFO_TYPE_X86_64:
429 x86_64_cpu_info (info);
430 break;
431 default:
432 info->use_asm = false;
433 break;
434 }
Josh Coalsonedade2c2001-05-23 20:53:31 +0000435}
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000436
Erik de Castro Lopo36a0ab12016-06-20 20:29:59 +1000437#if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000438
Erik de Castro Lopoa08e90c2016-06-26 21:09:08 +1000439#if defined _MSC_VER || defined __INTEL_COMPILER
440static inline void
441cpu_id_ex (int *cpuinfo, int result)
442{
443// Stupid MSVC doesn't know how to optimise out:
444// if (FLAC_AVC_SUPPORTER)
445// __cpuidex(cpuinfo, level, 0); /* for AVX2 detection */
446#if FLAC__AVX_SUPPORTED
447 __cpuidex(cpuinfo, result, 0); /* for AVX2 detection */
448#else
449 __cpuid(cpuinfo, result); /* some old compilers don't support __cpuidex */
450#endif
451}
452#endif
453
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000454void FLAC__cpu_info_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx, FLAC__uint32 *ecx, FLAC__uint32 *edx)
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000455{
Erik de Castro Lopo57297ee2014-01-30 21:53:37 +1100456#if defined _MSC_VER || defined __INTEL_COMPILER
Erik de Castro Lopo8fe2c232013-09-25 23:05:13 +1000457 int cpuinfo[4];
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000458 int ext = level & 0x80000000;
459 __cpuid(cpuinfo, ext);
460 if((unsigned)cpuinfo[0] < level) {
461 *eax = *ebx = *ecx = *edx = 0;
Erik de Castro Lopo2c150522014-06-28 21:50:09 +1000462 return;
463 }
Erik de Castro Lopoa08e90c2016-06-26 21:09:08 +1000464
465 cpu_id_ex (cpuinfo, ext);
Erik de Castro Lopo936e9682016-06-26 11:32:06 +1000466
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000467 *eax = cpuinfo[0]; *ebx = cpuinfo[1]; *ecx = cpuinfo[2]; *edx = cpuinfo[3];
468#elif defined __GNUC__ && defined HAVE_CPUID_H
469 FLAC__uint32 ext = level & 0x80000000;
470 __cpuid(ext, *eax, *ebx, *ecx, *edx);
471 if (*eax < level) {
472 *eax = *ebx = *ecx = *edx = 0;
473 return;
474 }
475 __cpuid_count(level, 0, *eax, *ebx, *ecx, *edx);
476#else
477 *eax = *ebx = *ecx = *edx = 0;
478#endif
479}
480
Erik de Castro Lopob8d58e32014-06-15 20:29:34 +1000481#endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */