Skip to content

Commit

Permalink
remove blank line between function and the description
Browse files Browse the repository at this point in the history
- remove the blank line between the declaration of the function and it's comment, leads to better IDE support when hovering over a function to see it's description
- remove the unnecessary duplication of the function name in the functions description
- slightly refactored code for lsb, msb in bitboard.h

No functional change
  • Loading branch information
Disservin committed Oct 22, 2023
1 parent b7b7800 commit 9d1be44
Show file tree
Hide file tree
Showing 24 changed files with 164 additions and 272 deletions.
3 changes: 1 addition & 2 deletions src/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const std::vector<std::string> Defaults = {

namespace Stockfish {

// setup_bench() builds a list of UCI commands to be run by bench. There
// Builds a list of UCI commands to be run by bench. There
// are five parameters: TT size in MB, number of search threads that
// should be used, the limit value spent for each position, a file name
// where to look for positions in FEN format, and the type of the limit:
Expand All @@ -108,7 +108,6 @@ namespace Stockfish {
// bench 64 1 100000 default nodes : search default positions for 100K nodes each
// bench 64 4 5000 current movetime : search current position with 4 threads for 5 sec
// bench 16 1 5 blah perft : run a perft 5 on positions in file "blah"

std::vector<std::string> setup_bench(const Position& current, std::istream& is) {

std::vector<std::string> fens, list;
Expand Down
12 changes: 4 additions & 8 deletions src/bitboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,16 @@ void init_magics(PieceType pt, Bitboard table[], Magic magics[]);

}

// safe_destination() returns the bitboard of target square for the given step
// Returns the bitboard of target square for the given step
// from the given square. If the step is off the board, returns empty bitboard.

inline Bitboard safe_destination(Square s, int step) {
Square to = Square(s + step);
return is_ok(to) && distance(s, to) <= 2 ? square_bb(to) : Bitboard(0);
}


// Bitboards::pretty() returns an ASCII representation of a bitboard suitable
// Returns an ASCII representation of a bitboard suitable
// to be printed to standard output. Useful for debugging.

std::string Bitboards::pretty(Bitboard b) {

std::string s = "+---+---+---+---+---+---+---+---+\n";
Expand All @@ -75,9 +73,8 @@ std::string Bitboards::pretty(Bitboard b) {
}


// Bitboards::init() initializes various bitboard tables. It is called at
// Initializes various bitboard tables. It is called at
// startup and relies on global objects to be already zero-initialized.

void Bitboards::init() {

for (unsigned i = 0; i < (1 << 16); ++i)
Expand Down Expand Up @@ -137,11 +134,10 @@ Bitboard sliding_attack(PieceType pt, Square sq, Bitboard occupied) {
}


// init_magics() computes all rook and bishop attacks at startup. Magic
// Computes all rook and bishop attacks at startup. Magic
// bitboards are used to look up attacks of sliding pieces. As a reference see
// www.chessprogramming.org/Magic_Bitboards. In particular, here we use the so
// called "fancy" approach.

void init_magics(PieceType pt, Bitboard table[], Magic magics[]) {

// Optimal PRNG seeds to pick the correct magics in the shortest time
Expand Down
90 changes: 40 additions & 50 deletions src/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ constexpr Bitboard file_bb(File f) { return FileABB << f; }
constexpr Bitboard file_bb(Square s) { return file_bb(file_of(s)); }


// shift() moves a bitboard one or two steps as specified by the direction D

// Moves a bitboard one or two steps as specified by the direction D
template<Direction D>
constexpr Bitboard shift(Bitboard b) {
return D == NORTH ? b << 8
Expand All @@ -143,9 +142,8 @@ constexpr Bitboard shift(Bitboard b) {
}


// pawn_attacks_bb() returns the squares attacked by pawns of the given color
// Returns the squares attacked by pawns of the given color
// from the squares in the given bitboard.

template<Color C>
constexpr Bitboard pawn_attacks_bb(Bitboard b) {
return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
Expand All @@ -158,11 +156,10 @@ inline Bitboard pawn_attacks_bb(Color c, Square s) {
return PawnAttacks[c][s];
}

// line_bb() returns a bitboard representing an entire line (from board edge
// Returns a bitboard representing an entire line (from board edge
// to board edge) that intersects the two given squares. If the given squares
// are not on a same file/rank/diagonal, the function returns 0. For instance,
// line_bb(SQ_C4, SQ_F7) will return a bitboard with the A2-G8 diagonal.

inline Bitboard line_bb(Square s1, Square s2) {

assert(is_ok(s1) && is_ok(s2));
Expand All @@ -171,24 +168,22 @@ inline Bitboard line_bb(Square s1, Square s2) {
}


// between_bb(s1, s2) returns a bitboard representing the squares in the semi-open
// Returns a bitboard representing the squares in the semi-open
// segment between the squares s1 and s2 (excluding s1 but including s2). If the
// given squares are not on a same file/rank/diagonal, it returns s2. For instance,
// between_bb(SQ_C4, SQ_F7) will return a bitboard with squares D5, E6 and F7, but
// between_bb(SQ_E6, SQ_F8) will return a bitboard with the square F8. This trick
// allows to generate non-king evasion moves faster: the defending piece must either
// interpose itself to cover the check or capture the checking piece.

inline Bitboard between_bb(Square s1, Square s2) {

assert(is_ok(s1) && is_ok(s2));

return BetweenBB[s1][s2];
}

// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
// Returns true if the squares s1, s2 and s3 are aligned either on a
// straight or on a diagonal line.

inline bool aligned(Square s1, Square s2, Square s3) { return line_bb(s1, s2) & s3; }


Expand All @@ -197,24 +192,26 @@ inline bool aligned(Square s1, Square s2, Square s3) { return line_bb(s1, s2) &

template<typename T1 = Square>
inline int distance(Square x, Square y);

template<>
inline int distance<File>(Square x, Square y) {
return std::abs(file_of(x) - file_of(y));
}

template<>
inline int distance<Rank>(Square x, Square y) {
return std::abs(rank_of(x) - rank_of(y));
}

template<>
inline int distance<Square>(Square x, Square y) {
return SquareDistance[x][y];
}

inline int edge_distance(File f) { return std::min(f, File(FILE_H - f)); }

// attacks_bb(Square) returns the pseudo attacks of the given piece type
// Returns the pseudo attacks of the given piece type
// assuming an empty board.

template<PieceType Pt>
inline Bitboard attacks_bb(Square s) {

Expand All @@ -224,10 +221,9 @@ inline Bitboard attacks_bb(Square s) {
}


// attacks_bb(Square, Bitboard) returns the attacks by the given piece
// Returns the attacks by the given piece
// assuming the board is occupied according to the passed Bitboard.
// Sliding piece attacks do not continue passed an occupied square.

template<PieceType Pt>
inline Bitboard attacks_bb(Square s, Bitboard occupied) {

Expand All @@ -246,6 +242,9 @@ inline Bitboard attacks_bb(Square s, Bitboard occupied) {
}
}

// Returns the attacks by the given piece
// assuming the board is occupied according to the passed Bitboard.
// Sliding piece attacks do not continue passed an occupied square.
inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {

assert((pt != PAWN) && (is_ok(s)));
Expand All @@ -264,8 +263,7 @@ inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
}


// popcount() counts the number of non-zero bits in a bitboard

// Counts the number of non-zero bits in a bitboard.
inline int popcount(Bitboard b) {

#ifndef USE_POPCNT
Expand All @@ -287,43 +285,22 @@ inline int popcount(Bitboard b) {
#endif
}


// lsb() and msb() return the least/most significant bit in a non-zero bitboard

#if defined(__GNUC__) // GCC, Clang, ICX

// Returns the least significant bit in a non-zero bitboard.
inline Square lsb(Bitboard b) {
assert(b);
return Square(__builtin_ctzll(b));
}

inline Square msb(Bitboard b) {
assert(b);
return Square(63 ^ __builtin_clzll(b));
}
#if defined(__GNUC__) // GCC, Clang, ICX

#elif defined(_MSC_VER) // MSVC
return Square(__builtin_ctzll(b));

#elif defined(_MSC_VER)
#ifdef _WIN64 // MSVC, WIN64

inline Square lsb(Bitboard b) {
assert(b);
unsigned long idx;
_BitScanForward64(&idx, b);
return (Square) idx;
}

inline Square msb(Bitboard b) {
assert(b);
unsigned long idx;
_BitScanReverse64(&idx, b);
return (Square) idx;
}

#else // MSVC, WIN32

inline Square lsb(Bitboard b) {
assert(b);
unsigned long idx;

if (b & 0xffffffff)
Expand All @@ -336,10 +313,29 @@ inline Square lsb(Bitboard b) {
_BitScanForward(&idx, int32_t(b >> 32));
return Square(idx + 32);
}
#endif
#else // Compiler is neither GCC nor MSVC compatible
#error "Compiler not supported."
#endif
}

// Returns the most significant bit in a non-zero bitboard.
inline Square msb(Bitboard b) {
assert(b);

#if defined(__GNUC__) // GCC, Clang, ICX

return Square(63 ^ __builtin_clzll(b));

#elif defined(_MSC_VER)
#ifdef _WIN64 // MSVC, WIN64

unsigned long idx;
_BitScanReverse64(&idx, b);
return (Square) idx;

#else // MSVC, WIN32

unsigned long idx;

if (b >> 32)
Expand All @@ -352,26 +348,20 @@ inline Square msb(Bitboard b) {
_BitScanReverse(&idx, int32_t(b));
return Square(idx);
}
}

#endif

#else // Compiler is neither GCC nor MSVC compatible

#error "Compiler not supported."

#endif
}

// least_significant_square_bb() returns the bitboard of the least significant
// Returns the bitboard of the least significant
// square of a non-zero bitboard. It is equivalent to square_bb(lsb(bb)).

inline Bitboard least_significant_square_bb(Bitboard b) {
assert(b);
return b & -b;
}

// pop_lsb() finds and clears the least significant bit in a non-zero bitboard

// Finds and clears the least significant bit in a non-zero bitboard.
inline Square pop_lsb(Bitboard& b) {
assert(b);
const Square s = lsb(b);
Expand Down
14 changes: 5 additions & 9 deletions src/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ namespace Eval {

std::string currentEvalFileName = "None";

// NNUE::init() tries to load a NNUE network at startup time, or when the engine
// Tries to load a NNUE network at startup time, or when the engine
// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
// The name of the NNUE network is always retrieved from the EvalFile option.
// We search the given network in three locations: internally (the default
// network may be embedded in the binary), in the active working directory and
// in the engine directory. Distro packagers may define the DEFAULT_NNUE_DIRECTORY
// variable to have the engine search in a special directory in their distro.

void NNUE::init() {

std::string eval_file = std::string(Options["EvalFile"]);
Expand Down Expand Up @@ -111,7 +110,7 @@ void NNUE::init() {
}
}

// NNUE::verify() verifies that the last net used was loaded successfully
// Verifies that the last net used was loaded successfully
void NNUE::verify() {

std::string eval_file = std::string(Options["EvalFile"]);
Expand Down Expand Up @@ -145,19 +144,17 @@ void NNUE::verify() {
}


// simple_eval() returns a static, purely materialistic evaluation of the position
// Returns a static, purely materialistic evaluation of the position
// from the point of view of the given color. It can be divided by PawnValue to get
// an approximation of the material advantage on the board in terms of pawns.

Value Eval::simple_eval(const Position& pos, Color c) {
return PawnValue * (pos.count<PAWN>(c) - pos.count<PAWN>(~c))
+ (pos.non_pawn_material(c) - pos.non_pawn_material(~c));
}


// evaluate() is the evaluator for the outer world. It returns a static evaluation
// Evaluate is the evaluator for the outer world. It returns a static evaluation
// of the position from the point of view of the side to move.

Value Eval::evaluate(const Position& pos) {

assert(!pos.checkers());
Expand Down Expand Up @@ -197,11 +194,10 @@ Value Eval::evaluate(const Position& pos) {
return v;
}

// trace() is like evaluate(), but instead of returning a value, it returns
// Like evaluate(), but instead of returning a value, it returns
// a string (suitable for outputting to stdout) that contains the detailed
// descriptions and values of each evaluation term. Useful for debugging.
// Trace scores are from white's point of view

std::string Eval::trace(Position& pos) {

if (pos.checkers())
Expand Down
Loading

0 comments on commit 9d1be44

Please sign in to comment.