Skip to content

Commit

Permalink
Convert XGETBV to equivalent byte form in GetXCR0Eax (#69)
Browse files Browse the repository at this point in the history
osxcross (https://github.com/tpoechtrager/osxcross) gives the following error, which also happens with regular gcc on OS X (asmjit/asmjit#78):
```
cpu_features/src/cpuinfo_x86.c:44:no such instruction: `XGETBV'
```
  • Loading branch information
natanbc authored and gchatelet committed Mar 20, 2019
1 parent 7806502 commit 084ec5c
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/cpuinfo_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ Leaf CpuId(uint32_t leaf_id) {

uint32_t GetXCR0Eax(void) {
uint32_t eax, edx;
__asm("XGETBV" : "=a"(eax), "=d"(edx) : "c"(0));
/* named form of xgetbv not supported on OSX, so must use byte form, see:
https://github.com/asmjit/asmjit/issues/78
*/
__asm(".byte 0x0F, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(0));
return eax;
}

Expand Down

1 comment on commit 084ec5c

@noloader
Copy link

@noloader noloader commented on 084ec5c May 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will probably want this for Windows 64-bit. It provides XGETBV for downlevel Windows clients. 64-bit Windows does not have inline assembly (from x64dll.asm):

    ALIGN   8
XGETBV64 PROC
;; First paramter is RCX, and xgetbv expects the CTRL in ECX
;; http://www.agner.org/optimize/vectorclass/read.php?i=65
DB  	0fh, 01h, 0d0h
;; xcr = (EDX << 32) | EAX
and 	rax, 0ffffffffh
shl 	rdx, 32
or  	rax, rdx
ret
XGETBV64 ENDP

You would use Microsoft's _xgetbv if available. On 64-bit platforms you would guard for Visual Studio 2008 and below:

#if defined(_MSC_VER) && _MSC_VER <= 1500 && defined(_M_X64)
    uint64_t xcr0 = XGETBV64(0);
    g_hasAVX = (xcr0 & YMM_FLAG) == YMM_FLAG;
#endif

Please sign in to comment.