Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/board_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ int mvvLva[12][12] = {
};

int minorPieces[6] = {B, b, N, n, K, k};
int majorPieces[4] = {R, r, Q, q};
int whiteNonPawnPieces[5] = { R, N, B, Q, K };
int blackNonPawnPieces[5] = {r, n, b, q, k };

Expand Down
1 change: 1 addition & 0 deletions src/board_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern char promotedPieces[];
extern char *unicodePieces[12];
extern int charPieces[];
extern int minorPieces[6];
extern int majorPieces[4];
extern int whiteNonPawnPieces[5];
extern int blackNonPawnPieces[5];
extern int mvvLva[12][12];
Expand Down
1 change: 1 addition & 0 deletions src/fen.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void parseFEN(char *fen, board* position) {
position->hashKey = generateHashKey(position);
position->pawnKey = generatePawnKey(position);
position->minorKey = generateMinorKey(position);
position->majorKey = generateMajorKey(position);
position->whiteNonPawnKey = generate_white_np_hash_key(position);
position->blackNonPawnKey = generate_black_np_hash_key(position);
}
Expand Down
35 changes: 34 additions & 1 deletion src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void copyBoard(board *p, struct copyposition *cp) {
cp->hashKeyCopy = p->hashKey;
cp->pawnKeyCopy = p->pawnKey;
cp->minorKeyCopy = p->minorKey;
cp->majorKeyCopy = p->majorKey;
cp->whiteNonPawnKeyCopy = p->whiteNonPawnKey;
cp->blackNonPawnKeyCopy = p->blackNonPawnKey;
cp->sideCopy = p->side, cp->enpassantCopy = p->enpassant, cp->castleCopy = p->castle;
Expand All @@ -68,6 +69,7 @@ void takeBack(board *p, struct copyposition *cp) {
p->hashKey = cp->hashKeyCopy;
p->pawnKey = cp->pawnKeyCopy;
p->minorKey = cp->minorKeyCopy;
p->majorKey = cp->majorKeyCopy;
p->whiteNonPawnKey = cp->whiteNonPawnKeyCopy;
p->blackNonPawnKey = cp->blackNonPawnKeyCopy;
p->side = cp->sideCopy, p->enpassant = cp->enpassantCopy, p->castle = cp->castleCopy;
Expand Down Expand Up @@ -162,6 +164,13 @@ bool isMinor (int piece) {
return false;
}

bool isMajor (int piece) {
if (piece == Q || piece == q || piece == R || piece == r) {
return true;
}
return false;
}

// make move on chess board
int makeMove(int move, int moveFlag, board* position) {
int isLegalCapture = getMoveCapture(move);
Expand Down Expand Up @@ -212,6 +221,12 @@ int makeMove(int move, int moveFlag, board* position) {
position->minorKey ^= pieceKeys[piece][targetSquare];
}

if (isMajor(piece)) {
position->majorKey ^= pieceKeys[piece][sourceSquare];
position->majorKey ^= pieceKeys[piece][targetSquare];

}


// handling capture moves
if (capture) {
Expand Down Expand Up @@ -248,6 +263,11 @@ int makeMove(int move, int moveFlag, board* position) {
if (isMinor(bbPiece)) {
position->minorKey ^= pieceKeys[bbPiece][targetSquare];
}

if (isMajor(bbPiece)) {
position->majorKey ^= pieceKeys[bbPiece][targetSquare];
}

break;
}
}
Expand Down Expand Up @@ -316,6 +336,11 @@ int makeMove(int move, int moveFlag, board* position) {
if (isMinor(promotedPiece)) {
position->minorKey ^= pieceKeys[promotedPiece][targetSquare];
}

if (isMajor(promotedPiece)) {
position->majorKey ^= pieceKeys[promotedPiece][targetSquare];

}
}


Expand Down Expand Up @@ -351,6 +376,8 @@ int makeMove(int move, int moveFlag, board* position) {
position->hashKey ^= pieceKeys[R][f1]; // put rook on f1 into a hash key
position->whiteNonPawnKey ^= pieceKeys[R][h1];
position->whiteNonPawnKey ^= pieceKeys[R][f1];
position->majorKey ^= pieceKeys[R][h1];
position->majorKey ^= pieceKeys[R][f1];
break;

// white castles queen side
Expand All @@ -366,6 +393,8 @@ int makeMove(int move, int moveFlag, board* position) {
position->hashKey ^= pieceKeys[R][d1]; // put rook on d1 into a hash key
position->whiteNonPawnKey ^= pieceKeys[R][a1];
position->whiteNonPawnKey ^= pieceKeys[R][d1];
position->majorKey ^= pieceKeys[R][a1];
position->majorKey ^= pieceKeys[R][d1];
break;

// black castles king side
Expand All @@ -381,6 +410,8 @@ int makeMove(int move, int moveFlag, board* position) {
position->hashKey ^= pieceKeys[r][f8]; // put rook on f8 into a hash key
position->blackNonPawnKey ^= pieceKeys[r][h8];
position->blackNonPawnKey ^= pieceKeys[r][f8];
position->majorKey ^= pieceKeys[r][h8];
position->majorKey ^= pieceKeys[r][f8];
break;

// black castles queen side
Expand All @@ -396,6 +427,8 @@ int makeMove(int move, int moveFlag, board* position) {
position->hashKey ^= pieceKeys[r][d8]; // put rook on d8 into a hash key
position->blackNonPawnKey ^= pieceKeys[r][a8];
position->blackNonPawnKey ^= pieceKeys[r][d8];
position->majorKey ^= pieceKeys[r][a8];
position->majorKey ^= pieceKeys[r][d8];
break;
}
}
Expand Down Expand Up @@ -442,7 +475,7 @@ int makeMove(int move, int moveFlag, board* position) {
takeBack(position, &copyPosition);
// return illegal move
return 0;
}
}

return 1;
}
Expand Down
22 changes: 20 additions & 2 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@

int PAWN_CORRECTION_HISTORY[2][16384];
int MINOR_CORRECTION_HISTORY[2][16384];
int NON_PAWN_CORRECTION_HISTORY[2][2][16384];
int MAJOR_CORRECTION_HISTORY[2][16384];
int NON_PAWN_CORRECTION_HISTORY[2][2][16384];


/*╔═══════════════════════════════╗
Expand Down Expand Up @@ -431,6 +432,20 @@ void updateMinorCorrectionHistory(board *position, const int depth, const int di
MINOR_CORRECTION_HISTORY[position->side][minorKey % CORRHIST_SIZE] = entry;
}

void updateMajorCorrectionHistory(board *position, const int depth, const int diff) {
U64 majorKey = generateMajorKey(position);

int entry = MAJOR_CORRECTION_HISTORY[position->side][majorKey % CORRHIST_SIZE];

const int scaledDiff = diff * CORRHIST_GRAIN;
const int newWeight = 2 * myMIN(depth + 1, 16);

entry = (entry * (CORRHIST_WEIGHT_SCALE - newWeight) + scaledDiff * newWeight) / CORRHIST_WEIGHT_SCALE;
entry = clamp(entry, -CORRHIST_MAX, CORRHIST_MAX);

MAJOR_CORRECTION_HISTORY[position->side][majorKey % CORRHIST_SIZE] = entry;
}

void update_non_pawn_corrhist(board *position, const int depth, const int diff) {
U64 whiteKey = position->whiteNonPawnKey;
U64 blackKey = position->blackNonPawnKey;
Expand All @@ -455,9 +470,11 @@ void update_non_pawn_corrhist(board *position, const int depth, const int diff)
int adjustEvalWithCorrectionHistory(board *position, const int rawEval) {
U64 pawnKey = position->pawnKey;
U64 minorKey = position->minorKey;
U64 majorKey = position->majorKey;

int pawnEntry = PAWN_CORRECTION_HISTORY[position->side][pawnKey % CORRHIST_SIZE];
int minorEntry = MINOR_CORRECTION_HISTORY[position->side][minorKey % CORRHIST_SIZE];
int majorEntry = MAJOR_CORRECTION_HISTORY[position->side][majorKey % CORRHIST_SIZE];

U64 whiteNPKey = position->whiteNonPawnKey;
int whiteNPEntry = NON_PAWN_CORRECTION_HISTORY[white][position->side][whiteNPKey % CORRHIST_SIZE];
Expand All @@ -467,7 +484,7 @@ int adjustEvalWithCorrectionHistory(board *position, const int rawEval) {

int mateFound = mateValue - maxPly;

int adjust = pawnEntry + minorEntry + whiteNPEntry + blackNPEntry;
int adjust = pawnEntry + minorEntry + majorEntry + whiteNPEntry + blackNPEntry;

return clamp(rawEval + adjust / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1);
}
Expand Down Expand Up @@ -1406,6 +1423,7 @@ int negamax(int alpha, int beta, int depth, board* pos, time* time, bool cutNode
int corrhistBonus = clamp(bestScore - static_eval, -CORRHIST_LIMIT, CORRHIST_LIMIT);
updatePawnCorrectionHistory(pos, depth, corrhistBonus);
updateMinorCorrectionHistory(pos, depth, corrhistBonus);
updateMajorCorrectionHistory(pos, depth, corrhistBonus);
update_non_pawn_corrhist(pos, depth, corrhistBonus);
}

Expand Down
4 changes: 4 additions & 0 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern int counterMoves[2][maxPly][maxPly];
extern int PAWN_CORRECTION_HISTORY[2][16384];
extern int MINOR_CORRECTION_HISTORY[2][16384];
extern int NON_PAWN_CORRECTION_HISTORY[2][2][16384];
extern int MAJOR_CORRECTION_HISTORY[2][16384];

int isRepetition(board* position);
uint8_t isMaterialDraw(board *pos);
Expand All @@ -48,6 +49,9 @@ int SEE(board *pos, int move, int threshold);
uint64_t all_attackers_to_square(board *pos, uint64_t occupied, int sq);
void updatePawnCorrectionHistory(board *position, const int depth, const int diff);
void updateMinorCorrectionHistory(board *position, const int depth, const int diff);
void updateMajorCorrectionHistory(board *position, const int depth, const int diff);
int adjustEvalWithCorrectionHistory(board *position, const int rawEval);
void update_non_pawn_corrhist(board *position, const int depth, const int diff);
int quiescence(int alpha, int beta, board* position, time* time);
void quiescence_sort_moves(moves *moveList, board* position);
int quiescenceScoreMove(int move, board* position);
Expand Down
2 changes: 2 additions & 0 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef struct {
U64 hashKey;
U64 pawnKey;
U64 minorKey;
U64 majorKey;
U64 whiteNonPawnKey;
U64 blackNonPawnKey;

Expand Down Expand Up @@ -85,6 +86,7 @@ struct copyposition {
U64 hashKeyCopy;
U64 pawnKeyCopy;
U64 minorKeyCopy;
U64 majorKeyCopy;
U64 whiteNonPawnKeyCopy;
U64 blackNonPawnKeyCopy;

Expand Down
22 changes: 22 additions & 0 deletions src/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ U64 generateMinorKey(board *position) {
return final_key;
}

U64 generateMajorKey(board *position) {
uint64_t final_key = 0ULL;
uint64_t bitboard;


for (int i = 0; i < 4; i++) {

int piece = majorPieces[i];
bitboard = position->bitboards[piece];

while (bitboard) {

int square = getLS1BIndex(bitboard);

final_key ^= pieceKeys[piece][square];
popBit(bitboard, square);
}
}

return final_key;
}

// generates white non pawn hashing key
U64 generate_white_np_hash_key(board *position) {
uint64_t final_key = 0ULL;
Expand Down
1 change: 1 addition & 0 deletions src/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ int readHashEntry(board *position, int *move, int16_t *tt_score,
uint8_t *tt_depth, uint8_t *tt_flag, bool *tt_pv);
U64 generatePawnKey(board* position);
U64 generateMinorKey(board *position);
U64 generateMajorKey(board *position);
U64 generate_white_np_hash_key(board *position);
U64 generate_black_np_hash_key(board *position);
void clearHashTable(void);
Expand Down
1 change: 1 addition & 0 deletions src/uci.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ void uciProtocol(int argc, char *argv[], board *position, time *time_ctrl) {
memset(PAWN_CORRECTION_HISTORY, 0, sizeof(PAWN_CORRECTION_HISTORY));
memset(continuationHistory, 0, sizeof(continuationHistory));
memset(MINOR_CORRECTION_HISTORY, 0, sizeof(PAWN_CORRECTION_HISTORY));
memset(MAJOR_CORRECTION_HISTORY, 0, sizeof(MAJOR_CORRECTION_HISTORY));
memset(NON_PAWN_CORRECTION_HISTORY, 0, sizeof(NON_PAWN_CORRECTION_HISTORY));

// call parse position function
Expand Down