## Disproving the existence of some magics

Discussion of chess software programming and technical issues.

Moderators: Harvey Williamson, bob, hgm

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
niklasf
Posts: 42
Joined: Sat May 16, 2015 9:41 pm

### Disproving the existence of some magics

I want to share a little observation that makes it possible to limit the search space for magic candidates. For some squares it is then possible to exhaustively test them all.

Consider a relevant occupancy r. Then there is a smallest period p(r) > 0 such that

r * p(r) = 0 (mod 2^64)

namely

p(r) = 2^64 / gcd(r, 2^64)

If you go one step further it will just wrap around completely.

r * (p(r) + 1) = r (mod 2^64)

Now take the least common multiple of the p(r) for all relevant occupancies of a square s.

P(s) = lcm(p(r_1), ..., p(r_n))

We know that r * P(s) = 0 (mod 2^64) for all relevant occupancies of that square. In other words all magic candidates are equivalent to a candidate in the range [0, P(s) - 1].

It depends on the relevant occupancies (specifically the lowest relevant square) if P(s) is small enough to have any practical consequences. Some bishop squares have very low periods, so the best possible magics can be easily found by exhaustive search:

Bishops squares:

Code: Select all

``````   |  s | P&#40;s&#41; | c | best magic shift 9     | best magic shift c  | best magic shift c-1
---+----+------+---+------------------------+---------------------+---------------------
d8 | 59 | 2^26 | 5 | 0x84030 &#40;maps to 0-60&#41; | 0x208800 &#40;0-31&#41;     | disproven
e8 | 60 | 2^31 | 5 | 0x1002020 &#40;0-31&#41;       | 0x4f68bcb9 &#40;0-29&#41;   | disproven
d7 | 51 | 2^34 | 5 | 0x8403000 &#40;0-60&#41;       | 0x20880000 &#40;0-31&#41;   | disproven
c8 | 58 | 2^34 | 5 | 0x8208060 &#40;0-35&#41;       | 0x50c3417a &#40;0-29&#41;   | disproven
e7 | 52 | 2^39 | 5 | 0x100202000 &#40;0-31&#41;     | 0x4f68bcb86d &#40;0-29&#41; | disproven
f8 | 61 | 2^39 | 5 | 0x40408020 &#40;0-31&#41;      | 0x74486419f &#40;0-26&#41;  | disproven
h2 | 15 | 2^42 | 5 | e0x820820020 &#40;0-61&#41;    | 0x29748305f5 &#40;0-22&#41; | 0x410509fff0 (*) &#40;0-15&#41;
d6 | 43 | 2^42 | 7 | 0x8840200040 &#40;0-132&#41;   | 0xa44000800 &#40;0-127&#41; | disproven
c7 | 50 | 2^42 | 5 | 0x820806000 &#40;0-35&#41;     | 0x50c34179e6 &#40;0-29&#41; | disproven
b8 | 57 | 2^42 | 5 | e0x820820020 &#40;0-61&#41;    | 0x58c328c2ee &#40;0-22&#41; | 0x1ec04eae595 (*) &#40;0-15&#41;

( ... snip ... )

h8 | 63 | 2^55 | 6 |                        |                     | exists &#40;Gerd Isenberg&#41;
``````
(*) Previous magic found by Gerd Isenberg are equally good

Rook squares generally have higher periods, because there's always a relevant second rank or even first rank occupancy. I still think some of them are now very much feasible, and I definitely plan to check them (after spending some time optimizing the magic testing).

Unfortunately the most practically intresting squares a1 and h1 remain among the most difficult ones.

Rook squares:

Code: Select all

``````   |  s | P&#40;s&#41; |  c | magic with shift c-1
---+----+------+----|--------------------------
h3 | 23 | 2^49 | 11 |
h4 | 31 | 2^49 | 11 |
h5 | 39 | 2^49 | 11 |
h6 | 47 | 2^49 | 11 |
h7 | 55 | 2^49 | 11 | exists &#40;Grant Osborne&#41;
h8 | 63 | 2^49 | 12 | exists &#40;Grant Osborne&#41;

( ... snip ... )

h1 |  7 | 2^63 | 12 |
``````
Previous results taken from https://chessprogramming.wikispaces.com ... ics+so+far

Gerd Isenberg
Posts: 2192
Joined: Wed Mar 08, 2006 7:47 pm
Location: Hattingen, Germany

### Re: Disproving the existence of some magics

Very interesting ... good luck in further testing to half the size of the big rook squares. While it seems that clever fixed shift has the edge now, your findings may even help for denser ranges in Volker's packed or black magics.

Gerd

grant
Posts: 67
Joined: Mon Aug 06, 2007 2:42 pm
Location: London, England

### Re: Disproving the existence of some magics

I am doubtful you will find a1 or h1 but good luck to you.

Grant

niklasf
Posts: 42
Joined: Sat May 16, 2015 9:41 pm

### Re: Disproving the existence of some magics

Agreed, those squares remain out of reach.

Some unscientific thoughts about them: So far it seems that there are either no magics at all, or hundreths of thousands of them. And indeed so far for each square a magic had already been found or there was none. So I'd personally bet that a1/h1 have no shift 11 magics.

Evert
Posts: 2929
Joined: Fri Jan 21, 2011 11:42 pm
Location: NL
Contact:

### Re: Disproving the existence of some magics

It's probably not worth it, but one could flip the occupancy board (bswap64) if the square is on the lower half of the board, do the table-lookup for a8/h8, then flip the resulting move set (another bswap). This could even cut the size of the tables fully in half, and allow for a more compact table because you don't need the problematic squares a1/h1.
Cost: two conditional bswaps. Might be interesting to try though.