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

Use None=0 #144

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 @@ sh build-mini.sh

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

---
Expand Down
45 changes: 23 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ using namespace std;

enum
{
None,
Pawn,
Knight,
Bishop,
Rook,
Queen,
King,
None
King
};

[[nodiscard]] int64_t now() {
Expand All @@ -57,7 +57,8 @@ using u64 = uint64_t;
struct [[nodiscard]] Position {
array<int, 4> castling = {true, true, true, true};
array<u64, 2> colour = {0xFFFFULL, 0xFFFF000000000000ULL};
array<u64, 6> pieces = {0xFF00000000FF00ULL,
array<u64, 7> pieces = {0x0ULL,
0xFF00000000FF00ULL,
0x4200000000000042ULL,
0x2400000000000024ULL,
0x8100000000000081ULL,
Expand Down Expand Up @@ -154,15 +155,15 @@ vector<TT_Entry> transposition_table;
str += '1' + (flip ? (7 - move.from / 8) : (move.from / 8));
str += 'a' + (move.to % 8);
str += '1' + (flip ? (7 - move.to / 8) : (move.to / 8));
if (move.promo != None) {
str += "\0nbrq\0\0"[move.promo];
if (move.promo) {
str += "\0\0nbrq\0"[move.promo];
}
return str;
}

[[nodiscard]] int piece_on(const Position &pos, const int sq) {
const u64 bb = 1ULL << sq;
for (int i = 0; i < 6; ++i) {
for (int i = 1; i < 7; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this run from 0 to 7? (maybe with a compiled out assert for the fallthrough)

if (pos.pieces[i] & bb) {
return i;
}
Expand All @@ -173,7 +174,7 @@ vector<TT_Entry> transposition_table;
void flip(Position &pos) {
pos.colour[0] = flip(pos.colour[0]);
pos.colour[1] = flip(pos.colour[1]);
for (int i = 0; i < 6; ++i) {
for (int i = 1; i < 7; ++i) {
pos.pieces[i] = flip(pos.pieces[i]);
}
pos.ep = flip(pos.ep);
Expand Down Expand Up @@ -252,7 +253,7 @@ auto makemove(Position &pos, const Move &move) {
}

// Captures
if (captured != None) {
if (captured) {
pos.colour[1] ^= to;
pos.pieces[captured] ^= to;
}
Expand Down Expand Up @@ -348,7 +349,7 @@ void generate_piece_moves(Move *const movelist,
}

const int phases[] = {0, 1, 1, 2, 4, 0};
const int max_material[] = {133, 418, 401, 603, 1262, 0, 0};
const int max_material[] = {0, 133, 418, 401, 603, 1262, 0};
const int material[] = {S(80, 133), S(418, 292), S(401, 328), S(546, 603), S(1262, 1065), 0};
const int psts[][4] = {
{S(-19, 0), S(-1, -2), S(6, 0), S(6, 11)},
Expand Down Expand Up @@ -391,7 +392,7 @@ const int pawn_attacked[] = {S(-64, -14), S(-155, -142)};

// For each piece type
for (int p = 0; p < 6; ++p) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this loop simply going from 1 to 7, avoiding all the p + 1? I'd compute that once into a var for the eval array indexing, if only the avoid the eyebleed this is now.

auto copy = pos.colour[0] & pos.pieces[p];
auto copy = pos.colour[0] & pos.pieces[p + 1];
while (copy) {
phase += phases[p];

Expand Down Expand Up @@ -424,7 +425,7 @@ const int pawn_attacked[] = {S(-64, -14), S(-155, -142)};
score += pawn_attacked[c];
}

if (p == Pawn) {
if (p + 1 == Pawn) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeeaargh.

// Passed pawns
u64 blockers = 0x101010101010101ULL << sq;
blockers = nw(blockers) | ne(blockers);
Expand All @@ -448,7 +449,7 @@ const int pawn_attacked[] = {S(-64, -14), S(-155, -142)};
if ((north(piece_bb) | north(north(piece_bb))) & pawns[0]) {
score += pawn_doubled;
}
} else if (p == Rook) {
} else if (p + 1 == Rook) {
// Rook on open or semi-open files
const u64 file_bb = 0x101010101010101ULL << file;
if (!(file_bb & pawns[0])) {
Expand All @@ -463,7 +464,7 @@ const int pawn_attacked[] = {S(-64, -14), S(-155, -142)};
if (rank >= 6) {
score += rook_rank78;
}
} else if (p == King && piece_bb & 0xE7) {
} else if (p + 1 == King && piece_bb & 0xE7) {
const u64 shield = file < 3 ? 0x700 : 0xE000;
score += count(shield & pawns[0]) * king_shield[0];
score += count(north(shield) & pawns[0]) * king_shield[1];
Expand All @@ -487,18 +488,18 @@ const int pawn_attacked[] = {S(-64, -14), S(-155, -142)};
u64 hash = pos.flipped;

// Pieces
for (int p = Pawn; p < None; p++) {
for (int p = 1; p < 7; p++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could still use symbols.

u64 copy = pos.pieces[p] & pos.colour[0];
while (copy) {
const int sq = lsb(copy);
copy &= copy - 1;
hash ^= keys[p * 64 + sq];
hash ^= keys[(p - 1) * 64 + sq];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can cheat and make keys bigger to avoid the -1.

}
copy = pos.pieces[p] & pos.colour[1];
while (copy) {
const int sq = lsb(copy);
copy &= copy - 1;
hash ^= keys[(p + 6) * 64 + sq];
hash ^= keys[(p + 5) * 64 + sq];
}
}

Expand Down Expand Up @@ -638,8 +639,8 @@ int alphabeta(Position &pos,
if (i == !(no_move == tt_move)) {
for (int j = 0; j < num_moves; ++j) {
const int capture = piece_on(pos, moves[j].to);
if (capture != None) {
move_scores[j] = (capture + 1) * (1LL << 54);
if (capture) {
move_scores[j] = capture * (1LL << 54);
kz04px marked this conversation as resolved.
Show resolved Hide resolved
} else if (moves[j] == stack[ply].killer) {
move_scores[j] = 1LL << 50;
} else {
Expand Down Expand Up @@ -711,7 +712,7 @@ int alphabeta(Position &pos,
hash_history);
} else {
// Late move reduction
int reduction = depth > 2 && num_moves_evaluated > 4 && piece_on(pos, move.to) == None
int reduction = depth > 2 && num_moves_evaluated > 4 && !piece_on(pos, move.to)
? 1 + num_moves_evaluated / 14 + depth / 17 + (alpha == beta - 1) - improving +
(hh_table[pos.flipped][move.from][move.to] < 0) -
(hh_table[pos.flipped][move.from][move.to] > 0)
Expand Down Expand Up @@ -749,7 +750,7 @@ int alphabeta(Position &pos,
}

num_moves_evaluated++;
if (piece_on(pos, move.to) == None) {
if (!piece_on(pos, move.to)) {
stack[ply].quiets_evaluated[num_quiets_evaluated] = move;
num_quiets_evaluated++;
}
Expand All @@ -771,7 +772,7 @@ int alphabeta(Position &pos,

if (alpha >= beta) {
tt_flag = 1; // Beta flag
if (piece_on(pos, move.to) == None) {
if (!piece_on(pos, move.to)) {
hh_table[pos.flipped][move.from][move.to] += depth * depth;
for (int j = 0; j < num_quiets_evaluated - 1; ++j) {
hh_table[pos.flipped][stack[ply].quiets_evaluated[j].from][stack[ply].quiets_evaluated[j].to] -=
Expand Down Expand Up @@ -1176,7 +1177,7 @@ int main(
const int num_moves = movegen(pos, moves, false);
for (int i = 0; i < num_moves; ++i) {
if (word == move_str(moves[i], pos.flipped)) {
if (piece_on(pos, moves[i].to) != None || piece_on(pos, moves[i].from) == Pawn) {
if (piece_on(pos, moves[i].to) || piece_on(pos, moves[i].from) == Pawn) {
hash_history.clear();
} else {
hash_history.emplace_back(get_hash(pos));
Expand Down