-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitboard.c
133 lines (111 loc) · 2.3 KB
/
bitboard.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bitboard.h"
#include <time.h>
Bitboard128 newBitboard96()
{
Bitboard128 board = 0;
return board;
}
Bitboard128 oneHotBitboard96(int hot_index)
{
return (Bitboard128)1 << hot_index;
}
int is_empty(Bitboard128 *bb)
{
return bb == 0;
}
int lowest_set_bit(__int128 num)
{
if (num == 0)
{
return -1; // No set bits
}
int position = 0;
uint64_t highPart = (uint64_t)(num >> 64); // Extract upper 64 bits
uint64_t lowPart = (uint64_t)num; // Extract lower 64 bits
if (lowPart != 0)
{
position = __builtin_ctzll(lowPart);
}
else
{
position = 64 + __builtin_ctzll(highPart);
}
return position;
}
int highest_set_bit(__int128 num)
{
if (num == 0)
{
return -1; // No set bits
}
int position = 0;
uint64_t highPart = (uint64_t)(num >> 64); // Extract upper 64 bits
uint64_t lowPart = (uint64_t)num; // Extract lower 64 bits
if (highPart != 0)
{
position = 64 + __builtin_clzll(highPart);
}
else
{
position = __builtin_clzll(lowPart);
}
return 127 - position;
}
int index_of_fist_one(Bitboard128 bb)
{
return lowest_set_bit(bb);
}
void set_bit(Bitboard128 *board, int index)
{
*board |= (__int128_t)1 << index;
}
void clear_bit(Bitboard128 *board, int index)
{
*board &= ~((__int128_t)1 << index);
}
int is_bit_set(Bitboard128 *board, int index)
{
return (*board & ((__int128_t)1 << index)) != 0;
}
void fill_with_noise(Bitboard128 *board)
{
// TODO implement
board = (__int128_t)rand() << 32 | rand();
}
int count_ones(Bitboard128 num)
{
int count = 0;
while (num > 0)
{
count += __builtin_popcountll((uint64_t)num);
num >>= 64;
}
return count;
}
void pprint_bitboard128(Bitboard128 board, char symbol, int start, int end, int cols)
{
int row_index = 0;
for (int i = start; i < end; i++)
{
if (is_bit_set(&board, i) == 1)
{
printf(" %c ", symbol);
}
else
{
printf(" _ ");
}
if (row_index == cols - 1)
{
printf("\n");
row_index = 0;
}
else
{
row_index += 1;
}
}
}