chess/
util.rs

1use crate::movegen::{PieceColour, PieceType, Square};
2use crate::BoardState;
3
4#[allow(dead_code)]
5pub fn print_board(bs: &BoardState) {
6    let pawn = " ♙ ";
7    let knight = " ♘ ";
8    let bishop = " ♗ ";
9    let rook = " ♖ ";
10    let queen = " ♕ ";
11    let king = " ♔ ";
12
13    let bking = " ♚ ";
14    let bqueen = " ♛ ";
15    let brook = " ♜ ";
16    let bbishop = " ♝ ";
17    let bknight = " ♞ ";
18    let bpawn = " ♟︎ ";
19
20    for (num, j) in bs.get_pos64().iter().enumerate() {
21        match j {
22            Square::Piece(p) => match p.pcolour {
23                PieceColour::White => match p.ptype {
24                    PieceType::Pawn => {
25                        print!("{}", pawn);
26                    }
27                    PieceType::Knight => {
28                        print!("{}", knight);
29                    }
30                    PieceType::Bishop => {
31                        print!("{}", bishop);
32                    }
33                    PieceType::Rook => {
34                        print!("{}", rook);
35                    }
36                    PieceType::Queen => {
37                        print!("{}", queen);
38                    }
39                    PieceType::King => {
40                        print!("{}", king);
41                    }
42                },
43                PieceColour::Black => match p.ptype {
44                    PieceType::Pawn => {
45                        print!("{}", bpawn);
46                    }
47                    PieceType::Knight => {
48                        print!("{}", bknight);
49                    }
50                    PieceType::Bishop => {
51                        print!("{}", bbishop);
52                    }
53                    PieceType::Rook => {
54                        print!("{}", brook);
55                    }
56                    PieceType::Queen => {
57                        print!("{}", bqueen);
58                    }
59                    PieceType::King => {
60                        print!("{}", bking);
61                    }
62                },
63            },
64            Square::Empty => {
65                print!(" - ");
66            }
67        }
68
69        // new rank
70        if (num + 1) % 8 == 0 {
71            println!();
72        }
73    }
74}
75
76#[inline]
77pub fn bytes_to_str(size: usize) -> String {
78    let units = ["B", "KiB", "MiB", "GiB", "TiB"];
79    let mut size = size as f64;
80    let mut i = 0;
81    while size >= 1024.0 {
82        size /= 1024.0;
83        i += 1;
84    }
85    format!("{:.2} {}", size, units[i])
86}
87
88// returns the high bits of u64 returning a u32
89#[inline(always)]
90pub const fn high_bits(x: u64) -> u32 {
91    // type casting to u32 truncates the high bits, so shift right by 32 bits and cast to u32
92    (x >> 32) as u32
93}
94
95// returns the low bits of u64 returning a u32
96#[inline(always)]
97pub const fn low_bits(x: u64) -> u32 {
98    // type casting to u32 truncates the high bits
99    x as u32
100}
101
102// return pretty-print string of a hash (full width hex hash)
103#[inline(always)]
104pub fn hash_to_string(hash: u64) -> String {
105    format!("{:016x}", hash)
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn test_bytes_to_str() {
114        assert_eq!(bytes_to_str(1023), "1023.00 B");
115        assert_eq!(bytes_to_str(1024), "1.00 KiB");
116        assert_eq!(bytes_to_str(1048576), "1.00 MiB");
117    }
118
119    #[test]
120    fn test_high_bits() {
121        assert_eq!(high_bits(0x123456789ABCDEF0), 0x12345678);
122        assert_eq!(high_bits(0xFFFFFFFFFFFFFFFF), 0xFFFFFFFF);
123    }
124
125    #[test]
126    fn test_low_bits() {
127        assert_eq!(low_bits(0x123456789ABCDEF0), 0x9ABCDEF0);
128        assert_eq!(low_bits(0xFFFFFFFFFFFFFFFF), 0xFFFFFFFF);
129    }
130
131    #[test]
132    fn test_hash_to_string() {
133        assert_eq!(hash_to_string(0x123456789ABCDEF0), "123456789abcdef0");
134        assert_eq!(hash_to_string(0xFFFFFFFFFFFFFF), "00ffffffffffffff");
135    }
136}