1// Copyright (c) 2015 Klaus Post, released under MIT License. See LICENSE file.
2
3//+build amd64,!gccgo,!noasm,!appengine
4
5// func asmCpuid(op uint32) (eax, ebx, ecx, edx uint32)
6TEXT ·asmCpuid(SB), 7, $0
7	XORQ CX, CX
8	MOVL op+0(FP), AX
9	CPUID
10	MOVL AX, eax+8(FP)
11	MOVL BX, ebx+12(FP)
12	MOVL CX, ecx+16(FP)
13	MOVL DX, edx+20(FP)
14	RET
15
16// func asmCpuidex(op, op2 uint32) (eax, ebx, ecx, edx uint32)
17TEXT ·asmCpuidex(SB), 7, $0
18	MOVL op+0(FP), AX
19	MOVL op2+4(FP), CX
20	CPUID
21	MOVL AX, eax+8(FP)
22	MOVL BX, ebx+12(FP)
23	MOVL CX, ecx+16(FP)
24	MOVL DX, edx+20(FP)
25	RET
26
27// func asmXgetbv(index uint32) (eax, edx uint32)
28TEXT ·asmXgetbv(SB), 7, $0
29	MOVL index+0(FP), CX
30	BYTE $0x0f; BYTE $0x01; BYTE $0xd0 // XGETBV
31	MOVL AX, eax+8(FP)
32	MOVL DX, edx+12(FP)
33	RET
34
35// func asmRdtscpAsm() (eax, ebx, ecx, edx uint32)
36TEXT ·asmRdtscpAsm(SB), 7, $0
37	BYTE $0x0F; BYTE $0x01; BYTE $0xF9 // RDTSCP
38	MOVL AX, eax+0(FP)
39	MOVL BX, ebx+4(FP)
40	MOVL CX, ecx+8(FP)
41	MOVL DX, edx+12(FP)
42	RET
43
44// From https://go-review.googlesource.com/c/sys/+/285572/
45// func asmDarwinHasAVX512() bool
46TEXT ·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
68no_avx512:
69#endif
70#endif
71	RET
72
73