45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:计算黑白棋行动力的方法

计算黑白棋行动力的方法

2016-08-30 15:33:57 来源:www.45fan.com 【

计算黑白棋行动力的方法

WZebra作者Gunnar Andersson提供的原始版本,具体的网页我已经记不住了,好像WZebra主页http://www.nada.kth.se/~gunnar/othello.html已经访问不了了,我也好久好久不关注WZebra了。使用了bitboard技术和MMX指令,这个是原始版本(gcc汇编版),必须在gcc下才可能编译。作者曾说不喜欢VC的汇编格式(确切的说是Intel的习惯),说句实话,我们才不适应gcc的汇编习惯呢,嘿嘿,先入为主这句话还是很正确的,没准将来你儿子张开眼镜看到的第一个东东是河马,他就要认河马做老爸了,嘎嘎~~

Gunnar Andersson的介绍文字:

Bitboard trickery

Zebra's endgame solver is very fast, maybe the fastest around. One reason for this is that it represents the Othello board as four 32-bit words, two words for the black discs and two words for the white discs. Using this representation it is possible to write a very fast move generation functions. The fastest-first heuristics used for move ordering benefits from this as well, here you find a C+assembly function that computes the number of legal moves for one player in less than 200 clock cycles on an AMD Athlon - I have not timed it on other platforms, on a Pentium III I expect the cycle count to be roughly the same, whereas on a Pentium IV my guess is that it needs 300-400 cycles.

The code is used as follows: Call init_mmx() to initialize some constants (you only have to call this function once), then use bitboard_mobility() to get the mobility. The code can be compiled as C code using GCC's asm extension. It is straightforward but boring to convert it into code that can be compiled using Microsoft Visual C++.

The main trick used by the code is to find all moves that flip discs in a certain direction, e.g. up, in parallel. This can be done in several different ways, this code uses a variation first suggested by Richard Delorme which I have improved somewhat. There are 8 possible flip directions, 6 of these are managed by the MMX ALUs and the remaining 2 by the integer ALUs.

Currently the code averages about 2 instructions per clock cycle on an AMD Athlon - 400 instructions in 200 cycles. Using the processor optimally it should be possible to execute 3 instructions per clock cycle. For this application it may be impossible to attain the theoretical optimum, but it almost certainly is possible to improve on my code by considering pairing and register stalls throughout - I am a novice assembly programmer and have probably introduced lots of unnecessary stalls. It is possible to make it slightly faster by using the psadbw instruction which is available on Pentium III, Pentium IV, and Athlon. If you can optimize the code in any other way, please let me know.

Source:
 

/*
Copyright (c) 2003, Gunnar Andersson
All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the
following conditions are met:

* Redistributions of source code must retain the above copyright notice,
 this list of conditions and the following disclaimer. 

* Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following disclaimer in the documentation
 and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/



typedef struct {
 unsigned long high;
 unsigned long low;
} BitBoard;



static unsigned long long dir_mask0;
static unsigned long long dir_mask1;
static unsigned long long dir_mask2;
static unsigned long long dir_mask3;
static unsigned long long dir_mask4;
static unsigned long long dir_mask5;
static unsigned long long dir_mask6;
static unsigned long long dir_mask7;
static unsigned long long c0f;
static unsigned long long c33;
static unsigned long long c55;



void
init_mmx( void ) {
 dir_mask0 = 0x007e7e7e7e7e7e00ULL;
 dir_mask1 = 0x00ffffffffffff00ULL;
 dir_mask2 = 0x007e7e7e7e7e7e00ULL;
 dir_mask3 = 0x7e7e7e7e7e7e7e7eULL;
 dir_mask4 = 0x7e7e7e7e7e7e7e7eULL;
 dir_mask5 = 0x007e7e7e7e7e7e00ULL;
 dir_mask6 = 0x00ffffffffffff00ULL;
 dir_mask7 = 0x007e7e7e7e7e7e00ULL;
 c0f = 0x0f0f0f0f0f0f0f0fULL;
 c33 = 0x3333333333333333ULL;
 c55 = 0x5555555555555555ULL;
}



int
bitboard_mobility( const BitBoard my_bits,
   const BitBoard opp_bits ) {
 unsigned int count;
 asm volatile(
"movd  %%ebx, %%mm0/n/t"
"psllq $32, %%mm0/n/t"
"movd  %%ecx, %%mm3/n/t"
"por  %%mm3, %%mm0/n/t"
"movd  %%edi, %%mm1/n/t"
"psllq $32, %%mm1/n/t"
"movd  %%esi, %%mm4/n/t"
"por  %%mm4, %%mm1/n/t"
"pxor  %%mm2, %%mm2/n/t"

/* shift=-9  rowDelta=-1  colDelta=-1 */
/* shift=+9  rowDelta=+1  colDelta=+1 */

