I am playing around with SIMD and came across this very very weird sytnax, which I believe is logically correct.
What I have expected here to happen is obviously to not use the second [0…64], because logically the slice is already of lenght of 64.
Am I expecting too much? Is there a better way to solve this?
pub fn xor(a: []const []u8, b: []const []u8) void {
std.debug.assert(a.len == b.len);
std.debug.assert(a.len >= 0);
std.debug.assert(a[0].len % 64 == 0);
std.debug.assert(b[0].len % 64 == 0);
for (a, b) |ac, bc| {
for (0..a[0].len / 64) |i| {
const start = i * 64;
const end = (i + 1) * 64;
const c: @Vector(64, u8) = ac[start..end][0..64].*;
const d: @Vector(64, u8) = bc[start..end][0..64].*;
ac[start..end][0..64].* = c ^ d;
}
}
}
test xor {
var arr1: [128]u8 = @splat(0);
var arr2: [128]u8 = @splat(1);
const slices_arr1: [1][]u8 = .{arr1[0..arr1.len]};
const slices_arr2: [1][]u8 = .{arr2[0..arr2.len]};
xor(slices_arr1[0..1], slices_arr2[0..1]);
try std.testing.expectEqualDeep(slices_arr1, slices_arr2);
}
The code below results in the following error:
pub fn xor(a: []const []u8, b: []const []u8) void {
std.debug.assert(a.len == b.len);
std.debug.assert(a.len >= 0);
std.debug.assert(a[0].len % 64 == 0);
std.debug.assert(b[0].len % 64 == 0);
for (a, b) |ac, bc| {
for (0..a[0].len / 64) |i| {
const start = i * 64;
const end = (i + 1) * 64;
const c: @Vector(64, u8) = ac[start..end].*;
const d: @Vector(64, u8) = bc[start..end].*;
ac[start..end].* = c ^ d;
}
}
}
src/main.zig:15:54: error: index syntax required for slice type '[]u8'
const c: @Vector(64, u8) = ac[start..end].*;