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