Index mapping: Difference between revisions

Content deleted Content added
C example 1: ref pages
C example 1: rewrite example
Line 22:
 
===C example 1===
ThisRoger Sayle gives an example<ref>{{cite journal|last1=Sayle|first1=Roger Anthony|title=A Superoptimizer Analysis of Multiway Branch Code Generation|journal=Proceedings of the GCC Developers’ Summit|date=June 17, 2008|pages=103-116|url=https://www.nextmovesoftware.com/technology/SwitchOptimization.pdf|accessdate=26 November 2015}}</ref> of eliminating a Cmultiway functionbranch caused returning TRUE ifby a monthswitch (x) contains 30 days (otherwise FALSE), illustrates the concept succinctlystatement:
 
<source lang="c++">
if (((unsigned)x > 12) || ((unsigned)x <= 0) return 0; /*x>12 or x<=0?*/
bool Has30Days(int m) {
static const int T[12] ={0,0,0,1,0,1,0,0,1,0,1,0}; /* 0-based table 'if 30 days =1,else 0' */
switch (m) {
return T[x - 1]; /* return with boolean 1 = true, 0=false */
case 4: // April
case 6: // June
case 9: // September
case 11: // November
return true;
default:
return false;
}
}
</source>
 
Which can be replaced with a table lookup:
 
<source lang="c++">
bool Has30Days(int m) {
static const bool T[] = { 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };
return T[m];
}
</source>