// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r14
pushq %rbx
movq %rdi, %rbx
movq %rsi, 0x8(%rdi)
xorps %xmm0, %xmm0
andq $0x0, 0x34(%rdi)
movups %xmm0, 0x14(%rdi)
movups %xmm0, 0x24(%rdi)
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x50(%rdi)
movups %xmm0, 0x60(%rdi)
movups %xmm0, 0x70(%rdi)
movups %xmm0, 0x80(%rdi)
movups %xmm0, 0x90(%rdi)
movups %xmm0, 0xa0(%rdi)
movl 0x300(%rsi), %eax
addl 0x2e8(%rsi), %eax
je 0xb8ba
movq 0x20(%rsi), %rcx
movl %ecx, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
imulq $0x18, %rax, %rax
leaq 0x13da44(%rip), %r14    # 0x1492a0
cmpq %rcx, (%rax,%r14)
jne 0xb877
addq %rax, %r14
movq 0x8(%r14), %rax
movq %rax, 0x24(%rbx)
movq 0x10(%r14), %rax
movq %rax, 0x2c(%rbx)
jmp 0xb8ba
movq %rbx, %rdi
xorl %esi, %esi
callq 0xba50
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xba50
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
movl %eax, %ecx
andl $0x1fff, %ecx           # imm = 0x1FFF
imulq $0x18, %rcx, %rcx
movq %rax, (%rcx,%r14)
movq 0x24(%rbx), %rax
movq %rax, 0x8(%rcx,%r14)
movq 0x2c(%rbx), %rax
movq %rax, 0x10(%rcx,%r14)
movq 0x8(%rbx), %rsi
movss 0x10(%rsi), %xmm0
movss %xmm0, (%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xbcd4
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xbcd4
movq 0x8(%rbx), %rax
movq 0x288(%rax), %rcx
shlq $0x9, %rcx
movq 0x80(%rax), %rdx
orq 0x78(%rax), %rdx
andq %rcx, %rdx
movabsq $-0x101010101010200, %rsi # imm = 0xFEFEFEFEFEFEFE00
andq %rdx, %rsi
orq 0x40(%rbx), %rsi
movq %rsi, 0x40(%rbx)
movq 0x288(%rax), %rcx
shlq $0x7, %rcx
movq 0x80(%rax), %rdx
orq 0x78(%rax), %rdx
andq %rcx, %rdx
movabsq $0x7f7f7f7f7f7f7f00, %rcx # imm = 0x7F7F7F7F7F7F7F00
andq %rdx, %rcx
orq %rsi, %rcx
movq %rcx, 0x40(%rbx)
movq 0x2b8(%rax), %rdx
movq 0x80(%rax), %rsi
orq 0x78(%rax), %rsi
shrq $0x7, %rdx
andq %rdx, %rsi
movabsq $0xfefefefefefefe, %rdx # imm = 0xFEFEFEFEFEFEFE
andq %rsi, %rdx
orq 0x70(%rbx), %rdx
movq %rdx, 0x70(%rbx)
movq 0x2b8(%rax), %rsi
shrq $0x9, %rsi
movq 0x80(%rax), %rdi
orq 0x78(%rax), %rdi
andq %rsi, %rdi
movabsq $0x7f7f7f7f7f7f7f, %rax # imm = 0x7F7F7F7F7F7F7F
andq %rdi, %rax
orq %rdx, %rax
movq %rax, 0x70(%rbx)
orq %rcx, 0xa0(%rbx)
orq %rax, 0xa8(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xc4d8
movq %rbx, %rdi
movl %ebp, %esi
callq 0xc4d8
movq 0x8(%rbx), %rcx
movl (%rcx), %edx
xorl %esi, %esi
testq %rdx, %rdx
sete %sil
movl 0x14(%rbx,%rdx,4), %eax
shll $0x2, %esi
movl 0x14(%rbx,%rsi), %edi
addl 0x1c(%rbx,%rdx,4), %eax
addl 0x1c(%rbx,%rsi), %edi
addl 0x24(%rbx,%rdx,4), %eax
addl 0x24(%rbx,%rsi), %edi
addl 0x2c(%rbx,%rdx,4), %eax
addl 0x2c(%rbx,%rsi), %edi
subl %edi, %eax
movl %eax, 0x10(%rbx)
movl 0x4(%rcx), %esi
movl 0x318(%rcx,%rdx,8), %edi
movl 0x31c(%rcx,%rdx,8), %edx
subl 0x318(%rcx,%rsi,8), %edi
cvtsi2ss %edi, %xmm0
movss (%rbx), %xmm1
movss 0xb4cb(%rip), %xmm2     # 0x16ee0
subss %xmm1, %xmm2
subl 0x31c(%rcx,%rsi,8), %edx
mulss %xmm0, %xmm2
cvtsi2ss %edx, %xmm0
mulss %xmm1, %xmm0
addss %xmm2, %xmm0
mulss 0xb4ac(%rip), %xmm0     # 0x16ee4
cvtsi2ss %eax, %xmm1
addss %xmm0, %xmm1
cvttss2si %xmm1, %eax
addl $0xf, %eax
movl %eax, 0x10(%rbx)
popq %rbx
popq %r14
popq %rbp
retq
nop