/* Disc #1, flip direction 0. */
/* Disc #1, flip direction 7. */
"movq  %%mm1, %%mm3/n/t"
"movq  %%mm0, %%mm4/n/t"
"movq  %%mm0, %%mm6/n/t"
"pand  _dir_mask0, %%mm3/n/t"
"pushl %%esi/n/t"
"psllq $9, %%mm4/n/t"
"psrlq $9, %%mm6/n/t"
"pushl %%edi/n/t"
"pand  %%mm3, %%mm4/n/t"
"pand  %%mm3, %%mm6/n/t"
"pushl %%ecx/n/t"
/* Disc #2, flip direction 0. */
/* Disc #2, flip direction 7. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"psllq $9, %%mm5/n/t"
"psrlq $9, %%mm7/n/t"
"pushl %%ebx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"andl  $2122219134, %%edi/n/t"
"andl  $2122219134, %%esi/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"shll  $1, %%ebx/n/t"
"shll  $1, %%ecx/n/t"
/* Disc #3, flip direction 0. */
/* Disc #3, flip direction 7. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"andl  %%edi, %%ebx/n/t"
"andl  %%esi, %%ecx/n/t"
"psllq $9, %%mm5/n/t"
"psrlq $9, %%mm7/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"shll  $1, %%edx/n/t"
"shll  $1, %%eax/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
/* Disc #4, flip direction 0. */
/* Disc #4, flip direction 7. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"psllq $9, %%mm5/n/t"
"psrlq $9, %%mm7/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"shll  $1, %%edx/n/t"
"shll  $1, %%eax/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
/* Disc #5, flip direction 0. */
/* Disc #5, flip direction 7. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"psllq $9, %%mm5/n/t"
"psrlq $9, %%mm7/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"shll  $1, %%edx/n/t"
"shll  $1, %%eax/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
/* Disc #6, flip direction 0. */
/* Disc #6, flip direction 7. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"psrlq $9, %%mm7/n/t"
"psllq $9, %%mm5/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"shll  $1, %%edx/n/t"
"shll  $1, %%eax/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
"psllq $9, %%mm4/n/t"
"psrlq $9, %%mm6/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"por  %%mm4, %%mm2/n/t"
"por  %%mm6, %%mm2/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"

/* shift=-8  rowDelta=-1  colDelta=0 */
/* shift=+8  rowDelta=1  colDelta=0 */

/* Disc #1, flip direction 1. */
/* Disc #1, flip direction 6. */
"movq  %%mm1, %%mm3/n/t"
"movq  %%mm0, %%mm4/n/t"
"movq  %%mm0, %%mm6/n/t"
"pand  _dir_mask1, %%mm3/n/t"
"psllq $8, %%mm4/n/t"
"psrlq $8, %%mm6/n/t"
"shll  $1, %%edx/n/t"
"shll  $1, %%eax/n/t"
"pand  %%mm3, %%mm4/n/t"
"pand  %%mm3, %%mm6/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
/* Disc #2, flip direction 1. */
/* Disc #2, flip direction 6. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"psllq $8, %%mm5/n/t"
"psrlq $8, %%mm7/n/t"
"shll  $1, %%ebx/n/t"
"shll  $1, %%ecx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"

/* serialize here: add horizontal shl flips. */

"movd  %%ebx, %%mm5/n/t"
"psllq $32, %%mm5/n/t"
"movd  %%ecx, %%mm7/n/t"
"por  %%mm7, %%mm5/n/t"
"por  %%mm5, %%mm2/n/t"

/* Disc #3, flip direction 1. */
/* Disc #3, flip direction 6. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"psllq $8, %%mm5/n/t"
"psrlq $8, %%mm7/n/t"
"popl  %%ebx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"popl  %%ecx/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"pushl %%ecx/n/t"
/* Disc #4, flip direction 1. */
/* Disc #4, flip direction 6. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"pushl %%ebx/n/t"
"psllq $8, %%mm5/n/t"
"psrlq $8, %%mm7/n/t"
"shrl  $1, %%ebx/n/t"
"shrl  $1, %%ecx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"andl  %%edi, %%ebx/n/t"
"andl  %%esi, %%ecx/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
/* Disc #5, flip direction 1. */
/* Disc #5, flip direction 6. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"psllq $8, %%mm5/n/t"
"psrlq $8, %%mm7/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"shrl  $1, %%eax/n/t"
"shrl  $1, %%edx/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
/* Disc #6, flip direction 1. */
/* Disc #6, flip direction 6. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"psllq $8, %%mm5/n/t"
"psrlq $8, %%mm7/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"shrl  $1, %%eax/n/t"
"shrl  $1, %%edx/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
"psllq $8, %%mm4/n/t"
"psrlq $8, %%mm6/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"por  %%mm4, %%mm2/n/t"
"por  %%mm6, %%mm2/n/t"

/* shift=-7  rowDelta=-1  colDelta=1 */
/* shift=+7  rowDelta=1  colDelta=-1 */

