I want to be able to apply a SIMD operation to numbers within a range (i.e. 4 < n < 11). I got it working with the following code, but from it I have two questions:
- Is there a way to
and
two vectors ofbool
s without bit casting to an integer as shown in the code? - Is there a better way of creating such a range mask with vectors in Zig?
const std = @import("std");
const simd = std.simd;
pub fn main() !void {
const vec_len = simd.suggestVectorLength(u8) orelse unreachable;
const BitVec = @Vector(vec_len, u1);
const ByteVec = @Vector(vec_len, u8);
const Int = std.meta.Int(.unsigned, vec_len);
const array = [_]u8{ 1, 10, 3, 4, 5, 6, 2, 1, 9, 10, 11, 12, 8, 14, 15, 16, 17, 18, 19, 20 };
const v1: ByteVec = array[0..][0..vec_len].*;
const fours: ByteVec = @splat(4);
const gt_four = v1 > fours;
const elevens: ByteVec = @splat(11);
const lt_eleven = v1 < elevens;
// Can't do this
// const in_range = gt_four and lt_eleven;
// So have to do this
const gt_four_int: Int = @bitCast(gt_four);
const lt_eleven_int: Int = @bitCast(lt_eleven);
const in_range_int: Int = gt_four_int & lt_eleven_int;
const in_range_bits: BitVec = @bitCast(in_range_int);
const twos: ByteVec = @splat(2);
const masked: ByteVec = in_range_bits * twos;
const added: ByteVec = v1 + masked;
std.debug.print("{any}\n", .{added});
}