This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/61621] Normal enum switch slower than test case.


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61621

--- Comment #2 from Holger Seelig <holger.seelig at yahoo dot de> ---
To my understanding and what I know is that a switch like the following:

switch (i)
{
   case 10:
      func_a();
      break;
   case 11:
      func_b();
      break;
   case 12:
      func_c();
      break;
}

Should become something like this:

// N.B.: This is not C++ code
static address jump_table[] = { case_a, case_b, case_c, end };
unsigned int index = i - 10;
if (index > 3) goto end;
goto jump_table [index];
case_a: func_a(); goto end;
case_b: func_b(); goto end;
case_c: func_c();
end:

and with this code a switch should handle many enum cases very efficiently
regardless if a case is executed more frequently.  (With a normal enum type the
second line Âunsigned int index = i - 10;Â could be omitted).

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]