The data structure from the article would be:
const RGB = packed struct(u16) {
b: u5,
g: u6,
r: u5,
}
So the test becomes const gte: bool = x.r >= y.r and x.g >= y.g and x.b >= y.b;
Okay, so this doesn't get us the optimal form described in the article.Or does it? Since the compiler knows about packed structs, it could perform the optimization for me.
Does it, right this instant? Eh, probably not. But compilers have a tendency to improve, and this is a local pattern which would be fairly easy to recognize. The recognition is the point: it's much, much harder to recognize the intent in the initial implementations described in the article, and then replace them with the optimal version. The way I was able to write it in Zig, in addition to being far more expressive of the algorithm's intent, conveys to the compiler: compare the values of these bitfields, I don't care how. The compiler doesn't have to prove that I have no other reason for the other operations, besides making said comparison possible: it can just emit the optimal code.