Other attack generator just for good measure ... C++ only...

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
tvrzsky
Posts: 128
Joined: Sat Sep 23, 2006 5:10 pm
Location: Prague

Other attack generator just for good measure ... C++ only...

Post by tvrzsky » Thu Oct 29, 2009 12:44 am

Motto:
I agree it is more fun to rely on own ideas and code, and if your code and memory balance is sufficient, there are more important issues to improve engine strength
Gerd
So I had some fun toying with C++ templates and this generator is a result of my effort. I do not know how it compares with another ones in terms of speed but I have been using it for a long time. I think that on 32 bits it could be somewhat competitive, at least the queen attack. I hope that the code is self-explanatory as well.
Filip

Code: Select all

#include <cstdio>
#include <cstdlib>

#define R 1
#define B 2
#define Q &#40;R|B&#41;

enum SQ64 &#123;
            A1, A2, A3, A4, A5, A6, A7, A8,
            B1, B2, B3, B4, B5, B6, B7, B8,
            C1, C2, C3, C4, C5, C6, C7, C8,
            D1, D2, D3, D4, D5, D6, D7, D8,
            E1, E2, E3, E4, E5, E6, E7, E8,
            F1, F2, F3, F4, F5, F6, F7, F8,
            G1, G2, G3, G4, G5, G6, G7, G8,
            H1, H2, H3, H4, H5, H6, H7, H8
          &#125;;

inline int File&#40;const SQ64 x&#41; &#123; return &#40;x >> 3&#41; & 7; &#125;;
inline int Rank&#40;const SQ64 x&#41; &#123; return x & 7; &#125;;

typedef unsigned char U_8;
typedef unsigned long long U_64;

union BITBOARD
&#123;
  U_64 board;
  U_8 byte&#91;8&#93;;
  BITBOARD&#40;const U_64 x&#41; &#58; board&#40;x&#41; &#123; &#125;;
&#125;;

/////////////////////////////////////////////////////////////////////////////////////////////////////

#define PrintBitboard&#40;board, ...) PrintBitboardFunc&#40;board, #board, __LINE__, ## __VA_ARGS__)

