cpu.c 16.5 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* libFLAC - Free Lossless Audio Codec library
2
 * Copyright (C) 2001-2009  Josh Coalson
3
 * Copyright (C) 2011-2014  Xiph.Org Foundation
Josh Coalson's avatar
Josh Coalson committed
4
 *
5
6
7
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Josh Coalson's avatar
Josh Coalson committed
8
 *
9
10
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
Josh Coalson's avatar
Josh Coalson committed
11
 *
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Xiph.org Foundation nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson's avatar
Josh Coalson committed
31
32
 */

33
#ifdef HAVE_CONFIG_H
Josh Coalson's avatar
Josh Coalson committed
34
35
36
#  include <config.h>
#endif

Josh Coalson's avatar
Josh Coalson committed
37
#include "private/cpu.h"
38
#include "share/compat.h"
39
#include <stdlib.h>
40
#include <memory.h>
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#if defined (__NetBSD__) || defined(__OpenBSD__)
#  include <sys/param.h>
#  include <sys/sysctl.h>
#  include <machine/cpu.h>
#endif

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
#  include <sys/types.h>
#  include <sys/sysctl.h>
#endif

#if  defined(__linux__) && defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) && !FLAC__SSE_OS
#  include <sys/ucontext.h>
#endif

#if defined(_MSC_VER)
#  include <windows.h>
#  include <intrin.h> /* for __cpuid() and _xgetbv() */
#endif

#if defined __GNUC__ && defined HAVE_CPUID_H
#  include <cpuid.h> /* for __get_cpuid() and __get_cpuid_max() */
64
#endif
Josh Coalson's avatar
Josh Coalson committed
65

66
#ifdef DEBUG
67
68
#include <stdio.h>

69
#define dfprintf fprintf
70
#else
71
72
/* This is bad practice, it should be a static void empty function */
#define dfprintf(file, format, args...)
73
74
75
#endif


76
77
78
79
80
81
/* these are flags in EDX of CPUID AX=00000001 */
static const unsigned FLAC__CPUINFO_IA32_CPUID_CMOV = 0x00008000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_MMX = 0x00800000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_FXSR = 0x01000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE = 0x02000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE2 = 0x04000000;
82

83
84
85
/* these are flags in ECX of CPUID AX=00000001 */
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE3 = 0x00000001;
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSSE3 = 0x00000200;
86
87
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE41 = 0x00080000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE42 = 0x00100000;
Josh Coalson's avatar
Josh Coalson committed
88

89
90
91
92
93
94
95
/* these are flags in ECX of CPUID AX=00000001 */
static const unsigned FLAC__CPUINFO_IA32_CPUID_OSXSAVE = 0x08000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX = 0x10000000;
static const unsigned FLAC__CPUINFO_IA32_CPUID_FMA = 0x00001000;
/* these are flags in EBX of CPUID AX=00000007 */
static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX2 = 0x00000020;

96
97
98
/*
 * Extra stuff needed for detection of OS support for SSE on IA-32
 */
99
#if  defined(__linux__) && defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) && !FLAC__SSE_OS
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * If the OS doesn't support SSE, we will get here with a SIGILL.  We
 * modify the return address to jump over the offending SSE instruction
 * and also the operation following it that indicates the instruction
 * executed successfully.  In this way we use no global variables and
 * stay thread-safe.
 *
 * 3 + 3 + 6:
 *   3 bytes for "xorps xmm0,xmm0"
 *   3 bytes for estimate of how long the follwing "inc var" instruction is
 *   6 bytes extra in case our estimate is wrong
 * 12 bytes puts us in the NOP "landing zone"
 */
113
114
115
116
117
static void sigill_handler_sse_os(int signal, siginfo_t *si, void *uc)
{
	(void)signal, (void)si;
	((ucontext_t*)uc)->uc_mcontext.gregs[14/*REG_EIP*/] += 3 + 3 + 6;
}
118
119
#endif

