Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: C++ Programming Q: are const and define efficiency the same

Author: Robert Hyatt

Date: 12:43:27 01/17/04

Go up one level in this thread


On January 17, 2004 at 13:50:13, Russell Reagan wrote:

>On January 17, 2004 at 12:24:59, Robert Hyatt wrote:
>
>>There is probably some break-even point in how many times you use one or the
>>other, before the other is preferable.  I wouldn't try to predict without
>>running it however.
>
>One thing I do sometimes (based on my blind faith in the compiler) is store
>intermediate values in a const variable (usually in a function). My hope is that
>the compiler will be able to make the best choice about whether or not to go
>ahead and store the intermediate value, or whether to substitute the small
>computation in the one or two places where it is used in the function. Is this a
>good idea, or is there any reason why I should avoid this?

I don't see how this can be the same thing we were talking about.  To use
an "immediate value" the value must be fixed at _compile time_.  Because only
then can the compiler fill in an immediate operand directly into the
instruction, rather than doing a load from memory..

All I can see your "const" declaration doing below is telling the compiler
"If I modify from or attacks in this function, then produce an error as it
has been declared to be a constant and is not modifiable."

Am I missing what you asked???


>
>An example of something I might do:
>
>// In movegen...
>Bitboard rooksLeft = friendlyRooks;
>while (rooksLeft)
>{
>    const int from = FirstOne(rooksLeft);
>    const Bitboard attacks = RookAttacks(from); // <-- Store intermediate value
>
>    // Generate captures
>    Bitboard moves = attacks & enemyPieces;     // <--- Use attacks once
>    while (moves)
>    {
>        // Generate the captures...
>    }
>
>    // Generate noncaptures
>    moves = attacks & emptySquares;             // <--- Use 'attacks' again
>    while (moves)
>    {
>        // Generate the noncaptures...
>    }
>}



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.