# Thread: multiplication question

1. ## 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

2. ## Re: multiplication question

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.

3. ## Re: multiplication question

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.

4. ## Re: multiplication question

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;

5. ## Re: multiplication question

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)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•