Skip to content

Commit bc02eeb

Browse files
authored
minor optimizations (#25)
* Update types.h * Update moves_io.cpp * Update moves_io.cpp * Update types.h bonus: fixes c isn't in "PNBRQK"
1 parent 3a288b4 commit bc02eeb

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

moves_io.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,18 @@ template <typename T, typename V> Move uciToMove(const _Position<T, V> &pos, std
9292

9393
// promotion
9494
if (pt == PAWN && uci.length() == 5 && (rank_of(target) == (pos.sideToMove() == WHITE ? RANK_8 : RANK_1))) {
95-
auto promotion = parse_pt(uci.substr(4, 1));
95+
auto promotion = parse_pt(uci[4]);
9696

9797
if (promotion == NO_PIECE_TYPE || promotion == KING || promotion == PAWN) {
98-
#ifdef __EXCEPTIONS
98+
#if defined(_DEBUG) || !defined(NDEBUG)
99+
assert(false && "promotions: NRBQ");
100+
#elif defined(__EXCEPTIONS) && (defined(_DEBUG) || !defined(NDEBUG))
99101
throw std::invalid_argument("promotions: NRBQ");
100102
#endif
101103
return Move::NO_MOVE;
102104
}
103105

104-
return Move::make<PROMOTION>(source, target, parse_pt(uci.substr(4, 1)));
106+
return Move::make<PROMOTION>(source, target, promotion);
105107
}
106108
auto move = (uci.length() == 4) ? Move::make(source, target) : Move::NO_MOVE;
107109
Movelist moves;
@@ -124,4 +126,4 @@ INSTANTITATE(ContiguousMappingPiece)
124126
#undef INSTANTITATE
125127
} // namespace uci
126128
std::string Move::uci() const { return uci::moveToUci(*this); }
127-
} // namespace chess
129+
} // namespace chess

types.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,17 @@ template <typename T, std::size_t MaxSize> class ValueList {
337337
using Movelist = ValueList<Move, 256>;
338338
constexpr int square_distance(Square a, Square b) { return std::max(std::abs(file_of(a) - file_of(b)), std::abs(rank_of(a) - rank_of(b))); }
339339
constexpr Square parse_square(std::string_view sv) { return make_sq(File(sv[0] - 'a'), Rank(sv[1] - '1')); }
340-
constexpr PieceType parse_pt(std::string_view sv) {
340+
constexpr PieceType parse_pt(unsigned char c) {
341341
const char a[] = "pnbrqk";
342-
int p = 0;
342+
int p = -1;
343+
(c >='A' && c<='Z') ? (c += 32) : (c); // tolower
343344
for (size_t i = 0; i < sizeof(a); i++) {
344-
if (sv[0] == a[i])
345+
if (c == a[i])
345346
p = i;
346347
}
347348
return static_cast<PieceType>(p + 1);
348349
}
349350

350351
} // namespace chess
352+
353+

0 commit comments

Comments
 (0)