Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: OT: switch Statement Performance Consideration

Author: Robert Hyatt

Date: 10:04:45 09/02/02

Go up one level in this thread


On September 02, 2002 at 08:51:21, Gerd Isenberg wrote:

>On September 01, 2002 at 23:33:26, Robert Hyatt wrote:
>
>>On September 01, 2002 at 20:46:55, Pham Hong Nguyen wrote:
>>
>>>The following article and discussion may be useful for you!
>>>
>>>http://www.codeguru.com/cpp_mfc/switch.html
>>
>>
>>That is basically wrong.
>>
>>Compile a simple piece of code with gcc, using -O and you will see
>>why.
>>
>>gcc creates a jump table of addresses for each case.  It computes the
>>entry to load and jumps to that.  No compares and branches.  Someone
>>doesn't know how compilers do things.  If you have really oddball case
>>value, it might have to resort to that.  But for the kinds of values
>>we use in chess, no way...
>
>In MSC there are at least one or two bound check compares before indexing the
>table (even if there is no explicite default case). What about using a table of
>function pointers, even with C++ member functions, specially if your case-code
>already calls subroutines?
>
>class CNode {
> ...
> typedef BitBoard (CNode::*PTR_GETATTACK)(unsigned int sq) const;
> static PTR_GETATTACK m_scPieceAtta[14]; // for each kind of piece
>
> __forceinline BitBoard GetAttack(unsigned int sq, unsigned int piece) const {
>           return (this->*m_scPieceAtta[piece])(sq); }
> ...
>};
>
>CNode::PTR_GETATTACK CNode::m_scPieceAtta[14] =
>{
>  AssertAttack,  // no valid piece code
>  AssertAttack,  // no valid piece code
>  WPawnAttacks,
>  BPawnAttacks,
>  ....
>};


The drawback is that function calls are not free.  To lose the upper/lower
bound range checking, you take a hit on procedure call overhead.  I have not
looked, but many compilers have a way to turn off bounds checking in a switch/
case/etc statement, if you don't mind taking the risk that the code will blow
up if a out-of-bound case value is hit...




This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.