Multiway branch


Multiway branch is the change to a program's control flow based upon a value matching a selected criteria. It is a form of conditional statement. A multiway branch is often the most efficient method of passing control to one of a set of program labels, especially if an index has been created beforehand from the raw data.

Examples

A multiway branch can, frequently, be replaced with an efficient indexed table lookup
"...the implementation of a switch statement has been equated with that of a multiway branch. However, for many uses of the switch statement in real code, it is possible to avoid branching altogether and replace the switch with one or more table look-ups. For example,the Has30Days example can be implemented as the following:"
by Roger Anthony Sayle

switch

can be replaced, using a "safe-hashing" technique, with -

unsigned int t = x | 2;
switch

or it can be replaced, using an index mapping table lookup, with -

x %= 12; /* to ensure x is in range 0-11*/
static const int T =; /* 0-based table 'if 30 days =1,else 0' */
return T; /* return with boolean 1 = true, 0=false */

Quotations