The following code seems to work correct. The code does not make sense, but has an interesting behaviour in Microsoft Visual Studio 2010 as x64 Release with the flag Ob1 or Ob2 (perhaps full optimization).
The variables current->value and temp should have the value 50100, but with x64 Release Ob2 in VC2010 the output is 50000.
I don't know the reason?
Perhaps you can copy the code and see yourself!
Console output:
Code: Select all
value...........50000
temp............50000
Fritz
Code: Select all
#include <stdio.h>
typedef unsigned __int64 bitboard_t; // long long
const int IS_ENPASSANT = 1 << 15;
struct move_info_c {
int move;
int value;
};
struct board_c {
int color_on[64];
int piece_on[64];
bitboard_t piece_bb[6];
bitboard_t color_bb[2];
};
class sort_c {
public:
sort_c();
move_info_c * last_move;
move_info_c move_list[256];
};
static bitboard_t pawn_attack_bb[2][64];
static board_c B;
static void initialize_fen_position(const char * position, const char * to_move, const char * castle, const char * enpassant);
static move_info_c * generate_evasions(move_info_c * move_list);
static int static_exchange_evaluation(int move);
static void initialize_bitboards();
inline int move_to(int move) {
return move & 63;
}
inline int move_from(int move) {
return (move >> 6) & 63;
}
inline int move_is_enpassant(int move) {
return (move & IS_ENPASSANT) != 0;
}
inline int make_enpassant(int from, int to) {
return to | (from << 6) | IS_ENPASSANT;
}
inline int square_to_file(int square) {
return square & 7;
}
inline int square_to_rank(int square) {
return square >> 3;
}
inline int file_and_rank_to_square(int file, int rank) {
return file + (rank << 3);
}
inline int piece_value_midgame(int piece) {
int piece_value_mg[7] = {100, 300, 400, 500, 900, 1000, 0};
return piece_value_mg[piece];
}
inline bitboard_t file_and_rank_to_bitboard(int file, int rank) {
return static_cast<bitboard_t>(1) << static_cast<bitboard_t>(file_and_rank_to_square(file, rank));
}
inline bitboard_t pawn_attack_bitboard(int color, int square) {
return pawn_attack_bb[color][square];
}
inline int color_on_square(int square) {
return B.color_on[square];
}
inline int piece_on_square(int square) {
return B.piece_on[square];
}
inline bitboard_t pawn_bitboard(int color) {
return B.piece_bb[0] & B.color_bb[color];
}
inline int move_is_capture(int move) {
return (piece_on_square(move_to(move)) != 6) || move_is_enpassant(move);
}
inline int move_piece(int move) {
return piece_on_square(move_from(move));
}
inline int move_piece_captured(int move) {
return move_is_enpassant(move) ? 0 : piece_on_square(move_to(move));
}
void initialize_bitboards() {
for (int square = 0; square < 64; square++) {
int file = square_to_file(square);
int rank = square_to_rank(square);
pawn_attack_bb[0][square] = 0;
pawn_attack_bb[1][square] = 0;
if (file - 1 >= 0 && rank + 1 <= 7) pawn_attack_bb[0][square] |= file_and_rank_to_bitboard(file - 1, rank + 1);
if (file + 1 <= 7 && rank + 1 <= 7) pawn_attack_bb[0][square] |= file_and_rank_to_bitboard(file + 1, rank + 1);
if (file - 1 >= 0 && rank - 1 >= 0) pawn_attack_bb[1][square] |= file_and_rank_to_bitboard(file - 1, rank - 1);
if (file + 1 <= 7 && rank - 1 >= 0) pawn_attack_bb[1][square] |= file_and_rank_to_bitboard(file + 1, rank - 1);
}
}
void initialize_fen_position() {
for (int square = 0; square < 64; square++) {
B.color_on[square] = 2;
B.piece_on[square] = 6;
}
B.color_on[28] = 1;
B.piece_on[28] = 0;
B.color_on[29] = 0;
B.piece_on[29] = 0;
}
move_info_c * generate_evasions(move_info_c * move_list) {
(move_list++)->move = make_enpassant(28, 21);
return move_list;
}
int static_exchange_evaluation(int move) {
int from = move_from(move);
int to = move_to(move);
int me = color_on_square(from);
int you = me ^ 1;
int piece = piece_on_square(from);
int piece_captured = move_piece_captured(move);
if ( (pawn_attack_bitboard(me, to) & pawn_bitboard(you))
&& (piece_value_midgame(piece) > piece_value_midgame(piece_captured))
&& (piece_value_midgame(piece) > 100)) {
return piece_value_midgame(piece_captured) - piece_value_midgame(piece);
}
return 0;
}
sort_c::sort_c() {
int temp = 0;
last_move = generate_evasions(move_list);
move_info_c * current = move_list;
int move = current->move;
int value = static_exchange_evaluation(move);
if (value < 0) {
current->value = value;
}
else if (move_is_capture(move)) {
temp = 50000 + piece_value_midgame(move_piece_captured(move)) - move_piece(move);
current->value = temp;
}
else {
current->value = 0;
}
printf_s("value...........%d\n", current->value);
printf_s("temp............%d\n", temp);
}
int main() {
initialize_bitboards();
initialize_fen_position();
sort_c s;
return 0;
}