-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitboard.cs
80 lines (67 loc) · 2.06 KB
/
Bitboard.cs
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
using System.Numerics;
using System.Runtime.CompilerServices;
namespace Puffin
{
internal struct Bitboard
{
public ulong Value { get; private set; }
public Bitboard()
{
Value = 0;
}
public Bitboard(ulong board)
{
Value = board;
}
public void Reset()
{
Value = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetBit(int square)
{
Value |= 1UL << square;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ResetBit(int square)
{
Value &= ~(1ul << square);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ClearLSB()
{
Value &= Value - 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetLSB()
{
return BitOperations.TrailingZeroCount(Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetMSB()
{
return 63 - BitOperations.LeadingZeroCount(Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CountBits()
{
return BitOperations.PopCount(Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong RightShift()
{
return (Value & ~Constants.FILE_MASKS[(int)File.H]) << 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int LSB(ulong value)
{
return BitOperations.TrailingZeroCount(value);
}
public static Bitboard operator &(Bitboard a, ulong b) => new(a.Value & b);
public static Bitboard operator &(Bitboard a, Bitboard b) => new(a.Value & b.Value);
public static Bitboard operator |(Bitboard a, Bitboard b) => new(a.Value | b.Value);
public static bool operator ==(Bitboard a, Bitboard b) => a.Value == b.Value;
public static bool operator !=(Bitboard a, Bitboard b) => a.Value != b.Value;
public static implicit operator bool(Bitboard a) => a.Value != 0;
}
}