A.D.A. Amiga Demoscene Archive

        Welcome guest!

  

  

  

log in with SceneID

  

Demos Amiga Demoscene Archive Forum / Coding / Avoiding deeply nested switch statements

 

Author Message
dalton
Member
#1 - Posted: 11 Nov 2016 12:53
Reply Quote
I sometimes find myself in a situation where I have to put some kind of conditional execution in a deeply nested loop, but the actual condition doesn't change during the loop, so the same path of execution will occur each time, like this:

a = something;

for (...)
{
for (...)
{
for (...)
{
// big chunk of code here

if (a == 5)
{
// some code
}
else if (a == 7)
{
// some other code
}
else
{
// some third code
}

// another big chunk of code here
}
}
}


In this situation I feel like putting the conditions outside of the loop statements, but then I have several copies of those "big chunks of code". That makes it a pain to maintain the code, because any change has to go in several places.

I could also imagine placing a call to a function pointer in the inner loop. That pointer could be set outside of the loops. It would however introduce the overhead of a function call in the inner loop. Bad idea?

Maybe the deeply nested switch statement is not a problem on the 68060, due to branch prediction?

Cheers
krabob
Member
#2 - Posted: 12 Nov 2016 14:43
Reply Quote
You.... are scanning a marching cube , do you ? :)
dalton
Member
#3 - Posted: 13 Nov 2016 13:15
Reply Quote
Actually I'm sorting :) There are so many variations (ascending, descending, signedness, float or int). It would be a mess to maintain all those variations. The only real difference is a deeply nested comparison operation.
krabob
Member
#4 - Posted: 19 Nov 2016 17:43 - Edited
Reply Quote
sort of related,...
I've just discovered even a 68000 can do this:

jsr (d0.w, pc, _label.w)

... "wouah".

 

  Please log in to comment

  

  

  

 

A.D.A. Amiga Demoscene Archive, Version 3.0