Skip to content

Commit c6a3519

Browse files
authored
Add darwin AVX512 detection (#74)
* Add darwin AVX512 detection Imported from https://go-review.googlesource.com/c/sys/+/285572/ See golang/go#43089
1 parent 3df0045 commit c6a3519

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

cpuid.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"fmt"
1616
"math"
1717
"os"
18+
"runtime"
1819
"strings"
1920
)
2021

@@ -209,6 +210,7 @@ var cpuid func(op uint32) (eax, ebx, ecx, edx uint32)
209210
var cpuidex func(op, op2 uint32) (eax, ebx, ecx, edx uint32)
210211
var xgetbv func(index uint32) (eax, edx uint32)
211212
var rdtscpAsm func() (eax, ebx, ecx, edx uint32)
213+
var darwinHasAVX512 = func() bool { return false }
212214

213215
// CPU contains information about the CPU as detected on startup,
214216
// or when Detect last was called.
@@ -922,7 +924,11 @@ func support() flagSet {
922924
// Verify that XCR0[7:5] = ‘111b’ (OPMASK state, upper 256-bit of ZMM0-ZMM15 and
923925
// ZMM16-ZMM31 state are enabled by OS)
924926
/// and that XCR0[2:1] = ‘11b’ (XMM state and YMM state are enabled by OS).
925-
if (eax>>5)&7 == 7 && (eax>>1)&3 == 3 {
927+
hasAVX512 := (eax>>5)&7 == 7 && (eax>>1)&3 == 3
928+
if runtime.GOOS == "darwin" {
929+
hasAVX512 = fs.inSet(AVX) && darwinHasAVX512()
930+
}
931+
if hasAVX512 {
926932
fs.setIf(ebx&(1<<16) != 0, AVX512F)
927933
fs.setIf(ebx&(1<<17) != 0, AVX512DQ)
928934
fs.setIf(ebx&(1<<21) != 0, AVX512IFMA)

cpuid_386.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ TEXT ·asmRdtscpAsm(SB), 7, $0
4040
MOVL CX, ecx+8(FP)
4141
MOVL DX, edx+12(FP)
4242
RET
43+
44+
// func asmDarwinHasAVX512() bool
45+
TEXT ·asmDarwinHasAVX512(SB), 7, $0
46+
MOVL $0, eax+0(FP)
47+
RET

cpuid_amd64.s

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,33 @@ TEXT ·asmRdtscpAsm(SB), 7, $0
4040
MOVL CX, ecx+8(FP)
4141
MOVL DX, edx+12(FP)
4242
RET
43+
44+
// From https://go-review.googlesource.com/c/sys/+/285572/
45+
// func asmDarwinHasAVX512() bool
46+
TEXT ·asmDarwinHasAVX512(SB), 7, $0-1
47+
MOVB $0, ret+0(FP) // default to false
48+
49+
#ifdef GOOS_darwin // return if not darwin
50+
#ifdef GOARCH_amd64 // return if not amd64
51+
// These values from:
52+
// https://github.com/apple/darwin-xnu/blob/xnu-4570.1.46/osfmk/i386/cpu_capabilities.h
53+
#define commpage64_base_address 0x00007fffffe00000
54+
#define commpage64_cpu_capabilities64 (commpage64_base_address+0x010)
55+
#define commpage64_version (commpage64_base_address+0x01E)
56+
#define hasAVX512F 0x0000004000000000
57+
MOVQ $commpage64_version, BX
58+
MOVW (BX), AX
59+
CMPW AX, $13 // versions < 13 do not support AVX512
60+
JL no_avx512
61+
MOVQ $commpage64_cpu_capabilities64, BX
62+
MOVQ (BX), AX
63+
MOVQ $hasAVX512F, CX
64+
ANDQ CX, AX
65+
JZ no_avx512
66+
MOVB $1, ret+0(FP)
67+
68+
no_avx512:
69+
#endif
70+
#endif
71+
RET
72+

detect_x86.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ func asmCpuid(op uint32) (eax, ebx, ecx, edx uint32)
88
func asmCpuidex(op, op2 uint32) (eax, ebx, ecx, edx uint32)
99
func asmXgetbv(index uint32) (eax, edx uint32)
1010
func asmRdtscpAsm() (eax, ebx, ecx, edx uint32)
11+
func asmDarwinHasAVX512() bool
1112

1213
func initCPU() {
1314
cpuid = asmCpuid
1415
cpuidex = asmCpuidex
1516
xgetbv = asmXgetbv
1617
rdtscpAsm = asmRdtscpAsm
18+
darwinHasAVX512 = asmDarwinHasAVX512
1719
}
1820

1921
func addInfo(c *CPUInfo, safe bool) {

0 commit comments

Comments
 (0)