I have (almost) never done anything with simd operations. Time to play around with it.
When I want to bitwise combine the following (chess) struct would that be a good candidate for simd (instead of ‘manually’ combine all fields with another Threats struct)?
Operations like and, or, xor.
Or something like ~self.
pub const Threats = struct {
by_pawns: u64 = 0,
by_knights: u64 = 0,
by_bishops: u64 = 0,
by_rooks: u64 = 0,
by_queens: u64 = 0,
by_king: u64 = 0,
};
Yes it would make sense to use vectors here, the resulting assembly code can get a lot simpler and the code you write yourself is also shorter and less prone to copy-paste mistakes.
See a comparison on godbolt (which is a great tool for answering these kinds of questions)
Thank you very much! Thats enough to get me started.
Yes Vectors 100%
A pattern that I like to use in cases like this is to use a union:
pub const Threats = extern union {
breakdown: extern struct {
by_pawns: u64 = 0,
by_knights: u64 = 0,
by_bishops: u64 = 0,
by_rooks: u64 = 0,
by_queens: u64 = 0,
by_king: u64 = 0,
},
arr: [6]u64,
}
Now you can convert very eazily between vectors and Threats with
const vec: @Vector(6, u64) = t.arr;
If it was size 8 you could also put the vector in the union, but because the size is 6, if you put a vector in that union it will increase its size.
ericlang July 11, 2026, 12:37pm 5
Now that is a great trick! I actually looked for something like this several times.
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.