/* Disc #1, flip direction 2. */
/* Disc #1, flip direction 5. */
"movq  %%mm1, %%mm3/n/t"
"movq  %%mm0, %%mm4/n/t"
"movq  %%mm0, %%mm6/n/t"
"pand  _dir_mask2, %%mm3/n/t"
"psllq $7, %%mm4/n/t"
"psrlq $7, %%mm6/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"pand  %%mm3, %%mm4/n/t"
"pand  %%mm3, %%mm6/n/t"
"shrl  $1, %%eax/n/t"
"shrl  $1, %%edx/n/t"
/* Disc #2, flip direction 2. */
/* Disc #2, flip direction 5. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
"psllq $7, %%mm5/n/t"
"psrlq $7, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"shrl  $1, %%eax/n/t"
"shrl  $1, %%edx/n/t"
/* Disc #3, flip direction 2. */
/* Disc #3, flip direction 5. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
"psllq $7, %%mm5/n/t"
"psrlq $7, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"shrl  $1, %%eax/n/t"
"shrl  $1, %%edx/n/t"
/* Disc #4, flip direction 2. */
/* Disc #4, flip direction 5. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
"psllq $7, %%mm5/n/t"
"psrlq $7, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"movl  %%ebx, %%eax/n/t"
"movl  %%ecx, %%edx/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"shrl  $1, %%eax/n/t"
"shrl  $1, %%edx/n/t"
/* Disc #5, flip direction 2. */
/* Disc #5, flip direction 5. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"andl  %%edi, %%eax/n/t"
"andl  %%esi, %%edx/n/t"
"psllq $7, %%mm5/n/t"
"psrlq $7, %%mm7/n/t"
"orl  %%eax, %%ebx/n/t"
"orl  %%edx, %%ecx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"shrl  $1, %%ebx/n/t"
"shrl  $1, %%ecx/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"

/* serialize here: add horizontal shr flips. */

"movd  %%ebx, %%mm5/n/t"
"psllq $32, %%mm5/n/t"
"movd  %%ecx, %%mm7/n/t"
"por  %%mm7, %%mm5/n/t"
"por  %%mm5, %%mm2/n/t"
"popl  %%ebx/n/t"

/* Disc #6, flip direction 2. */
/* Disc #6, flip direction 5. */
"movq  %%mm4, %%mm5/n/t"
"movq  %%mm6, %%mm7/n/t"
"psllq $7, %%mm5/n/t"
"psrlq $7, %%mm7/n/t"
"popl  %%ecx/n/t"
"pand  %%mm3, %%mm5/n/t"
"pand  %%mm3, %%mm7/n/t"
"popl  %%edi/n/t"
"por  %%mm5, %%mm4/n/t"
"por  %%mm7, %%mm6/n/t"
"popl  %%esi/n/t"
"psllq $7, %%mm4/n/t"
"psrlq $7, %%mm6/n/t"
"por  %%mm4, %%mm2/n/t"
"por  %%mm6, %%mm2/n/t"

/* mm2 is the pseudo-feasible moves at this point. */
/* Let mm7 be the feasible moves, i.e., mm2 restricted to empty squares. */

"movq  %%mm0, %%mm7/n/t"
"por  %%mm1, %%mm7/n/t"
"pandn %%mm2, %%mm7/n/t"

/* Count the moves, i.e., the number of bits set in mm7. */

   "movq %%mm7, %%mm1/n/t"
   "psrld $1, %%mm7/n/t"
   "pand _c55, %%mm7/n/t"
   "psubd %%mm7, %%mm1/n/t"
   "movq %%mm1, %%mm7/n/t"
   "psrld $2, %%mm1/n/t"
   "pand _c33, %%mm7/n/t"
   "pand _c33, %%mm1/n/t"
   "paddd %%mm1, %%mm7/n/t"
   "movq %%mm7, %%mm1/n/t"
   "psrld $4, %%mm7/n/t"
   "paddd %%mm1, %%mm7/n/t"
   "pand _c0f, %%mm7/n/t"

   "movq %%mm7, %%mm1/n/t"
   "psrld $8, %%mm7/n/t"
   "paddd %%mm1, %%mm7/n/t"
   "movq %%mm7, %%mm1/n/t"
   "psrld $16, %%mm7/n/t"
   "paddd %%mm1, %%mm7/n/t"
   "movq %%mm7, %%mm1/n/t"
   "psrlq $32, %%mm7/n/t"
   "paddd %%mm1, %%mm7/n/t"
   "movd %%mm7, %%eax/n/t"
   "andl $63, %%eax/n/t"

/* Reset the FP/MMX unit. */
"emms/n"
  : "=a" (count)
  : "b" (my_bits.high), "c" (my_bits.low), "D" (opp_bits.high), "S" (opp_bits.low) );

 return count;
}
 

本文地址:http://www.45fan.com/a/question/69887.html
Tags: 黑白棋 动力 MMX
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部