authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-11-25 19:45:38+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-11-28 17:07:34+01:00
log29aafdcd5520e15d2d395d616f2aee6ed9ba4648
tree0c7827520a7f3300fd86226c7e5c71276441e160
parent6edf9127fe810a5f6493e61f123f9f25a2c3f7dd

windows: detect native CPU features for aarch64 SoCs


2 files changed, 42 insertions(+), 0 deletions(-)

lib/std/zig/system/NativeTargetInfo.zig+1
......@@ -978,6 +978,7 @@ fn detectNativeCpuAndFeatures(cpu_arch: Target.Cpu.Arch, os: Target.Os, cross_ta
978978 switch (builtin.os.tag) {
979979 .linux => return linux.detectNativeCpuAndFeatures(),
980980 .macos => return darwin.macos.detectNativeCpuAndFeatures(),
981 .windows => return windows.detectNativeCpuAndFeatures(),
981982 else => {},
982983 }
983984
lib/std/zig/system/windows.zig+41
......@@ -1,6 +1,10 @@
11const std = @import("std");
2const builtin = @import("builtin");
3const Target = std.Target;
24
35pub const WindowsVersion = std.Target.Os.WindowsVersion;
6pub const PF = std.os.windows.PF;
7pub const IsProcessorFeaturePresent = std.os.windows.IsProcessorFeaturePresent;
48
59/// Returns the highest known WindowsVersion deduced from reported runtime information.
610/// Discards information about in-between versions we don't differentiate.
......@@ -38,3 +42,40 @@ pub fn detectRuntimeVersion() WindowsVersion {
3842
3943 return @intToEnum(WindowsVersion, version);
4044}
45
46fn detectNativeCpuAndFeaturesArm64() Target.Cpu {
47 const Feature = Target.aarch64.Feature;
48
49 var cpu = Target.Cpu{
50 .arch = .aarch64,
51 .model = Target.Cpu.Model.generic(.aarch64),
52 .features = Target.Cpu.Feature.Set.empty,
53 };
54
55 if (IsProcessorFeaturePresent(PF.ARM_NEON_INSTRUCTIONS_AVAILABLE)) {
56 cpu.features.addFeature(@enumToInt(Feature.neon));
57 }
58 if (IsProcessorFeaturePresent(PF.ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE)) {
59 cpu.features.addFeature(@enumToInt(Feature.crc));
60 }
61 if (IsProcessorFeaturePresent(PF.ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE)) {
62 cpu.features.addFeature(@enumToInt(Feature.crypto));
63 }
64
65 return cpu;
66}
67
68fn getCpuCount() usize {
69 return std.os.windows.peb().NumberOfProcessors;
70}
71
72pub fn detectNativeCpuAndFeatures() ?Target.Cpu {
73 switch (builtin.cpu.arch) {
74 .aarch64 => return detectNativeCpuAndFeaturesArm64(),
75 else => |arch| return .{
76 .arch = arch,
77 .model = Target.Cpu.Model.generic(arch),
78 .features = Target.Cpu.Feature.Set.empty,
79 },
80 }
81}