Skip to content

Tiny utility to call cpuid, especially to set /arch in binding.gyp

Notifications You must be signed in to change notification settings

cellengine/mini-cpuid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A tiny, cross-platform module to execute cpuid. Intended to be used in binding.gyp files to set MSVC's /arch in the same way that gcc and clang support -march=native, but can be used to retrieve any info.

In binding.gyp:

  ...
  "msvs_settings": {
    "VCCLCompilerTool": {
      "AdditionalOptions": [
        "<!@(node -p \"require('mini-cpuid').getArch()\")"
      ]
    }
  }
  ...

API

cpuid.getArch(): "/arch:AVX"|"/arch:AVX2"|"/arch:AVX512"|""

Currently supports x86_64.

cpuid.cpuid(outRegIdx: number, initEax: number, initEcx: number = 0): number

Returns the outRegIdxth (0-3) register value (a uint32) after calling cpuid with the initial EAX and ECX values initEax and initEcx, respectively.

Example:

import cpuid from "mini-cpuid";
// Get EBX register value when `cpuid` is invoked with EAX set to 7:
const ebx = cpuid(1, 7);
// Test if AVX2 is supported:
const isAVX2Supported = ebx & (1 << 5);

Notes

  • Why use AdditionalOptions instead of EnableEnhancedInstructionSet? Because the latter doesn't support AVX-512.

  • Why not use one of the other cpuid npm modules? As of July 2022, they're either big (depend on libCPUID), not maintained and/or not cross-platform.

  • Why not use CPU dispatching? If you're shipping prebuilt Node.js modules, you should do this. But if you're compiling locally, dispatching requires creating per-extension builds, which increases code size.

About

Tiny utility to call cpuid, especially to set /arch in binding.gyp

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published