diff options
| author | Robin Voetter <robin@voetter.nl> | 2021-07-26 02:37:11 +0200 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-07-26 20:05:48 -0400 |
| commit | 50a29f7c213d4a906839dfd625b6280663348781 (patch) | |
| tree | f0371b90219e295987d33f6335ad0aeb60fd2f7a /test/behavior | |
| parent | fc105f268149b195ea4a4189da59d40e96e455b4 (diff) | |
| download | zig-50a29f7c213d4a906839dfd625b6280663348781.tar.gz zig-50a29f7c213d4a906839dfd625b6280663348781.zip | |
Add @select
@select(
comptime T: type,
pred: std.meta.Vector(len, bool),
a: std.meta.Vector(len, T),
b: std.meta.Vector(len, T)
) std.meta.Vector(len, T)
Constructs a vector from a & b, based on the values in the predicate vector. For indices where the predicate value is true, the corresponding
element from the a vector is selected, and otherwise from b.
Diffstat (limited to 'test/behavior')
| -rw-r--r-- | test/behavior/select.zig | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/test/behavior/select.zig b/test/behavior/select.zig new file mode 100644 index 0000000000..5c69094413 --- /dev/null +++ b/test/behavior/select.zig @@ -0,0 +1,25 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const mem = std.mem; +const expect = std.testing.expect; +const Vector = std.meta.Vector; + +test "@select" { + const S = struct { + fn doTheTest() !void { + var a: Vector(4, bool) = [4]bool{ true, false, true, false }; + var b: Vector(4, i32) = [4]i32{ -1, 4, 999, -31 }; + var c: Vector(4, i32) = [4]i32{ -5, 1, 0, 1234 }; + var abc = @select(i32, a, b, c); + try expect(mem.eql(i32, &@as([4]i32, abc), &[4]i32{ -1, 1, 999, 1234 })); + + var x: Vector(4, bool) = [4]bool{ false, false, false, true }; + var y: Vector(4, f32) = [4]f32{ 0.001, 33.4, 836, -3381.233 }; + var z: Vector(4, f32) = [4]f32{ 0.0, 312.1, -145.9, 9993.55 }; + var xyz = @select(f32, x, y, z); + try expect(mem.eql(f32, &@as([4]f32, xyz), &[4]f32{ 0.0, 312.1, -145.9, -3381.233 })); + } + }; + try S.doTheTest(); + comptime try S.doTheTest(); +} |
