Skip to content

Commit

Permalink
feat: exploit known info and beat libataxx
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed May 26, 2024
1 parent fa8521a commit ea874eb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions ataxx/src/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl BitBoard {
/// ```
#[inline(always)]
pub fn lsb(self) -> Square {
Square::try_from(self.0.trailing_zeros()).unwrap()
Square::unsafe_from(self.0.trailing_zeros())
}

/// get_msb returns the most significant Square from the BitBoard.
Expand All @@ -372,7 +372,7 @@ impl BitBoard {
/// ```
#[inline(always)]
pub fn msb(self) -> Square {
Square::try_from(63 - self.0.leading_zeros()).unwrap()
Square::unsafe_from(63 - self.0.leading_zeros())
}

pub fn singles(self) -> BitBoard {
Expand Down
8 changes: 4 additions & 4 deletions ataxx/src/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ impl Move {
#[inline(always)]
#[rustfmt::skip]
pub fn source(self) -> Square {
Square::try_from(
Square::unsafe_from(
(self.0 >> Move::SOURCE_OFFSET) & Move::SOURCE_MASK
).unwrap()
)
}

/// Target returns the target Square of the moving piece.
Expand All @@ -116,9 +116,9 @@ impl Move {
#[inline(always)]
#[rustfmt::skip]
pub fn target(self) -> Square {
Square::try_from(
Square::unsafe_from(
(self.0 >> Move::TARGET_OFFSET) & Move::TARGET_MASK
).unwrap()
)
}

/// is_single checks if the given Move is singular in nature. The result of this
Expand Down
4 changes: 2 additions & 2 deletions ataxx/src/piece.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum Piece {

// Implement conversions from numerical types.
type_macros::impl_from_integer_for_enum! {
for Piece:
for Piece Piece::N + 1 =>

// unsigned integers
usize, Piece::from_usize;
Expand All @@ -59,7 +59,7 @@ impl ops::Not for Piece {
/// not implements the not unary operator (!) which switches the current Piece
/// to its opposite, i.e. [`Piece::Black`] to [`Piece::White`] and vice versa.
fn not(self) -> Self::Output {
Piece::try_from(self as usize ^ 1).unwrap()
Piece::unsafe_from(self as usize ^ 1)
}
}

Expand Down
24 changes: 12 additions & 12 deletions ataxx/src/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Square {
/// assert_eq!(Square::new(File::A, Rank::First), Square::A1);
/// ```
pub fn new(file: File, rank: Rank) -> Square {
Square::try_from(rank as usize * File::N + file as usize).unwrap()
Square::unsafe_from(rank as usize * File::N + file as usize)
}

/// file returns the File of the current Square.
Expand All @@ -56,7 +56,7 @@ impl Square {
/// ```
#[inline(always)]
pub fn file(self) -> File {
File::try_from(self as usize % File::N).unwrap()
File::unsafe_from(self as usize % File::N)
}

/// rank returns the Rank of the current Square.
Expand All @@ -67,7 +67,7 @@ impl Square {
/// ```
#[inline(always)]
pub fn rank(self) -> Rank {
Rank::try_from(self as usize / File::N).unwrap()
Rank::unsafe_from(self as usize / File::N)
}

/// north returns the Square to the North of this one.
Expand All @@ -77,7 +77,7 @@ impl Square {
/// assert_eq!(Square::D4.north(), Square::D5);
/// ```
pub fn north(self) -> Self {
Square::try_from(self as usize + File::N).unwrap()
Square::unsafe_from(self as usize + File::N)
}

/// south returns the Square to the South of this one.
Expand All @@ -87,7 +87,7 @@ impl Square {
/// assert_eq!(Square::D4.south(), Square::D3);
/// ```
pub fn south(self) -> Self {
Square::try_from(self as usize - File::N).unwrap()
Square::unsafe_from(self as usize - File::N)
}

/// east returns the Square to the East of this one.
Expand All @@ -97,7 +97,7 @@ impl Square {
/// assert_eq!(Square::D4.east(), Square::E4);
/// ```
pub fn east(self) -> Self {
Square::try_from(self as usize + 1).unwrap()
Square::unsafe_from(self as usize + 1)
}

/// west returns the Square to the West of this one.
Expand All @@ -107,7 +107,7 @@ impl Square {
/// assert_eq!(Square::D4.west(), Square::C4);
/// ```
pub fn west(self) -> Self {
Square::try_from(self as usize - 1).unwrap()
Square::unsafe_from(self as usize - 1)
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ impl FromStr for Square {

// Implement from and into traits for all primitive integer types.
type_macros::impl_from_integer_for_enum! {
for Square:
for Square Square::N =>

// Unsigned Integers.
usize, Square::from_usize;
Expand Down Expand Up @@ -243,13 +243,13 @@ impl FromStr for File {
return Err(FileParseError::InvalidFileString);
}

Ok(File::try_from(ident - b'a').unwrap())
Ok(File::unsafe_from(ident - b'a'))
}
}

// Implement from and into traits for all primitive integer types.
type_macros::impl_from_integer_for_enum! {
for File:
for File File::N =>

// Unsigned Integers.
usize, File::from_usize;
Expand Down Expand Up @@ -336,13 +336,13 @@ impl FromStr for Rank {
return Err(RankParseError::InvalidRankString);
}

Ok(Rank::try_from(ident - b'1').unwrap())
Ok(Rank::unsafe_from(ident - b'1'))
}
}

// Implement from and into traits for all primitive integer types.
type_macros::impl_from_integer_for_enum! {
for Rank:
for Rank Rank::N =>

// Unsigned Integers.
usize, Rank::from_usize;
Expand Down
14 changes: 12 additions & 2 deletions ataxx/src/type_macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_export]
#[doc(hidden)]
macro_rules! impl_from_integer_for_enum {
(for $type:ident: $($num:ident, $fn:path;)*) => {$(
(for $type:ident $size:expr => $($num:ident, $fn:path;)*) => {$(
impl TryFrom<$num> for $type {
type Error = ();
#[inline(always)]
Expand All @@ -19,7 +19,17 @@ macro_rules! impl_from_integer_for_enum {
number as Self
}
}
)*};
)*

impl $type {
#[inline(always)]
pub fn unsafe_from<T: num_traits::ToPrimitive>(number: T) -> Self {
debug_assert!(number.to_u64().unwrap() < $size as u64);
unsafe {
std::mem::transmute_copy(&number)
}
}
}};
}

pub use impl_from_integer_for_enum;
Expand Down

0 comments on commit ea874eb

Please sign in to comment.