test_shuffle_builtin.zig (776B) - Raw
1 const std = @import("std"); 2 const expect = std.testing.expect; 3 4 test "vector @shuffle" { 5 const a = @Vector(7, u8){ 'o', 'l', 'h', 'e', 'r', 'z', 'w' }; 6 const b = @Vector(4, u8){ 'w', 'd', '!', 'x' }; 7 8 // To shuffle within a single vector, pass undefined as the second argument. 9 // Notice that we can re-order, duplicate, or omit elements of the input vector 10 const mask1 = @Vector(5, i32){ 2, 3, 1, 1, 0 }; 11 const res1: @Vector(5, u8) = @shuffle(u8, a, undefined, mask1); 12 try expect(std.mem.eql(u8, &@as([5]u8, res1), "hello")); 13 14 // Combining two vectors 15 const mask2 = @Vector(6, i32){ -1, 0, 4, 1, -2, -3 }; 16 const res2: @Vector(6, u8) = @shuffle(u8, a, b, mask2); 17 try expect(std.mem.eql(u8, &@as([6]u8, res2), "world!")); 18 } 19 20 // test