1
2
3
4
5
6
7 package cpu
8
9 const CacheLinePadSize = 64
10
11
12 func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32)
13
14
15 func xgetbv() (eax, edx uint32)
16
17
18 func getGOAMD64level() int32
19
20 const (
21
22 cpuid_SSE3 = 1 << 0
23 cpuid_PCLMULQDQ = 1 << 1
24 cpuid_SSSE3 = 1 << 9
25 cpuid_FMA = 1 << 12
26 cpuid_SSE41 = 1 << 19
27 cpuid_SSE42 = 1 << 20
28 cpuid_POPCNT = 1 << 23
29 cpuid_AES = 1 << 25
30 cpuid_OSXSAVE = 1 << 27
31 cpuid_AVX = 1 << 28
32
33
34 cpuid_BMI1 = 1 << 3
35 cpuid_AVX2 = 1 << 5
36 cpuid_BMI2 = 1 << 8
37 cpuid_ERMS = 1 << 9
38 cpuid_AVX512F = 1 << 16
39 cpuid_AVX512DQ = 1 << 17
40 cpuid_ADX = 1 << 19
41 cpuid_AVX512CD = 1 << 28
42 cpuid_SHA = 1 << 29
43 cpuid_AVX512BW = 1 << 30
44 cpuid_AVX512VL = 1 << 31
45
46
47 cpuid_AVX512_VBMI = 1 << 1
48 cpuid_AVX512_VBMI2 = 1 << 6
49 cpuid_GFNI = 1 << 8
50 cpuid_AVX512VPCLMULQDQ = 1 << 10
51 cpuid_AVX512_BITALG = 1 << 12
52
53
54 cpuid_FSRM = 1 << 4
55
56 cpuid_RDTSCP = 1 << 27
57 )
58
59 var maxExtendedFunctionInformation uint32
60
61 func doinit() {
62 options = []option{
63 {Name: "adx", Feature: &X86.HasADX},
64 {Name: "aes", Feature: &X86.HasAES},
65 {Name: "erms", Feature: &X86.HasERMS},
66 {Name: "fsrm", Feature: &X86.HasFSRM},
67 {Name: "pclmulqdq", Feature: &X86.HasPCLMULQDQ},
68 {Name: "rdtscp", Feature: &X86.HasRDTSCP},
69 {Name: "sha", Feature: &X86.HasSHA},
70 {Name: "vpclmulqdq", Feature: &X86.HasAVX512VPCLMULQDQ},
71 }
72 level := getGOAMD64level()
73 if level < 2 {
74
75
76 options = append(options,
77 option{Name: "popcnt", Feature: &X86.HasPOPCNT},
78 option{Name: "sse3", Feature: &X86.HasSSE3},
79 option{Name: "sse41", Feature: &X86.HasSSE41},
80 option{Name: "sse42", Feature: &X86.HasSSE42},
81 option{Name: "ssse3", Feature: &X86.HasSSSE3})
82 }
83 if level < 3 {
84
85
86 options = append(options,
87 option{Name: "avx", Feature: &X86.HasAVX},
88 option{Name: "avx2", Feature: &X86.HasAVX2},
89 option{Name: "bmi1", Feature: &X86.HasBMI1},
90 option{Name: "bmi2", Feature: &X86.HasBMI2},
91 option{Name: "fma", Feature: &X86.HasFMA})
92 }
93 if level < 4 {
94
95
96 options = append(options,
97 option{Name: "avx512f", Feature: &X86.HasAVX512F},
98 option{Name: "avx512cd", Feature: &X86.HasAVX512CD},
99 option{Name: "avx512bw", Feature: &X86.HasAVX512BW},
100 option{Name: "avx512dq", Feature: &X86.HasAVX512DQ},
101 option{Name: "avx512vl", Feature: &X86.HasAVX512VL},
102 )
103 }
104
105 maxID, _, _, _ := cpuid(0, 0)
106
107 if maxID < 1 {
108 return
109 }
110
111 maxExtendedFunctionInformation, _, _, _ = cpuid(0x80000000, 0)
112
113 _, _, ecx1, _ := cpuid(1, 0)
114
115 X86.HasSSE3 = isSet(ecx1, cpuid_SSE3)
116 X86.HasPCLMULQDQ = isSet(ecx1, cpuid_PCLMULQDQ)
117 X86.HasSSSE3 = isSet(ecx1, cpuid_SSSE3)
118 X86.HasSSE41 = isSet(ecx1, cpuid_SSE41)
119 X86.HasSSE42 = isSet(ecx1, cpuid_SSE42)
120 X86.HasPOPCNT = isSet(ecx1, cpuid_POPCNT)
121 X86.HasAES = isSet(ecx1, cpuid_AES)
122
123
124
125
126 X86.HasOSXSAVE = isSet(ecx1, cpuid_OSXSAVE)
127
128
129
130
131
132 X86.HasFMA = isSet(ecx1, cpuid_FMA) && X86.HasOSXSAVE
133
134 osSupportsAVX := false
135 osSupportsAVX512 := false
136
137 if X86.HasOSXSAVE {
138 eax, _ := xgetbv()
139
140 osSupportsAVX = isSet(eax, 1<<1) && isSet(eax, 1<<2)
141
142
143
144
145
146 osSupportsAVX512 = osSupportsAVX && isSet(eax, 1<<5) && isSet(eax, 1<<6) && isSet(eax, 1<<7)
147 }
148
149 X86.HasAVX = isSet(ecx1, cpuid_AVX) && osSupportsAVX
150
151 if maxID < 7 {
152 return
153 }
154
155 _, ebx7, ecx7, edx7 := cpuid(7, 0)
156 X86.HasBMI1 = isSet(ebx7, cpuid_BMI1)
157 X86.HasAVX2 = isSet(ebx7, cpuid_AVX2) && osSupportsAVX
158 X86.HasBMI2 = isSet(ebx7, cpuid_BMI2)
159 X86.HasERMS = isSet(ebx7, cpuid_ERMS)
160 X86.HasADX = isSet(ebx7, cpuid_ADX)
161 X86.HasSHA = isSet(ebx7, cpuid_SHA)
162
163 X86.HasAVX512F = isSet(ebx7, cpuid_AVX512F) && osSupportsAVX512
164 if X86.HasAVX512F {
165 X86.HasAVX512CD = isSet(ebx7, cpuid_AVX512CD)
166 X86.HasAVX512BW = isSet(ebx7, cpuid_AVX512BW)
167 X86.HasAVX512DQ = isSet(ebx7, cpuid_AVX512DQ)
168 X86.HasAVX512VL = isSet(ebx7, cpuid_AVX512VL)
169 X86.HasAVX512VPCLMULQDQ = isSet(ecx7, cpuid_AVX512VPCLMULQDQ)
170 X86.HasAVX512VBMI = isSet(ecx7, cpuid_AVX512_VBMI)
171 X86.HasAVX512VBMI2 = isSet(ecx7, cpuid_AVX512_VBMI2)
172 X86.HasGFNI = isSet(ecx7, cpuid_GFNI)
173 X86.HasAVX512BITALG = isSet(ecx7, cpuid_AVX512_BITALG)
174 }
175
176 X86.HasFSRM = isSet(edx7, cpuid_FSRM)
177
178 var maxExtendedInformation uint32
179 maxExtendedInformation, _, _, _ = cpuid(0x80000000, 0)
180
181 if maxExtendedInformation < 0x80000001 {
182 return
183 }
184
185 _, _, _, edxExt1 := cpuid(0x80000001, 0)
186 X86.HasRDTSCP = isSet(edxExt1, cpuid_RDTSCP)
187
188 doDerived = func() {
189
190
191
192
193
194
195
196 X86.HasAVX512 = X86.HasAVX512F && X86.HasAVX512CD && X86.HasAVX512BW && X86.HasAVX512DQ && X86.HasAVX512VL
197 }
198 }
199
200 func isSet(hwc uint32, value uint32) bool {
201 return hwc&value != 0
202 }
203
204
205
206
207 func Name() string {
208 if maxExtendedFunctionInformation < 0x80000004 {
209 return ""
210 }
211
212 data := make([]byte, 0, 3*4*4)
213
214 var eax, ebx, ecx, edx uint32
215 eax, ebx, ecx, edx = cpuid(0x80000002, 0)
216 data = appendBytes(data, eax, ebx, ecx, edx)
217 eax, ebx, ecx, edx = cpuid(0x80000003, 0)
218 data = appendBytes(data, eax, ebx, ecx, edx)
219 eax, ebx, ecx, edx = cpuid(0x80000004, 0)
220 data = appendBytes(data, eax, ebx, ecx, edx)
221
222
223 for len(data) > 0 && data[0] == ' ' {
224 data = data[1:]
225 }
226
227
228 for i, c := range data {
229 if c == '\x00' {
230 data = data[:i]
231 break
232 }
233 }
234
235 return string(data)
236 }
237
238 func appendBytes(b []byte, args ...uint32) []byte {
239 for _, arg := range args {
240 b = append(b,
241 byte((arg >> 0)),
242 byte((arg >> 8)),
243 byte((arg >> 16)),
244 byte((arg >> 24)))
245 }
246 return b
247 }
248
View as plain text