King is perfectly hashable - meaning you just can pick 256 slots per square and be done with it.
Obviously you could fiddle with the offsets and seeds to compact the table but this is a KISS solution.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <set>
#include <iostream>
#include <bitset>
#include <bit>
#include <immintrin.h>
constexpr int bits = 8;
uint64_t _rnd_seed{};
uint64_t rnd()
{
uint64_t z = (_rnd_seed += UINT64_C(0x9E3779B97F4A7C15));
z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
return z ^ (z >> 31);
}
struct SquareHash
{
int sq;
std::vector<uint64_t> AllAttacks;
};
static constexpr uint64_t fold(uint64_t num, uint64_t seed)
{
return (num * seed) >> (64 - bits);
}
static constexpr uint64_t KingAttacks[] = {
0x0000000000000302, 0x0000000000000705, 0x0000000000000E0A, 0x0000000000001C14, 0x0000000000003828, 0x0000000000007050, 0x000000000000E0A0, 0x000000000000C040,
0x0000000000030203, 0x0000000000070507, 0x00000000000E0A0E, 0x00000000001C141C, 0x0000000000382838, 0x0000000000705070, 0x0000000000E0A0E0, 0x0000000000C040C0,
0x0000000003020300, 0x0000000007050700, 0x000000000E0A0E00, 0x000000001C141C00, 0x0000000038283800, 0x0000000070507000, 0x00000000E0A0E000, 0x00000000C040C000,
0x0000000302030000, 0x0000000705070000, 0x0000000E0A0E0000, 0x0000001C141C0000, 0x0000003828380000, 0x0000007050700000, 0x000000E0A0E00000, 0x000000C040C00000,
0x0000030203000000, 0x0000070507000000, 0x00000E0A0E000000, 0x00001C141C000000, 0x0000382838000000, 0x0000705070000000, 0x0000E0A0E0000000, 0x0000C040C0000000,
0x0003020300000000, 0x0007050700000000, 0x000E0A0E00000000, 0x001C141C00000000, 0x0038283800000000, 0x0070507000000000, 0x00E0A0E000000000, 0x00C040C000000000,
0x0302030000000000, 0x0705070000000000, 0x0E0A0E0000000000, 0x1C141C0000000000, 0x3828380000000000, 0x7050700000000000, 0xE0A0E00000000000, 0xC040C00000000000,
0x0203000000000000, 0x0507000000000000, 0x0A0E000000000000, 0x141C000000000000, 0x2838000000000000, 0x5070000000000000, 0xA0E0000000000000, 0x40C0000000000000,
};
int main(int argc, char* argv[])
{
printf("Preparing all possible attacks...\n");
std::vector<SquareHash> sqrs;
for (int i = 0; i < 64; i++)
{
SquareHash sq;
sq.sq = i;
int bits = std::popcount(KingAttacks[i]);
int max = 1 << bits;
printf("King - %i sees: %i \n", i, max);
for (uint64_t config = 0; config < max; config++)
{
sq.AllAttacks.push_back(_pdep_u64(config, KingAttacks[i]));
}
sqrs.push_back(sq);
}
char indexes[1 << bits]{ };
int max = 0;
printf("\nSolving hashes...\n");
for (int r = 0; r < 64; r++)
{
uint64_t hash;
while (true)
{
hash = rnd() & rnd();
auto& data = sqrs[r].AllAttacks;
int n = 0;
for (n = 0; n < data.size(); n++) {
int idx = fold(data[n], hash);
if (indexes[idx] != 0) break;
else {
indexes[idx] = 1;
}
}
if (n == data.size()) {
break;
}
for (int m = 0; m <= n; m++)
{
indexes[fold(data[m], hash)] = 0;
}
}
memset(indexes, 0, sizeof(indexes));
std::cout << "offset: " << r * (1ull << bits) << " hash: " << hash << "ull,\n";
}
return 0;
}
Duration: 0.8s
Time to code: 23 minutes
Time to create this comment: 6 minutes
Good evening: Yes
Not enough time spent with my family: Also yes
Code: Select all
offset: 0 hash: 7070801120907052452ull,
offset: 256 hash: 3647995412772061314ull,
offset: 512 hash: 9656034352776775979ull,
offset: 768 hash: 9872171867031310341ull,
offset: 1024 hash: 4918318714698474312ull,
offset: 1280 hash: 11695880298579107858ull,
offset: 1536 hash: 3560395457913946117ull,
offset: 1792 hash: 313044180354105376ull,
offset: 2048 hash: 5623184287324246281ull,
offset: 2304 hash: 2594741888837877952ull,
offset: 2560 hash: 17294598824379220418ull,
offset: 2816 hash: 9800976838070141296ull,
offset: 3072 hash: 1441207406165692416ull,
offset: 3328 hash: 9367514987603951681ull,
offset: 3584 hash: 216315787634360332ull,
offset: 3840 hash: 2353359654043880752ull,
offset: 4096 hash: 2595200559335080202ull,
offset: 4352 hash: 7286842089993732096ull,
offset: 4608 hash: 10376857797085367368ull,
offset: 4864 hash: 785880750612807729ull,
offset: 5120 hash: 667659505907274248ull,
offset: 5376 hash: 2974065309374817280ull,
offset: 5632 hash: 9331740041468193330ull,
offset: 5888 hash: 12307286778091439110ull,
offset: 6144 hash: 2325301143867361346ull,
offset: 6400 hash: 3177324791308878336ull,
offset: 6656 hash: 621584744421003848ull,
offset: 6912 hash: 1486541348164864ull,
offset: 7168 hash: 288815325066068993ull,
offset: 7424 hash: 9385785298048319700ull,
offset: 7680 hash: 581845062930923592ull,
offset: 7936 hash: 579763140296994ull,
offset: 8192 hash: 14123715974026854402ull,
offset: 8448 hash: 324295594686546184ull,
offset: 8704 hash: 194220001719354466ull,
offset: 8960 hash: 164386437290067081ull,
offset: 9216 hash: 11110732141628523092ull,
offset: 9472 hash: 2308871347706019853ull,
offset: 9728 hash: 579100271700615204ull,
offset: 9984 hash: 16092224332266211585ull,
offset: 10240 hash: 2595846347999570272ull,
offset: 10496 hash: 2165221246067901448ull,
offset: 10752 hash: 14559011781550285057ull,
offset: 11008 hash: 6917599404035760384ull,
offset: 11264 hash: 1173188819646696448ull,
offset: 11520 hash: 221205522014050368ull,
offset: 11776 hash: 5478218039440708128ull,
offset: 12032 hash: 12909886229102863114ull,
offset: 12288 hash: 1152978683808661764ull,
offset: 12544 hash: 316699489637239172ull,
offset: 12800 hash: 4625198273325433186ull,
offset: 13056 hash: 146688054685859937ull,
offset: 13312 hash: 2314929935946514508ull,
offset: 13568 hash: 11857227922030630ull,
offset: 13824 hash: 4738633492137566227ull,
offset: 14080 hash: 14339483513353539973ull,
offset: 14336 hash: 358552495457544ull,
offset: 14592 hash: 4654784745142514737ull,
offset: 14848 hash: 9369785209685806248ull,
offset: 15104 hash: 4793326623606882872ull,
offset: 15360 hash: 2026646257774117162ull,
offset: 15616 hash: 18158093228589586ull,
offset: 15872 hash: 28217938506989665ull,
offset: 16128 hash: 6921480672498688066ull,
It was interesting and needed to hash all possible slider attacks (14bit per slot) in a table as small as possible.
Hoping above helps in your problem.
I am addicted to chessprogramming again... Taking a 1 week break cya.