PDA

View Full Version : const usuage



hbuel
02-13-2007, 05:24 AM
Hi,

I was wondering: are the current GPUs somehow paying attention to the const qualifier when using it inside some function arguments?

For example - would any fragment be faster on the screen, if I wrote:

float foo( const float x );
// instead of
float fooTwo( float x);
Thank you very much!

Zengar
02-13-2007, 11:14 AM
Most GPU's inline the functions anyhow, so I would tip on "no"

Korval
02-13-2007, 12:57 PM
Const is, and has always been, a compiler thing. It doesn't change the code that is generated; it simply allows the compiler to catch semantic mistakes the programmer makes. So if you have a const float x, and you change x, the compiler can complain. But it won't change the code generated.

Zengar
02-13-2007, 02:27 PM
Well, no exacly... In GLSL, yes, but in C-family const will imply passing arguments by reference

Humus
02-14-2007, 10:36 AM
const does not imply pass by reference. To pass by reference you need to write:

float foo( const float &x );

V-man
02-15-2007, 12:04 AM
float foo( const float x );


For the above line, I think it makes no difference but for

const float myvar=2.0;

maybe the compiler will generate special instructions for certain const numbers and depending on how you use it.

Other numbers that might be special could be
1.0
0.5
4.0
8.0

Zengar
02-15-2007, 12:21 AM
Humus, Korval, I guess you were right, sorry *embarassed* It seems I was thinking about pascal...