blob: 33e4bd2723f3a9db481ef27e7fcd94371973443f [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 */
Erik de Castro Lopo51fc9482016-07-01 06:41:18 +100072#define dfprintf(file, format, ...)
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 */
Erik de Castro Lopo23f6a352016-09-08 20:39:16 +100077#if defined FLAC__CPU_IA32
Josh Coalsonf2f328b2007-03-13 06:35:55 +000078static const unsigned FLAC__CPUINFO_IA32_CPUID_CMOV = 0x00008000;
79static const unsigned FLAC__CPUINFO_IA32_CPUID_MMX = 0x00800000;
80static const unsigned FLAC__CPUINFO_IA32_CPUID_FXSR = 0x01000000;
81static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE = 0x02000000;
82static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE2 = 0x04000000;
Erik de Castro Lopo23f6a352016-09-08 20:39:16 +100083#endif
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +100084
Josh Coalsonf2f328b2007-03-13 06:35:55 +000085/* these are flags in ECX of CPUID AX=00000001 */
86static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE3 = 0x00000001;
87static const unsigned FLAC__CPUINFO_IA32_CPUID_SSSE3 = 0x00000200;
Erik de Castro Lopo8fe2c232013-09-25 23:05:13 +100088static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE41 = 0x00080000;
89static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE42 = 0x00100000;
Josh Coalsonedade2c2001-05-23 20:53:31 +000090
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +100091/* these are flags in ECX of CPUID AX=00000001 */
92static const unsigned FLAC__CPUINFO_IA32_CPUID_OSXSAVE = 0x08000000;
93static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX = 0x10000000;
94static const unsigned FLAC__CPUINFO_IA32_CPUID_FMA = 0x00001000;
95/* these are flags in EBX of CPUID AX=00000007 */
96static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX2 = 0x00000020;
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +100097
Josh Coalson83f42e32007-03-03 01:52:19 +000098/*
99 * Extra stuff needed for detection of OS support for SSE on IA-32
100 */
Erik de Castro Lopo23778a32016-06-25 17:02:06 +1000101#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 +0000102/*
103 * If the OS doesn't support SSE, we will get here with a SIGILL. We
104 * modify the return address to jump over the offending SSE instruction
105 * and also the operation following it that indicates the instruction
106 * executed successfully. In this way we use no global variables and
107 * stay thread-safe.
108 *
109 * 3 + 3 + 6:
110 * 3 bytes for "xorps xmm0,xmm0"
111 * 3 bytes for estimate of how long the follwing "inc var" instruction is
112 * 6 bytes extra in case our estimate is wrong
113 * 12 bytes puts us in the NOP "landing zone"
114 */
Erik de Castro Lopo23778a32016-06-25 17:02:06 +1000115static void sigill_handler_sse_os(int signal, siginfo_t *si, void *uc)
116{
117 (void)signal, (void)si;
118 ((ucontext_t*)uc)->uc_mcontext.gregs[14/*REG_EIP*/] += 3 + 3 + 6;
119}
Josh Coalson83f42e32007-03-03 01:52:19 +0000120#endif
121
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000122#if defined FLAC__CPU_IA32
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000123static void
124ia32_disable_sse(FLAC__CPUInfo *info)
125{
126 info->ia32.sse = false;
127 info->ia32.sse2 = false;
128 info->ia32.sse3 = false;
129 info->ia32.ssse3 = false;
130 info->ia32.sse41 = false;
131 info->ia32.sse42 = false;
132}
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000133#endif
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000134
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000135#if defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000136static uint32_t
137cpu_xgetbv_x86(void)
Erik de Castro Lopofa246132016-06-26 10:39:18 +1000138{
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000139#if (defined _MSC_VER || defined __INTEL_COMPILER) && FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED
Erik de Castro Lopofa246132016-06-26 10:39:18 +1000140 return (uint32_t)_xgetbv(0);
141#elif defined __GNUC__
142 uint32_t lo, hi;
143 asm volatile (".byte 0x0f, 0x01, 0xd0" : "=a"(lo), "=d"(hi) : "c" (0));
144 return lo;
145#else
146 return 0;
147#endif
148}
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000149#endif
Josh Coalson83f42e32007-03-03 01:52:19 +0000150
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000151static void
152ia32_cpu_info (FLAC__CPUInfo *info)
Josh Coalsonedade2c2001-05-23 20:53:31 +0000153{
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000154#if !defined FLAC__CPU_IA32
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000155 (void) info;
Julian Calaby078c6312016-06-27 22:13:30 +1000156#elif defined(__ANDROID__) || defined(ANDROID)
157 /* no need to check OS SSE support */
158 info->use_asm = true;
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000159#else
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000160 FLAC__bool ia32_fxsr = false;
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000161 FLAC__bool ia32_osxsave = false;
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000162 FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;
163
Erik de Castro Lopo36a0ab12016-06-20 20:29:59 +1000164#if !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN)
Josh Coalsonf2f328b2007-03-13 06:35:55 +0000165 info->use_asm = true; /* we assume a minimum of 80386 with FLAC__CPU_IA32 */
Erik de Castro Lopofa246132016-06-26 10:39:18 +1000166#if !FLAC__HAS_X86INTRIN
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000167 if(!FLAC__cpu_have_cpuid_asm_ia32())
168 return;
169#endif
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000170 /* http://www.sandpile.org/x86/cpuid.htm */
171 if (FLAC__HAS_X86INTRIN) {
172 FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
173 info->ia32.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */
174 FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
175 }
176 else {
177 FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000178 }
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000179
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000180 info->ia32.cmov = (flags_edx & FLAC__CPUINFO_IA32_CPUID_CMOV ) ? true : false;
181 info->ia32.mmx = (flags_edx & FLAC__CPUINFO_IA32_CPUID_MMX ) ? true : false;
182 ia32_fxsr = (flags_edx & FLAC__CPUINFO_IA32_CPUID_FXSR ) ? true : false;
183 info->ia32.sse = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE ) ? true : false;
184 info->ia32.sse2 = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE2 ) ? true : false;
185 info->ia32.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false;
186 info->ia32.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false;
187 info->ia32.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false;
188 info->ia32.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false;
189
Erik de Castro Lopo936e9682016-06-26 11:32:06 +1000190 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000191 ia32_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false;
192 info->ia32.avx = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX ) ? true : false;
193 info->ia32.fma = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA ) ? true : false;
194 FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
195 info->ia32.avx2 = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2 ) ? true : false;
Erik de Castro Lopo936e9682016-06-26 11:32:06 +1000196 }
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000197
Julian Calaby601c0512016-06-27 22:08:51 +1000198 dfprintf(stderr, "CPU info (IA-32):\n");
199 dfprintf(stderr, " CMOV ....... %c\n", info->ia32.cmov ? 'Y' : 'n');
200 dfprintf(stderr, " MMX ........ %c\n", info->ia32.mmx ? 'Y' : 'n');
201 dfprintf(stderr, " SSE ........ %c\n", info->ia32.sse ? 'Y' : 'n');
202 dfprintf(stderr, " SSE2 ....... %c\n", info->ia32.sse2 ? 'Y' : 'n');
203 dfprintf(stderr, " SSE3 ....... %c\n", info->ia32.sse3 ? 'Y' : 'n');
204 dfprintf(stderr, " SSSE3 ...... %c\n", info->ia32.ssse3 ? 'Y' : 'n');
205 dfprintf(stderr, " SSE41 ...... %c\n", info->ia32.sse41 ? 'Y' : 'n');
206 dfprintf(stderr, " SSE42 ...... %c\n", info->ia32.sse42 ? 'Y' : 'n');
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000207
Julian Calaby601c0512016-06-27 22:08:51 +1000208 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
209 dfprintf(stderr, " AVX ........ %c\n", info->ia32.avx ? 'Y' : 'n');
210 dfprintf(stderr, " FMA ........ %c\n", info->ia32.fma ? 'Y' : 'n');
211 dfprintf(stderr, " AVX2 ....... %c\n", info->ia32.avx2 ? 'Y' : 'n');
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000212 }
Josh Coalsonf2f328b2007-03-13 06:35:55 +0000213
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000214 /*
215 * now have to check for OS support of SSE instructions
216 */
217 if(info->ia32.sse) {
Erik de Castro Lopo23778a32016-06-25 17:02:06 +1000218#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000219 int sse = 0;
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000220 size_t len = sizeof(sse);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000221 /* at least one of these must work: */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000222 sse = sse || (sysctlbyname("hw.instruction_sse", &sse, &len, NULL, 0) == 0 && sse);
223 sse = sse || (sysctlbyname("hw.optional.sse" , &sse, &len, NULL, 0) == 0 && sse); /* __APPLE__ ? */
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000224 if(!sse)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000225 ia32_disable_sse(info);
Josh Coalson83f42e32007-03-03 01:52:19 +0000226#elif defined(__NetBSD__) || defined (__OpenBSD__)
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000227 int val = 0, mib[2] = { CTL_MACHDEP, CPU_SSE };
228 size_t len = sizeof(val);
229 if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000230 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000231 else { /* double-check SSE2 */
232 mib[1] = CPU_SSE2;
233 len = sizeof(val);
234 if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val) {
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000235 ia32_disable_sse(info);
Erik de Castro Lopo35c14b12014-10-04 09:05:08 +1000236 info->ia32.sse = true;
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000237 }
238 }
Erik de Castro Lopoe1200372016-06-19 21:51:31 +1000239#elif defined(__linux__) && !FLAC__SSE_OS
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000240 int sse = 0;
241 struct sigaction sigill_save;
242 struct sigaction sigill_sse;
243 sigill_sse.sa_sigaction = sigill_handler_sse_os;
Erik de Castro Lopoa9f84422016-03-14 18:14:31 +1100244 sigemptyset(&sigill_sse.sa_mask);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000245 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 */
246 if(0 == sigaction(SIGILL, &sigill_sse, &sigill_save))
247 {
248 /* http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html */
249 /* see sigill_handler_sse_os() for an explanation of the following: */
250 asm volatile (
251 "xorps %%xmm0,%%xmm0\n\t" /* will cause SIGILL if unsupported by OS */
252 "incl %0\n\t" /* SIGILL handler will jump over this */
253 /* landing zone */
254 "nop\n\t" /* SIGILL jump lands here if "inc" is 9 bytes */
255 "nop\n\t"
256 "nop\n\t"
257 "nop\n\t"
258 "nop\n\t"
259 "nop\n\t"
260 "nop\n\t" /* SIGILL jump lands here if "inc" is 3 bytes (expected) */
261 "nop\n\t"
262 "nop" /* SIGILL jump lands here if "inc" is 1 byte */
263 : "=r"(sse)
264 : "0"(sse)
265 );
Josh Coalson83f42e32007-03-03 01:52:19 +0000266
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000267 sigaction(SIGILL, &sigill_save, NULL);
268 }
Josh Coalson83f42e32007-03-03 01:52:19 +0000269
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000270 if(!sse)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000271 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000272#elif defined(_MSC_VER)
273 __try {
274 __asm {
275 xorps xmm0,xmm0
276 }
277 }
278 __except(EXCEPTION_EXECUTE_HANDLER) {
279 if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000280 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000281 }
282#elif defined(__GNUC__) /* MinGW goes here */
283 int sse = 0;
284 /* Based on the idea described in Agner Fog's manual "Optimizing subroutines in assembly language" */
285 /* 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 +1000286 if (ia32_fxsr) {
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000287 struct {
288 FLAC__uint32 buff[128];
289 } __attribute__((aligned(16))) fxsr;
290 FLAC__uint32 old_val, new_val;
291
Erik de Castro Lopo8d432822015-10-13 20:20:29 +1100292 memset(fxsr.buff, 0, sizeof (fxsr.buff));
293
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000294 asm volatile ("fxsave %0" : "=m" (fxsr) : "m" (fxsr));
295 old_val = fxsr.buff[50];
296 fxsr.buff[50] ^= 0x0013c0de; /* change value in the buffer */
297 asm volatile ("fxrstor %0" : "=m" (fxsr) : "m" (fxsr)); /* try to change SSE register */
298 fxsr.buff[50] = old_val; /* restore old value in the buffer */
Erik de Castro Lopo6ec64d32015-11-18 18:59:26 +1100299 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 +1000300 new_val = fxsr.buff[50]; /* == old_val if FXRSTOR didn't change SSE register and (old_val ^ 0x0013c0de) otherwise */
301 fxsr.buff[50] = old_val; /* again restore old value in the buffer */
302 asm volatile ("fxrstor %0" : "=m" (fxsr) : "m" (fxsr)); /* restore old values of registers */
303
304 if ((old_val^new_val) == 0x0013c0de)
305 sse = 1;
306 }
307 if(!sse)
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000308 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000309#else
310 /* no way to test, disable to be safe */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000311 ia32_disable_sse(info);
Josh Coalson83f42e32007-03-03 01:52:19 +0000312#endif
Julian Calaby601c0512016-06-27 22:08:51 +1000313 dfprintf(stderr, " SSE OS sup . %c\n", info->ia32.sse ? 'Y' : 'n');
Josh Coalsonedade2c2001-05-23 20:53:31 +0000314 }
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000315 else /* info->ia32.sse == false */
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000316 ia32_disable_sse(info);
Erik de Castro Lopocf732a82014-09-24 06:41:56 +1000317
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000318 /*
319 * now have to check for OS support of AVX instructions
320 */
Julian Calaby42c5de62016-06-27 23:22:20 +1000321 if (!FLAC__HAS_X86INTRIN || !info->ia32.avx || !ia32_osxsave || (cpu_xgetbv_x86() & 0x6) != 0x6) {
322 /* no OS AVX support */
323 info->ia32.avx = false;
324 info->ia32.avx2 = false;
325 info->ia32.fma = false;
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000326 }
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000327
Erik de Castro Lopobba26a52016-06-28 07:12:42 +1000328 if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
Julian Calaby42c5de62016-06-27 23:22:20 +1000329 dfprintf(stderr, " AVX OS sup . %c\n", info->ia32.avx ? 'Y' : 'n');
Erik de Castro Lopobba26a52016-06-28 07:12:42 +1000330 }
Erik de Castro Lopo826b64f2015-11-07 07:06:23 +1100331#else
Josh Coalsonedade2c2001-05-23 20:53:31 +0000332 info->use_asm = false;
333#endif
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000334#endif
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000335}
Josh Coalson83f42e32007-03-03 01:52:19 +0000336
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000337static void
338x86_64_cpu_info (FLAC__CPUInfo *info)
339{
Erik de Castro Lopo85c902f2016-06-27 05:09:07 +1000340#if !defined FLAC__CPU_X86_64
Erik de Castro Lopo162a4492016-06-26 17:28:27 +1000341 (void) info;
Julian Calaby078c6312016-06-27 22:13:30 +1000342#elif defined(__ANDROID__) || defined(ANDROID)
343 /* no need to check OS SSE support */
344 info->use_asm = true;
Julian Calabye58a6e72016-06-27 22:17:03 +1000345#elif !defined FLAC__NO_ASM && FLAC__HAS_X86INTRIN
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000346 FLAC__bool x86_osxsave = false;
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000347 FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;
348
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000349 info->use_asm = true;
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000350
351 /* http://www.sandpile.org/x86/cpuid.htm */
352 FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
353 info->x86.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */
354 FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
355 info->x86.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false;
356 info->x86.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false;
357 info->x86.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false;
358 info->x86.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false;
359
360 if (FLAC__AVX_SUPPORTED) {
361 x86_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false;
362 info->x86.avx = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX ) ? true : false;
363 info->x86.fma = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA ) ? true : false;
364 FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
365 info->x86.avx2 = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2 ) ? true : false;
366 }
367
Julian Calaby601c0512016-06-27 22:08:51 +1000368 dfprintf(stderr, "CPU info (x86-64):\n");
369 dfprintf(stderr, " SSE3 ....... %c\n", info->x86.sse3 ? 'Y' : 'n');
370 dfprintf(stderr, " SSSE3 ...... %c\n", info->x86.ssse3 ? 'Y' : 'n');
371 dfprintf(stderr, " SSE41 ...... %c\n", info->x86.sse41 ? 'Y' : 'n');
372 dfprintf(stderr, " SSE42 ...... %c\n", info->x86.sse42 ? 'Y' : 'n');
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000373
Julian Calaby601c0512016-06-27 22:08:51 +1000374 if (FLAC__AVX_SUPPORTED) {
375 dfprintf(stderr, " AVX ........ %c\n", info->x86.avx ? 'Y' : 'n');
376 dfprintf(stderr, " FMA ........ %c\n", info->x86.fma ? 'Y' : 'n');
377 dfprintf(stderr, " AVX2 ....... %c\n", info->x86.avx2 ? 'Y' : 'n');
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000378 }
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000379
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000380 /*
381 * now have to check for OS support of AVX instructions
382 */
Julian Calaby42c5de62016-06-27 23:22:20 +1000383 if (!info->x86.avx || !x86_osxsave || (cpu_xgetbv_x86() & 0x6) != 0x6) {
384 /* no OS AVX support */
385 info->x86.avx = false;
386 info->x86.avx2 = false;
387 info->x86.fma = false;
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000388 }
Julian Calaby42c5de62016-06-27 23:22:20 +1000389
Erik de Castro Lopobba26a52016-06-28 07:12:42 +1000390 if (FLAC__AVX_SUPPORTED) {
Julian Calaby42c5de62016-06-27 23:22:20 +1000391 dfprintf(stderr, " AVX OS sup . %c\n", info->x86.avx ? 'Y' : 'n');
Erik de Castro Lopobba26a52016-06-28 07:12:42 +1000392 }
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000393#endif
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000394}
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000395
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000396void FLAC__cpu_info (FLAC__CPUInfo *info)
397{
398 memset(info, 0, sizeof(*info));
399
400#ifdef FLAC__CPU_IA32
401 info->type = FLAC__CPUINFO_TYPE_IA32;
402#elif defined FLAC__CPU_X86_64
403 info->type = FLAC__CPUINFO_TYPE_X86_64;
Josh Coalsonedade2c2001-05-23 20:53:31 +0000404#else
Josh Coalsonedade2c2001-05-23 20:53:31 +0000405 info->type = FLAC__CPUINFO_TYPE_UNKNOWN;
406 info->use_asm = false;
407#endif
Erik de Castro Lopo7a8f3592016-06-26 12:59:11 +1000408
409 switch (info->type) {
410 case FLAC__CPUINFO_TYPE_IA32:
411 ia32_cpu_info (info);
412 break;
413 case FLAC__CPUINFO_TYPE_X86_64:
414 x86_64_cpu_info (info);
415 break;
416 default:
417 info->use_asm = false;
418 break;
419 }
Josh Coalsonedade2c2001-05-23 20:53:31 +0000420}
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000421
Erik de Castro Lopo36a0ab12016-06-20 20:29:59 +1000422#if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
Erik de Castro Lopo84c3e3d2013-09-15 09:46:17 +1000423
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000424void 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 +1000425{
Erik de Castro Lopo57297ee2014-01-30 21:53:37 +1100426#if defined _MSC_VER || defined __INTEL_COMPILER
Erik de Castro Lopo8fe2c232013-09-25 23:05:13 +1000427 int cpuinfo[4];
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000428 int ext = level & 0x80000000;
429 __cpuid(cpuinfo, ext);
Julian Calaby79ca23e2016-06-27 22:37:45 +1000430 if((unsigned)cpuinfo[0] >= level) {
Julian Calabyeae338e2016-06-27 22:41:13 +1000431#if FLAC__AVX_SUPPORTED
432 __cpuidex(cpuinfo, ext, 0); /* for AVX2 detection */
433#else
434 __cpuid(cpuinfo, ext); /* some old compilers don't support __cpuidex */
435#endif
Julian Calaby79ca23e2016-06-27 22:37:45 +1000436
437 *eax = cpuinfo[0]; *ebx = cpuinfo[1]; *ecx = cpuinfo[2]; *edx = cpuinfo[3];
438
Erik de Castro Lopo2c150522014-06-28 21:50:09 +1000439 return;
440 }
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000441#elif defined __GNUC__ && defined HAVE_CPUID_H
442 FLAC__uint32 ext = level & 0x80000000;
443 __cpuid(ext, *eax, *ebx, *ecx, *edx);
Julian Calaby79ca23e2016-06-27 22:37:45 +1000444 if (*eax >= level) {
445 __cpuid_count(level, 0, *eax, *ebx, *ecx, *edx);
446
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000447 return;
448 }
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000449#endif
Julian Calaby79ca23e2016-06-27 22:37:45 +1000450 *eax = *ebx = *ecx = *edx = 0;
Erik de Castro Lopoa75b8702014-10-04 09:14:18 +1000451}
452
Erik de Castro Lopob8d58e32014-06-15 20:29:34 +1000453#endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */