aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-02-02 19:38:23 +0300
committereug-vs <eugene@eug-vs.xyz>2023-02-02 19:38:23 +0300
commit61b3cf2133c359034cbd885921cf04f88936f622 (patch)
tree1e0955f929069d866d24fa132d03e676ae6728e0
parent847cdaf46737d2b914971177a4afa05bbe13243b (diff)
downloadchessnost-61b3cf2133c359034cbd885921cf04f88936f622.tar.gz
perf: faster implementation of pop_count
-rw-r--r--src/bitboard.rs10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/bitboard.rs b/src/bitboard.rs
index d7a4fdb..3df4902 100644
--- a/src/bitboard.rs
+++ b/src/bitboard.rs
@@ -22,11 +22,13 @@ pub fn print(bb: Bitboard, title: &str) {
}
/// Return bitboard cardinality, aka number of elements in the set
-pub fn pop_count(bb: Bitboard) -> u8 {
- if bb == 0 {
- return 0;
+pub fn pop_count(mut bb: Bitboard) -> u8 {
+ let mut count = 0;
+ while bb > 0 {
+ count += 1;
+ bb &= bb - 1;
}
- return pop_count(bb >> 1) + (bb & 1) as u8;
+ return count;
}
/// Return Bitboard with only Least Single Bit