File: x86.c

package info (click to toggle)
crafty 19.15-1
  • links: PTS
  • area: non-free
  • in suites: sarge
  • size: 2,488 kB
  • ctags: 3,374
  • sloc: ansic: 28,852; cpp: 5,834; makefile: 431; sh: 108; perl: 30
file content (405 lines) | stat: -rw-r--r-- 13,383 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <stdio.h>
#include <stdlib.h>
#include "chess.h"
#include "data.h"

#ifdef USE_ASSEMBLY_A
     
/* Diag info offsets */

#define D_ATTACKS        0   /* Must be 0 - hard-coded (see comments) */ 

#define D_MOBILITY       4
#define D_WHICH_ATTACK   8
#define D_SHIFT         12
#define D_MASK          13
#define AD_SHIFT        14
#define AD_MASK         15
#define AD_WHICH_ATTACK 16
#define AD_MOBILITY     20
#define AD_ATTACKS      24

/* Position offsets */

#define W_OCCUPIED        0
#define B_OCCUPIED        8
#define RL90             16
#define RL45             24
#define RR45             32

     
/* Struct at offsets:
        struct at {
          unsigned char which_attack[8][64];
          BITBOARD      file_attack_bitboards[8][12]; 
          unsigned char rank_attack_bitboards[8][12]; 
          unsigned char length8_mobility[8][12]; 
          unsigned char short_mobility[116];
        } at;
*/

#define WHICH_ATTACK       0
#define FILE_ATTACKS     512
#define RANK_ATTACKS    1280
#define LEN8_MOBILITY   1376
#define SHRT_MOBILITY   1472     
     
/*
  BITBOARD AttacksDiaga1Func (DIAG_INFO *diag, POSITION *board)
*/

__declspec(naked) BITBOARD __cdecl AttacksDiaga1Func (DIAG_INFO *diag, POSITION *board) {

  __asm {
        push    esi
        mov     esi, 8[esp]                   /* diag_info     */ 
        mov     eax, 12[esp]                  /* boardp        */ 
        mov     cl, AD_SHIFT[esi]             /* shift         */ 
        cmp     cl, 32
        sbb     edx, edx
        mov     eax, RL45+4[eax+edx*4]        /* occupied      */ 
        movzx   edx, byte ptr AD_MASK[esi]    /* mask          */ 
        shr     eax, cl
        and     eax, edx
        add     eax, AD_WHICH_ATTACK[esi]     /* which_attack  */ 
        mov     ecx, AD_ATTACKS[esi]          /* attack_vector */ 
        movzx   edx, byte ptr [eax]           /* attack_index  */ 
        pop     esi
        mov     eax, [ecx+edx*8]
        mov     edx, 4[ecx+edx*8]
        ret
  }
}
     
/*
  BITBOARD AttacksDiagh1Func(DIAG_INFO *diag, POSITION *board)
*/

__declspec(naked) BITBOARD __cdecl AttacksDiagh1Func(DIAG_INFO *diag, POSITION *board) {

  __asm {
        push    esi
        mov     esi, 8[esp]                   /* diag_info     */ 
        mov     eax, 12[esp]                  /* boardp        */ 
        mov     cl, D_SHIFT[esi]              /* shift         */ 
        cmp     cl, 32
        sbb     edx, edx
        mov     eax, RR45+4[eax+edx*4]        /* occupied      */ 
        movzx   edx, byte ptr D_MASK[esi]     /* mask          */ 
        shr     eax, cl
        and     eax, edx
        add     eax, D_WHICH_ATTACK[esi]      /* which_attack  */ 
        mov     ecx, [esi]                    /* D_ATTACKS     */
                                              /* attack_vector */
        movzx   edx, byte ptr [eax]           /* attack_index  */ 
        pop     esi
        mov     eax, [ecx+edx*8]
        mov     edx, 4[ecx+edx*8]
        ret
  }
}
     
/*
  BITBOARD AttacksBishopFunc(DIAG_INFO *diag, POSITION *board)
*/

__declspec(naked) BITBOARD __cdecl AttacksBishopFunc(DIAG_INFO *diag, POSITION *board) {

  __asm {
        push    ebx
        push    esi
        push    edi
        mov     esi, 16[esp]                  /* diag_info     */ 
        mov     edi, 20[esp]                  /* boardp        */ 
        mov     cl, byte ptr AD_SHIFT[esi]    /* shift         */ 
        cmp     cl, 32
        sbb     edx, edx
        movzx   ebx, byte ptr AD_MASK[esi]    /* mask          */ 
        mov     edi, RL45+4[edi+edx*4]        /* occupied      */ 
        shr     edi, cl
        and     edi, ebx
        add     edi, AD_WHICH_ATTACK[esi]     /* which_attack  */ 
        mov     ecx, AD_ATTACKS[esi]          /* attack_vector */ 
        movzx   ebx, byte ptr [edi]           /* attack_index  */ 
        mov     edi, 20[esp]                  /* again boardp  */ 
        lea     edx, [ecx+ebx*8]
        mov     cl, D_SHIFT[esi]              /* shift         */ 
        cmp     cl, 32
        sbb     ebx, ebx
        mov     edi, RR45+4[edi+ebx*4]        /* occupied      */ 
        movzx   ebx, byte ptr D_MASK[esi]     /* mask          */ 
        shr     edi, cl
        mov     ecx, [esi]                    /* D_ATTACKS     */
                                              /* attack_vector */
        and     edi, ebx
        mov     eax, [edx]
        add     edi, D_WHICH_ATTACK[esi]      /* which_attack  */ 
        mov     edx, 4[edx]
        movzx   ebx, byte ptr [edi]           /* attack_index  */ 
        pop     edi
        or      eax, [ecx+ebx*8]
        pop     esi
        or      edx, 4[ecx+ebx*8]
        pop     ebx
        ret
  }
}
     
