optimization of switch statements on i386

Ian Lance Taylor iant@google.com
Sat Feb 9 02:52:00 GMT 2008


"Godmar Back" <godmar@gmail.com> writes:

> gback@top [324](~/tmp) > cat switch.c
> enum four { A, B, C, D };
> extern void g(int);
> 
> void f(enum four x)
> {
>   switch (x)
>     {
>       case A: g(1); break;
>       case B: g(2); break;
>       case C: g(3); break;
>       case D: g(4); break;
>       default: gcc_unreachable();
>     }
> }

You don't have enough cases.  gcc will always uses a binary search if
there are fewer than five cases.

Try this one.

Ian

enum four { A, B, C, D, E, F, G, H, I, J, K, L, M, N };
extern void g(int);

void f(enum four x)
{
  switch (x)
    {
      case A: g(1); break;
      case B: g(2); break;
      case C: g(3); break;
      case D: g(4); break;
      case E: g(5); break;
      case F: g(6); break;
      case G: g(7); break;
      case H: g(8); break;
      case I: g(9); break;
      case J: g(10); break;
      case K: g(11); break;
      case L: g(12); break;
      case M: g(13); break;
      case N: g(14); break;
      default: gcc_unreachable();
    }
}



More information about the Gcc-help mailing list