// File: SethKasmann[P]clever-girl/src/evaluation.cpp
// Function: Evaluate::Evaluate(State const&)
Evaluate::Evaluate(const State& pState)
: mState(pState)
, mMaterial{}
, mPawnStructure{}
, mMobility{}
, mKingSafety{}
, mAttacks{}
, mPieceAttacksBB{}
, mAllAttacksBB{}
{

// -------------------------------------------------------------------------- //
//                                                                            //
// If any pawns are on the board, check if there is a valid entry in the      //
// pawn hash table. The pawn key for the current state is used to check if    //
// the value in the hash table matches the current state. If a pawn is        //
// neither moved or captured, we can use some of the same evaluation          //
// information. If a valid is found, we can use calculations for:             //
//   1. Pawn Structure                                                        //
//   2. Pawn Material                                                         //
//                                                                            //
// -------------------------------------------------------------------------- //
    if (mState.getPieceCount<pawn>())
    {
        const PawnEntry* pawnEntry = probe(mState.getPawnKey());
        if (pawnEntry && pawnEntry->getKey() == mState.getPawnKey())
        {
            mPawnStructure = pawnEntry->getStructure();
            mMaterial = pawnEntry->getMaterial();
        }
        else
        {
// -------------------------------------------------------------------------- //
//                                                                            //
// If no pawn entry is found in the hash table, evaluate pawns for both       // 
// players and store the result as a new entry in the hash table.             //
//                                                                            //
// -------------------------------------------------------------------------- //
            evalPawns(white);
            evalPawns(black);
            store(mState.getPawnKey(), mPawnStructure, mMaterial);
        }
    }

// -------------------------------------------------------------------------- //
//                                                                            //
// For evaluation concepts that have different weights for mid-game and       //
// end-game (such as PST), a game phase is used to interpolate these values   //
// and avoid any evaluation discontinuity. This is also known as a tapered    //
// evaluation. The game phase is between 0 - 255, closer to 0 puts more       //
// weight on the mid-game, closer to 255 puts more weight on the endgame.     //
//                                                                            //
// -------------------------------------------------------------------------- //
    mGamePhase = mState.getGamePhase();

    evalPieces(white);
    evalPieces(black);

// -------------------------------------------------------------------------- //
//                                                                            //
// Add pawn attacks to the attacks bitboards. These can't be stored in the    //
// pawn hash, since the same configuration of pawns could give different      //
// attack sets depening on the location of non-pawn pieces.                   //
//                                                                            //
// -------------------------------------------------------------------------- //
    mPieceAttacksBB[white][pawn] |= (mState.getPieceBB<pawn>(white) & Not_a_file) << 9
        & mState.getOccupancyBB();
    mPieceAttacksBB[white][pawn] |= (mState.getPieceBB<pawn>(white) & Not_h_file) << 7
        & mState.getOccupancyBB();
    mPieceAttacksBB[black][pawn] |= (mState.getPieceBB<pawn>(black) & Not_a_file) >> 7
        & mState.getOccupancyBB();
    mPieceAttacksBB[black][pawn] |= (mState.getPieceBB<pawn>(black) & Not_h_file) >> 9
        & mState.getOccupancyBB();
    mAllAttacksBB[white] |= mPieceAttacksBB[white][pawn];
    mAllAttacksBB[black] |= mPieceAttacksBB[black][pawn];

    evalAttacks(white);
    evalAttacks(black);

    Color c = mState.getOurColor();
    mScore = mMobility[c]      - mMobility[!c]
          + mKingSafety[c]    - mKingSafety[!c]
          + mPawnStructure[c] - mPawnStructure[!c]
          + mMaterial[c]      - mMaterial[!c];

    mScore += ((mState.getPstScore(middle) * (256 - mGamePhase))
           + mState.getPstScore(late) * mGamePhase) / 256;

// -------------------------------------------------------------------------- //
//                                                                            //
// Tempo is added to the score to give the side to move a slight advantage.   //
//                                                                            //
// -------------------------------------------------------------------------- //
    mScore += tempo;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq 0xe0(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x8(%rdi)
addq $0x14, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x5150
movq 0xa8(%rsp), %rdi
addq $0x1c, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x5150
movq 0xa8(%rsp), %rdi
addq $0x24, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x5150
movq 0xa8(%rsp), %rdi
addq $0x2c, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x5150
movq 0xa8(%rsp), %rdi
addq $0x34, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x5150
movq 0xa8(%rsp), %rdi
addq $0x40, %rdi
movq %rdi, 0xb0(%rsp)
xorl %esi, %esi
movl $0x60, %edx
callq 0x5150
movq 0xb0(%rsp), %rax
movq %rax, %rcx
addq $0x60, %rcx
movq %rcx, 0xb8(%rsp)
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq 0xb8(%rsp), %rcx
addq $0x30, %rax
cmpq %rcx, %rax
movq %rax, 0xc0(%rsp)
jne 0x219f2
movq 0xa8(%rsp), %rdi
addq $0xa0, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x5150
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x24990
cmpl $0x0, %eax
je 0x21b3c
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x24a40
movq %rax, %rdi
callq 0x24a00
movq %rax, 0xd0(%rsp)
cmpq $0x0, 0xd0(%rsp)
je 0x21aed
movq 0xd0(%rsp), %rdi
callq 0x24a50
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movq %rcx, 0xa0(%rsp)
movq 0x8(%rax), %rdi
callq 0x24a40
movq %rax, %rcx
movq 0xa0(%rsp), %rax
cmpq %rcx, %rax
jne 0x21aed
movq 0xd0(%rsp), %rdi
callq 0x24a60
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movq (%rcx), %rcx
movq %rcx, 0x24(%rax)
movq 0xd0(%rsp), %rdi
callq 0x24a70
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movq (%rcx), %rcx
movq %rcx, 0x2c(%rax)
jmp 0x21b3a
movq 0xa8(%rsp), %rdi
xorl %esi, %esi
callq 0x22070
movq 0xa8(%rsp), %rdi
movl $0x1, %esi
callq 0x22070
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x24a40
movq 0xa8(%rsp), %rdx
movq %rax, %rdi
movq %rdx, %rsi
addq $0x24, %rsi
addq $0x2c, %rdx
callq 0x24a80
jmp 0x21b3c
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x24b70
movq 0xa8(%rsp), %rdi
movss %xmm0, (%rdi)
xorl %esi, %esi
callq 0x22610
movq 0xa8(%rsp), %rdi
movl $0x1, %esi
callq 0x22610
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
xorl %esi, %esi
callq 0x24b80
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movabsq $0x7f7f7f7f7f7f7f7f, %rdx # imm = 0x7F7F7F7F7F7F7F7F
andq %rdx, %rcx
shlq $0x9, %rcx
movq %rcx, 0x8(%rsp)
movq 0x8(%rax), %rdi
callq 0x24bc0
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movq 0x8(%rsp), %rax
andq %rcx, %rax
movq %rax, 0x10(%rsp)
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c10
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c40
movq 0x10(%rsp), %rdx
movq %rax, %rcx
movq 0xa8(%rsp), %rax
orq (%rcx), %rdx
movq %rdx, (%rcx)
movq 0x8(%rax), %rdi
xorl %esi, %esi
callq 0x24b80
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movabsq $-0x101010101010102, %rdx # imm = 0xFEFEFEFEFEFEFEFE
andq %rdx, %rcx
shlq $0x7, %rcx
movq %rcx, 0x18(%rsp)
movq 0x8(%rax), %rdi
callq 0x24bc0
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movq 0x18(%rsp), %rax
andq %rcx, %rax
movq %rax, 0x20(%rsp)
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c10
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c40
movq 0x20(%rsp), %rdx
movq %rax, %rcx
movq 0xa8(%rsp), %rax
orq (%rcx), %rdx
movq %rdx, (%rcx)
movq 0x8(%rax), %rdi
movl $0x1, %esi
callq 0x24b80
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movabsq $0x7f7f7f7f7f7f7f7f, %rdx # imm = 0x7F7F7F7F7F7F7F7F
andq %rdx, %rcx
shrq $0x7, %rcx
movq %rcx, 0x28(%rsp)
movq 0x8(%rax), %rdi
callq 0x24bc0
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movq 0x28(%rsp), %rax
andq %rcx, %rax
movq %rax, 0x30(%rsp)
addq $0x40, %rdi
movl $0x1, %esi
callq 0x24c10
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c40
movq 0x30(%rsp), %rdx
movq %rax, %rcx
movq 0xa8(%rsp), %rax
orq (%rcx), %rdx
movq %rdx, (%rcx)
movq 0x8(%rax), %rdi
movl $0x1, %esi
callq 0x24b80
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movabsq $-0x101010101010102, %rdx # imm = 0xFEFEFEFEFEFEFEFE
andq %rdx, %rcx
shrq $0x9, %rcx
movq %rcx, 0x38(%rsp)
movq 0x8(%rax), %rdi
callq 0x24bc0
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movq 0x38(%rsp), %rax
andq %rcx, %rax
movq %rax, 0x40(%rsp)
addq $0x40, %rdi
movl $0x1, %esi
callq 0x24c10
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c40
movq 0x40(%rsp), %rcx
movq 0xa8(%rsp), %rdi
orq (%rax), %rcx
movq %rcx, (%rax)
addq $0x40, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c10
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c40
movq 0xa8(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x48(%rsp)
addq $0xa0, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c70
movq 0x48(%rsp), %rcx
movq 0xa8(%rsp), %rdi
orq (%rax), %rcx
movq %rcx, (%rax)
addq $0x40, %rdi
movl $0x1, %esi
callq 0x24c10
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x24c40
movq 0xa8(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x50(%rsp)
addq $0xa0, %rdi
movl $0x1, %esi
callq 0x24c70
movq 0x50(%rsp), %rcx
movq 0xa8(%rsp), %rdi
orq (%rax), %rcx
movq %rcx, (%rax)
xorl %esi, %esi
callq 0x235b0
movq 0xa8(%rsp), %rdi
movl $0x1, %esi
callq 0x235b0
movq 0xa8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x24ca0
movq 0xa8(%rsp), %rdi
movl %eax, 0xcc(%rsp)
addq $0x14, %rdi
movl 0xcc(%rsp), %eax
movl %eax, %esi
callq 0x24cb0
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movl (%rcx), %ecx
movl %ecx, 0x60(%rsp)
addq $0x14, %rax
movq %rax, 0x58(%rsp)
movl 0xcc(%rsp), %edi
callq 0x24ce0
movq 0x58(%rsp), %rdi
movl %eax, %eax
movl %eax, %esi
callq 0x24cb0
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movl 0x60(%rsp), %eax
subl (%rcx), %eax
movl %eax, 0x64(%rsp)
addq $0x1c, %rdi
movl 0xcc(%rsp), %eax
movl %eax, %esi
callq 0x24cb0
movl 0x64(%rsp), %ecx
movq %rax, %rdx
movq 0xa8(%rsp), %rax
addl (%rdx), %ecx
movl %ecx, 0x70(%rsp)
addq $0x1c, %rax
movq %rax, 0x68(%rsp)
movl 0xcc(%rsp), %edi
callq 0x24ce0
movq 0x68(%rsp), %rdi
movl %eax, %eax
movl %eax, %esi
callq 0x24cb0
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movl 0x70(%rsp), %eax
subl (%rcx), %eax
movl %eax, 0x74(%rsp)
addq $0x24, %rdi
movl 0xcc(%rsp), %eax
movl %eax, %esi
callq 0x24cb0
movl 0x74(%rsp), %ecx
movq %rax, %rdx
movq 0xa8(%rsp), %rax
addl (%rdx), %ecx
movl %ecx, 0x80(%rsp)
addq $0x24, %rax
movq %rax, 0x78(%rsp)
movl 0xcc(%rsp), %edi
callq 0x24ce0
movq 0x78(%rsp), %rdi
movl %eax, %eax
movl %eax, %esi
callq 0x24cb0
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movl 0x80(%rsp), %eax
subl (%rcx), %eax
movl %eax, 0x84(%rsp)
addq $0x2c, %rdi
movl 0xcc(%rsp), %eax
movl %eax, %esi
callq 0x24cb0
movl 0x84(%rsp), %ecx
movq %rax, %rdx
movq 0xa8(%rsp), %rax
addl (%rdx), %ecx
movl %ecx, 0x94(%rsp)
addq $0x2c, %rax
movq %rax, 0x88(%rsp)
movl 0xcc(%rsp), %edi
callq 0x24ce0
movq 0x88(%rsp), %rdi
movl %eax, %eax
movl %eax, %esi
callq 0x24cb0
movl 0x94(%rsp), %ecx
movq %rax, %rdx
movq 0xa8(%rsp), %rax
subl (%rdx), %ecx
movl %ecx, 0x10(%rax)
movq 0x8(%rax), %rdi
xorl %esi, %esi
callq 0x24d00
movl %eax, %ecx
movq 0xa8(%rsp), %rax
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0x98(%rsp)
movss 0x1eff5(%rip), %xmm0    # 0x40ff0
subss (%rax), %xmm0
movss %xmm0, 0x9c(%rsp)
movq 0x8(%rax), %rdi
movl $0x1, %esi
callq 0x24d00
movss 0x98(%rsp), %xmm1
movss 0x9c(%rsp), %xmm2
movl %eax, %ecx
movq 0xa8(%rsp), %rax
cvtsi2ss %ecx, %xmm0
mulss (%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x1efa6(%rip), %xmm0    # 0x40ff0
divss %xmm0, %xmm1
cvtsi2ssl 0x10(%rax), %xmm0
addss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
movl %ecx, 0x10(%rax)
movl 0x10(%rax), %ecx
addl $0xf, %ecx
movl %ecx, 0x10(%rax)
addq $0xe8, %rsp
retq
nop