From 90f7034b69e59c3deac4fc8850682b2d750793a4 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 19 Oct 2020 09:45:35 +0200 Subject: [PATCH] std: Implement featureSetHasAny/featureSetHasAll Introduce two helper functions to avoid writing short novels only to check for more than a single feature. --- lib/std/target.zig | 19 +++++++++++++++++++ lib/std/zig/cross_target.zig | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/lib/std/target.zig b/lib/std/target.zig index 728d4c4c4..e7cb931c0 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -637,9 +637,28 @@ pub const Target = struct { return x; } + /// Returns true if the specified feature is enabled. pub fn featureSetHas(set: Set, feature: F) bool { return set.isEnabled(@enumToInt(feature)); } + + /// Returns true if any specified feature is enabled. + pub fn featureSetHasAny(set: Set, features: anytype) bool { + comptime std.debug.assert(std.meta.trait.isIndexable(@TypeOf(features))); + inline for (features) |feature| { + if (set.isEnabled(@enumToInt(@as(F, feature)))) return true; + } + return false; + } + + /// Returns true if every specified feature is enabled. + pub fn featureSetHasAll(set: Set, features: anytype) bool { + comptime std.debug.assert(std.meta.trait.isIndexable(@TypeOf(features))); + inline for (features) |feature| { + if (!set.isEnabled(@enumToInt(@as(F, feature)))) return false; + } + return true; + } }; } }; diff --git a/lib/std/zig/cross_target.zig b/lib/std/zig/cross_target.zig index b8af5545d..b26f416d2 100644 --- a/lib/std/zig/cross_target.zig +++ b/lib/std/zig/cross_target.zig @@ -827,6 +827,11 @@ test "CrossTarget.parse" { std.testing.expect(Target.x86.featureSetHas(target.cpu.features, .cmov)); std.testing.expect(Target.x86.featureSetHas(target.cpu.features, .fxsr)); + std.testing.expect(Target.x86.featureSetHasAny(target.cpu.features, .{ .sse, .avx, .cmov })); + std.testing.expect(!Target.x86.featureSetHasAny(target.cpu.features, .{ .sse, .avx })); + std.testing.expect(Target.x86.featureSetHasAll(target.cpu.features, .{ .mmx, .x87 })); + std.testing.expect(!Target.x86.featureSetHasAll(target.cpu.features, .{ .mmx, .x87, .sse })); + const text = try cross_target.zigTriple(std.testing.allocator); defer std.testing.allocator.free(text); std.testing.expectEqualSlices(u8, "x86_64-linux-gnu", text);