00001
00002
00003 #ifndef _BOARD_BIT_MASK_H
00004 #define _BOARD_BIT_MASK_H
00005 #include "osl/position.h"
00006 #include "osl/misc/carray.h"
00007 #include "osl/misc/carray2d.h"
00008 #include <iosfwd>
00009
00010 namespace osl
00011 {
00012 namespace effect
00013 {
00018 #ifdef USE_XMM
00019 typedef int v4sf __attribute__ ((mode(V4SF)));
00020 #endif
00021 struct BoardBitMask{
00022 #ifdef USE_XMM
00023 union {
00024 v4sf xmm;
00025 CArray<unsigned long long,2> mask;
00026 CArray<unsigned char,16> bMask;
00027 };
00028 #else
00029 union {
00030 CArray<unsigned long long,2> mask;
00031 CArray<unsigned char,16> bMask;
00032 };
00033 #endif
00034 friend BoardBitMask& operator&=(BoardBitMask& lhs,BoardBitMask const& rhs);
00035 friend BoardBitMask& operator^=(BoardBitMask& lhs,BoardBitMask const& rhs);
00036 friend BoardBitMask operator^(BoardBitMask& src1,BoardBitMask const& src2);
00037 public:
00038 BoardBitMask(){}
00039 template<class State>
00040 explicit BoardBitMask(State const& st){
00041 clearAll();
00042 for(int y=1;y<=9;y++){
00043 for(int x=1;x<=9;x++){
00044 Position position(x,y);
00045 if (st.getPieceAt(position).isEmpty())
00046 setBit(positionToOffset(position));
00047 }
00048 }
00049 }
00053 void clearAll(){mask[0]=mask[1]=0ull;}
00054 void setAll(){mask[0]=mask[1]=static_cast<unsigned long long>(-1ll);}
00062 static int positionToOffset(Position pos){
00063 assert(pos.isOnBoard());
00064 const int x=pos.x();
00065 const int y=pos.y();
00066 return (x-1)*11+(y-1);
00067 }
00071 void setBit(int offset){
00072 assert(0<=offset && offset<=96);
00073 int index=offset>>6;
00074 unsigned long long tmpMask=1ull<<(offset&63);
00075 assert((index == 0) || (index == 1));
00076 mask[index]|= tmpMask;
00077 }
00078 void setBit(Position pos){
00079 setBit(positionToOffset(pos));
00080 }
00084 void clearBit(int offset){
00085 assert(0<=offset && offset<=96);
00086 int index=offset>>6;
00087 unsigned long long tmpMask=1ull<<(offset&63);
00088 assert((index == 0) || (index == 1));
00089 mask[index]&= ~tmpMask;
00090 }
00091 void clearBit(Position pos){
00092 clearBit(positionToOffset(pos));
00093 }
00094 bool isZero() const{
00095 return (mask[0]|mask[1])==0ull;
00096 }
00097 BoardBitMask& operator=(BoardBitMask const& rhs){
00098 if (this == &rhs)
00099 return *this;
00100
00101 #ifdef USE_XMM
00102 xmm=rhs.xmm;
00103 #else
00104 mask[0]=rhs.mask[0];
00105 mask[1]=rhs.mask[1];
00106 #endif
00107 return *this;
00108 }
00109
00110 };
00111
00112
00113 inline BoardBitMask& operator^=(BoardBitMask& lhs,BoardBitMask const& rhs){
00114 #ifdef USE_XMM
00115 lhs.xmm=__builtin_ia32_xorps(lhs.xmm,rhs.xmm);
00116 #else
00117 lhs.mask[0]^=rhs.mask[0];
00118 lhs.mask[1]^=rhs.mask[1];
00119 #endif
00120 return lhs;
00121 }
00122
00123 inline BoardBitMask operator^(BoardBitMask const& lhs,BoardBitMask const& rhs){
00124 #ifdef USE_XMM
00125 BoardBitMask ret=lhs;
00126 ret.xmm=__builtin_ia32_xorps(lhs.xmm,rhs.xmm);
00127 return ret;
00128 #else
00129 BoardBitMask ret=lhs;
00130 ret.mask[0]^=rhs.mask[0];
00131 ret.mask[1]^=rhs.mask[1];
00132 return ret;
00133 #endif
00134 }
00135 std::ostream& operator<<(std::ostream& os,BoardBitMask const& boardBitMask);
00136
00137 class BoardBitMaskTable{
00138 CArray<BoardBitMask, Position::SIZE> maskOfPosition;
00143 CArray2d<BoardBitMask,Position::SIZE,Position::SIZE> rookBetweenMask;
00147 CArray2d<BoardBitMask, Position::SIZE,Position::SIZE> lanceBetweenMask;
00148 CArray2d<BoardBitMask, Position::SIZE,Position::SIZE> bishopBetweenMask;
00149 private:
00150 void initMaskOfPosition();
00151 void initBetweenMask();
00152 public:
00153 BoardBitMaskTable();
00154 const BoardBitMask& getMask(Position pos) const{
00155 assert(pos.isOnBoard());
00156 return maskOfPosition[pos.index()];
00157 }
00158 const BoardBitMask& getRookMask(Position from,Position to) const{
00159 assert(from.isOnBoard() && to.isOnBoard());
00160 return rookBetweenMask[from.index()][to.index()];
00161 }
00162 const BoardBitMask& getBishopMask(Position from,Position to) const{
00163 assert(from.isOnBoard() && to.isOnBoard());
00164 return bishopBetweenMask[from.index()][to.index()];
00165 }
00166 const BoardBitMask& getLanceMask(Position from,Position to) const{
00167 assert(from.isOnBoard() && to.isOnBoard());
00168 return lanceBetweenMask[from.index()][to.index()];
00169 }
00170 };
00171 #if 0
00172
00173 extern const BoardBitMaskTable Board_Bit_Mask_Table;
00174 #endif
00175 }
00176 }
00177 #endif // _BOARD_BIT_MASK_H
00178
00179
00180
00181