Splitting GLSL vs. Multiplication

Let's say I have something like:

float foo;

float bar = baz / foo;
float qux = quux / foo;

      

What happens when I do the following:

foo = 1./foo;
float bar = baz * foo;    //can i expect roughly the same number as with division?
floar qux = quux * foo;   //is it always going to be faster?

      

Given the wide gamut of webgl hardware, can we expect something like this to happen under the hood when compiling, or is it just safer to write it like this?

Also interested in

uniform float invFoo;
...
bar = baz * invFoo;
//instead of:
uniform float foo;
...
bar = baz / foo;

      

I don't quite understand this :

// A stupid compiler might use these as written: a divide, then add.
vec4 result1 = (value / 2.0) + 1.0;
vec4 result2 = (value / 2.0) - 1.0;
vec4 result3 = (value / -2.0) + 1.0;

// There are most likely converted to a single MAD operation (per line).
vec4 result1 = (value * 0.5) + 1.0;
vec4 result2 = (value * 0.5) - 1.0;
vec4 result3 = (value * -0.5) + 1.0;

      

I don't understand what the stupid compiler is doing in this context. Is it overlooked that it can be done as a single MAD instruction, or not that division can be expressed as multiplication (at the same time there is no MAD).

Also, how about grouping something like this, but moving around:

vec2 foo,bar;
float baz;

baz = 1./baz;
vec2 temp = vec2( foo.x , bar.x ) * baz; //move but do one mult?
foo.x = temp.x;
bar.x = temp.y;

      

+3


source to share


1 answer


Can you expect about the same number as for division?

Strictly speaking, no:

$ cat a.cc
#include <iostream>

int main() {
    const float a = 1.40129846e-45f;
    const float b = a;

    std::cout << "b / a:       " << (b / a) << std::endl;
    std::cout << "b * (1 / a): " <<(b * (1.f / a)) << std::endl;

    return 0;
}
$ clang++ a.cc && ./a.out
b / a:       1
b * (1 / a): inf
$

      



However, in more or less real life examples, multiplication and division can give similar results. Everything else depends on the stability of the calculation with which you will use the result. If these calculations are unstable (i.e. the result of such a calculation changes significantly, if any of its parameters changes slightly), then there may be a difference in the results. Therefore, you need to know what values ​​you are using and especially what you have. GLSL floats are not always IEEE 32-bit floats, they may be less accurate.

Is it missing that it can be done as a single MAD instruction, or does he not see that division can be expressed as multiplication (with no MAD at the same time).

Depends on the specific quirks of the particular compiler, but probably the second one.

+3


source







All Articles