-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagicmoves.h
More file actions
130 lines (116 loc) · 5.39 KB
/
magicmoves.h
File metadata and controls
130 lines (116 loc) · 5.39 KB
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
/**
*magicmoves.h
*
*Header file for magic move bitboard generation. Include this in any files
*need this functionality.
*
*Usage:
*You must first initialize the generator with a call to initmagicmoves().
*Then you can use the following macros for generating move bitboards by
*giving them a square and an occupancy. The macro will then "return"
*the correct move bitboard for that particular square and occupancy. It
*has been named Rmagic and Bmagic so that it will not conflict with
*any functions/macros in your chess program called Rmoves/Bmoves. You
*can macro Bmagic/Rmagic to Bmoves/Rmoves if you wish. If you want to
*minimize the size of the bitboards, make MINIMIZE_MAGIC uncommented in this
*header (more info on this later). Where you typedef your unsigned 64-bit
*integer declare __64_BIT_INTEGER_DEFINED__. If USE_INLINING is uncommented,
*the macros will be expressed as MMINLINEd functions. If PERFECT_MAGIC_HASH is
*uncomment, the move generator will use an additional indrection to make the
*table sizes smaller : (~50kb+((original size)/sizeof(PERFECT_MAGIC_HASH)).
*The size listed from here on out are the sizes without PERFECT_MAGIC_HASH.
*
*Bmagic(square, occupancy)
*Rmagic(square, occupancy)
*
*Square is an integer that is greater than or equal to zero and less than 64.
*Occupancy is any unsigned 64-bit integer that describes which squares on
*the board are occupied.
*
*The following macros are identical to Rmagic and Bmagic except that the
*occupancy is assumed to already have been "masked". Look at the following
*source or read up on the internet about magic bitboard move generation to
*understand the usage of these macros and what it means by "an occupancy that
*has already been masked". Using the following macros when possible might be
*a tiny bit faster than using Rmagic and Bmagic because it avoids an array
*access and a 64-bit & operation.
*
*BmagicNOMASK(square, occupancy)
*RmagicNOMASK(square, occupancy)
*
*Unsigned 64 bit integers are referenced by this generator as U64.
*Edit the beginning lines of this header for the defenition of a 64 bit
*integer if necessary.
*
*If MINIMIZE_MAGIC is defined before including this file:
*The move bitboard generator will use up 841kb of memory.
*41kb of memory is used for the bishop database and 800kb is used for the rook
*database. If you feel the 800kb rook database is too big, then comment it out
*and use a more traditional move bitboard generator in conjunction with the
*magic move bitboard generator for bishops.
*
*If MINIMIAZE_MAGIC is not defined before including this file:
*The move bitboard generator will use up 2304kb of memory but might perform a bit
*faster.
*
*Copyright (C) 2007 Pradyumna Kannan.
*
*This code is provided 'as-is', without any expressed or implied warranty.
*In no event will the authors be held liable for any damages arising from
*the use of this code. Permission is granted to anyone to use this
*code for any purpose, including commercial applications, and to alter
*it and redistribute it freely, subject to the following restrictions:
*
*1. The origin of this code must not be misrepresented; you must not
*claim that you wrote the original code. If you use this code in a
*product, an acknowledgment in the product documentation would be
*appreciated but is not required.
*
*2. Altered source versions must be plainly marked as such, and must not be
*misrepresented as being the original code.
*
*3. This notice may not be removed or altered from any source distribution.
*/
/* the contents of this file has been altered */
#ifndef _magicmovesh
#define _magicmovesh
//typedef uint64_t u64;
/*Defining the inlining keyword*/
#define MMINLINE inline
#define C64(constantU64) constantU64
extern const u64 magicmoves_r_magics[64];
extern const u64 magicmoves_r_mask[64];
extern const u64 magicmoves_b_magics[64];
extern const u64 magicmoves_b_mask[64];
extern const unsigned int magicmoves_b_shift[64];
extern const unsigned int magicmoves_r_shift[64];
#define MINIMAL_B_BITS_SHIFT(square) 55
#define MINIMAL_R_BITS_SHIFT(square) 52
extern u64 magicmovesbdb[64][1<<9];
extern u64 magicmovesrdb[64][1<<12];
static inline u64 Bmagic(const unsigned int square,const u64 occupancy)
{
return magicmovesbdb[square][(((occupancy)&magicmoves_b_mask[square])*magicmoves_b_magics[square])>>MINIMAL_B_BITS_SHIFT(square)];
}
static inline u64 Rmagic(const unsigned int square,const u64 occupancy)
{
return magicmovesrdb[square][(((occupancy)&magicmoves_r_mask[square])*magicmoves_r_magics[square])>>MINIMAL_R_BITS_SHIFT(square)];
}
static inline u64 BmagicNOMASK(const unsigned int square,const u64 occupancy)
{
return magicmovesbdb[square][((occupancy)*magicmoves_b_magics[square])>>MINIMAL_B_BITS_SHIFT(square)];
}
static inline u64 RmagicNOMASK(const unsigned int square, const u64 occupancy)
{
return magicmovesrdb[square][((occupancy)*magicmoves_r_magics[square])>>MINIMAL_R_BITS_SHIFT(square)];
}
static inline u64 Qmagic(const unsigned int square,const u64 occupancy)
{
return Bmagic(square,occupancy)|Rmagic(square,occupancy);
}
static inline u64 QmagicNOMASK(const unsigned int square, const u64 occupancy)
{
return BmagicNOMASK(square,occupancy)|RmagicNOMASK(square,occupancy);
}
extern void init_magicmoves(void);
#endif //_magicmoveshvesh