boardBitMask.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
boardBitMask.h
Go to the documentation of this file.
1 /* boardBitMask.h
2 */
3 #ifndef _BOARD_BIT_MASK_H
4 #define _BOARD_BIT_MASK_H
5 #include "osl/square.h"
6 #include "osl/misc/carray.h"
7 #include "osl/misc/carray2d.h"
8 #include <iosfwd>
9
10 namespace osl
11 {
12 namespace effect
13 {
18 #ifdef USE_XMM
19 typedef int v4sf __attribute__ ((mode(V4SF))); // works
20 #endif
21 struct BoardBitMask{
22 #ifdef USE_XMM
23 union {
24 v4sf xmm;
25 CArray<unsigned long long,2> mask;
26 CArray<unsigned char,16> bMask;
27 };
28 #else
29 union {
30 CArray<unsigned long long,2> mask;
31 CArray<unsigned char,16> bMask;
32 };
33 #endif
34 friend BoardBitMask& operator&=(BoardBitMask& lhs,BoardBitMask const& rhs);
35 friend BoardBitMask& operator^=(BoardBitMask& lhs,BoardBitMask const& rhs);
36 friend BoardBitMask operator^(BoardBitMask& src1,BoardBitMask const& src2);
37 public:
38 BoardBitMask(){}
39 template<class State>
40 explicit BoardBitMask(State const& st){
41 clearAll();
42 for(int y=1;y<=9;y++){
43 for(int x=1;x<=9;x++){
44 Square position(x,y);
45 if (st.pieceAt(position).isEmpty())
46 setBit(positionToOffset(position));
47 }
48 }
49 }
53 void clearAll(){mask[0]=mask[1]=0ull;}
54 void setAll(){mask[0]=mask[1]=static_cast<unsigned long long>(-1ll);}
62 static int positionToOffset(Square pos){
63 assert(pos.isOnBoard());
64 const int x=pos.x();
65 const int y=pos.y();
66 return (x-1)*11+(y-1);
67 }
71 void setBit(int offset){
72 assert(0<=offset && offset<=96);
73 int index=offset>>6;
74 unsigned long long tmpMask=1ull<<(offset&63);
75 assert((index == 0) || (index == 1));
76 mask[index]|= tmpMask;
77 }
78 void setBit(Square pos){
79 setBit(positionToOffset(pos));
80 }
84 void clearBit(int offset){
85 assert(0<=offset && offset<=96);
86 int index=offset>>6;
87 unsigned long long tmpMask=1ull<<(offset&63);
88 assert((index == 0) || (index == 1));
89 mask[index]&= ~tmpMask;
90 }
91 void clearBit(Square pos){
92 clearBit(positionToOffset(pos));
93 }
94 bool isZero() const{
95 return (mask[0]|mask[1])==0ull;
96 }
97 BoardBitMask& operator=(BoardBitMask const& rhs){
98 if (this == &rhs)
99 return *this;
100
101 #ifdef USE_XMM
102 xmm=rhs.xmm;
103 #else
104 mask[0]=rhs.mask[0];
105 mask[1]=rhs.mask[1];
106 #endif
107 return *this;
108 }
109
110 };
111
112
113 inline BoardBitMask& operator^=(BoardBitMask& lhs,BoardBitMask const& rhs){
114 #ifdef USE_XMM
115 lhs.xmm=__builtin_ia32_xorps(lhs.xmm,rhs.xmm);
116 #else
117 lhs.mask[0]^=rhs.mask[0];
118 lhs.mask[1]^=rhs.mask[1];
119 #endif
120 return lhs;
121 }
122
123 inline BoardBitMask operator^(BoardBitMask const& lhs,BoardBitMask const& rhs){
124 #ifdef USE_XMM
125 BoardBitMask ret=lhs;
126 ret.xmm=__builtin_ia32_xorps(lhs.xmm,rhs.xmm);
127 return ret;
128 #else
129 BoardBitMask ret=lhs;
130 ret.mask[0]^=rhs.mask[0];
131 ret.mask[1]^=rhs.mask[1];
132 return ret;
133 #endif
134 }
135 std::ostream& operator<<(std::ostream& os,BoardBitMask const& boardBitMask);
136
137 class BoardBitMaskTable{
138 CArray<BoardBitMask, Square::SIZE> maskOfSquare;
143 CArray2d<BoardBitMask,Square::SIZE,Square::SIZE> rookBetweenMask;
147 CArray2d<BoardBitMask, Square::SIZE,Square::SIZE> lanceBetweenMask;
148 CArray2d<BoardBitMask, Square::SIZE,Square::SIZE> bishopBetweenMask;
149 private:
150 void initMaskOfSquare();
151 void initBetweenMask();
152 public:
153 BoardBitMaskTable();
154 const BoardBitMask& getMask(Square pos) const{
155 assert(pos.isOnBoard());
156 return maskOfSquare[pos.index()];
157 }
158 const BoardBitMask& getRookMask(Square from,Square to) const{
159 assert(from.isOnBoard() && to.isOnBoard());
160 return rookBetweenMask[from.index()][to.index()];
161 }
162 const BoardBitMask& getBishopMask(Square from,Square to) const{
163 assert(from.isOnBoard() && to.isOnBoard());
164 return bishopBetweenMask[from.index()][to.index()];
165 }
166 const BoardBitMask& getLanceMask(Square from,Square to) const{
167 assert(from.isOnBoard() && to.isOnBoard());
168 return lanceBetweenMask[from.index()][to.index()];
169 }
170 };
171 #if 0
172 // テーブル削除予定
173 extern const BoardBitMaskTable Board_Bit_Mask_Table;
174 #endif
175 } // namespace effect
176 } // namespace osl
177 #endif // _BOARD_BIT_MASK_H
178 // ;;; Local Variables:
179 // ;;; mode:c++
180 // ;;; c-basic-offset:2
181 // ;;; End: