Skip to content

Commit c2f884f

Browse files
committed
Automate code formatting
$ clang-format -i **/*.c **/*.h
1 parent a0e5a1d commit c2f884f

31 files changed

+606
-835
lines changed

.clang-format

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BasedOnStyle: LLVM
2+
ColumnLimit: '100'
3+
IndentPPDirectives: BeforeHash
4+
IndentWidth: 4

src/bitboard.c

Lines changed: 78 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,54 @@
1111
*
1212
* You should have received a copy of the GNU General Public License along with this program. If
1313
* not, see <http://www.gnu.org/licenses/>.
14-
*/
15-
#include <stdio.h>
14+
*/
1615
#include "bitboard.h"
16+
#include <stdio.h>
1717

1818
bitboard_t Rank[NB_RANK], File[NB_FILE];
1919
bitboard_t PawnAttacks[NB_COLOR][NB_SQUARE], KnightAttacks[NB_SQUARE], KingAttacks[NB_SQUARE];
2020
bitboard_t Segment[NB_SQUARE][NB_SQUARE], Ray[NB_SQUARE][NB_SQUARE];
2121

22-
static void safe_set_bit(bitboard_t *b, int rank, int file)
23-
{
22+
static void safe_set_bit(bitboard_t *b, int rank, int file) {
2423
if (0 <= rank && rank < NB_RANK && 0 <= file && file < NB_FILE)
2524
bb_set(b, square_from(rank, file));
2625
}
2726

2827
static const bitboard_t RookMagic[NB_SQUARE] = {
2928
0x808000645080c000, 0x208020001480c000, 0x4180100160008048, 0x8180100018001680,
3029
0x4200082010040201, 0x8300220400010008, 0x3100120000890004, 0x4080004500012180,
31-
0x1548000a1804008, 0x4881004005208900, 0x480802000801008, 0x2e8808010008800,
32-
0x8cd804800240080, 0x8a058002008c0080, 0x514000c480a1001, 0x101000282004d00,
30+
0x1548000a1804008, 0x4881004005208900, 0x480802000801008, 0x2e8808010008800,
31+
0x8cd804800240080, 0x8a058002008c0080, 0x514000c480a1001, 0x101000282004d00,
3332
0x2048848000204000, 0x3020088020804000, 0x4806020020841240, 0x6080420008102202,
34-
0x10050011000800, 0xac00808004000200, 0x10100020004, 0x1500020004004581,
35-
0x4c00180052080, 0x220028480254000, 0x2101200580100080, 0x407201200084200,
36-
0x18004900100500, 0x100200020008e410, 0x81020400100811, 0x12200024494,
37-
0x8006c002808006a5, 0x4201000404000, 0x5402202001180, 0x81001002100,
38-
0x100801000500, 0x4000020080800400, 0x4005050214001008, 0x810100118b000042,
39-
0xd01020040820020, 0x140a010014000, 0x420001500210040, 0x54210010030009,
40-
0x4000408008080, 0x2000400090100, 0x840200010100, 0x233442820004,
41-
0x800a42002b008200, 0x240200040009080, 0x242001020408200, 0x4000801000480480,
42-
0x2288008044000880, 0xa800400020180, 0x30011002880c00, 0x41110880440200,
43-
0x2001100442082, 0x1a0104002208101, 0x80882014010200a, 0x100100600409,
44-
0x2011048204402, 0x12000168041002, 0x80100008a000421, 0x240022044031182
45-
};
33+
0x10050011000800, 0xac00808004000200, 0x10100020004, 0x1500020004004581,
34+
0x4c00180052080, 0x220028480254000, 0x2101200580100080, 0x407201200084200,
35+
0x18004900100500, 0x100200020008e410, 0x81020400100811, 0x12200024494,
36+
0x8006c002808006a5, 0x4201000404000, 0x5402202001180, 0x81001002100,
37+
0x100801000500, 0x4000020080800400, 0x4005050214001008, 0x810100118b000042,
38+
0xd01020040820020, 0x140a010014000, 0x420001500210040, 0x54210010030009,
39+
0x4000408008080, 0x2000400090100, 0x840200010100, 0x233442820004,
40+
0x800a42002b008200, 0x240200040009080, 0x242001020408200, 0x4000801000480480,
41+
0x2288008044000880, 0xa800400020180, 0x30011002880c00, 0x41110880440200,
42+
0x2001100442082, 0x1a0104002208101, 0x80882014010200a, 0x100100600409,
43+
0x2011048204402, 0x12000168041002, 0x80100008a000421, 0x240022044031182};
4644

