forked from stevemaughan/maverick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbittwiddle.h
93 lines (76 loc) · 1.84 KB
/
bittwiddle.h
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
//===========================================================//
//
// Maverick Chess Engine
// Copyright 2013 Steve Maughan
//
//===========================================================//
#if defined(MINGW64)
static inline t_chess_square bitscan(t_bitboard b) {
t_bitboard index;
__asm__("bsfq %1, %0": "=r"(index): "rm"(b) );
return (t_chess_square)index;
}
static inline void prefetch(struct t_move_record *move) {
__builtin_prefetch(move, 0, 3);
}
#if defined (POPCOUNT)
static inline int popcount(t_bitboard b)
{
return __builtin_popcountll(b);
}
#else
static inline int popcount(t_bitboard b)
{
int i = 0;
t_bitboard a = b;
while (a) {
i++;
a &= (a - 1);
}
return(i);
}
#endif
static inline t_chess_square bitscan_reset(t_bitboard *b)
{
//t_bitboard c = *b ^ (*b - 1);
//*b &= (*b - 1);
//unsigned int folded = (int) (c ^ (c >> 32));
//return bitscan_table[folded * 0x78291ACF >> 26];
//t_chess_square s = bitscan(*b);
//*b ^= SQUARE64(s);
//return s;
t_bitboard index;
__asm__("bsfq %1, %0": "=r"(index): "rm"(*b) );
*b &= (*b - 1);
return (t_chess_square)index;
}
#else
inline void prefetch(struct t_move_record *move) {
}
static inline t_chess_square bitscan(t_bitboard b)
{
b ^= (b - 1);
unsigned int folded = (int) (b ^ (b >> 32));
return bitscan_table[folded * 0x78291ACF >> 26];
}
static inline int popcount(t_bitboard b)
{
int i = 0;
t_bitboard a = b;
while (a) {
i++;
a &= (a - 1);
}
return(i);
}
static inline t_chess_square bitscan_reset(t_bitboard *b)
{
t_bitboard c = *b ^ (*b - 1);
*b &= (*b - 1);
unsigned int folded = (int) (c ^ (c >> 32));
return bitscan_table[folded * 0x78291ACF >> 26];
}
#endif
static inline BOOL is_bit_set(t_bitboard b, int i) {
return ((SQUARE64(i) & b) != 0);
}