#include "bithistory.h"
#include <memory.h>

BitHistory::BitHistory()
{
    clear();
}

void BitHistory::clear()
{
    memset( history, 0, sizeof(history) );
#ifdef USE_COUNTER
	memset( counter, 0, sizeof(counter) );
#endif
}

void BitHistory::add( const BitMove &bm, const BitBoard &b, i32 depth )
{
    if ( (bm.flags & (bcapture | bcastling)) || bm.promo )
    {
        return;			// don't add these!
    }
    i32 turn = b.flags().turn;
    i32 pt = b.pieceType( bm.from );
	ASSERT( depth > 0 );
    i32 inc = depth*depth;
    u16 &h = history[ turn ][ pt ][ bm.to ];
    if ( (u16)( h + inc ) <= h )
    {
        // overflow!
        i32 i, j, k;
        for (i=0; i<2; i++)
        {
            for (j=0; j<7; j++)
            {
                for (k=0; k<64; k++)
                {
                    history[ i ][ j ][ k ] >>= 1;
                }
            }
        }
    }
    h += inc;
}
