cpu.h 5.81 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-2016  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
34
35
36
37
 */

#ifndef FLAC__PRIVATE__CPU_H
#define FLAC__PRIVATE__CPU_H

#include "FLAC/ordinals.h"

Josh Coalson's avatar
Josh Coalson committed
38
39
40
41
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef FLAC__CPU_X86_64

#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
#define FLAC__CPU_X86_64
#endif

#endif

#ifndef FLAC__CPU_IA32

#if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) ||defined( __i386) || defined(_M_IX86)
#define FLAC__CPU_IA32
#endif

#endif

Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
58
59
60
#ifndef __has_attribute
#define __has_attribute(x) 0
#endif
61

62
#if FLAC__HAS_X86INTRIN
63
64
65
66
67
68
69
70
71
/* SSE intrinsics support by ICC/MSVC/GCC */
#if defined __INTEL_COMPILER
  #define FLAC__SSE_TARGET(x)
  #define FLAC__SSE_SUPPORTED 1
  #define FLAC__SSE2_SUPPORTED 1
  #if (__INTEL_COMPILER >= 1000) /* Intel C++ Compiler 10.0 */
    #define FLAC__SSSE3_SUPPORTED 1
    #define FLAC__SSE4_1_SUPPORTED 1
  #endif
72
73
74
75
76
77
78
  #if (__INTEL_COMPILER >= 1110) /* Intel C++ Compiler 11.1 */
    #define FLAC__AVX_SUPPORTED 1
  #endif
  #if (__INTEL_COMPILER >= 1300) /* Intel C++ Compiler 13.0 */
    #define FLAC__AVX2_SUPPORTED 1
    #define FLAC__FMA_SUPPORTED 1
  #endif
79
80
81
82
83
84
85
86
#elif defined _MSC_VER
  #define FLAC__SSE_TARGET(x)
  #define FLAC__SSE_SUPPORTED 1
  #define FLAC__SSE2_SUPPORTED 1
  #if (_MSC_VER >= 1500) /* MS Visual Studio 2008 */
    #define FLAC__SSSE3_SUPPORTED 1
    #define FLAC__SSE4_1_SUPPORTED 1
  #endif
87
88
89
90
91
92
93
  #if (_MSC_FULL_VER >= 160040219) /* MS Visual Studio 2010 SP1 */
    #define FLAC__AVX_SUPPORTED 1
  #endif
  #if (_MSC_VER >= 1700) /* MS Visual Studio 2012 */
    #define FLAC__AVX2_SUPPORTED 1
    #define FLAC__FMA_SUPPORTED 1
  #endif
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
94
95
96
#elif defined __clang__ && __has_attribute(__target__) /* clang */
  #define FLAC__SSE_TARGET(x) __attribute__ ((__target__ (x)))
  #if __has_builtin(__builtin_ia32_maxps)
97
    #define FLAC__SSE_SUPPORTED 1
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
98
99
  #endif
  #if __has_builtin(__builtin_ia32_pmuludq128)
100
    #define FLAC__SSE2_SUPPORTED 1
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
101
102
  #endif
  #if __has_builtin(__builtin_ia32_pabsd128)
103
    #define FLAC__SSSE3_SUPPORTED 1
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
104
105
  #endif
  #if __has_builtin(__builtin_ia32_pmuldq128)
106
    #define FLAC__SSE4_1_SUPPORTED 1
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
107
108
109
110
111
112
113
114
115
116
117
  #endif
  #if __has_builtin(__builtin_ia32_pabsd256)
    #define FLAC__AVX2_SUPPORTED 1
  #endif
#elif defined __GNUC__ && !defined __clang__ && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) /* GCC 4.9+ */
  #define FLAC__SSE_TARGET(x) __attribute__ ((__target__ (x)))
  #define FLAC__SSE_SUPPORTED 1
  #define FLAC__SSE2_SUPPORTED 1
  #define FLAC__SSSE3_SUPPORTED 1
  #define FLAC__SSE4_1_SUPPORTED 1
  #ifdef FLAC__USE_AVX
118
119
120
    #define FLAC__AVX_SUPPORTED 1
    #define FLAC__AVX2_SUPPORTED 1
    #define FLAC__FMA_SUPPORTED 1
Erik de Castro Lopo's avatar
Erik de Castro Lopo committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  #endif
#else
  #define FLAC__SSE_TARGET(x)
  #ifdef __SSE__
    #define FLAC__SSE_SUPPORTED 1
  #endif
  #ifdef __SSE2__
    #define FLAC__SSE2_SUPPORTED 1
  #endif
  #ifdef __SSSE3__
    #define FLAC__SSSE3_SUPPORTED 1
  #endif
  #ifdef __SSE4_1__
    #define FLAC__SSE4_1_SUPPORTED 1
  #endif
  #ifdef __AVX__
    #define FLAC__AVX_SUPPORTED 1
  #endif
  #ifdef __AVX2__
    #define FLAC__AVX2_SUPPORTED 1
  #endif
  #ifdef __FMA__
    #define FLAC__FMA_SUPPORTED 1
144
  #endif
145
#endif /* compiler version */
146
#endif /* intrinsics support */
147

148
149
150
151
152

#ifndef FLAC__AVX_SUPPORTED
#define FLAC__AVX_SUPPORTED 0
#endif

Josh Coalson's avatar
Josh Coalson committed
153
154
typedef enum {
	FLAC__CPUINFO_TYPE_IA32,
155
	FLAC__CPUINFO_TYPE_X86_64,
Josh Coalson's avatar
Josh Coalson committed
156
157
158
159
	FLAC__CPUINFO_TYPE_UNKNOWN
} FLAC__CPUInfo_Type;

typedef struct {
160
161
	FLAC__bool intel;

Josh Coalson's avatar
Josh Coalson committed
162
163
164
165
	FLAC__bool cmov;
	FLAC__bool mmx;
	FLAC__bool sse;
	FLAC__bool sse2;
166

167
168
	FLAC__bool sse3;
	FLAC__bool ssse3;
169
170
	FLAC__bool sse41;
	FLAC__bool sse42;
171
172
173
	FLAC__bool avx;
	FLAC__bool avx2;
	FLAC__bool fma;
Josh Coalson's avatar
Josh Coalson committed
174
} FLAC__CPUInfo_IA32;
175

176
typedef struct {
177
178
	FLAC__bool intel;

179
180
	FLAC__bool sse3;
	FLAC__bool ssse3;
181
182
	FLAC__bool sse41;
	FLAC__bool sse42;
183
184
185
	FLAC__bool avx;
	FLAC__bool avx2;
	FLAC__bool fma;
186
} FLAC__CPUInfo_x86;
187

188

Josh Coalson's avatar
Josh Coalson committed
189
typedef struct {
Josh Coalson's avatar
Josh Coalson committed
190
	FLAC__bool use_asm;
Josh Coalson's avatar
Josh Coalson committed
191
	FLAC__CPUInfo_Type type;
192
	FLAC__CPUInfo_IA32 ia32;
193
	FLAC__CPUInfo_x86 x86;
Josh Coalson's avatar
Josh Coalson committed
194
195
196
197
} FLAC__CPUInfo;

void FLAC__cpu_info(FLAC__CPUInfo *info);

198
FLAC__uint32 FLAC__cpu_have_cpuid_asm_ia32(void);
199

200
void         FLAC__cpu_info_asm_ia32(FLAC__uint32 *flags_edx, FLAC__uint32 *flags_ecx);
201

202
void         FLAC__cpu_info_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx, FLAC__uint32 *ecx, FLAC__uint32 *edx);
Josh Coalson's avatar
Josh Coalson committed
203
204

#endif