-
Notifications
You must be signed in to change notification settings - Fork 0
/
amd64variant.go
140 lines (118 loc) · 2.31 KB
/
amd64variant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//go:build amd64
// +build amd64
package archvariant
import (
"fmt"
"sync"
)
var cacheOnce sync.Once
var amdVariantCache string
func cpuid(ax, cx uint32) (eax, ebx, ecx, edx uint32)
func xgetbv() (eax uint32)
const (
sse3 = 0
ssse3 = 9
cx16 = 13
sse4_1 = 19
sse4_2 = 20
popcnt = 23
fma = 12
movbe = 22
xsave = 26
osxsave = 27
avx = 28
f16c = 29
v2Features = 1<<sse3 | 1<<ssse3 | 1<<cx16 | 1<<sse4_1 | 1<<sse4_2 | 1<<popcnt
v3Features = v2Features | 1<<fma | 1<<movbe | 1<<xsave | 1<<osxsave | 1<<avx | 1<<f16c
)
const (
bmi1 = 3
avx2 = 5
bmi2 = 8
avx512F = 16
avx512Dq = 17
avx512Cd = 28
avx512Bw = 30
avx512Vl = 31
v3ExtFeaturesBX = 1<<bmi1 | 1<<avx2 | 1<<bmi2
v4ExtFeaturesBX = v3ExtFeaturesBX | 1<<avx512F | 1<<avx512Dq | 1<<avx512Cd | 1<<avx512Bw | 1<<avx512Vl
)
const (
lahfLm = 0
abm = 5
v2ExtFeatureCX = 1 << lahfLm
v3ExtFeatureCX = v2ExtFeatureCX | 1<<abm
)
const (
// XCR0
xmm = 1
ymm = 2
opmask = 5
zmmHi16 = 6
zmmHi256 = 7
v3OSSupport = 1<<xmm | 1<<ymm
v4OSSupport = v3OSSupport | 1<<opmask | 1<<zmmHi16 | 1<<zmmHi256
)
func detectVersion() int {
// highest basic calling parameter
ax, _, _, _ := cpuid(0, 0)
if ax < 7 {
return 1
}
// highest extended calling parameter
ax, _, _, _ = cpuid(0x80000000, 0)
if ax < 0x80000001 {
return 1
}
version := 4 // initialize to highest version
// feature bits
_, _, cx, _ := cpuid(1, 0)
if cx&v3Features != v3Features {
version = 2
if cx&v2Features != v2Features {
return 1
}
}
// extended features
_, bx, _, _ := cpuid(7, 0)
if version == 4 {
if bx&v4ExtFeaturesBX != v4ExtFeaturesBX {
version = 3
}
}
if version == 3 {
if bx&v3ExtFeaturesBX != v3ExtFeaturesBX {
version = 2
}
}
// extended processor info and feature bits
_, _, cx, _ = cpuid(0x80000001, 0)
if version >= 3 {
if cx&v3ExtFeatureCX != v3ExtFeatureCX {
version = 2
}
}
if version == 2 {
if cx&v2ExtFeatureCX != v2ExtFeatureCX {
return 1
}
}
if version >= 3 {
ax = xgetbv()
if version == 4 {
if !osAVX512Supported(ax) {
version = 3
}
}
if ax&v3OSSupport != v3OSSupport {
version = 2
}
}
return version
}
func AMD64Variant() string {
cacheOnce.Do(func() {
amdVariantCache = "v" + fmt.Sprintf("%d", detectVersion())
})
return amdVariantCache
}