3 ms·
While Zig doesn't support this automatically, I think there's a path towards this thanks to comptime support. For example: const std = @import("std");
by Laremere 3y ago
While Zig doesn't support this automatically, I think there's a path towards this thanks to comptime support. For example:
const std = @import("std");
fn f(comptime width: comptime_int, value: i32) i32 {
const v = @splat(width, @as(i32, value));
return @reduce(.Add, v);
}
pub fn main() !void {
std.debug.print("1={d}\n", .{f(1, 5)});
std.debug.print("2={d}\n", .{f(2, 5)});
std.debug.print("4={d}\n", .{f(4, 5)});
}
Here it creates 3 different versions of the function f at compile time, and then calls them each in succession. Running it prints:
1=5
2=10
4=20
In practice you'd need to set up a dispatch that chooses the function based on the hardware, and ensure that zig/LLVM are actually using the full width of the vectors when compiling.