• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

# changing code on its roots

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1lomateron  Members

Posted 13 June 2013 - 05:39 PM

I have this code

float Code(float a,float b, int c)

{

float r=0;

if( c==0)

{

r=a*b;

}

else if( c==1)

{

r=a/b;

}

else if( c==2)

{

r=2*a;

}

return r;

}

Lets say i don't have 3 different "if" but a thousand different "if", and "c" as it is in the code just selects one "if"

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code

Or is there anything related to this that solves the problem in a different way?

Edited by lomateron, 13 June 2013 - 08:30 PM.

### #2Bacterius  Members

Posted 13 June 2013 - 05:49 PM

A switch statement? It'll create a jump table to efficiently map code to execute to the value of c.

switch(c)
{
case 0: r = a * b; break;
case 1: r = a / b; break;
case 2: r = 2 * a; break;
// ...
}

Or you can directly return if you have nothing to after the conditional.

EDIT: damn, missed the HLSL tag, I don't think it has switch case support. In this case, does the value of c stay the same for every shader invocation? If so, you might be able to have the compiler optimize it down. Otherwise, if it changes dynamically, I'm afraid you're going to have to do the comparisons in any case.. are you looking for syntactic sugar or an actual performance improvement? GPU's aren't that good at control flow so any "clever" hacks to minimize the number of comparisons might yield even worse performance. If there's any way you can weave "c" directly into the formula that would be ideal of course, but it doesn't seem possible here.

Edited by Bacterius, 13 June 2013 - 05:54 PM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #3lomateron  Members

Posted 13 June 2013 - 06:05 PM

HLSL supports switches and in my case the "if"s will translate the same way a switch will do to assembly code

don't derail my real question

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code while the application runs

### #4Juliean  Members

Posted 13 June 2013 - 06:26 PM

Has anyone think about a way to make this faster by creating a way of recompiling the code so it deletes all the unnecessary "if" lines in the assembly code while the application runs

Maybe shader permuation with compile defines?

#ifdef SOMETHING
r = a*b;
#else ifdef SOMETHINGELSE
r = a/b;
#else
r = 2*a;
#endif


Pardon me if I got the actual defines wrong. You can now employ a system, which is called "shader permumations", which will generate for each shader a set of possible compile-define combinations (in that case, a maximum of three). I don't know if this is too complicated/sofisticated for you, you might can up with an easier system based on that, but thats AFAIK the only way to fully eliminate the if-statements.

Edited by Juliean, 13 June 2013 - 06:26 PM.

### #5Adam_42  Members

Posted 13 June 2013 - 06:29 PM

You can do it with somewhat less code modification like this, but you'll still need to recompile modified source for each shader:

#define c 42

float Code(float a,float b)
{
// original logic stays here
}


### #6lomateron  Members

Posted 13 June 2013 - 06:46 PM

this solves what I was asking

### #7MJP  Moderators

Posted 13 June 2013 - 07:27 PM

Dynamic linking can definitely be used to implement this, although it's a little wacky to use and will often generate sub-optimal code. Personally I would just do this by pre-compiling several permutations of the shader, with the value of c defined in a a preprocessor macro (similar to what Adam_42 suggests). Doing it this way allows the compiler to completely optimize away the if statement, and also any additional operations performed with the value of c. You can specify the macro definition when compiling the shader using the "pDefines" parameter of D3DCompile, and then just compile the shaders in a for loop.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.