Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: multiplication question

  1. #1
    Senior Member OpenGL Guru zed's Avatar
    Join Date
    Jul 2000
    Location
    S41.16.25 E173.16.21
    Posts
    2,609

    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. #2
    Advanced Member Frequent Contributor
    Join Date
    Feb 2001
    Location
    Australia
    Posts
    587

    Re: multiplication question

    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.

  3. #3
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    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. #4
    Junior Member Regular Contributor
    Join Date
    Jan 2002
    Location
    France
    Posts
    134

    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. #5
    Senior Member OpenGL Guru zed's Avatar
    Join Date
    Jul 2000
    Location
    S41.16.25 E173.16.21
    Posts
    2,609

    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
  •