Hi, I'm (totally real name) Halfen, pretty new to chess programming, but not to programming itself. I've been scowering through the internet high and low seaching for an answer to no avail. let me explain.
const int index64[64] = {
0, 1, 48, 2, 57, 49, 28, 3,
61, 58, 50, 42, 38, 29, 17, 4,
62, 55, 59, 36, 53, 51, 43, 22,
45, 39, 33, 30, 24, 18, 12, 5,
63, 47, 56, 27, 60, 41, 37, 16,
54, 35, 52, 21, 44, 32, 23, 11,
46, 26, 40, 15, 34, 20, 31, 10,
25, 14, 19, 9, 13, 8, 7, 6
};
/**
* bitScanForward
* @author Martin Läuter (1997)
* Charles E. Leiserson
* Harald Prokop
* Keith H. Randall
* "Using de Bruijn Sequences to Index a 1 in a Computer Word"
* @param bb bitboard to scan
* @precondition bb != 0
* @return index (0..63) of least significant one bit
*/
int bitScanForward(U64 bb) {
const U64 debruijn64 = C64(0x03f79d71b4cb0a89);
assert (bb != 0);
return index64[((bb & -bb) * debruijn64) >> 58];
}
this one I figured is pretty straightforward, that index array index64[64] is generated by the DeBruijn Sequence (DBS for short) 0x03f79d71b4cb0a89 (000000 is the 0th substring, 000001 is the 1st, 000010 is the 48th etc. ) some bit manipulation here and there and you could find the index of the LSB, pretty simple, but here is where my brain ran out of memory:
const int index64[64] = {
0, 47, 1, 56, 48, 27, 2, 60,
57, 49, 41, 37, 28, 16, 3, 61,
54, 58, 35, 52, 50, 42, 21, 44,
38, 32, 29, 23, 17, 11, 4, 62,
46, 55, 26, 59, 40, 36, 15, 53,
34, 51, 20, 43, 31, 22, 10, 45,
25, 39, 14, 33, 19, 30, 9, 24,
13, 18, 8, 12, 7, 6, 5, 63
};
/**
* bitScanForward
* @author Kim Walisch (2012)
* @param bb bitboard to scan
* @precondition bb != 0
* @return index (0..63) of least significant one bit
*/
int bitScanForward(U64 bb) {
const U64 debruijn64 = C64(0x03f79d71b4cb0a89);
assert (bb != 0);
return index64[((bb ^ (bb-1)) * debruijn64) >> 58];
}
this one uses the same DBS, but a different index array and multiplies the DBS with not only the LSB but also all the bits below it. I've pondered for awhile and i cant wrap my head around on how to generate that index array, same goes with Matt Taylor's idea
Matt Taylor 1:
const int lsb_64_table[64] =
{
63, 30, 3, 32, 59, 14, 11, 33,
60, 24, 50, 9, 55, 19, 21, 34,
61, 29, 2, 53, 51, 23, 41, 18,
56, 28, 1, 43, 46, 27, 0, 35,
62, 31, 58, 4, 5, 49, 54, 6,
15, 52, 12, 40, 7, 42, 45, 16,
25, 57, 48, 13, 10, 39, 8, 44,
20, 47, 38, 22, 17, 37, 36, 26
};
/**
* bitScanForward
* @author Matt Taylor (2003)
* @param bb bitboard to scan
* @precondition bb != 0
* @return index (0..63) of least significant one bit
*/
int bitScanForward(U64 bb) {
unsigned int folded;
assert (bb != 0);
bb ^= bb - 1;
folded = (int) bb ^ (bb >> 32);
return lsb_64_table[folded * 0x78291ACF >> 26];
}
Matt Taylor 2:
const int BitTable[64] = {
63, 30, 3, 32, 25, 41, 22, 33,
15, 50, 42, 13, 11, 53, 19, 34,
61, 29, 2, 51, 21, 43, 45, 10,
18, 47, 1, 54, 9, 57, 0, 35,
62, 31, 40, 4, 49, 5, 52, 26,
60, 6, 23, 44, 46, 27, 56, 16,
7, 39, 48, 24, 59, 14, 12, 55,
38, 28, 58, 20, 37, 17, 36, 8
};
int pop_1st_bit(uint64 *bb) {
uint64 b = *bb ^ (*bb - 1);
unsigned int fold = (unsigned) ((b & 0xffffffff) ^ (b >> 32));
*bb &= (*bb - 1);
return BitTable[(fold * 0x783a9b23) >> 26];
}
uint64 index_to_uint64(int index, int bits, uint64 m) {
int i, j;
uint64 result = 0ULL;
for(i = 0; i < bits; i++) {
j = pop_1st_bit(&m);
if(index & (1 << i)) result |= (1ULL << j);
}
return result;
}
in those two Matt Taylor used a different B(2, 5) (32-bit) DBS, yet somehow he was able to create a key array with 64 elements in it? I know I shouldnt try to reinvent the wheel here, but if anyone could enlighten me or point me to the right direction it will be very appreciated (can't believe I'm dreaming in binary and losing sleep because of this, its been bugging me for days!)
PS: I'm also still unsure on how that folding trick works, if anyone could give a proper digestible explanation that would help too
Anyone knows how did Matt Taylor and Kim Walisch generate their bit key table?
Moderator: Ras
-
- Posts: 1
- Joined: Sun Jun 05, 2022 3:54 pm
- Full name: HalfenLudith