/*
  unsigned MobilityDiaga1Func(DIAG_INFO *diag, POSITION *board)
*/

__declspec(naked) unsigned __cdecl MobilityDiaga1Func(DIAG_INFO *diag, POSITION *board) {

  __asm {
        push    esi
        mov     esi, 8[esp]                   /* diag_info     */ 
        mov     eax, 12[esp]                  /* boardp        */ 
        mov     cl, AD_SHIFT[esi]             /* shift         */ 
        cmp     cl, 32
        sbb     edx, edx
        mov     eax, RL45+4[eax+edx*4]        /* occupied      */ 
        movzx   edx, byte ptr AD_MASK[esi]    /* mask          */ 
        shr     eax, cl
        and     eax, edx
        add     eax, AD_WHICH_ATTACK[esi]     /* which_attack  */ 
        mov     ecx, AD_MOBILITY[esi]         /* mobility_vector */ 
        movzx   edx, byte ptr [eax]           /* attack_index  */ 
        pop     esi
        movzx   eax, byte ptr [ecx+edx*1]
        ret
  }
}
     
/*
  unsigned MobilityDiagh1Func(DIAG_INFO *diag, POSITION *board)
*/

__declspec(naked) unsigned __cdecl MobilityDiagh1Func(DIAG_INFO *diag, POSITION *board) {

  __asm {
        push    esi
        mov     esi, 8[esp]                   /* diag_info     */ 
        mov     eax, 12[esp]                  /* boardp        */ 
        mov     cl, D_SHIFT[esi]              /* shift         */ 
        cmp     cl, 32
        sbb     edx, edx
        mov     eax, RR45+4[eax+edx*4]        /* occupied      */ 
        movzx   edx, byte ptr D_MASK[esi]     /* mask          */ 
        shr     eax, cl
        and     eax, edx
        add     eax, D_WHICH_ATTACK[esi]      /* which_attack  */ 
        mov     ecx, D_MOBILITY[esi]          /* mobility_vector */ 
        movzx   edx, byte ptr [eax]           /* attack_index  */ 
        pop     esi
        movzx   eax, byte ptr [ecx+edx*1]
        ret
  }
}
     
/*
  BITBOARD AttacksRankFunc(int square, POSITION *board)
*/

__declspec(naked) BITBOARD __cdecl AttacksRankFunc(int square, POSITION *board) {

  __asm {

        mov     ecx, 4[esp]                   /* square               */ 
        mov     edx, 8[esp]                   /* boardp               */ 
        push    esi
        mov     esi, ecx
        and     esi, 7                        /* file                 */ 
        not     ecx
        and     ecx, 0x38
        push    ebp
        cmp     ecx, 32
        push    edi
        sbb     ebp, ebp
        lea     edi, [esi+esi*2]              /* file * 3             */ 
        shl     esi, 6                        /* file * 64            */ 
        mov     eax, W_OCCUPIED+4[edx+ebp*4]
        inc     ecx
        or      eax, B_OCCUPIED+4[edx+ebp*4] 
        shr     eax, cl
        and     eax, 0x3f
        movzx   edx, byte ptr at+WHICH_ATTACK[eax+esi] 
        dec     ecx
        movzx   eax, byte ptr at+RANK_ATTACKS[edx+edi*4] 
        shl     eax, cl
        pop     edi
        mov     edx, eax
        and     eax, ebp
        not     ebp
        and     edx, ebp
        pop     ebp
        pop     esi
        ret
  }
}
     
/*
  BITBOARD AttacksFileFunc(int square, POSITION *board)
*/

__declspec(naked) BITBOARD __cdecl AttacksFileFunc(int square, POSITION *board) {

  __asm {
        mov     ecx, 4[esp]                   /* square               */ 
        mov     edx, 8[esp]                   /* boardp               */ 
        push    esi
        mov     esi, ecx
        not     ecx
        and     ecx, 7                        /* file                 */ 
        shr     esi, 3                        /* rank                 */ 
        push    edi
        lea     edi, [esi+esi*2]              /* rank * 3             */ 
        shl     esi, 6                        /* rank * 64            */ 
        lea     ecx, 1[ecx*8]                 /* (file << 3) + 1      */ 
        cmp     ecx, 32
        sbb     eax, eax
        shl     edi, 5
        mov     eax, RL90+4[edx+eax*4]
        shr     eax, cl
        and     eax, 0x3f
        movzx   edx, byte ptr at+WHICH_ATTACK[eax+esi] 
        dec     ecx
        mov     eax, at+FILE_ATTACKS[edi+edx*8] 
        shr     ecx, 3
        mov     edx, at+FILE_ATTACKS+4[edi+edx*8] 
        pop     edi
        shl     eax, cl
        pop     esi
        shl     edx, cl
        ret
  }
}
     
