HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
pawn-lookups.h
Go to the documentation of this file.
1// Hoover Chess Utilities / PGN reader
2// Copyright (C) 2025 Sami Kiminki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef HOOVER_CHESS_UTILS__PGN_READER__PAWN_LOOKUPS_H_INCLUDED
18#define HOOVER_CHESS_UTILS__PGN_READER__PAWN_LOOKUPS_H_INCLUDED
19
20#include "chessboard-types.h"
22#include "lookup-utils.h"
23
24#include <array>
25#include <cstdint>
26
28{
29
30template <Color side>
32
33template <>
35{
36 static constexpr SquareSet rank1 { SquareSet::row(0U) };
37 static constexpr SquareSet rank2 { SquareSet::row(1U) };
38 static constexpr SquareSet rank3 { SquareSet::row(2U) };
39 static constexpr SquareSet rank4 { SquareSet::row(3U) };
40 static constexpr SquareSet rank5 { SquareSet::row(4U) };
41 static constexpr SquareSet rank6 { SquareSet::row(5U) };
42 static constexpr SquareSet rank7 { SquareSet::row(6U) };
43 static constexpr SquareSet promoRank { SquareSet::row(7U) };
44
45 static constexpr SquareSet pawnAdvance(SquareSet pawns) noexcept
46 {
47 return pawns << 8U;
48 }
49
50 static constexpr SquareSet captureLeft(SquareSet pawns) noexcept
51 {
52 return (pawns & ~SquareSet::column(0U)) << 7U;
53 }
54
55 static constexpr SquareSet captureRight(SquareSet pawns) noexcept
56 {
57 return (pawns & ~SquareSet::column(7U)) << 9U;
58 }
59
60 static constexpr Square captureLeftSq(Square sq) noexcept
61 {
62 assert(columnOf(sq) >= 1U);
63 assert(rowOf(sq) <= 6U);
64 return addToSquareNoOverflowCheck(sq, 7);
65 }
66
67 static constexpr Square captureRightSq(Square sq) noexcept
68 {
69 assert(columnOf(sq) <= 6U);
70 assert(rowOf(sq) <= 6U);
71 return addToSquareNoOverflowCheck(sq, 9);
72 }
73
74 static constexpr Square pawnRetreatSq(Square sq) noexcept
75 {
76 assert(sq >= Square::A3);
77 return addToSquareNoOverflowCheck(sq, -8);
78 }
79
80 static constexpr Square pawnDoubleRetreatSq(Square sq) noexcept
81 {
82 assert(rowOf(sq) == 3U);
83 return addToSquareNoOverflowCheck(sq, -16);
84 }
85};
86
87template <>
89{
90 static constexpr SquareSet rank1 { SquareSet::row(7U) };
91 static constexpr SquareSet rank2 { SquareSet::row(6U) };
92 static constexpr SquareSet rank3 { SquareSet::row(5U) };
93 static constexpr SquareSet rank4 { SquareSet::row(4U) };
94 static constexpr SquareSet rank5 { SquareSet::row(3U) };
95 static constexpr SquareSet rank6 { SquareSet::row(2U) };
96 static constexpr SquareSet rank7 { SquareSet::row(1U) };
97 static constexpr SquareSet promoRank { SquareSet::row(0U) };
98
99 static constexpr SquareSet pawnAdvance(SquareSet pawns) noexcept
100 {
101 return pawns >> 8U;
102 }
103
104 static constexpr SquareSet captureLeft(SquareSet pawns) noexcept
105 {
106 return (pawns & ~SquareSet::column(0U)) >> 9U;
107 }
108
109 static constexpr SquareSet captureRight(SquareSet pawns) noexcept
110 {
111 return (pawns & ~SquareSet::column(7U)) >> 7U;
112 }
113
114 static constexpr Square captureLeftSq(Square sq) noexcept
115 {
116 assert(columnOf(sq) >= 1U);
117 assert(rowOf(sq) >= 1U);
118 return addToSquareNoOverflowCheck(sq, -9);
119 }
120
121 static constexpr Square captureRightSq(Square sq) noexcept
122 {
123 assert(columnOf(sq) <= 6U);
124 assert(rowOf(sq) >= 1U);
125 return addToSquareNoOverflowCheck(sq, -7);
126 }
127
128 static constexpr Square pawnRetreatSq(Square sq) noexcept
129 {
130 assert(sq <= Square::H6);
131 return addToSquareNoOverflowCheck(sq, +8);
132 }
133
134 static constexpr Square pawnDoubleRetreatSq(Square sq) noexcept
135 {
136 assert(rowOf(sq) == 4U);
137 return addToSquareNoOverflowCheck(sq, +16);
138 }
139};
140
142{
143private:
144 static constexpr std::array<SquareSet, 2U> ctRank8 {
145
146 // white promotion dst square
147 SquareSet::row(7U),
148
149 // black promotion dst square
150 SquareSet::row(0U),
151 };
152
153 static constexpr std::array<SquareSet, 2U> ctRank3 {
154 // white double advancing pawns after single square retreat (rank 3)
155 SquareSet::row(2U),
156
157 // black double advancing pawns after single square retreat (rank 3)
158 SquareSet::row(5U),
159 };
160
161 static constexpr std::array<std::int64_t, 2U> ctPawnAdvanceShift
162 {
163 8,
164 -8
165 };
166
167 static constexpr std::array<SquareSet, 2U> ctAdvanceNoPromoLegalDstMask {
168 // white single advancing pawns
170
171 // black single advancing pawns
173 };
174
175public:
177 {
179 }
180
181 static inline SquareSet rank3(Color turn) noexcept
182 {
183 return turnSpecificLookup(ctRank3, turn);
184 }
185
186 static inline SquareSet rank8(Color turn) noexcept
187 {
188 return turnSpecificLookup(ctRank8, turn);
189 }
190
191 static inline std::int64_t pawnAdvanceShift(Color turn) noexcept
192 {
194 }
195
196};
197
198}
199
200#endif
Definition pawn-lookups.h:142
static SquareSet rank3(Color turn) noexcept
Definition pawn-lookups.h:181
static constexpr std::array< std::int64_t, 2U > ctPawnAdvanceShift
Definition pawn-lookups.h:162
static SquareSet singleAdvanceNoPromoLegalDstMask(Color turn) noexcept
Definition pawn-lookups.h:176
static SquareSet rank8(Color turn) noexcept
Definition pawn-lookups.h:186
static constexpr std::array< SquareSet, 2U > ctAdvanceNoPromoLegalDstMask
Definition pawn-lookups.h:167
static constexpr std::array< SquareSet, 2U > ctRank3
Definition pawn-lookups.h:153
static constexpr std::array< SquareSet, 2U > ctRank8
Definition pawn-lookups.h:144
static std::int64_t pawnAdvanceShift(Color turn) noexcept
Definition pawn-lookups.h:191
Set of squares. Implemented using a bit-mask.
Definition chessboard-types-squareset.h:35
static constexpr SquareSet column(RowColumn col) noexcept
Returns a set of squares in column number col.
Definition chessboard-types-squareset.h:471
static constexpr SquareSet row(RowColumn row) noexcept
Returns a set of squares in row number row.
Definition chessboard-types-squareset.h:485
constexpr RowColumn rowOf(Square sq) noexcept
Returns row number of square.
Definition chessboard-types.h:343
constexpr Square addToSquareNoOverflowCheck(Square sq, std::int_fast8_t shift) noexcept
Adds to square. This function performs no overflow checking.
Definition chessboard-types.h:390
Color
Color of a piece or side to move.
Definition chessboard-types.h:194
Square
Named square.
Definition chessboard-types.h:122
constexpr RowColumn columnOf(Square sq) noexcept
Returns column number of square.
Definition chessboard-types.h:331
@ BLACK
Black piece or black side to move.
@ WHITE
White piece or white side to move.
const U64Type & turnSpecificLookup(const std::array< U64Type, 2U > &array, Color turn) noexcept
Piece attack tables.
Definition lookup-utils.h:64
Definition chessboard-types-squareset.h:30
static constexpr Square pawnRetreatSq(Square sq) noexcept
Definition pawn-lookups.h:128
static constexpr Square captureLeftSq(Square sq) noexcept
Definition pawn-lookups.h:114
static constexpr SquareSet captureRight(SquareSet pawns) noexcept
Definition pawn-lookups.h:109
static constexpr Square pawnDoubleRetreatSq(Square sq) noexcept
Definition pawn-lookups.h:134
static constexpr SquareSet pawnAdvance(SquareSet pawns) noexcept
Definition pawn-lookups.h:99
static constexpr SquareSet captureLeft(SquareSet pawns) noexcept
Definition pawn-lookups.h:104
static constexpr Square captureRightSq(Square sq) noexcept
Definition pawn-lookups.h:121
static constexpr Square pawnRetreatSq(Square sq) noexcept
Definition pawn-lookups.h:74
static constexpr Square pawnDoubleRetreatSq(Square sq) noexcept
Definition pawn-lookups.h:80
static constexpr Square captureRightSq(Square sq) noexcept
Definition pawn-lookups.h:67
static constexpr SquareSet captureLeft(SquareSet pawns) noexcept
Definition pawn-lookups.h:50
static constexpr Square captureLeftSq(Square sq) noexcept
Definition pawn-lookups.h:60
static constexpr SquareSet captureRight(SquareSet pawns) noexcept
Definition pawn-lookups.h:55
static constexpr SquareSet pawnAdvance(SquareSet pawns) noexcept
Definition pawn-lookups.h:45