I am having the following issue with @Vector and casting:
- if I just perform a
@intCastfromi32tou8, it works, as you can see in the first test. - if I perform an operation on the
i32(@select) and then cast, I don’t get what I expect
const std = @import("std");
test "vector intCast(i32->u8) simple" {
const N = 8;
var buf: [N]u8 = undefined;
const v_i32: @Vector(N, i32) = .{ 1, 2, 3, 4, 5, 6, 7, 8 };
const v_u8: @Vector(N, u8) = @intCast(v_i32);
buf[0..][0..N].* = v_u8;
const expected = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
try std.testing.expectEqualSlices(u8, &expected, &buf);
}
test "vector intCast(i32->u8) with @select" {
const N = 8;
var buf: [N]u8 = undefined;
var v_i32: @Vector(N, i32) = .{ 1, 2, 3, 4, 5, 6, 7, 8 };
// clamp the vector between 0-255
// but it's already in range, so it shouldn't modify
const zero: @Vector(N, i32) = @splat(0);
const maxv: @Vector(N, i32) = @splat(255);
v_i32 = @select(i32, v_i32 < zero, zero, v_i32);
v_i32 = @select(i32, v_i32 > maxv, maxv, v_i32);
const v_u8: @Vector(N, u8) = @intCast(v_i32);
buf[0..][0..N].* = v_u8; // store after cast
const expected = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
try std.testing.expectEqualSlices(u8, &expected, &buf);
// Expected: { 1, 2, 3, 4, 5, 6, 7, 8 }
// Actual v_u8: { 1, 2, 3, 4, 1, 2, 3, 4 }
}
Am I missing something or is this a bug?
I am on 0.16.0-dev.235+377a8b2a3