Skip to content

Commit 18400dd

Browse files
committed
Test one pext
1 parent e9ac783 commit 18400dd

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

src/bitboard.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ namespace {
126126
template<PieceType pt>
127127
void init_magics(Bitboard table[], Magic magics[] IF_NOT_PEXT(, const Bitboard magicsInit[])) {
128128

129-
Bitboard edges, b;
129+
Bitboard edges, b, mask;
130130
uint64_t size = 0;
131131

132132
for (Square s = SQ_A0; s <= SQ_I9; ++s)
@@ -139,17 +139,37 @@ void init_magics(Bitboard table[], Magic magics[] IF_NOT_PEXT(, const Bitboard m
139139
// all the attacks for each possible subset of the mask and so is 2 power
140140
// the number of 1s of the mask.
141141
Magic& m = magics[s];
142-
m.mask = pt == ROOK ? Bitboards::sliding_attack<pt>(s, 0)
143-
: pt == CANNON ? RookMagics[s].mask
144-
: Bitboards::lame_leaper_path<pt>(s);
142+
mask = pt == ROOK || pt == CANNON ? Bitboards::sliding_attack<ROOK>(s, 0)
143+
: Bitboards::lame_leaper_path<pt>(s);
145144
if (pt != KNIGHT_TO)
146-
m.mask &= ~edges;
145+
mask &= ~edges;
146+
147+
m.mask_lo = (uint64_t) mask;
148+
m.mask_hi = (uint64_t) (mask >> 64);
149+
150+
int best_relative_shift = 0;
151+
152+
if (m.mask_hi != 0)
153+
{
154+
int hi_end = msb(m.mask_hi);
155+
for (int k = 0; k < (64 - hi_end); ++k)
156+
{
157+
uint64_t shifted_pattern = m.mask_hi << k;
158+
159+
if ((m.mask_lo & shifted_pattern) == 0)
160+
{
161+
best_relative_shift = k;
162+
break;
163+
}
164+
}
165+
}
166+
m.mask = (m.mask_hi << best_relative_shift) | m.mask_lo;
147167

148168
#ifdef USE_PEXT
149-
m.shift = popcount(uint64_t(m.mask));
169+
m.shift = best_relative_shift;
150170
#else
151171
m.magic = magicsInit[s];
152-
m.shift = 128 - popcount(m.mask);
172+
m.shift = 64 - popcount(m.mask);
153173
#endif
154174

155175
// Set the offset for the attacks table of the square. We have individual
@@ -166,7 +186,7 @@ void init_magics(Bitboard table[], Magic magics[] IF_NOT_PEXT(, const Bitboard m
166186
: Bitboards::lame_leaper_attack<pt>(s, b);
167187

168188
size++;
169-
b = (b - m.mask) & m.mask;
189+
b = (b - mask) & mask;
170190
} while (b);
171191
}
172192
}

src/bitboard.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,20 @@ extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
8282

8383
// Magic holds all magic bitboards relevant data for a single square
8484
struct Magic {
85-
Bitboard mask;
85+
uint64_t mask_lo, mask_hi;
86+
uint64_t mask;
8687
Bitboard* attacks;
8788
unsigned shift;
88-
IF_NOT_PEXT(Bitboard magic;)
89+
IF_NOT_PEXT(uint64_t magic;)
8990

9091
// Compute the attack's index using the 'magic bitboards' approach
9192
unsigned index(Bitboard occupied) const {
9293

94+
uint64_t lo = uint64_t(occupied) & mask_lo;
95+
uint64_t hi = uint64_t(occupied >> 64) & mask_hi;
96+
9397
#ifdef USE_PEXT
94-
return unsigned(pext(occupied, mask, shift));
98+
return unsigned(pext((hi << shift) | lo, mask));
9599
#else
96100
return unsigned(((occupied & mask) * magic) >> shift);
97101
#endif
@@ -314,29 +318,19 @@ inline Square lsb(Bitboard b) {
314318
#endif
315319
}
316320

317-
// Returns the most significant bit in a non-zero bitboard.
318-
inline Square msb(Bitboard b) {
321+
// Returns the most significant bit in a 64 bit integer.
322+
inline int msb(uint64_t b) {
319323
assert(b);
320324

321325
#if defined(_MSC_VER)
322326

323327
unsigned long idx;
324-
if (b._Word[1])
325-
{
326-
_BitScanReverse(&idx, b._Word[1]);
327-
return Square(idx);
328-
}
329-
else
330-
{
331-
_BitScanReverse(&idx, b._Word[0]);
332-
return Square(idx + 64);
333-
}
328+
_BitScanReverse64(&idx, b);
329+
return idx;
334330

335331
#else // Assumed gcc or compatible compiler
336332

337-
if (uint64_t(b >> 64))
338-
return Square(__builtin_clzll(b));
339-
return Square(__builtin_clzll(b) + 64);
333+
return 63 ^ __builtin_clzll(b);
340334

341335
#endif
342336
}

src/types.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,9 @@ using __uint128_t = std::_Unsigned128;
9191

9292
#if defined(USE_PEXT)
9393
#include <immintrin.h> // Header for _pext_u64() intrinsic
94-
#if defined(_MSC_VER) && !defined(__clang__)
95-
#define pext(b, m, s) \
96-
((_pext_u64(b._Word[1], m._Word[1]) << s) | _pext_u64(b._Word[0], m._Word[0]))
97-
#else
98-
#define pext(b, m, s) ((_pext_u64(b >> 64, m >> 64) << s) | _pext_u64(b, m))
99-
#endif
94+
#define pext(b, m) _pext_u64(b, m)
10095
#else
101-
#define pext(b, m, s) 0
96+
#define pext(b, m) 0
10297
#endif
10398

10499
namespace Stockfish {

0 commit comments

Comments
 (0)