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.
2021-07-26 01:37:11 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
const mem = std.mem;
|
|
|
|
const expect = std.testing.expect;
|
|
|
|
|
2022-03-24 22:27:23 +00:00
|
|
|
test "@select vectors" {
|
2022-03-23 20:41:35 +00:00
|
|
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
2022-10-11 14:39:47 +01:00
|
|
|
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
2022-03-23 21:06:07 +00:00
|
|
|
|
2022-03-24 22:27:23 +00:00
|
|
|
comptime try selectVectors();
|
|
|
|
try selectVectors();
|
2022-03-23 20:41:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 22:27:23 +00:00
|
|
|
fn selectVectors() !void {
|
2022-03-23 20:41:35 +00:00
|
|
|
var a = @Vector(4, bool){ true, false, true, false };
|
|
|
|
var b = @Vector(4, i32){ -1, 4, 999, -31 };
|
|
|
|
var c = @Vector(4, i32){ -5, 1, 0, 1234 };
|
|
|
|
var abc = @select(i32, a, b, c);
|
|
|
|
try expect(abc[0] == -1);
|
|
|
|
try expect(abc[1] == 1);
|
|
|
|
try expect(abc[2] == 999);
|
|
|
|
try expect(abc[3] == 1234);
|
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.
2021-07-26 01:37:11 +01:00
|
|
|
|
2022-03-23 20:41:35 +00:00
|
|
|
var x = @Vector(4, bool){ false, false, false, true };
|
|
|
|
var y = @Vector(4, f32){ 0.001, 33.4, 836, -3381.233 };
|
|
|
|
var z = @Vector(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 }));
|
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.
2021-07-26 01:37:11 +01:00
|
|
|
}
|
2022-03-24 22:27:23 +00:00
|
|
|
|
|
|
|
test "@select arrays" {
|
|
|
|
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
2022-10-11 14:39:47 +01:00
|
|
|
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
2022-03-24 22:27:23 +00:00
|
|
|
|
|
|
|
comptime try selectArrays();
|
|
|
|
try selectArrays();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn selectArrays() !void {
|
|
|
|
var a = [4]bool{ false, true, false, true };
|
|
|
|
var b = [4]usize{ 0, 1, 2, 3 };
|
|
|
|
var c = [4]usize{ 4, 5, 6, 7 };
|
|
|
|
var abc = @select(usize, a, b, c);
|
|
|
|
try expect(abc[0] == 4);
|
|
|
|
try expect(abc[1] == 1);
|
|
|
|
try expect(abc[2] == 6);
|
|
|
|
try expect(abc[3] == 3);
|
|
|
|
|
|
|
|
var x = [4]bool{ false, false, false, true };
|
|
|
|
var y = [4]f32{ 0.001, 33.4, 836, -3381.233 };
|
|
|
|
var z = [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 }));
|
|
|
|
}
|