PDA

View Full Version : evaluating side-effetcs and Logical && and || for vectors



enrique
07-16-2009, 09:53 AM
Since logical && and || need to be evaluated component-wise for vectors, are c[0] and c[2] the only components that need to be evaluated because only b[0] and b[2] were nonzero? What if the RHS of && is a complicated expression(e.g. b&&foo())? Is the statement about evaluating the RHS only when LHS is equal/unequal to 0 meant only apply when both operands are scalar?

int4 a;
int4 b=(int4)(1,0,1,0);
int4 c=(int4)(10,10,10,10);
a=(b&&--c);
//a is (-1,0,-1,0) but should c be now (9,10,9,10) or (9,9,9,9)?

The 1.0 spec states:
"And (&&) will only evaluate the right hand operand if the left hand operand compares unequal to 0. Or (||) will only evaluate the right hand operand if the left hand operand compares equal to 0. For built-in vector types, the operators are applied component-wise"

affie
07-28-2009, 08:37 AM
Operations on vectors are applied component-wise.

In the example you give below, the logical && will be applied as follows:

a.x = b.x && c.x;
a.y = b.y && c.y;
a.z = b.z && c.z;
a.w = b.w && c.w;

The spec states that "And (&&) will only evaluate the right hand operand if the left hand operand compares unequal to 0.". For the example above, c.x is evaluated only if b.x is not zero. Note that the left hand and right hand operands can be expressions. The expression will result in a scalar value. The statement in the spec is for evaluating the RHS only when LHS is equal/unequal to 0 for scalars.

tanakamura
07-30-2009, 06:44 PM
RHS can have subeffect that cannot be interpreted as component wise. For example:


a = (b && (func(c))); // function call
a = (b && (puts("a"), c)); // comma operator

How to interpret it?