std.zig.system.x86.bit
function
inline fn bit(input: u32, offset: u5) bool
Parameters
input: u32
offset: u5
std.zig.system.x86.builtin
constant
std.zig.system.x86.cpuid
function
fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf
Parameters
leaf_id: u32
subid: u32
std.zig.system.x86.CpuidLeaf
container
Fields
eax: u32
ebx: u32
ecx: u32
edx: u32
std.zig.system.x86.detectAmdProcessor
function
fn detectAmdProcessor(cpu: *const Target.Cpu, family: u32, model: u32) ?*const Target.Cpu.Model
Parameters
std.zig.system.x86.detectIntelProcessor
function
fn detectIntelProcessor(cpu: *const Target.Cpu, family: u32, model: u32, brand_id: u32) ?*const Target.Cpu.Model
Parameters
std.zig.system.x86.detectNativeCpuAndFeatures
function
pub fn detectNativeCpuAndFeatures(arch: Target.Cpu.Arch) Target.Cpu
Parameters
std.zig.system.x86.detectNativeFeatures
function
fn detectNativeFeatures(cpu: *Target.Cpu) void
Parameters
std.zig.system.x86.getXCR0
function
fn getXCR0() u32
std.zig.system.x86.hasMask
function
inline fn hasMask(input: u32, mask: u32) bool
Parameters
input: u32
mask: u32
std.zig.system.x86.setFeature
function
fn setFeature(cpu: *Target.Cpu, feature: Target.x86.Feature, enabled: bool) void
Parameters
std.zig.system.x86.std
alias
Alias of std
std.zig.system.x86.Target
alias
Alias of std.Target
std.zig.system.x86.Xcr0
container
Only covers EAX for now.
Fields
x87: bool
sse: bool
avx: bool
bndreg: bool
bndcsr: bool
opmask: bool
zmm_hi256: bool
hi16_zmm: bool
pt: bool
pkru: bool
pasid: bool
cet_u: bool
cet_s: bool
hdc: bool
uintr: bool
lbr: bool
hwp: bool
xtilecfg: bool
xtiledata: bool
apx: bool
_reserved: u12
std.zig.system.x86.zig_x86_cpuid
function
This is a workaround for the C backend until zig has the ability to put C code in inline assembly.
extern fn zig_x86_cpuid(leaf_id: u32, subid: u32, eax: *u32, ebx: *u32, ecx: *u32, edx: *u32) callconv(.c) void
Parameters
leaf_id: u32
subid: u32
eax: *u32
ebx: *u32
ecx: *u32
edx: *u32
std.zig.system.x86.zig_x86_get_xcr0
function
This is a workaround for the C backend until zig has the ability to put C code in inline assembly.
extern fn zig_x86_get_xcr0() callconv(.c) u32
Source code
std/zig/system/x86.zig
const std = @import("std");
const builtin = @import("builtin");
const Target = std.Target;
/// Only covers EAX for now.
const Xcr0 = packed struct(u32) {
x87: bool,
sse: bool,
avx: bool,
bndreg: bool,
bndcsr: bool,
opmask: bool,
zmm_hi256: bool,
hi16_zmm: bool,
pt: bool,
pkru: bool,
pasid: bool,
cet_u: bool,
cet_s: bool,
hdc: bool,
uintr: bool,
lbr: bool,
hwp: bool,
xtilecfg: bool,
xtiledata: bool,
apx: bool,
_reserved: u12,
};
fn setFeature(cpu: *Target.Cpu, feature: Target.x86.Feature, enabled: bool) void {
const idx = @as(Target.Cpu.Feature.Set.Index, @backingInt(feature));
if (enabled) cpu.features.addFeature(idx) else cpu.features.removeFeature(idx);
}
inline fn bit(input: u32, offset: u5) bool {
return (input >> offset) & 1 != 0;
}
inline fn hasMask(input: u32, mask: u32) bool {
return (input & mask) == mask;
}
pub fn detectNativeCpuAndFeatures(arch: Target.Cpu.Arch) Target.Cpu {
var cpu = Target.Cpu{
.arch = arch,
.model = Target.Cpu.Model.generic(arch),
.features = Target.Cpu.Feature.Set.empty,
};
// First we detect features, to use as hints when detecting CPU Model.
detectNativeFeatures(&cpu);
var leaf = cpuid(0, 0);
const max_leaf = leaf.eax;
const vendor = leaf.ebx;
if (max_leaf > 0) {
leaf = cpuid(0x1, 0);
const brand_id = leaf.ebx & 0xff;
// Detect model and family
var family = (leaf.eax >> 8) & 0xf;
var model = (leaf.eax >> 4) & 0xf;
if (family == 6 or family == 0xf) {
if (family == 0xf) {
family += (leaf.eax >> 20) & 0xff;
}
model += ((leaf.eax >> 16) & 0xf) << 4;
}
// Now we detect the model.
if (switch (vendor) {
0x756e6547 => detectIntelProcessor(&cpu, family, model, brand_id),
0x68747541 => detectAmdProcessor(&cpu, family, model),
else => null,
}) |m| b: {
// Some hypervisors are evil liars and will operate in long mode while identifying as a
// CPU model that never had long mode in reality. We've seen this in practice with
// athlon_xp (AMD K7). Catch this contradiction and fall back to using a generic CPU
// model with the features we detected earlier.
if (cpu.has(.x86, .@"64bit") and !Target.x86.featureSetHas(m.features, .@"64bit")) break :b;
cpu.model = m;
}
}
// Add the CPU model's feature set into the working set, but then
// override with actual detected features again.
cpu.features.addFeatureSet(cpu.model.features);
detectNativeFeatures(&cpu);
cpu.features.populateDependencies(cpu.arch.allFeaturesList());
return cpu;
}
fn detectIntelProcessor(cpu: *const Target.Cpu, family: u32, model: u32, brand_id: u32) ?*const Target.Cpu.Model {
if (brand_id != 0) return null;
return switch (family) {
3 => &Target.x86.cpu.i386,
4 => &Target.x86.cpu.i486,
5 => if (cpu.has(.x86, .mmx))
&Target.x86.cpu.pentium_mmx
else
&Target.x86.cpu.pentium,
6 => switch (model) {
0x01 => &Target.x86.cpu.pentiumpro,
0x03, 0x05, 0x06 => &Target.x86.cpu.pentium2,
0x07, 0x08, 0x0a, 0x0b => &Target.x86.cpu.pentium3,
0x09, 0x0d, 0x15 => &Target.x86.cpu.pentium_m,
0x0e => &Target.x86.cpu.yonah,
0x0f, 0x16 => &Target.x86.cpu.core2,
0x17, 0x1d => &Target.x86.cpu.penryn,
0x1a, 0x1e, 0x1f, 0x2e => &Target.x86.cpu.nehalem,
0x25, 0x2c, 0x2f => &Target.x86.cpu.westmere,
0x2a, 0x2d => &Target.x86.cpu.sandybridge,
0x3a, 0x3e => &Target.x86.cpu.ivybridge,
0x3c, 0x3f, 0x45, 0x46 => &Target.x86.cpu.haswell,
0x3d, 0x47, 0x4f, 0x56 => &Target.x86.cpu.broadwell,
0x4e, 0x5e, 0x8e, 0x9e, 0xa5, 0xa6 => &Target.x86.cpu.skylake,
0xa7 => &Target.x86.cpu.rocketlake,
0x55 => if (cpu.has(.x86, .avx512bf16))
&Target.x86.cpu.cooperlake
else if (cpu.has(.x86, .avx512vnni))
&Target.x86.cpu.cascadelake
else
&Target.x86.cpu.skylake_avx512,
0x66 => &Target.x86.cpu.cannonlake,
0x7d, 0x7e => &Target.x86.cpu.icelake_client,
0x6a, 0x6c => &Target.x86.cpu.icelake_server,
0x8c, 0x8d => &Target.x86.cpu.tigerlake,
0x97, 0x9a => &Target.x86.cpu.alderlake,
0xbe => &Target.x86.cpu.gracemont,
0xb7, 0xba, 0xbf => &Target.x86.cpu.raptorlake,
0xaa, 0xac => &Target.x86.cpu.meteorlake,
0xc5, 0xb5 => &Target.x86.cpu.arrowlake,
0xc6 => &Target.x86.cpu.arrowlake_s,
0xbd => &Target.x86.cpu.lunarlake,
0xcc, 0xd5 => &Target.x86.cpu.pantherlake,
0xad => &Target.x86.cpu.graniterapids,
0xae => &Target.x86.cpu.graniterapids_d,
0xcf => &Target.x86.cpu.emeraldrapids,
0x8f => &Target.x86.cpu.sapphirerapids,
0x1c, 0x26, 0x27, 0x35, 0x36 => &Target.x86.cpu.bonnell,
0x37, 0x4a, 0x4d, 0x5a, 0x5d, 0x4c => &Target.x86.cpu.silvermont,
0x5c, 0x5f => &Target.x86.cpu.goldmont,
0x7a => &Target.x86.cpu.goldmont_plus,
0x86, 0x8a, 0x96, 0x9c => &Target.x86.cpu.tremont,
0xaf => &Target.x86.cpu.sierraforest,
0xb6 => &Target.x86.cpu.grandridge,
0xdd => &Target.x86.cpu.clearwaterforest,
0x57 => &Target.x86.cpu.knl,
0x85 => &Target.x86.cpu.knm,
else => null,
},
15 => if (cpu.has(.x86, .@"64bit"))
&Target.x86.cpu.nocona
else if (cpu.has(.x86, .sse3))
&Target.x86.cpu.prescott
else
&Target.x86.cpu.pentium4,
18 => switch (model) {
0x01, 0x03 => &Target.x86.cpu.novalake,
else => null,
},
19 => switch (model) {
0x01 => &Target.x86.cpu.diamondrapids,
else => null,
},
else => null,
};
}
fn detectAmdProcessor(cpu: *const Target.Cpu, family: u32, model: u32) ?*const Target.Cpu.Model {
return switch (family) {
4 => &Target.x86.cpu.i486,
5 => switch (model) {
6, 7 => &Target.x86.cpu.k6,
8 => &Target.x86.cpu.k6_2,
9, 13 => &Target.x86.cpu.k6_3,
10 => &Target.x86.cpu.geode,
else => &Target.x86.cpu.pentium,
},
6 => if (cpu.has(.x86, .sse))
&Target.x86.cpu.athlon_xp
else
&Target.x86.cpu.athlon,
15 => if (cpu.has(.x86, .sse3))
&Target.x86.cpu.k8_sse3
else
&Target.x86.cpu.k8,
16, 18 => &Target.x86.cpu.amdfam10,
20 => &Target.x86.cpu.btver1,
21 => switch (model) {
0x60...0x7f => &Target.x86.cpu.bdver4,
0x30...0x3f => &Target.x86.cpu.bdver3,
0x02, 0x10...0x1f => &Target.x86.cpu.bdver2,
else => &Target.x86.cpu.bdver1,
},
22 => &Target.x86.cpu.btver2,
23 => switch (model) {
0x30...0x3f, 0x47, 0x60...0x6f, 0x70...0x7f, 0x84...0x87, 0x90...0x9f, 0xa0...0xaf => &Target.x86.cpu.znver2,
else => &Target.x86.cpu.znver1,
},
25 => switch (model) {
0x10...0x1f, 0x60...0x6f, 0x70...0x7f, 0xa0...0xaf => &Target.x86.cpu.znver4,
else => &Target.x86.cpu.znver3,
},
26 => &Target.x86.cpu.znver5,
else => null,
};
}
fn detectNativeFeatures(cpu: *Target.Cpu) void {
var leaf = cpuid(0, 0);
const max_level = leaf.eax;
leaf = cpuid(1, 0);
setFeature(cpu, .sse3, bit(leaf.ecx, 0));
setFeature(cpu, .pclmul, bit(leaf.ecx, 1));
setFeature(cpu, .ssse3, bit(leaf.ecx, 9));
setFeature(cpu, .cx16, bit(leaf.ecx, 13));
setFeature(cpu, .sse4_1, bit(leaf.ecx, 19));
setFeature(cpu, .sse4_2, bit(leaf.ecx, 20));
setFeature(cpu, .movbe, bit(leaf.ecx, 22));
setFeature(cpu, .popcnt, bit(leaf.ecx, 23));
setFeature(cpu, .aes, bit(leaf.ecx, 25));
setFeature(cpu, .rdrnd, bit(leaf.ecx, 30));
setFeature(cpu, .cx8, bit(leaf.edx, 8));
setFeature(cpu, .cmov, bit(leaf.edx, 15));
setFeature(cpu, .mmx, bit(leaf.edx, 23));
setFeature(cpu, .fxsr, bit(leaf.edx, 24));
setFeature(cpu, .sse, bit(leaf.edx, 25));
setFeature(cpu, .sse2, bit(leaf.edx, 26));
const has_xsave = bit(leaf.ecx, 27);
const has_avx = bit(leaf.ecx, 28);
// Make sure not to call xgetbv if xsave is not supported
const xcr0: Xcr0 = if (has_xsave and has_avx) @bitCast(getXCR0()) else @bitCast(@as(u32, 0));
const has_avx_save = xcr0.sse and xcr0.avx;
// LLVM approaches avx512_save by hardcoding it to true on Darwin,
// because the kernel saves the context even if the bit is not set.
// https://github.com/llvm/llvm-project/blob/bca373f73fc82728a8335e7d6cd164e8747139ec/llvm/lib/Support/Host.cpp#L1378
//
// Google approaches this by using a different series of checks and flags,
// and this may report the feature more accurately on a technically correct
// but ultimately less useful level.
// https://github.com/google/cpu_features/blob/b5c271c53759b2b15ff91df19bd0b32f2966e275/src/cpuinfo_x86.c#L113
// (called from https://github.com/google/cpu_features/blob/b5c271c53759b2b15ff91df19bd0b32f2966e275/src/cpuinfo_x86.c#L1052)
//
// Right now, we use LLVM's approach, because even if the target doesn't support
// the feature, the kernel should provide the same functionality transparently,
// so the implementation details don't make a difference.
// That said, this flag impacts other CPU features' availability,
// so until we can verify that this doesn't come with side affects,
// we'll say TODO verify this.
// Darwin lazily saves the AVX512 context on first use: trust that the OS will
// save the AVX512 context if we use AVX512 instructions, even if the bit is not
// set right now.
const has_avx512_save = if (builtin.target.os.tag.isDarwin())
true
else
xcr0.zmm_hi256 and xcr0.hi16_zmm;
// AMX requires additional context to be saved by the OS.
const has_amx_save = xcr0.xtilecfg and xcr0.xtiledata;
const has_apx_save = xcr0.apx;
setFeature(cpu, .avx, has_avx_save);
setFeature(cpu, .fma, bit(leaf.ecx, 12) and has_avx_save);
// Only enable XSAVE if OS has enabled support for saving YMM state.
setFeature(cpu, .xsave, bit(leaf.ecx, 26) and has_avx_save);
setFeature(cpu, .f16c, bit(leaf.ecx, 29) and has_avx_save);
leaf = cpuid(0x80000000, 0);
const max_ext_level = leaf.eax;
if (max_ext_level >= 0x80000001) {
leaf = cpuid(0x80000001, 0);
setFeature(cpu, .sahf, bit(leaf.ecx, 0));
setFeature(cpu, .lzcnt, bit(leaf.ecx, 5));
setFeature(cpu, .sse4a, bit(leaf.ecx, 6));
setFeature(cpu, .prfchw, bit(leaf.ecx, 8));
setFeature(cpu, .xop, bit(leaf.ecx, 11) and has_avx_save);
setFeature(cpu, .lwp, bit(leaf.ecx, 15));
setFeature(cpu, .fma4, bit(leaf.ecx, 16) and has_avx_save);
setFeature(cpu, .tbm, bit(leaf.ecx, 21));
setFeature(cpu, .mwaitx, bit(leaf.ecx, 29));
setFeature(cpu, .@"64bit", bit(leaf.edx, 29));
} else {
for ([_]Target.x86.Feature{
.sahf,
.lzcnt,
.sse4a,
.prfchw,
.xop,
.lwp,
.fma4,
.tbm,
.mwaitx,
.@"64bit",
}) |feat| {
setFeature(cpu, feat, false);
}
}
// Misc. memory-related features.
if (max_ext_level >= 0x80000008) {
leaf = cpuid(0x80000008, 0);
setFeature(cpu, .clzero, bit(leaf.ebx, 0));
setFeature(cpu, .rdpru, bit(leaf.ebx, 4));
setFeature(cpu, .wbnoinvd, bit(leaf.ebx, 9));
} else {
for ([_]Target.x86.Feature{
.clzero,
.rdpru,
.wbnoinvd,
}) |feat| {
setFeature(cpu, feat, false);
}
}
if (max_ext_level >= 0x80000021) {
leaf = cpuid(0x80000021, 0);
// AMD uses a different bit for prefetchi.
setFeature(cpu, .prefetchi, bit(leaf.eax, 20));
} else {
for ([_]Target.x86.Feature{
.prefetchi,
}) |feat| {
setFeature(cpu, feat, false);
}
}
const has_avx10 = if (max_level >= 0x7) has_avx10: {
leaf = cpuid(0x7, 0);
setFeature(cpu, .fsgsbase, bit(leaf.ebx, 0));
setFeature(cpu, .sgx, bit(leaf.ebx, 2));
setFeature(cpu, .bmi, bit(leaf.ebx, 3));
// AVX2 is only supported if we have the OS save support from AVX.
setFeature(cpu, .avx2, bit(leaf.ebx, 5) and has_avx_save);
setFeature(cpu, .smep, bit(leaf.ebx, 7));
setFeature(cpu, .bmi2, bit(leaf.ebx, 8));
setFeature(cpu, .invpcid, bit(leaf.ebx, 10));
setFeature(cpu, .rtm, bit(leaf.ebx, 11));
// AVX512 is only supported if the OS supports the context save for it.
setFeature(cpu, .avx512f, bit(leaf.ebx, 16) and has_avx512_save);
setFeature(cpu, .avx512dq, bit(leaf.ebx, 17) and has_avx512_save);
setFeature(cpu, .rdseed, bit(leaf.ebx, 18));
setFeature(cpu, .adx, bit(leaf.ebx, 19));
setFeature(cpu, .smap, bit(leaf.ebx, 20));
setFeature(cpu, .avx512ifma, bit(leaf.ebx, 21) and has_avx512_save);
setFeature(cpu, .clflushopt, bit(leaf.ebx, 23));
setFeature(cpu, .clwb, bit(leaf.ebx, 24));
setFeature(cpu, .avx512pf, bit(leaf.ebx, 26) and has_avx512_save);
setFeature(cpu, .avx512er, bit(leaf.ebx, 27) and has_avx512_save);
setFeature(cpu, .avx512cd, bit(leaf.ebx, 28) and has_avx512_save);
setFeature(cpu, .sha, bit(leaf.ebx, 29));
setFeature(cpu, .avx512bw, bit(leaf.ebx, 30) and has_avx512_save);
setFeature(cpu, .avx512vl, bit(leaf.ebx, 31) and has_avx512_save);
setFeature(cpu, .prefetchwt1, bit(leaf.ecx, 0));
setFeature(cpu, .avx512vbmi, bit(leaf.ecx, 1) and has_avx512_save);
setFeature(cpu, .pku, bit(leaf.ecx, 4));
setFeature(cpu, .waitpkg, bit(leaf.ecx, 5));
setFeature(cpu, .avx512vbmi2, bit(leaf.ecx, 6) and has_avx512_save);
setFeature(cpu, .shstk, bit(leaf.ecx, 7));
setFeature(cpu, .gfni, bit(leaf.ecx, 8));
setFeature(cpu, .vaes, bit(leaf.ecx, 9) and has_avx_save);
setFeature(cpu, .vpclmulqdq, bit(leaf.ecx, 10) and has_avx_save);
setFeature(cpu, .avx512vnni, bit(leaf.ecx, 11) and has_avx512_save);
setFeature(cpu, .avx512bitalg, bit(leaf.ecx, 12) and has_avx512_save);
setFeature(cpu, .avx512vpopcntdq, bit(leaf.ecx, 14) and has_avx512_save);
setFeature(cpu, .rdpid, bit(leaf.ecx, 22));
setFeature(cpu, .kl, bit(leaf.ecx, 23));
setFeature(cpu, .cldemote, bit(leaf.ecx, 25));
setFeature(cpu, .movdiri, bit(leaf.ecx, 27));
setFeature(cpu, .movdir64b, bit(leaf.ecx, 28));
setFeature(cpu, .enqcmd, bit(leaf.ecx, 29));
// There are two CPUID leafs which information associated with the pconfig
// instruction:
// EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th
// bit of EDX), while the EAX=0x1b leaf returns information on the
// availability of specific pconfig leafs.
// The target feature here only refers to the the first of these two.
// Users might need to check for the availability of specific pconfig
// leaves using cpuid, since that information is ignored while
// detecting features using the "-march=native" flag.
// For more info, see X86 ISA docs.
setFeature(cpu, .uintr, bit(leaf.edx, 5));
setFeature(cpu, .avx512vp2intersect, bit(leaf.edx, 8) and has_avx512_save);
setFeature(cpu, .serialize, bit(leaf.edx, 14));
setFeature(cpu, .tsxldtrk, bit(leaf.edx, 16));
setFeature(cpu, .pconfig, bit(leaf.edx, 18));
setFeature(cpu, .amx_bf16, bit(leaf.edx, 22) and has_amx_save);
setFeature(cpu, .avx512fp16, bit(leaf.edx, 23) and has_avx512_save);
setFeature(cpu, .amx_tile, bit(leaf.edx, 24) and has_amx_save);
setFeature(cpu, .amx_int8, bit(leaf.edx, 25) and has_amx_save);
if (leaf.eax >= 1) {
leaf = cpuid(0x7, 0x1);
setFeature(cpu, .sha512, bit(leaf.eax, 0));
setFeature(cpu, .sm3, bit(leaf.eax, 1));
setFeature(cpu, .sm4, bit(leaf.eax, 2));
setFeature(cpu, .raoint, bit(leaf.eax, 3));
setFeature(cpu, .avxvnni, bit(leaf.eax, 4) and has_avx_save);
setFeature(cpu, .avx512bf16, bit(leaf.eax, 5) and has_avx512_save);
setFeature(cpu, .cmpccxadd, bit(leaf.eax, 7));
setFeature(cpu, .amx_fp16, bit(leaf.eax, 21) and has_amx_save);
setFeature(cpu, .hreset, bit(leaf.eax, 22));
setFeature(cpu, .avxifma, bit(leaf.eax, 23) and has_avx_save);
setFeature(cpu, .avxvnniint8, bit(leaf.edx, 4) and has_avx_save);
setFeature(cpu, .avxneconvert, bit(leaf.edx, 5) and has_avx_save);
setFeature(cpu, .amx_complex, bit(leaf.edx, 8) and has_amx_save);
setFeature(cpu, .avxvnniint16, bit(leaf.edx, 10) and has_avx_save);
// This needs to account for prefetchi already being detected above on AMD.
setFeature(cpu, .prefetchi, cpu.has(.x86, .prefetchi) or bit(leaf.edx, 14));
setFeature(cpu, .usermsr, bit(leaf.edx, 15));
// APX
setFeature(cpu, .egpr, bit(leaf.edx, 21) and has_apx_save);
setFeature(cpu, .push2pop2, bit(leaf.edx, 21) and has_apx_save);
setFeature(cpu, .ppx, bit(leaf.edx, 21) and has_apx_save);
setFeature(cpu, .ndd, bit(leaf.edx, 21) and has_apx_save);
setFeature(cpu, .ccmp, bit(leaf.edx, 21) and has_apx_save);
setFeature(cpu, .nf, bit(leaf.edx, 21) and has_apx_save);
setFeature(cpu, .cf, bit(leaf.edx, 21) and has_apx_save);
setFeature(cpu, .zu, bit(leaf.edx, 21) and has_apx_save);
break :has_avx10 bit(leaf.edx, 19);
} else {
for ([_]Target.x86.Feature{
.sha512,
.sm3,
.sm4,
.raoint,
.avxvnni,
.avx512bf16,
.cmpccxadd,
.amx_fp16,
.hreset,
.avxifma,
.avxvnniint8,
.avxneconvert,
.amx_complex,
.avxvnniint16,
// prefetchi already handled earlier.
.usermsr,
.egpr,
.push2pop2,
.ppx,
.ndd,
.ccmp,
.nf,
.cf,
.zu,
}) |feat| {
setFeature(cpu, feat, false);
}
}
break :has_avx10 false;
} else has_avx10: {
for ([_]Target.x86.Feature{
.fsgsbase,
.sgx,
.bmi,
.avx2,
.smep,
.bmi2,
.invpcid,
.rtm,
.avx512f,
.avx512dq,
.rdseed,
.adx,
.smap,
.avx512ifma,
.clflushopt,
.clwb,
.avx512pf,
.avx512er,
.avx512cd,
.sha,
.avx512bw,
.avx512vl,
.prefetchwt1,
.avx512vbmi,
.pku,
.waitpkg,
.avx512vbmi2,
.shstk,
.gfni,
.vaes,
.vpclmulqdq,
.avx512vnni,
.avx512bitalg,
.avx512vpopcntdq,
.rdpid,
.kl,
.cldemote,
.movdiri,
.movdir64b,
.enqcmd,
.uintr,
.avx512vp2intersect,
.serialize,
.tsxldtrk,
.pconfig,
.amx_bf16,
.avx512fp16,
.amx_tile,
.amx_int8,
.sha512,
.sm3,
.sm4,
.raoint,
.avxvnni,
.avx512bf16,
.cmpccxadd,
.amx_fp16,
.hreset,
.avxifma,
.avxvnniint8,
.avxneconvert,
.amx_complex,
.avxvnniint16,
// prefetchi already handled earlier.
.usermsr,
.egpr,
.push2pop2,
.ppx,
.ndd,
.ccmp,
.nf,
.cf,
.zu,
}) |feat| {
setFeature(cpu, feat, false);
}
break :has_avx10 false;
};
if (max_level >= 0xD and has_avx_save) {
leaf = cpuid(0xD, 0x1);
// Only enable XSAVE if OS has enabled support for saving YMM state.
setFeature(cpu, .xsaveopt, bit(leaf.eax, 0));
setFeature(cpu, .xsavec, bit(leaf.eax, 1));
setFeature(cpu, .xsaves, bit(leaf.eax, 3));
} else {
for ([_]Target.x86.Feature{
.xsaveopt,
.xsavec,
.xsaves,
}) |feat| {
setFeature(cpu, feat, false);
}
}
if (max_level >= 0x14) {
leaf = cpuid(0x14, 0);
setFeature(cpu, .ptwrite, bit(leaf.ebx, 4));
} else {
for ([_]Target.x86.Feature{
.ptwrite,
}) |feat| {
setFeature(cpu, feat, false);
}
}
if (max_level >= 0x19) {
leaf = cpuid(0x19, 0);
setFeature(cpu, .widekl, bit(leaf.ebx, 2));
} else {
for ([_]Target.x86.Feature{
.widekl,
}) |feat| {
setFeature(cpu, feat, false);
}
}
if (max_level >= 0x24) {
leaf = cpuid(0x24, 0);
const avx_ver = leaf.ebx & 0xff;
setFeature(cpu, .avx10_1, has_avx10 and avx_ver >= 1);
setFeature(cpu, .avx10_2, has_avx10 and avx_ver >= 2);
} else {
for ([_]Target.x86.Feature{
.avx10_1,
.avx10_2,
}) |feat| {
setFeature(cpu, feat, false);
}
}
}
const CpuidLeaf = packed struct {
eax: u32,
ebx: u32,
ecx: u32,
edx: u32,
};
/// This is a workaround for the C backend until zig has the ability to put
/// C code in inline assembly.
extern fn zig_x86_cpuid(leaf_id: u32, subid: u32, eax: *u32, ebx: *u32, ecx: *u32, edx: *u32) callconv(.c) void;
fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf {
// valid for both x86 and x86_64
var eax: u32 = undefined;
var ebx: u32 = undefined;
var ecx: u32 = undefined;
var edx: u32 = undefined;
if (builtin.zig_backend == .stage2_c) {
zig_x86_cpuid(leaf_id, subid, &eax, &ebx, &ecx, &edx);
} else {
asm volatile ("cpuid"
: [_] "={eax}" (eax),
[_] "={ebx}" (ebx),
[_] "={ecx}" (ecx),
[_] "={edx}" (edx),
: [_] "{eax}" (leaf_id),
[_] "{ecx}" (subid),
);
}
return .{ .eax = eax, .ebx = ebx, .ecx = ecx, .edx = edx };
}
/// This is a workaround for the C backend until zig has the ability to put
/// C code in inline assembly.
extern fn zig_x86_get_xcr0() callconv(.c) u32;
// Read control register 0 (XCR0). Used to detect features such as AVX.
fn getXCR0() u32 {
if (builtin.zig_backend == .stage2_c) {
return zig_x86_get_xcr0();
}
return asm volatile (
\\ xor %%ecx, %%ecx
\\ xgetbv
: [_] "={eax}" (-> u32),
:
: .{ .edx = true, .ecx = true });
}