-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbits.h
65 lines (51 loc) · 1.23 KB
/
bits.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
#ifndef BITS_H
#define BITS_H
#ifdef _MSC_VER
#ifdef _64BIT
#include <smmintrin.h>
#include <intrin.h>
# define builtin_popcount _mm_popcnt_u64
# define builtin_lsb _BitScanForward64
#else
# include <intrin.h>
# define builtin_popcount __popcnt
# define builtin_lsb _BitScanForward
#endif
#else
# define builtin_popcount __builtin_popcountll
#endif
#include <iostream>
#include "types.h"
#include "bitboards.h"
namespace bits {
inline void print(const U64& b) {
printf("+---+---+---+---+---+---+---+---+\n");
for (Row r = r8; r >= r1; --r) {
for (Col c = A; c <= H; ++c) {
U64 s = bitboards::squares[8 * r + c];
std::cout << (b & s ? "| X " : "| ");
}
std::cout << "|" << std::endl;
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
}
std::cout << " a b c d e f g h" << std::endl;
}
inline int count(const U64& b) { return builtin_popcount(b); }
inline int lsb(U64& b) {
#ifdef _MSC_VER
unsigned long r = 0; builtin_lsb(&r, b);
return r;
#else
return __builtin_ffsll(b) - 1; // needs benchmarking
#endif
}
inline int pop_lsb(U64& b) {
const int s = lsb(b);
b &= (b - 1);
return s;
}
inline bool more_than_one(const U64& b) {
return b & (b - 1);
}
}
#endif