| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import sys |
| 4 |
import struct |
| 5 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 6 |
from rpython.rlib.rmmap import alloc, free |
| 7 |
|
| 8 |
CPU_ID_FUNC_PTR = lltype.Ptr(lltype.FuncType([], lltype.Signed)) |
| 9 |
|
| 10 |
def cpu_info(instr): |
| 11 |
data = alloc(4096) |
| 12 |
pos = 0 |
| 13 |
for c in instr: |
| 14 |
data[pos] = c |
| 15 |
pos += 1 |
| 16 |
fnptr = rffi.cast(CPU_ID_FUNC_PTR, data) |
| 17 |
code = fnptr() |
| 18 |
free(data, 4096) |
| 19 |
return code |
| 20 |
|
| 21 |
def detect_sse2(): |
| 22 |
code = cpu_id(eax=1) |
| 23 |
return bool(code & (1<<25)) and bool(code & (1<<26)) |
| 24 |
|
| 25 |
def cpu_id(eax = 1, ret_edx = True, ret_ecx = False): |
| 26 |
asm = ["\xB8", |
| 27 |
chr(eax & 0xff), |
| 28 |
chr((eax >> 8) & 0xff), |
| 29 |
chr((eax >> 16) & 0xff), |
| 30 |
chr((eax >> 24) & 0xff), |
| 31 |
"\x53", |
| 32 |
"\x0F\xA2", |
| 33 |
"\x5B", |
| 34 |
] |
| 35 |
if ret_edx: |
| 36 |
asm.append("\x92") |
| 37 |
elif ret_ecx: |
| 38 |
asm.append("\x91") |
| 39 |
asm.append("\xC3") |
| 40 |
return cpu_info(''.join(asm)) |
| 41 |
|
| 42 |
def detect_sse4_1(code=-1): |
| 43 |
if code == -1: |
| 44 |
code = cpu_id(eax=1, ret_edx=False, ret_ecx=True) |
| 45 |
return bool(code & (1<<19)) |
| 46 |
|
| 47 |
def detect_sse4_2(code=-1): |
| 48 |
if code == -1: |
| 49 |
code = cpu_id(eax=1, ret_edx=False, ret_ecx=True) |
| 50 |
return bool(code & (1<<20)) |
| 51 |
|
| 52 |
def detect_sse4a(code=-1): |
| 53 |
if code == -1: |
| 54 |
code = cpu_id(eax=0x80000001, ret_edx=False, ret_ecx=True) |
| 55 |
return bool(code & (1<<20)) |
| 56 |
|
| 57 |
def detect_x32_mode(): |
| 58 |
|
| 59 |
code = cpu_info("\x48" |
| 60 |
"\xB8\xC8\x00\x00\x00" |
| 61 |
"\x40\x40\x40\x40" |
| 62 |
"\xC3") |
| 63 |
assert code in (200, 204, 0x40404040000000C8) |
| 64 |
return code == 200 |
| 65 |
|
| 66 |
|
| 67 |
if __name__ == '__main__': |
| 68 |
if detect_sse2(): |
| 69 |
print('Processor supports sse2') |
| 70 |
if detect_sse4_1(): |
| 71 |
print('Processor supports sse4.1') |
| 72 |
if detect_sse4_2(): |
| 73 |
print('Processor supports sse4.2') |
| 74 |
if detect_sse4a(): |
| 75 |
print('Processor supports sse4a') |
| 76 |
|
| 77 |
if detect_x32_mode(): |
| 78 |
print('Process is running in "x32" mode.') |