| 1 |
package runtime |
| 2 |
|
| 3 |
func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32) |
| 4 |
func xgetbv() (eax, edx uint32) |
| 5 |
|
| 6 |
// HasAVX2 is whether the CPU and the OS support AVX2. |
| 7 |
var HasAVX2 = detectAVX2() |
| 8 |
|
| 9 |
func detectAVX2() bool { |
| 10 |
maxID, _, _, _ := cpuid(0, 0) |
| 11 |
if maxID < 7 { |
| 12 |
return false |
| 13 |
} |
| 14 |
_, _, ecx1, _ := cpuid(1, 0) |
| 15 |
const osxsave, avx = 1 << 27, 1 << 28 |
| 16 |
if ecx1&osxsave == 0 || ecx1&avx == 0 { |
| 17 |
return false |
| 18 |
} |
| 19 |
// the OS saves the YMM registers. |
| 20 |
if eax, _ := xgetbv(); eax&6 != 6 { |
| 21 |
return false |
| 22 |
} |
| 23 |
_, ebx7, _, _ := cpuid(7, 0) |
| 24 |
const avx2 = 1 << 5 |
| 25 |
return ebx7&avx2 != 0 |
| 26 |
} |