View Full Version : using ?: with vectors

What is the correct way to use the selection operator with vectors?

For example, this works fine when a and b are floats:

a = (b==.0f) ? .0f : a/b;

but if they are float4 it errors when building:

"used type 'int __attribute__((ext_vector_type(4)))' where arithmetic or pointer type is required"

Making the zeros vectors doesn't help:

a = (b==(float4)(0,0,0,0)) ? (float4)(0,0,0,0) : a / b;

The reference guide makes it sound like this should work, what am I missing?

Thanks!

david.garcia

03-22-2011, 04:58 PM

This is what the spec says:

The ternary selection operator (?:) operates on three expressions (exp1 ? exp2 : exp3). This operator evaluates the first expression exp1, which can be a scalar or vector result except float. If the result is a scalar value then it selects to evaluate the second expression if the result compares unequal to 0, otherwise it selects to evaluate the third expression. If the result is a vector value, then this is equivalent to calling select(exp3, exp2, exp1). The select function is described in table 6.14. The second and third expressions can be any type, as long their types match, or there is a conversion in section 6.2.1 Implicit conversions that can be applied to one of the expressions to make their types match, or one is a vector and the other is a scalar and the scalar may be subject to the usual arithmetic conversion to the element type used by the vector operand and widened to the same type as the vector type. This resulting matching type is the type of the entire expression.

If you are still as confused as I am we can start a club :lol:

It sounds like what you want to do can be achieved as well with the select() builtin. Have you tried that?

Yes, the select function is working, so I can proceed with that, thanks.

However it seems like a bug since, as you quoted above, the spec claims: "If the [exp1] result is a vector value, then this is equivalent to calling select(exp3, exp2, exp1)"

but (exp1 ? exp2 : exp3) fails where select(exp3, exp2, exp1) works.

I'm on OS X so I'll submit a bug report to Apple.

david.garcia

03-23-2011, 03:14 PM

Well done submitting an error to the vendor :)

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.