// File: /workspace/github_cpp_makefile_star2_O1/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 %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq %rsi, 0x8(%rdi)
xorps %xmm0, %xmm0
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, 0x14(%rdi)
movups %xmm0, 0x24(%rdi)
movq $0x0, 0x34(%rdi)
movups %xmm0, 0xa0(%rdi)
movl 0x300(%rsi), %eax
addl 0x2e8(%rsi), %eax
je 0xe0d8
movq 0x20(%rsi), %rcx
movl %ecx, %eax
andl $0x1fff, %eax           # imm = 0x1FFF
leaq (%rax,%rax,2), %rax
leaq 0x10d205(%rip), %r14    # 0x11b290
cmpq %rcx, (%r14,%rax,8)
jne 0xe09f
leaq (%r14,%rax,8), %rax
movups 0x8(%rax), %xmm0
movups %xmm0, 0x24(%rbx)
jmp 0xe0d8
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe27e
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe27e
movq 0x8(%rbx), %rax
movq 0x20(%rax), %rax
movl %eax, %ecx
andl $0x1fff, %ecx           # imm = 0x1FFF
leaq (%rcx,%rcx,2), %rcx
movq %rax, (%r14,%rcx,8)
movups 0x24(%rbx), %xmm0
movups %xmm0, 0x8(%r14,%rcx,8)
movq 0x8(%rbx), %rax
movss 0x10(%rax), %xmm0
movss %xmm0, (%rbx)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe506
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe506
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 0xee24
movq %rbx, %rdi
movl $0x1, %esi
callq 0xee24
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 0xdd16(%rip), %xmm2     # 0x1bf50
subss %xmm1, %xmm2
subl 0x31c(%rcx,%rsi,8), %edx
mulss %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
mulss %xmm1, %xmm0
addss %xmm2, %xmm0
mulss 0xdcf4(%rip), %xmm0     # 0x1bf54
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
addss %xmm0, %xmm1
cvttss2si %xmm1, %eax
addl $0xf, %eax
movl %eax, 0x10(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
retq
nop