@@ -126,7 +126,7 @@ namespace {
126126template <PieceType pt>
127127void 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}
0 commit comments