Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Switch-Case Optimization

Glum_Person
Beginner
403 Views
I try to use ICC, which is positioned as optimizing, and wrote simplest test code for comparison with MS compiler. int __fastcall TestFunc(int Arg) { static const int RetVals[] = { 0,1,2,3 }; switch (Arg &= 3) { case 0: return RetVals[0]; case 1: return RetVals[1]; case 2: return RetVals[2]; case 3: return RetVals[3]; default: __assume(0); } }; Disassembler output of this sample compiled by MS compiler: http://a.radikal.ru/a17/2010/a7/4707764b260b.png Disassembler output of this sample compiled by Intel compiler: http://c.radikal.ru/c24/2010/28/08812a62def0.png o_O I don't understand where are announced perfect optimization?
0 Kudos
4 Replies
GouthamK_Intel
Moderator
370 Views

Hi,

Thanks for reaching out to us!

Could you please provide detail of the compiler and its version which you are referring to?

Also, provide the commands, compiler flags you used along with steps to reproduce.

Since your comparing optimizations at the assembly level, we are forwarding this case to Subject Matter Experts.


Thanks & Regards

Goutham


Viet_H_Intel
Moderator
307 Views

Please provide us a complete test case, compiler version and compiler options to investigate.

Thanks,


Viet_H_Intel
Moderator
185 Views

Since we have not heard from you, we assume that this is no longer an issue. Thus, we will no longer respond to this thread.  

If you require additional assistance from Intel, please start a new thread. Any further interaction in this thread will be considered community only.

Thanks,


jimdempseyatthecove
Black Belt
172 Views

RE:

 int __fastcall TestFunc(int Arg) {
  static const int RetVals[] = { 0,1,2,3 };
  switch (Arg &= 3) {
    case 0: return RetVals[0];
    case 1: return RetVals[1];
    case 2: return RetVals[2];
    case 3: return RetVals[3];
    default: __assume(0);
  }
}; 

In the specific case above, even the MS compiler did not fully optimize the code above. That specific function (and return static const RetVals list) could have been reduced to return(Arg & 3).

FWIW your source statement switch(Arg &= 3) should not have had the "=".

Your code .png for the MS example did not show the dispatch table referenced by

jmp [00401020+ecx*4]

The execution of the MS code would have required a memory fetch from the dispatch table which is not in the instruction pipeline.

In a case of arbitrary values in RetVals, your source code should have been written as

return(RetVals[Arg&3]);

IOW, you should work with your compiler as opposed to relying on the compiler to correct your inefficiencies.

For this example, both compilers did poorly.

Jim Dempsey

 

Reply