Page 1 of 2

very small bitboard move/attack generator

Posted: Sat Oct 27, 2018 4:55 pm
by xr_a_y
I'd like to test a bitboard approach in Minic for both move generation, attack detection and to get some useful tools to add some evaluation features easily. But what I found in other engines are very "lines of code consuming" implementations. Does someone can point me a small and easy to implement bitboard approach ?

Re: very small bitboard move/attack generator

Posted: Sat Oct 27, 2018 7:30 pm
by smatovic
xr_a_y wrote:
Sat Oct 27, 2018 4:55 pm
I'd like to test a bitboard approach in Minic for both move generation, attack detection and to get some useful tools to add some evaluation features easily. But what I found in other engines are very "lines of code consuming" implementations. Does someone can point me a small and easy to implement bitboard approach ?
https://www.chessprogramming.org/Dumb7F ... ed_Attacks

--
Srdja

Re: very small bitboard move/attack generator

Posted: Mon Oct 29, 2018 4:28 pm
by xr_a_y
Thanks.

A very good resource : https://github.com/ZirconiumX/bbattack

Re: very small bitboard move/attack generator

Posted: Mon Oct 29, 2018 7:57 pm
by Desperado
// simple direct computations for sliders (unify bishop and rook for queen attacks)

Code: Select all

uint64_t Eng::gen_b_attack(int s, uint64_t o)
{
    uint64_t tmp, att = 0;
    remove_bit(&o,s);
    tmp = Setbit64(s);
    while(False(tmp & (o | BB_HF | BB_R8))) att |= tmp <<= 9;
    tmp = Setbit64(s);
    while(False(tmp & (o | BB_AF | BB_R8))) att |= tmp <<= 7;
    tmp = Setbit64(s);
    while(False(tmp & (o | BB_AF | BB_R1))) att |= tmp >>= 9;
    tmp = Setbit64(s);
    while(False(tmp & (o | BB_HF | BB_R1))) att |= tmp >>= 7;
    return att;
}

uint64_t Eng::gen_r_attack(int s, uint64_t o)
{
    uint64_t tmp, att = 0;
    remove_bit(&o,s);
    tmp = Setbit64(s);
    while(False(tmp & (o | BB_HF))) att |= tmp <<= 1;
    tmp = Setbit64(s);
    while(False(tmp & (o | BB_AF))) att |= tmp >>= 1;
    tmp = Setbit64(s);
    while(False(tmp & (o | BB_R8))) att |= tmp <<= 8;
    tmp = Setbit64(s);
    while(False(tmp & (o | BB_R1))) att |= tmp >>= 8;
    return att;
}
// pre-computations to fill lookup tables for p,n,k

Code: Select all

uint64_t Eng::gen_wp_attack(int s)
{
    uint64_t target = 0;
    target |= (Setbit64(s) & ~BB_HF) << 9;
    target |= (Setbit64(s) & ~BB_AF) << 7;
    return target;
}

uint64_t Eng::gen_bp_attack(int s)
{
    uint64_t target = 0;
    target |= (Setbit64(s) & ~BB_HF) >> 7;
    target |= (Setbit64(s) & ~BB_AF) >> 9;
    return target;
}

uint64_t Eng::gen_n_attack(int s)
{
    uint64_t target = 0;
    target |= (Setbit64(s) & ~(BB_R7 | BB_R8 | BB_HF)) << 17;
    target |= (Setbit64(s) & ~(BB_R7 | BB_R8 | BB_AF)) << 15;
    target |= (Setbit64(s) & ~(BB_R8 | BB_GF | BB_HF)) << 10;
    target |= (Setbit64(s) & ~(BB_R8 | BB_BF | BB_AF)) <<  6;
    target |= (Setbit64(s) & ~(BB_R1 | BB_R2 | BB_AF)) >> 17;
    target |= (Setbit64(s) & ~(BB_R1 | BB_R2 | BB_HF)) >> 15;
    target |= (Setbit64(s) & ~(BB_R1 | BB_BF | BB_AF)) >> 10;
    target |= (Setbit64(s) & ~(BB_R1 | BB_GF | BB_HF)) >>  6;
    return target;
}

uint64_t Eng::gen_k_attack(int s)
{
    uint64_t target = 0;
    target |= (Setbit64(s) & ~(BB_R8)) << 8;
    target |= (Setbit64(s) & ~(BB_R1)) >> 8;
    target |= (Setbit64(s) & ~(BB_HF)) << 1;
    target |= (Setbit64(s) & ~(BB_AF)) >> 1;
    target |= (Setbit64(s) & ~(BB_R8 | BB_HF)) << 9;
    target |= (Setbit64(s) & ~(BB_R1 | BB_AF)) >> 9;
    target |= (Setbit64(s) & ~(BB_AF | BB_R8)) << 7;
    target |= (Setbit64(s) & ~(BB_HF | BB_R1)) >> 7;
    return target;
}
Hope it is simple and short enough :)

Re: very small bitboard move/attack generator

Posted: Tue Oct 30, 2018 8:34 am
by xr_a_y
Thanks, that's short enought.
This is already usefull if I use then to iterate other set bits using more or less the current algorithm for move generation or attack.
But I suspect it won't be a huge speed boost ;i'd like to do more with this.
I guess I shall keep a bitboard of white and black pieces to combined occuped square this those attack bitboard in order to get in one shot the possible move (including captures).
How do you handle this for sliding piece with minimal code ?

Re: very small bitboard move/attack generator

Posted: Tue Oct 30, 2018 10:45 am
by abulmo2
You may have a look at Dumb, my simple chess program in D language (a C/C++ like language):
https://github.com/abulmo/Dumb
It is bitboard based, using the hyperbola quintessence algorithm to generate moves.
Overall, Dumb has less lines of code than minic and probably use less sophisticated algorithms, but it looks much faster and a little stronger.

Re: very small bitboard move/attack generator

Posted: Tue Oct 30, 2018 11:21 am
by xr_a_y
Great input thanks.

Indeed Dumb as window/pvs/nullmove/TT/check extension/IID but no LMR, but its evaluation is smarter than Minic PST only.
Great source to read, i'll give feedback.

Re: very small bitboard move/attack generator

Posted: Tue Oct 30, 2018 11:37 am
by xr_a_y
Wow on my hardware (and even without -flto because I lack llvm gold ...) Dumb is crunching 3Mnps when Minic is at 300knps :shock:

Re: very small bitboard move/attack generator

Posted: Thu Nov 01, 2018 7:01 am
by abulmo2
xr_a_y wrote:
Tue Oct 30, 2018 11:21 am
Indeed Dumb as window/pvs/nullmove/TT/check extension/IID but no LMR, but its evaluation is smarter than Minic PST only.
Dumb's evaluation is tapered and well tuned but it remains quite simplistic: material + pst + tempo.

Re: very small bitboard move/attack generator

Posted: Thu Nov 01, 2018 8:00 am
by maksimKorzh
Vivien, it's I'm so happy people are gaining interest in minimalist chess, I've found your minic engine idea to be very interesting. Unfortunately it's not always easy to define the true goals while writing a minimalist chess, I mean what kind of minimalism is about to prefer - minimalist RAM usage/minimalist source code/minimalist design and features. How did you come with an idea to write minic? What are your goals? I really wonder.