I want to compute the kronecker delta (i = j returns 1, otherwise 0) for each element of a stream of type uint4. If I recall correctly, conditionals operate only on the first element of a vector type, so anything based on a conditional is out. I tried abs(sign(i - j)) but abs() will not accept a uint4, presumably because it has a conditional buried in it. The best I've come up with is:
kernel uint4 kronecker_delta(uint4 i<>, uint j<>
{
int4 k_d = sign((int4) i - (int4)j);
return (uint4)(k_d * k_d);
}
My Brook+ experience is near zero, so I wonder if I'm missing some much easier/faster way to do this.
just make it in 4 separate if
afaik that's the only way
btw, in brook+ you need to specifiy output variable explicitly
and there is no return, all basic kernel in brook+ is void
kernel void kronecker_delta(unit i<>, unit j<>, out unit4 k<>
you do the rest
I should have mentioned the pseudo-code I included was a sub-kernel.
that's what I was looking for. I was so focused on the fact that conditionals only operate on the value of the first vector component that I ignored that the boolean operation would operate on all components and set a result for each. Your answer helped me focus on this more powerful approach. Thanks.
BTW, what you wrote doesnlt actually compile, you need to write
return (uint4)(((int4) i != (int4) j) & int4(1,1,1,1));
at least that's what I came up with to get rid of compile errors.