Add XBoard protocol drivers
[bonanza.git] / gendrop.c
1 #include "shogi.h"
2
3 unsigned int *
4 b_gen_drop( tree_t * restrict __ptree__, unsigned int * restrict pmove )
5 {
6   const tree_t * restrict ptree = __ptree__;
7   bitboard_t bb_target;
8   unsigned int ihand, ibb_target0a, ibb_target0b, ibb_pawn_cmp, utemp;
9   unsigned int ais_pawn[nfile];
10   int nhand, ito, i, nolance, noknight;
11   int ahand[6];
12
13   if ( ! HAND_B ) { return pmove; }     /* return! */
14   ihand = HAND_B;
15   nhand = 0;
16   if ( IsHandKnight( ihand ) ) { ahand[ nhand++ ] = Drop2Move(knight); }
17   noknight = nhand;
18   if ( IsHandLance( ihand ) )  { ahand[ nhand++ ] = Drop2Move(lance); }
19   nolance  = nhand;
20   if ( IsHandSilver( ihand ) ) { ahand[ nhand++ ] = Drop2Move(silver); }
21   if ( IsHandGold( ihand ) )   { ahand[ nhand++ ] = Drop2Move(gold); }
22   if ( IsHandBishop( ihand ) ) { ahand[ nhand++ ] = Drop2Move(bishop); }
23   if ( IsHandRook( ihand ) )   { ahand[ nhand++ ] = Drop2Move(rook); }
24
25   BBOr( bb_target, BB_BOCCUPY, BB_WOCCUPY );
26   BBNot( bb_target, bb_target );
27   ibb_target0a = bb_target.p[0] & 0x7fc0000U;
28   ibb_target0b = bb_target.p[0] & 0x003fe00U;
29   bb_target.p[0] &= 0x00001ffU;
30   bb_target.p[1] &= 0x7ffffffU;
31   bb_target.p[2] &= 0x7ffffffU;
32
33   if ( IsHandPawn( ihand ) )
34     {
35       ibb_pawn_cmp= BB_BPAWN_ATK.p[0] | BB_BPAWN_ATK.p[1] | BB_BPAWN_ATK.p[2];
36       ais_pawn[0] = ibb_pawn_cmp & ( mask_file1 >> 0 );
37       ais_pawn[1] = ibb_pawn_cmp & ( mask_file1 >> 1 );
38       ais_pawn[2] = ibb_pawn_cmp & ( mask_file1 >> 2 );
39       ais_pawn[3] = ibb_pawn_cmp & ( mask_file1 >> 3 );
40       ais_pawn[4] = ibb_pawn_cmp & ( mask_file1 >> 4 );
41       ais_pawn[5] = ibb_pawn_cmp & ( mask_file1 >> 5 );
42       ais_pawn[6] = ibb_pawn_cmp & ( mask_file1 >> 6 );
43       ais_pawn[7] = ibb_pawn_cmp & ( mask_file1 >> 7 );
44       ais_pawn[8] = ibb_pawn_cmp & ( mask_file1 >> 8 );
45  
46       while ( BBToU( bb_target ) )
47         {
48           ito   = LastOne( bb_target );
49           utemp = To2Move(ito);
50           if ( ! ais_pawn[aifile[ito]] && ! IsMateBPawnDrop(__ptree__,ito) )
51             {
52               *pmove++ = utemp|Drop2Move(pawn);
53             }
54           for ( i = 0; i < nhand; i++ ) { *pmove++ = utemp | ahand[i]; }
55           Xor( ito, bb_target );
56         }
57
58       while ( ibb_target0b )
59         {
60           ito   = last_one0( ibb_target0b );
61           utemp = To2Move(ito);
62           if ( ! ais_pawn[aifile[ito]] && ! IsMateBPawnDrop(__ptree__,ito) )
63             {
64               *pmove++ = utemp | Drop2Move(pawn);
65             }
66           for ( i = noknight; i < nhand; i++ ) { *pmove++ = utemp|ahand[i]; }
67           ibb_target0b ^= abb_mask[ito].p[0];
68         }
69     }
70   else {
71     while ( BBToU( bb_target ) )
72       {
73         ito   = LastOne( bb_target );
74         utemp = To2Move(ito);
75         for ( i = 0; i < nhand; i++ ) { *pmove++ = utemp|ahand[i]; }
76         Xor( ito, bb_target );
77       }
78
79     while ( ibb_target0b )
80       {
81         ito = last_one0( ibb_target0b );
82         utemp = To2Move(ito);
83         for ( i = noknight; i < nhand; i++ ) { *pmove++ = utemp|ahand[ i ]; }
84         ibb_target0b ^= abb_mask[ ito ].p[0];
85       }
86   }
87
88   while ( ibb_target0a )
89     {
90       ito = last_one0( ibb_target0a );
91       utemp = To2Move(ito);
92       for ( i = nolance; i < nhand; i++ ) { *pmove++ = utemp|ahand[ i ]; }
93       ibb_target0a ^= abb_mask[ ito ].p[0];
94     }
95
96   return pmove;
97 }
98
99
100 unsigned int *
101 w_gen_drop( tree_t * restrict __ptree__, unsigned int * restrict pmove )
102 {
103   const tree_t * restrict ptree = __ptree__;
104   bitboard_t bb_target;
105   unsigned int ihand, ibb_target2a, ibb_target2b, ibb_pawn_cmp, utemp;
106   unsigned int ais_pawn[nfile];
107   int nhand, ito, i, nolance, noknight;
108   int ahand[6];
109
110   if ( ! HAND_W ) { return pmove; }     /* return! */
111   ihand = HAND_W;
112   nhand = 0;
113   if ( IsHandKnight( ihand ) ) { ahand[ nhand++ ] = Drop2Move(knight); }
114   noknight = nhand;
115   if ( IsHandLance( ihand ) )  { ahand[ nhand++ ] = Drop2Move(lance); }
116   nolance  = nhand;
117   if ( IsHandSilver( ihand ) ) { ahand[ nhand++ ] = Drop2Move(silver); }
118   if ( IsHandGold( ihand ) )   { ahand[ nhand++ ] = Drop2Move(gold); }
119   if ( IsHandBishop( ihand ) ) { ahand[ nhand++ ] = Drop2Move(bishop); }
120   if ( IsHandRook( ihand ) )   { ahand[ nhand++ ] = Drop2Move(rook); }
121
122   BBOr( bb_target, BB_BOCCUPY, BB_WOCCUPY );
123   BBNot( bb_target, bb_target );
124   ibb_target2a = bb_target.p[2] & 0x00001ffU;
125   ibb_target2b = bb_target.p[2] & 0x003fe00U;
126   bb_target.p[0] &= 0x7ffffffU;
127   bb_target.p[1] &= 0x7ffffffU;
128   bb_target.p[2] &= 0x7fc0000U;
129
130   if ( IsHandPawn( ihand ) )
131     {
132       ibb_pawn_cmp= BB_WPAWN_ATK.p[0] | BB_WPAWN_ATK.p[1] | BB_WPAWN_ATK.p[2];
133       ais_pawn[0] = ibb_pawn_cmp & ( mask_file1 >> 0 );
134       ais_pawn[1] = ibb_pawn_cmp & ( mask_file1 >> 1 );
135       ais_pawn[2] = ibb_pawn_cmp & ( mask_file1 >> 2 );
136       ais_pawn[3] = ibb_pawn_cmp & ( mask_file1 >> 3 );
137       ais_pawn[4] = ibb_pawn_cmp & ( mask_file1 >> 4 );
138       ais_pawn[5] = ibb_pawn_cmp & ( mask_file1 >> 5 );
139       ais_pawn[6] = ibb_pawn_cmp & ( mask_file1 >> 6 );
140       ais_pawn[7] = ibb_pawn_cmp & ( mask_file1 >> 7 );
141       ais_pawn[8] = ibb_pawn_cmp & ( mask_file1 >> 8 );
142  
143       while ( BBToU( bb_target ) )
144         {
145           ito   = FirstOne( bb_target );
146           utemp = To2Move(ito);
147           if ( ! ais_pawn[aifile[ito]] && ! IsMateWPawnDrop(__ptree__,ito) )
148             {
149               *pmove++ = utemp | Drop2Move(pawn);
150             }
151           for ( i = 0; i < nhand; i++ ) { *pmove++ = utemp | ahand[i]; }
152           Xor( ito, bb_target );
153         }
154
155       while ( ibb_target2b )
156         {
157           ito   = first_one2( ibb_target2b );
158           utemp = To2Move(ito);
159           if ( ! ais_pawn[aifile[ito]] && ! IsMateWPawnDrop(__ptree__,ito) )
160             {
161               *pmove++ = utemp | Drop2Move(pawn);
162             }
163           for ( i = noknight; i < nhand; i++ ) { *pmove++ = utemp | ahand[i]; }
164           ibb_target2b ^= abb_mask[ito].p[2];
165         }
166     }
167   else {
168     while ( BBToU( bb_target ) )
169       {
170         ito   = FirstOne( bb_target );
171         utemp = To2Move(ito);
172         for ( i = 0; i < nhand; i++ ) { *pmove++ = utemp|ahand[i]; }
173         Xor( ito, bb_target );
174       }
175
176     while ( ibb_target2b )
177       {
178         ito   = first_one2( ibb_target2b );
179         utemp = To2Move(ito);
180         for ( i = noknight; i < nhand; i++ ) { *pmove++ = utemp|ahand[i]; }
181         ibb_target2b ^= abb_mask[ito].p[2];
182       }
183   }
184
185   while ( ibb_target2a )
186     {
187       ito   = first_one2( ibb_target2a );
188       utemp = To2Move(ito);
189       for ( i = nolance; i < nhand; i++ ) { *pmove++ = utemp|ahand[i]; }
190       ibb_target2a ^= abb_mask[ito].p[2];
191     }
192
193   return pmove;
194 }