After almost a week of refactoring and experimenting with several different approaches, I've updated my Zig nD SIMD vector library to be compatible with the latest Zig 0.15.1, and at the same time cleaned up some internals.
The solution I settled on is a mix of techniques proposed by others, and was needed due to the removal of the struct/namespace-merging syntax in the new Zig version, which this library heavily relies on. I don't like that the new source code is now more than 2x larger and involves a huge amount of duplication to address the many special cases of supported operations for different vector sizes and types. I might still take another pass to eliminate those (by using @compileError()
for unsupported cases), but that'd be an implementation detail downstream users don't have to care about. I tried AOT code generation as well, but the special case handling made this feel less maintainable...
UPDATE: The only breaking change is the handling of vector swizzles. I had to remove the hundreds of named swizzle functions and replaced them with a single (comptime optimized) .swizzle(vec, pattern)
, e.g. .swizzle(vec, "xxyy")
...
If you're interested, the new code is here:
https://github.com/thi-ng/zig-thing/blob/main/src/vectors.zig
The readme contains details about the many supported operations:
https://github.com/thi-ng/zig-thing/blob/main/doc/vectors.md
Installation instructions in the main repo readme:
https://github.com/thi-ng/zig-thing/tree/main
Btw. It's amazing that this swizzle function gets compiled into single WASM i8x16.shuffle
ops (per 4 vector components, i.e. swizzling into an 8-dimensional vector would require 2 shuffles):