• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

### #ActualBacterius

Posted 13 June 2013 - 05:54 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.

### #2Bacterius

Posted 13 June 2013 - 05:50 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?

### #1Bacterius

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.

PARTNERS