Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip evaluating and stand-pat if we are in check #242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bash build-mini.sh

## 4ku-mini Size
```
3,946 bytes
3,941 bytes
```

---
Expand Down
54 changes: 32 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,25 +356,35 @@ void generate_piece_moves(Move *const movelist,
const i32 phases[] = {0, 1, 1, 2, 4, 0};
const i32 max_material[] = {139, 449, 452, 841, 1674, 0, 0};
const i32 material[] = {S(100, 139), S(329, 449), S(341, 452), S(455, 841), S(825, 1674), 0};
const i32 pst_rank[][8] = {
{0, S(-2, 0), S(-3, -1), S(-1, -1), S(1, 0), S(5, 2), 0, 0},
{S(-4, -5), S(-2, -3), S(-1, -1), S(1, 3), S(3, 4), S(7, 1), S(5, 0), S(-11, 1)},
{S(-2, -2), S(1, -2), S(1, 0), S(1, 0), S(2, 1), S(4, 0), S(1, 0), S(-8, 2)},
{S(-2, -4), S(-2, -4), S(-3, -2), S(-4, 1), S(-1, 2), S(3, 2), S(3, 3), S(6, 1)},
{S(1, -13), S(1, -10), S(0, -5), S(-1, 1), S(-1, 6), S(1, 5), S(-2, 9), S(1, 7)},
{S(0, -6), S(0, -2), S(-2, 0), S(-4, 3), S(-1, 4), S(5, 4), S(3, 3), S(4, -4)},
const i32 pst_rank[] = {
0, S(-2, 0), S(-3, -1), S(-1, -1), S(1, 0), S(5, 2), 0, 0, // Pawn
S(-4, -5), S(-2, -3), S(-1, -1), S(1, 3), S(3, 4), S(7, 1), S(5, 0), S(-11, 1), // Knight
S(-2, -2), S(1, -2), S(1, 0), S(1, 0), S(2, 1), S(4, 0), S(1, 0), S(-8, 2), // Bishop
S(-2, -4), S(-2, -4), S(-3, -2), S(-4, 1), S(-1, 2), S(3, 2), S(3, 3), S(6, 1), // Rook
S(1, -13), S(1, -10), S(0, -5), S(-1, 1), S(-1, 6), S(1, 5), S(-2, 9), S(1, 7), // Queen
S(0, -6), S(0, -2), S(-2, 0), S(-4, 3), S(-1, 4), S(5, 4), S(3, 3), S(4, -4) // King
};
const i32 pst_file[][8] = {
{S(-2, 1), S(-1, 1), S(-1, 0), S(0, -1), S(1, 0), S(2, 0), S(2, 0), S(-1, -1)},
{S(-4, -3), S(-2, -1), S(0, 1), S(2, 3), S(2, 2), S(2, 0), S(1, -1), S(-1, -3)},
{S(-2, 0), 0, S(1, 0), S(0, 1), S(0, 1), S(-1, 1), S(2, -1), S(0, -1)},
{S(-2, 0), S(-2, 1), S(-1, 1), S(1, 0), S(1, -1), S(1, 0), S(2, 0), S(-1, -1)},
{S(-3, -4), S(-2, -2), S(-1, 0), S(0, 1), S(0, 2), S(1, 3), S(2, 1), S(3, -1)},
{S(-2, -5), S(2, -1), S(-2, 1), S(-3, 2), S(-4, 2), S(-1, 1), S(2, -1), S(0, -5)},
const i32 pst_file[] = {
S(-2, 1), S(-1, 1), S(-1, 0), S(0, -1), S(1, 0), S(2, 0), S(2, 0), S(-1, -1), // Pawn
S(-4, -3), S(-2, -1), S(0, 1), S(2, 3), S(2, 2), S(2, 0), S(1, -1), S(-1, -3), // Knight
S(-2, 0), 0, S(1, 0), S(0, 1), S(0, 1), S(-1, 1), S(2, -1), S(0, -1), // Bishop
S(-2, 0), S(-2, 1), S(-1, 1), S(1, 0), S(1, -1), S(1, 0), S(2, 0), S(-1, -1), // Rook
S(-3, -4), S(-2, -2), S(-1, 0), S(0, 1), S(0, 2), S(1, 3), S(2, 1), S(3, -1), // Queen
S(-2, -5), S(2, -1), S(-2, 1), S(-3, 2), S(-4, 2), S(-1, 1), S(2, -1), S(0, -5) // King
};
const i32 open_files[][5] = {
{S(3, 4), S(-4, 20), S(20, 16), S(3, 19), S(-23, 10)},
{S(-3, -12), S(-11, 0), S(47, 0), S(-13, 35), S(-63, 1)},
const i32 open_files[] = {
// Semi open files
S(3, 4),
S(-4, 20),
S(20, 16),
S(3, 19),
S(-23, 10),
// Open files
S(-3, -12),
S(-11, 0),
S(47, 0),
S(-13, 35),
S(-63, 1),
};
const i32 mobilities[] = {S(9, 5), S(8, 7), S(4, 4), S(4, 3), S(-5, -1)};
const i32 pawn_protection[] = {S(24, 14), S(2, 16), S(8, 17), S(9, 8), S(-5, 23), S(-34, 26)};
Expand Down Expand Up @@ -425,8 +435,8 @@ const i32 pawn_attacked_penalty[] = {S(64, 14), S(155, 142)};
const i32 file = sq % 8;

// Split quantized PSTs
score += pst_rank[p][rank] * 8;
score += pst_file[p][file] * 8;
score += pst_rank[p * 8 + rank] * 8;
score += pst_file[p * 8 + file] * 8;

// Pawn protection
const u64 piece_bb = 1ULL << sq;
Expand Down Expand Up @@ -480,7 +490,7 @@ const i32 pawn_attacked_penalty[] = {S(64, 14), S(155, 142)};
// Open or semi-open files
const u64 file_bb = 0x101010101010101ULL << file;
if (!(file_bb & pawns[0]))
score += open_files[!(file_bb & pawns[1])][p - 1];
score += open_files[!(file_bb & pawns[1]) * 5 + p - 1];

if (p == King && piece_bb & 0xC3D7) {
// C3D7 = Reasonable king squares
Expand Down Expand Up @@ -580,12 +590,12 @@ i32 alphabeta(Position &pos,
else
depth -= depth > 3;

i32 static_eval = stack[ply].score = eval(pos);
i32 static_eval = stack[ply].score = in_check ? -inf : eval(pos);
const i32 improving = ply > 1 && static_eval > stack[ply - 2].score;

// If static_eval > tt_entry.score, tt_entry.flag cannot be Lower (ie must be Upper or Exact).
// Otherwise, tt_entry.flag cannot be Upper (ie must be Lower or Exact).
if (tt_entry.key == tt_key && tt_entry.flag != static_eval > tt_entry.score)
if (!in_check && tt_entry.key == tt_key && tt_entry.flag != static_eval > tt_entry.score)
static_eval = tt_entry.score;

if (in_qsearch && static_eval > alpha) {
Expand Down