simd: simplify isa.funcMap, find Zig compiler bug

This commit is contained in:
Mitchell Hashimoto
2024-01-30 13:33:09 -08:00
parent 1e51cedd94
commit fe098f80cc
3 changed files with 14 additions and 10 deletions

View File

@ -142,7 +142,6 @@ inline fn hasMask(input: u32, mask: u32) bool {
/// will not be included on x86_64. /// will not be included on x86_64.
pub fn funcMap( pub fn funcMap(
comptime Func: type, comptime Func: type,
comptime name: []const u8,
v: ISA, v: ISA,
comptime map: anytype, comptime map: anytype,
) *const Func { ) *const Func {
@ -153,7 +152,12 @@ pub fn funcMap(
// Find the entry for this tag and return the function. // Find the entry for this tag and return the function.
inline for (map) |entry| { inline for (map) |entry| {
if (entry[0] == tag) return @field(entry[1], name); if (entry[0] == tag) {
// If we return &entry[1] directly the compiler crashes:
// https://github.com/ziglang/zig/issues/18754
const func = entry[1];
return &func;
}
} else unreachable; } else unreachable;
}, },
} }

View File

@ -9,10 +9,10 @@ pub const Count = fn ([]const u8) usize;
/// Returns the count function for the given ISA. /// Returns the count function for the given ISA.
pub fn countFunc(v: isa.ISA) *const Count { pub fn countFunc(v: isa.ISA) *const Count {
return isa.funcMap(Count, "count", v, .{ return isa.funcMap(Count, v, .{
.{ .avx2, Scalar }, // todo .{ .avx2, Scalar.count }, // todo
.{ .neon, Neon }, .{ .neon, Neon.count },
.{ .scalar, Scalar }, .{ .scalar, Scalar.count },
}); });
} }

View File

@ -12,10 +12,10 @@ const Validate = fn ([]const u8) bool;
// - https://simdutf.github.io/simdutf/ (MIT License) // - https://simdutf.github.io/simdutf/ (MIT License)
pub fn validateFunc(v: isa.ISA) *const Validate { pub fn validateFunc(v: isa.ISA) *const Validate {
return isa.funcMap(Validate, "validate", v, .{ return isa.funcMap(Validate, v, .{
.{ .avx2, Scalar }, // todo .{ .avx2, Scalar.validate }, // todo
.{ .neon, Neon }, .{ .neon, Neon.validate },
.{ .scalar, Scalar }, .{ .scalar, Scalar.validate },
}); });
} }