From 9fcf3b8a98c66ec3b75759b44a0d5d4478dfc604 Mon Sep 17 00:00:00 2001 From: Tait Hoyem <44244401+TTWNO@users.noreply.github.com> Date: Thu, 18 Apr 2019 01:20:13 +0000 Subject: [PATCH] Change macro to function for saftey, and style --- src/bitwise.cpp | 54 +++++++++++++++++++++++++++++++++++++++++ src/bitwise.h | 24 +++++++++++++++--- tests/Makefile | 2 +- tests/bitwise_tests.cpp | 29 +++++++++++----------- 4 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 src/bitwise.cpp diff --git a/src/bitwise.cpp b/src/bitwise.cpp new file mode 100644 index 0000000..4fc311f --- /dev/null +++ b/src/bitwise.cpp @@ -0,0 +1,54 @@ +#include "bitwise.h" + +// Get first 6 bits of int +int get_from_sq(int mv){ + return (mv & 0x3f); +} + +// Get bits 7-12 of int +int get_to_sq(int mv){ + return ((mv >> 6) & 0x3f); +} + +// Get bits 13-16 of int +int get_captured_pc(int mv){ + return ((mv >> 12) & 0xf); +} + +// Get bits 17-20 of int +int get_promoted_to_pc(int mv){ + return ((mv >> 16) & 0xf); +} + +// Get 21st bit of int +int get_en_pass_flag(int mv){ + return ((mv >> 20) & 0x1); +} + +// Get 22nd bit of int +int get_pawn_st_flag(int mv){ + return ((mv >> 21) & 0x1); +} + +// Get 23rd bit of int +int get_castle_flag(int mv){ + return ((mv >> 22) & 0x1); +} + +// Get last 3 bits of number (the rank). +// Techincally this gets all bits 4-32/64 but I'm just assuming it won't be larger than 63 before the shift. +int get_rank(int position){ + return (position >> 3); +} + +// Get first 3 bits of number +int get_file(int position){ + return (position & 7); +} + +// if the position has a 7th bit we know it's off the board. +// it indicates that is it off the board. +// ASSUMING position <= 128 but if it was that big that'd be weird. +bool is_valid_position(int position){ + return (position & 0x40); +} diff --git a/src/bitwise.h b/src/bitwise.h index fed74b4..e5cd126 100644 --- a/src/bitwise.h +++ b/src/bitwise.h @@ -1,10 +1,12 @@ #ifndef BITWISE_H #define BITWISE_H -// Using macros for ease of use, can also use inline functons, but I don't see the point. +#include + +// Using macros for ease of use, can also use functons, but I don't see the point. /* - * From (Position): 6 bits (2^6 == 64) possibilities + * From (Position): 7 bits (2^6 == 64) possibilities * To (Position): same as above * Captured piece, if any: 4 bits (16) possibilities * Promoted to, if any: 4 bits (16) possibilities @@ -13,7 +15,7 @@ * castle move flag: 1 bit * * 0000 0000 0000 0000 0011 1111 -> From square position (& 0x3F) - * 0000 0000 0000 1111 1100 0000 -> To square position (>> 6 & 0x3F) + * 0000 0000 0000 0111 1100 0000 -> To square position (>> 6 & 0x3F) * 0000 0000 1111 0000 0000 0000 -> captured piece, if any (>> 12 & 0xF) * 0000 1111 0000 0000 0000 0000 -> if prmoted, what to? (>> 16 & 0xF) * 0001 0000 0000 0000 0000 0000 -> en passant (& 0x100000) @@ -21,6 +23,7 @@ * 0100 0000 0000 0000 0000 0000 -> castle move (& 0x400000) * */ +/* OLD, DO NOT USE #define FROMSQ(m) ((m) & 0x3f) #define TOSQ(m) ((m>>6) & 0x3f) #define CAPT(m) ((m>>12) & 0xf ) @@ -28,5 +31,20 @@ #define ENPASS(m) ((m>>20) & 0x1 ) #define PAWNST(m) ((m>>21) & 0x1 ) #define CAST(m) ((m>>22) & 0x1 ) +*/ + +// Redefine as functions for fun :shrug: +int get_from_sq(int mv); +int get_to_sq(int mv); +int get_captured_pc(int mv); +int get_promoted_to_pc(int mv); +int get_en_pass_flag(int mv); +int get_pawn_st_flag(int mv); +int get_castle_flag(int mv); + +int get_rank(int position); +int get_file(int position); + +bool is_valid_position(int position); #endif diff --git a/tests/Makefile b/tests/Makefile index a038e1c..648e44c 100755 --- a/tests/Makefile +++ b/tests/Makefile @@ -8,7 +8,7 @@ custom_printing.o: g++ -w -I../src/ -c -o custom_printing.o custom_printing.cpp bitwise.out: catch_main.o - g++ -I../src/ -o bitwise.out catch_main.o bitwise_tests.cpp + g++ -I../src/ -o bitwise.out ../src/bitwise.cpp catch_main.o bitwise_tests.cpp # TODO: Allw all.out to contain bitwise tests all.out: catch_main.o custom_printing.o diff --git a/tests/bitwise_tests.cpp b/tests/bitwise_tests.cpp index f26ed01..04647ff 100644 --- a/tests/bitwise_tests.cpp +++ b/tests/bitwise_tests.cpp @@ -1,6 +1,7 @@ #include #include #include "catch.hpp" + /* * From (Position): 6 bits (2^6 == 64) possibilities * To (Position): same as above @@ -39,10 +40,10 @@ const unsigned int MOVE_G1_TO_H1 = 0xffe; const unsigned int MOVE_H1_TO_G1 = 0b111110111111; // Move from H1 to G1 and capture -const unsigned int MOVE_H1_TO_G1_CAPTURE_B_KNIGHT = 0x8fbf; +const unsigned int MOVE_H1_TO_G1_get_captured_pcURE_B_KNIGHT = 0x8fbf; // Move from H1 to G1 and promote -const unsigned int MOVE_H1_TO_G1_PROMOTE_TO_QUEEN = 0xb0fbf; +const unsigned int MOVE_H1_TO_G1_get_promoted_to_pcOTE_TO_QUEEN = 0xb0fbf; // Move from H1 to G1 and en passant const unsigned int MOVE_H1_TO_G1_EN_PASSANT = 0x100fbf; @@ -51,7 +52,7 @@ const unsigned int MOVE_H1_TO_G1_EN_PASSANT = 0x100fbf; const unsigned int MOVE_H1_TO_G1_PAWN_START = 0x200fbf; // Move from H1 to G1 and castle -const unsigned int MOVE_H1_TO_G1_AND_CASTLE = 0x400fbf; +const unsigned int MOVE_H1_TO_G1_AND_get_castle_flagLE = 0x400fbf; // Want: // From: E3 (21) [0x15] [0x15] <0x15> @@ -66,19 +67,19 @@ const unsigned int MOVE_H1_TO_G1_AND_CASTLE = 0x400fbf; const unsigned int GET_ALL_INT = 0x1003DF; TEST_CASE("Test that bitwise operators return appropriate values. Move.from", "[bitwise_from_pos]"){ - CHECK(FROMSQ(MOVE_G1) == Position::H1); - CHECK(FROMSQ(MOVE_H1) == Position::G1); - CHECK(TOSQ(MOVE_G1_TO_H1) == Position::H1); - CHECK(FROMSQ(MOVE_G1_TO_H1) == Position::G1); + CHECK(get_from_sq(MOVE_G1) == Position::H1); + CHECK(get_from_sq(MOVE_H1) == Position::G1); + CHECK(get_to_sq(MOVE_G1_TO_H1) == Position::H1); + CHECK(get_from_sq(MOVE_G1_TO_H1) == Position::G1); - CHECK(TOSQ(MOVE_H1_TO_G1) == Position::G1); - CHECK(FROMSQ(MOVE_H1_TO_G1) == Position::H1); + CHECK(get_to_sq(MOVE_H1_TO_G1) == Position::G1); + CHECK(get_from_sq(MOVE_H1_TO_G1) == Position::H1); - CHECK(CAPT(MOVE_H1_TO_G1_CAPTURE_B_KNIGHT) == PieceType::B_KNIGHT); - CHECK(PROM(MOVE_H1_TO_G1_PROMOTE_TO_QUEEN) == PieceType::B_QUEEN); + CHECK(get_captured_pc(MOVE_H1_TO_G1_get_captured_pcURE_B_KNIGHT) == PieceType::B_KNIGHT); + CHECK(get_promoted_to_pc(MOVE_H1_TO_G1_get_promoted_to_pcOTE_TO_QUEEN) == PieceType::B_QUEEN); - CHECK(ENPASS(MOVE_H1_TO_G1_EN_PASSANT) == 1); - CHECK(PAWNST(MOVE_H1_TO_G1_PAWN_START) == 1); - CHECK(CAST(MOVE_H1_TO_G1_AND_CASTLE) == 1); + CHECK(get_en_pass_flag(MOVE_H1_TO_G1_EN_PASSANT) == 1); + CHECK(get_pawn_st_flag(MOVE_H1_TO_G1_PAWN_START) == 1); + CHECK(get_castle_flag(MOVE_H1_TO_G1_AND_get_castle_flagLE) == 1); }