Code: Select all
struct Rays{
   uint64_t Horizontal, 
   uint64_t Vertical,
   uint64_t Diag1,
   uint64_t Diag2
   Rays(int sq){
        //??
   }
}Moderator: Ras
Code: Select all
struct Rays{
   uint64_t Horizontal, 
   uint64_t Vertical,
   uint64_t Diag1,
   uint64_t Diag2
   Rays(int sq){
        //??
   }
}
														Let us start with the public solutions likedangi12012 wrote: ↑Sun Jan 16, 2022 2:23 pmWhat is the most efficient C++ code to generate all 4 rays inside the constructor? (no external lookup is allowed)Code: Select all
struct Rays{ uint64_t Horizontal, uint64_t Vertical, uint64_t Diag1, uint64_t Diag2 Rays(int sq){ //?? } }
Code: Select all
#define FileOf(S) ((S) & 7)
uint64_t M64::setup_rank(int s)
{
    static const uint64_t C = 0xFF;
    return C << (s & 56);
}
uint64_t M64::setup_file(int s)
{
    static const uint64_t C = 0x0101010101010101;
    return C << FileOf(s);
}
uint64_t M64::setup_diag(int sq)
{
    static const uint64_t C = 0x8040201008040201;
    int d = 8 * FileOf(sq) - (sq & 56);
    int n = -d & (d >> 31);
    int s = d & (-d >> 31);
    return (C >> s) << n;
}
uint64_t M64::setup_anti(int sq)
{
    static const uint64_t C = 0x0102040810204080;
    int d = 56 - 8 * FileOf(sq) - (sq & 56);
    int n = -d & (d >> 31);
    int s = d & (-d >> 31);
    return (C >> s) << n;
}
Code: Select all
#include <cstdint>
#define FileOf(sq)     ((sq) & 7)
#define RankMaskOf(sq) ((sq) & 56)
inline uint64_t setup_d(uint64_t C, int d)
{
    int n = -d & (d >> 31);
    int s = d & (-d >> 31);
    return (C >> s) << n;
}
struct Rays {
    uint64_t m_horizontal;
    uint64_t m_vertical;
    uint64_t m_diag1;
    uint64_t m_diag2;
    Rays(int sq)
        : m_horizontal   (uint64_t(0xff)               << RankMaskOf(sq)),
          m_vertical     (uint64_t(0x0101010101010101) << FileOf(sq)),
          m_diag1(setup_d(uint64_t(0x8040201008040201),      8 * FileOf(sq) - RankMaskOf(sq))),
          m_diag2(setup_d(uint64_t(0x0102040810204080), 56 - 8 * FileOf(sq) - RankMaskOf(sq)))
    {
    }
};
														https://www.chessprogramming.org/Diagonalstcusr wrote: ↑Sun Jan 16, 2022 5:46 pm this is the simplest code for anti/diagonals
https://github.com/Luecx/Koivisto/blob/ ... ard.h#L178
i don't know how you can get a formula out of this, i don't know where ANTI_DIAGONAL_7_BB is...
Code: Select all
	#define FileOf(S) ((S) & 7)
template<int dir>
static constexpr uint64_t dirMask(int sq)
{
	if constexpr (dir == 0) {
		uint64_t C = 0xFFull;
		return (C << (sq & 56)) & ~(1ull << sq);
	}
	else if constexpr (dir == 1) {
		uint64_t C = 0x0101010101010101ull;
		return (C << FileOf(sq)) & ~(1ull << sq);
	}
	else if constexpr (dir == 2) {
		uint64_t C = 0x8040201008040201ull;
		int d = 8 * FileOf(sq) - (sq & 56);
		int n = -d & (d >> 31);
		int s = d & (-d >> 31);
		return ((C >> s) << n) & ~(1ull << sq);
	}
	else if constexpr (dir == 3) {
		uint64_t C = 0x0102040810204080ull;
		int d = 56 - 8 * FileOf(sq) - (sq & 56);
		int n = -d & (d >> 31);
		int s = d & (-d >> 31);
		return ((C >> s) << n) & ~(1ull << sq);
	}
}Why is that shorter in lines of code than my proposal above?dangi12012 wrote: ↑Sun Jan 16, 2022 8:02 pm Thanks everyone this is what I ended up with - and it works fine!
The constexpr and template stuff is just so I have fewer lines of code compared to 4 different functions with the same signature.Code: Select all
#define FileOf(S) ((S) & 7) template<int dir> static constexpr uint64_t dirMask(int sq) { if constexpr (dir == 0) { uint64_t C = 0xFFull; return (C << (sq & 56)) & ~(1ull << sq); } else if constexpr (dir == 1) { uint64_t C = 0x0101010101010101ull; return (C << FileOf(sq)) & ~(1ull << sq); } else if constexpr (dir == 2) { uint64_t C = 0x8040201008040201ull; int d = 8 * FileOf(sq) - (sq & 56); int n = -d & (d >> 31); int s = d & (-d >> 31); return ((C >> s) << n) & ~(1ull << sq); } else if constexpr (dir == 3) { uint64_t C = 0x0102040810204080ull; int d = 56 - 8 * FileOf(sq) - (sq & 56); int n = -d & (d >> 31); int s = d & (-d >> 31); return ((C >> s) << n) & ~(1ull << sq); } }
True but in fact we got rid of the usage of diagonals at the same time we got rid of HCE.tcusr wrote: ↑Sun Jan 16, 2022 7:52 pmhttps://www.chessprogramming.org/Diagonalstcusr wrote: ↑Sun Jan 16, 2022 5:46 pm this is the simplest code for anti/diagonals
https://github.com/Luecx/Koivisto/blob/ ... ard.h#L178
i don't know how you can get a formula out of this, i don't know where ANTI_DIAGONAL_7_BB is...
koivisto seems to use 7 + rank - file enumeration for diagonals and rank + file for anti-diagonals