So here's my question - does a magic (for a specific square) need to map each bit of the mask to one of the top 'n' bits? This is my understanding (but I could be wrong). In which case this code should test a mask and magic combination by cycling through the mask bits, multiplying each one by the magic and seeing if it adds one additional bit to the top n bits. Can anyone see a problem with my code or my logic?
Thanks,
Steve
Code: Select all
function TBoard.IsValidMagic(Mask, Magic: bitboard; bits: integer): boolean;
var
a, c, d: bitboard;
i: integer;
begin
if BitCount((Mask * Magic) shr (64 - bits)) <> BitCount(Mask) then
begin
result := false;
end
else
begin
a := Mask;
d := 0;
i := 0;
while a <> 0 do
begin
c := (a and (a - 1)) xor a;
d := d or (c * Magic);
if BitCount(d shr (64 - bits)) <> i + 1 then
begin
result := false;
exit;
end;
inc(i);
a := (a and (a - 1));
end;
result := true;
end;
end;