Skip to content

Commit

Permalink
Reimplement newBitboard to avoid string operations. (#75)
Browse files Browse the repository at this point in the history
Instead of building a string of "0"s and "1"s and then converting it,
newBitboard now uses bit arithmetic to convert a map to a uint64. This
avoids memory allocation and a bunch of string operations.
  • Loading branch information
tov authored Nov 25, 2024
1 parent 61dffef commit 6d754fa
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions bitboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ import (
type bitboard uint64

func newBitboard(m map[Square]bool) bitboard {
s := ""
var bb uint64
for sq := 0; sq < numOfSquaresInBoard; sq++ {
bb <<= 1
if m[Square(sq)] {
s += "1"
} else {
s += "0"
bb |= 1
}
}
bb, err := strconv.ParseUint(s, 2, 64)
if err != nil {
panic(err)
}
return bitboard(bb)
}

Expand Down

0 comments on commit 6d754fa

Please sign in to comment.