4745
static const bitboard_t BishopMagic[NB_SQUARE] = {
48-
0x88b030028800d040, 0x18242044c008010, 0x10008200440000, 0x4311040888800a00,
49-
0x1910400000410a, 0x2444240440000000, 0xcd2080108090008, 0x2048242410041004,
50-
0x8884441064080180, 0x42131420a0240, 0x28882800408400, 0x204384040b820200,
51-
0x402040420800020, 0x20910282304, 0x96004b10082200, 0x4000a44218410802,
52-
0x808034002081241, 0x101805210e1408, 0x9020400208010220, 0x820050c010044,
53-
0x24005480a00000, 0x200200900890, 0x808040049c100808, 0x9020202200820802,
54-
0x410282124200400, 0x90106008010110, 0x8001100501004201, 0x104080004030c10,
55-
0x80840040802008, 0x2008008102406000, 0x2000888004040460, 0xd0421242410410,
56-
0x8410100401280800, 0x801012000108428, 0x402080300b04, 0xc20020080480080,
46+
0x88b030028800d040, 0x18242044c008010, 0x10008200440000, 0x4311040888800a00,
47+
0x1910400000410a, 0x2444240440000000, 0xcd2080108090008, 0x2048242410041004,
48+
0x8884441064080180, 0x42131420a0240, 0x28882800408400, 0x204384040b820200,
49+
0x402040420800020, 0x20910282304, 0x96004b10082200, 0x4000a44218410802,
50+
0x808034002081241, 0x101805210e1408, 0x9020400208010220, 0x820050c010044,
51+
0x24005480a00000, 0x200200900890, 0x808040049c100808, 0x9020202200820802,
52+
0x410282124200400, 0x90106008010110, 0x8001100501004201, 0x104080004030c10,
53+
0x80840040802008, 0x2008008102406000, 0x2000888004040460, 0xd0421242410410,
54+
0x8410100401280800, 0x801012000108428, 0x402080300b04, 0xc20020080480080,
5755
0x40100e0201502008, 0x4014208200448800, 0x4050020607084501, 0x1002820180020288,
58-
0x800610040540a0c0, 0x301009014081004, 0x2200610040502800, 0x300442011002800,
59-
0x1022009002208, 0x110011000202100, 0x1464082204080240, 0x21310205800200,
60-
0x814020210040109, 0xc102008208c200a0, 0xc100702128080000, 0x1044205040000,
61-
0x1041002020000, 0x4200040408021000, 0x4004040c494000, 0x2010108900408080,
62-
0x820801040284, 0x800004118111000, 0x203040201108800, 0x2504040804208803,
63-
0x228000908030400, 0x10402082020200, 0xa0402208010100, 0x30c0214202044104
64-
};
56+
0x800610040540a0c0, 0x301009014081004, 0x2200610040502800, 0x300442011002800,
57+
0x1022009002208, 0x110011000202100, 0x1464082204080240, 0x21310205800200,
58+
0x814020210040109, 0xc102008208c200a0, 0xc100702128080000, 0x1044205040000,
59+
0x1041002020000, 0x4200040408021000, 0x4004040c494000, 0x2010108900408080,
60+
0x820801040284, 0x800004118111000, 0x203040201108800, 0x2504040804208803,
61+
0x228000908030400, 0x10402082020200, 0xa0402208010100, 0x30c0214202044104};
6562

6663
static bitboard_t RookDB[0x19000], BishopDB[0x1480];
6764
static bitboard_t *BishopAttacks[NB_SQUARE], *RookAttacks[NB_SQUARE];
@@ -71,8 +68,7 @@ static unsigned BishopShift[NB_SQUARE], RookShift[NB_SQUARE];
7168