/*
  BITBOARD AttacksRookFunc(int square, POSITION *board)
*/

__declspec(naked) BITBOARD __cdecl AttacksRookFunc(int square, POSITION *board) {

  __asm {
        mov     ecx, 4[esp]                   /* square               */ 
        mov     edx, 8[esp]                   /* boardp               */ 
        push    ebp
        push    esi
        mov     esi, ecx
        not     ecx
        and     esi, 7                        /* file                 */ 
        push    ebx
        and     ecx, 0x38                     /* rank << 3            */ 
        push    edi
        cmp     ecx, 32
        lea     edi, [esi+esi*2]              /* file * 3             */ 
        sbb     eax, eax
        shl     esi, 6                        /* file * 64            */ 
        mov     ebp, W_OCCUPIED+4[edx+eax*4]
        inc     ecx
        or      ebp, B_OCCUPIED+4[edx+eax*4] 
        shr     ebp, cl
        and     ebp, 0x3f
        movzx   ebx, byte ptr at+WHICH_ATTACK[esi+ebp*1] 
        dec     ecx
        mov     esi, 20[esp]                  /* square               */ 
        movzx   ebp, byte ptr at+RANK_ATTACKS[ebx+edi*4]
        shl     ebp, cl
        mov     ecx, esi
        mov     ebx, ebp
        not     ecx
        and     ebp, eax
        and     ecx, 7                        /* file                 */ 
        not     eax
        shr     esi, 3                        /* rank                 */ 
        and     ebx, eax
        /* Now we have: ebp - ebx, lo - hi */
        lea     ecx, 1[ecx*8]                 /* (file << 3) + 1      */ 
        lea     edi, [esi+esi*2]              /* rank * 3             */ 
        shl     esi, 6                        /* rank * 64            */ 
        cmp     ecx, 32
        sbb     eax, eax
        shl     edi, 5
        mov     eax, RL90+4[edx+eax*4]
        shr     eax, cl
        and     eax, 0x3f
        movzx   edx, byte ptr at+WHICH_ATTACK[eax+esi] 
        dec     ecx
        mov     eax, at+FILE_ATTACKS[edi+edx*8] 
        shr     ecx, 3
        shl     eax, cl
        mov     edx, at+FILE_ATTACKS+4[edi+edx*8] 
        shl     edx, cl
        pop     edi
        or      edx, ebx
        pop     ebx
        or      eax, ebp
        pop     esi
        pop     ebp
        ret
  }
}
     
/*
  unsigned MobilityRankFunc(int square, POSITION *board)
*/

__declspec(naked) unsigned __cdecl MobilityRankFunc(int square, POSITION *board) {

  __asm {
        mov     ecx, 4[esp]                   /* square               */ 
        push    esi
        mov     esi, ecx
        not     ecx
        and     ecx, 0x38
        cmp     ecx, 32
        sbb     edx, edx
        shl     edx, 2
        add     edx, 12[esp]                  /* boardp               */ 
        and     esi, 7                        /* file                 */ 
        mov     eax, W_OCCUPIED+4[edx]
        inc     ecx
        or      eax, B_OCCUPIED+4[edx]
        shr     eax, cl
        lea     ecx, [esi+esi*2]              /* file * 3             */ 
        shl     esi, 6                        /* file * 64            */ 
        and     eax, 0x3f
        movzx   edx, byte ptr at+WHICH_ATTACK[eax+esi] 
        pop     esi
        movzx   eax, byte ptr at+LEN8_MOBILITY[edx+ecx*4] 
        ret
  }
}
     
/*
  unsigned MobilityFileFunc(int square, POSITION *board)
*/

__declspec(naked) unsigned __cdecl MobilityFileFunc(int square, POSITION *board) {

  __asm {
        mov     ecx, 4[esp]                   /* square               */ 
        push    esi
        mov     esi, ecx
        not     ecx
        and     ecx, 7                        /* file                 */ 
        shr     esi, 3                        /* rank                 */ 
        lea     ecx, 1[ecx*8]                 /* (file << 3) + 1      */ 
        cmp     ecx, 32
        sbb     edx, edx
        shl     edx, 2
        add     edx, 12[esp]                  /* boardp               */ 
        mov     eax, RL90+4[edx]
        shr     eax, cl
        lea     ecx, [esi+esi*2]              /* rank * 3             */ 
        shl     esi, 6                        /* rank * 64            */ 
        and     eax, 0x3f
        movzx   edx, byte ptr at+WHICH_ATTACK[eax+esi] 
        pop     esi
        movzx   eax, byte ptr at+LEN8_MOBILITY[edx+ecx*4] 
        ret
  }
}
     
#endif /* USE_ASSEMBLY_A */