120
#if defined FLAC__CPU_IA32
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
static void
ia32_disable_sse(FLAC__CPUInfo *info)
{
	info->ia32.sse   = false;
	info->ia32.sse2  = false;
	info->ia32.sse3  = false;
	info->ia32.ssse3 = false;
	info->ia32.sse41 = false;
	info->ia32.sse42 = false;
}

static void
ia32_disable_avx(FLAC__CPUInfo *info)
{
	info->ia32.avx     = false;
	info->ia32.avx2    = false;
	info->ia32.fma     = false;
}
139
#endif
140

141
#if defined FLAC__CPU_X86_64
142
143
144
145
146
147
148
static void
x86_64_disable_avx(FLAC__CPUInfo *info)
{
	info->x86.avx     = false;
	info->x86.avx2    = false;
	info->x86.fma     = false;
}
149
#endif
150

151
#if defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64
152
153
static uint32_t
cpu_xgetbv_x86(void)
154
{
155
#if (defined _MSC_VER || defined __INTEL_COMPILER) && FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED
156
157
158
159
160
161
162
163
164
	return (uint32_t)_xgetbv(0);
#elif defined __GNUC__
	uint32_t lo, hi;
	asm volatile (".byte 0x0f, 0x01, 0xd0" : "=a"(lo), "=d"(hi) : "c" (0));
	return lo;
#else
	return 0;
#endif
}
165
#endif
166

167
168
static void
ia32_cpu_info (FLAC__CPUInfo *info)
Josh Coalson's avatar
Josh Coalson committed
169
{
170
#if !defined FLAC__CPU_IA32
171
	(void) info;
172
173
174
#elif defined(__ANDROID__) || defined(ANDROID)
	/* no need to check OS SSE support */
	info->use_asm = true;
175
#else
176
	FLAC__bool ia32_fxsr = false;
177
	FLAC__bool ia32_osxsave = false;
178
179
	FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;

180
#if !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN)
181
	info->use_asm = true; /* we assume a minimum of 80386 with FLAC__CPU_IA32 */
182
#if !FLAC__HAS_X86INTRIN
183
	if(!FLAC__cpu_have_cpuid_asm_ia32())
184
		return;
185
#endif
186
187
188
189
190
191
192
193
	/* http://www.sandpile.org/x86/cpuid.htm */
    if (FLAC__HAS_X86INTRIN) {
		FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
		info->ia32.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */
		FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
	}
	else {
		FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx);
194
	}
195

196
197
198
199
200
201
202
203
204
205
	info->ia32.cmov  = (flags_edx & FLAC__CPUINFO_IA32_CPUID_CMOV ) ? true : false;
	info->ia32.mmx   = (flags_edx & FLAC__CPUINFO_IA32_CPUID_MMX  ) ? true : false;
	      ia32_fxsr  = (flags_edx & FLAC__CPUINFO_IA32_CPUID_FXSR ) ? true : false;
	info->ia32.sse   = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE  ) ? true : false;
	info->ia32.sse2  = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE2 ) ? true : false;
	info->ia32.sse3  = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false;
	info->ia32.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false;
	info->ia32.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false;
	info->ia32.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false;

206
    if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
207
208
209
210
211
212
213
	    ia32_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false;
		info->ia32.avx   = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX    ) ? true : false;
		info->ia32.fma   = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA    ) ? true : false;
		FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
		info->ia32.avx2  = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2   ) ? true : false;
	}