7269
// Compute (from scratch) the squares attacked by a sliding piece, moving in directions dir, given
7370
// board occupancy occ.
74-
static bitboard_t slider_attacks(int square, bitboard_t occ, const int dir[4][2])
75-
{
71+
static bitboard_t slider_attacks(int square, bitboard_t occ, const int dir[4][2]) {
7672
bitboard_t result = 0;
7773

7874
for (int i = 0; i < 4; i++) {
@@ -94,17 +90,15 @@ static bitboard_t slider_attacks(int square, bitboard_t occ, const int dir[4][2]
9490
return result;
9591
}
9692

97-
static unsigned slider_index(bitboard_t occ, bitboard_t mask, bitboard_t magic, unsigned shift)
98-
{
93+
static unsigned slider_index(bitboard_t occ, bitboard_t mask, bitboard_t magic, unsigned shift) {
9994
return (unsigned)(((occ & mask) * magic) >> shift);
10095
}
10196

10297
static void init_slider_attacks(int square, bitboard_t mask[NB_SQUARE],
103-
const bitboard_t magic[NB_SQUARE], unsigned shift[NB_SQUARE], bitboard_t *attacksPtr[NB_SQUARE],
104-
const int dir[4][2])
105-
{
98+
const bitboard_t magic[NB_SQUARE], unsigned shift[NB_SQUARE],
99+
bitboard_t *attacksPtr[NB_SQUARE], const int dir[4][2]) {
106100
const bitboard_t edges = ((Rank[RANK_1] | Rank[RANK_8]) & ~Rank[rank_of(square)]) |
107-
((File[RANK_1] | File[RANK_8]) & ~File[file_of(square)]);
101+
((File[RANK_1] | File[RANK_8]) & ~File[file_of(square)]);
108102
mask[square] = slider_attacks(square, 0, dir) & ~edges;
109103
shift[square] = (unsigned)(64 - bb_count(mask[square]));
110104

@@ -115,19 +109,20 @@ static void init_slider_attacks(int square, bitboard_t mask[NB_SQUARE],
115109
bitboard_t occ = 0;
116110

117111
do {
118-
attacksPtr[square][slider_index(occ, mask[square], magic[square], shift[square])]
119-
= slider_attacks(square, occ, dir);
120-
occ = (occ - mask[square]) & mask[square]; // Carry-Rippler trick
112+
attacksPtr[square][slider_index(occ, mask[square], magic[square], shift[square])] =
113+
slider_attacks(square, occ, dir);
114+
occ = (occ - mask[square]) & mask[square]; // Carry-Rippler trick
121115
} while (occ);
122116
}
123117

124-
static __attribute__((constructor)) void bb_init(void)
125-
{
126-
static const int PawnDir[2][2] = {{1,-1}, {1,1}};
127-
static const int KnightDir[8][2] = {{-2,-1}, {-2,1}, {-1,-2}, {-1,2}, {1,-2}, {1,2}, {2,-1}, {2,1}};
128-
static const int KingDir[8][2] = {{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,1}, {1,-1}, {1,0}, {1,1}};
129-
static const int BishopDir[4][2] = {{-1,-1}, {-1,1}, {1,-1}, {1,1}};
130-
static const int RookDir[4][2] = {{-1,0}, {0,-1}, {0,1}, {1,0}};
118+
static __attribute__((constructor)) void bb_init(void) {
119+
static const int PawnDir[2][2] = {{1, -1}, {1, 1}};
120+
static const int KnightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
121+
{1, -2}, {1, 2}, {2, -1}, {2, 1}};
122+
static const int KingDir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
123+
{0, 1}, {1, -1}, {1, 0}, {1, 1}};
124+
static const int BishopDir[4][2] = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
125+
static const int RookDir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
131126

132127
// Initialise Rank[] and File[]
133128
for (int i = 0; i < 8; i++) {
@@ -181,140 +176,110 @@ static __attribute__((constructor)) void bb_init(void)
181176
}
182177
}
183178

184-
int opposite(int color)
185-
{
179+
int opposite(int color) {
186180
BOUNDS(color, NB_COLOR);
187-
return color ^ BLACK; // branchless for: color == WHITE ? BLACK : WHITE
181+
return color ^ BLACK; // branchless for: color == WHITE ? BLACK : WHITE
188182
}
189183

190-
int push_inc(int color)
191-
{
184+
int push_inc(int color) {
192185
BOUNDS(color, NB_COLOR);
193-
return UP - color * (UP - DOWN); // branchless for: color == WHITE ? UP : DOWN
186+
return UP - color * (UP - DOWN); // branchless for: color == WHITE ? UP : DOWN
194187
}
195188

196-
int square_from(int rank, int file)
197-
{
189+
int square_from(int rank, int file) {
198190
BOUNDS(rank, NB_RANK);
199191
BOUNDS(file, NB_FILE);
200192
return NB_FILE * rank + file;
201193
}
202194

203-
int rank_of(int square)
204-
{
195+
int rank_of(int square) {
205196
BOUNDS(square, NB_SQUARE);
206197
return square / NB_FILE;
207198
}
208199

209-
int file_of(int square)
210-
{
200+
int file_of(int square) {
211201
BOUNDS(square, NB_SQUARE);
212202
return square % NB_FILE;
213203
}
214204

215-
int relative_rank(int color, int rank)
216-
{
205+
int relative_rank(int color, int rank) {
217206
BOUNDS(color, NB_COLOR);
218207
BOUNDS(rank, NB_RANK);
219-
return rank ^ (RANK_8 * color); // branchless for: color == WHITE ? rank : RANK_8 - rank
208+
return rank ^ (RANK_8 * color); // branchless for: color == WHITE ? rank : RANK_8 - rank
220209
}
221210

222-
move_t move_build(int from, int to, int prom)
223-
{
211+
move_t move_build(int from, int to, int prom) {
224212
BOUNDS(from, NB_SQUARE);
225213
BOUNDS(to, NB_SQUARE);
226214
assert((unsigned)prom <= QUEEN || prom == NB_PIECE);
227215
return (move_t)(from | (to << 6) | (prom << 12));
228216
}
229217

230-
int move_from(move_t m)
231-
{
232-
return m & 077;
233-
}
218+
int move_from(move_t m) { return m & 077; }
234219

235-
int move_to(move_t m)
236-
{
237-
return (m >> 6) & 077;
238-
}
220+
int move_to(move_t m) { return (m >> 6) & 077; }
239221

240-
int move_prom(move_t m)
241-
{
222+
int move_prom(move_t m) {
242223
const unsigned prom = m >> 12;
243224
assert(prom <= QUEEN || prom == NB_PIECE);
244225
return (int)prom;
245226
}
246227

247-
bitboard_t bb_bishop_attacks(int square, bitboard_t occ)
248-
{
228+
bitboard_t bb_bishop_attacks(int square, bitboard_t occ) {
249229
BOUNDS(square, NB_SQUARE);
250230
return BishopAttacks[square][slider_index(occ, BishopMask[square], BishopMagic[square],
251-
BishopShift[square])];
231+
BishopShift[square])];
252232
}
253233

254-
bitboard_t bb_rook_attacks(int square, bitboard_t occ)
255-
{
234+
bitboard_t bb_rook_attacks(int square, bitboard_t occ) {
256235
BOUNDS(square, NB_SQUARE);
257-
return RookAttacks[square][slider_index(occ, RookMask[square], RookMagic[square],
258-
RookShift[square])];
236+
return RookAttacks[square]
237+
[slider_index(occ, RookMask[square], RookMagic[square], RookShift[square])];
259238
}
260239

261-
bool bb_test(bitboard_t b, int square)
262-
{
240+
bool bb_test(bitboard_t b, int square) {
263241
BOUNDS(square, NB_SQUARE);
264242
return b & (1ULL << square);
265243
}
266244

267-
void bb_clear(bitboard_t *b, int square)
268-
{
245+
void bb_clear(bitboard_t *b, int square) {
269246
BOUNDS(square, NB_SQUARE);
270247
assert(bb_test(*b, square));
271248
*b ^= 1ULL << square;
272249
}
273250

274-
void bb_set(bitboard_t *b, int square)
275-
{
251+
void bb_set(bitboard_t *b, int square) {
276252
BOUNDS(square, NB_SQUARE);
277253
assert(!bb_test(*b, square));
278254
*b ^= 1ULL << square;
279255
}
280256

281-
bitboard_t bb_shift(bitboard_t b, int i)
282-
{
283-
assert(-63 <= i && i <= 63); // oversized shift is undefined
257+
bitboard_t bb_shift(bitboard_t b, int i) {
258+
assert(-63 <= i && i <= 63); // oversized shift is undefined
284259
return i > 0 ? b << i : b >> -i;
285260
}
286261

287-
int bb_lsb(bitboard_t b)
288-
{
289-
assert(b); // lsb(0) is undefined
262+
int bb_lsb(bitboard_t b) {
263+
assert(b); // lsb(0) is undefined
290264
return __builtin_ctzll(b);
291265
}
292266

293-
int bb_msb(bitboard_t b)
294-
{
295-
assert(b); // msb(0) is undefined
267+
int bb_msb(bitboard_t b) {
268+
assert(b); // msb(0) is undefined
296269
return 63 - __builtin_clzll(b);
297270
}
298271

299-
int bb_pop_lsb(bitboard_t *b)
300-
{
272+
int bb_pop_lsb(bitboard_t *b) {
301273
const int square = bb_lsb(*b);
302274
*b &= *b - 1;
303275
return square;
304276
}
305277

306-
bool bb_several(bitboard_t b)
307-
{
308-
return b & (b - 1);
309-
}
278+
bool bb_several(bitboard_t b) { return b & (b - 1); }
310279

311-
int bb_count(bitboard_t b)
312-
{
313-
return __builtin_popcountll(b);
314-
}
280+
int bb_count(bitboard_t b) { return __builtin_popcountll(b); }
315281

316-
void bb_print(bitboard_t b)
317-
{
282+
void bb_print(bitboard_t b) {
318283
for (int rank = RANK_8; rank >= RANK_1; rank--) {
319284
char line[] = ". . . . . . . .";
320285

src/bitboard.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@
1111
*
1212
* You should have received a copy of the GNU General Public License along with this program. If
1313
* not, see <http://www.gnu.org/licenses/>.
14-
*/
14+
*/
1515
#pragma once
1616
#include <assert.h>
1717
#include <inttypes.h>
1818
#include <stdbool.h>
1919

2020
#define BOUNDS(v, ub) assert((unsigned)(v) < (ub))
2121

22-
enum {RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, NB_RANK};
23-
enum {FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, NB_FILE};
24-
enum {NB_SQUARE = 64}; // square = 8 * rank + file
22+
enum { RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, NB_RANK };
23+
enum { FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, NB_FILE };
24+
enum { NB_SQUARE = 64 }; // square = 8 * rank + file
2525

26-
enum {UP = 8, DOWN = -8, LEFT = -1, RIGHT = 1};
27-
enum {WHITE, BLACK, NB_COLOR};
28-
enum {KNIGHT, BISHOP, ROOK, QUEEN, KING, PAWN, NB_PIECE};
26+
enum { UP = 8, DOWN = -8, LEFT = -1, RIGHT = 1 };
27+
enum { WHITE, BLACK, NB_COLOR };
28+
enum { KNIGHT, BISHOP, ROOK, QUEEN, KING, PAWN, NB_PIECE };
2929

3030
int opposite(int color);
3131
int push_inc(int color);
@@ -36,17 +36,18 @@ int file_of(int square);
3636

3737
int relative_rank(int color, int rank);
3838

39-
typedef uint16_t move_t; // move encoding: from:6, to:6, prom: 4 (NB_PIECE if none)
39+
typedef uint16_t move_t; // move encoding: from:6, to:6, prom: 4 (NB_PIECE if none)
4040

4141
move_t move_build(int from, int to, int prom);
4242
int move_from(move_t m);
4343
int move_to(move_t m);
4444
int move_prom(move_t m);
4545

46-
typedef uint64_t bitboard_t; // bitfield to represent a set of squares
46+
typedef uint64_t bitboard_t; // bitfield to represent a set of squares
4747

4848
extern bitboard_t Rank[NB_RANK], File[NB_FILE];
49-
extern bitboard_t PawnAttacks[NB_COLOR][NB_SQUARE], KnightAttacks[NB_SQUARE], KingAttacks[NB_SQUARE];
49+
extern bitboard_t PawnAttacks[NB_COLOR][NB_SQUARE], KnightAttacks[NB_SQUARE],
50+
KingAttacks[NB_SQUARE];
5051
extern bitboard_t Segment[NB_SQUARE][NB_SQUARE];
5152
extern bitboard_t Ray[NB_SQUARE][NB_SQUARE];
5253

0 commit comments

Comments
 (0)