Except the control flow graph of your program is completely different. The cmp3() function now gets executed every time an alternate loop is selected. What if the function had side effects?dodicat wrote:Here is the original wanted, with C (loosly similar to switch) type code, cascading through the cases, visiting each one in turn
And the purpose of the construct, is to reduce the number of branches taken in a performance critical core loop.
It reduces it from 2 branches (+ an exit condition) to: between 1 and 2 (+ exit) depending on the data, saving half a branch and improving the PBU predictions too.
Calling the SELECT CASE again makes it pointless, that's even more expensive.
As for GOTOs as a language feature. I do avoid them when reasonable. Probably 95% of my programs are "goto" free. But sometimes the control flow graph you need, doesn't fit into nice: nested loops, nested branches, or combination thereof; In which case: you need them.
And yes I know, Branchless programming is a thing, "MOV" is Turing-Complete, and technically any program can be converted to a linear series of instructions, But the cost is abysmal speed and lack of readability that puts the worst Spaghetti code to shame! Fewer branches are not always more readable than more. (I mean, the MOVfuscator was conceived as a (joke) anti-reverse-engineering tool after all.)