214
215
216
217
218
219
220
221
222
223
224
225
226
227
	dfprintf(stderr, "CPU info (IA-32):\n");
	dfprintf(stderr, "  CMOV ....... %c\n", info->ia32.cmov    ? 'Y' : 'n');
	dfprintf(stderr, "  MMX ........ %c\n", info->ia32.mmx     ? 'Y' : 'n');
	dfprintf(stderr, "  SSE ........ %c\n", info->ia32.sse     ? 'Y' : 'n');
	dfprintf(stderr, "  SSE2 ....... %c\n", info->ia32.sse2    ? 'Y' : 'n');
	dfprintf(stderr, "  SSE3 ....... %c\n", info->ia32.sse3    ? 'Y' : 'n');
	dfprintf(stderr, "  SSSE3 ...... %c\n", info->ia32.ssse3   ? 'Y' : 'n');
	dfprintf(stderr, "  SSE41 ...... %c\n", info->ia32.sse41   ? 'Y' : 'n');
	dfprintf(stderr, "  SSE42 ...... %c\n", info->ia32.sse42   ? 'Y' : 'n');

	if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) {
		dfprintf(stderr, "  AVX ........ %c\n", info->ia32.avx     ? 'Y' : 'n');
		dfprintf(stderr, "  FMA ........ %c\n", info->ia32.fma     ? 'Y' : 'n');
		dfprintf(stderr, "  AVX2 ....... %c\n", info->ia32.avx2    ? 'Y' : 'n');
228
	}
229

230
231
232
233
	/*
	 * now have to check for OS support of SSE instructions
	 */
	if(info->ia32.sse) {
234
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
235
		int sse = 0;
236
		size_t len = sizeof(sse);
237
		/* at least one of these must work: */
238
239
		sse = sse || (sysctlbyname("hw.instruction_sse", &sse, &len, NULL, 0) == 0 && sse);
		sse = sse || (sysctlbyname("hw.optional.sse"   , &sse, &len, NULL, 0) == 0 && sse); /* __APPLE__ ? */
240
		if(!sse)
241
			ia32_disable_sse(info);
242
#elif defined(__NetBSD__) || defined (__OpenBSD__)
243
244
245
		int val = 0, mib[2] = { CTL_MACHDEP, CPU_SSE };
		size_t len = sizeof(val);
		if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val)
246
			ia32_disable_sse(info);
247
248
249
250
		else { /* double-check SSE2 */
			mib[1] = CPU_SSE2;
			len = sizeof(val);
			if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val) {
251
				ia32_disable_sse(info);
252
				info->ia32.sse = true;
253
			}
254
		}
255
#elif defined(__linux__) && !FLAC__SSE_OS
256
257
258
259
		int sse = 0;
		struct sigaction sigill_save;
		struct sigaction sigill_sse;
		sigill_sse.sa_sigaction = sigill_handler_sse_os;
260
		sigemptyset(&sigill_sse.sa_mask);
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
		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 */
		if(0 == sigaction(SIGILL, &sigill_sse, &sigill_save))
		{
			/* http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html */
			/* see sigill_handler_sse_os() for an explanation of the following: */
			asm volatile (
				"xorps %%xmm0,%%xmm0\n\t" /* will cause SIGILL if unsupported by OS */
				"incl %0\n\t"             /* SIGILL handler will jump over this */
				/* landing zone */
				"nop\n\t" /* SIGILL jump lands here if "inc" is 9 bytes */
				"nop\n\t"
				"nop\n\t"
				"nop\n\t"
				"nop\n\t"
				"nop\n\t"
				"nop\n\t" /* SIGILL jump lands here if "inc" is 3 bytes (expected) */
				"nop\n\t"
				"nop"     /* SIGILL jump lands here if "inc" is 1 byte */
				: "=r"(sse)
				: "0"(sse)
			);
282

283
284
			sigaction(SIGILL, &sigill_save, NULL);
		}
285

286
		if(!sse)
287
			ia32_disable_sse(info);
288
#elif defined(_MSC_VER)
289
290
291
		__try {
			__asm {
				xorps xmm0,xmm0
292
			}
293
294
295
		}
		__except(EXCEPTION_EXECUTE_HANDLER) {
			if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION)
296
				ia32_disable_sse(info);
297
		}
