Magic BitBoards Magic numbers
Posted: Sat Oct 14, 2017 6:37 pm
Hi all!
I would like magic bitboard with 32 bits integers.
I always get an overflow loop.
Blockers array and AttacksFromDebug is 100% good.
What do I do wrong?
Thanks: Tamas
I would like magic bitboard with 32 bits integers.
I always get an overflow loop.
Blockers array and AttacksFromDebug is 100% good.
What do I do wrong?
Thanks: Tamas
Code: Select all
for (var from = 0; from < 120; from++)
{
MagicRAttacks[from] = new Array();
MagicBAttacks[from] = new Array();
for (pce = BISHOP; pce <= ROOK; pce++)
{
if (pce == ROOK) {
MagicRShifts[from] = 32 - PopCount(BlockerBBMask[AttackBBidx(ROOK, from, LOW)]) - PopCount(BlockerBBMask[AttackBBidx(ROOK, from, HIGH)]);
var MagicShift = MagicRShifts[from];
} else {
MagicBShifts[from] = 32 - PopCount(BlockerBBMask[AttackBBidx(BISHOP, from, LOW)]) - PopCount(BlockerBBMask[AttackBBidx(BISHOP, from, HIGH)]);
var MagicShift = MagicBShifts[from];
}
while (true)
{
var index = 0;
var collision = false;
var b = { Low : 0, High : 0 };
var MagicLow = Math.random() * 0xFFFFFFFF | 0;
var MagicHigh = Math.random() * 0xFFFFFFFF | 0;
loop_1: do {
b.High = 0; // Hack
b.Low = (b.Low - BlockerBBMask[AttackBBidx(pce, from, LOW)]) & BlockerBBMask[AttackBBidx(pce, from, LOW)]; // Blockers Low
loop_2: do {
b.High = (b.High - BlockerBBMask[AttackBBidx(pce, from, HIGH)]) & BlockerBBMask[AttackBBidx(pce, from, HIGH)]; // Blockers High
var attacks = AttacksFromDebug(pce, from, b);
index = ((b.Low * MagicLow) ^ (b.High * MagicHigh)) >>> MagicShift;
if (pce == ROOK) {
if (MagicRAttacks[from][index] == null || (MagicRAttacks[from][index].Low == attacks.Low && MagicRAttacks[from][index].High == attacks.High)) {
MagicRMagics[from << 1 | LOW] = MagicLow;
MagicRMagics[from << 1 | HIGH] = MagicHigh
MagicRAttacks[from][index] = { Low : attacks.Low, High : attacks.High };
} else {
MagicRAttacks[from][index] = null;
collision = true; break loop_1;
}
} else {
if (MagicBAttacks[from][index] == null || (MagicBAttacks[from][index].Low == attacks.Low && MagicBAttacks[from][index].High == attacks.High)) {
MagicBMagics[from << 1 | LOW] = MagicLow;
MagicBMagics[from << 1 | HIGH] = MagicHigh;
MagicBAttacks[from][index] = { Low : attacks.Low, High : attacks.High };
} else {
MagicBAttacks[from][index] = null;
collision = true; break loop_1;
}
}
} while (b.High);
} while (b.Low);
if (!collision) {
break;
}
}
}
}