Knowledge

Multiway branch

Source 📝

89:...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 406:
recently wrote me that he considers the use of tables to control program flow as a basic idea of computer science that has been nearly forgotten; but he expects it will be ripe for rediscovery any day now. It is the key to efficiency in all the best compilers I have
393:(in view of the simplicity of the latter case, it would be preferable to implement it in-line, since the overhead of using a function call may be greater than the indexed lookup itself.) 520: 454: 98: 485: 402:
Multiway branching is an important programming technique which is all too often replaced by an inefficient sequence of if tests.
536: 502: 429: 24: 461: 32: 28: 514: 508: 79:(using the data value itself or a calculated derivative of the data value, as the index of an 80: 36: 8: 479: 63: 57: 530: 263: 434: 412: 76: 52: 20: 75:
A multiway branch can, frequently, be replaced with an efficient indexed
403: 40: 23:
based upon a value matching a selected criteria. It is a form of
99:"A Superoptimizer Analysis of Multiway Branch Code Generation" 521:
A Superoptimizer Analysis of Multiway Branch Code Generation
186:
can be replaced, using a "safe-hashing" technique, with -
503:
Coding Multiway Branches Using Customized Hash functions
66:- where a subroutine is invoked and a return is made 376:/* 0-based table 'if 30 days =1,else 0' */ 528: 93:example can be implemented as the following:" 31:method of passing control to one of a set of 417:Structured Programming with go to Statements 388:/* return with boolean 1 = true, 0=false */ 529: 484:: CS1 maint: archived copy as title ( 27:. A multiway branch is often the most 39:has been created beforehand from the 13: 14: 548: 496: 283:/* to ensure x is in range 0-11*/ 262:or it can be replaced, using an 70: 447: 1: 440: 396: 60:- see also alternatives below 19:is the change to a program's 7: 517:By Nell B. Dale, Chip Weems 423: 46: 10: 553: 430:Conditional (programming) 268: 188: 103: 101:by Roger Anthony Sayle 537:Conditional constructs 523:by Roger Anthony Sayle 421: 96: 400: 266:table lookup, with - 85: 25:conditional statement 169:/* November */ 157:/* September */ 145:/* June */ 133:/* April */ 121:/* x is month no */ 35:, especially if an 515:Programming in C++ 64:Multiple dispatch 544: 490: 489: 483: 475: 473: 472: 466: 460:. Archived from 459: 451: 419: 389: 386: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 281: 278: 275: 272: 258: 255: 252: 249: 246: 243: 240: 237: 234: 231: 228: 225: 222: 219: 216: 213: 210: 207: 204: 201: 198: 195: 192: 182: 179: 176: 173: 170: 167: 164: 161: 158: 155: 152: 149: 146: 143: 140: 137: 134: 131: 128: 125: 122: 119: 116: 113: 110: 107: 92: 58:Switch statement 552: 551: 547: 546: 545: 543: 542: 541: 527: 526: 509:Learning Python 499: 494: 493: 477: 476: 470: 468: 464: 457: 455:"Archived copy" 453: 452: 448: 443: 426: 420: 411: 399: 391: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 324: 321: 318: 315: 312: 309: 306: 303: 300: 297: 294: 291: 288: 285: 282: 279: 276: 273: 270: 260: 259: 256: 253: 250: 247: 244: 241: 238: 235: 232: 229: 226: 223: 220: 217: 214: 211: 208: 205: 202: 199: 196: 193: 190: 184: 183: 180: 177: 174: 171: 168: 165: 162: 159: 156: 153: 150: 147: 144: 141: 138: 135: 132: 129: 126: 123: 120: 117: 114: 111: 108: 105: 90: 73: 49: 17:Multiway branch 12: 11: 5: 550: 540: 539: 525: 524: 518: 512: 506: 505:by H. G. Dietz 498: 497:External links 495: 492: 491: 445: 444: 442: 439: 438: 437: 432: 425: 422: 409: 398: 395: 269: 189: 104: 72: 69: 68: 67: 61: 55: 48: 45: 33:program labels 9: 6: 4: 3: 2: 549: 538: 535: 534: 532: 522: 519: 516: 513: 510: 507: 504: 501: 500: 487: 481: 467:on 2012-02-27 463: 456: 450: 446: 436: 433: 431: 428: 427: 418: 414: 408: 405: 394: 267: 265: 264:index mapping 187: 102: 100: 95: 94: 84: 82: 78: 65: 62: 59: 56: 54: 51: 50: 44: 42: 38: 34: 30: 26: 22: 18: 511:By Mark Lutz 469:. Retrieved 462:the original 449: 435:Lookup table 416: 413:Donald Knuth 401: 392: 261: 185: 97: 88: 86: 77:table lookup 74: 71:Alternatives 53:Branch table 21:control flow 16: 15: 471:2009-11-18 441:References 404:Peter Naur 397:Quotations 91:Has30Days 29:efficient 531:Category 480:cite web 424:See also 410:—  407:studied. 191:unsigned 47:Examples 41:raw data 379:return 286:static 248:return 215:switch 172:return 106:switch 465:(PDF) 458:(PDF) 289:const 81:array 37:index 486:link 251:true 239:case 230:case 175:true 160:case 148:case 136:case 124:case 292:int 194:int 533:: 482:}} 478:{{ 415:, 373:}; 277:12 274:%= 242:11 163:11 83:) 43:. 488:) 474:. 385:; 382:T 370:1 367:, 364:0 361:, 358:1 355:, 352:0 349:, 346:0 343:, 340:1 337:, 334:0 331:, 328:1 325:, 322:0 319:, 316:0 313:, 310:0 307:, 304:0 301:{ 298:= 295:T 280:; 271:x 257:} 254:; 245:: 236:: 233:6 227:{ 224:) 221:t 218:( 212:; 209:2 206:| 203:x 200:= 197:t 181:} 178:; 166:: 154:: 151:9 142:: 139:6 130:: 127:4 118:{ 115:) 112:x 109:( 87:"

Index

control flow
conditional statement
efficient
program labels
index
raw data
Branch table
Switch statement
Multiple dispatch
table lookup
array
"A Superoptimizer Analysis of Multiway Branch Code Generation"
index mapping
Peter Naur
Donald Knuth
Conditional (programming)
Lookup table
"Archived copy"
the original
cite web
link
Coding Multiway Branches Using Customized Hash functions
Learning Python
Programming in C++
A Superoptimizer Analysis of Multiway Branch Code Generation
Category
Conditional constructs

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.