298
#elif defined(__GNUC__) /* MinGW goes here */
299
300
301
		int sse = 0;
		/* Based on the idea described in Agner Fog's manual "Optimizing subroutines in assembly language" */
		/* In theory, not guaranteed to detect lack of OS SSE support on some future Intel CPUs, but in practice works (see the aforementioned manual) */
302
		if (ia32_fxsr) {
303
304
305
306
			struct {
				FLAC__uint32 buff[128];
			} __attribute__((aligned(16))) fxsr;
			FLAC__uint32 old_val, new_val;
307

308
309
			memset(fxsr.buff, 0, sizeof (fxsr.buff));

310
311
312
313
314
			asm volatile ("fxsave %0"  : "=m" (fxsr) : "m" (fxsr));
			old_val = fxsr.buff[50];
			fxsr.buff[50] ^= 0x0013c0de;                             /* change value in the buffer */
			asm volatile ("fxrstor %0" : "=m" (fxsr) : "m" (fxsr));  /* try to change SSE register */
			fxsr.buff[50] = old_val;                                 /* restore old value in the buffer */
315
			asm volatile ("fxsave %0"  : "=m" (fxsr) : "m" (fxsr));  /* old value will be overwritten if SSE register was changed */
316
317
318
			new_val = fxsr.buff[50];                                 /* == old_val if FXRSTOR didn't change SSE register and (old_val ^ 0x0013c0de) otherwise */
			fxsr.buff[50] = old_val;                                 /* again restore old value in the buffer */
			asm volatile ("fxrstor %0" : "=m" (fxsr) : "m" (fxsr));  /* restore old values of registers */
319

320
321
322
323
			if ((old_val^new_val) == 0x0013c0de)
				sse = 1;
		}
		if(!sse)
324
			ia32_disable_sse(info);
325
326
#else
		/* no way to test, disable to be safe */
327
		ia32_disable_sse(info);
328
#endif
329
		dfprintf(stderr, "  SSE OS sup . %c\n", info->ia32.sse ? 'Y' : 'n');
Josh Coalson's avatar
Josh Coalson committed
330
	}
331
	else /* info->ia32.sse == false */
332
		ia32_disable_sse(info);
333

334
335
336
	/*
	 * now have to check for OS support of AVX instructions
	 */
337
	if (FLAC__HAS_X86INTRIN && info->ia32.avx && ia32_osxsave) {
338
		FLAC__uint32 ecr = cpu_xgetbv_x86();
339
		if ((ecr & 0x6) != 0x6)
340
			ia32_disable_avx(info);
341
342

		dfprintf(stderr, "  AVX OS sup . %c\n", info->ia32.avx ? 'Y' : 'n');
343
	}
344
	else /* no OS AVX support */
345
346
		ia32_disable_avx(info);

Josh Coalson's avatar
Josh Coalson committed
347
348
349
#else
	info->use_asm = false;
#endif
350
#endif
351
}
352

353
354
355
static void
x86_64_cpu_info (FLAC__CPUInfo *info)
{
356
#if !defined FLAC__CPU_X86_64
357
	(void) info;
358
359
360
#elif defined(__ANDROID__) || defined(ANDROID)
	/* no need to check OS SSE support */
	info->use_asm = true;
361
#elif !defined FLAC__NO_ASM && FLAC__HAS_X86INTRIN
362
	FLAC__bool x86_osxsave = false;
363
364
	FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx;

365
	info->use_asm = true;
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

	/* http://www.sandpile.org/x86/cpuid.htm */
	FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
	info->x86.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */
	FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
	info->x86.sse3  = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false;
	info->x86.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false;
	info->x86.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false;
	info->x86.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false;

	if (FLAC__AVX_SUPPORTED) {
	    x86_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false;
		info->x86.avx   = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX    ) ? true : false;
		info->x86.fma   = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA    ) ? true : false;
		FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx);
		info->x86.avx2  = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2   ) ? true : false;
	}

