2008. augusztus 15., péntek

How to detect if a CPU supports MMX


Problem/Question/Abstract:

How to detect if a CPU supports MMX

Answer:

You have to use the CPUID instruction. Bit 23 of the feature flags (for EAX = 1) indicate if a processor supports the MMX instructions. Here is an example of how it could be used for MMX detection only (on Delphi versions prior to D6, you'll need to replace CPUID by DB $0f,$a2):

function SupportsMMX: Boolean;
var
  Supported: LongBool;
asm
  pushad
  mov Supported, 0
  pushfd
  pop eax
  mov edx, eax
  xor eax, 1 shl 21
  push eax
  popfd
  pushfd
  pop eax
  xor eax, edx
  and eax, 1 shl 21  {only if bit 21 can toggle, CPUID is supported}
  jz @ending  {if not, then exit}
  xor eax, eax
  cpuid
  cmp eax, 0  {check highest input value for CPUID}
  je @ending  {if highest value is zero, then exit}
  mov eax, 1
  cpuid  {We only need feature flags}
  test edx, 1 shl 23
  jz @ending
  inc Supported
@ending:
  popad
  mov eax, DWORD PTR Supported
end;

CPUID causes a lot of overhead, so make sure you call it only once during initialization and store the result somewhere to consult at any later stage. All remarks made apply to Intel processors unless specifiedotherwise

Nincsenek megjegyzések:

Megjegyzés küldése