Skip to content

CPU features detection at runtime with the x86 CPUID instruction

Notifications You must be signed in to change notification settings

Bad-Sam/cpu_specs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cpu_specs

Personal C library to identify common CPU features at runtime. This is useful to perform CPU dispatching.

#include "cpu_specs.h"

int main()
{
  cpu_specs_init();

  s32 has_sse4_2   = cpu_specs.instructions & SSE4_2;
  s32 is_multicore = cpu_specs.core_count > 1;

  if (has_sse4_2 && is_multicore)
  {
    // Required CPU features available. Run the program
  }
  else
  {
    // Required CPU features unavailable. Present an error message explaining why the program
    // can't be run and exit early
  }
  
  return 0;
}

Features

cpu_specs is designed to run on:

  • Windows XP and above
  • x86 architectures (32-bit and 64-bit)
  • AMD or Intel CPUs (with defaults for unsupported CPU vendors)

cpu_specs can detect:

  • L1, L2 and L3 data cache size and attached core count
  • Cache line size
  • Number of threads per physical core
  • Total number of physical core in the CPU
  • Available instructions:
    • SIMD instruction sets (SSE1, SSE2, SSE3, SSSE3, SSE4.1, SEE4.2, AVX1, AVX2, AVX512F, FMA3)
    • Bit manipulation instructions (POPCNT, LZNCT, TZCNT, BMI1, BMI2, TBM)
    • Half-precision conversion instructions (F16C)
    • Read time stamp counter instructions (RDTSC/RDTSCP)
  • The CPU's name, manufacturer name, family, model and stepping

The detection of features is currently solely based on the CPUID instruction, whose availability is checked at runtime. When it is available (see cpuid_is_available()):

  • CPU features can be initialized with cpu_specs_init(), and are then accessible in the global variable cpu_specs.
  • CPU identity fields (name, manufacturer name, family, model and stepping) can be initialized with cpu_identity_init(), and are then accessible in the global variable cpu_identity

When CPUID is unavailable, cpu_specs fields are initialized to represent a low-end CPU (see the top of cpu_specs.c).

No dynamic allocation is performed. The C standard library isn't used.

Possible improvements

  • Properly detect and distinguish efficient and performance cores, such as with the Intel Core i7-12800HX, and return their accurate number of cores
  • Add additional useful features to cpu_specs (page extensions?)
  • Extend support to ARM architectures
  • Extend support to UNIX-based operating systems (implies gcc support)

Resources

About

CPU features detection at runtime with the x86 CPUID instruction

Resources

Stars

Watchers

Forks

Languages