384
385
386
387
388
389
390
391
392
393
	dfprintf(stderr, "CPU info (x86-64):\n");
	dfprintf(stderr, "  SSE3 ....... %c\n", info->x86.sse3  ? 'Y' : 'n');
	dfprintf(stderr, "  SSSE3 ...... %c\n", info->x86.ssse3 ? 'Y' : 'n');
	dfprintf(stderr, "  SSE41 ...... %c\n", info->x86.sse41 ? 'Y' : 'n');
	dfprintf(stderr, "  SSE42 ...... %c\n", info->x86.sse42 ? 'Y' : 'n');

	if (FLAC__AVX_SUPPORTED) {
		dfprintf(stderr, "  AVX ........ %c\n", info->x86.avx   ? 'Y' : 'n');
		dfprintf(stderr, "  FMA ........ %c\n", info->x86.fma   ? 'Y' : 'n');
		dfprintf(stderr, "  AVX2 ....... %c\n", info->x86.avx2  ? 'Y' : 'n');
394
395
	}

396
397
398
	/*
	 * now have to check for OS support of AVX instructions
	 */
399
	if (info->x86.avx && x86_osxsave) {
400
		FLAC__uint32 ecr = cpu_xgetbv_x86();
401
		if ((ecr & 0x6) != 0x6)
402
			x86_64_disable_avx(info);
403
404

		dfprintf(stderr, "  AVX OS sup . %c\n", info->x86.avx ? 'Y' : 'n');
405
	}
406
	else /* no OS AVX support */
407
		x86_64_disable_avx(info);
408
#endif
409
}
410

411
412
413
414
415
416
417
418
void FLAC__cpu_info (FLAC__CPUInfo *info)
{
	memset(info, 0, sizeof(*info));

#ifdef FLAC__CPU_IA32
	info->type = FLAC__CPUINFO_TYPE_IA32;
#elif defined FLAC__CPU_X86_64
	info->type = FLAC__CPUINFO_TYPE_X86_64;
Josh Coalson's avatar
Josh Coalson committed
419
420
421
422
#else
	info->type = FLAC__CPUINFO_TYPE_UNKNOWN;
	info->use_asm = false;
#endif
423
424
425
426
427
428
429
430
431
432
433
434

	switch (info->type) {
	case FLAC__CPUINFO_TYPE_IA32:
		ia32_cpu_info (info);
		break;
	case FLAC__CPUINFO_TYPE_X86_64:
		x86_64_cpu_info (info);
		break;
	default:
		info->use_asm = false;
		break;
	}
Josh Coalson's avatar
Josh Coalson committed
435
}
436

437
#if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
438

439
void FLAC__cpu_info_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx, FLAC__uint32 *ecx, FLAC__uint32 *edx)
440
{
441
#if defined _MSC_VER || defined __INTEL_COMPILER
442
	int cpuinfo[4];
443
444
	int ext = level & 0x80000000;
	__cpuid(cpuinfo, ext);
445
	if((unsigned)cpuinfo[0] >= level) {
446
447
448
449
450
#if FLAC__AVX_SUPPORTED
		__cpuidex(cpuinfo, ext, 0); /* for AVX2 detection */
#else
		__cpuid(cpuinfo, ext); /* some old compilers don't support __cpuidex */
#endif
451

452
		*eax = cpuinfo[0]; *ebx = cpuinfo[1]; *ecx = cpuinfo[2]; *edx = cpuinfo[3];
453

454
455
		return;
	}
456
#elif defined __GNUC__ && defined HAVE_CPUID_H
457
458
	FLAC__uint32 ext = level & 0x80000000;
	__cpuid(ext, *eax, *ebx, *ecx, *edx);
459
460
461
	if (*eax >= level) {
		__cpuid_count(level, 0, *eax, *ebx, *ecx, *edx);

462
463
464
		return;
	}
#endif
465
	*eax = *ebx = *ecx = *edx = 0;
466
467
}

468
#endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */