I want to know how to use the ternary operator for vector in openCL. I think it should be ‘select’, in the specification:
http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf
page 219
say, I have a float4 for each work-item(thread)
float4 a,b,c;
// I want to do this for every component, i.e. y, z, w
if (a.xb.x>0)
{
c.x=2a.x*b.x;
}
else
{
c.x=0.0;
}
I think writing it like this will slow down the program. I tried to use ‘select’, to just use one line, but don’t know how… Could anyone give me an example?
Thank you very much.
// 4-tuple integer
int4 myResult;
// Variable to hold boolean condition
int4 c;
// Component wise compare --
// According to the spec:
// These functions shall return a 0 if the specified relation
// is false and a –1 (i.e. all bits set) if the specified relation
// is true for vector argument types.
//
// Most Significant Bit (MSB)
// Translation: If true, MSB is set.
// If false, it is not set.
c = isgreater( a * b, 0)
// c should now have each entry with MSB set or unset
// Can do the ternary comparison now
// If true then (parameter 1): 2 * a * b
// If false then (parameter 0): 0
myResult = select(0, a*b*2, c);
I didn’t check if this would compile. I constructed this from pp 218-219.