/* 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; }