Change macro to function for saftey, and style

master
Tait Hoyem 5 years ago
parent 50763bd2b5
commit 9fcf3b8a98

@ -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);
}

@ -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 <unordered_set>
// 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

@ -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

@ -1,6 +1,7 @@
#include <constants.h>
#include <bitwise.h>
#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);
}

Loading…
Cancel
Save