Multiplication question

i have real 2 numbers and i want to multiple them together but if either of them is negative i want the answer to be negative. only if theyre both positive than i want the positive sum.
eg
-2 * -3 = -6

what would be the best way to acomplish this without using conditional statements?
cheers zed

What about:

float result = sign(min(x, y)) * abs(x) * abs(y);

Dunno if it’s the best way. Dunno if abs, min and sign are as expensive as conditionals - logic says they might be.

That is not the same :
sign : it is an actual operation, same as “get the sign bit”
min : condition on only 2 variables, that is likely to be implemented as (almost) a single opcode.
Whereas true conditionals means branching in the code, and that is much more difficult with a GPU architecture.

From a general point of view, I think everyone should try to replace “if” statements by GLSL functions, which are much faster.
For example I often use the “step” function, which return 0 or 1 if one var is inferior to another, and use it to multiply another var.

ie:

if (a<b)
{
result=a;
} else {
result=b;
}

becomes:

result=step(a,b)*a+step(b,a)*b;

or

test=step(a,b);
result=test*a+(1-test)*b;

float result = sign(min(x, y)) * abs(x) * abs(y);

cheers ffish that works, but visually its not that appealing, im trying to ‘bumpmap’ my shadows
ie the diffuse light = dot( Light, normal ) * 0.75 + 0.25. so the sides away from the light aint completely flat, and modulating that by the shadow amount.
oh well back to the drawing board (jotpad)

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.