int PrintBitboardFunc&#40;char* pointer, const BITBOARD& board, int line, const char* name&#41;
&#123;
  const char* start = pointer;
  *pointer++ = '\n';
  *pointer++ = ' ';
  for &#40;int k = 0; k < 31; k++) *pointer++ = '_';
  *pointer++ = '\n';
  U_8* uk = &#40;U_8*)&board;
  for &#40;unsigned char j = 0x80; j ; j >>= 1&#41;
  &#123;
    if &#40;j & 0x55&#41; pointer += sprintf&#40;pointer, "|///|   |///|   |///|   |///|   |\n");
    else pointer += sprintf&#40;pointer, "|   |///|   |///|   |///|   |///|\n");
    for &#40;int i = 0; i < 8; i++)
    &#123;
      *pointer++ = '|';
      if (!&#40;j & 0x55&#41; ^ &#40;i & 1&#41;) *pointer++ = ' '; else *pointer++ = ' ';
      if &#40;uk&#91;i&#93; & j&#41; *pointer++ = '1';
      else *pointer++ = ' ';
      if (!&#40;j & 0x55&#41; ^ &#40;i & 1&#41;) *pointer++ = ' '; else *pointer++ = ' ';
    &#125;
    *pointer++ = '|';
    if &#40;j == 0x80&#41; pointer += sprintf&#40;pointer, "   %s   on line %d", name, line&#41;;
    *pointer++ = '\n';
    pointer += sprintf&#40;pointer, "|___|___|___|___|___|___|___|___|\n");
  &#125;
  return pointer - start;
&#125;

void PrintBitboardFunc&#40;const BITBOARD& board, const char *name, int line, FILE* file = stdout&#41;
&#123;
  char buf&#91;1000&#93;;
  PrintBitboardFunc&#40;buf, board, line, name&#41;;
  fputs&#40;buf, file&#41;;
	system&#40;"pause");
&#125;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

U_8 SlidingArray_1&#91;64&#93;&#91;8&#93;;
U_8 SlidingArray_2&#91;8&#93;&#91;256&#93; = &#123;
0,1,4,5,8,9,0,0,16,17,0,0,0,0,0,0,32,33,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,128,129,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0,0,4,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,0,129,0,130,0,0,0,132,0,0,0,0,0,0,0,136,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,144,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,160, &#125;;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <int file, int piece> inline void InnerAttackLoop&#40;BITBOARD* const target, const BITBOARD* const free_squares, const int rank&#41;
&#123;
  PrintBitboard&#40;*target&#41;;
  target->byte&#91;file&#93; = &#40;piece & R&#41; ? SlidingArray_1&#91;&#40;free_squares->byte&#91;file&#93; & 0x7e&#41; >> 1&#93;&#91;rank&#93; &#58; 0;
  PrintBitboard&#40;*target&#41;;
  const U_8 start = (((&#40;piece & R&#41; ? 0x100 &#58; 0&#41;|(&#40;piece & B&#41; ? 0x280 &#58; 0&#41;) << rank&#41; >> 8;
  if &#40;file > 0&#41; target->byte&#91;file-1&#93; = start;
  if &#40;file < 7&#41; target->byte&#91;file+1&#93; = start;
  PrintBitboard&#40;*target&#41;;
  for &#40;int i = file + 2; i < 8; i++)
  &#123;
    target->byte&#91;i&#93; = &#40;piece & B&#41; ? SlidingArray_2&#91;rank&#93;&#91;target->byte&#91;i-1&#93; & free_squares->byte&#91;i-1&#93;&#93; &#58; target->byte&#91;i-1&#93; & free_squares->byte&#91;i-1&#93;;
    PrintBitboard&#40;*target&#41;;
  &#125;
  for &#40;int i = file - 2; i >= 0; i--)
  &#123;
    target->byte&#91;i&#93; = &#40;piece & B&#41; ? SlidingArray_2&#91;rank&#93;&#91;target->byte&#91;i+1&#93; & free_squares->byte&#91;i+1&#93;&#93; &#58; target->byte&#91;i+1&#93; & free_squares->byte&#91;i+1&#93;;
    PrintBitboard&#40;*target&#41;;
  &#125;
&#125;;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template <const int piece> void AttackFromSquare&#40;const SQ64 square, BITBOARD* const target, const BITBOARD* const free_squares&#41;
&#123;
#define CASE&#40;file&#41; case file&#58; InnerAttackLoop<file,piece>&#40;target, free_squares, Rank&#40;square&#41;); break;
  switch &#40;File&#40;square&#41;) &#123; CASE&#40;0&#41; CASE&#40;1&#41; CASE&#40;2&#41; CASE&#40;3&#41; CASE&#40;4&#41; CASE&#40;5&#41; CASE&#40;6&#41; CASE&#40;7&#41; &#125;
#undef CASE
&#125;

///////////////////////////////////////////////////////////////////////////////////////////////////

void Initialise_SlidingArray_1&#40;)
&#123;
 for &#40;int i = 0; i < 256; i++)
  &#123;
    for &#40;int j = 0; j < 8; j++)
    &#123;
      U_8 result = 0;
      U_8 square = 1<<j;
      do
      &#123;
        square <<= 1;
        result |= square;
      &#125; while &#40;square && !&#40;i & square&#41;);
      square = 1<<j;
      do
      &#123;
        square >>= 1;
        result |= square;
      &#125; while &#40;square && !&#40;i & square&#41;);
      SlidingArray_1&#91;((~i&#41;&0x7e&#41;>>1&#93;&#91;j&#93; = result;
    &#125;
  &#125;
&#125;

////////////////////////////////////////////////////////////////////////////////////////////////////

int main&#40;)
&#123;
  Initialise_SlidingArray_1&#40;);
  BITBOARD free_squares = 0xaaaf9cffcffac94fULL;
  PrintBitboard&#40;free_squares&#41;;
  BITBOARD target = 0xffffffffffffffffULL;
  AttackFromSquare<Q>&#40;E6, &target, &free_squares&#41;;
  return 0;
&#125;


User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 8:05 pm
Location: Italy
Full name: Stefano Gemma
Contact:

Re: Other attack generator just for good measure ... C++ onl

Post by stegemma » Fri Oct 30, 2009 3:54 pm

It seems to me that this is simple a C code with very little C++ functionality (only templates, i think). You could get similar results with C, without templates, and maybe it would be more readable.

tvrzsky
Posts: 128
Joined: Sat Sep 23, 2006 5:10 pm
Location: Prague

Re: Other attack generator just for good measure ... C++ onl

Post by tvrzsky » Fri Oct 30, 2009 7:12 pm

stegemma wrote:It seems to me that this is simple a C code with very little C++ functionality (only templates, i think). You could get similar results with C, without templates, and maybe it would be more readable.
This is the manner I am using C++ (better C). Indeed every C++ code can be expressed in C but I am afraid that functionally equivalent C code would not be more simple and readable apart from the fact that it would be qiute error prone and incredible tedious to write. Maybe using preprocessor macros?.

mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 7:17 pm

Re: Other attack generator just for good measure ... C++ onl

Post by mcostalba » Fri Oct 30, 2009 7:20 pm

tvrzsky wrote: Maybe using preprocessor macros?.
May be a bit more of comments would be enough or even better few words on how your alghoritm works and what are the ideas behind it.

Of course we can always spend half an hour trying to understand ourself decrypting the code, but because I am already doing it with Ippolit in these days I would be glad to read an abstract or a summary from someone that is not a cloner and consciously writes in a highly obfuscated form. ;-)

tvrzsky
Posts: 128
Joined: Sat Sep 23, 2006 5:10 pm
Location: Prague

Re: Other attack generator just for good measure ... C++ onl

Post by tvrzsky » Fri Oct 30, 2009 8:08 pm

mcostalba wrote:
tvrzsky wrote: Maybe using preprocessor macros?.
May be a bit more of comments would be enough or even better few words on how your alghoritm works and what are the ideas behind it.

Of course we can always spend half an hour trying to understand ourself decrypting the code, but because I am already doing it with Ippolit in these days I would be glad to read an abstract or a summary from someone that is not a cloner and consciously writes in a highly obfuscated form. ;-)
Well, I thought that after anybody compiles the code and lets it run it would be clear how it works ... :(
This is the output of this exemplary program:

Code: Select all

 _______________________________
|   |///|   |///|   |///|   |///|
|   | 1 | 1 | 1 | 1 | 1 | 1 | 1 |   free_squares   on line 174
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 |   |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
|   |   | 1 |   | 1 |   | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
|   |   | 1 |   | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 |   |   | 1 | 1 | 1 | 1 |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 |   | 1 | 1 | 1 |   | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 |   | 1 | 1 |   | 1 |   |
|___|___|___|___|___|___|___|___|

 _______________________________
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |   *target   on line 114
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|

 _______________________________
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |   *target   on line 116
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 |   | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|

 _______________________________
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   | 1 | 1 |   *target   on line 120
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 |   | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 |   | 1 |   | 1 | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 |   | 1 |   | 1 | 1 |
|___|___|___|___|___|___|___|___|

 _______________________________
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   |   | 1 |   *target   on line 124
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 |   | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 |   | 1 |   | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 |   | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   | 1 | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 |   | 1 |   |   | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   |   | 1 |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 |   | 1 |   |   | 1 |
|___|___|___|___|___|___|___|___|

 _______________________________
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   |   |   |   *target   on line 124
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 | 1 |   | 1 |   |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 | 1 | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   | 1 |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 |   | 1 |   |   | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   |   |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 | 1 |   | 1 |   |   |   |
|___|___|___|___|___|___|___|___|

 _______________________________
|   |///|   |///|   |///|   |///|
| 1 | 1 | 1 |   | 1 |   |   |   |   *target   on line 129
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 |   | 1 | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 |   | 1 |   | 1 |   |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 |   | 1 | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 |   |   | 1 |   | 1 |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 |   |   | 1 |   |   | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 | 1 |   |   | 1 |   |   |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 | 1 |   |   | 1 |   |   |   |
|___|___|___|___|___|___|___|___|

 _______________________________
|   |///|   |///|   |///|   |///|
| 1 |   | 1 |   | 1 |   |   |   |   *target   on line 129
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 |   |   | 1 | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 |   |   | 1 |   | 1 |   |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 |   |   | 1 | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 |   |   |   | 1 |   | 1 |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 |   |   |   | 1 |   |   | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
| 1 |   |   |   | 1 |   |   |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
| 1 |   |   |   | 1 |   |   |   |
|___|___|___|___|___|___|___|___|

 _______________________________
|   |///|   |///|   |///|   |///|
|   |   | 1 |   | 1 |   |   |   |   *target   on line 129
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
|   |   |   | 1 | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
|   |   |   | 1 |   | 1 |   |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
|   |   |   | 1 | 1 | 1 |   |   |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
|   |   |   |   | 1 |   | 1 |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
|   |   |   |   | 1 |   |   | 1 |
|___|___|___|___|___|___|___|___|
|   |///|   |///|   |///|   |///|
|   |   |   |   | 1 |   |   |   |
|___|___|___|___|___|___|___|___|
|///|   |///|   |///|   |///|   |
|   |   |   |   | 1 |   |   |   |
|___|___|___|___|___|___|___|___|

Basic idea is that attack is computed byte after byte and every byte of the attack (after logical AND with byte of free squares) serves as an index to array SlidingArray_2 from which we obtain subsequent byte.
Attack in the file of the initial square we obtain from SlidingArray_1.
Function AttackFromSquare() is nothing more than a switch by the file of the initial square in which we then go to one of the InnerAttackLoop() functions where real work is done.
Compiler from these templates generates 3 functions AttackFromSquare() (one from every possible type of attack - Q, B, R) in which eight InnerAttackLoop() functions are inlined, one for every file.
Apart from this one switch the code (after processing of templates and optimization) is branchless. And queen attack is not much more expensive than bishop or rook attack because it works at once, not as a combination of those.

mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 7:17 pm

Re: Other attack generator just for good measure ... C++ onl

Post by mcostalba » Sat Oct 31, 2009 4:01 am

tvrzsky wrote: Basic idea is that attack is computed byte after byte and every byte of the attack (after logical AND with byte of free squares) serves as an index to array SlidingArray_2 from which we obtain subsequent byte.
Attack in the file of the initial square we obtain from SlidingArray_1.
Function AttackFromSquare() is nothing more than a switch by the file of the initial square in which we then go to one of the InnerAttackLoop() functions where real work is done.
Thanks ! :-)

Post Reply