@@ -106,12 +106,12 @@ func getCPUVariantFromArch(arch string) (string, error) {
106106 return variant , nil
107107}
108108
109- // getCPUVariant returns cpu variant for ARM
109+ // getArmCPUVariant returns cpu variant for ARM
110110// We first try reading "Cpu architecture" field from /proc/cpuinfo
111111// If we can't find it, then fall back using a system call
112112// This is to cover running ARM in emulated environment on x86 host as this field in /proc/cpuinfo
113113// was not present.
114- func getCPUVariant () (string , error ) {
114+ func getArmCPUVariant () (string , error ) {
115115 variant , err := getCPUInfo ("Cpu architecture" )
116116 if err != nil {
117117 if errors .Is (err , errNotFound ) {
@@ -158,3 +158,40 @@ func getCPUVariant() (string, error) {
158158
159159 return variant , nil
160160}
161+
162+ func getAmd64MicroArchLevel () (string , error ) {
163+ flags , err := getCPUInfo ("flags" )
164+ if errors .Is (err , errNotFound ) {
165+ return "" , fmt .Errorf ("failure getting CPU flags: %v" , err )
166+ }
167+
168+ containsAll := func (set map [string ]interface {}, toMatch []string ) bool {
169+ for _ , m := range toMatch {
170+ if _ , ok := set [m ]; ! ok {
171+ return false
172+ }
173+ }
174+ return true
175+ }
176+
177+ flagSet := map [string ]interface {}{}
178+ for _ , flag := range strings .Split (flags , " " ) {
179+ flagSet [flag ] = true
180+ }
181+
182+ // https://unix.stackexchange.com/questions/631217/how-do-i-check-if-my-cpu-supports-x86-64-v2
183+ level := 1
184+ if containsAll (flagSet , []string {"lm" , "cmov" , "cx8" , "fpu" , "fxsr" , "mmx" , "syscall" , "sse2" }) {
185+ level = 1
186+ }
187+ if level == 1 && containsAll (flagSet , []string {"cx16" , "lahf_lm" , "popcnt" , "sse4_1" , "sse4_2" , "ssse3" }) {
188+ level = 2
189+ }
190+ if level == 2 && containsAll (flagSet , []string {"avx" , "avx2" , "bmi1" , "bmi2" , "f16c" , "fma" , "abm" , "movbe" , "xsave" }) {
191+ level = 3
192+ }
193+ if level == 3 && containsAll (flagSet , []string {"avx512f" , "avx512bw" , "avx512cd" , "avx512dq" , "avx512vl" }) {
194+ level = 4
195+ }
196+ return fmt .Sprintf ("v%d" , level ), nil
197+ }
0 commit comments