Skip to content

Commit

Permalink
Inlined everything :/
Browse files Browse the repository at this point in the history
  • Loading branch information
sfleischman105 committed Nov 28, 2017
1 parent 46068b3 commit 443e626
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pleco/benches/bit_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn bench_popcount_1000_rust(b: &mut Bencher) {
b.iter(|| {
black_box({
for bits in BIT_SETS_DENSE_1000.iter() {
black_box({black_box((*bits).0).count_ones();})
black_box({black_box(black_box((*bits).0)).count_ones();})
}
})
})
Expand Down
10 changes: 10 additions & 0 deletions pleco/src/board/piece_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn place(&mut self, square: SQ, player: Player, piece: Piece) {
assert!(square.is_okay());
self.data[square.0 as usize] = self.create_sq(player, piece);
Expand All @@ -59,6 +60,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn remove(&mut self, square: SQ) {
assert!(square.is_okay());
self.data[square.0 as usize] = 0b0111
Expand All @@ -69,6 +71,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if square is of index higher than 63.
#[inline]
pub fn piece_at(&self, square: SQ) -> Option<Piece> {
assert!(square.is_okay());
let byte: u8 = self.data[square.0 as usize] & 0b0111;
Expand All @@ -93,6 +96,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn piece_at_for_player(&self, square: SQ, player: Player) -> Option<Piece> {
let op = self.player_piece_at(square);
if op.is_some() {
Expand All @@ -112,6 +116,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn player_at(&self, square: SQ) -> Option<Player> {
let byte: u8 = self.data[square.0 as usize];
if byte == 0b0111 || byte == 0b1111 {
Expand All @@ -130,6 +135,7 @@ impl PieceLocations {
/// # Panics
///
/// Panics if Square is of index higher than 63.
#[inline]
pub fn player_piece_at(&self, square: SQ) -> Option<(Player, Piece)> {
let byte: u8 = self.data[square.0 as usize];
match byte {
Expand All @@ -153,13 +159,15 @@ impl PieceLocations {
}

/// Returns if there is a `SQ` is occupied.
#[inline]
pub fn at_square(&self, square: SQ) -> bool {
assert!(square.is_okay());
let byte: u8 = self.data[square.0 as usize];
byte != 0b0111 && byte != 0b1111
}

/// Returns the first square (if any) that a piece / player is at.
#[inline]
pub fn first_square(&self, piece: Piece, player: Player) -> Option<SQ> {
let target = self.create_sq(player, piece);
for x in 0..64 {
Expand All @@ -171,6 +179,7 @@ impl PieceLocations {
}

/// Returns if the Board contains a particular piece / player.
#[inline]
pub fn contains(&self, piece: Piece, player: Player) -> bool {
self.first_square(piece,player).is_some()
}
Expand Down Expand Up @@ -221,6 +230,7 @@ impl PieceLocations {


/// Helper method to return the bit representation of a given piece and player.
#[inline]
fn create_sq(&self, player: Player, piece: Piece) -> u8 {
let mut loc: u8 = match piece {
Piece::P => 0b0000,
Expand Down
1 change: 1 addition & 0 deletions pleco/src/core/bit_twiddles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ pub fn reverse_bytes(b: u64) -> u64 {
/// let reverse = 0b10000000;
/// assert_eq!(reverse, reverse_byte(x));
/// ```
#[inline]
pub fn reverse_byte(b: u8) -> u8 {
let m: u8 = ((0b0000_0001 & b) << 7) | ((0b0000_0010 & b) << 5) | ((0b0000_0100 & b) << 3) |
((0b0000_1000 & b) << 1) |
Expand Down
2 changes: 2 additions & 0 deletions pleco/src/core/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl BitBoard {
impl Shl<SQ> for BitBoard {
type Output = BitBoard;

#[inline(always)]
fn shl(self, rhs: SQ) -> BitBoard {
BitBoard((self.0).wrapping_shl(rhs.0 as u32))
}
Expand All @@ -193,6 +194,7 @@ impl Shl<SQ> for BitBoard {
impl Iterator for BitBoard {
type Item = SQ;

#[inline(always)]
fn next(&mut self) -> Option<Self::Item> {
if self.is_empty() {
None
Expand Down
48 changes: 48 additions & 0 deletions pleco/src/core/mono_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,95 +79,131 @@ pub struct WhiteType {}
pub struct BlackType {}

impl PlayerTrait for WhiteType {
#[inline(always)]
fn player() -> Player {
Player::White
}
#[inline(always)]
fn opp_player() -> Player {
Player::Black
}

#[inline(always)]
fn down(sq: SQ) -> SQ { sq - SQ(8) }

#[inline(always)]
fn up(sq: SQ) -> SQ { sq + SQ(8) }

#[inline(always)]
fn left(sq: SQ) -> SQ { sq - SQ(1) }

#[inline(always)]
fn right(sq: SQ) -> SQ { sq + SQ(1) }

#[inline(always)]
fn down_left(sq: SQ) -> SQ { sq - SQ(9) }

#[inline(always)]
fn down_right(sq: SQ) -> SQ { sq - SQ(7) }

#[inline(always)]
fn up_left(sq: SQ) -> SQ { sq + SQ(7) }

#[inline(always)]
fn up_right(sq: SQ) -> SQ { sq + SQ(9) }

#[inline(always)]
fn shift_down(bb: BitBoard) -> BitBoard { bb >> 8 }

#[inline(always)]
fn shift_up(bb: BitBoard) -> BitBoard { bb << 8 }

#[inline(always)]
fn shift_left(bb: BitBoard) -> BitBoard { (bb & !BitBoard::FILE_A) >> 1 }

#[inline(always)]
fn shift_right(bb: BitBoard) -> BitBoard { (bb & !BitBoard::FILE_H) << 1 }

#[inline(always)]
fn shift_down_left(bb: BitBoard) -> BitBoard { (bb & !BitBoard::FILE_A) >> 9 }

#[inline(always)]
fn shift_down_right(bb: BitBoard) -> BitBoard { (bb & !BitBoard::FILE_H) >> 7 }

#[inline(always)]
fn shift_up_left(bb: BitBoard) -> BitBoard { (bb & !BitBoard::FILE_A) << 7 }

#[inline(always)]
fn shift_up_right(bb: BitBoard) -> BitBoard { (bb & !BitBoard::FILE_H) << 9 }
}

impl PlayerTrait for BlackType {
#[inline(always)]
fn player() -> Player {
Player::Black
}

#[inline(always)]
fn opp_player() -> Player {
Player::White
}

#[inline(always)]
fn down(sq: SQ) -> SQ { sq + SQ(8) }

#[inline(always)]
fn up(sq: SQ) -> SQ { sq - SQ(8) }

#[inline(always)]
fn left(sq: SQ) -> SQ { sq + SQ(1) }

#[inline(always)]
fn right(sq: SQ) -> SQ { sq - SQ(1) }

#[inline(always)]
fn down_left(sq: SQ) -> SQ { sq + SQ(9) }

#[inline(always)]
fn down_right(sq: SQ) -> SQ { sq + SQ(7) }

#[inline(always)]
fn up_left(sq: SQ) -> SQ { sq - SQ(7) }

#[inline(always)]
fn up_right(sq: SQ) -> SQ { sq - SQ(9) }

#[inline(always)]
fn shift_down(bb: BitBoard) -> BitBoard { bb << (8) }

#[inline(always)]
fn shift_up(bb: BitBoard) -> BitBoard { bb >> (8) }

#[inline(always)]
fn shift_left(bb: BitBoard) -> BitBoard {
(bb & !BitBoard::FILE_H) << (1)
}

#[inline(always)]
fn shift_right(bb: BitBoard) -> BitBoard {
(bb & !BitBoard::FILE_A) >> (1)
}

#[inline(always)]
fn shift_down_left(bb: BitBoard) -> BitBoard {
(bb & !BitBoard::FILE_H) << (9)
}

#[inline(always)]
fn shift_down_right(bb: BitBoard) -> BitBoard {
(bb & !BitBoard::FILE_A) << (7)
}

#[inline(always)]
fn shift_up_left(bb: BitBoard) -> BitBoard {
(bb & !BitBoard::FILE_H) >> (7)
}

#[inline(always)]
fn shift_up_right(bb: BitBoard) -> BitBoard {
(bb & !BitBoard::FILE_A) >> (9)
}
Expand Down Expand Up @@ -198,36 +234,42 @@ pub struct EvasionsGenType {}
pub struct NonEvasionsGenType {}

impl GenTypeTrait for AllGenType {
#[inline(always)]
fn gen_type() -> GenTypes {
GenTypes::All
}
}

impl GenTypeTrait for CapturesGenType {
#[inline(always)]
fn gen_type() -> GenTypes {
GenTypes::Captures
}
}

impl GenTypeTrait for QuietsGenType {
#[inline(always)]
fn gen_type() -> GenTypes {
GenTypes::Quiets
}
}

impl GenTypeTrait for QuietChecksGenType {
#[inline(always)]
fn gen_type() -> GenTypes {
GenTypes::QuietChecks
}
}

impl GenTypeTrait for EvasionsGenType {
#[inline(always)]
fn gen_type() -> GenTypes {
GenTypes::Evasions
}
}

impl GenTypeTrait for NonEvasionsGenType {
#[inline(always)]
fn gen_type() -> GenTypes {
GenTypes::NonEvasions
}
Expand Down Expand Up @@ -258,36 +300,42 @@ pub struct QueenType {}
pub struct KingType {}

impl PieceTrait for PawnType {
#[inline(always)]
fn piece_type() -> Piece {
Piece::P
}
}

impl PieceTrait for KnightType {
#[inline(always)]
fn piece_type() -> Piece {
Piece::N
}
}

impl PieceTrait for BishopType {
#[inline(always)]
fn piece_type() -> Piece {
Piece::B
}
}

impl PieceTrait for RookType {
#[inline(always)]
fn piece_type() -> Piece {
Piece::R
}
}

impl PieceTrait for QueenType {
#[inline(always)]
fn piece_type() -> Piece {
Piece::Q
}
}

impl PieceTrait for KingType {
#[inline(always)]
fn piece_type() -> Piece {
Piece::K
}
Expand Down
3 changes: 3 additions & 0 deletions pleco/src/core/move_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct MoveList {
}

impl Default for MoveList {
#[inline]
fn default() -> Self {
MoveList {
inner: [BitMove::null(); 256],
Expand Down Expand Up @@ -150,6 +151,7 @@ impl<'a> IntoIterator for &'a MoveList {
type Item = BitMove;
type IntoIter = MoveIter<'a>;

#[inline]
fn into_iter(self) -> Self::IntoIter {
MoveIter {
movelist: &self,
Expand Down Expand Up @@ -202,6 +204,7 @@ impl IntoIterator for MoveList {
type Item = BitMove;
type IntoIter = MoveIntoIter;

#[inline]
fn into_iter(self) -> Self::IntoIter {
MoveIntoIter {
movelist: self,
Expand Down
6 changes: 5 additions & 1 deletion pleco/src/core/piece_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl BitMove {
///
/// Using this method cannot guarantee that the move is legal. The input bits must be encoding a legal
/// move, or else there is Undefined Behavior if the resulting BitMove is used.
#[inline]
pub fn new(input: u16) -> BitMove {
BitMove { data: input }
}
Expand Down Expand Up @@ -174,14 +175,16 @@ impl BitMove {
/// # Safety
///
/// A Null move is never a valid move to play. Using a Null move should onl be used for search and
/// evaluation purposes.
/// evaluation purposes.
#[inline]
pub fn null() -> Self {
BitMove { data: 0 }
}

/// Returns if a [BitMove] is a Null Move.
///
/// See [BitMove::null()] for more information on Null moves.
#[inline]
pub fn is_null(&self) -> bool {
self.data == 0
}
Expand Down Expand Up @@ -336,6 +339,7 @@ impl BitMove {
}

/// Returns the raw number representation of the move.
#[inline]
pub fn get_raw(&self) -> u16 {
self.data
}
Expand Down
1 change: 1 addition & 0 deletions pleco/src/core/sq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl SQ {
/// assert_eq!(a1.distance(b2), 1);
/// assert_eq!(a1.distance(b3), 2);
/// ```
#[inline]
pub fn distance(self, sq_other: SQ) -> u8 {
let x = diff(self.rank_idx_of_sq(), sq_other.rank_idx_of_sq());
let y = diff(self.file_idx_of_sq(), sq_other.file_idx_of_sq());
Expand Down
Loading

0 comments on commit 443e626

Please sign in to comment.